From 1bfc2cc31bf13780d2f4c4acef7a77fdf11d8c89 Mon Sep 17 00:00:00 2001 From: Hiroshi Muramatsu Date: Mon, 9 Jul 2018 18:00:10 +1000 Subject: [PATCH 1/3] Use embedded field directly --- pdf/model/font.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pdf/model/font.go b/pdf/model/font.go index 514f0134..6efa7374 100644 --- a/pdf/model/font.go +++ b/pdf/model/font.go @@ -37,12 +37,12 @@ func (font PdfFont) String() string { // BaseFont returns the font's "BaseFont" field. func (font PdfFont) BaseFont() string { - return font.fontSkeleton.basefont + return font.basefont } // Subtype returns the font's "Subtype" field. func (font PdfFont) Subtype() string { - subtype := font.fontSkeleton.subtype + subtype := font.subtype if t, ok := font.context.(*pdfFontType0); ok { subtype = fmt.Sprintf("%s:%s", subtype, t.DescendantFont.Subtype()) } From c919f47730145d980e76e70b9101440b9586ce14 Mon Sep 17 00:00:00 2001 From: Hiroshi Muramatsu Date: Mon, 9 Jul 2018 18:01:22 +1000 Subject: [PATCH 2/3] Add basic test cases --- pdf/model/font_test.go | 40 +++++++++++++++++++++++++++++++++++-- pdf/model/functions_test.go | 22 +++++++------------- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/pdf/model/font_test.go b/pdf/model/font_test.go index c896167f..380a9149 100644 --- a/pdf/model/font_test.go +++ b/pdf/model/font_test.go @@ -1,4 +1,4 @@ -package model +package model_test import ( "errors" @@ -6,6 +6,8 @@ import ( "github.com/unidoc/unidoc/common" . "github.com/unidoc/unidoc/pdf/core" + "github.com/unidoc/unidoc/pdf/model" + "github.com/unidoc/unidoc/pdf/model/fonts" ) func init() { @@ -88,6 +90,40 @@ var compositeFontDicts = []string{ >>`, } +func TestNewstandard14Font(t *testing.T) { + type expect struct { + subtype string + basefont string + fonts.CharMetrics + } + tests := map[string]expect{ + "Courier": expect{ + subtype: "Type1", + basefont: "Courier", + CharMetrics: fonts.CharMetrics{Wx: 600, Wy: 0}}, + } + + for in, expect := range tests { + result, err := model.NewStandard14Font(in) + if err != nil { + t.Errorf("%v: %s", err, in) + } + if result.Subtype() != expect.subtype || result.BaseFont() != expect.basefont { + t.Errorf("Expected BaseFont=%s SubType=%s for %s, but got BaseFont=%s SubType=%s", + expect.basefont, expect.subtype, in, result.BaseFont(), result.Subtype()) + } + + metrics, ok := result.GetGlyphCharMetrics("space") + if !ok { + t.Errorf(`Failed to get glyph metrics for "space" of %s`, in) + } + if metrics.Wx != expect.Wx || metrics.Wy != expect.Wy { + t.Errorf("Expected glyph metrics for %s is Wx=%f Wy=%f, but got Wx=%f Wy=%f", + in, expect.Wx, expect.Wy, metrics.Wx, metrics.Wy) + } + } +} + // TestSimpleFonts checks that we correctly recreate simple fonts that we parse. func TestSimpleFonts(t *testing.T) { for _, d := range simpleFontDicts { @@ -112,7 +148,7 @@ func objFontObj(t *testing.T, fontDict string) error { t.Errorf("objFontObj: Failed to parse dict obj. fontDict=%q err=%v", fontDict, err) return err } - font, err := NewPdfFontFromPdfObject(obj) + font, err := model.NewPdfFontFromPdfObject(obj) if err != nil { t.Errorf("Failed to parse font object. obj=%s err=%v", obj, err) return err diff --git a/pdf/model/functions_test.go b/pdf/model/functions_test.go index 73b4a60c..a08b5d85 100644 --- a/pdf/model/functions_test.go +++ b/pdf/model/functions_test.go @@ -53,20 +53,17 @@ endobj obj, err := parser.ParseIndirectObject() if err != nil { - t.Errorf("Failed to parse indirect obj (%s)", err) - return + t.Fatalf("Failed to parse indirect obj (%s)", err) } - stream, ok := obj.(*PdfObjectStream) + _, ok := obj.(*PdfObjectStream) if !ok { - t.Errorf("Invalid object type (%q)", obj) - return + t.Fatalf("Invalid object type (%q)", obj) } fun, err := newPdfFunctionFromPdfObject(obj) if err != nil { - t.Errorf("Failed: %v", err) - return + t.Fatalf("Failed: %v", err) } // z = sin(360*x)/2 + sin(360*y)/2 @@ -81,23 +78,18 @@ endobj for _, testcase := range testcases { outputs, err := fun.Evaluate(testcase.Inputs) if err != nil { - t.Errorf("Failed: %v", err) - return + t.Fatalf("Failed: %v", err) } fmt.Println(testcase) fmt.Println(outputs) if len(outputs) != len(testcase.Expected) { - t.Errorf("Failed, output length mismatch") - return + t.Fatalf("Failed, output length mismatch") } for i := 0; i < len(outputs); i++ { if math.Abs(outputs[i]-testcase.Expected[i]) > 0.000001 { - t.Errorf("Failed, output and expected mismatch") - return + t.Fatalf("Failed, output and expected mismatch") } } } - - fmt.Printf("%s", stream.Stream) } From 18481ea7e8ea212620f484cc2f1871cc8c0dce7a Mon Sep 17 00:00:00 2001 From: Hiroshi Muramatsu Date: Mon, 9 Jul 2018 18:10:38 +1000 Subject: [PATCH 3/3] Modify log messages --- pdf/model/font_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pdf/model/font_test.go b/pdf/model/font_test.go index 380a9149..27d3eb1a 100644 --- a/pdf/model/font_test.go +++ b/pdf/model/font_test.go @@ -90,7 +90,7 @@ var compositeFontDicts = []string{ >>`, } -func TestNewstandard14Font(t *testing.T) { +func TestNewStandard14Font(t *testing.T) { type expect struct { subtype string basefont string @@ -104,21 +104,21 @@ func TestNewstandard14Font(t *testing.T) { } for in, expect := range tests { - result, err := model.NewStandard14Font(in) + font, err := model.NewStandard14Font(in) if err != nil { - t.Errorf("%v: %s", err, in) + t.Fatalf("%s: %v", in, err) } - if result.Subtype() != expect.subtype || result.BaseFont() != expect.basefont { - t.Errorf("Expected BaseFont=%s SubType=%s for %s, but got BaseFont=%s SubType=%s", - expect.basefont, expect.subtype, in, result.BaseFont(), result.Subtype()) + if font.Subtype() != expect.subtype || font.BaseFont() != expect.basefont { + t.Fatalf("%s: expected BaseFont=%s SubType=%s, but got BaseFont=%s SubType=%s", + in, expect.basefont, expect.subtype, font.BaseFont(), font.Subtype()) } - metrics, ok := result.GetGlyphCharMetrics("space") + metrics, ok := font.GetGlyphCharMetrics("space") if !ok { - t.Errorf(`Failed to get glyph metrics for "space" of %s`, in) + t.Fatalf("%s: failed to get glyph metric", in) } if metrics.Wx != expect.Wx || metrics.Wy != expect.Wy { - t.Errorf("Expected glyph metrics for %s is Wx=%f Wy=%f, but got Wx=%f Wy=%f", + t.Errorf("%s: expected glyph metrics is Wx=%f Wy=%f, but got Wx=%f Wy=%f", in, expect.Wx, expect.Wy, metrics.Wx, metrics.Wy) } }