@openpresentation/opf-render 0.33.0 → 0.34.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.
@@ -1,3 +1,4 @@
1
+ import { embeddingAllowed, fsTypeOf } from './font-fstype.js';
1
2
  // RR-64: `text: "paths"` draws every <text> of a slide as glyph outlines, so the SVG needs no font. The renderer has
2
3
  // already decided every line, position and width (x, y, text-anchor, textLength); the vector PDF export's SVG text layout
3
4
  // (layoutText: text chunks, bidirectional runs, per-character fallback, fontkit shaping, textLength) turns that into positioned
@@ -13,7 +14,7 @@
13
14
 
14
15
  import { FontLibrary, shape as fontkitShape } from "./pdf-fonts.js";
15
16
  import { featuresOf, layoutText } from "./pdf-text.js";
16
- import { ROOT_STYLE, attributesOf, inheritStyle, parseFontFamilies, parseLength } from "./pdf-style.js";
17
+ import { ROOT_STYLE, attributesOf, inheritStyle, parseFontFamilies, parseFontWeight, parseLength } from "./pdf-style.js";
17
18
  import { openTypeLanguage } from "./script-fonts.js";
18
19
  import { decodeEntities, escapeAttribute, escapeText, parseXml } from "./pdf-xml.js";
19
20
 
