Socket
Socket
Sign inDemoInstall

@jimp/plugin-print

Package Overview
Dependencies
Maintainers
2
Versions
242
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@jimp/plugin-print - npm Package Compare versions

Comparing version 1.1.5--canary.d9590e7.0 to 1.1.5

12

CHANGELOG.md

@@ -0,1 +1,13 @@

# v1.1.5 (Mon Sep 02 2024)
#### 🐛 Bug Fix
- Fix loading fonts in web-worker [#1322](https://github.com/jimp-dev/jimp/pull/1322) ([@hipstersmoothie](https://github.com/hipstersmoothie))
#### Authors: 1
- Andrew Lisowski ([@hipstersmoothie](https://github.com/hipstersmoothie))
---
# v1.1.2 (Mon Sep 02 2024)

@@ -2,0 +14,0 @@

123

dist/commonjs/load-bitmap-font.d.ts
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[];
}
/**

@@ -7,12 +15,115 @@ *

*/
declare function loadBitmapFontData(bufferOrUrl: string | Buffer): Promise<{
chars: BmCharacter[];
kernings: BmKerning[];
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>[];
inspect(): string;
toString(): string;
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>[];
inspect(): string;
toString(): string;
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>;
pages: string[];
}>;
type RawFont = Awaited<ReturnType<typeof loadBitmapFontData>>;
export declare function loadBitmapFont(bufferOrUrl: string | Buffer): Promise<Omit<BmFont, "pages"> & Pick<RawFont, "pages">>;
export {};
//# sourceMappingURL=load-bitmap-font.d.ts.map

@@ -6,6 +6,14 @@ "use strict";

Object.defineProperty(exports, "__esModule", { value: true });
exports.loadBitmapFont = loadBitmapFont;
exports.isWebWorker = void 0;
exports.loadBitmapFontData = loadBitmapFontData;
exports.processBitmapFont = processBitmapFont;
const parse_bmfont_ascii_1 = __importDefault(require("parse-bmfont-ascii"));
const parse_bmfont_xml_1 = __importDefault(require("parse-bmfont-xml"));
const parse_bmfont_binary_1 = __importDefault(require("parse-bmfont-binary"));
const js_png_1 = __importDefault(require("@jimp/js-png"));
const core_1 = require("@jimp/core");
const path_1 = __importDefault(require("path"));
const simple_xml_to_json_1 = require("simple-xml-to-json");
exports.isWebWorker = typeof self !== "undefined" && self.document === undefined;
const CharacterJimp = (0, core_1.createJimp)({ formats: [js_png_1.default] });
const HEADER = Buffer.from([66, 77, 70, 3]);

@@ -38,2 +46,18 @@ function isBinary(buf) {

}
// 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;
}
/**

@@ -45,5 +69,34 @@ *

async function loadBitmapFontData(bufferOrUrl) {
if (typeof bufferOrUrl === "string") {
if (exports.isWebWorker && typeof bufferOrUrl === "string") {
const res = await fetch(bufferOrUrl);
const text = await res.text();
const json = (0, simple_xml_to_json_1.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);

@@ -55,4 +108,3 @@ }

}
async function loadBitmapFont(bufferOrUrl) {
const font = await loadBitmapFontData(bufferOrUrl);
async function processBitmapFont(file, font) {
const chars = {};

@@ -74,4 +126,5 @@ const kernings = {};

kernings,
pages: await Promise.all(font.pages.map(async (page) => CharacterJimp.read(path_1.default.join(path_1.default.dirname(file), page)))),
};
}
//# sourceMappingURL=load-bitmap-font.js.map

4

dist/commonjs/load-font.d.ts

@@ -17,2 +17,4 @@ /**

export declare function loadFont(file: string): Promise<{
chars: Record<string, import("./types.js").BmCharacter>;
kernings: Record<string, import("./types.js").BmKerning>;
pages: ({

@@ -121,4 +123,2 @@ bitmap: import("@jimp/types").Bitmap;

}, import("@jimp/core").JimpPlugin>)[];
chars: Record<string, import("./types.js").BmCharacter>;
kernings: Record<string, import("./types.js").BmKerning>;
common: import("./types.js").BmCommonProps;

@@ -125,0 +125,0 @@ info: Record<string, any>;

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadFont = loadFont;
const load_bitmap_font_js_1 = require("./load-bitmap-font.js");
const core_1 = require("@jimp/core");
const js_png_1 = __importDefault(require("@jimp/js-png"));
const path_1 = __importDefault(require("path"));
const CharacterJimp = (0, core_1.createJimp)({ formats: [js_png_1.default] });
/**

@@ -29,3 +22,3 @@ * Loads a Bitmap Font from a file.

let fileOrBuffer = file;
if (typeof window === "undefined") {
if (typeof window === "undefined" && !load_bitmap_font_js_1.isWebWorker) {
const { existsSync, promises: fs } = await import("fs");

@@ -36,8 +29,5 @@ if (existsSync(file)) {

}
const font = await (0, load_bitmap_font_js_1.loadBitmapFont)(fileOrBuffer);
return {
...font,
pages: await Promise.all(font.pages.map(async (page) => CharacterJimp.read(path_1.default.join(path_1.default.dirname(file), page)))),
};
const data = await (0, load_bitmap_font_js_1.loadBitmapFontData)(fileOrBuffer);
return (0, load_bitmap_font_js_1.processBitmapFont)(file, data);
}
//# sourceMappingURL=load-font.js.map
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[];
}
/**

@@ -7,12 +15,115 @@ *

*/
declare function loadBitmapFontData(bufferOrUrl: string | Buffer): Promise<{
chars: BmCharacter[];
kernings: BmKerning[];
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>[];
inspect(): string;
toString(): string;
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>[];
inspect(): string;
toString(): string;
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>;
pages: string[];
}>;
type RawFont = Awaited<ReturnType<typeof loadBitmapFontData>>;
export declare function loadBitmapFont(bufferOrUrl: string | Buffer): Promise<Omit<BmFont, "pages"> & Pick<RawFont, "pages">>;
export {};
//# sourceMappingURL=load-bitmap-font.d.ts.map
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 { convertXML } from "simple-xml-to-json";
export const isWebWorker = typeof self !== "undefined" && self.document === undefined;
const CharacterJimp = createJimp({ formats: [png] });
const HEADER = Buffer.from([66, 77, 70, 3]);

@@ -31,2 +37,18 @@ function isBinary(buf) {

}
// 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;
}
/**

@@ -37,6 +59,35 @@ *

*/
async function loadBitmapFontData(bufferOrUrl) {
if (typeof bufferOrUrl === "string") {
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);

@@ -48,4 +99,3 @@ }

}
export async function loadBitmapFont(bufferOrUrl) {
const font = await loadBitmapFontData(bufferOrUrl);
export async function processBitmapFont(file, font) {
const chars = {};

@@ -67,4 +117,5 @@ const kernings = {};

kernings,
pages: await Promise.all(font.pages.map(async (page) => CharacterJimp.read(path.join(path.dirname(file), page)))),
};
}
//# sourceMappingURL=load-bitmap-font.js.map

