Got Type0 font -> obj -> font test passing

This commit is contained in:
Peter Williams 2018-06-29 18:09:44 +10:00
parent d0448a9662
commit c6f7cf9eef
5 changed files with 48 additions and 6 deletions

View File

@ -312,7 +312,7 @@ func FlattenObject(obj PdfObject) PdfObject {
func flattenObject(obj PdfObject, depth int) PdfObject { func flattenObject(obj PdfObject, depth int) PdfObject {
if depth > TraceMaxDepth { if depth > TraceMaxDepth {
common.Log.Error("Trace depth level beyond %d - error!", TraceMaxDepth) common.Log.Error("Trace depth level beyond %d - error!", TraceMaxDepth)
return nil return MakeNull()
} }
switch t := obj.(type) { switch t := obj.(type) {
case *PdfIndirectObject: case *PdfIndirectObject:

View File

@ -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. // 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 { 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) common.Log.Debug("ERROR: toDict. Overriding subtype to %#q %s", subtype, skel)
} else if subtype == "" && skel.subtype == "" { } else if subtype == "" && skel.subtype == "" {
common.Log.Debug("ERROR: toDict no subtype. font=%s", skel) common.Log.Debug("ERROR: toDict no subtype. font=%s", skel)
@ -312,7 +312,7 @@ func (skel fontSkeleton) String() string {
if skel.fontDescriptor != nil { if skel.fontDescriptor != nil {
descriptor = "(has descriptor)" 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. // isCIDFont returns true if `skel` is a CID font.

View File

@ -122,8 +122,8 @@ func (font *pdfFontType0) ToPdfObject() PdfObject {
d := font.skeleton.toDict("Type0") d := font.skeleton.toDict("Type0")
font.container.PdfObject = d font.container.PdfObject = d
if font.Encoding != nil { if font.encoder != nil {
d.Set("Encoding", font.Encoding) d.Set("Encoding", font.encoder.ToPdfObject())
} }
if font.DescendantFont != nil { if font.DescendantFont != nil {
// Shall be 1 element array. // Shall be 1 element array.

View File

@ -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. // TestSimpleFonts checks that we correctly recreate simple fonts that we parse.
func TestSimpleFonts(t *testing.T) { func TestSimpleFonts(t *testing.T) {
for _, d := range simpleFontDicts { 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 // 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 // the new PDF object is the same as the input object
func objFontObj(t *testing.T, fontDict string) error { func objFontObj(t *testing.T, fontDict string) error {

View File

@ -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. // 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 { func (enc IdentityEncoder) ToPdfObject() PdfObject {
return nil if enc.baseName != "" {
return MakeName(enc.baseName)
}
return MakeNull()
} }