Skip to content

Commit cf366d0

Browse files
authored
refactor(pdfkit): remove encode glyphs font methods (#3333)
1 parent a8301b2 commit cf366d0

5 files changed

Lines changed: 44 additions & 30 deletions

File tree

.changeset/sixty-jeans-hug.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@react-pdf/pdfkit": major
3+
"@react-pdf/render": patch
4+
---
5+
6+
refactor(pdfkit): remove encode glyphs font methods

packages/pdfkit/src/font/embedded.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,6 @@ const createEmbeddedFont = (PDFFont) =>
117117
return [res, positions];
118118
}
119119

120-
encodeGlyphs(glyphs) {
121-
const res = [];
122-
for (let i = 0; i < glyphs.length; i++) {
123-
const glyph = glyphs[i];
124-
const gid = this.subset.includeGlyph(glyph.id);
125-
res.push(`0000${gid.toString(16)}`.slice(-4));
126-
127-
if (this.widths[gid] == null) {
128-
this.widths[gid] = glyph.advanceWidth * this.scale;
129-
}
130-
if (this.unicode[gid] == null) {
131-
this.unicode[gid] = glyph.codePoints;
132-
}
133-
}
134-
135-
return res;
136-
}
137-
138120
widthOfString(string, size, features) {
139121
const width = this.layout(string, features, true).advanceWidth;
140122
const scale = size / 1000;

packages/pdfkit/src/font/standard.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@ const createStandardFont = (PDFFont) =>
5050
return [encoded, positions];
5151
}
5252

53-
encodeGlyphs(glyphs) {
54-
const res = [];
55-
56-
for (let glyph of Array.from(glyphs)) {
57-
res.push(`00${glyph.id.toString(16)}`.slice(-2));
58-
}
59-
60-
return res;
61-
}
62-
6353
widthOfString(string, size) {
6454
const glyphs = this.font.glyphsForString(`${string}`);
6555
const advances = this.font.advancesForGlyphs(glyphs);
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Glyph } from '@react-pdf/textkit';
2+
3+
const encodeGlyphs = (font: any, glyphs: Glyph[]) => {
4+
// Embedded font path (has font subset)
5+
if (font.subset) {
6+
const res: string[] = [];
7+
8+
for (let i = 0; i < glyphs.length; i++) {
9+
const glyph = glyphs[i];
10+
const gid = font.subset.includeGlyph(glyph.id);
11+
res.push(`0000${gid.toString(16)}`.slice(-4));
12+
13+
if (font.widths[gid] == null) {
14+
font.widths[gid] = glyph.advanceWidth * font.scale;
15+
}
16+
17+
if (font.unicode[gid] == null) {
18+
font.unicode[gid] = glyph.codePoints;
19+
}
20+
}
21+
22+
return res;
23+
}
24+
25+
// Standard font path
26+
const res: string[] = [];
27+
28+
for (const glyph of glyphs) {
29+
res.push(`00${glyph.id.toString(16)}`.slice(-2));
30+
}
31+
32+
return res;
33+
};
34+
35+
export default encodeGlyphs;

packages/render/src/primitives/renderGlyphs.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Glyph, Position } from '@react-pdf/textkit';
44
import { Context } from '../types';
5+
import encodeGlyphs from '../operations/encodeGlyphs';
56

67
const number = (n: number) => {
78
if (n > -1e21 && n < 1e21) {
@@ -13,7 +14,7 @@ const number = (n: number) => {
1314

1415
const _renderGlyphs = (
1516
ctx: Context,
16-
encoded: Glyph[],
17+
encoded: string[],
1718
positions: Position[],
1819
x: number,
1920
y: number,
@@ -122,7 +123,7 @@ const renderGlyphs = (
122123
const advanceWidthScale = 1000 / unitsPerEm;
123124

124125
// Glyph encoding and positioning
125-
const encodedGlyphs = ctx._font.encodeGlyphs(glyphs);
126+
const encodedGlyphs = encodeGlyphs(ctx._font, glyphs);
126127
const encodedPositions = positions.map((pos, i) => ({
127128
xAdvance: pos.xAdvance * scale,
128129
yAdvance: pos.yAdvance * scale,

0 commit comments

Comments
 (0)