All files / client lazyFont.js

100% Statements 111/111
89.74% Branches 35/39
100% Functions 15/15
100% Lines 101/101

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334                                  19x             61x 61x 61x     61x 61x 61x       110592x 884736x         61x 61x     61x 61x                 61x   150421x   150421x 86478x   74956x   74956x 74956x       2951168x     2951168x     74956x     11522x 11522x 11522x         11522x 11522x       1474816x     1474816x     11522x     11522x 11522x 11522x 11522x                       11522x           11522x       150421x                 61x 4887x   4887x   4880x             4880x   4880x 4880x       276480x 94208x       4880x 4880x     4880x 4160x   720x 720x 720x 720x                     720x         4887x               61x 65x   65x 64x 64x 64x 64x   64x 1280x     64x     65x             61x 61x               61x 976x 15616x 78080x           976x               976x 976x 4880x         976x 4x 64x           61x   61x 1x     50x 50x 2x       11x 1x                       72322x   72322x 1x           72321x                           2x       2x   2x 1x 1x     1x 1x                         1x          
/**
 * Lazy Font Loader - Only generates glyphs on demand
 * This module provides a memory-efficient font loading system that generates
 * character glyphs only when they are first needed, rather than pre-generating
 * all 65,536 possible combinations (16 foregrounds × 16 backgrounds × 256 characters).
 */
import { createCanvas } from './ui.js';
import magicNumbers from './magicNumbers.js';
 
/**
 * Creates a lazy font loader that generates glyphs on demand
 * @param {Object} fontData - Font data with width, height, and bitmap data
 * @param {Object} palette - Palette object with getRGBAColor method
 * @param {boolean} letterSpacing - Whether to use letter spacing
 * @param {number} scaleFactor - Scale factor for zoom (0.5x - 4x, default 1)
 * @returns {Object} Lazy font object with methods for drawing and glyph access
 */
