From b0f53294258208ff08dacdbe3bb85711e41e5240 Mon Sep 17 00:00:00 2001 From: Peter Williams Date: Mon, 24 Sep 2018 17:53:12 +1000 Subject: [PATCH] Allow TrueType font files to not have PostScript entries in their "name" table. --- pdf/model/font_composite.go | 5 +++++ pdf/model/font_test.go | 10 ++++++++++ pdf/model/fonts/ttfparser.go | 3 ++- pdf/model/testdata/font/tesseract.txt | Bin 0 -> 1908 bytes pdf/model/textencoding/truetype.go | 4 ++-- 5 files changed, 19 insertions(+), 3 deletions(-) create mode 100755 pdf/model/testdata/font/tesseract.txt diff --git a/pdf/model/font_composite.go b/pdf/model/font_composite.go index d54db909..fd6f6a4d 100644 --- a/pdf/model/font_composite.go +++ b/pdf/model/font_composite.go @@ -316,9 +316,14 @@ func (font pdfCIDFontType2) SetEncoder(encoder textencoding.TextEncoder) { // GetGlyphCharMetrics returns the character metrics for the specified glyph. A bool flag is // returned to indicate whether or not the entry was found in the glyph to charcode mapping. +// XXX:TODO(peterwilliams97): Cache enc creation and cache glyph:metrics in a map. func (font pdfCIDFontType2) GetGlyphCharMetrics(glyph string) (fonts.CharMetrics, bool) { metrics := fonts.CharMetrics{} + if font.ttfParser == nil { + return metrics, false + } + enc := textencoding.NewTrueTypeFontEncoder(font.ttfParser.Chars) // Convert the glyph to character code. diff --git a/pdf/model/font_test.go b/pdf/model/font_test.go index 8f8372b6..ec3d0be2 100644 --- a/pdf/model/font_test.go +++ b/pdf/model/font_test.go @@ -317,6 +317,16 @@ var charcodeBytesToUnicodeTest = []fontFragmentTest{ 75, 76, 77}, " *ﺏﻁﻝﺍﺔﻴﻠﻜ،ﺕﺭﺘﻌﻤﺎﺠﻲﻨﻘﺩﻬ/ﻙﻭﻕﺃﻡﻋﻓﺴ٢٠٣ﻯﻥﺒﺸﺌﺱﻷ,ﺯﺤﺄﻀـﺓﺫ.)٤(٩ل٥٧٨ﻸﻰ%١ﺇ٦ﺡﻫﻱﻅﻐﺼﻑﺨﺀﻊLM", }, + fontFragmentTest{"Tesseract", + "./testdata/font/tesseract.txt", 3, + []byte{0, 65, 0, 97, + 1, 2, 1, 65, 1, 97, + 12, 2, 12, 65, 12, 97, + 20, 65, 20, 97, 20, 255, + 42, 2, 42, 65, 42, 97, + 65, 66, 67, 255}, + "AaĂŁšంుౡᑁᑡᓿ⨂⩁⩡䅂䏿", + }, } type fontFragmentTest struct { diff --git a/pdf/model/fonts/ttfparser.go b/pdf/model/fonts/ttfparser.go index 230297af..d66d1bf8 100644 --- a/pdf/model/fonts/ttfparser.go +++ b/pdf/model/fonts/ttfparser.go @@ -566,6 +566,7 @@ func (t *ttfParser) parseCmapFormat12() error { return nil } +// ParseName parses the "name" table. func (t *ttfParser) ParseName() error { if err := t.Seek("name"); err != nil { return err @@ -596,7 +597,7 @@ func (t *ttfParser) ParseName() error { } } if t.rec.PostScriptName == "" { - return fmt.Errorf("the name PostScript was not found") + common.Log.Debug("ParseName: The name PostScript was not found.") } return nil } diff --git a/pdf/model/testdata/font/tesseract.txt b/pdf/model/testdata/font/tesseract.txt new file mode 100755 index 0000000000000000000000000000000000000000..ea37c1658fda921f53aa0896a927a1e1191561d4 GIT binary patch literal 1908 zcmbVMJ&YSg6n^&k!rdl_ixi?12)#oo4y22BeQ}b)vT}D$E*619akvx_B75txy=XnV z+1-XfTn!8jURS4kqGm|80FlqP?)| z>poZ8U8Pe$9j{&!eP?es9Y>0*H!<7LEQ(eHu9}UTO$(a}0NL$d0oiP~ZiZX!)*Eib zb%7vTLfIK7sT#I*FNDIR!!T^eT@?$ml?k#}VHV2`)%TM$9`njN0H}$&vlm5y8X^l8 zzyJte!72ra{U{A%zBoB{njg*=AIYpH0W&E=YTe?xZCgdMg1boey3+l0z%|FlNq&&3 z82^Ers+K}omBaz4a);LF)88MI7Ky0*+20?0v;5KLC9Iz@_TQf)IygYk^YD7Wzkd7G zl{3%qH>QV}|6j}>|M8pyTEgw;x#c{#Ff9#N^TMpv*W?~@Ke_9+_QBD&;?V1;lzYDJ z#wrQ-Vo&idu2ox3jk~^==91BQLpQPx5hY{&5looj&D)zD;Rd)oLyTsVyQ+unz`{4( zA?R#Kw3?BuH(o_l;bGVSM2K)QNOG@97jMs~Dyu4%zdl-QE%IoCQlC zP3%~jY^UhFy*LJ=O-T%9!QyK*ty&aDvccqg@=Ap61zXqU9Z1;$Vkyg}8(mX=e^6+$ zmm=j=$Fxr1_7kp~B|2ndBOEav4;w%9gxMy0L!^JeepTa3u$itFRaV`no}+f^2EMnh z`++ijTJ50ucleUQRCHg_v@_i&`xp7-B&%OEjhjghi&$`uq|Yqcr#a&7PPMl4%lG$) zN-NObkxIjM9`eVKU+xFv-ixoSpN0H2)-I59SqY^ z1q=9ph-o1RJ(o@x`99`jLw6KWh2~*jfXo_nd-U-1&yatF{CX57DewVd`6aQ-O1}H| z?k>IiLgTN>a}q}M!zYim{Qvab7mwt>LPoztfG1u>qqL>o$DI`ry4xJ-=N5zRBYIe@qhpUvM N#E$tuTRquM{12>4tET_} literal 0 HcmV?d00001 diff --git a/pdf/model/textencoding/truetype.go b/pdf/model/textencoding/truetype.go index 680d1bfb..4dc8753c 100644 --- a/pdf/model/textencoding/truetype.go +++ b/pdf/model/textencoding/truetype.go @@ -24,8 +24,8 @@ type TrueTypeFontEncoder struct { cmap CMap } -// NewTrueTypeFontEncoder creates a new text encoder for TTF fonts with a pre-loaded -// runeToGlyphIndexMap, that has been pre-loaded from the font file. +// NewTrueTypeFontEncoder creates a new text encoder for TTF fonts with a runeToGlyphIndexMap that +// has been preloaded from the font file. // The new instance is preloaded with a CMapIdentityH (Identity-H) CMap which maps 2-byte charcodes // to CIDs (glyph index). func NewTrueTypeFontEncoder(runeToGlyphIndexMap map[uint16]uint16) TrueTypeFontEncoder {