mirror of
https://github.com/unidoc/unipdf.git
synced 2025-05-05 19:30:30 +08:00
commit
d2998c2183
@ -37,12 +37,12 @@ func (font PdfFont) String() string {
|
|||||||
|
|
||||||
// BaseFont returns the font's "BaseFont" field.
|
// BaseFont returns the font's "BaseFont" field.
|
||||||
func (font PdfFont) BaseFont() string {
|
func (font PdfFont) BaseFont() string {
|
||||||
return font.fontSkeleton.basefont
|
return font.basefont
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subtype returns the font's "Subtype" field.
|
// Subtype returns the font's "Subtype" field.
|
||||||
func (font PdfFont) Subtype() string {
|
func (font PdfFont) Subtype() string {
|
||||||
subtype := font.fontSkeleton.subtype
|
subtype := font.subtype
|
||||||
if t, ok := font.context.(*pdfFontType0); ok {
|
if t, ok := font.context.(*pdfFontType0); ok {
|
||||||
subtype = fmt.Sprintf("%s:%s", subtype, t.DescendantFont.Subtype())
|
subtype = fmt.Sprintf("%s:%s", subtype, t.DescendantFont.Subtype())
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package model
|
package model_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -6,6 +6,8 @@ import (
|
|||||||
|
|
||||||
"github.com/unidoc/unidoc/common"
|
"github.com/unidoc/unidoc/common"
|
||||||
. "github.com/unidoc/unidoc/pdf/core"
|
. "github.com/unidoc/unidoc/pdf/core"
|
||||||
|
"github.com/unidoc/unidoc/pdf/model"
|
||||||
|
"github.com/unidoc/unidoc/pdf/model/fonts"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
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 {
|
||||||
|
font, err := model.NewStandard14Font(in)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("%s: %v", in, err)
|
||||||
|
}
|
||||||
|
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 := font.GetGlyphCharMetrics("space")
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("%s: failed to get glyph metric", in)
|
||||||
|
}
|
||||||
|
if metrics.Wx != expect.Wx || metrics.Wy != expect.Wy {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
@ -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)
|
t.Errorf("objFontObj: Failed to parse dict obj. fontDict=%q err=%v", fontDict, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
font, err := NewPdfFontFromPdfObject(obj)
|
font, err := model.NewPdfFontFromPdfObject(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to parse font object. obj=%s err=%v", obj, err)
|
t.Errorf("Failed to parse font object. obj=%s err=%v", obj, err)
|
||||||
return err
|
return err
|
||||||
|
@ -53,20 +53,17 @@ endobj
|
|||||||
|
|
||||||
obj, err := parser.ParseIndirectObject()
|
obj, err := parser.ParseIndirectObject()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed to parse indirect obj (%s)", err)
|
t.Fatalf("Failed to parse indirect obj (%s)", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
stream, ok := obj.(*PdfObjectStream)
|
_, ok := obj.(*PdfObjectStream)
|
||||||
if !ok {
|
if !ok {
|
||||||
t.Errorf("Invalid object type (%q)", obj)
|
t.Fatalf("Invalid object type (%q)", obj)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun, err := newPdfFunctionFromPdfObject(obj)
|
fun, err := newPdfFunctionFromPdfObject(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed: %v", err)
|
t.Fatalf("Failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// z = sin(360*x)/2 + sin(360*y)/2
|
// z = sin(360*x)/2 + sin(360*y)/2
|
||||||
@ -81,23 +78,18 @@ endobj
|
|||||||
for _, testcase := range testcases {
|
for _, testcase := range testcases {
|
||||||
outputs, err := fun.Evaluate(testcase.Inputs)
|
outputs, err := fun.Evaluate(testcase.Inputs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Failed: %v", err)
|
t.Fatalf("Failed: %v", err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
fmt.Println(testcase)
|
fmt.Println(testcase)
|
||||||
fmt.Println(outputs)
|
fmt.Println(outputs)
|
||||||
|
|
||||||
if len(outputs) != len(testcase.Expected) {
|
if len(outputs) != len(testcase.Expected) {
|
||||||
t.Errorf("Failed, output length mismatch")
|
t.Fatalf("Failed, output length mismatch")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
for i := 0; i < len(outputs); i++ {
|
for i := 0; i < len(outputs); i++ {
|
||||||
if math.Abs(outputs[i]-testcase.Expected[i]) > 0.000001 {
|
if math.Abs(outputs[i]-testcase.Expected[i]) > 0.000001 {
|
||||||
t.Errorf("Failed, output and expected mismatch")
|
t.Fatalf("Failed, output and expected mismatch")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s", stream.Stream)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user