export const createLazyFont = (
	fontData,
	palette,
	letterSpacing = false,
	scaleFactor = 1,
) => {
	// Cache for generated glyphs
	const glyphCache = new Map();
	const alphaGlyphCache = new Map();
	const letterSpacingCache = new Map();
 
	// Pre-generate bitmap data once
	const bits = new Uint8Array(fontData.width * fontData.height * 256);
	for (
		let i = 0, k = 0;
		i < (fontData.width * fontData.height * 256) / 8;
		i += 1
	) {
		for (let j = 7; j >= 0; j -= 1, k += 1) {
			bits[k] = (fontData.data[i] >> j) & 1;
		}
	}
 
	// Calculate scaled dimensions
	const scaledWidth = Math.floor(fontData.width * scaleFactor);
	const scaledHeight = Math.floor(fontData.height * scaleFactor);
 
	// Canvas for glyph generation at ORIGINAL size
	const canvas = createCanvas(fontData.width, fontData.height);
	const ctx = canvas.getContext('2d');
 
	/**
	 * Generate a single glyph on demand with scaling
	 * @param {number} charCode - Character code (0-255)
	 * @param {number} foreground - Foreground color (0-15)
	 * @param {number} background - Background color (0-15)
	 * @returns {ImageData} Generated glyph image data at scaled size
	 */
	const getGlyph = (charCode, foreground, background) => {
		// Include scaleFactor in cache key
		const key = `${charCode}-${foreground}-${background}-${scaleFactor}`;
 
		if (!glyphCache.has(key)) {
			if (scaleFactor === 1) {
				// No scaling - create directly at original size
				const imageData = ctx.createImageData(fontData.width, fontData.height);
 
				for (
					let i = 0, j = charCode * fontData.width * fontData.height;
					i < fontData.width * fontData.height;
					i += 1, j += 1
				) {
					const color = palette.getRGBAColor(
						bits[j] === 1 ? foreground : background,
					);
					imageData.data.set(color, i * 4);
				}
 
				glyphCache.set(key, imageData);
			} else {
				// Scaling needed - generate at original size, then scale
				const tempCanvas = createCanvas(fontData.width, fontData.height);
				const tempCtx = tempCanvas.getContext('2d');
				const imageData = tempCtx.createImageData(
					fontData.width,
					fontData.height,
				);
 
				for (
					let i = 0, j = charCode * fontData.width * fontData.height;
					i < fontData.width * fontData.height;
					i += 1, j += 1
				) {
					const color = palette.getRGBAColor(
						bits[j] === 1 ? foreground : background,
					);
					imageData.data.set(color, i * 4);
				}
 
				tempCtx.putImageData(imageData, 0, 0);
 
				// Scale using nearest-neighbor
				const scaledCanvas = createCanvas(scaledWidth, scaledHeight);
				const scaledCtx = scaledCanvas.getContext('2d');
				scaledCtx.imageSmoothingEnabled = false;
				scaledCtx.drawImage(
					tempCanvas,
					0,
					0,
					fontData.width,
					fontData.height,
					0,
					0,
					scaledWidth,
					scaledHeight,
				);
 
				const scaledImageData = scaledCtx.getImageData(
					0,
					0,
					scaledWidth,
					scaledHeight,
				);
				glyphCache.set(key, scaledImageData);
			}
		}
 
		return glyphCache.get(key);
	};
 
	/**
	 * Generate an alpha glyph (transparent background) with scaling
	 * @param {number} charCode - Character code
	 * @param {number} foreground - Foreground color (0-15)
	 * @returns {HTMLCanvasElement} Canvas with alpha glyph at scaled size
	 */
	const getAlphaGlyph = (charCode, foreground) => {
		const key = `${charCode}-${foreground}-${scaleFactor}`;
 
		if (!alphaGlyphCache.has(key)) {
			// Only generate alpha glyphs for specific characters
			Eif (
				charCode === magicNumbers.LOWER_HALFBLOCK ||
				charCode === magicNumbers.UPPER_HALFBLOCK ||
				charCode === magicNumbers.CHAR_SLASH ||
				charCode === magicNumbers.CHAR_PIPE ||
				charCode === magicNumbers.CHAR_CAPITAL_X
			) {
				const imageData = ctx.createImageData(fontData.width, fontData.height);
 
				for (
					let i = 0, j = charCode * fontData.width * fontData.height;
					i < fontData.width * fontData.height;
					i += 1, j += 1
				) {
					if (bits[j] === 1) {
						imageData.data.set(palette.getRGBAColor(foreground), i * 4);
					}
				}
 
				const tempCanvas = createCanvas(fontData.width, fontData.height);
				tempCanvas.getContext('2d').putImageData(imageData, 0, 0);
 
				// Scale if needed
				if (scaleFactor === 1) {
					alphaGlyphCache.set(key, tempCanvas);
				} else {
					const scaledCanvas = createCanvas(scaledWidth, scaledHeight);
					const scaledCtx = scaledCanvas.getContext('2d');
					scaledCtx.imageSmoothingEnabled = false;
					scaledCtx.drawImage(
						tempCanvas,
						0,
						0,
						fontData.width,
						fontData.height,
						0,
						0,
						scaledWidth,
						scaledHeight,
					);
					alphaGlyphCache.set(key, scaledCanvas);
				}
			}
		}
 
		return alphaGlyphCache.get(key);
	};
 
	/**
	 * Get letter spacing image data for a specific color with scaling
	 * @param {number} colorIndex - Color index (0-15)
	 * @returns {ImageData} Letter spacing image data at scaled size
	 */
	const getLetterSpacingData = colorIndex => {
		const key = `${colorIndex}-${scaleFactor}`;
 
		if (!letterSpacingCache.has(key)) {
			const spacingCanvas = createCanvas(1, scaledHeight);
			const spacingCtx = spacingCanvas.getContext('2d');
			const imageData = spacingCtx.getImageData(0, 0, 1, scaledHeight);
			const color = palette.getRGBAColor(colorIndex);
 
			for (let j = 0; j < scaledHeight; j++) {
				imageData.data.set(color, j * 4);
			}
 
			letterSpacingCache.set(key, imageData);
		}
 
		return letterSpacingCache.get(key);
	};
 
	/**
	 * Pre-generate commonly used glyphs for instant access
	 * Common characters: space (32) and block characters (176, 177, 178, 219)
	 */
	const preGenerateCommonGlyphs = () => {
		const commonChars = [
			32, // Space
			176, // Light block ░
			177, // Medium block ▒
			178, // Dark block ▓
			219, // Full block █
		];
 
		for (let fg = 0; fg < 16; fg++) {
			for (let bg = 0; bg < 16; bg++) {
				commonChars.forEach(charCode => {
					getGlyph(charCode, fg, bg);
				});
			}
		}
 
		// Also pre-generate alpha glyphs for special drawing characters
		const alphaChars = [
			magicNumbers.LOWER_HALFBLOCK,
			magicNumbers.UPPER_HALFBLOCK,
			magicNumbers.CHAR_SLASH,
			magicNumbers.CHAR_PIPE,
			magicNumbers.CHAR_CAPITAL_X,
		];
 
		for (let fg = 0; fg < 16; fg++) {
			alphaChars.forEach(charCode => {
				getAlphaGlyph(charCode, fg);
			});
		}
 
		// Pre-generate all letter spacing data
		if (letterSpacing) {
			for (let i = 0; i < 16; i++) {
				getLetterSpacingData(i);
			}
		}
	};
 
	// Pre-generate common glyphs on initialization
	preGenerateCommonGlyphs();
 
	return {
		getData: () => fontData,
		// Return scaled dimensions
		getWidth: () =>
			letterSpacing ? scaledWidth + Math.floor(1 * scaleFactor) : scaledWidth,
		getHeight: () => scaledHeight,
		getScaleFactor: () => scaleFactor,
		getGlyph: getGlyph,
		getAlphaGlyph: getAlphaGlyph,
		getLetterSpacingData: getLetterSpacingData,
		getCacheSize: () => glyphCache.size,
		getAlphaCacheSize: () => alphaGlyphCache.size,
 
		/**
		 * Draw a character at specified position (scaling handled internally)
		 * @param {number} charCode - Character code
		 * @param {number} foreground - Foreground color
		 * @param {number} background - Background color
		 * @param {CanvasRenderingContext2D} drawCtx - Canvas context to draw on
		 * @param {number} x - X coordinate in character grid
		 * @param {number} y - Y coordinate in character grid
		 */
		draw: (charCode, foreground, background, drawCtx, x, y) => {
			const glyph = getGlyph(charCode, foreground, background);
 
			if (letterSpacing) {
				drawCtx.putImageData(
					glyph,
					x * (scaledWidth + Math.floor(1 * scaleFactor)),
					y * scaledHeight,
				);
			} else {
				drawCtx.putImageData(glyph, x * scaledWidth, y * scaledHeight);
			}
		},
 
		/**
		 * Draw a character with alpha transparency (scaling handled internally)
		 * @param {number} charCode - Character code
		 * @param {number} foreground - Foreground color
		 * @param {CanvasRenderingContext2D} drawCtx - Canvas context to draw on
		 * @param {number} x - X coordinate in character grid
		 * @param {number} y - Y coordinate in character grid
		 */
		drawWithAlpha: (charCode, foreground, drawCtx, x, y) => {
			// Use fallback character (X) if requested character has no alpha glyph
			const effectiveCharCode = getAlphaGlyph(charCode, foreground)
				? charCode
				: magicNumbers.CHAR_CAPITAL_X;
 
			const canvasToUse = getAlphaGlyph(effectiveCharCode, foreground);
 
			if (letterSpacing) {
				const effectiveWidth = scaledWidth + Math.floor(1 * scaleFactor);
				drawCtx.drawImage(canvasToUse, x * effectiveWidth, y * scaledHeight);
 
				// Handle special line drawing characters
				Eif (effectiveCharCode >= 192 && effectiveCharCode <= 223) {
					drawCtx.drawImage(
						canvasToUse,
						scaledWidth - 1,
						0,
						1,
						scaledHeight,
						x * effectiveWidth + scaledWidth,
						y * scaledHeight,
						1,
						scaledHeight,
					);
				}
			} else {
				drawCtx.drawImage(canvasToUse, x * scaledWidth, y * scaledHeight);
			}
		},
	};
};