@@ -17,2 +17,4 @@ /**

export declare function loadFont(file: string): Promise<{
chars: Record<string, import("./types.js").BmCharacter>;
kernings: Record<string, import("./types.js").BmKerning>;
pages: ({

@@ -121,4 +123,2 @@ bitmap: import("@jimp/types").Bitmap;

}, import("@jimp/core").JimpPlugin>)[];
chars: Record<string, import("./types.js").BmCharacter>;
kernings: Record<string, import("./types.js").BmKerning>;
common: import("./types.js").BmCommonProps;

@@ -125,0 +125,0 @@ info: Record<string, any>;

@@ -1,6 +0,2 @@

import { loadBitmapFont } from "./load-bitmap-font.js";
import { createJimp } from "@jimp/core";
import png from "@jimp/js-png";
import path from "path";
const CharacterJimp = createJimp({ formats: [png] });
import { isWebWorker, loadBitmapFontData, processBitmapFont, } from "./load-bitmap-font.js";
/**

@@ -23,3 +19,3 @@ * Loads a Bitmap Font from a file.

let fileOrBuffer = file;
if (typeof window === "undefined") {
if (typeof window === "undefined" && !isWebWorker) {
const { existsSync, promises: fs } = await import("fs");

@@ -30,8 +26,5 @@ if (existsSync(file)) {

}
const font = await loadBitmapFont(fileOrBuffer);
return {
...font,
pages: await Promise.all(font.pages.map(async (page) => CharacterJimp.read(path.join(path.dirname(file), page)))),
};
const data = await loadBitmapFontData(fileOrBuffer);
return processBitmapFont(file, data);
}
//# sourceMappingURL=load-font.js.map
{
"name": "@jimp/plugin-print",
"version": "1.1.5--canary.d9590e7.0",
"version": "1.1.5",
"repository": "jimp-dev/jimp",

@@ -19,6 +19,6 @@ "engines": {

"devDependencies": {
"@jimp/config-eslint": "1.1.5--canary.d9590e7.0",
"@jimp/config-typescript": "1.1.5--canary.d9590e7.0",
"@jimp/config-eslint": "1.1.5",
"@jimp/config-typescript": "1.1.5",
"@jimp/js-png": "workspace:*",
"@jimp/test-utils": "1.1.5--canary.d9590e7.0",
"@jimp/test-utils": "1.1.5",
"@types/node": "^18.19.48",

@@ -78,10 +78,11 @@ "eslint": "^9.9.1",

"dependencies": {
"@jimp/core": "1.1.5--canary.d9590e7.0",
"@jimp/js-jpeg": "1.1.5--canary.d9590e7.0",
"@jimp/js-png": "1.1.5--canary.d9590e7.0",
"@jimp/plugin-blit": "1.1.5--canary.d9590e7.0",
"@jimp/types": "1.1.5--canary.d9590e7.0",
"@jimp/core": "1.1.5",
"@jimp/js-jpeg": "1.1.5",
"@jimp/js-png": "1.1.5",
"@jimp/plugin-blit": "1.1.5",
"@jimp/types": "1.1.5",
"parse-bmfont-ascii": "^1.0.6",
"parse-bmfont-binary": "^1.0.6",
"parse-bmfont-xml": "^1.1.6",
"simple-xml-to-json": "^1.2.2",
"zod": "^3.23.8"

@@ -93,3 +94,3 @@ },

"module": "./dist/esm/index.js",
"gitHead": "d9590e7bf90a7de03c5f67f1c1b2f661d1f30002"
"gitHead": "3d90ad817b81c7b7f2327e204ec94662b934d3b9"
}

@@ -5,3 +5,11 @@ import parseASCII from "parse-bmfont-ascii";

import { BmCharacter, BmKerning, BmFont, BmCommonProps } from "./types.js";
import png from "@jimp/js-png";
import { createJimp } from "@jimp/core";
import path from "path";
import { convertXML } from "simple-xml-to-json";
export const isWebWorker =
typeof self !== "undefined" && self.document === undefined;
const CharacterJimp = createJimp({ formats: [png] });
const HEADER = Buffer.from([66, 77, 70, 3]);

@@ -24,6 +32,3 @@

function parseFont(
file: string,
data: Buffer | string,
): {
export interface LoadedFont {
chars: BmCharacter[];

@@ -35,3 +40,5 @@ kernings: BmKerning[];

pages: string[];
} {
}
function parseFont(file: string, data: Buffer | string): LoadedFont {
if (isBinary(data)) {

@@ -58,2 +65,20 @@ if (typeof data === "string") {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function parseNumbersInObject<T extends Record<string, any>>(obj: T) {
for (const key in obj) {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(obj as any)[key] = parseInt(obj[key], 10);
} catch {
// do nothing
}
if (typeof obj[key] === "object") {
parseNumbersInObject(obj[key]);
}
}
return obj;
}
/**

@@ -64,7 +89,44 @@ *

*/
async function loadBitmapFontData(bufferOrUrl: string | Buffer) {
if (typeof bufferOrUrl === "string") {
export async function loadBitmapFontData(
bufferOrUrl: string | Buffer
): Promise<LoadedFont> {
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: Record<string, any>, i: any) => ({ ...acc, ...i }),
{}
);
const pages: LoadedFont["pages"] = [];
const chars: LoadedFont["chars"] = [];
const kernings: LoadedFont["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,
} satisfies LoadedFont;
} else if (typeof bufferOrUrl === "string") {
const res = await fetch(bufferOrUrl);
const text = await res.text();
return parseFont(bufferOrUrl, text);

@@ -77,7 +139,5 @@ } else {

type RawFont = Awaited<ReturnType<typeof loadBitmapFontData>>;
export type ResolveBmFont = Omit<BmFont, "pages"> & Pick<RawFont, "pages">;
export async function loadBitmapFont(
bufferOrUrl: string | Buffer,
): Promise<Omit<BmFont, "pages"> & Pick<RawFont, "pages">> {
const font = await loadBitmapFontData(bufferOrUrl);
export async function processBitmapFont(file: string, font: LoadedFont) {
const chars: Record<string, BmCharacter> = {};

@@ -103,3 +163,8 @@ const kernings: Record<string, BmKerning> = {};

kernings,
pages: await Promise.all(
font.pages.map(async (page) =>
CharacterJimp.read(path.join(path.dirname(file), page))
)
),
};
}

@@ -1,8 +0,7 @@

import { loadBitmapFont } from "./load-bitmap-font.js";
import { createJimp } from "@jimp/core";
import png from "@jimp/js-png";
import path from "path";
import {
isWebWorker,
loadBitmapFontData,
processBitmapFont,
} from "./load-bitmap-font.js";
const CharacterJimp = createJimp({ formats: [png] });
/**

@@ -26,3 +25,3 @@ * Loads a Bitmap Font from a file.

if (typeof window === "undefined") {
if (typeof window === "undefined" && !isWebWorker) {
const { existsSync, promises: fs } = await import("fs");

@@ -35,12 +34,4 @@

const font = await loadBitmapFont(fileOrBuffer);
return {
...font,
pages: await Promise.all(
font.pages.map(async (page) =>
CharacterJimp.read(path.join(path.dirname(file), page)),
),
),
};
const data = await loadBitmapFontData(fileOrBuffer);
return processBitmapFont(file, data);
}

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc