Ejemplo n.º 1
0
func helveticaText(s string) *RichText {
	rt, err := New(s, afm_fonts.Families("Helvetica"), 10, options.Options{})
	if err != nil {
		panic(err)
	}
	return rt
}
Ejemplo n.º 2
0
func TestRichText_InsertStringAtOffsets(t *testing.T) {
	st := SuperTest{t}
	afmFonts := afm_fonts.Families("Helvetica")
	ttfFonts := ttf_fonts.Families("Arial", "STFangsong")
	fonts := append(afmFonts, ttfFonts...)
	text := "Automatic "
	text1 := "hyphenation "
	text2 := "aids word wrapping."
	rt, err := New(text, fonts, 10, options.Options{})
	if err != nil {
		t.Fatal(err)
	}
	rt, err = rt.Add(text1, fonts, 10, options.Options{})
	if err != nil {
		t.Fatal(err)
	}
	rt, err = rt.Add(text2, fonts, 10, options.Options{})
	if err != nil {
		t.Fatal(err)
	}

	offsets := []int{4, 16, 36}
	chars := rt.Chars()
	rt0 := rt.InsertStringAtOffsets("-", offsets)
	st.Equal("Auto-matic hyphen-ation aids word wrap-ping.", rt0.String())
	st.Equal(chars+len(offsets), rt0.Chars(), "New text should be larger.")
}
Ejemplo n.º 3
0
func TestNewRichText_EnglishRussianAndChineseLanguages(t *testing.T) {
	st := SuperTest{t}
	afmFonts := afm_fonts.Families("Helvetica")
	ttfFonts := ttf_fonts.Families("Arial", "STFangsong")
	fonts := append(afmFonts, ttfFonts...)
	rt, err := New(englishRussianChinese, fonts, 10, options.Options{})
	if err != nil {
		t.Fatal(err)
	}
	st.Must(len(rt.pieces) == 5)
	st.Equal("Here is some Russian, ", rt.pieces[0].Text)
	st.Equal(10.0, rt.pieces[0].FontSize)
	st.Equal("Неприкосновенность", rt.pieces[1].Text)
	st.Equal(10.0, rt.pieces[1].FontSize)
	st.Equal(", and some Chinese, ", rt.pieces[2].Text)
	st.Equal(10.0, rt.pieces[2].FontSize)
	st.Equal("表明你已明确同意你的回答接受评估", rt.pieces[3].Text)
	st.Equal(10.0, rt.pieces[3].FontSize)
	st.Equal(".", rt.pieces[4].Text)
	st.Equal(10.0, rt.pieces[4].FontSize)
	st.Equal(fonts[0], rt.pieces[0].Font, "Should be tagged with Helvetica font.")
	st.Equal(fonts[1], rt.pieces[1].Font, "Should be tagged with Arial font.")
	st.Equal(fonts[0], rt.pieces[2].Font, "Should be tagged with Helvetica font.")
	st.Equal(fonts[2], rt.pieces[3].Font, "Should be tagged with STFangsong font.")
	st.Equal(fonts[0], rt.pieces[4].Font, "Should be tagged with Helvetica font.")
}
Ejemplo n.º 4
0
func mixedText() *RichText {
	afmFonts := afm_fonts.Families("Helvetica")
	ttfFonts := ttf_fonts.Families("Arial", "STFangsong")
	fonts := append(afmFonts, ttfFonts...)
	rt, err := New(englishRussianChinese, fonts, 10, options.Options{})
	if err != nil {
		panic(err)
	}
	return rt
}
Ejemplo n.º 5
0
func TestRichText_Zapf(t *testing.T) {
	st := SuperTest{t}
	fonts := afm_fonts.Families("ZapfDingbats")
	st.Must(len(fonts) == 1, "ZapfDingbats must be available.")
	st.Must(fonts[0].HasRune('N'), "Font must have rune.")
	rt, err := New("N", fonts, 10, options.Options{})
	if err != nil {
		t.Fatal(err)
	}
	st.Equal("N", rt.Text)
	st.Equal(10.0, rt.FontSize)
	st.Equal(fonts[0], rt.Font, "Should be tagged with ZapfDingbats font.")
}
Ejemplo n.º 6
0
// 14,930 ns go1.1.2
// 14,685 ns go1.2.1
// 10,873 ns go1.4.2
//  5,664 ns go1.7.3 mbp
func BenchmarkNewRichText(b *testing.B) {
	b.StopTimer()
	afmFonts := afm_fonts.Families("Helvetica")
	ttfFonts := ttf_fonts.Families("Arial", "STFangsong")
	fonts := append(afmFonts, ttfFonts...)
	b.StartTimer()

	for i := 0; i < b.N; i++ {
		_, err := New(englishRussianChinese, fonts, 10, options.Options{})
		if err != nil {
			b.Fatal(err)
		}
	}
}
Ejemplo n.º 7
0
func TestRichText_Merge(t *testing.T) {
	st := SuperTest{t}
	afmFonts := afm_fonts.Families("Helvetica")
	ttfFonts := ttf_fonts.Families("Arial", "STFangsong")
	fonts := append(afmFonts, ttfFonts...)
	text := "Here is some "
	text1 := "Russian, Неприкосновенность, "
	text2 := "and some Chinese, 表明你已明确同意你的回答接受评估."
	original, err := New(text, fonts, 10, options.Options{})
	if err != nil {
		t.Fatal(err)
	}
	original, err = original.Add(text1, fonts, 10, options.Options{})
	if err != nil {
		t.Fatal(err)
	}
	original, err = original.Add(text2, fonts, 10, options.Options{})
	if err != nil {
		t.Fatal(err)
	}

	// fmt.Println("Original pieces: ", original.Count())
	// i := 0
	// original.VisitAll(func(p *RichText) {
	// 	i++
	// 	fmt.Println(i, p.Text, len(p.Pieces))
	// })

	// Node
	//     Leaf: Here is some  0
	//     Node
	// 	       Leaf: Russian,
	//         Leaf: Неприкосновенность
	//         Leaf: ,
	//     Node
	//         Leaf: and some Chinese,
	//         Leaf: 表明你已明确同意你的回答接受评估
	//         Leaf: .

	piece0 := *original.pieces[0]
	merged := original.Merge()

	// fmt.Println("Merged pieces: ", merged.Count())
	// i = 0
	// merged.VisitAll(func(p *RichText) {
	// 	i++
	// 	fmt.Println(i, p.Text, len(p.pieces))
	// })

	// Node
	// Leaf: Here is some Russian,
	// Leaf: Неприкосновенность
	// Leaf: , and some Chinese,
	// Leaf: 表明你已明确同意你的回答接受评估
	// Leaf: .

	st.Equal(text+text1+text2, merged.String())
	st.Must(len(merged.pieces) == 5)

	st.Equal("Here is some Russian, ", merged.pieces[0].Text)
	st.Equal(10.0, merged.pieces[0].FontSize)
	st.Equal(fonts[0], merged.pieces[0].Font, "Should be tagged with Helvetica font.")

	st.Equal("Неприкосновенность", merged.pieces[1].Text)
	st.Equal(10.0, merged.pieces[1].FontSize)
	st.Equal(fonts[1], merged.pieces[1].Font, "Should be tagged with Arial font.")

	st.Equal(", and some Chinese, ", merged.pieces[2].Text)
	st.Equal(10.0, merged.pieces[2].FontSize)
	st.Equal(fonts[0], merged.pieces[2].Font, "Should be tagged with Helvetica font.")

	st.Equal("表明你已明确同意你的回答接受评估", merged.pieces[3].Text)
	st.Equal(10.0, merged.pieces[3].FontSize)
	st.Equal(fonts[2], merged.pieces[3].Font, "Should be tagged with STFangsong font.")

	st.Equal(".", merged.pieces[4].Text)
	st.Equal(10.0, merged.pieces[4].FontSize)
	st.Equal(fonts[0], merged.pieces[4].Font, "Should be tagged with Helvetica font.")

	st.Equal(piece0.Text, original.pieces[0].Text, "Original should be unchanged.")
}