@vyaz/core 0.0.9 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,47 +2,69 @@
2
2
  * FontMetricsProvider.ts — isomorphic font metrics provider.
3
3
  *
4
4
  * Strategy (priority):
5
- * 1. FontEngine (fontkit) — from registered buffer
6
- * - 'browser' mode: hhea.ascender / hhea.descender
7
- * - 'office' mode: OS/2.usWinAscent / OS/2.usWinDescent
5
+ * 1. FontEngine (fontkit) — from registered buffer, with smart weight fallback
8
6
  * 2. Canvas TextMetrics (browser fallback when fontkit unavailable)
9
7
  *
10
- * Uses FontEngine as the single entry point for all fontkit operations.
8
+ * Key features:
9
+ * - Nested registry: family → variants (weight + style)
10
+ * - Smart weight fallback: if exact weight not found, picks closest
11
+ * - Style fallback: if exact style not found, falls back to 'normal'
12
+ * - Tracks pending registrations to warn about race conditions
13
+ * - Reusable off-screen canvas for Canvas TextMetrics fallback
11
14
  */
12
15
  import type { FontMetrics, IFontMetricsProvider } from '../types/FontTypes.js';
13
16
  import type { FontFace } from './FontEngine.js';
14
17
  /**
15
18
  * Factor used when a glyph is not found in the font.
16
19
  * Multiplied by fontSize to estimate the missing glyph width.
17
- * Used across all measurement code paths (ParagraphLayoutEngine, canvas-polyfill).
18
20
  */
19
21
  export declare const MISSING_GLYPH_FACTOR = 0.5;
20
22
  export declare class FontMetricsProvider implements IFontMetricsProvider {
21
- /** Map<string, FontFace> — font engine font face cache */
22
- private cache;
23
+ /**
24
+ * Nested registry: family → variantKey → FontFace.
25
+ * Example:
26
+ * "Roboto" → { "400_normal": FontFace, "700_normal": FontFace, "400_italic": FontFace }
27
+ */
28
+ private registry;
29
+ /** Metrics cache keyed by variantKey_fontSize_mode */
23
30
  private metricsCache;
24
31
  private mode;
32
+ private pendingRegistrations;
33
+ private _measureCanvas;
34
+ private _measureCtx;
35
+ private _getMeasureContext;
25
36
  setMode(mode: 'browser' | 'office'): void;
26
37
  getMode(): 'browser' | 'office';
27
38
  /**
28
- * Register a binary font for use with fontkit.
29
- *
30
- * In both Node.js and browser the font is loaded via FontEngine.
31
- * In the browser the caller must provide font bytes (e.g. fetched via
32
- * `getFontBuffer()` from `../utils/font.js`).
33
- *
34
- * @param source Font file bytes (ArrayBuffer / Uint8Array), or a URL string
35
- * @param sourcePath Optional filesystem path (used for @napi-rs/canvas in Node.js)
39
+ * Flatten the nested registry into a single map for office mode.
40
+ * Office mode needs key → FontFace lookup by family_weight_style.
36
41
  */
42
+ private _flattenCache;
37
43
  registerFont(family: string, options: {
38
44
  weight?: string;
39
45
  style?: string;
40
46
  }, source: string | ArrayBuffer | Uint8Array, sourcePath?: string): Promise<void>;
47
+ private _registerFontInternal;
48
+ waitForPendingRegistrations(): Promise<void>;
49
+ /**
50
+ * List all families registered in the provider.
51
+ */
52
+ getRegisteredFamilies(): string[];
41
53
  /**
42
- * Get font engine FontFace object for per-character calculations.
43
- * Returns undefined if font is not registered.
54
+ * Get all variant keys registered for a given family.
55
+ * Returns empty array if family not found.
56
+ */
57
+ getFamilyVariants(family: string): string[];
58
+ /**
59
+ * Get font engine FontFace for per-character calculations.
60
+ * Uses the same smart fallback logic as getMetrics().
44
61
  */
45
62
  getFont(family: string, weight?: string, style?: string): FontFace | undefined;
63
+ /**
64
+ * Resolve a font request with fallback.
65
+ * Returns { font, resolvedWeight, resolvedStyle } or null.
66
+ */
67
+ private _resolveFont;
46
68
  getMetrics(fontFamily: string, fontSize: number, weight?: string, style?: string): FontMetrics;
47
69
  }
48
70
  /** Singleton */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vyaz/core",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",