diff --git a/pdf/core/utils.go b/pdf/core/utils.go index c1fe2692..e6b16523 100644 --- a/pdf/core/utils.go +++ b/pdf/core/utils.go @@ -312,7 +312,7 @@ func FlattenObject(obj PdfObject) PdfObject { func flattenObject(obj PdfObject, depth int) PdfObject { if depth > TraceMaxDepth { common.Log.Error("Trace depth level beyond %d - error!", TraceMaxDepth) - return nil + return MakeNull() } switch t := obj.(type) { case *PdfIndirectObject: diff --git a/pdf/model/font.go b/pdf/model/font.go index 73af6f72..d67131f7 100644 --- a/pdf/model/font.go +++ b/pdf/model/font.go @@ -284,7 +284,7 @@ type fontSkeleton struct { // NOTE: The returned dict's "Subtype" field is set to `subtype` if `skel` doesn't have a subtype. func (skel fontSkeleton) toDict(subtype string) *PdfObjectDictionary { - if subtype != "" && skel.subtype != "" { + if subtype != "" && skel.subtype != "" && subtype != skel.subtype { common.Log.Debug("ERROR: toDict. Overriding subtype to %#q %s", subtype, skel) } else if subtype == "" && skel.subtype == "" { common.Log.Debug("ERROR: toDict no subtype. font=%s", skel) @@ -312,7 +312,7 @@ func (skel fontSkeleton) String() string { if skel.fontDescriptor != nil { descriptor = "(has descriptor)" } - return fmt.Sprintf("%#q %#q %s", skel.subtype, skel.basefont, descriptor) + return fmt.Sprintf("FONT{%#q %#q %s}", skel.subtype, skel.basefont, descriptor) } // isCIDFont returns true if `skel` is a CID font. diff --git a/pdf/model/font_composite.go b/pdf/model/font_composite.go index 232bedb9..1637b5c7 100644 --- a/pdf/model/font_composite.go +++ b/pdf/model/font_composite.go @@ -122,8 +122,8 @@ func (font *pdfFontType0) ToPdfObject() PdfObject { d := font.skeleton.toDict("Type0") font.container.PdfObject = d - if font.Encoding != nil { - d.Set("Encoding", font.Encoding) + if font.encoder != nil { + d.Set("Encoding", font.encoder.ToPdfObject()) } if font.DescendantFont != nil { // Shall be 1 element array. diff --git a/pdf/model/font_test.go b/pdf/model/font_test.go index fbca0722..c896167f 100644 --- a/pdf/model/font_test.go +++ b/pdf/model/font_test.go @@ -56,6 +56,38 @@ var simpleFontDicts = []string{ >>`, } +var compositeFontDicts = []string{ + `<< /Type /Font + /Subtype /Type0 + /Encoding /Identity-H + /DescendantFonts [<< + /Type /Font + /Subtype /CIDFontType2 + /BaseFont /FLDOLC+PingFangSC-Regular + /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> + /W [ ] + /DW 1000 + /FontDescriptor << + /Type /FontDescriptor + /FontName /FLDOLC+PingFangSC-Regular + /Flags 4 + /FontBBox [-123 -263 1177 1003] + /ItalicAngle 0 + /Ascent 972 + /Descent -232 + /CapHeight 864 + /StemV 70 + /XHeight 648 + /StemH 64 + /AvgWidth 1000 + /MaxWidth 1300 + % /FontFile3 182 0 R + >> + >>] + /BaseFont /FLDOLC+PingFangSC-Regular + >>`, +} + // TestSimpleFonts checks that we correctly recreate simple fonts that we parse. func TestSimpleFonts(t *testing.T) { for _, d := range simpleFontDicts { @@ -63,6 +95,13 @@ func TestSimpleFonts(t *testing.T) { } } +// TestCompositeFonts checks that we correctly recreate composite fonts that we parse. +func TestCompositeFonts(t *testing.T) { + for _, d := range compositeFontDicts { + objFontObj(t, d) + } +} + // objFontObj parses `fontDict` to a make a Font, creates a PDF object from the Font and checks that // the new PDF object is the same as the input object func objFontObj(t *testing.T, fontDict string) error { diff --git a/pdf/model/textencoding/identity.go b/pdf/model/textencoding/identity.go index 3c8d6041..786be712 100644 --- a/pdf/model/textencoding/identity.go +++ b/pdf/model/textencoding/identity.go @@ -114,5 +114,8 @@ func (enc IdentityEncoder) GlyphToRune(glyph string) (rune, bool) { // ToPdfObject returns a nil as it is not truly a PDF object and should not be attempted to store in file. func (enc IdentityEncoder) ToPdfObject() PdfObject { - return nil + if enc.baseName != "" { + return MakeName(enc.baseName) + } + return MakeNull() }