@@ -68,7 +69,16 @@ function newLibrary(fonts) {
68
69
  // (only the PDF cannot subset it); a face whose fsType restricts embedding is never outlined: its text stays text.
69
70
  function addFace(library, data, family, described) {
70
71
  const face = library.addData(data, "fonts", family);
71
- if (!face || face.outlineStyled) return;
72
+ prepareOutlineFace(library,face,described);
73
+ }
74
+
75
+ // Apply actual byte permissions BEFORE permitting a CFF outline face.
76
+ export function prepareOutlineFace(library,face,described) {
77
+ if (!face) return;
78
+ library.restricted??=new Set();
79
+ if(!embeddingAllowed(fsTypeOf(face.data))||!embeddingAllowed(face.fsType)){face.embeddable=false;face.unusable="pdf-font-embedding-restricted";}
80
+ if(face.unusable==="pdf-font-embedding-restricted")for(const name of face.names)library.restricted.add(name);
81
+ if(face.outlineStyled)return;
72
82
  if (Number.isInteger(described?.weight)) face.weight = described.weight;
73
83
  if (typeof described?.italic === "boolean") face.italic = described.italic;
74
84
  if (face.unusable === "pdf-font-unsupported-format" && face.font) face.embeddable = true;
@@ -88,7 +98,7 @@ function decodeBase64(text) {
88
98
  * `rootStyle` holds what the <svg> element gives its children (lang), `report(diagnostic)` receives what cannot be outlined
89
99
  * and `fail(code, message)` builds the error thrown when no face draws a text. Returns the new content and the <defs> markup.
90
100
  */
91
- export function outlineSlideText(content, { library, shaper, rootStyle = {}, report = () => {}, fail }) {
101
+ export function outlineSlideText(content, { library, shaper, shape, selectText, onRefusal, validateFace, rootStyle = {}, report = () => {}, fail }) {
92
102
  const defs = new Map();
93
103
  const reported = new Set();
94
104
  const env = {
@@ -96,11 +106,21 @@ export function outlineSlideText(content, { library, shaper, rootStyle = {}, rep
96
106
  defaults: [],
97
107
  splitByOwner: true,
98
108
  // HarfBuzz, the browser's shaper, when the handle carries one (RR-64 phase 2); fontkit otherwise.
99
- ...(shaper ? { shape: harfBuzzShape(shaper) } : {}),
109
+ ...(shape ? {shape} : shaper ? { shape: harfBuzzShape(shaper) } : {}),
100
110
  // The renderer reports its own font substitutions and missing glyphs while it lays the slide out.
101
- diagnostic: () => {},
102
- missing: () => {},
103
- unavailable: (families) => fail("text-as-paths-font-unavailable", `No font face can outline text in ${families.length ? `'${families.join("', '")}'` : "the requested family"}; pass the fonts handle the slide was laid out with.`),
111
+ diagnostic: onRefusal ? issue => {
112
+ const family=issue.requestedFamily??issue.fontFamily,restricted=library.restricted?.has(family?.toLowerCase());
113
+ // An absent first name can legitimately select a later named/generic
114
+ // family. Defer ONLY that cosmetic warning to the mandatory full-stack,
115
+ // weight/style/face validation below; glyph fallback and unknown issues
116
+ // still refuse immediately, as does a restricted first family.
117
+ if(issue.code==="pdf-font-substituted"&&!restricted&&typeof validateFace==="function")return;
118
+ throw new OutlineRefusal(restricted?"restricted":"unresolved",family);
119
+ } : () => {},
120
+ missing: onRefusal ? issue => {throw new OutlineRefusal("unresolved",issue.fontFamily);} : () => {},
121
+ onRefusal,
122
+ validateFace,
123
+ unavailable: (families) => onRefusal ? new OutlineRefusal(families.some(family=>library.restricted?.has(family.toLowerCase()))?"restricted":"unresolved",families.join(", ")) : fail("text-as-paths-font-unavailable", `No font face can outline text in ${families.length ? `'${families.join("', '")}'` : "the requested family"}; pass the fonts handle the slide was laid out with.`),
104
124
  };
105
125
  const base = inheritStyle(ROOT_STYLE, rootStyle);
106
126
  const REASONS = {
@@ -111,15 +131,15 @@ export function outlineSlideText(content, { library, shaper, rootStyle = {}, rep
111
131
  const keep = (family, reason) => {
112
132
  if (reported.has(`${family}|${reason}`)) return;
113
133
  reported.add(`${family}|${reason}`);
114
- report({ code: "text-as-paths-fallback", reason, fontFamily: family, message: `'${family}' ${REASONS[reason]}; that text stays text and the SVG embeds its face.` });
134
+ report({ code: "text-as-paths-fallback", reason, fontFamily: family, message: `'${family}' ${REASONS[reason]}; ${reason==='restricted'?'that text stays text and the restricted face is not embedded':'that text stays text and the SVG embeds its face'}.` });
115
135
  };
116
- const outlined = content.map((markup) => typeof markup === "string" && markup.includes("<text") ? outlineMarkup(markup, base, env, defs, keep) : markup);
136
+ const outlined = content.map((markup) => typeof markup === "string" && markup.includes("<text") ? outlineMarkup(markup, base, env, defs, keep, selectText) : markup);
117
137
  const definitions = [...defs].map(([id, d]) => `<path id="${id}" d="${d}"/>`).join("");
118
138
  return { content: outlined, defs: definitions ? `<defs>${definitions}</defs>` : "" };
119
139
  }
120
140
 
121
141
  // Splices each <text> element of `markup`; the style it inherits is folded from its ancestors' attributes.
122
- function outlineMarkup(markup, base, env, defs, keep) {
142
+ function outlineMarkup(markup, base, env, defs, keep, selectText) {
123
143
  const ancestors = [];
124
144
  let out = "", from = 0, start = -1, depth = 0, parentStyle = null;
125
145
  for (const match of markup.matchAll(TOKENS)) {
@@ -129,7 +149,7 @@ function outlineMarkup(markup, base, env, defs, keep) {
129
149
  if (name === "text") depth += close ? -1 : selfClose ? 0 : 1;
130
150
  if (depth > 0) continue;
131
151
  const end = match.index + token.length;
132
- out += markup.slice(from, start) + outlineText(markup.slice(start, end), parentStyle, env, defs, keep);
152
+ out += markup.slice(from, start) + outlineText(markup.slice(start, end), parentStyle, env, defs, keep, selectText);
133
153
  from = end;
134
154
  start = -1;
135
155
  continue;
@@ -138,7 +158,7 @@ function outlineMarkup(markup, base, env, defs, keep) {
138
158
  if (name === "text" && selfClose) {
139
159
  // An empty <text/> (an empty table cell) draws nothing: it becomes an empty group with the element's other attributes.
140
160
  const end = match.index + token.length;
141
- out += markup.slice(from, match.index) + outlineText(token, base, env, defs, keep);
161
+ out += markup.slice(from, match.index) + outlineText(token, base, env, defs, keep, selectText);
142
162
  from = end;
143
163
  continue;
144
164
  }
@@ -159,10 +179,22 @@ function parseAttributes(text) {
159
179
  return attrs;
160
180
  }
161
181
 
162
- function outlineText(source, parentStyle, env, defs, keep) {
182
+ class OutlineRefusal extends Error {
183
+ constructor(reason,fontFamily){super(reason);this.reason=reason;this.fontFamily=fontFamily;}
184
+ }
185
+
186
+ function outlineText(source, parentStyle, env, defs, keep, selectText) {
163
187
  const node = parseXml(`<svg>${source}</svg>`)?.children.find((child) => child.name === "text");
164
- if (!node) return source;
165
- const { runs } = layoutText(node, env, parentStyle, null);
188
+ if (!node || (selectText && !selectText(node))) return source;
189
+ let runs;
190
+ try{({runs}=layoutText(node,env,parentStyle,null));}
191
+ catch(error){if(!(error instanceof OutlineRefusal)||!env.onRefusal)throw error;env.onRefusal({reason:error.reason,fontFamily:error.fontFamily});return source;}
192
+ // Raster compatibility must not partially outline a token and leave the rest
193
+ // to a backend whose bidi/anchor handling is the reason for the rewrite.
194
+ if(env.onRefusal){
195
+ const refusal=runs.map(run=>({run,reason:env.validateFace?.(run.face,run.style)??fallbackReason(run,env)})).find(value=>value.reason);
196
+ if(refusal){env.onRefusal({reason:refusal.reason,fontFamily:[...refusal.run.face.names][0]});return source;}
197
+ }
166
198
  const attrs = Object.entries(node.attrs).filter(([key]) => !TEXT_ONLY.has(key));
167
199
  const hidden = node.attrs["aria-hidden"] === "true";
168
200
  const groups = [], outlined = [];
@@ -208,7 +240,7 @@ function fallbackReason(run, env) {
208
240
  if (COLOUR_TABLES.some((table) => run.face.font.directory?.tables?.[table])) return "colour-font";
209
241
  for (const family of parseFontFamilies(run.style["font-family"])) {
210
242
  if (env.fonts.restricted?.has(family.toLowerCase())) return "restricted";
211
- if (run.face.names.has(family.toLowerCase())) break;
243
+ if (run.face.names.has(family.toLowerCase()) || (env.onRefusal && env.validateFace && env.fonts.match([family],parseFontWeight(run.style["font-weight"],400),/italic|oblique/i.test(run.style["font-style"]??""))?.face===run.face)) break;
212
244
  }
213
245
  if (env.shape && !run.scope) {
214
246
  const direction = run.level % 2 === 1 ? "rtl" : "ltr", language = openTypeLanguage(run.style.lang ?? run.style["xml:lang"]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openpresentation/opf-render",
3
- "version": "0.33.0",
3
+ "version": "0.34.0",
4
4
  "description": "Deterministic local OPF renderer for SVG, PNG, and PDF output.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -94,7 +94,7 @@
94
94
  "fontkit": "^2.0.4",
95
95
  "harfbuzzjs": "1.6.4",
96
96
  "pako": "3.0.3",
97
- "@openpresentation/opf": "^0.33.0"
97
+ "@openpresentation/opf": "^0.34.0"
98
98
  },
99
99
  "peerDependencies": {
100
100
  "@expo-google-fonts/arimo": "0.4.3",
@@ -340,10 +340,10 @@
340
340
  "axe-core": "4.14.0",
341
341
  "esbuild": "0.28.2",
342
342
  "pdf-lib": "1.17.1",
343
- "pdfjs-dist": "6.4.299",
344
- "playwright": "1.64.1",
343
+ "pdfjs-dist": "6.5.438",
344
+ "playwright": "1.65.0",
345
345
  "sharp": "0.35.5",
346
- "@openpresentation/gallery": "0.33.0"
346
+ "@openpresentation/gallery": "0.34.0"
347
347
  },
348
348
  "bin": {
349
349
  "opf-preview-fonts": "./dist/preview-fonts-cli.js"
@@ -400,5 +400,5 @@
400
400
  "test:preview-a11y-browser": "node test/preview-a11y-browser.mjs",
401
401
  "test:forced-colors-browser": "node test/forced-colors-browser.mjs artifacts/forced-colors"
402
402
  },
403
- "gitHead": "26fd4de9cee7e57f9e0620eddcad2dcef04e00e3"
403
+ "gitHead": "8f395d3d332b48dae0b62a2f767a4153eed7ca18"
404
404
  }