Skip to content

Commit

Permalink
Refactorings, fixed typecheck errors
Browse files Browse the repository at this point in the history
  • Loading branch information
vpmedia committed Nov 21, 2024
1 parent 8e8f162 commit 5b0563e
Show file tree
Hide file tree
Showing 56 changed files with 65 additions and 178 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @vpmedia/bitcharify

[![npm version](https://badge.fury.io/js/@vpmedia%2Fbitcharify.svg?v=1.11.0)](https://badge.fury.io/js/@vpmedia%2Fbitcharify)
[![npm version](https://badge.fury.io/js/@vpmedia%2Fbitcharify.svg?v=1.12.0)](https://badge.fury.io/js/@vpmedia%2Fbitcharify)
[![Node.js CI](https://github.com/vpmedia/bitcharify/actions/workflows/ci.yml/badge.svg)](https://github.com/vpmedia/bitcharify/actions/workflows/ci.yml)

@vpmedia/bitcharify is a bitmap font generator targeting the Phaser game engine.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vpmedia/bitcharify",
"version": "1.11.0",
"version": "1.12.0",
"description": "@vpmedia/bitcharify is a bitmap font generator",
"author": "Andras Csizmadia <[email protected]> (www.vpmedia.hu)",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions src/bitcharify/core/drawGlyph.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { toFontString } from './toFontString.js';
* @param {number} resolution - TBD.
* @param {object} style - TBD.
*/
export function drawGlyph(canvas, context, metrics, x, y, resolution, style) {
export const drawGlyph = (canvas, context, metrics, x, y, resolution, style) => {
const char = metrics.text;
const fontProperties = metrics.fontProperties;
context.translate(x, y);
Expand Down Expand Up @@ -39,4 +39,4 @@ export function drawGlyph(canvas, context, metrics, x, y, resolution, style) {
}
context.setTransform(1, 0, 0, 1, 0, 0); // defaults needed for older browsers (e.g. Opera 29)
context.fillStyle = 'rgba(0, 0, 0, 0)';
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/generateChars.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { measureText } from './measureText.js';
* @param {string[]} charList - TBD.
* @throws Error.
*/
export function generateChars(canvas, context, config, style, fontData, charList) {
export const generateChars = (canvas, context, config, style, fontData, charList) => {
const resolution = config.resolution;
const textureWidth = config.width;
const textureHeight = config.height;
Expand Down Expand Up @@ -63,4 +63,4 @@ export function generateChars(canvas, context, config, style, fontData, charList
positionX += (textureGlyphWidth + 2 * padding) * resolution;
positionX = Math.ceil(positionX);
}
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/generateKernings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param {object} fontData - TBD.
* @param {object[]} charList - TBD.
*/
export function generateKernings(context, fontData, charList) {
export const generateKernings = (context, fontData, charList) => {
for (let i = 0, len = charList.length; i < len; i++) {
const first = charList[i];
for (let j = 0; j < len; j++) {
Expand All @@ -22,4 +22,4 @@ export function generateKernings(context, fontData, charList) {
}
}
}
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/getConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { CHARS_ALPHANUMERIC } from './const.js';
* TBD.
* @returns {object} TBD.
*/
export function getConfig() {
export const getConfig = () => {
return {
resolution: 1,
width: 512,
height: 512,
padding: 4,
chars: CHARS_ALPHANUMERIC,
};
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/getFontData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* TBD.
* @returns {object} TBD.
*/
export function getFontData() {
export const getFontData = () => {
return {
info: [],
common: [],
Expand All @@ -11,4 +11,4 @@ export function getFontData() {
kerning: [],
distanceField: [],
};
}
};
6 changes: 3 additions & 3 deletions src/bitcharify/core/getTextMetrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @param {object} fontProperties - TBD.
* @returns {object} TBD.
*/
export function getTextMetrics(
export const getTextMetrics = (
text,
style,
width,
Expand All @@ -21,7 +21,7 @@ export function getTextMetrics(
lineHeight,
maxLineWidth,
fontProperties
) {
) => {
return {
text,
style,
Expand All @@ -33,4 +33,4 @@ export function getTextMetrics(
maxLineWidth,
fontProperties,
};
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/graphemeSegmenter.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* @param {string} s - TBD.
* @returns {string[]} TBD.
*/
export function graphemeSegmenter(s) {
export const graphemeSegmenter = (s) => {
if (typeof Intl?.Segmenter !== 'function') {
return [...s];
}
const segmenter = new Intl.Segmenter();
return [...segmenter.segment(s)].map((x) => x.segment);
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/isBreakingSpace.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const BREAKING_SPACES = [
* @param {string} char - TBD.
* @returns {boolean} TBD.
*/
export function isBreakingSpace(char) {
export const isBreakingSpace = (char) => {
if (typeof char !== 'string') {
return false;
}
return BREAKING_SPACES.includes(char.charCodeAt(0));

Check warning on line 28 in src/bitcharify/core/isBreakingSpace.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Prefer `String#codePointAt()` over `String#charCodeAt()`
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/isNewLine.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const NEW_LINES = [
* @param {string} char - TBD.
* @returns {object} TBD.
*/
export function isNewline(char) {
export const isNewline = (char) => {
if (typeof char !== 'string') {
return false;
}
return NEW_LINES.includes(char.charCodeAt(0));

Check warning on line 15 in src/bitcharify/core/isNewLine.js

View workflow job for this annotation

GitHub Actions / build (20.x)

Prefer `String#codePointAt()` over `String#charCodeAt()`
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/loadImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {string} url - TBD.
* @returns {Promise} TBD.
*/
export function loadImage(url) {
export const loadImage = (url) => {
return new Promise((resolve, reject) => {
const image = new Image();
const createListeners = () => {
Expand All @@ -25,4 +25,4 @@ export function loadImage(url) {
createListeners();
image.src = url;
});
}
};
6 changes: 3 additions & 3 deletions src/bitcharify/core/measureText.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { graphemeSegmenter } from './graphemeSegmenter.js';
import { getTextMetrics } from './getTextMetrics.js';
import { graphemeSegmenter } from './graphemeSegmenter.js';
import { toFontString } from './toFontString.js';

const METRICS_STRING = '|ÉqÅ';
Expand Down Expand Up @@ -131,7 +131,7 @@ function _wordWrap(text, style, canvas) {
* @param {HTMLCanvasElement} canvas - TBD.
* @returns {object} TBD.
*/
export function measureText(text, style, wordWrap, canvas) {
export const measureText = (text, style, wordWrap, canvas) => {
wordWrap = wordWrap === undefined || wordWrap === null ? style.wordWrap : wordWrap;
const font = toFontString(style);
const fontProperties = _measureFont(font);
Expand Down Expand Up @@ -175,4 +175,4 @@ export function measureText(text, style, wordWrap, canvas) {
maxLineWidth,
fontProperties
);
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/resolveCharacters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @returns {string[]} TBD.
* @throws Error.
*/
export function resolveCharacters(chars) {
export const resolveCharacters = (chars) => {
if (typeof chars === 'string') {
chars = [chars];
}
Expand All @@ -31,4 +31,4 @@ export function resolveCharacters(chars) {
throw new Error('Empty set when resolving characters.');
}
return result;
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/toFontString.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @param {object} style - TBD.
* @returns {string} TBD.
*/
export function toFontString(style) {
export const toFontString = (style) => {
const fontSizeString = typeof style.fontSize === 'number' ? `${style.fontSize}px` : style.fontSize;
let fontFamilies = style.fontFamily;
if (!Array.isArray(style.fontFamily)) {
Expand All @@ -17,4 +17,4 @@ export function toFontString(style) {
fontFamilies[i] = fontFamily;
}
return `${style.fontStyle} ${style.fontVariant} ${style.fontWeight} ${fontSizeString} ${fontFamilies.join(',')}`;
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/core/tokenize.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { isNewline } from './isNewLine.js';
* @param {string} text - TBD.
* @returns {string[]} TBD.
*/
export function tokenize(text) {
export const tokenize = (text) => {
const tokens = [];
let token = '';
if (typeof text !== 'string') {
Expand All @@ -29,4 +29,4 @@ export function tokenize(text) {
tokens.push(token);
}
return tokens;
}
};
10 changes: 5 additions & 5 deletions src/bitcharify/generateBitmapFont.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { generateChars } from './core/generateChars.js';
import { generateKernings } from './core/generateKernings.js';
import { getConfig } from './core/getConfig.js';
import { resolveCharacters } from './core/resolveCharacters.js';
import { getFontData } from './core/getFontData.js';
import { generateKernings } from './core/generateKernings.js';
import { generateChars } from './core/generateChars.js';
import { resolveCharacters } from './core/resolveCharacters.js';

/**
* TBD.
Expand All @@ -11,7 +11,7 @@ import { generateChars } from './core/generateChars.js';
* @param {object} options - TBD.
* @returns {object} TBD.
*/
export function generateBitmapFont(canvas, style, options = {}) {
export const generateBitmapFont = (canvas, style, options = {}) => {
// init config
const defaultConfig = getConfig();
const config = { ...defaultConfig, ...options };
Expand Down Expand Up @@ -54,4 +54,4 @@ export function generateBitmapFont(canvas, style, options = {}) {
generateKernings(context, data, charList);
// process completed
return data;
}
};
4 changes: 2 additions & 2 deletions src/bitcharify/generateBitmapFonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { generateBitmapFont } from './generateBitmapFont.js';
* @param {object} options - TBD.
* @returns {object[]} TBD.
*/
export function generateBitmapFonts(canvas, styles, options = {}) {
export const generateBitmapFonts = (canvas, styles, options = {}) => {
const results = [];
styles.forEach((fontStyle) => {
const fontData = generateBitmapFont(canvas, fontStyle, options);
results.push({ imageData: canvas.toDataURL(), fontData, fontStyle });
});
return results;
}
};
1 change: 0 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ export { CHARS_ALPHA, CHARS_ALPHANUMERIC, CHARS_ASCII, CHARS_NUMERIC } from './b
export { loadImage } from './bitcharify/core/loadImage.js';
export { generateBitmapFont } from './bitcharify/generateBitmapFont.js';
export { generateBitmapFonts } from './bitcharify/generateBitmapFonts.js';

5 changes: 1 addition & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
"jsx": "react-jsx",
"module": "NodeNext",
"moduleResolution": "nodenext",
"types": [
"jest",
"node"
],
"types": ["jest", "node"],
"allowJs": true,
"checkJs": true,
"declaration": true,
Expand Down
4 changes: 2 additions & 2 deletions typedefs/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare global {
interface Window {
__BITCHARIFY_CANVAS__: HTMLCanvasElement|OffscreenCanvas;
__BITCHARIFY_CONTEXT__: CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D;
__BITCHARIFY_CANVAS__: HTMLCanvasElement | OffscreenCanvas;
__BITCHARIFY_CONTEXT__: CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
}
}

Expand Down
10 changes: 0 additions & 10 deletions types/bitcharify/core/drawGlyph.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
/**
* TBD.
* @param {HTMLCanvasElement} canvas - TBD.
* @param {CanvasRenderingContext2D} context - TBD.
* @param {object} metrics - TBD.
* @param {number} x - TBD.
* @param {number} y - TBD.
* @param {number} resolution - TBD.
* @param {object} style - TBD.
*/
export function drawGlyph(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D, metrics: object, x: number, y: number, resolution: number, style: object): void;
//# sourceMappingURL=drawGlyph.d.ts.map
2 changes: 1 addition & 1 deletion types/bitcharify/core/drawGlyph.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions types/bitcharify/core/generateChars.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,2 @@
/**
* TBD.
* @param {HTMLCanvasElement} canvas - TBD.
* @param {CanvasRenderingContext2D} context - TBD.
* @param {object} config - TBD.
* @param {object} style - TBD.
* @param {object} fontData - TBD.
* @param {string[]} charList - TBD.
* @throws Error.
*/
export function generateChars(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D, config: object, style: object, fontData: object, charList: string[]): void;
//# sourceMappingURL=generateChars.d.ts.map
2 changes: 1 addition & 1 deletion types/bitcharify/core/generateChars.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions types/bitcharify/core/generateKernings.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
/**
* TBD.
* @param {CanvasRenderingContext2D} context - TBD.
* @param {object} fontData - TBD.
* @param {object[]} charList - TBD.
*/
export function generateKernings(context: CanvasRenderingContext2D, fontData: object, charList: object[]): void;
//# sourceMappingURL=generateKernings.d.ts.map
2 changes: 1 addition & 1 deletion types/bitcharify/core/generateKernings.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions types/bitcharify/core/getConfig.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
/**
* TBD.
* @returns {object} TBD.
*/
export function getConfig(): object;
//# sourceMappingURL=getConfig.d.ts.map
2 changes: 1 addition & 1 deletion types/bitcharify/core/getConfig.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions types/bitcharify/core/getFontData.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@
/**
* TBD.
* @returns {object} TBD.
*/
export function getFontData(): object;
//# sourceMappingURL=getFontData.d.ts.map
2 changes: 1 addition & 1 deletion types/bitcharify/core/getFontData.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5b0563e

Please sign in to comment.