Initial commit
This commit is contained in:
commit
e49726988c
5445 files changed
+1089914
No files matched your search
+2
@@ -0,0 +1,2 @@
|
||||
export declare const dirname: string;
|
||||
//# sourceMappingURL=dirname.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"dirname.d.ts","sourceRoot":"","sources":["../../src/dirname.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,OAAO,QAA+C,CAAC"}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
import { fileURLToPath } from "url";
|
||||
import path from "path";
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore Dual package
|
||||
export const dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
//# sourceMappingURL=dirname.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"dirname.js","sourceRoot":"","sources":["../../src/dirname.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACpC,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,6DAA6D;AAC7D,0BAA0B;AAC1B,MAAM,CAAC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC"}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
export declare const SANS_8_BLACK: string;
|
||||
export declare const SANS_10_BLACK: string;
|
||||
export declare const SANS_12_BLACK: string;
|
||||
export declare const SANS_14_BLACK: string;
|
||||
export declare const SANS_16_BLACK: string;
|
||||
export declare const SANS_32_BLACK: string;
|
||||
export declare const SANS_64_BLACK: string;
|
||||
export declare const SANS_128_BLACK: string;
|
||||
export declare const SANS_8_WHITE: string;
|
||||
export declare const SANS_16_WHITE: string;
|
||||
export declare const SANS_32_WHITE: string;
|
||||
export declare const SANS_64_WHITE: string;
|
||||
export declare const SANS_128_WHITE: string;
|
||||
//# sourceMappingURL=fonts.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"fonts.d.ts","sourceRoot":"","sources":["../../src/fonts.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,YAAY,QAGxB,CAAC;AAEF,eAAO,MAAM,aAAa,QAGzB,CAAC;AAEF,eAAO,MAAM,aAAa,QAGzB,CAAC;AAEF,eAAO,MAAM,aAAa,QAGzB,CAAC;AAEF,eAAO,MAAM,aAAa,QAGzB,CAAC;AAEF,eAAO,MAAM,aAAa,QAGzB,CAAC;AAEF,eAAO,MAAM,aAAa,QAGzB,CAAC;AAEF,eAAO,MAAM,cAAc,QAG1B,CAAC;AAEF,eAAO,MAAM,YAAY,QAGxB,CAAC;AAEF,eAAO,MAAM,aAAa,QAGzB,CAAC;AAEF,eAAO,MAAM,aAAa,QAGzB,CAAC;AAEF,eAAO,MAAM,aAAa,QAGzB,CAAC;AAEF,eAAO,MAAM,cAAc,QAG1B,CAAC"}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
import path from "path";
|
||||
import { dirname } from "./dirname.js";
|
||||
const dir = path.join(dirname, "../");
|
||||
export const SANS_8_BLACK = path.join(dir, "fonts/open-sans/open-sans-8-black/open-sans-8-black.fnt");
|
||||
export const SANS_10_BLACK = path.join(dir, "fonts/open-sans/open-sans-10-black/open-sans-10-black.fnt");
|
||||
export const SANS_12_BLACK = path.join(dir, "fonts/open-sans/open-sans-12-black/open-sans-12-black.fnt");
|
||||
export const SANS_14_BLACK = path.join(dir, "fonts/open-sans/open-sans-14-black/open-sans-14-black.fnt");
|
||||
export const SANS_16_BLACK = path.join(dir, "fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt");
|
||||
export const SANS_32_BLACK = path.join(dir, "fonts/open-sans/open-sans-32-black/open-sans-32-black.fnt");
|
||||
export const SANS_64_BLACK = path.join(dir, "fonts/open-sans/open-sans-64-black/open-sans-64-black.fnt");
|
||||
export const SANS_128_BLACK = path.join(dir, "fonts/open-sans/open-sans-128-black/open-sans-128-black.fnt");
|
||||
export const SANS_8_WHITE = path.join(dir, "fonts/open-sans/open-sans-8-white/open-sans-8-white.fnt");
|
||||
export const SANS_16_WHITE = path.join(dir, "fonts/open-sans/open-sans-16-white/open-sans-16-white.fnt");
|
||||
export const SANS_32_WHITE = path.join(dir, "fonts/open-sans/open-sans-32-white/open-sans-32-white.fnt");
|
||||
export const SANS_64_WHITE = path.join(dir, "fonts/open-sans/open-sans-64-white/open-sans-64-white.fnt");
|
||||
export const SANS_128_WHITE = path.join(dir, "fonts/open-sans/open-sans-128-white/open-sans-128-white.fnt");
|
||||
//# sourceMappingURL=fonts.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"fonts.js","sourceRoot":"","sources":["../../src/fonts.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAEvC,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;AAEtC,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CACnC,GAAG,EACH,yDAAyD,CAC1D,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CACpC,GAAG,EACH,2DAA2D,CAC5D,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CACpC,GAAG,EACH,2DAA2D,CAC5D,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CACpC,GAAG,EACH,2DAA2D,CAC5D,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CACpC,GAAG,EACH,2DAA2D,CAC5D,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CACpC,GAAG,EACH,2DAA2D,CAC5D,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CACpC,GAAG,EACH,2DAA2D,CAC5D,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CACrC,GAAG,EACH,6DAA6D,CAC9D,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAG,IAAI,CAAC,IAAI,CACnC,GAAG,EACH,yDAAyD,CAC1D,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CACpC,GAAG,EACH,2DAA2D,CAC5D,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CACpC,GAAG,EACH,2DAA2D,CAC5D,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CACpC,GAAG,EACH,2DAA2D,CAC5D,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,IAAI,CAAC,IAAI,CACrC,GAAG,EACH,6DAA6D,CAC9D,CAAC"}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
import { HorizontalAlign, VerticalAlign } from "@jimp/core";
|
||||
import { JimpClass } from "@jimp/types";
|
||||
import { z } from "zod";
|
||||
import { BmFont } from "./types.js";
|
||||
export { measureText, measureTextHeight } from "./measure-text.js";
|
||||
export * from "./types.js";
|
||||
declare const PrintOptionsSchema: z.ZodObject<{
|
||||
/** the x position to draw the image */
|
||||
x: z.ZodNumber;
|
||||
/** the y position to draw the image */
|
||||
y: z.ZodNumber;
|
||||
/** the text to print */
|
||||
text: z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodObject<{
|
||||
text: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
|
||||
alignmentX: z.ZodOptional<z.ZodNativeEnum<typeof HorizontalAlign>>;
|
||||
alignmentY: z.ZodOptional<z.ZodNativeEnum<typeof VerticalAlign>>;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
text: string | number;
|
||||
alignmentX?: HorizontalAlign | undefined;
|
||||
alignmentY?: VerticalAlign | undefined;
|
||||
}, {
|
||||
text: string | number;
|
||||
alignmentX?: HorizontalAlign | undefined;
|
||||
alignmentY?: VerticalAlign | undefined;
|
||||
}>]>;
|
||||
/** the boundary width to draw in */
|
||||
maxWidth: z.ZodOptional<z.ZodNumber>;
|
||||
/** the boundary height to draw in */
|
||||
maxHeight: z.ZodOptional<z.ZodNumber>;
|
||||
/** a callback for when complete that ahs the end co-ordinates of the text */
|
||||
cb: z.ZodOptional<z.ZodFunction<z.ZodTuple<[z.ZodObject<{
|
||||
x: z.ZodNumber;
|
||||
y: z.ZodNumber;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
x: number;
|
||||
y: number;
|
||||
}, {
|
||||
x: number;
|
||||
y: number;
|
||||
}>], null>, z.ZodUnknown>>;
|
||||
}, "strip", z.ZodTypeAny, {
|
||||
x: number;
|
||||
y: number;
|
||||
text: string | number | {
|
||||
text: string | number;
|
||||
alignmentX?: HorizontalAlign | undefined;
|
||||
alignmentY?: VerticalAlign | undefined;
|
||||
};
|
||||
maxWidth?: number | undefined;
|
||||
maxHeight?: number | undefined;
|
||||
cb?: ((args_0: {
|
||||
x: number;
|
||||
y: number;
|
||||
}) => unknown) | undefined;
|
||||
}, {
|
||||
x: number;
|
||||
y: number;
|
||||
text: string | number | {
|
||||
text: string | number;
|
||||
alignmentX?: HorizontalAlign | undefined;
|
||||
alignmentY?: VerticalAlign | undefined;
|
||||
};
|
||||
maxWidth?: number | undefined;
|
||||
maxHeight?: number | undefined;
|
||||
cb?: ((args_0: {
|
||||
x: number;
|
||||
y: number;
|
||||
}) => unknown) | undefined;
|
||||
}>;
|
||||
export type PrintOptions = z.infer<typeof PrintOptionsSchema>;
|
||||
export declare const methods: {
|
||||
/**
|
||||
* Draws a text on a image on a given boundary
|
||||
* @param font a bitmap font loaded from `Jimp.loadFont` command
|
||||
* @param x the x position to start drawing the text
|
||||
* @param y the y position to start drawing the text
|
||||
* @param text the text to draw (string or object with `text`, `alignmentX`, and/or `alignmentY`)
|
||||
* @example
|
||||
* ```ts
|
||||
* import { Jimp } from "jimp";
|
||||
*
|
||||
* const image = await Jimp.read("test/image.png");
|
||||
* const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
|
||||
*
|
||||
* image.print({ font, x: 10, y: 10, text: "Hello world!" });
|
||||
* ```
|
||||
*/
|
||||
print<I extends JimpClass>(image: I, { font, ...options }: PrintOptions & {
|
||||
/** the BMFont instance */
|
||||
font: BmFont<I>;
|
||||
}): I;
|
||||
};
|
||||
//# sourceMappingURL=index.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAe,MAAM,EAAE,MAAM,YAAY,CAAC;AAEjD,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACnE,cAAc,YAAY,CAAC;AAE3B,QAAA,MAAM,kBAAkB;IACtB,uCAAuC;;IAEvC,uCAAuC;;IAEvC,wBAAwB;;;;;;;;;;;;;;IASxB,oCAAoC;;IAEpC,qCAAqC;;IAErC,6EAA6E;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAI7E,CAAC;AAEH,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAmF9D,eAAO,MAAM,OAAO;IAClB;;;;;;;;;;;;;;;OAeG;UACG,CAAC,SAAS,SAAS,SAChB,CAAC,wBAIL,YAAY,GAAG;QAChB,0BAA0B;QAC1B,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;KACjB;CA4EJ,CAAC"}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
import { HorizontalAlign, VerticalAlign } from "@jimp/core";
|
||||
import { methods as blitMethods } from "@jimp/plugin-blit";
|
||||
import { z } from "zod";
|
||||
import { measureText, measureTextHeight, splitLines } from "./measure-text.js";
|
||||
export { measureText, measureTextHeight } from "./measure-text.js";
|
||||
export * from "./types.js";
|
||||
const PrintOptionsSchema = z.object({
|
||||
/** the x position to draw the image */
|
||||
x: z.number(),
|
||||
/** the y position to draw the image */
|
||||
y: z.number(),
|
||||
/** the text to print */
|
||||
text: z.union([
|
||||
z.union([z.string(), z.number()]),
|
||||
z.object({
|
||||
text: z.union([z.string(), z.number()]),
|
||||
alignmentX: z.nativeEnum(HorizontalAlign).optional(),
|
||||
alignmentY: z.nativeEnum(VerticalAlign).optional(),
|
||||
}),
|
||||
]),
|
||||
/** the boundary width to draw in */
|
||||
maxWidth: z.number().optional(),
|
||||
/** the boundary height to draw in */
|
||||
maxHeight: z.number().optional(),
|
||||
/** a callback for when complete that ahs the end co-ordinates of the text */
|
||||
cb: z
|
||||
.function(z.tuple([z.object({ x: z.number(), y: z.number() })]))
|
||||
.optional(),
|
||||
});
|
||||
function xOffsetBasedOnAlignment(font, line, maxWidth, alignment) {
|
||||
if (alignment === HorizontalAlign.LEFT) {
|
||||
return 0;
|
||||
}
|
||||
if (alignment === HorizontalAlign.CENTER) {
|
||||
return (maxWidth - measureText(font, line)) / 2;
|
||||
}
|
||||
return maxWidth - measureText(font, line);
|
||||
}
|
||||
function drawCharacter(image, font, x, y, char) {
|
||||
if (char.width > 0 && char.height > 0) {
|
||||
const characterPage = font.pages[char.page];
|
||||
if (characterPage) {
|
||||
image = blitMethods.blit(image, {
|
||||
src: characterPage,
|
||||
x: x + char.xoffset,
|
||||
y: y + char.yoffset,
|
||||
srcX: char.x,
|
||||
srcY: char.y,
|
||||
srcW: char.width,
|
||||
srcH: char.height,
|
||||
});
|
||||
}
|
||||
}
|
||||
return image;
|
||||
}
|
||||
function printText(image, font, x, y, text, defaultCharWidth) {
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const stringChar = text[i];
|
||||
let char;
|
||||
if (font.chars[stringChar]) {
|
||||
char = stringChar;
|
||||
}
|
||||
else if (/\s/.test(stringChar)) {
|
||||
char = "";
|
||||
}
|
||||
else {
|
||||
char = "?";
|
||||
}
|
||||
const fontChar = font.chars[char] || { xadvance: undefined };
|
||||
const fontKerning = font.kernings[char];
|
||||
if (fontChar) {
|
||||
drawCharacter(image, font, x, y, fontChar);
|
||||
}
|
||||
const nextChar = text[i + 1];
|
||||
const kerning = fontKerning && nextChar && fontKerning[nextChar]
|
||||
? fontKerning[nextChar] || 0
|
||||
: 0;
|
||||
x += kerning + (fontChar.xadvance || defaultCharWidth);
|
||||
}
|
||||
}
|
||||
export const methods = {
|
||||
/**
|
||||
* Draws a text on a image on a given boundary
|
||||
* @param font a bitmap font loaded from `Jimp.loadFont` command
|
||||
* @param x the x position to start drawing the text
|
||||
* @param y the y position to start drawing the text
|
||||
* @param text the text to draw (string or object with `text`, `alignmentX`, and/or `alignmentY`)
|
||||
* @example
|
||||
* ```ts
|
||||
* import { Jimp } from "jimp";
|
||||
*
|
||||
* const image = await Jimp.read("test/image.png");
|
||||
* const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
|
||||
*
|
||||
* image.print({ font, x: 10, y: 10, text: "Hello world!" });
|
||||
* ```
|
||||
*/
|
||||
print(image, { font, ...options }) {
|
||||
let {
|
||||
// eslint-disable-next-line prefer-const
|
||||
x, y, text,
|
||||
// eslint-disable-next-line prefer-const
|
||||
maxWidth = Infinity,
|
||||
// eslint-disable-next-line prefer-const
|
||||
maxHeight = Infinity,
|
||||
// eslint-disable-next-line prefer-const
|
||||
cb = () => { }, } = PrintOptionsSchema.parse(options);
|
||||
let alignmentX;
|
||||
let alignmentY;
|
||||
if (typeof text === "object" &&
|
||||
text.text !== null &&
|
||||
text.text !== undefined) {
|
||||
alignmentX = text.alignmentX || HorizontalAlign.LEFT;
|
||||
alignmentY = text.alignmentY || VerticalAlign.TOP;
|
||||
({ text } = text);
|
||||
}
|
||||
else {
|
||||
alignmentX = HorizontalAlign.LEFT;
|
||||
alignmentY = VerticalAlign.TOP;
|
||||
text = text.toString();
|
||||
}
|
||||
if (typeof text === "number") {
|
||||
text = text.toString();
|
||||
}
|
||||
if (maxHeight !== Infinity && alignmentY === VerticalAlign.BOTTOM) {
|
||||
y += maxHeight - measureTextHeight(font, text, maxWidth);
|
||||
}
|
||||
else if (maxHeight !== Infinity && alignmentY === VerticalAlign.MIDDLE) {
|
||||
y += maxHeight / 2 - measureTextHeight(font, text, maxWidth) / 2;
|
||||
}
|
||||
const defaultCharWidth = Object.entries(font.chars).find((c) => c[1].xadvance)?.[1].xadvance;
|
||||
if (typeof defaultCharWidth !== "number") {
|
||||
throw new Error("Could not find default character width");
|
||||
}
|
||||
const { lines, longestLine } = splitLines(font, text, maxWidth);
|
||||
lines.forEach((line) => {
|
||||
const lineString = line.join(" ");
|
||||
const alignmentWidth = xOffsetBasedOnAlignment(font, lineString, maxWidth, alignmentX);
|
||||
printText(image, font, x + alignmentWidth, y, lineString, defaultCharWidth);
|
||||
y += font.common.lineHeight;
|
||||
});
|
||||
cb.bind(image)({ x: x + longestLine, y });
|
||||
return image;
|
||||
},
|
||||
};
|
||||
//# sourceMappingURL=index.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAE5D,OAAO,EAAE,OAAO,IAAI,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAG/E,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAC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AO,gBAAgB,KAAK,QAAQ,EAAE,CAAC;YACzC,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;QAC5D,CAAC;QAED,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,GAAG,UAAU,CAAC,IAAI,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;QAEhE,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;YACrB,MAAM,UAAU,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAClC,MAAM,cAAc,GAAG,uBAAuB,CAC5C,IAAI,EACJ,UAAU,EACV,QAAQ,EACR,UAAU,CACX,CAAC;YAEF,SAAS,CACP,KAAK,EACL,IAAI,EACJ,CAAC,GAAG,cAAc,EAClB,CAAC,EACD,UAAU,EACV,gBAAgB,CACjB,CAAC;YACF,CAAC,IAAI,IAAI,CALine truncated
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
import { BmCharacter, BmKerning, BmFont, BmCommonProps } from "./types.js";
|
||||
export declare const isWebWorker: boolean;
|
||||
export interface LoadedFont {
|
||||
chars: BmCharacter[];
|
||||
kernings: BmKerning[];
|
||||
common: BmCommonProps;
|
||||
info: Record<string, any>;
|
||||
pages: string[];
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param bufferOrUrl A URL to a file or a buffer
|
||||
* @returns
|
||||
*/
|
||||
export declare function loadBitmapFontData(bufferOrUrl: string | Buffer): Promise<LoadedFont>;
|
||||
type RawFont = Awaited<ReturnType<typeof loadBitmapFontData>>;
|
||||
export type ResolveBmFont = Omit<BmFont, "pages"> & Pick<RawFont, "pages">;
|
||||
export declare function processBitmapFont(file: string, font: LoadedFont): Promise<{
|
||||
chars: Record<string, BmCharacter>;
|
||||
kernings: Record<string, BmKerning>;
|
||||
pages: ({
|
||||
bitmap: import("@jimp/types").Bitmap;
|
||||
background: number;
|
||||
formats: import("@jimp/types").Format<any>[];
|
||||
mime?: string;
|
||||
inspect(): string;
|
||||
toString(): string;
|
||||
readonly width: number;
|
||||
readonly height: number;
|
||||
getBuffer<ProvidedMimeType extends "image/png", Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T ? T extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T extends Record<ProvidedMimeType, infer O> ? O : never : never : never>(mime: ProvidedMimeType, options?: Options | undefined): Promise<Buffer>;
|
||||
getBase64<ProvidedMimeType extends "image/png", Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T ? T extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T extends Record<ProvidedMimeType, infer O> ? O : never : never : never>(mime: ProvidedMimeType, options?: Options | undefined): Promise<string>;
|
||||
write<Extension extends string, Mime extends Record<"png", "image/png"> extends infer T ? T extends Record<"png", "image/png"> ? T extends Record<Extension, infer M> ? M : never : never : never, Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T_1 ? T_1 extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T_1 extends Record<Mime, infer O> ? O : never : never : never>(path: `${string}.${Extension}`, options?: Options | undefined): Promise<void>;
|
||||
clone<S extends unknown>(this: S): S;
|
||||
getPixelIndex(x: number, y: number, edgeHandling?: import("@jimp/types").Edge): number;
|
||||
getPixelColor(x: number, y: number): number;
|
||||
setPixelColor(hex: number, x: number, y: number): any;
|
||||
hasAlpha(): boolean;
|
||||
composite<I extends unknown>(src: I, x?: number, y?: number, options?: {
|
||||
mode?: import("@jimp/core").BlendMode;
|
||||
opacitySource?: number;
|
||||
opacityDest?: number;
|
||||
}): any;
|
||||
scan(f: (x: number, y: number, idx: number) => any): any;
|
||||
scan(x: number, y: number, w: number, h: number, cb: (x: number, y: number, idx: number) => any): any;
|
||||
scanIterator(x?: number, y?: number, w?: number, h?: number): Generator<{
|
||||
x: number;
|
||||
y: number;
|
||||
idx: number;
|
||||
image: any;
|
||||
}, void, unknown>;
|
||||
} & import("@jimp/core").JimpInstanceMethods<{
|
||||
bitmap: import("@jimp/types").Bitmap;
|
||||
background: number;
|
||||
formats: import("@jimp/types").Format<any>[];
|
||||
mime?: string;
|
||||
inspect(): string;
|
||||
toString(): string;
|
||||
readonly width: number;
|
||||
readonly height: number;
|
||||
getBuffer<ProvidedMimeType extends "image/png", Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T ? T extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T extends Record<ProvidedMimeType, infer O> ? O : never : never : never>(mime: ProvidedMimeType, options?: Options | undefined): Promise<Buffer>;
|
||||
getBase64<ProvidedMimeType extends "image/png", Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T ? T extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T extends Record<ProvidedMimeType, infer O> ? O : never : never : never>(mime: ProvidedMimeType, options?: Options | undefined): Promise<string>;
|
||||
write<Extension extends string, Mime extends Record<"png", "image/png"> extends infer T ? T extends Record<"png", "image/png"> ? T extends Record<Extension, infer M> ? M : never : never : never, Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T_1 ? T_1 extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T_1 extends Record<Mime, infer O> ? O : never : never : never>(path: `${string}.${Extension}`, options?: Options | undefined): Promise<void>;
|
||||
clone<S extends unknown>(this: S): S;
|
||||
getPixelIndex(x: number, y: number, edgeHandling?: import("@jimp/types").Edge): number;
|
||||
getPixelColor(x: number, y: number): number;
|
||||
setPixelColor(hex: number, x: number, y: number): any;
|
||||
hasAlpha(): boolean;
|
||||
composite<I extends unknown>(src: I, x?: number, y?: number, options?: {
|
||||
mode?: import("@jimp/core").BlendMode;
|
||||
opacitySource?: number;
|
||||
opacityDest?: number;
|
||||
}): any;
|
||||
scan(f: (x: number, y: number, idx: number) => any): any;
|
||||
scan(x: number, y: number, w: number, h: number, cb: (x: number, y: number, idx: number) => any): any;
|
||||
scanIterator(x?: number, y?: number, w?: number, h?: number): Generator<{
|
||||
x: number;
|
||||
y: number;
|
||||
idx: number;
|
||||
image: any;
|
||||
}, void, unknown>;
|
||||
}, import("@jimp/core").JimpPlugin>)[];
|
||||
common: BmCommonProps;
|
||||
info: Record<string, any>;
|
||||
}>;
|
||||
export {};
|
||||
//# sourceMappingURL=load-bitmap-font.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"load-bitmap-font.d.ts","sourceRoot":"","sources":["../../src/load-bitmap-font.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAQ3E,eAAO,MAAM,WAAW,SACoC,CAAC;AAoB7D,MAAM,WAAW,UAAU;IACzB,KAAK,EAAE,WAAW,EAAE,CAAC;IACrB,QAAQ,EAAE,SAAS,EAAE,CAAC;IACtB,MAAM,EAAE,aAAa,CAAC;IAEtB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,KAAK,EAAE,MAAM,EAAE,CAAC;CACjB;AA0CD;;;;GAIG;AACH,wBAAsB,kBAAkB,CACtC,WAAW,EAAE,MAAM,GAAG,MAAM,GAC3B,OAAO,CAAC,UAAU,CAAC,CA4CrB;AAED,KAAK,OAAO,GAAG,OAAO,CAAC,UAAU,CAAC,OAAO,kBAAkB,CAAC,CAAC,CAAC;AAC9D,MAAM,MAAM,aAAa,GAAG,IAAI,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AAE3E,wBAAsB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;gBA4Byie,CAAC;yBAAsC,CAAC;uBAAiC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;gBAAs3R,CAAC;yBAAsC,CAAC;uBAAiC,CAAC;;;;;;;;;;;YAlI/mwB,aAAa;UAEf,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;GA+H1B"}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
import parseASCII from "parse-bmfont-ascii";
|
||||
import parseXML from "parse-bmfont-xml";
|
||||
import readBinary from "parse-bmfont-binary";
|
||||
import png from "@jimp/js-png";
|
||||
import { createJimp } from "@jimp/core";
|
||||
import path from "path";
|
||||
import xmlPackage from "simple-xml-to-json";
|
||||
const { convertXML } = xmlPackage;
|
||||
export const isWebWorker = typeof self !== "undefined" && self.document === undefined;
|
||||
const CharacterJimp = createJimp({ formats: [png] });
|
||||
const HEADER = Buffer.from([66, 77, 70, 3]);
|
||||
function isBinary(buf) {
|
||||
if (typeof buf === "string") {
|
||||
return buf.substring(0, 3) === "BMF";
|
||||
}
|
||||
const startOfHeader = buf.slice(0, 4);
|
||||
return (buf.length > 4 &&
|
||||
startOfHeader[0] === HEADER[0] &&
|
||||
startOfHeader[1] === HEADER[1] &&
|
||||
startOfHeader[2] === HEADER[2]);
|
||||
}
|
||||
function parseFont(file, data) {
|
||||
if (isBinary(data)) {
|
||||
if (typeof data === "string") {
|
||||
data = Buffer.from(data, "binary");
|
||||
}
|
||||
return readBinary(data);
|
||||
}
|
||||
data = data.toString().trim();
|
||||
if (/.json$/.test(file) || data.charAt(0) === "{") {
|
||||
return JSON.parse(data);
|
||||
}
|
||||
if (/.xml$/.test(file) || data.charAt(0) === "<") {
|
||||
return parseXML(data);
|
||||
}
|
||||
return parseASCII(data);
|
||||
}
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
function parseNumbersInObject(obj) {
|
||||
for (const key in obj) {
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
obj[key] = parseInt(obj[key], 10);
|
||||
}
|
||||
catch {
|
||||
// do nothing
|
||||
}
|
||||
if (typeof obj[key] === "object") {
|
||||
parseNumbersInObject(obj[key]);
|
||||
}
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @param bufferOrUrl A URL to a file or a buffer
|
||||
* @returns
|
||||
*/
|
||||
export async function loadBitmapFontData(bufferOrUrl) {
|
||||
if (isWebWorker && typeof bufferOrUrl === "string") {
|
||||
const res = await fetch(bufferOrUrl);
|
||||
const text = await res.text();
|
||||
const json = convertXML(text);
|
||||
const font = json.font.children.reduce(
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
(acc, i) => ({ ...acc, ...i }), {});
|
||||
const pages = [];
|
||||
const chars = [];
|
||||
const kernings = [];
|
||||
for (let i = 0; i < font.pages.children.length; i++) {
|
||||
const p = font.pages.children[i].page;
|
||||
const id = parseInt(p.id, 10);
|
||||
pages[id] = parseNumbersInObject(p.file);
|
||||
}
|
||||
for (let i = 0; i < font.chars.children.length; i++) {
|
||||
chars.push(parseNumbersInObject(font.chars.children[i].char));
|
||||
}
|
||||
for (let i = 0; i < font.kernings.children.length; i++) {
|
||||
kernings.push(parseNumbersInObject(font.kernings.children[i].kerning));
|
||||
}
|
||||
return {
|
||||
info: font.info,
|
||||
common: font.common,
|
||||
pages,
|
||||
chars,
|
||||
kernings,
|
||||
};
|
||||
}
|
||||
else if (typeof bufferOrUrl === "string") {
|
||||
const res = await fetch(bufferOrUrl);
|
||||
const text = await res.text();
|
||||
return parseFont(bufferOrUrl, text);
|
||||
}
|
||||
else {
|
||||
return parseFont("", bufferOrUrl);
|
||||
}
|
||||
}
|
||||
export async function processBitmapFont(file, font) {
|
||||
const chars = {};
|
||||
const kernings = {};
|
||||
for (let i = 0; i < font.chars.length; i++) {
|
||||
const char = font.chars[i];
|
||||
chars[String.fromCharCode(char.id)] = char;
|
||||
}
|
||||
for (let i = 0; i < font.kernings.length; i++) {
|
||||
const firstString = String.fromCharCode(font.kernings[i].first);
|
||||
kernings[firstString] = kernings[firstString] || {};
|
||||
kernings[firstString][String.fromCharCode(font.kernings[i].second)] =
|
||||
font.kernings[i].amount;
|
||||
}
|
||||
return {
|
||||
...font,
|
||||
chars,
|
||||
kernings,
|
||||
pages: await Promise.all(font.pages.map(async (page) => CharacterJimp.read(path.join(path.dirname(file), page)))),
|
||||
};
|
||||
}
|
||||
//# sourceMappingURL=load-bitmap-font.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"load-bitmap-font.js","sourceRoot":"","sources":["../../src/load-bitmap-font.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,oBAAoB,CAAC;AAC5C,OAAO,QAAQ,MAAM,kBAAkB,CAAC;AACxC,OAAO,UAAU,MAAM,qBAAqB,CAAC;AAE7C,OAAO,GAAG,MAAM,cAAc,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,UAAU,MAAM,oBAAoB,CAAC;AAE5C,MAAM,EAAE,UAAU,EAAE,GAAG,UAAU,CAAC;AAElC,MAAM,CAAC,MAAM,WAAW,GACtB,OAAO,IAAI,KAAK,WAAW,IAAI,IAAI,CAAC,QAAQ,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QACL,QAAQ;QACR,KAAK,EAAE,MAAM,OAAO,CAAC,GAAG,CACtB,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAC5B,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,CACxD,CACF;KACF,CAAC;AACJ,CAAC"}
|
||||
+131
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* Loads a Bitmap Font from a file.
|
||||
* @param file A path or URL to a font file
|
||||
* @returns A collection of Jimp images that can be used to print text
|
||||
* @example
|
||||
* ```ts
|
||||
* import { Jimp, loadFont } from "jimp";
|
||||
* import { SANS_10_BLACK } from "jimp/fonts";
|
||||
*
|
||||
* const font = await loadFont(SANS_10_BLACK);
|
||||
* const image = new Jimp({ width: 200, height: 100, color: 0xffffffff });
|
||||
*
|
||||
* image.print(font, 10, 10, "Hello world!");
|
||||
* ```
|
||||
*/
|
||||
export declare function loadFont(file: string): Promise<{
|
||||
chars: Record<string, import("./types.js").BmCharacter>;
|
||||
kernings: Record<string, import("./types.js").BmKerning>;
|
||||
pages: ({
|
||||
bitmap: import("@jimp/types").Bitmap;
|
||||
background: number;
|
||||
formats: import("@jimp/types").Format<any>[];
|
||||
mime?: string;
|
||||
inspect(): string;
|
||||
toString(): string;
|
||||
readonly width: number;
|
||||
readonly height: number;
|
||||
getBuffer<ProvidedMimeType extends "image/png", Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T ? T extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T extends Record<ProvidedMimeType, infer O> ? O : never : never : never>(mime: ProvidedMimeType, options?: Options | undefined): Promise<Buffer>;
|
||||
getBase64<ProvidedMimeType extends "image/png", Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T ? T extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T extends Record<ProvidedMimeType, infer O> ? O : never : never : never>(mime: ProvidedMimeType, options?: Options | undefined): Promise<string>;
|
||||
write<Extension extends string, Mime extends Record<"png", "image/png"> extends infer T ? T extends Record<"png", "image/png"> ? T extends Record<Extension, infer M> ? M : never : never : never, Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T_1 ? T_1 extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T_1 extends Record<Mime, infer O> ? O : never : never : never>(path: `${string}.${Extension}`, options?: Options | undefined): Promise<void>;
|
||||
clone<S extends unknown>(this: S): S;
|
||||
getPixelIndex(x: number, y: number, edgeHandling?: import("@jimp/types").Edge): number;
|
||||
getPixelColor(x: number, y: number): number;
|
||||
setPixelColor(hex: number, x: number, y: number): any;
|
||||
hasAlpha(): boolean;
|
||||
composite<I extends unknown>(src: I, x?: number, y?: number, options?: {
|
||||
mode?: import("@jimp/core").BlendMode;
|
||||
opacitySource?: number;
|
||||
opacityDest?: number;
|
||||
}): any;
|
||||
scan(f: (x: number, y: number, idx: number) => any): any;
|
||||
scan(x: number, y: number, w: number, h: number, cb: (x: number, y: number, idx: number) => any): any;
|
||||
scanIterator(x?: number, y?: number, w?: number, h?: number): Generator<{
|
||||
x: number;
|
||||
y: number;
|
||||
idx: number;
|
||||
image: any;
|
||||
}, void, unknown>;
|
||||
} & import("@jimp/core").JimpInstanceMethods<{
|
||||
bitmap: import("@jimp/types").Bitmap;
|
||||
background: number;
|
||||
formats: import("@jimp/types").Format<any>[];
|
||||
mime?: string;
|
||||
inspect(): string;
|
||||
toString(): string;
|
||||
readonly width: number;
|
||||
readonly height: number;
|
||||
getBuffer<ProvidedMimeType extends "image/png", Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T ? T extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T extends Record<ProvidedMimeType, infer O> ? O : never : never : never>(mime: ProvidedMimeType, options?: Options | undefined): Promise<Buffer>;
|
||||
getBase64<ProvidedMimeType extends "image/png", Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T ? T extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T extends Record<ProvidedMimeType, infer O> ? O : never : never : never>(mime: ProvidedMimeType, options?: Options | undefined): Promise<string>;
|
||||
write<Extension extends string, Mime extends Record<"png", "image/png"> extends infer T ? T extends Record<"png", "image/png"> ? T extends Record<Extension, infer M> ? M : never : never : never, Options extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> extends infer T_1 ? T_1 extends Record<"image/png", Omit<import("@jimp/js-png").PNGJSOptions, "filterType" | "colorType" | "inputColorType"> & {
|
||||
filterType?: import("@jimp/js-png").PNGFilterType;
|
||||
colorType?: import("@jimp/js-png").PNGColorType;
|
||||
inputColorType?: import("@jimp/js-png").PNGColorType;
|
||||
}> ? T_1 extends Record<Mime, infer O> ? O : never : never : never>(path: `${string}.${Extension}`, options?: Options | undefined): Promise<void>;
|
||||
clone<S extends unknown>(this: S): S;
|
||||
getPixelIndex(x: number, y: number, edgeHandling?: import("@jimp/types").Edge): number;
|
||||
getPixelColor(x: number, y: number): number;
|
||||
setPixelColor(hex: number, x: number, y: number): any;
|
||||
hasAlpha(): boolean;
|
||||
composite<I extends unknown>(src: I, x?: number, y?: number, options?: {
|
||||
mode?: import("@jimp/core").BlendMode;
|
||||
opacitySource?: number;
|
||||
opacityDest?: number;
|
||||
}): any;
|
||||
scan(f: (x: number, y: number, idx: number) => any): any;
|
||||
scan(x: number, y: number, w: number, h: number, cb: (x: number, y: number, idx: number) => any): any;
|
||||
scanIterator(x?: number, y?: number, w?: number, h?: number): Generator<{
|
||||
x: number;
|
||||
y: number;
|
||||
idx: number;
|
||||
image: any;
|
||||
}, void, unknown>;
|
||||
}, import("@jimp/core").JimpPlugin>)[];
|
||||
common: import("./types.js").BmCommonProps;
|
||||
info: Record<string, any>;
|
||||
}>;
|
||||
//# sourceMappingURL=load-font.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"load-font.d.ts","sourceRoot":"","sources":["../../src/load-font.ts"],"names":[],"mappings":"AAMA;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,QAAQ,CAAC,IAAI,EAAE,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;gBAcy/kB,CAAC;yBAAsC,CAAC;uBAAiC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;gBAAs3R,CAAC;yBAAsC,CAAC;uBAAiC,CAAC;;;;;;;;;;;;;GAD7i3B"}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
import { isWebWorker, loadBitmapFontData, processBitmapFont, } from "./load-bitmap-font.js";
|
||||
/**
|
||||
* Loads a Bitmap Font from a file.
|
||||
* @param file A path or URL to a font file
|
||||
* @returns A collection of Jimp images that can be used to print text
|
||||
* @example
|
||||
* ```ts
|
||||
* import { Jimp, loadFont } from "jimp";
|
||||
* import { SANS_10_BLACK } from "jimp/fonts";
|
||||
*
|
||||
* const font = await loadFont(SANS_10_BLACK);
|
||||
* const image = new Jimp({ width: 200, height: 100, color: 0xffffffff });
|
||||
*
|
||||
* image.print(font, 10, 10, "Hello world!");
|
||||
* ```
|
||||
*/
|
||||
export async function loadFont(file) {
|
||||
let fileOrBuffer = file;
|
||||
if (typeof window === "undefined" && !isWebWorker) {
|
||||
const { existsSync, promises: fs } = await import("fs");
|
||||
if (existsSync(file)) {
|
||||
fileOrBuffer = await fs.readFile(file);
|
||||
}
|
||||
}
|
||||
const data = await loadBitmapFontData(fileOrBuffer);
|
||||
return processBitmapFont(file, data);
|
||||
}
|
||||
//# sourceMappingURL=load-font.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"load-font.js","sourceRoot":"","sources":["../../src/load-font.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,iBAAiB,GAClB,MAAM,uBAAuB,CAAC;AAE/B;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,IAAY;IACzC,IAAI,YAAY,GAAoB,IAAI,CAAC;IAEzC,IAAI,OAAO,MAAM,KAAK,WAAW,IAAI,CAAC,WAAW,EAAE,CAAC;QAClD,MAAM,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,EAAE,GAAG,MAAM,MAAM,CAAC,IAAI,CAAC,CAAC;QAExD,IAAI,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;YACrB,YAAY,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,kBAAkB,CAAC,YAAY,CAAC,CAAC;IACpD,OAAO,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AACvC,CAAC"}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import { BmFont } from "./types.js";
|
||||
export declare function measureText(font: BmFont, text: string): number;
|
||||
export declare function splitLines(font: BmFont, text: string, maxWidth: number): {
|
||||
lines: string[][];
|
||||
longestLine: number;
|
||||
};
|
||||
export declare function measureTextHeight(font: BmFont, text: string, maxWidth: number): number;
|
||||
//# sourceMappingURL=measure-text.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"measure-text.d.ts","sourceRoot":"","sources":["../../src/measure-text.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAEpC,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,UAoBrD;AAED,wBAAgB,UAAU,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;;;EAyDtE;AAED,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,UAKjB"}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
export function measureText(font, text) {
|
||||
let x = 0;
|
||||
for (let i = 0; i < text.length; i++) {
|
||||
const char = text[i];
|
||||
const fontChar = font.chars[char];
|
||||
if (fontChar) {
|
||||
const fontKerning = font.kernings[char];
|
||||
const nextChar = text[i + 1];
|
||||
const kerning = fontKerning && nextChar && fontKerning[nextChar]
|
||||
? fontKerning[nextChar] || 0
|
||||
: 0;
|
||||
x += (fontChar.xadvance || 0) + kerning;
|
||||
}
|
||||
}
|
||||
return x;
|
||||
}
|
||||
export function splitLines(font, text, maxWidth) {
|
||||
const words = text.replace(/[\r\n]+/g, " \n").split(" ");
|
||||
const lines = [];
|
||||
let currentLine = [];
|
||||
let longestLine = 0;
|
||||
words.forEach((word) => {
|
||||
const wordWidth = measureText(font, word + (words.length > 1 ? " " : ""));
|
||||
// If a word is longer than the allowable width we need to split it across lines.
|
||||
if (wordWidth > maxWidth) {
|
||||
const characterIterator = word[Symbol.iterator]();
|
||||
let current = "";
|
||||
for (const char of characterIterator) {
|
||||
const nextLine = [...currentLine, current + char].join(" ");
|
||||
const length = measureText(font, nextLine);
|
||||
if (length < maxWidth) {
|
||||
current += char;
|
||||
}
|
||||
else if (length > maxWidth) {
|
||||
lines.push([...currentLine, current]);
|
||||
currentLine = [];
|
||||
current = char;
|
||||
}
|
||||
else {
|
||||
lines.push([...currentLine, current + char]);
|
||||
currentLine = [];
|
||||
current = "";
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
const line = [...currentLine, word].join(" ");
|
||||
const length = measureText(font, line);
|
||||
if (length <= maxWidth && !word.includes("\n")) {
|
||||
if (length > longestLine) {
|
||||
longestLine = length;
|
||||
}
|
||||
currentLine.push(word);
|
||||
}
|
||||
else {
|
||||
lines.push(currentLine);
|
||||
currentLine = [word.replace("\n", "")];
|
||||
}
|
||||
});
|
||||
lines.push(currentLine);
|
||||
return {
|
||||
lines,
|
||||
longestLine,
|
||||
};
|
||||
}
|
||||
export function measureTextHeight(font, text, maxWidth) {
|
||||
const { lines } = splitLines(font, text, maxWidth);
|
||||
return lines.length * font.common.lineHeight;
|
||||
}
|
||||
//# sourceMappingURL=measure-text.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"measure-text.js","sourceRoot":"","sources":["../../src/measure-text.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,IAAY;IACpD,IAAI,CAAC,GAAG,CAAC,CAAC;IAEV,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACrC,MAAM,IAAI,GAAG,IAAI,CAAC,CAAC,CAAE,CAAC;QACtB,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAElC,IAAI,QAAQ,EAAE,CAAC;YACb,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC;YACxC,MAAM,QAAQ,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;YAC7B,MAAM,OAAO,GACX,WAAW,IAAI,QAAQ,IAAI,WAAW,CAAC,QAAQ,CAAC;gBAC9C,CAAC,CAAC,WAAW,CAAC,QAAQ,CAAC,IAAI,CAAC;gBAC5B,CAAC,CAAC,CAAC,CAAC;YAER,C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}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"type": "module"
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import { JimpClass } from "@jimp/types";
|
||||
export interface BmCharacter {
|
||||
id: number;
|
||||
xadvance: number;
|
||||
width: number;
|
||||
height: number;
|
||||
xoffset: number;
|
||||
yoffset: number;
|
||||
page: number;
|
||||
x: number;
|
||||
y: number;
|
||||
}
|
||||
export interface BmKerning {
|
||||
[secondString: string]: number;
|
||||
first: number;
|
||||
second: number;
|
||||
amount: number;
|
||||
}
|
||||
export interface BmCommonProps {
|
||||
lineHeight: number;
|
||||
}
|
||||
export interface BmFont<T extends JimpClass = JimpClass> {
|
||||
chars: Record<string, BmCharacter>;
|
||||
kernings: Record<string, BmKerning>;
|
||||
common: BmCommonProps;
|
||||
info: Record<string, any>;
|
||||
pages: T[];
|
||||
}
|
||||
//# sourceMappingURL=types.d.ts.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,MAAM,WAAW,WAAW;IAC1B,EAAE,EAAE,MAAM,CAAC;IACX,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAED,MAAM,WAAW,SAAS;IACxB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAAC;IAC/B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,aAAa;IAC5B,UAAU,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,WAAW,MAAM,CAAC,CAAC,SAAS,SAAS,GAAG,SAAS;IACrD,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACnC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IACpC,MAAM,EAAE,aAAa,CAAC;IAEtB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC1B,KAAK,EAAE,CAAC,EAAE,CAAC;CACZ"}
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=types.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":""}
|
||||
Reference in new issue
Block a user