Skip to content

Commit b457a0c

Browse files
diegomurachihyux
andauthored
fix: copy-paste for registered font (#2488)
* fix: copy-paste for registered font * fix: copy paste wrong chars in browser preview * chore: add changeset --------- Co-authored-by: Alison <chihyu@dentall.io>
1 parent e5c8fde commit b457a0c

2 files changed

Lines changed: 46 additions & 42 deletions

File tree

.changeset/moody-jars-relax.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@react-pdf/pdfkit': patch
3+
---
4+
5+
fix: copy-paste for registered font

packages/pdfkit/src/font/embedded.js

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/* eslint-disable no-cond-assign */
2-
// eslint-disable-next-line no-unused-vars
3-
import PDFObject from '../object';
4-
51
const toHex = function(...codePoints) {
62
const codes = Array.from(codePoints).map(code =>
73
`0000${code.toString(16)}`.slice(-4)
@@ -113,9 +109,7 @@ const createEmbeddedFont = PDFFont =>
113109
this.widths[gid] = glyph.advanceWidth * this.scale;
114110
}
115111
if (this.unicode[gid] == null) {
116-
this.unicode[gid] = this.font._cmapProcessor.codePointsForGlyph(
117-
glyph.id
118-
);
112+
this.unicode[gid] = glyph.codePoints;
119113
}
120114
}
121115

@@ -133,9 +127,7 @@ const createEmbeddedFont = PDFFont =>
133127
this.widths[gid] = glyph.advanceWidth * this.scale;
134128
}
135129
if (this.unicode[gid] == null) {
136-
this.unicode[gid] = this.font._cmapProcessor.codePointsForGlyph(
137-
glyph.id
138-
);
130+
this.unicode[gid] = glyph.codePoints;
139131
}
140132
}
141133

@@ -210,9 +202,9 @@ const createEmbeddedFont = PDFFont =>
210202

211203
descriptor.end();
212204

213-
const descendantFont = this.document.ref({
205+
const descendantFontData = {
214206
Type: 'Font',
215-
Subtype: isCFF ? 'CIDFontType0' : 'CIDFontType2',
207+
Subtype: 'CIDFontType0',
216208
BaseFont: name,
217209
CIDSystemInfo: {
218210
Registry: new String('Adobe'),
@@ -221,7 +213,14 @@ const createEmbeddedFont = PDFFont =>
221213
},
222214
FontDescriptor: descriptor,
223215
W: [0, this.widths]
224-
});
216+
};
217+
218+
if (!isCFF) {
219+
descendantFontData.Subtype = 'CIDFontType2';
220+
descendantFontData.CIDToGIDMap = 'Identity';
221+
}
222+
223+
const descendantFont = this.document.ref(descendantFontData);
225224

226225
descendantFont.end();
227226

@@ -242,45 +241,45 @@ const createEmbeddedFont = PDFFont =>
242241
// unicode characters represented by each glyph.
243242
toUnicodeCmap() {
244243
const cmap = this.document.ref();
244+
let entries = [];
245+
let unicodeMap =
246+
'/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n/CIDSystemInfo <<\n /Registry (Adobe)\n /Ordering (UCS)\n /Supplement 0\n>> def\n/CMapName /Adobe-Identity-UCS def\n/CMapType 2 def\n1 begincodespacerange\n<0000><ffff>\nendcodespacerange';
245247

246-
const entries = [];
247-
for (let codePoints of Array.from(this.unicode)) {
248+
for (let [index, codePoints] of this.unicode.entries()) {
248249
const encoded = [];
249-
for (let value of Array.from(codePoints)) {
250+
if (entries.length >= 100) {
251+
unicodeMap +=
252+
'\n' +
253+
entries.length +
254+
' beginbfchar\n' +
255+
entries.join('\n') +
256+
'\nendbfchar';
257+
entries = [];
258+
}
259+
// encode codePoints to utf16
260+
for (let value of codePoints) {
250261
if (value > 0xffff) {
251262
value -= 0x10000;
252263
encoded.push(toHex(((value >>> 10) & 0x3ff) | 0xd800));
253264
value = 0xdc00 | (value & 0x3ff);
254265
}
255-
256266
encoded.push(toHex(value));
257-
258-
entries.push(`<${encoded.join(' ')}>`);
259267
}
260-
}
261268

262-
cmap.end(`\
263-
/CIDInit /ProcSet findresource begin
264-
12 dict begin
265-
begincmap
266-
/CIDSystemInfo <<
267-
/Registry (Adobe)
268-
/Ordering (UCS)
269-
/Supplement 0
270-
>> def
271-
/CMapName /Adobe-Identity-UCS def
272-
/CMapType 2 def
273-
1 begincodespacerange
274-
<0000><ffff>
275-
endcodespacerange
276-
1 beginbfrange
277-
<0000> <${toHex(entries.length - 1)}> [${entries.join(' ')}]
278-
endbfrange
279-
endcmap
280-
CMapName currentdict /CMap defineresource pop
281-
end
282-
end\
283-
`);
269+
// eslint-disable-next-line no-useless-concat
270+
entries.push('<' + toHex(index) + '>' + '<' + encoded.join(' ') + '>');
271+
}
272+
if (entries.length) {
273+
unicodeMap +=
274+
'\n' +
275+
entries.length +
276+
' beginbfchar\n' +
277+
entries.join('\n') +
278+
'\nendbfchar\n';
279+
}
280+
unicodeMap +=
281+
'endcmap\nCMapName currentdict /CMap defineresource pop\nend\nend';
282+
cmap.end(unicodeMap);
284283

285284
return cmap;
286285
}

0 commit comments

Comments
 (0)