@vyaz/core 0.0.9 → 0.0.10

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.
@@ -18208,6 +18208,21 @@ class FontMetricsProvider {
18208
18208
  cache = new Map;
18209
18209
  metricsCache = new Map;
18210
18210
  mode = "browser";
18211
+ pendingRegistrations = new Set;
18212
+ _measureCanvas = null;
18213
+ _measureCtx = null;
18214
+ _getMeasureContext() {
18215
+ if (!this._measureCtx) {
18216
+ if (typeof OffscreenCanvas !== "undefined") {
18217
+ this._measureCanvas = new OffscreenCanvas(1, 1);
18218
+ this._measureCtx = this._measureCanvas.getContext("2d");
18219
+ } else {
18220
+ this._measureCanvas = document.createElement("canvas");
18221
+ this._measureCtx = this._measureCanvas.getContext("2d");
18222
+ }
18223
+ }
18224
+ return this._measureCtx;
18225
+ }
18211
18226
  setMode(mode) {
18212
18227
  if (this.mode === mode)
18213
18228
  return;
@@ -18223,6 +18238,15 @@ class FontMetricsProvider {
18223
18238
  return this.mode;
18224
18239
  }
18225
18240
  async registerFont(family, options, source, sourcePath) {
18241
+ const promise = this._registerFontInternal(family, options, source, sourcePath);
18242
+ this.pendingRegistrations.add(promise);
18243
+ try {
18244
+ await promise;
18245
+ } finally {
18246
+ this.pendingRegistrations.delete(promise);
18247
+ }
18248
+ }
18249
+ async _registerFontInternal(family, options, source, sourcePath) {
18226
18250
  const { createFontFace: createFontFace2 } = await Promise.resolve().then(() => exports_FontEngine);
18227
18251
  const { registerCanvasFont: registerCanvasFont2 } = await init_canvas_polyfill().then(() => exports_canvas_polyfill);
18228
18252
  if (typeof source === "string") {
@@ -18237,11 +18261,18 @@ class FontMetricsProvider {
18237
18261
  registerCanvasFont2(sourcePath, family);
18238
18262
  }
18239
18263
  }
18264
+ async waitForPendingRegistrations() {
18265
+ await Promise.all(this.pendingRegistrations);
18266
+ }
18240
18267
  getFont(family, weight = "normal", style = "normal") {
18241
18268
  const key = cacheKey2(family, weight, style);
18242
18269
  return this.cache.get(key);
18243
18270
  }
18244
18271
  getMetrics(fontFamily, fontSize, weight = "normal", style = "normal") {
18272
+ const _process2 = typeof globalThis !== "undefined" ? globalThis.process : undefined;
18273
+ if (this.pendingRegistrations.size > 0 && _process2?.env?.NODE_ENV !== "production") {
18274
+ console.warn("[vyaz] getMetrics() called while registerFont() promises are still pending. Wait for registerFont() to resolve before layout to avoid inaccurate metrics. Use fontMetricsProvider.waitForPendingRegistrations() if needed.");
18275
+ }
18245
18276
  const key = cacheKey2(fontFamily, weight, style);
18246
18277
  const metricsKey = `${key}_${fontSize}_${this.mode}`;
18247
18278
  const cached = this.metricsCache.get(metricsKey);
@@ -18277,8 +18308,7 @@ class FontMetricsProvider {
18277
18308
  }
18278
18309
  if (typeof document !== "undefined") {
18279
18310
  try {
18280
- const canvas = document.createElement("canvas");
18281
- const ctx = canvas.getContext("2d");
18311
+ const ctx = this._getMeasureContext();
18282
18312
  ctx.font = `${style} ${weight} ${fontSize}px ${fontFamily}`;
18283
18313
  const m = ctx.measureText("M");
18284
18314
  const metrics = {
package/dist/index.d.ts CHANGED
@@ -5,7 +5,46 @@
5
5
  * layout engines, font metric providers, renderers, and utilities.
6
6
  */
7
7
  export type { TextFrame, Paragraph, ParagraphStyle, TextRun, InlineWidget, AutofitConfig, TextAlignment, WritingMode, TextOrientation, VerticalAlignment, ScriptType, WhiteSpace, MultiColumnConfig, DominantBaseline, LineFitEdge, TextAlignLast, WordBreak, LineBreak, OverflowWrap, TextDecorationStyle, TextTransform, ListType, NumberFormat, ListStylePosition, ListStyle, } from './types/Document.js';
8
- export { DEFAULT_PARAGRAPH_STYLE, DEFAULT_TEXT_STYLE, } from './types/Document.js';
8
+ export declare const DEFAULT_PARAGRAPH_STYLE: {
9
+ alignment: import("./index.js").TextAlignment;
10
+ lineHeight: number;
11
+ spaceBefore: number;
12
+ spaceAfter: number;
13
+ indent?: number;
14
+ leftIndent?: number;
15
+ rightIndent?: number;
16
+ textIndent?: number;
17
+ letterSpacing?: number;
18
+ textAlignLast?: import("./index.js").TextAlignLast;
19
+ wordBreak?: import("./index.js").WordBreak;
20
+ lineBreak?: import("./index.js").LineBreak;
21
+ overflowWrap?: import("./index.js").OverflowWrap;
22
+ hyphens?: boolean;
23
+ whiteSpace?: import("./index.js").WhiteSpace;
24
+ listStyle?: import("./index.js").ListStyle;
25
+ listRestart?: boolean;
26
+ };
27
+ export declare const DEFAULT_TEXT_STYLE: {
28
+ type?: "text" | "inline-box" | undefined;
29
+ text?: string | undefined;
30
+ inlineWidget?: import("./index.js").InlineWidget | undefined;
31
+ fontFamily?: string | undefined;
32
+ fontSize?: number | undefined;
33
+ fontWeight?: number | "bold" | "normal" | undefined;
34
+ fontStyle?: "normal" | "italic" | undefined;
35
+ color?: string | undefined;
36
+ backgroundColor?: string | undefined;
37
+ letterSpacing?: number | undefined;
38
+ script?: import("./index.js").ScriptType | undefined;
39
+ underline?: boolean | undefined;
40
+ strikethrough?: boolean | undefined;
41
+ overline?: boolean | undefined;
42
+ textDecorationStyle?: import("./index.js").TextDecorationStyle | undefined;
43
+ textDecorationColor?: string | undefined;
44
+ textTransform?: import("./index.js").TextTransform | undefined;
45
+ fullWidth?: boolean | undefined;
46
+ fullSizeKana?: boolean | undefined;
47
+ };
9
48
  export type { ParagraphLayoutResult, Line, Span, SpanFontMetrics, SemanticParagraph, SemanticLine, SemanticFragment, } from './types/LayoutTypes.js';
10
49
  export type { FontMetrics, IFontMetricsProvider, GlyphData, } from './types/FontTypes.js';
11
50
  export { ParagraphLayoutEngine, paragraphLayoutEngine } from './layout/ParagraphLayoutEngine.js';
@@ -19,7 +58,12 @@ export type { AutoFitOptions, AutoFitResult } from './layout/AutoFitEngine.js';
19
58
  export { groupLinesByParagraph } from './utils/groupLinesByParagraph.js';
20
59
  export type { ParagraphGroup } from './utils/groupLinesByParagraph.js';
21
60
  export { transformText } from './utils/textTransform.js';
22
- export { formatListNumber, defaultBulletChar, BULLET_CHARACTERS } from './utils/list.js';
61
+ import { formatListNumber as _fln, defaultBulletChar as _dbc } from './utils/list.js';
62
+ export declare const formatListNumber: typeof _fln;
63
+ export declare const defaultBulletChar: typeof _dbc;
64
+ export declare const BULLET_CHARACTERS: {
65
+ [x: number]: string;
66
+ };
23
67
  export { compileParagraph, getParagraphText, makeFontToken, splitParagraphByHardBreaks, collapseSegmentWhitespace } from './compile/DocumentCompiler.js';
24
68
  export type { PreparedRichInlineItem } from './compile/DocumentCompiler.js';
25
69
  export type { FontFace } from './measure/FontEngine.js';