Skip to content

Commit

Permalink
PDF: fix bugs in CMap, see #332
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Jan 21, 2025
1 parent c15dfee commit fecd1de
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions renderers/pdf/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,45 +404,50 @@ func (w *pdfWriter) writeFont(ref pdfRef, font *canvas.Font, vertical bool) {
length++
} else {
if 1 < length {
fmt.Fprintf(&bfRange, "<%04X> <%04X> <%04X>\n", startGlyphID, startGlyphID+length-1, startUnicode)
fmt.Fprintf(&bfRange, "\n<%04X> <%04X> <%04X>", startGlyphID, startGlyphID+length-1, startUnicode)
bfRangeCount++
} else {
fmt.Fprintf(&bfChar, "<%04X> <%04X>\n", startGlyphID, startUnicode)
fmt.Fprintf(&bfChar, "\n<%04X> <%04X>", startGlyphID, startUnicode)
bfCharCount++
}
startGlyphID = uint16(subsetGlyphID + 1)
startUnicode = unicode
length = 1
}
}
if 1 < length {
fmt.Fprintf(&bfRange, "<%04X> <%04X> <%04X>\n", startGlyphID, startGlyphID+length-1, startUnicode)
fmt.Fprintf(&bfRange, "\n<%04X> <%04X> <%04X>", startGlyphID, startGlyphID+length-1, startUnicode)
bfRangeCount++
} else {
fmt.Fprintf(&bfChar, "<%04X> <%04X>\n", startGlyphID, startUnicode)
fmt.Fprintf(&bfChar, "\n<%04X> <%04X>", startGlyphID, startUnicode)
bfCharCount++
}

toUnicode := fmt.Sprintf(`/CIDInit /ProcSet findresource begin
toUnicode := bytes.Buffer{}
fmt.Fprintf(&toUnicode, `/CIDInit /ProcSet findresource begin
12 dict begin
begincmap
/CIDSystemInfo
<< /Registry (Adobe)
/Ordering (UCS)
/Supplement 0
>> def
/CIDSystemInfo <</Registry(Adobe)/Ordering(UCS)/Supplement 0>> def
/CMapName /Adobe-Identity-UCS def
/CMapType 2 def
1 begincodespacerange
<0000> <FFFF>
endcodespacerange
%d beginbfrange
%sendbfrange
%d beginbfchar
%sendbfchar
<0000> <FFFF> endcodespacerange`)
if 0 < bfRangeCount {
fmt.Fprintf(&toUnicode, `
%d beginbfrange%s endbfrange`, bfRangeCount, bfRange.String())
}
if 0 < bfCharCount {
fmt.Fprintf(&toUnicode, `
%d beginbfchar%s endbfchar`, bfCharCount, bfChar.String())
}
fmt.Fprintf(&toUnicode, `
endcmap
CMapName currentdict /CMap defineresource pop
end
end`, bfRangeCount, bfRange.String(), bfCharCount, bfChar.String())
end`)
toUnicodeStream := pdfStream{
dict: pdfDict{},
stream: []byte(toUnicode),
stream: toUnicode.Bytes(),
}
if w.compress {
toUnicodeStream.dict["Filter"] = pdfFilterFlate
Expand Down

0 comments on commit fecd1de

Please sign in to comment.