Skip to content

Commit 421d59e

Browse files
committed
Replaced opentype.js build with submodule
1 parent 728921a commit 421d59e

16 files changed

Lines changed: 42 additions & 48 deletions

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
[submodule "tests/test-assets"]
55
path = tests/test-assets
66
url = https://github.com/scribe-js/test-assets.git
7+
[submodule "js/font-parser"]
8+
path = js/font-parser
9+
url = https://github.com/scribe-js/font-parser.git

.npmignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ karma.conf.cjs
2424
jsconfig.json
2525
.eslintrc.json
2626
.gitmodules
27-
build-deno-compile.sh
27+
build-deno-compile.sh
28+
js/font-parser/fonts
29+
js/font-parser/test

js/containers/fontContainer.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// all fonts should have an identical OpenType.js and FontFace version.
55

66
// Node.js case
7-
import opentype from '../../lib/opentype.module.js';
7+
import opentype from '../font-parser/src/index.js';
88
import { determineSansSerif, getStyleLookup, clearObjectProperties } from '../utils/miscUtils.js';
99
import { ca } from '../canvasAdapter.js';
1010

@@ -43,9 +43,13 @@ export function checkMultiFontMode(charMetricsObj) {
4343
* @param {string|ArrayBuffer} src
4444
* @param {?Object.<string, number>} [kerningPairs=null]
4545
*/
46+
/** Tables not used by scribe.js — skipping them avoids decompression and parsing overhead. */
47+
const defaultSkipTables = ['GSUB', 'GPOS', 'OS/2', 'cvt ', 'fpgm', 'prep', 'COLR', 'CPAL', 'meta'];
48+
4649
export async function loadOpentype(src, kerningPairs = null) {
47-
const font = typeof (src) === 'string' ? await opentype.load(src) : await opentype.parse(src, { lowMemory: false });
48-
font.tables.gsub = null;
50+
const font = typeof (src) === 'string'
51+
? opentype.parse(await fetch(src).then((r) => r.arrayBuffer()), { skipTables: defaultSkipTables })
52+
: opentype.parse(src, { skipTables: defaultSkipTables });
4953
// Re-apply kerningPairs object so when toArrayBuffer is called on this font later (when making a pdf) kerning data will be included
5054
if (kerningPairs) font.kerningPairs = kerningPairs;
5155
return font;

js/export/pdf/writePdf.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ function consolidateAnnotations(pageAnnotations, pageObj) {
484484
* @param {boolean} [params.rotateBackground=false]
485485
* @param {number} [params.confThreshHigh=85]
486486
* @param {number} [params.confThreshMed=75]
487-
* @param {?import('opentype.js').Font} [params.fontChiSim=null]
487+
* @param {?import('../../font-parser/src/font.js').Font} [params.fontChiSim=null]
488488
* @param {Array<number>} [params.imageObjIndices=[]] - Array of image object indices
489489
* @param {?string} [params.imageName=null]
490490
* @param {Array<AnnotationHighlight>} [params.pageAnnotations=[]] - Highlight annotations for this page

js/export/pdf/writePdfFonts.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export function hex(arrayBuffer) {
3333
* Creates a ToUnicode CMap string for a font.
3434
* The CMap maps character codes to Unicode values to enable text extraction.
3535
*
36-
* @param {import('opentype.js').Font} font - Opentype.js font object
36+
* @param {import('../../font-parser/src/font.js').Font} font - Opentype.js font object
3737
* @returns {string} The ToUnicode CMap content string
3838
*/
3939
export function createToUnicode(font) {
@@ -107,7 +107,7 @@ const generateFontFlags = (serif, italic, smallcap, symbolic) => { /* eslint-dis
107107

108108
/**
109109
*
110-
* @param {opentype.Font} font - Opentype.js font object
110+
* @param {opentypeFont} font - Opentype.js font object
111111
* @param {number} objIndex - Index for font descriptor PDF object
112112
* @param {boolean} italic
113113
* @param {?number} embeddedObjIndex - Index for embedded font file PDF object.
@@ -174,7 +174,7 @@ function createFontDescriptor(font, objIndex, italic, embeddedObjIndex = null) {
174174
* Converts a Opentype.js font object into an array of strings containing PDF objects.
175175
* The font is represented as a simple "Type 1" font.
176176
*
177-
* @param {opentype.Font} font - Opentype.js font object
177+
* @param {opentypeFont} font - Opentype.js font object
178178
* @param {number} firstObjIndex - Index for the first PDF object
179179
* @param {boolean} [italic=false] - Whether the font is italic.
180180
* @param {boolean} [isStandardFont=false] - Whether the font is a standard font.
@@ -250,7 +250,7 @@ export function createEmbeddedFontType1(font, firstObjIndex, italic = false, isS
250250
* The font is represented as a composite "Type 0" font.
251251
*
252252
* @param {Object} options - Configuration object
253-
* @param {opentype.Font} options.font - Opentype.js font object
253+
* @param {opentypeFont} options.font - Opentype.js font object
254254
* @param {number} options.firstObjIndex - Index for the first PDF object
255255
* @param {boolean} [options.italic=false] - Whether the font is italic.
256256
*

js/font-parser

Submodule font-parser added at 1c58d24

js/fontEval.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { gs } from './generalWorkerMain.js';
1111
/**
1212
* Compute the RMSD between OCR character widths and a font's glyph advance widths.
1313
*
14-
* @param {opentype.Font} fontOpentype
14+
* @param {opentypeFont} fontOpentype
1515
* @param {CharMetricsFont} charMetrics
1616
* @returns {number}
1717
*/

js/global.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ declare global {
9090
boldItalic: ArrayBuffer | null;
9191
};
9292

93-
type opentypeFont = import("../lib/opentype.module.js").Font;
94-
type opentypeGlyph = import("../lib/opentype.module.js").Glyph;
93+
type opentypeFont = import("./font-parser/src/index.js").Font;
94+
type opentypeGlyph = import("./font-parser/src/index.js").Glyph;
9595
type GeneralScheduler = import("./generalWorkerMain.js").GeneralScheduler;
9696

9797
// Image objects

js/import/convertDocDocx.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ function isSentenceEnding(word) {
5757
return true;
5858
}
5959

60-
/** @type {?opentype.Font} */
60+
/** @type {?opentypeFont} */
6161
let fontOpentype = null;
6262

6363
/**
6464
* Calculates the advance of a string in pixels.
6565
* @param {string} text
6666
* @param {number} size
67-
* @param {opentype.Font} font
67+
* @param {opentypeFont} font
6868
*/
6969
function getTextWidth(text, size, font) {
7070
const charMetrics = calcWordCharMetrics(text, font);

js/import/convertPageText.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ const LINE_HEIGHT = 14.4;
1111
const MARGIN_VERTICAL = 30;
1212
const MARGIN_HORIZONTAL = 20;
1313

14-
/** @type {?opentype.Font} */
14+
/** @type {?opentypeFont} */
1515
let fontOpentype = null;
1616

1717
/**
1818
* Calculates the advance of a string in pixels.
1919
* @param {string} text
2020
* @param {number} size
21-
* @param {opentype.Font} font
21+
* @param {opentypeFont} font
2222
*/
2323
function getTextWidth(text, size, font) {
2424
const { advanceArr, kerningArr } = calcWordCharMetrics(text, font);

0 commit comments

Comments
 (0)