Skip to content

Commit fecd1de

Browse files
committed
PDF: fix bugs in CMap, see #332
1 parent c15dfee commit fecd1de

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

renderers/pdf/writer.go

+23-18
Original file line numberDiff line numberDiff line change
@@ -404,45 +404,50 @@ func (w *pdfWriter) writeFont(ref pdfRef, font *canvas.Font, vertical bool) {
404404
length++
405405
} else {
406406
if 1 < length {
407-
fmt.Fprintf(&bfRange, "<%04X> <%04X> <%04X>\n", startGlyphID, startGlyphID+length-1, startUnicode)
407+
fmt.Fprintf(&bfRange, "\n<%04X> <%04X> <%04X>", startGlyphID, startGlyphID+length-1, startUnicode)
408+
bfRangeCount++
408409
} else {
409-
fmt.Fprintf(&bfChar, "<%04X> <%04X>\n", startGlyphID, startUnicode)
410+
fmt.Fprintf(&bfChar, "\n<%04X> <%04X>", startGlyphID, startUnicode)
411+
bfCharCount++
410412
}
411413
startGlyphID = uint16(subsetGlyphID + 1)
412414
startUnicode = unicode
413415
length = 1
414416
}
415417
}
416418
if 1 < length {
417-
fmt.Fprintf(&bfRange, "<%04X> <%04X> <%04X>\n", startGlyphID, startGlyphID+length-1, startUnicode)
419+
fmt.Fprintf(&bfRange, "\n<%04X> <%04X> <%04X>", startGlyphID, startGlyphID+length-1, startUnicode)
420+
bfRangeCount++
418421
} else {
419-
fmt.Fprintf(&bfChar, "<%04X> <%04X>\n", startGlyphID, startUnicode)
422+
fmt.Fprintf(&bfChar, "\n<%04X> <%04X>", startGlyphID, startUnicode)
423+
bfCharCount++
420424
}
421425

422-
toUnicode := fmt.Sprintf(`/CIDInit /ProcSet findresource begin
426+
toUnicode := bytes.Buffer{}
427+
fmt.Fprintf(&toUnicode, `/CIDInit /ProcSet findresource begin
423428
12 dict begin
424429
begincmap
425-
/CIDSystemInfo
426-
<< /Registry (Adobe)
427-
/Ordering (UCS)
428-
/Supplement 0
429-
>> def
430+
/CIDSystemInfo <</Registry(Adobe)/Ordering(UCS)/Supplement 0>> def
430431
/CMapName /Adobe-Identity-UCS def
431432
/CMapType 2 def
432433
1 begincodespacerange
433-
<0000> <FFFF>
434-
endcodespacerange
435-
%d beginbfrange
436-
%sendbfrange
437-
%d beginbfchar
438-
%sendbfchar
434+
<0000> <FFFF> endcodespacerange`)
435+
if 0 < bfRangeCount {
436+
fmt.Fprintf(&toUnicode, `
437+
%d beginbfrange%s endbfrange`, bfRangeCount, bfRange.String())
438+
}
439+
if 0 < bfCharCount {
440+
fmt.Fprintf(&toUnicode, `
441+
%d beginbfchar%s endbfchar`, bfCharCount, bfChar.String())
442+
}
443+
fmt.Fprintf(&toUnicode, `
439444
endcmap
440445
CMapName currentdict /CMap defineresource pop
441446
end
442-
end`, bfRangeCount, bfRange.String(), bfCharCount, bfChar.String())
447+
end`)
443448
toUnicodeStream := pdfStream{
444449
dict: pdfDict{},
445-
stream: []byte(toUnicode),
450+
stream: toUnicode.Bytes(),
446451
}
447452
if w.compress {
448453
toUnicodeStream.dict["Filter"] = pdfFilterFlate

0 commit comments

Comments
 (0)