emf-converter 4.25.7 → 4.27.0

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.
package/README.md CHANGED
@@ -344,7 +344,7 @@ page. The defaults are `1252` and `437`; explicit text charsets are unchanged.
344
344
  | `maxRecords` | `number` | `200000`/`500000` | Records processed per stream before replay stops (EMF+ uses the higher default unless overridden) |
345
345
  | `gdiAntialias` | `boolean` | `false` (PNG) | `true` smooths every shape edge with Canvas antialiasing instead of reproducing Windows' own GDI/GDI+ rasterisation |
346
346
  | `fonts` | `Array<ArrayBuffer \| ArrayBufferView>` | None | TrueType (`.ttf`/`.ttc`) and raster (`.fon`/`.fnt`) font files for exact GDI text |
347
- | `fontSmoothing` | `'cleartype' \| 'gray' \| 'mono'` | `'cleartype'` | What `DEFAULT_QUALITY` / `DRAFT_QUALITY` / `PROOF_QUALITY` fonts render as (Windows' system setting) |
347
+ | `fontSmoothing` | `'cleartype' \| 'gray' \| 'mono'` | `'cleartype'` | What `DEFAULT_QUALITY` / `DRAFT_QUALITY` / `PROOF_QUALITY` fonts and GDI+ SystemDefault text render as (Windows' system setting) |
348
348
  | `fontFamilyMap` | `Record<string, string>` | None | Without `fonts`: maps Windows face names (case-insensitive) to locally available fonts, e.g. `{ calibri: 'Carlito' }` |
349
349
  | `signal` | `AbortSignal` | None | Cancels the conversion: the promise rejects with `signal.reason` when it aborts (see [Cancellation](#cancellation)) |
350
350
  | `timeoutMs` | `number` | None | Rejects with a `DOMException` named `TimeoutError` after this many milliseconds |
package/dist/browser.js CHANGED
@@ -36984,7 +36984,35 @@ function parseTrueTypeFace(view, base = 0) {
36984
36984
  }
36985
36985
  const locaAt = (g) => longLoca ? view.getUint32(loca.offset + g * 4) : view.getUint16(loca.offset + g * 2) * 2;
36986
36986
  const glyphCache = /* @__PURE__ */ new Map();
36987
+ const kernT = tables.get("kern");
36988
+ let kernPairs = null;
36989
+ const kerning = (left, right) => {
36990
+ if (!kernT) return 0;
36991
+ if (!kernPairs) {
36992
+ kernPairs = /* @__PURE__ */ new Map();
36993
+ const end = kernT.offset + kernT.length;
36994
+ if (kernT.offset + 4 <= end && view.getUint16(kernT.offset) === 0) {
36995
+ const n = view.getUint16(kernT.offset + 2);
36996
+ let o = kernT.offset + 4;
36997
+ for (let t = 0; t < n && o + 6 <= end; t++) {
36998
+ const length = view.getUint16(o + 2);
36999
+ const coverage = view.getUint16(o + 4);
37000
+ if (coverage >> 8 === 0 && (coverage & 7) === 1 && o + 14 <= end) {
37001
+ const pairs = view.getUint16(o + 6);
37002
+ for (let i = 0; i < pairs && o + 14 + i * 6 + 6 <= end; i++) {
37003
+ const p = o + 14 + i * 6;
37004
+ kernPairs.set(view.getUint16(p) * 65536 + view.getUint16(p + 2), view.getInt16(p + 4));
37005
+ }
37006
+ }
37007
+ if (length < 6) break;
37008
+ o += length;
37009
+ }
37010
+ }
37011
+ }
37012
+ return kernPairs.get(left * 65536 + right) ?? 0;
37013
+ };
36987
37014
  return {
37015
+ kerning,
36988
37016
  family: names2.get(1) ?? "",
36989
37017
  subfamily: names2.get(2) ?? "",
36990
37018
  fullName: names2.get(4) ?? "",
@@ -42878,7 +42906,15 @@ var RealizedFont = class {
42878
42906
  this.advanceOutlines = /* @__PURE__ */ new Map();
42879
42907
  this.outlines = /* @__PURE__ */ new Map();
42880
42908
  this.glyphs = /* @__PURE__ */ new Map();
42909
+ /**
42910
+ * The font was asked for at NONANTIALIASED_QUALITY (GDI+ SingleBitPerPixelGridFit), not turned monochrome by
42911
+ * the `gasp` table under AntiAliasGridFit: those glyphs keep the previous path (the native grayscale corpora,
42912
+ * `gdiplus-courier-gray` and `gdiplus-shpix-x`, lose exact glyphs the GDI way).
42913
+ */
42914
+ this.singleBitRequested = false;
42881
42915
  this.reverseCmap = null;
42916
+ this.gdiMonoSize = null;
42917
+ this.gdiMonoOutlines = /* @__PURE__ */ new Map();
42882
42918
  this.ctSize = null;
42883
42919
  /** CLEARTYPE_NATURAL_QUALITY: ClearType glyphs keep their own (natural) widths. */
42884
42920
  this.naturalWidths = false;
@@ -42938,6 +42974,17 @@ var RealizedFont = class {
42938
42974
  * way, at a half turn it rounded (all 530 ascents and descents of five faces at lfHeight -8..-60 for 90, 180 and
42939
42975
  * 270 degrees, escapement and world rotation alike). Undefined for an upright baseline.
42940
42976
  */
42977
+ /**
42978
+ * GDI+ SingleBitPerPixelGridFit glyphs are close to GDI's monochrome glyphs: hinted with GDI's arithmetic and
42979
+ * scan-converted with GDI's converter, 1,658 of 1,680 native upright regular glyphs are exact (`xwvyAkMWKVXZ4&%`,
42980
+ * Arial, Tahoma, Times New Roman and Segoe UI at 9 to 36 px), against 1,442 with the previous arithmetic. GDI+'s
42981
+ * own arithmetic is neither: 15 glyphs (Arial 16 `w` among them) are exact only the old way, and no subset of
42982
+ * GDI's projection, movement and ISECT rules gets both. Italic faces keep the previous path (Times New Roman
42983
+ * Italic 22 in `textx-plus-singlebitgridfit`: 8 pixels the GDI way).
42984
+ */
42985
+ plusMonoAsGdi(gdiPlus, mode, gridFit) {
42986
+ return gdiPlus && mode === "mono" && gridFit && this.singleBitRequested;
42987
+ }
42941
42988
  rotatedMetrics(ux, uy) {
42942
42989
  if (Math.abs(ux) > 1e-9 && Math.abs(uy) > 1e-9) {
42943
42990
  return { ascent: this.rotatedAscent, descent: this.rotatedDescent };
@@ -42972,6 +43019,20 @@ var RealizedFont = class {
42972
43019
  }
42973
43020
  return this.reverseCmap.get(index) ?? 65533;
42974
43021
  }
43022
+ /**
43023
+ * The outline of an upright GDI+ SingleBitPerPixelGridFit glyph, hinted
43024
+ * with GDI's arithmetic (see {@link plusMonoAsGdi}). Advances keep coming
43025
+ * from {@link outline}, whose placement the DrawString fixtures pin.
43026
+ */
43027
+ gdiMonoOutline(index) {
43028
+ let g = this.gdiMonoOutlines.get(index);
43029
+ if (!g) {
43030
+ this.gdiMonoSize ?? (this.gdiMonoSize = new HintedSize(this.ttf, this.ppemX, this.ppem, { version: 35, grayscale: false, gdiArithmetic: true, reportStretched: this.playbackStretched }, true));
43031
+ g = this.gdiMonoSize.hintGlyph(index < this.ttf.numGlyphs ? index : NOTDEF);
43032
+ this.gdiMonoOutlines.set(index, g);
43033
+ }
43034
+ return g;
43035
+ }
42975
43036
  outline(index) {
42976
43037
  let g = this.outlines.get(index);
42977
43038
  if (!g) {
@@ -43118,7 +43179,8 @@ var RealizedFont = class {
43118
43179
  }
43119
43180
  const skew = m && !(Math.abs(m[1]) < 1e-9 && Math.abs(m[2]) < 1e-9) && !(Math.abs(m[0]) < 1e-9 && Math.abs(m[3]) < 1e-9);
43120
43181
  const rotation = skew && m && !this.gdiPlus && this.ppemX === this.ppem ? rotationMatrix(m, this.ppem, (this.ttf.headFlags & 8) !== 0) : null;
43121
- const src = skew ? this.rotatedOutline(index, rotation ? rotation.ppem : 0) : this.outline(index);
43182
+ const asGdiMono = !m && !subX && !subY && this.ppemX === this.ppem && !this.syntheticItalic && !(this.ttf.fsSelection & 1) && this.plusMonoAsGdi(this.gdiPlus, this.mode, this.gridFit);
43183
+ const src = skew ? this.rotatedOutline(index, rotation ? rotation.ppem : 0) : asGdiMono ? this.gdiMonoOutline(index) : this.outline(index);
43122
43184
  if (skew && m && rotation) {
43123
43185
  m = rotation.matrix;
43124
43186
  } else if (skew && m) {
@@ -43161,7 +43223,7 @@ var RealizedFont = class {
43161
43223
  if (this.mode === "mono") {
43162
43224
  const smartGrid = !m && !subX && !subY && (!this.gdiPlus || this.ppemX === this.ppem && !this.syntheticItalic);
43163
43225
  const controlOn = src.scanSet ? src.scanControl : this.ppem <= DEFAULT_DROPOUT_PPEM;
43164
- bitmap = this.gdiPlus ? rasterizeMono(o, this.gdiPlus && !this.gridFit ? UNHINTED_MONO_DROPOUT : dropoutMode(controlOn, src.scanType), void 0, true, smartGrid, false, false, false, false, true) : msMonoBitmap(o, msScanFlags(controlOn, src.scanType));
43226
+ bitmap = this.gdiPlus && !asGdiMono ? rasterizeMono(o, this.gdiPlus && !this.gridFit ? UNHINTED_MONO_DROPOUT : dropoutMode(controlOn, src.scanType), void 0, true, smartGrid, false, false, false, false, true) : msMonoBitmap(o, msScanFlags(controlOn, src.scanType));
43165
43227
  } else if (this.mode === "cleartype") {
43166
43228
  const dropout = src.scanSet ? dropoutMode(src.scanControl, src.scanType) : !this.gdiPlus ? 0 : 2;
43167
43229
  bitmap = rasterizeClearType(o, this.gdiPlus, this.gdiPlus ? clearTypeSpread(this.ttf) : void 0, dropout, !this.gdiPlus && clearTypeSmoothsVertically(this.ttf, this.ppem));
@@ -43416,6 +43478,7 @@ var GdiFontCollection = class _GdiFontCollection {
43416
43478
  this.installedFonts
43417
43479
  );
43418
43480
  tt2.naturalWidths = spec.quality === CLEARTYPE_NATURAL_QUALITY;
43481
+ tt2.singleBitRequested = spec.quality === NONANTIALIASED_QUALITY;
43419
43482
  return tt2;
43420
43483
  };
43421
43484
  let tt = make(resolvePpem(best, spec.height), spec.height > 0 ? Math.round(spec.height) : 0);
@@ -54869,10 +54932,16 @@ function worldUnitFont(rCtx, obj) {
54869
54932
  const em = obj.emSize * getPageUnitMultiplier(unit, 1, dpi) / (page || 1);
54870
54933
  return Number.isFinite(em) && em > 0 ? { ...obj, emSize: em, unit: 0 } : obj;
54871
54934
  }
54935
+ function resolvePlusTextHint(hint, smoothing) {
54936
+ if (hint !== 0) {
54937
+ return hint;
54938
+ }
54939
+ return smoothing === "mono" ? 1 : smoothing === "gray" ? 3 : 5;
54940
+ }
54872
54941
  function realizePlusEngineFont(rCtx, font, paint, brushSampler2) {
54873
54942
  const fonts = rCtx.fonts;
54874
54943
  const color = typeof paint === "string" ? rgbaToHex(paint) : null;
54875
- const hint = rCtx.textRenderingHint ?? 0;
54944
+ const hint = resolvePlusTextHint(rCtx.textRenderingHint ?? 0, fonts?.defaultSmoothing);
54876
54945
  const graySmooth = hint === 3 || hint === 4 || hint === 5;
54877
54946
  const solidViaCoverage = !!color && graySmooth && !isSvgContext(rCtx.ctx);
54878
54947
  const sampler = isSvgContext(rCtx.ctx) ? null : solidViaCoverage ? solidSampler(cssColorToArgb(paint) ?? 4278190080) : color ? null : brushSampler2;
@@ -54918,14 +54987,14 @@ function realizePlusEngineFont(rCtx, font, paint, brushSampler2) {
54918
54987
  plusBars: hint === 4 ? "fractional" : "snapped"
54919
54988
  };
54920
54989
  }
54921
- function paintPlusEngineRun(rCtx, ef, run) {
54990
+ function paintPlusEngineRun(rCtx, ef, run, clip) {
54922
54991
  const base = {
54923
54992
  ...run,
54924
54993
  textAlign: 24,
54925
54994
  bkColor: "#ffffff",
54926
54995
  bkMode: 1,
54927
- options: 0,
54928
- rect: null,
54996
+ options: clip ? ETO_CLIPPED : 0,
54997
+ rect: clip ?? null,
54929
54998
  matrix: null
54930
54999
  };
54931
55000
  if ((!ef.color || ef.solidViaCoverage) && ef.sampler) {
@@ -54933,6 +55002,19 @@ function paintPlusEngineRun(rCtx, ef, run) {
54933
55002
  if (!cov) {
54934
55003
  return true;
54935
55004
  }
55005
+ if (clip) {
55006
+ for (let yy = 0; yy < cov.height; yy++) {
55007
+ const py = cov.y + yy;
55008
+ for (let xx = 0; xx < cov.width; xx++) {
55009
+ const px = cov.x + xx;
55010
+ if (px < clip.left || px >= clip.right || py < clip.top || py >= clip.bottom) {
55011
+ const i = yy * cov.width + xx;
55012
+ cov.data.fill(0, i * cov.channels, (i + 1) * cov.channels);
55013
+ if (cov.bars) cov.bars[i] = 0;
55014
+ }
55015
+ }
55016
+ }
55017
+ }
54936
55018
  const gamma = textGamma(rCtx.ext?.textContrast);
54937
55019
  if (cov.channels === 1) {
54938
55020
  applyTextContrast(cov.data, gamma);
@@ -54996,8 +55078,126 @@ function drawPlusStringWithEngine(rCtx, font, text, layoutX, layoutY, layoutW, l
54996
55078
  const tracking = format?.tracking ?? PLUS_DEFAULT_TRACKING;
54997
55079
  const ctGrid = !unhinted && realized.mode === "cleartype" && !!realized.ctGridAdvance && !realized.syntheticItalic && tracking > 1;
54998
55080
  const designUnits = !!realized.designAdvance && m[0] === 1 && m[3] === 1;
54999
- const dx = unhinted ? codes.map((c) => designUnits ? realized.designAdvance(realized.glyphIndex(c), tracking) : realized.advance(realized.glyphIndex(c)) * tracking) : ctGrid ? codes.map((c) => realized.ctGridAdvance(realized.glyphIndex(c))) : null;
55000
- return paintPlusEngineRun(rCtx, ef, { codes, glyphIndices: false, x, y, dx, dy: null });
55081
+ const advanceOf = (c) => {
55082
+ const gi = realized.glyphIndex(c);
55083
+ return unhinted ? designUnits ? realized.designAdvance(gi, tracking) : realized.advance(gi) * tracking : ctGrid ? realized.ctGridAdvance(gi) : realized.advance(gi);
55084
+ };
55085
+ const dx = unhinted || ctGrid ? codes.map(advanceOf) : null;
55086
+ const flags = format?.flags ?? 0;
55087
+ const left = m[0] * layoutX + m[4];
55088
+ const top = m[3] * layoutY + m[5];
55089
+ const width = m[0] * layoutW;
55090
+ const height = m[3] * layoutH;
55091
+ const rect = width > 0 && height > 0 && !(flags & STRING_FORMAT_NO_CLIP) ? { left: Math.round(left), top: Math.round(top), right: Math.round(left + width), bottom: Math.round(top + height) } : void 0;
55092
+ let clip = void 0;
55093
+ const available = width > 0 && !(flags & STRING_FORMAT_NO_WRAP) ? width - emPx * ((format?.leadingMargin ?? PLUS_LEADING_EM) + (format?.trailingMargin ?? PLUS_LEADING_EM)) : Infinity;
55094
+ const fit = (realized.designAdvance ? codes.map((c) => realized.designAdvance(realized.glyphIndex(c), tracking)) : codes.map(advanceOf)).map((w, i) => i + 1 < codes.length && ttf.kerning ? w + ttf.kerning(realized.glyphIndex(codes[i]), realized.glyphIndex(codes[i + 1])) * emPx / ttf.unitsPerEm : w);
55095
+ let lines = layoutPlusLines(codes, fit, available);
55096
+ const paragraph = lines.length > 1;
55097
+ if (paragraph) clip = rect;
55098
+ const descentUnits = ttf.winDescent ?? 0;
55099
+ const lineUnits = ttf.hheaAscender !== void 0 && ttf.hheaDescender !== void 0 ? ttf.hheaAscender - ttf.hheaDescender + (ttf.hheaLineGap ?? 0) : ttf.winAscent + descentUnits;
55100
+ const lineSpacing = lineUnits * emPx / ttf.unitsPerEm;
55101
+ if (rect) {
55102
+ const visible = flags & STRING_FORMAT_LINE_LIMIT ? Math.floor((height - (ttf.winAscent + descentUnits) * emPx / ttf.unitsPerEm) / lineSpacing) + 1 : Math.floor(height / lineSpacing - 0.25) + 1;
55103
+ if (lines.length > visible) {
55104
+ clip = rect;
55105
+ const kept = lines.slice(0, Math.max(0, visible));
55106
+ if (kept.length > 0 && (format?.trimming ?? 1) === 1) {
55107
+ const last = kept[kept.length - 1];
55108
+ let end = last.start;
55109
+ while (end < codes.length && codes[end] !== 10 && codes[end] !== 13) {
55110
+ let w = 0;
55111
+ for (let i = last.start; i <= end; i++) w += fit[i];
55112
+ if (w > available && end > last.start) break;
55113
+ end++;
55114
+ }
55115
+ while (end > last.start && codes[end - 1] === 32) end--;
55116
+ kept[kept.length - 1] = { start: last.start, end };
55117
+ }
55118
+ lines = kept;
55119
+ }
55120
+ }
55121
+ if (lines.length === 0) {
55122
+ return true;
55123
+ }
55124
+ if (!paragraph) {
55125
+ return paintPlusEngineRun(rCtx, ef, {
55126
+ codes: codes.slice(0, lines[0].end === trimmedLength(codes) ? codes.length : lines[0].end),
55127
+ glyphIndices: false,
55128
+ x,
55129
+ y,
55130
+ dx: dx ? dx.slice(0, lines[0].end === trimmedLength(codes) ? codes.length : lines[0].end) : null,
55131
+ dy: null
55132
+ }, clip);
55133
+ }
55134
+ for (const [n, line] of lines.entries()) {
55135
+ const exact = top + (ttf.winAscent + n * lineUnits) * emPx / ttf.unitsPerEm;
55136
+ const lineCodes = codes.slice(line.start, line.end);
55137
+ if (lineCodes.length === 0) {
55138
+ continue;
55139
+ }
55140
+ const kern = (i) => {
55141
+ const k = i + 1 < line.end && ttf.kerning ? ttf.kerning(realized.glyphIndex(codes[i]), realized.glyphIndex(codes[i + 1])) * emPx / ttf.unitsPerEm : 0;
55142
+ return unhinted ? k : Math.round(k);
55143
+ };
55144
+ const lineDx = Array.from({ length: line.end - line.start }, (_, k) => advanceOf(codes[line.start + k]) + kern(line.start + k));
55145
+ paintPlusEngineRun(rCtx, ef, {
55146
+ codes: lineCodes,
55147
+ glyphIndices: false,
55148
+ x,
55149
+ y: unhinted ? top + Math.round(exactAscent) + n * lineSpacing : Math.round(exact),
55150
+ dx: lineDx.some((v, k) => v !== advanceOf(codes[line.start + k])) || dx ? lineDx : null,
55151
+ dy: null
55152
+ }, clip);
55153
+ }
55154
+ return true;
55155
+ }
55156
+ function trimmedLength(codes) {
55157
+ let n = codes.length;
55158
+ while (n > 0 && codes[n - 1] === 32) n--;
55159
+ return n;
55160
+ }
55161
+ var STRING_FORMAT_NO_WRAP = 4096;
55162
+ var STRING_FORMAT_LINE_LIMIT = 8192;
55163
+ var STRING_FORMAT_NO_CLIP = 16384;
55164
+ function layoutPlusLines(codes, advances, available) {
55165
+ const lines = [];
55166
+ const trimEnd = (start, end) => {
55167
+ while (end > start && codes[end - 1] === 32) end--;
55168
+ return end;
55169
+ };
55170
+ const width = (start, end) => {
55171
+ let w = 0;
55172
+ for (let i = start; i < end; i++) w += advances[i];
55173
+ return w;
55174
+ };
55175
+ let p = 0;
55176
+ while (p <= codes.length) {
55177
+ let e = p;
55178
+ while (e < codes.length && codes[e] !== 10 && codes[e] !== 13) e++;
55179
+ let pos = p;
55180
+ do {
55181
+ let end = trimEnd(pos, e);
55182
+ if (Number.isFinite(available) && width(pos, end) > available) {
55183
+ let best = -1;
55184
+ for (let i = pos + 1; i < e; i++) {
55185
+ if (codes[i - 1] === 32 && codes[i] !== 32 && width(pos, trimEnd(pos, i)) <= available) best = i;
55186
+ }
55187
+ if (best > pos) {
55188
+ end = best;
55189
+ } else {
55190
+ end = pos + 1;
55191
+ while (end < e && codes[end] !== 32 && width(pos, end + 1) <= available) end++;
55192
+ }
55193
+ }
55194
+ lines.push({ start: pos, end: trimEnd(pos, end) });
55195
+ pos = end;
55196
+ while (pos < e && codes[pos] === 32 && pos > p) pos++;
55197
+ } while (pos < e);
55198
+ p = e + (codes[e] === 13 && codes[e + 1] === 10 ? 2 : 1);
55199
+ }
55200
+ return lines;
55001
55201
  }
55002
55202
  var DRIVER_STRING_CMAP_LOOKUP = 1;
55003
55203
  var DRIVER_STRING_VERTICAL = 2;
@@ -55808,7 +56008,8 @@ function handleEmfPlusObjectRecord(rCtx, recFlags, dataOff, recDataSize, cacheKe
55808
56008
  lineAlignment: lineAlignment ?? 0,
55809
56009
  leadingMargin: hasSpacing ? view.getFloat32(dataOff + 36, true) : void 0,
55810
56010
  trailingMargin: hasSpacing ? view.getFloat32(dataOff + 40, true) : void 0,
55811
- tracking: hasSpacing ? view.getFloat32(dataOff + 44, true) : void 0
56011
+ tracking: hasSpacing ? view.getFloat32(dataOff + 44, true) : void 0,
56012
+ trimming: recDataSize >= 52 ? view.getUint32(dataOff + 48, true) : void 0
55812
56013
  });
55813
56014
  }
55814
56015
  break;
package/dist/browser.mjs CHANGED
@@ -36982,7 +36982,35 @@ function parseTrueTypeFace(view, base = 0) {
36982
36982
  }
36983
36983
  const locaAt = (g) => longLoca ? view.getUint32(loca.offset + g * 4) : view.getUint16(loca.offset + g * 2) * 2;
36984
36984
  const glyphCache = /* @__PURE__ */ new Map();
36985
+ const kernT = tables.get("kern");
36986
+ let kernPairs = null;
36987
+ const kerning = (left, right) => {
36988
+ if (!kernT) return 0;
36989
+ if (!kernPairs) {
36990
+ kernPairs = /* @__PURE__ */ new Map();
36991
+ const end = kernT.offset + kernT.length;
36992
+ if (kernT.offset + 4 <= end && view.getUint16(kernT.offset) === 0) {
36993
+ const n = view.getUint16(kernT.offset + 2);
36994
+ let o = kernT.offset + 4;
36995
+ for (let t = 0; t < n && o + 6 <= end; t++) {
36996
+ const length = view.getUint16(o + 2);
36997
+ const coverage = view.getUint16(o + 4);
36998
+ if (coverage >> 8 === 0 && (coverage & 7) === 1 && o + 14 <= end) {
36999
+ const pairs = view.getUint16(o + 6);
37000
+ for (let i = 0; i < pairs && o + 14 + i * 6 + 6 <= end; i++) {
37001
+ const p = o + 14 + i * 6;
37002
+ kernPairs.set(view.getUint16(p) * 65536 + view.getUint16(p + 2), view.getInt16(p + 4));
37003
+ }
37004
+ }
37005
+ if (length < 6) break;
37006
+ o += length;
37007
+ }
37008
+ }
37009
+ }
37010
+ return kernPairs.get(left * 65536 + right) ?? 0;
37011
+ };
36985
37012
  return {
37013
+ kerning,
36986
37014
  family: names2.get(1) ?? "",
36987
37015
  subfamily: names2.get(2) ?? "",
36988
37016
  fullName: names2.get(4) ?? "",
@@ -42876,7 +42904,15 @@ var RealizedFont = class {
42876
42904
  this.advanceOutlines = /* @__PURE__ */ new Map();
42877
42905
  this.outlines = /* @__PURE__ */ new Map();
42878
42906
  this.glyphs = /* @__PURE__ */ new Map();
42907
+ /**
42908
+ * The font was asked for at NONANTIALIASED_QUALITY (GDI+ SingleBitPerPixelGridFit), not turned monochrome by
42909
+ * the `gasp` table under AntiAliasGridFit: those glyphs keep the previous path (the native grayscale corpora,
42910
+ * `gdiplus-courier-gray` and `gdiplus-shpix-x`, lose exact glyphs the GDI way).
42911
+ */
42912
+ this.singleBitRequested = false;
42879
42913
  this.reverseCmap = null;
42914
+ this.gdiMonoSize = null;
42915
+ this.gdiMonoOutlines = /* @__PURE__ */ new Map();
42880
42916
  this.ctSize = null;
42881
42917
  /** CLEARTYPE_NATURAL_QUALITY: ClearType glyphs keep their own (natural) widths. */
42882
42918
  this.naturalWidths = false;
@@ -42936,6 +42972,17 @@ var RealizedFont = class {
42936
42972
  * way, at a half turn it rounded (all 530 ascents and descents of five faces at lfHeight -8..-60 for 90, 180 and
42937
42973
  * 270 degrees, escapement and world rotation alike). Undefined for an upright baseline.
42938
42974
  */
42975
+ /**
42976
+ * GDI+ SingleBitPerPixelGridFit glyphs are close to GDI's monochrome glyphs: hinted with GDI's arithmetic and
42977
+ * scan-converted with GDI's converter, 1,658 of 1,680 native upright regular glyphs are exact (`xwvyAkMWKVXZ4&%`,
42978
+ * Arial, Tahoma, Times New Roman and Segoe UI at 9 to 36 px), against 1,442 with the previous arithmetic. GDI+'s
42979
+ * own arithmetic is neither: 15 glyphs (Arial 16 `w` among them) are exact only the old way, and no subset of
42980
+ * GDI's projection, movement and ISECT rules gets both. Italic faces keep the previous path (Times New Roman
42981
+ * Italic 22 in `textx-plus-singlebitgridfit`: 8 pixels the GDI way).
42982
+ */
42983
+ plusMonoAsGdi(gdiPlus, mode, gridFit) {
42984
+ return gdiPlus && mode === "mono" && gridFit && this.singleBitRequested;
42985
+ }
42939
42986
  rotatedMetrics(ux, uy) {
42940
42987
  if (Math.abs(ux) > 1e-9 && Math.abs(uy) > 1e-9) {
42941
42988
  return { ascent: this.rotatedAscent, descent: this.rotatedDescent };
@@ -42970,6 +43017,20 @@ var RealizedFont = class {
42970
43017
  }
42971
43018
  return this.reverseCmap.get(index) ?? 65533;
42972
43019
  }
43020
+ /**
43021
+ * The outline of an upright GDI+ SingleBitPerPixelGridFit glyph, hinted
43022
+ * with GDI's arithmetic (see {@link plusMonoAsGdi}). Advances keep coming
43023
+ * from {@link outline}, whose placement the DrawString fixtures pin.
43024
+ */
43025
+ gdiMonoOutline(index) {
43026
+ let g = this.gdiMonoOutlines.get(index);
43027
+ if (!g) {
43028
+ this.gdiMonoSize ?? (this.gdiMonoSize = new HintedSize(this.ttf, this.ppemX, this.ppem, { version: 35, grayscale: false, gdiArithmetic: true, reportStretched: this.playbackStretched }, true));
43029
+ g = this.gdiMonoSize.hintGlyph(index < this.ttf.numGlyphs ? index : NOTDEF);
43030
+ this.gdiMonoOutlines.set(index, g);
43031
+ }
43032
+ return g;
43033
+ }
42973
43034
  outline(index) {
42974
43035
  let g = this.outlines.get(index);
42975
43036
  if (!g) {
@@ -43116,7 +43177,8 @@ var RealizedFont = class {
43116
43177
  }
43117
43178
  const skew = m && !(Math.abs(m[1]) < 1e-9 && Math.abs(m[2]) < 1e-9) && !(Math.abs(m[0]) < 1e-9 && Math.abs(m[3]) < 1e-9);
43118
43179
  const rotation = skew && m && !this.gdiPlus && this.ppemX === this.ppem ? rotationMatrix(m, this.ppem, (this.ttf.headFlags & 8) !== 0) : null;
43119
- const src = skew ? this.rotatedOutline(index, rotation ? rotation.ppem : 0) : this.outline(index);
43180
+ const asGdiMono = !m && !subX && !subY && this.ppemX === this.ppem && !this.syntheticItalic && !(this.ttf.fsSelection & 1) && this.plusMonoAsGdi(this.gdiPlus, this.mode, this.gridFit);
43181
+ const src = skew ? this.rotatedOutline(index, rotation ? rotation.ppem : 0) : asGdiMono ? this.gdiMonoOutline(index) : this.outline(index);
43120
43182
  if (skew && m && rotation) {
43121
43183
  m = rotation.matrix;
43122
43184
  } else if (skew && m) {
@@ -43159,7 +43221,7 @@ var RealizedFont = class {
43159
43221
  if (this.mode === "mono") {
43160
43222
  const smartGrid = !m && !subX && !subY && (!this.gdiPlus || this.ppemX === this.ppem && !this.syntheticItalic);
43161
43223
  const controlOn = src.scanSet ? src.scanControl : this.ppem <= DEFAULT_DROPOUT_PPEM;
43162
- bitmap = this.gdiPlus ? rasterizeMono(o, this.gdiPlus && !this.gridFit ? UNHINTED_MONO_DROPOUT : dropoutMode(controlOn, src.scanType), void 0, true, smartGrid, false, false, false, false, true) : msMonoBitmap(o, msScanFlags(controlOn, src.scanType));
43224
+ bitmap = this.gdiPlus && !asGdiMono ? rasterizeMono(o, this.gdiPlus && !this.gridFit ? UNHINTED_MONO_DROPOUT : dropoutMode(controlOn, src.scanType), void 0, true, smartGrid, false, false, false, false, true) : msMonoBitmap(o, msScanFlags(controlOn, src.scanType));
43163
43225
  } else if (this.mode === "cleartype") {
43164
43226
  const dropout = src.scanSet ? dropoutMode(src.scanControl, src.scanType) : !this.gdiPlus ? 0 : 2;
43165
43227
  bitmap = rasterizeClearType(o, this.gdiPlus, this.gdiPlus ? clearTypeSpread(this.ttf) : void 0, dropout, !this.gdiPlus && clearTypeSmoothsVertically(this.ttf, this.ppem));
@@ -43414,6 +43476,7 @@ var GdiFontCollection = class _GdiFontCollection {
43414
43476
  this.installedFonts
43415
43477
  );
43416
43478
  tt2.naturalWidths = spec.quality === CLEARTYPE_NATURAL_QUALITY;
43479
+ tt2.singleBitRequested = spec.quality === NONANTIALIASED_QUALITY;
43417
43480
  return tt2;
43418
43481
  };
43419
43482
  let tt = make(resolvePpem(best, spec.height), spec.height > 0 ? Math.round(spec.height) : 0);
@@ -54867,10 +54930,16 @@ function worldUnitFont(rCtx, obj) {
54867
54930
  const em = obj.emSize * getPageUnitMultiplier(unit, 1, dpi) / (page || 1);
54868
54931
  return Number.isFinite(em) && em > 0 ? { ...obj, emSize: em, unit: 0 } : obj;
54869
54932
  }
54933
+ function resolvePlusTextHint(hint, smoothing) {
54934
+ if (hint !== 0) {
54935
+ return hint;
54936
+ }
54937
+ return smoothing === "mono" ? 1 : smoothing === "gray" ? 3 : 5;
54938
+ }
54870
54939
  function realizePlusEngineFont(rCtx, font, paint, brushSampler2) {
54871
54940
  const fonts = rCtx.fonts;
54872
54941
  const color = typeof paint === "string" ? rgbaToHex(paint) : null;
54873
- const hint = rCtx.textRenderingHint ?? 0;
54942
+ const hint = resolvePlusTextHint(rCtx.textRenderingHint ?? 0, fonts?.defaultSmoothing);
54874
54943
  const graySmooth = hint === 3 || hint === 4 || hint === 5;
54875
54944
  const solidViaCoverage = !!color && graySmooth && !isSvgContext(rCtx.ctx);
54876
54945
  const sampler = isSvgContext(rCtx.ctx) ? null : solidViaCoverage ? solidSampler(cssColorToArgb(paint) ?? 4278190080) : color ? null : brushSampler2;
@@ -54916,14 +54985,14 @@ function realizePlusEngineFont(rCtx, font, paint, brushSampler2) {
54916
54985
  plusBars: hint === 4 ? "fractional" : "snapped"
54917
54986
  };
54918
54987
  }
54919
- function paintPlusEngineRun(rCtx, ef, run) {
54988
+ function paintPlusEngineRun(rCtx, ef, run, clip) {
54920
54989
  const base = {
54921
54990
  ...run,
54922
54991
  textAlign: 24,
54923
54992
  bkColor: "#ffffff",
54924
54993
  bkMode: 1,
54925
- options: 0,
54926
- rect: null,
54994
+ options: clip ? ETO_CLIPPED : 0,
54995
+ rect: clip ?? null,
54927
54996
  matrix: null
54928
54997
  };
54929
54998
  if ((!ef.color || ef.solidViaCoverage) && ef.sampler) {
@@ -54931,6 +55000,19 @@ function paintPlusEngineRun(rCtx, ef, run) {
54931
55000
  if (!cov) {
54932
55001
  return true;
54933
55002
  }
55003
+ if (clip) {
55004
+ for (let yy = 0; yy < cov.height; yy++) {
55005
+ const py = cov.y + yy;
55006
+ for (let xx = 0; xx < cov.width; xx++) {
55007
+ const px = cov.x + xx;
55008
+ if (px < clip.left || px >= clip.right || py < clip.top || py >= clip.bottom) {
55009
+ const i = yy * cov.width + xx;
55010
+ cov.data.fill(0, i * cov.channels, (i + 1) * cov.channels);
55011
+ if (cov.bars) cov.bars[i] = 0;
55012
+ }
55013
+ }
55014
+ }
55015
+ }
54934
55016
  const gamma = textGamma(rCtx.ext?.textContrast);
54935
55017
  if (cov.channels === 1) {
54936
55018
  applyTextContrast(cov.data, gamma);
@@ -54994,8 +55076,126 @@ function drawPlusStringWithEngine(rCtx, font, text, layoutX, layoutY, layoutW, l
54994
55076
  const tracking = format?.tracking ?? PLUS_DEFAULT_TRACKING;
54995
55077
  const ctGrid = !unhinted && realized.mode === "cleartype" && !!realized.ctGridAdvance && !realized.syntheticItalic && tracking > 1;
54996
55078
  const designUnits = !!realized.designAdvance && m[0] === 1 && m[3] === 1;
54997
- const dx = unhinted ? codes.map((c) => designUnits ? realized.designAdvance(realized.glyphIndex(c), tracking) : realized.advance(realized.glyphIndex(c)) * tracking) : ctGrid ? codes.map((c) => realized.ctGridAdvance(realized.glyphIndex(c))) : null;
54998
- return paintPlusEngineRun(rCtx, ef, { codes, glyphIndices: false, x, y, dx, dy: null });
55079
+ const advanceOf = (c) => {
55080
+ const gi = realized.glyphIndex(c);
55081
+ return unhinted ? designUnits ? realized.designAdvance(gi, tracking) : realized.advance(gi) * tracking : ctGrid ? realized.ctGridAdvance(gi) : realized.advance(gi);
55082
+ };
55083
+ const dx = unhinted || ctGrid ? codes.map(advanceOf) : null;
55084
+ const flags = format?.flags ?? 0;
55085
+ const left = m[0] * layoutX + m[4];
55086
+ const top = m[3] * layoutY + m[5];
55087
+ const width = m[0] * layoutW;
55088
+ const height = m[3] * layoutH;
55089
+ const rect = width > 0 && height > 0 && !(flags & STRING_FORMAT_NO_CLIP) ? { left: Math.round(left), top: Math.round(top), right: Math.round(left + width), bottom: Math.round(top + height) } : void 0;
55090
+ let clip = void 0;
55091
+ const available = width > 0 && !(flags & STRING_FORMAT_NO_WRAP) ? width - emPx * ((format?.leadingMargin ?? PLUS_LEADING_EM) + (format?.trailingMargin ?? PLUS_LEADING_EM)) : Infinity;
55092
+ const fit = (realized.designAdvance ? codes.map((c) => realized.designAdvance(realized.glyphIndex(c), tracking)) : codes.map(advanceOf)).map((w, i) => i + 1 < codes.length && ttf.kerning ? w + ttf.kerning(realized.glyphIndex(codes[i]), realized.glyphIndex(codes[i + 1])) * emPx / ttf.unitsPerEm : w);
55093
+ let lines = layoutPlusLines(codes, fit, available);
55094
+ const paragraph = lines.length > 1;
55095
+ if (paragraph) clip = rect;
55096
+ const descentUnits = ttf.winDescent ?? 0;
55097
+ const lineUnits = ttf.hheaAscender !== void 0 && ttf.hheaDescender !== void 0 ? ttf.hheaAscender - ttf.hheaDescender + (ttf.hheaLineGap ?? 0) : ttf.winAscent + descentUnits;
55098
+ const lineSpacing = lineUnits * emPx / ttf.unitsPerEm;
55099
+ if (rect) {
55100
+ const visible = flags & STRING_FORMAT_LINE_LIMIT ? Math.floor((height - (ttf.winAscent + descentUnits) * emPx / ttf.unitsPerEm) / lineSpacing) + 1 : Math.floor(height / lineSpacing - 0.25) + 1;
55101
+ if (lines.length > visible) {
55102
+ clip = rect;
55103
+ const kept = lines.slice(0, Math.max(0, visible));
55104
+ if (kept.length > 0 && (format?.trimming ?? 1) === 1) {
55105
+ const last = kept[kept.length - 1];
55106
+ let end = last.start;
55107
+ while (end < codes.length && codes[end] !== 10 && codes[end] !== 13) {
55108
+ let w = 0;
55109
+ for (let i = last.start; i <= end; i++) w += fit[i];
55110
+ if (w > available && end > last.start) break;
55111
+ end++;
55112
+ }
55113
+ while (end > last.start && codes[end - 1] === 32) end--;
55114
+ kept[kept.length - 1] = { start: last.start, end };
55115
+ }
55116
+ lines = kept;
55117
+ }
55118
+ }
55119
+ if (lines.length === 0) {
55120
+ return true;
55121
+ }
55122
+ if (!paragraph) {
55123
+ return paintPlusEngineRun(rCtx, ef, {
55124
+ codes: codes.slice(0, lines[0].end === trimmedLength(codes) ? codes.length : lines[0].end),
55125
+ glyphIndices: false,
55126
+ x,
55127
+ y,
55128
+ dx: dx ? dx.slice(0, lines[0].end === trimmedLength(codes) ? codes.length : lines[0].end) : null,
55129
+ dy: null
55130
+ }, clip);
55131
+ }
55132
+ for (const [n, line] of lines.entries()) {
55133
+ const exact = top + (ttf.winAscent + n * lineUnits) * emPx / ttf.unitsPerEm;
55134
+ const lineCodes = codes.slice(line.start, line.end);
55135
+ if (lineCodes.length === 0) {
55136
+ continue;
55137
+ }
55138
+ const kern = (i) => {
55139
+ const k = i + 1 < line.end && ttf.kerning ? ttf.kerning(realized.glyphIndex(codes[i]), realized.glyphIndex(codes[i + 1])) * emPx / ttf.unitsPerEm : 0;
55140
+ return unhinted ? k : Math.round(k);
55141
+ };
55142
+ const lineDx = Array.from({ length: line.end - line.start }, (_, k) => advanceOf(codes[line.start + k]) + kern(line.start + k));
55143
+ paintPlusEngineRun(rCtx, ef, {
55144
+ codes: lineCodes,
55145
+ glyphIndices: false,
55146
+ x,
55147
+ y: unhinted ? top + Math.round(exactAscent) + n * lineSpacing : Math.round(exact),
55148
+ dx: lineDx.some((v, k) => v !== advanceOf(codes[line.start + k])) || dx ? lineDx : null,
55149
+ dy: null
55150
+ }, clip);
55151
+ }
55152
+ return true;
55153
+ }
55154
+ function trimmedLength(codes) {
55155
+ let n = codes.length;
55156
+ while (n > 0 && codes[n - 1] === 32) n--;
55157
+ return n;
55158
+ }
55159
+ var STRING_FORMAT_NO_WRAP = 4096;
55160
+ var STRING_FORMAT_LINE_LIMIT = 8192;
55161
+ var STRING_FORMAT_NO_CLIP = 16384;
55162
+ function layoutPlusLines(codes, advances, available) {
55163
+ const lines = [];
55164
+ const trimEnd = (start, end) => {
55165
+ while (end > start && codes[end - 1] === 32) end--;
55166
+ return end;
55167
+ };
55168
+ const width = (start, end) => {
55169
+ let w = 0;
55170
+ for (let i = start; i < end; i++) w += advances[i];
55171
+ return w;
55172
+ };
55173
+ let p = 0;
55174
+ while (p <= codes.length) {
55175
+ let e = p;
55176
+ while (e < codes.length && codes[e] !== 10 && codes[e] !== 13) e++;
55177
+ let pos = p;
55178
+ do {
55179
+ let end = trimEnd(pos, e);
55180
+ if (Number.isFinite(available) && width(pos, end) > available) {
55181
+ let best = -1;
55182
+ for (let i = pos + 1; i < e; i++) {
55183
+ if (codes[i - 1] === 32 && codes[i] !== 32 && width(pos, trimEnd(pos, i)) <= available) best = i;
55184
+ }
55185
+ if (best > pos) {
55186
+ end = best;
55187
+ } else {
55188
+ end = pos + 1;
55189
+ while (end < e && codes[end] !== 32 && width(pos, end + 1) <= available) end++;
55190
+ }
55191
+ }
55192
+ lines.push({ start: pos, end: trimEnd(pos, end) });
55193
+ pos = end;
55194
+ while (pos < e && codes[pos] === 32 && pos > p) pos++;
55195
+ } while (pos < e);
55196
+ p = e + (codes[e] === 13 && codes[e + 1] === 10 ? 2 : 1);
55197
+ }
55198
+ return lines;
54999
55199
  }
55000
55200
  var DRIVER_STRING_CMAP_LOOKUP = 1;
55001
55201
  var DRIVER_STRING_VERTICAL = 2;
@@ -55806,7 +56006,8 @@ function handleEmfPlusObjectRecord(rCtx, recFlags, dataOff, recDataSize, cacheKe
55806
56006
  lineAlignment: lineAlignment ?? 0,
55807
56007
  leadingMargin: hasSpacing ? view.getFloat32(dataOff + 36, true) : void 0,
55808
56008
  trailingMargin: hasSpacing ? view.getFloat32(dataOff + 40, true) : void 0,
55809
- tracking: hasSpacing ? view.getFloat32(dataOff + 44, true) : void 0
56009
+ tracking: hasSpacing ? view.getFloat32(dataOff + 44, true) : void 0,
56010
+ trimming: recDataSize >= 52 ? view.getUint32(dataOff + 48, true) : void 0
55810
56011
  });
55811
56012
  }
55812
56013
  break;
package/dist/index.d.mts CHANGED
@@ -224,7 +224,10 @@ interface EmfConvertOptions {
224
224
  * Windows' system-wide font smoothing, which GDI applies to fonts that
225
225
  * ask for `DEFAULT_QUALITY`, `DRAFT_QUALITY` or `PROOF_QUALITY` (most
226
226
  * metafiles): `'cleartype'` (Windows' default), `'gray'` (standard
227
- * antialiasing) or `'mono'` (smoothing off). Only used with `fonts`.
227
+ * antialiasing) or `'mono'` (smoothing off). GDI+ text with the
228
+ * SystemDefault rendering hint (EMF+ files without a TextRenderingHint)
229
+ * follows it too: ClearTypeGridFit, AntiAliasGridFit or single-bit
230
+ * grid-fitted. Only used with `fonts`.
228
231
  */
229
232
  fontSmoothing?: 'cleartype' | 'gray' | 'mono';
230
233
  /**
package/dist/index.d.ts CHANGED
@@ -224,7 +224,10 @@ interface EmfConvertOptions {
224
224
  * Windows' system-wide font smoothing, which GDI applies to fonts that
225
225
  * ask for `DEFAULT_QUALITY`, `DRAFT_QUALITY` or `PROOF_QUALITY` (most
226
226
  * metafiles): `'cleartype'` (Windows' default), `'gray'` (standard
227
- * antialiasing) or `'mono'` (smoothing off). Only used with `fonts`.
227
+ * antialiasing) or `'mono'` (smoothing off). GDI+ text with the
228
+ * SystemDefault rendering hint (EMF+ files without a TextRenderingHint)
229
+ * follows it too: ClearTypeGridFit, AntiAliasGridFit or single-bit
230
+ * grid-fitted. Only used with `fonts`.
228
231
  */
229
232
  fontSmoothing?: 'cleartype' | 'gray' | 'mono';
230
233
  /**
package/dist/index.js CHANGED
@@ -36985,7 +36985,35 @@ function parseTrueTypeFace(view, base = 0) {
36985
36985
  }
36986
36986
  const locaAt = (g) => longLoca ? view.getUint32(loca.offset + g * 4) : view.getUint16(loca.offset + g * 2) * 2;
36987
36987
  const glyphCache = /* @__PURE__ */ new Map();
36988
+ const kernT = tables.get("kern");
36989
+ let kernPairs = null;
36990
+ const kerning = (left, right) => {
36991
+ if (!kernT) return 0;
36992
+ if (!kernPairs) {
36993
+ kernPairs = /* @__PURE__ */ new Map();
36994
+ const end = kernT.offset + kernT.length;
36995
+ if (kernT.offset + 4 <= end && view.getUint16(kernT.offset) === 0) {
36996
+ const n = view.getUint16(kernT.offset + 2);
36997
+ let o = kernT.offset + 4;
36998
+ for (let t = 0; t < n && o + 6 <= end; t++) {
36999
+ const length = view.getUint16(o + 2);
37000
+ const coverage = view.getUint16(o + 4);
37001
+ if (coverage >> 8 === 0 && (coverage & 7) === 1 && o + 14 <= end) {
37002
+ const pairs = view.getUint16(o + 6);
37003
+ for (let i = 0; i < pairs && o + 14 + i * 6 + 6 <= end; i++) {
37004
+ const p = o + 14 + i * 6;
37005
+ kernPairs.set(view.getUint16(p) * 65536 + view.getUint16(p + 2), view.getInt16(p + 4));
37006
+ }
37007
+ }
37008
+ if (length < 6) break;
37009
+ o += length;
37010
+ }
37011
+ }
37012
+ }
37013
+ return kernPairs.get(left * 65536 + right) ?? 0;
37014
+ };
36988
37015
  return {
37016
+ kerning,
36989
37017
  family: names2.get(1) ?? "",
36990
37018
  subfamily: names2.get(2) ?? "",
36991
37019
  fullName: names2.get(4) ?? "",
@@ -42879,7 +42907,15 @@ var RealizedFont = class {
42879
42907
  this.advanceOutlines = /* @__PURE__ */ new Map();
42880
42908
  this.outlines = /* @__PURE__ */ new Map();
42881
42909
  this.glyphs = /* @__PURE__ */ new Map();
42910
+ /**
42911
+ * The font was asked for at NONANTIALIASED_QUALITY (GDI+ SingleBitPerPixelGridFit), not turned monochrome by
42912
+ * the `gasp` table under AntiAliasGridFit: those glyphs keep the previous path (the native grayscale corpora,
42913
+ * `gdiplus-courier-gray` and `gdiplus-shpix-x`, lose exact glyphs the GDI way).
42914
+ */
42915
+ this.singleBitRequested = false;
42882
42916
  this.reverseCmap = null;
42917
+ this.gdiMonoSize = null;
42918
+ this.gdiMonoOutlines = /* @__PURE__ */ new Map();
42883
42919
  this.ctSize = null;
42884
42920
  /** CLEARTYPE_NATURAL_QUALITY: ClearType glyphs keep their own (natural) widths. */
42885
42921
  this.naturalWidths = false;
@@ -42939,6 +42975,17 @@ var RealizedFont = class {
42939
42975
  * way, at a half turn it rounded (all 530 ascents and descents of five faces at lfHeight -8..-60 for 90, 180 and
42940
42976
  * 270 degrees, escapement and world rotation alike). Undefined for an upright baseline.
42941
42977
  */
42978
+ /**
42979
+ * GDI+ SingleBitPerPixelGridFit glyphs are close to GDI's monochrome glyphs: hinted with GDI's arithmetic and
42980
+ * scan-converted with GDI's converter, 1,658 of 1,680 native upright regular glyphs are exact (`xwvyAkMWKVXZ4&%`,
42981
+ * Arial, Tahoma, Times New Roman and Segoe UI at 9 to 36 px), against 1,442 with the previous arithmetic. GDI+'s
42982
+ * own arithmetic is neither: 15 glyphs (Arial 16 `w` among them) are exact only the old way, and no subset of
42983
+ * GDI's projection, movement and ISECT rules gets both. Italic faces keep the previous path (Times New Roman
42984
+ * Italic 22 in `textx-plus-singlebitgridfit`: 8 pixels the GDI way).
42985
+ */
42986
+ plusMonoAsGdi(gdiPlus, mode, gridFit) {
42987
+ return gdiPlus && mode === "mono" && gridFit && this.singleBitRequested;
42988
+ }
42942
42989
  rotatedMetrics(ux, uy) {
42943
42990
  if (Math.abs(ux) > 1e-9 && Math.abs(uy) > 1e-9) {
42944
42991
  return { ascent: this.rotatedAscent, descent: this.rotatedDescent };
@@ -42973,6 +43020,20 @@ var RealizedFont = class {
42973
43020
  }
42974
43021
  return this.reverseCmap.get(index) ?? 65533;
42975
43022
  }
43023
+ /**
43024
+ * The outline of an upright GDI+ SingleBitPerPixelGridFit glyph, hinted
43025
+ * with GDI's arithmetic (see {@link plusMonoAsGdi}). Advances keep coming
43026
+ * from {@link outline}, whose placement the DrawString fixtures pin.
43027
+ */
43028
+ gdiMonoOutline(index) {
43029
+ let g = this.gdiMonoOutlines.get(index);
43030
+ if (!g) {
43031
+ this.gdiMonoSize ?? (this.gdiMonoSize = new HintedSize(this.ttf, this.ppemX, this.ppem, { version: 35, grayscale: false, gdiArithmetic: true, reportStretched: this.playbackStretched }, true));
43032
+ g = this.gdiMonoSize.hintGlyph(index < this.ttf.numGlyphs ? index : NOTDEF);
43033
+ this.gdiMonoOutlines.set(index, g);
43034
+ }
43035
+ return g;
43036
+ }
42976
43037
  outline(index) {
42977
43038
  let g = this.outlines.get(index);
42978
43039
  if (!g) {
@@ -43119,7 +43180,8 @@ var RealizedFont = class {
43119
43180
  }
43120
43181
  const skew = m && !(Math.abs(m[1]) < 1e-9 && Math.abs(m[2]) < 1e-9) && !(Math.abs(m[0]) < 1e-9 && Math.abs(m[3]) < 1e-9);
43121
43182
  const rotation = skew && m && !this.gdiPlus && this.ppemX === this.ppem ? rotationMatrix(m, this.ppem, (this.ttf.headFlags & 8) !== 0) : null;
43122
- const src = skew ? this.rotatedOutline(index, rotation ? rotation.ppem : 0) : this.outline(index);
43183
+ const asGdiMono = !m && !subX && !subY && this.ppemX === this.ppem && !this.syntheticItalic && !(this.ttf.fsSelection & 1) && this.plusMonoAsGdi(this.gdiPlus, this.mode, this.gridFit);
43184
+ const src = skew ? this.rotatedOutline(index, rotation ? rotation.ppem : 0) : asGdiMono ? this.gdiMonoOutline(index) : this.outline(index);
43123
43185
  if (skew && m && rotation) {
43124
43186
  m = rotation.matrix;
43125
43187
  } else if (skew && m) {
@@ -43162,7 +43224,7 @@ var RealizedFont = class {
43162
43224
  if (this.mode === "mono") {
43163
43225
  const smartGrid = !m && !subX && !subY && (!this.gdiPlus || this.ppemX === this.ppem && !this.syntheticItalic);
43164
43226
  const controlOn = src.scanSet ? src.scanControl : this.ppem <= DEFAULT_DROPOUT_PPEM;
43165
- bitmap = this.gdiPlus ? rasterizeMono(o, this.gdiPlus && !this.gridFit ? UNHINTED_MONO_DROPOUT : dropoutMode(controlOn, src.scanType), void 0, true, smartGrid, false, false, false, false, true) : msMonoBitmap(o, msScanFlags(controlOn, src.scanType));
43227
+ bitmap = this.gdiPlus && !asGdiMono ? rasterizeMono(o, this.gdiPlus && !this.gridFit ? UNHINTED_MONO_DROPOUT : dropoutMode(controlOn, src.scanType), void 0, true, smartGrid, false, false, false, false, true) : msMonoBitmap(o, msScanFlags(controlOn, src.scanType));
43166
43228
  } else if (this.mode === "cleartype") {
43167
43229
  const dropout = src.scanSet ? dropoutMode(src.scanControl, src.scanType) : !this.gdiPlus ? 0 : 2;
43168
43230
  bitmap = rasterizeClearType(o, this.gdiPlus, this.gdiPlus ? clearTypeSpread(this.ttf) : void 0, dropout, !this.gdiPlus && clearTypeSmoothsVertically(this.ttf, this.ppem));
@@ -43417,6 +43479,7 @@ var GdiFontCollection = class _GdiFontCollection {
43417
43479
  this.installedFonts
43418
43480
  );
43419
43481
  tt2.naturalWidths = spec.quality === CLEARTYPE_NATURAL_QUALITY;
43482
+ tt2.singleBitRequested = spec.quality === NONANTIALIASED_QUALITY;
43420
43483
  return tt2;
43421
43484
  };
43422
43485
  let tt = make(resolvePpem(best, spec.height), spec.height > 0 ? Math.round(spec.height) : 0);
@@ -54870,10 +54933,16 @@ function worldUnitFont(rCtx, obj) {
54870
54933
  const em = obj.emSize * getPageUnitMultiplier(unit, 1, dpi) / (page || 1);
54871
54934
  return Number.isFinite(em) && em > 0 ? { ...obj, emSize: em, unit: 0 } : obj;
54872
54935
  }
54936
+ function resolvePlusTextHint(hint, smoothing) {
54937
+ if (hint !== 0) {
54938
+ return hint;
54939
+ }
54940
+ return smoothing === "mono" ? 1 : smoothing === "gray" ? 3 : 5;
54941
+ }
54873
54942
  function realizePlusEngineFont(rCtx, font, paint, brushSampler2) {
54874
54943
  const fonts = rCtx.fonts;
54875
54944
  const color = typeof paint === "string" ? rgbaToHex(paint) : null;
54876
- const hint = rCtx.textRenderingHint ?? 0;
54945
+ const hint = resolvePlusTextHint(rCtx.textRenderingHint ?? 0, fonts?.defaultSmoothing);
54877
54946
  const graySmooth = hint === 3 || hint === 4 || hint === 5;
54878
54947
  const solidViaCoverage = !!color && graySmooth && !isSvgContext(rCtx.ctx);
54879
54948
  const sampler = isSvgContext(rCtx.ctx) ? null : solidViaCoverage ? solidSampler(cssColorToArgb(paint) ?? 4278190080) : color ? null : brushSampler2;
@@ -54919,14 +54988,14 @@ function realizePlusEngineFont(rCtx, font, paint, brushSampler2) {
54919
54988
  plusBars: hint === 4 ? "fractional" : "snapped"
54920
54989
  };
54921
54990
  }
54922
- function paintPlusEngineRun(rCtx, ef, run) {
54991
+ function paintPlusEngineRun(rCtx, ef, run, clip) {
54923
54992
  const base = {
54924
54993
  ...run,
54925
54994
  textAlign: 24,
54926
54995
  bkColor: "#ffffff",
54927
54996
  bkMode: 1,
54928
- options: 0,
54929
- rect: null,
54997
+ options: clip ? ETO_CLIPPED : 0,
54998
+ rect: clip ?? null,
54930
54999
  matrix: null
54931
55000
  };
54932
55001
  if ((!ef.color || ef.solidViaCoverage) && ef.sampler) {
@@ -54934,6 +55003,19 @@ function paintPlusEngineRun(rCtx, ef, run) {
54934
55003
  if (!cov) {
54935
55004
  return true;
54936
55005
  }
55006
+ if (clip) {
55007
+ for (let yy = 0; yy < cov.height; yy++) {
55008
+ const py = cov.y + yy;
55009
+ for (let xx = 0; xx < cov.width; xx++) {
55010
+ const px = cov.x + xx;
55011
+ if (px < clip.left || px >= clip.right || py < clip.top || py >= clip.bottom) {
55012
+ const i = yy * cov.width + xx;
55013
+ cov.data.fill(0, i * cov.channels, (i + 1) * cov.channels);
55014
+ if (cov.bars) cov.bars[i] = 0;
55015
+ }
55016
+ }
55017
+ }
55018
+ }
54937
55019
  const gamma = textGamma(rCtx.ext?.textContrast);
54938
55020
  if (cov.channels === 1) {
54939
55021
  applyTextContrast(cov.data, gamma);
@@ -54997,8 +55079,126 @@ function drawPlusStringWithEngine(rCtx, font, text, layoutX, layoutY, layoutW, l
54997
55079
  const tracking = format?.tracking ?? PLUS_DEFAULT_TRACKING;
54998
55080
  const ctGrid = !unhinted && realized.mode === "cleartype" && !!realized.ctGridAdvance && !realized.syntheticItalic && tracking > 1;
54999
55081
  const designUnits = !!realized.designAdvance && m[0] === 1 && m[3] === 1;
55000
- const dx = unhinted ? codes.map((c) => designUnits ? realized.designAdvance(realized.glyphIndex(c), tracking) : realized.advance(realized.glyphIndex(c)) * tracking) : ctGrid ? codes.map((c) => realized.ctGridAdvance(realized.glyphIndex(c))) : null;
55001
- return paintPlusEngineRun(rCtx, ef, { codes, glyphIndices: false, x, y, dx, dy: null });
55082
+ const advanceOf = (c) => {
55083
+ const gi = realized.glyphIndex(c);
55084
+ return unhinted ? designUnits ? realized.designAdvance(gi, tracking) : realized.advance(gi) * tracking : ctGrid ? realized.ctGridAdvance(gi) : realized.advance(gi);
55085
+ };
55086
+ const dx = unhinted || ctGrid ? codes.map(advanceOf) : null;
55087
+ const flags = format?.flags ?? 0;
55088
+ const left = m[0] * layoutX + m[4];
55089
+ const top = m[3] * layoutY + m[5];
55090
+ const width = m[0] * layoutW;
55091
+ const height = m[3] * layoutH;
55092
+ const rect = width > 0 && height > 0 && !(flags & STRING_FORMAT_NO_CLIP) ? { left: Math.round(left), top: Math.round(top), right: Math.round(left + width), bottom: Math.round(top + height) } : void 0;
55093
+ let clip = void 0;
55094
+ const available = width > 0 && !(flags & STRING_FORMAT_NO_WRAP) ? width - emPx * ((format?.leadingMargin ?? PLUS_LEADING_EM) + (format?.trailingMargin ?? PLUS_LEADING_EM)) : Infinity;
55095
+ const fit = (realized.designAdvance ? codes.map((c) => realized.designAdvance(realized.glyphIndex(c), tracking)) : codes.map(advanceOf)).map((w, i) => i + 1 < codes.length && ttf.kerning ? w + ttf.kerning(realized.glyphIndex(codes[i]), realized.glyphIndex(codes[i + 1])) * emPx / ttf.unitsPerEm : w);
55096
+ let lines = layoutPlusLines(codes, fit, available);
55097
+ const paragraph = lines.length > 1;
55098
+ if (paragraph) clip = rect;
55099
+ const descentUnits = ttf.winDescent ?? 0;
55100
+ const lineUnits = ttf.hheaAscender !== void 0 && ttf.hheaDescender !== void 0 ? ttf.hheaAscender - ttf.hheaDescender + (ttf.hheaLineGap ?? 0) : ttf.winAscent + descentUnits;
55101
+ const lineSpacing = lineUnits * emPx / ttf.unitsPerEm;
55102
+ if (rect) {
55103
+ const visible = flags & STRING_FORMAT_LINE_LIMIT ? Math.floor((height - (ttf.winAscent + descentUnits) * emPx / ttf.unitsPerEm) / lineSpacing) + 1 : Math.floor(height / lineSpacing - 0.25) + 1;
55104
+ if (lines.length > visible) {
55105
+ clip = rect;
55106
+ const kept = lines.slice(0, Math.max(0, visible));
55107
+ if (kept.length > 0 && (format?.trimming ?? 1) === 1) {
55108
+ const last = kept[kept.length - 1];
55109
+ let end = last.start;
55110
+ while (end < codes.length && codes[end] !== 10 && codes[end] !== 13) {
55111
+ let w = 0;
55112
+ for (let i = last.start; i <= end; i++) w += fit[i];
55113
+ if (w > available && end > last.start) break;
55114
+ end++;
55115
+ }
55116
+ while (end > last.start && codes[end - 1] === 32) end--;
55117
+ kept[kept.length - 1] = { start: last.start, end };
55118
+ }
55119
+ lines = kept;
55120
+ }
55121
+ }
55122
+ if (lines.length === 0) {
55123
+ return true;
55124
+ }
55125
+ if (!paragraph) {
55126
+ return paintPlusEngineRun(rCtx, ef, {
55127
+ codes: codes.slice(0, lines[0].end === trimmedLength(codes) ? codes.length : lines[0].end),
55128
+ glyphIndices: false,
55129
+ x,
55130
+ y,
55131
+ dx: dx ? dx.slice(0, lines[0].end === trimmedLength(codes) ? codes.length : lines[0].end) : null,
55132
+ dy: null
55133
+ }, clip);
55134
+ }
55135
+ for (const [n, line] of lines.entries()) {
55136
+ const exact = top + (ttf.winAscent + n * lineUnits) * emPx / ttf.unitsPerEm;
55137
+ const lineCodes = codes.slice(line.start, line.end);
55138
+ if (lineCodes.length === 0) {
55139
+ continue;
55140
+ }
55141
+ const kern = (i) => {
55142
+ const k = i + 1 < line.end && ttf.kerning ? ttf.kerning(realized.glyphIndex(codes[i]), realized.glyphIndex(codes[i + 1])) * emPx / ttf.unitsPerEm : 0;
55143
+ return unhinted ? k : Math.round(k);
55144
+ };
55145
+ const lineDx = Array.from({ length: line.end - line.start }, (_, k) => advanceOf(codes[line.start + k]) + kern(line.start + k));
55146
+ paintPlusEngineRun(rCtx, ef, {
55147
+ codes: lineCodes,
55148
+ glyphIndices: false,
55149
+ x,
55150
+ y: unhinted ? top + Math.round(exactAscent) + n * lineSpacing : Math.round(exact),
55151
+ dx: lineDx.some((v, k) => v !== advanceOf(codes[line.start + k])) || dx ? lineDx : null,
55152
+ dy: null
55153
+ }, clip);
55154
+ }
55155
+ return true;
55156
+ }
55157
+ function trimmedLength(codes) {
55158
+ let n = codes.length;
55159
+ while (n > 0 && codes[n - 1] === 32) n--;
55160
+ return n;
55161
+ }
55162
+ var STRING_FORMAT_NO_WRAP = 4096;
55163
+ var STRING_FORMAT_LINE_LIMIT = 8192;
55164
+ var STRING_FORMAT_NO_CLIP = 16384;
55165
+ function layoutPlusLines(codes, advances, available) {
55166
+ const lines = [];
55167
+ const trimEnd = (start, end) => {
55168
+ while (end > start && codes[end - 1] === 32) end--;
55169
+ return end;
55170
+ };
55171
+ const width = (start, end) => {
55172
+ let w = 0;
55173
+ for (let i = start; i < end; i++) w += advances[i];
55174
+ return w;
55175
+ };
55176
+ let p = 0;
55177
+ while (p <= codes.length) {
55178
+ let e = p;
55179
+ while (e < codes.length && codes[e] !== 10 && codes[e] !== 13) e++;
55180
+ let pos = p;
55181
+ do {
55182
+ let end = trimEnd(pos, e);
55183
+ if (Number.isFinite(available) && width(pos, end) > available) {
55184
+ let best = -1;
55185
+ for (let i = pos + 1; i < e; i++) {
55186
+ if (codes[i - 1] === 32 && codes[i] !== 32 && width(pos, trimEnd(pos, i)) <= available) best = i;
55187
+ }
55188
+ if (best > pos) {
55189
+ end = best;
55190
+ } else {
55191
+ end = pos + 1;
55192
+ while (end < e && codes[end] !== 32 && width(pos, end + 1) <= available) end++;
55193
+ }
55194
+ }
55195
+ lines.push({ start: pos, end: trimEnd(pos, end) });
55196
+ pos = end;
55197
+ while (pos < e && codes[pos] === 32 && pos > p) pos++;
55198
+ } while (pos < e);
55199
+ p = e + (codes[e] === 13 && codes[e + 1] === 10 ? 2 : 1);
55200
+ }
55201
+ return lines;
55002
55202
  }
55003
55203
  var DRIVER_STRING_CMAP_LOOKUP = 1;
55004
55204
  var DRIVER_STRING_VERTICAL = 2;
@@ -55809,7 +56009,8 @@ function handleEmfPlusObjectRecord(rCtx, recFlags, dataOff, recDataSize, cacheKe
55809
56009
  lineAlignment: lineAlignment ?? 0,
55810
56010
  leadingMargin: hasSpacing ? view.getFloat32(dataOff + 36, true) : void 0,
55811
56011
  trailingMargin: hasSpacing ? view.getFloat32(dataOff + 40, true) : void 0,
55812
- tracking: hasSpacing ? view.getFloat32(dataOff + 44, true) : void 0
56012
+ tracking: hasSpacing ? view.getFloat32(dataOff + 44, true) : void 0,
56013
+ trimming: recDataSize >= 52 ? view.getUint32(dataOff + 48, true) : void 0
55813
56014
  });
55814
56015
  }
55815
56016
  break;
package/dist/index.mjs CHANGED
@@ -36983,7 +36983,35 @@ function parseTrueTypeFace(view, base = 0) {
36983
36983
  }
36984
36984
  const locaAt = (g) => longLoca ? view.getUint32(loca.offset + g * 4) : view.getUint16(loca.offset + g * 2) * 2;
36985
36985
  const glyphCache = /* @__PURE__ */ new Map();
36986
+ const kernT = tables.get("kern");
36987
+ let kernPairs = null;
36988
+ const kerning = (left, right) => {
36989
+ if (!kernT) return 0;
36990
+ if (!kernPairs) {
36991
+ kernPairs = /* @__PURE__ */ new Map();
36992
+ const end = kernT.offset + kernT.length;
36993
+ if (kernT.offset + 4 <= end && view.getUint16(kernT.offset) === 0) {
36994
+ const n = view.getUint16(kernT.offset + 2);
36995
+ let o = kernT.offset + 4;
36996
+ for (let t = 0; t < n && o + 6 <= end; t++) {
36997
+ const length = view.getUint16(o + 2);
36998
+ const coverage = view.getUint16(o + 4);
36999
+ if (coverage >> 8 === 0 && (coverage & 7) === 1 && o + 14 <= end) {
37000
+ const pairs = view.getUint16(o + 6);
37001
+ for (let i = 0; i < pairs && o + 14 + i * 6 + 6 <= end; i++) {
37002
+ const p = o + 14 + i * 6;
37003
+ kernPairs.set(view.getUint16(p) * 65536 + view.getUint16(p + 2), view.getInt16(p + 4));
37004
+ }
37005
+ }
37006
+ if (length < 6) break;
37007
+ o += length;
37008
+ }
37009
+ }
37010
+ }
37011
+ return kernPairs.get(left * 65536 + right) ?? 0;
37012
+ };
36986
37013
  return {
37014
+ kerning,
36987
37015
  family: names2.get(1) ?? "",
36988
37016
  subfamily: names2.get(2) ?? "",
36989
37017
  fullName: names2.get(4) ?? "",
@@ -42877,7 +42905,15 @@ var RealizedFont = class {
42877
42905
  this.advanceOutlines = /* @__PURE__ */ new Map();
42878
42906
  this.outlines = /* @__PURE__ */ new Map();
42879
42907
  this.glyphs = /* @__PURE__ */ new Map();
42908
+ /**
42909
+ * The font was asked for at NONANTIALIASED_QUALITY (GDI+ SingleBitPerPixelGridFit), not turned monochrome by
42910
+ * the `gasp` table under AntiAliasGridFit: those glyphs keep the previous path (the native grayscale corpora,
42911
+ * `gdiplus-courier-gray` and `gdiplus-shpix-x`, lose exact glyphs the GDI way).
42912
+ */
42913
+ this.singleBitRequested = false;
42880
42914
  this.reverseCmap = null;
42915
+ this.gdiMonoSize = null;
42916
+ this.gdiMonoOutlines = /* @__PURE__ */ new Map();
42881
42917
  this.ctSize = null;
42882
42918
  /** CLEARTYPE_NATURAL_QUALITY: ClearType glyphs keep their own (natural) widths. */
42883
42919
  this.naturalWidths = false;
@@ -42937,6 +42973,17 @@ var RealizedFont = class {
42937
42973
  * way, at a half turn it rounded (all 530 ascents and descents of five faces at lfHeight -8..-60 for 90, 180 and
42938
42974
  * 270 degrees, escapement and world rotation alike). Undefined for an upright baseline.
42939
42975
  */
42976
+ /**
42977
+ * GDI+ SingleBitPerPixelGridFit glyphs are close to GDI's monochrome glyphs: hinted with GDI's arithmetic and
42978
+ * scan-converted with GDI's converter, 1,658 of 1,680 native upright regular glyphs are exact (`xwvyAkMWKVXZ4&%`,
42979
+ * Arial, Tahoma, Times New Roman and Segoe UI at 9 to 36 px), against 1,442 with the previous arithmetic. GDI+'s
42980
+ * own arithmetic is neither: 15 glyphs (Arial 16 `w` among them) are exact only the old way, and no subset of
42981
+ * GDI's projection, movement and ISECT rules gets both. Italic faces keep the previous path (Times New Roman
42982
+ * Italic 22 in `textx-plus-singlebitgridfit`: 8 pixels the GDI way).
42983
+ */
42984
+ plusMonoAsGdi(gdiPlus, mode, gridFit) {
42985
+ return gdiPlus && mode === "mono" && gridFit && this.singleBitRequested;
42986
+ }
42940
42987
  rotatedMetrics(ux, uy) {
42941
42988
  if (Math.abs(ux) > 1e-9 && Math.abs(uy) > 1e-9) {
42942
42989
  return { ascent: this.rotatedAscent, descent: this.rotatedDescent };
@@ -42971,6 +43018,20 @@ var RealizedFont = class {
42971
43018
  }
42972
43019
  return this.reverseCmap.get(index) ?? 65533;
42973
43020
  }
43021
+ /**
43022
+ * The outline of an upright GDI+ SingleBitPerPixelGridFit glyph, hinted
43023
+ * with GDI's arithmetic (see {@link plusMonoAsGdi}). Advances keep coming
43024
+ * from {@link outline}, whose placement the DrawString fixtures pin.
43025
+ */
43026
+ gdiMonoOutline(index) {
43027
+ let g = this.gdiMonoOutlines.get(index);
43028
+ if (!g) {
43029
+ this.gdiMonoSize ?? (this.gdiMonoSize = new HintedSize(this.ttf, this.ppemX, this.ppem, { version: 35, grayscale: false, gdiArithmetic: true, reportStretched: this.playbackStretched }, true));
43030
+ g = this.gdiMonoSize.hintGlyph(index < this.ttf.numGlyphs ? index : NOTDEF);
43031
+ this.gdiMonoOutlines.set(index, g);
43032
+ }
43033
+ return g;
43034
+ }
42974
43035
  outline(index) {
42975
43036
  let g = this.outlines.get(index);
42976
43037
  if (!g) {
@@ -43117,7 +43178,8 @@ var RealizedFont = class {
43117
43178
  }
43118
43179
  const skew = m && !(Math.abs(m[1]) < 1e-9 && Math.abs(m[2]) < 1e-9) && !(Math.abs(m[0]) < 1e-9 && Math.abs(m[3]) < 1e-9);
43119
43180
  const rotation = skew && m && !this.gdiPlus && this.ppemX === this.ppem ? rotationMatrix(m, this.ppem, (this.ttf.headFlags & 8) !== 0) : null;
43120
- const src = skew ? this.rotatedOutline(index, rotation ? rotation.ppem : 0) : this.outline(index);
43181
+ const asGdiMono = !m && !subX && !subY && this.ppemX === this.ppem && !this.syntheticItalic && !(this.ttf.fsSelection & 1) && this.plusMonoAsGdi(this.gdiPlus, this.mode, this.gridFit);
43182
+ const src = skew ? this.rotatedOutline(index, rotation ? rotation.ppem : 0) : asGdiMono ? this.gdiMonoOutline(index) : this.outline(index);
43121
43183
  if (skew && m && rotation) {
43122
43184
  m = rotation.matrix;
43123
43185
  } else if (skew && m) {
@@ -43160,7 +43222,7 @@ var RealizedFont = class {
43160
43222
  if (this.mode === "mono") {
43161
43223
  const smartGrid = !m && !subX && !subY && (!this.gdiPlus || this.ppemX === this.ppem && !this.syntheticItalic);
43162
43224
  const controlOn = src.scanSet ? src.scanControl : this.ppem <= DEFAULT_DROPOUT_PPEM;
43163
- bitmap = this.gdiPlus ? rasterizeMono(o, this.gdiPlus && !this.gridFit ? UNHINTED_MONO_DROPOUT : dropoutMode(controlOn, src.scanType), void 0, true, smartGrid, false, false, false, false, true) : msMonoBitmap(o, msScanFlags(controlOn, src.scanType));
43225
+ bitmap = this.gdiPlus && !asGdiMono ? rasterizeMono(o, this.gdiPlus && !this.gridFit ? UNHINTED_MONO_DROPOUT : dropoutMode(controlOn, src.scanType), void 0, true, smartGrid, false, false, false, false, true) : msMonoBitmap(o, msScanFlags(controlOn, src.scanType));
43164
43226
  } else if (this.mode === "cleartype") {
43165
43227
  const dropout = src.scanSet ? dropoutMode(src.scanControl, src.scanType) : !this.gdiPlus ? 0 : 2;
43166
43228
  bitmap = rasterizeClearType(o, this.gdiPlus, this.gdiPlus ? clearTypeSpread(this.ttf) : void 0, dropout, !this.gdiPlus && clearTypeSmoothsVertically(this.ttf, this.ppem));
@@ -43415,6 +43477,7 @@ var GdiFontCollection = class _GdiFontCollection {
43415
43477
  this.installedFonts
43416
43478
  );
43417
43479
  tt2.naturalWidths = spec.quality === CLEARTYPE_NATURAL_QUALITY;
43480
+ tt2.singleBitRequested = spec.quality === NONANTIALIASED_QUALITY;
43418
43481
  return tt2;
43419
43482
  };
43420
43483
  let tt = make(resolvePpem(best, spec.height), spec.height > 0 ? Math.round(spec.height) : 0);
@@ -54868,10 +54931,16 @@ function worldUnitFont(rCtx, obj) {
54868
54931
  const em = obj.emSize * getPageUnitMultiplier(unit, 1, dpi) / (page || 1);
54869
54932
  return Number.isFinite(em) && em > 0 ? { ...obj, emSize: em, unit: 0 } : obj;
54870
54933
  }
54934
+ function resolvePlusTextHint(hint, smoothing) {
54935
+ if (hint !== 0) {
54936
+ return hint;
54937
+ }
54938
+ return smoothing === "mono" ? 1 : smoothing === "gray" ? 3 : 5;
54939
+ }
54871
54940
  function realizePlusEngineFont(rCtx, font, paint, brushSampler2) {
54872
54941
  const fonts = rCtx.fonts;
54873
54942
  const color = typeof paint === "string" ? rgbaToHex(paint) : null;
54874
- const hint = rCtx.textRenderingHint ?? 0;
54943
+ const hint = resolvePlusTextHint(rCtx.textRenderingHint ?? 0, fonts?.defaultSmoothing);
54875
54944
  const graySmooth = hint === 3 || hint === 4 || hint === 5;
54876
54945
  const solidViaCoverage = !!color && graySmooth && !isSvgContext(rCtx.ctx);
54877
54946
  const sampler = isSvgContext(rCtx.ctx) ? null : solidViaCoverage ? solidSampler(cssColorToArgb(paint) ?? 4278190080) : color ? null : brushSampler2;
@@ -54917,14 +54986,14 @@ function realizePlusEngineFont(rCtx, font, paint, brushSampler2) {
54917
54986
  plusBars: hint === 4 ? "fractional" : "snapped"
54918
54987
  };
54919
54988
  }
54920
- function paintPlusEngineRun(rCtx, ef, run) {
54989
+ function paintPlusEngineRun(rCtx, ef, run, clip) {
54921
54990
  const base = {
54922
54991
  ...run,
54923
54992
  textAlign: 24,
54924
54993
  bkColor: "#ffffff",
54925
54994
  bkMode: 1,
54926
- options: 0,
54927
- rect: null,
54995
+ options: clip ? ETO_CLIPPED : 0,
54996
+ rect: clip ?? null,
54928
54997
  matrix: null
54929
54998
  };
54930
54999
  if ((!ef.color || ef.solidViaCoverage) && ef.sampler) {
@@ -54932,6 +55001,19 @@ function paintPlusEngineRun(rCtx, ef, run) {
54932
55001
  if (!cov) {
54933
55002
  return true;
54934
55003
  }
55004
+ if (clip) {
55005
+ for (let yy = 0; yy < cov.height; yy++) {
55006
+ const py = cov.y + yy;
55007
+ for (let xx = 0; xx < cov.width; xx++) {
55008
+ const px = cov.x + xx;
55009
+ if (px < clip.left || px >= clip.right || py < clip.top || py >= clip.bottom) {
55010
+ const i = yy * cov.width + xx;
55011
+ cov.data.fill(0, i * cov.channels, (i + 1) * cov.channels);
55012
+ if (cov.bars) cov.bars[i] = 0;
55013
+ }
55014
+ }
55015
+ }
55016
+ }
54935
55017
  const gamma = textGamma(rCtx.ext?.textContrast);
54936
55018
  if (cov.channels === 1) {
54937
55019
  applyTextContrast(cov.data, gamma);
@@ -54995,8 +55077,126 @@ function drawPlusStringWithEngine(rCtx, font, text, layoutX, layoutY, layoutW, l
54995
55077
  const tracking = format?.tracking ?? PLUS_DEFAULT_TRACKING;
54996
55078
  const ctGrid = !unhinted && realized.mode === "cleartype" && !!realized.ctGridAdvance && !realized.syntheticItalic && tracking > 1;
54997
55079
  const designUnits = !!realized.designAdvance && m[0] === 1 && m[3] === 1;
54998
- const dx = unhinted ? codes.map((c) => designUnits ? realized.designAdvance(realized.glyphIndex(c), tracking) : realized.advance(realized.glyphIndex(c)) * tracking) : ctGrid ? codes.map((c) => realized.ctGridAdvance(realized.glyphIndex(c))) : null;
54999
- return paintPlusEngineRun(rCtx, ef, { codes, glyphIndices: false, x, y, dx, dy: null });
55080
+ const advanceOf = (c) => {
55081
+ const gi = realized.glyphIndex(c);
55082
+ return unhinted ? designUnits ? realized.designAdvance(gi, tracking) : realized.advance(gi) * tracking : ctGrid ? realized.ctGridAdvance(gi) : realized.advance(gi);
55083
+ };
55084
+ const dx = unhinted || ctGrid ? codes.map(advanceOf) : null;
55085
+ const flags = format?.flags ?? 0;
55086
+ const left = m[0] * layoutX + m[4];
55087
+ const top = m[3] * layoutY + m[5];
55088
+ const width = m[0] * layoutW;
55089
+ const height = m[3] * layoutH;
55090
+ const rect = width > 0 && height > 0 && !(flags & STRING_FORMAT_NO_CLIP) ? { left: Math.round(left), top: Math.round(top), right: Math.round(left + width), bottom: Math.round(top + height) } : void 0;
55091
+ let clip = void 0;
55092
+ const available = width > 0 && !(flags & STRING_FORMAT_NO_WRAP) ? width - emPx * ((format?.leadingMargin ?? PLUS_LEADING_EM) + (format?.trailingMargin ?? PLUS_LEADING_EM)) : Infinity;
55093
+ const fit = (realized.designAdvance ? codes.map((c) => realized.designAdvance(realized.glyphIndex(c), tracking)) : codes.map(advanceOf)).map((w, i) => i + 1 < codes.length && ttf.kerning ? w + ttf.kerning(realized.glyphIndex(codes[i]), realized.glyphIndex(codes[i + 1])) * emPx / ttf.unitsPerEm : w);
55094
+ let lines = layoutPlusLines(codes, fit, available);
55095
+ const paragraph = lines.length > 1;
55096
+ if (paragraph) clip = rect;
55097
+ const descentUnits = ttf.winDescent ?? 0;
55098
+ const lineUnits = ttf.hheaAscender !== void 0 && ttf.hheaDescender !== void 0 ? ttf.hheaAscender - ttf.hheaDescender + (ttf.hheaLineGap ?? 0) : ttf.winAscent + descentUnits;
55099
+ const lineSpacing = lineUnits * emPx / ttf.unitsPerEm;
55100
+ if (rect) {
55101
+ const visible = flags & STRING_FORMAT_LINE_LIMIT ? Math.floor((height - (ttf.winAscent + descentUnits) * emPx / ttf.unitsPerEm) / lineSpacing) + 1 : Math.floor(height / lineSpacing - 0.25) + 1;
55102
+ if (lines.length > visible) {
55103
+ clip = rect;
55104
+ const kept = lines.slice(0, Math.max(0, visible));
55105
+ if (kept.length > 0 && (format?.trimming ?? 1) === 1) {
55106
+ const last = kept[kept.length - 1];
55107
+ let end = last.start;
55108
+ while (end < codes.length && codes[end] !== 10 && codes[end] !== 13) {
55109
+ let w = 0;
55110
+ for (let i = last.start; i <= end; i++) w += fit[i];
55111
+ if (w > available && end > last.start) break;
55112
+ end++;
55113
+ }
55114
+ while (end > last.start && codes[end - 1] === 32) end--;
55115
+ kept[kept.length - 1] = { start: last.start, end };
55116
+ }
55117
+ lines = kept;
55118
+ }
55119
+ }
55120
+ if (lines.length === 0) {
55121
+ return true;
55122
+ }
55123
+ if (!paragraph) {
55124
+ return paintPlusEngineRun(rCtx, ef, {
55125
+ codes: codes.slice(0, lines[0].end === trimmedLength(codes) ? codes.length : lines[0].end),
55126
+ glyphIndices: false,
55127
+ x,
55128
+ y,
55129
+ dx: dx ? dx.slice(0, lines[0].end === trimmedLength(codes) ? codes.length : lines[0].end) : null,
55130
+ dy: null
55131
+ }, clip);
55132
+ }
55133
+ for (const [n, line] of lines.entries()) {
55134
+ const exact = top + (ttf.winAscent + n * lineUnits) * emPx / ttf.unitsPerEm;
55135
+ const lineCodes = codes.slice(line.start, line.end);
55136
+ if (lineCodes.length === 0) {
55137
+ continue;
55138
+ }
55139
+ const kern = (i) => {
55140
+ const k = i + 1 < line.end && ttf.kerning ? ttf.kerning(realized.glyphIndex(codes[i]), realized.glyphIndex(codes[i + 1])) * emPx / ttf.unitsPerEm : 0;
55141
+ return unhinted ? k : Math.round(k);
55142
+ };
55143
+ const lineDx = Array.from({ length: line.end - line.start }, (_, k) => advanceOf(codes[line.start + k]) + kern(line.start + k));
55144
+ paintPlusEngineRun(rCtx, ef, {
55145
+ codes: lineCodes,
55146
+ glyphIndices: false,
55147
+ x,
55148
+ y: unhinted ? top + Math.round(exactAscent) + n * lineSpacing : Math.round(exact),
55149
+ dx: lineDx.some((v, k) => v !== advanceOf(codes[line.start + k])) || dx ? lineDx : null,
55150
+ dy: null
55151
+ }, clip);
55152
+ }
55153
+ return true;
55154
+ }
55155
+ function trimmedLength(codes) {
55156
+ let n = codes.length;
55157
+ while (n > 0 && codes[n - 1] === 32) n--;
55158
+ return n;
55159
+ }
55160
+ var STRING_FORMAT_NO_WRAP = 4096;
55161
+ var STRING_FORMAT_LINE_LIMIT = 8192;
55162
+ var STRING_FORMAT_NO_CLIP = 16384;
55163
+ function layoutPlusLines(codes, advances, available) {
55164
+ const lines = [];
55165
+ const trimEnd = (start, end) => {
55166
+ while (end > start && codes[end - 1] === 32) end--;
55167
+ return end;
55168
+ };
55169
+ const width = (start, end) => {
55170
+ let w = 0;
55171
+ for (let i = start; i < end; i++) w += advances[i];
55172
+ return w;
55173
+ };
55174
+ let p = 0;
55175
+ while (p <= codes.length) {
55176
+ let e = p;
55177
+ while (e < codes.length && codes[e] !== 10 && codes[e] !== 13) e++;
55178
+ let pos = p;
55179
+ do {
55180
+ let end = trimEnd(pos, e);
55181
+ if (Number.isFinite(available) && width(pos, end) > available) {
55182
+ let best = -1;
55183
+ for (let i = pos + 1; i < e; i++) {
55184
+ if (codes[i - 1] === 32 && codes[i] !== 32 && width(pos, trimEnd(pos, i)) <= available) best = i;
55185
+ }
55186
+ if (best > pos) {
55187
+ end = best;
55188
+ } else {
55189
+ end = pos + 1;
55190
+ while (end < e && codes[end] !== 32 && width(pos, end + 1) <= available) end++;
55191
+ }
55192
+ }
55193
+ lines.push({ start: pos, end: trimEnd(pos, end) });
55194
+ pos = end;
55195
+ while (pos < e && codes[pos] === 32 && pos > p) pos++;
55196
+ } while (pos < e);
55197
+ p = e + (codes[e] === 13 && codes[e + 1] === 10 ? 2 : 1);
55198
+ }
55199
+ return lines;
55000
55200
  }
55001
55201
  var DRIVER_STRING_CMAP_LOOKUP = 1;
55002
55202
  var DRIVER_STRING_VERTICAL = 2;
@@ -55807,7 +56007,8 @@ function handleEmfPlusObjectRecord(rCtx, recFlags, dataOff, recDataSize, cacheKe
55807
56007
  lineAlignment: lineAlignment ?? 0,
55808
56008
  leadingMargin: hasSpacing ? view.getFloat32(dataOff + 36, true) : void 0,
55809
56009
  trailingMargin: hasSpacing ? view.getFloat32(dataOff + 40, true) : void 0,
55810
- tracking: hasSpacing ? view.getFloat32(dataOff + 44, true) : void 0
56010
+ tracking: hasSpacing ? view.getFloat32(dataOff + 44, true) : void 0,
56011
+ trimming: recDataSize >= 52 ? view.getUint32(dataOff + 48, true) : void 0
55811
56012
  });
55812
56013
  }
55813
56014
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emf-converter",
3
- "version": "4.25.7",
3
+ "version": "4.27.0",
4
4
  "description": "Convert EMF/WMF metafile binaries to PNG, JPEG or WebP data URLs, SVG (markup, data URL, React/JSX) or PDF",
5
5
  "keywords": [
6
6
  "canvas",