// Test restricting a "preferred" font's usage to a particular range. // If two fonts each implement both of two ranges, but one font is superior for // one range and the other font is superior for the other range, the first font // can be restricted to only specified ranges. func TestNewRichText_ChineseAndEnglish_ranges(t *testing.T) { st := SuperTest{t} fc, err := ttf_fonts.New("/Library/Fonts/*.ttf") if err != nil { t.Fatal(err) } cjk, err := ttf.NewCodepointRangeSet("CJK Unified Ideographs") if err != nil { t.Fatal(err) } stFangsong, err := font.New("STFangsong", options.Options{"ranges": cjk}, font.FontSources{fc}) if err != nil { t.Fatal(err) } arial, err := font.New("Arial", options.Options{}, font.FontSources{fc}) if err != nil { t.Fatal(err) } fonts := []*font.Font{stFangsong, arial} rt, err := New("所有测abc", fonts, 10, options.Options{}) if err != nil { t.Fatal(err) } st.Must(len(rt.pieces) == 2, "Expecting 2 pieces.") st.Equal("所有测", rt.pieces[0].Text) st.Equal(10.0, rt.pieces[0].FontSize) st.Equal("abc", rt.pieces[1].Text) st.Equal(10.0, rt.pieces[1].FontSize) st.Equal(fonts[0], rt.pieces[0].Font, "Should be tagged with STFangsong font.") st.Equal(fonts[1], rt.pieces[1].Font, "Should be tagged with Arial font.") }
func main() { f, err := os.Create(name) if err != nil { panic(err) } doc := pdf.NewDocWriter() ttfc, err := ttf_fonts.New("/Library/Fonts/*.ttf") if err != nil { panic(err) } doc.AddFontSource(ttfc) doc.SetUnits("in") doc.NewPage() doc.MoveTo(1, 1) doc.SetFont("Courier New", 12, options.Options{}) doc.SetUnderline(true) doc.Print("Rich Text\n\n") doc.SetUnderline(false) doc.SetLineSpacing(1.2) lines := []*rich_text.RichText{ makeRtLine(doc, line1), makeRtLine(doc, line2), makeRtLine(doc, line3), makeRtLine(doc, line4), makeRtLine(doc, line5), makeRtLine(doc, line6), } doc.PrintParagraph(lines) doc.WriteTo(f) f.Close() exec.Command("open", name).Start() }
func TestPageWriter_flushText(t *testing.T) { fc, err := ttf_fonts.New("/Library/Fonts/*.ttf") if err != nil { t.Fatal(err) } dw := NewDocWriter() dw.AddFontSource(fc) pw := dw.NewPage() pw.SetFont("Arial", 12, options.Options{}) pw.Print("Hello") pw.Print(", World!") pw.flushText() expectS(t, "BT\n/F0 12 Tf\n(Hello, World!) Tj\n", pw.stream.String()) }
func NewDocWriter() *DocWriter { dw := pdf.NewDocWriter() ttFonts, err := ttf_fonts.New("/Library/Fonts/*.ttf") if err != nil { panic(err) } dw.AddFontSource(ttFonts) afmFonts, err := afm_fonts.New("../afm/data/fonts/*.afm") if err != nil { panic(err) } dw.AddFontSource(afmFonts) return &DocWriter{dw} }
func TestPageWriter_Write(t *testing.T) { dw := NewDocWriter() pw := newPageWriter(dw, options.Options{}) fc, err := ttf_fonts.New("/Library/Fonts/*.ttf") if err != nil { t.Fatal(err) } dw.AddFontSource(fc) pw.SetFont("Arial", 12, options.Options{}) check(t, pw.line == nil, "line should start out nil") fmt.Fprint(pw, "Hello") check(t, pw.line != nil, "line should no longer be nil") expectS(t, "Hello", pw.line.String()) fmt.Fprint(pw, ", World!") expectS(t, "Hello, World!", pw.line.String()) }
func TestPageWriter_FontStyle(t *testing.T) { dw := NewDocWriter() ttfc, err := ttf_fonts.New("/Library/Fonts/*.ttf") if err != nil { t.Fatal(err) } dw.AddFontSource(ttfc) afmfc, err := afm_fonts.New("../afm/data/fonts/*.afm") if err != nil { t.Fatal(err) } dw.AddFontSource(afmfc) pw := dw.NewPage() check(t, pw.Fonts() == nil, "Page font list should be empty by default") check(t, pw.FontStyle() == "", "Should default to empty string") prev, err := pw.SetFontStyle("Italic") expectS(t, "No current font to apply style Italic to.", err.Error()) check(t, prev == "", "Should return empty string when no font is set") _, err = pw.AddFont("Arial", options.Options{}) check(t, err == nil, "Failed to add Arial") _, err = pw.AddFont("Helvetica", options.Options{}) check(t, err == nil, "Failed to add Helvetica") fonts := pw.Fonts() checkFatal(t, len(fonts) == 2, "length of fonts should be 2") expectS(t, "", fonts[0].Style()) expectS(t, "", fonts[1].Style()) prev, err = pw.SetFontStyle("Italic") check(t, err == nil, "Error setting style to Italic.") check(t, pw.FontStyle() == "Italic", "FontStyle should now be Italic") fonts = pw.Fonts() checkFatal(t, len(fonts) == 2, "length of fonts should be 2") expectS(t, "Italic", fonts[0].Style()) expectS(t, "Italic", fonts[1].Style()) }
func TestPageWriter_SetFont(t *testing.T) { dw := NewDocWriter() fc, err := ttf_fonts.New("/Library/Fonts/*.ttf") if err != nil { t.Fatal(err) } dw.AddFontSource(fc) pw := dw.NewPage() check(t, pw.Fonts() == nil, "Page font list should be empty by default") fonts, _ := pw.SetFont("Arial", 12, options.Options{"color": green}) checkFatal(t, len(fonts) == 1, "length of fonts should be 1") expectS(t, "Arial", fonts[0].Family()) check(t, pw.FontColor() == green, "FontColor should now be green") expectF(t, 12, pw.FontSize()) expectF(t, 1.0, fonts[0].RelativeSize) check(t, fonts[0] == pw.Fonts()[0], "SetFont should return new font list") check(t, fonts[0] == dw.Fonts()[0], "SetFont changes to font list should be global") }