func TestRichText_WordsToWidth_zero(t *testing.T) { st := SuperTest{t} p := arialText("Supercalifragilisticexpialidocious") flags := make([]wordbreaking.Flags, p.Len()) wordbreaking.MarkRuneAttributes(p.String(), flags) line, remainder, remainderFlags := p.WordsToWidth(0, flags, true) st.MustNot(line == nil, "Line must not be nil.") st.Equal("S", line.String()) st.MustNot(remainder == nil, "There should be text left over.") st.Equal("upercalifragilisticexpialidocious", remainder.String()) st.Equal(33, len(remainderFlags)) }
func TestRichText_WrapToWidth_zero(t *testing.T) { const testString = "Lorem" st := SuperTest{t} p := arialText(testString) flags := make([]wordbreaking.Flags, p.Len()) wordbreaking.MarkRuneAttributes(p.String(), flags) lines := p.WrapToWidth(0, flags, true) st.Must(len(lines) == len(testString)) for i, l := range lines { st.Equal(string(testString[i]), l.String()) } }
func TestRichText_WrapToWidth_short(t *testing.T) { st := SuperTest{t} p, err := New("Lorem ipsum.", ttf_fonts.Families("Arial"), 10, options.Options{"nobreak": true}) if err != nil { t.Fatal(err) } flags := make([]wordbreaking.Flags, p.Len()) wordbreaking.MarkRuneAttributes(p.String(), flags) lines := p.WrapToWidth(100, flags, false) st.Must(len(lines) == 1, "Should return one line.") st.Equal(p.Text, lines[0].Text, "Text should match.") st.True(p.MatchesAttributes(lines[0])) }
func TestRichText_WordsToWidth_short(t *testing.T) { st := SuperTest{t} p := arialText("Lorem") flags := make([]wordbreaking.Flags, p.Len()) wordbreaking.MarkRuneAttributes(p.String(), flags) line, remainder, remainderFlags := p.WordsToWidth(30, flags, false) st.Equal(p, line, "Should return original piece.") if remainder != nil { t.Error("There should be nothing left over.") } if remainderFlags != nil { t.Error("There should be no flags remaining.") } }
func TestRichText_WrapToWidth_nobreak_simple(t *testing.T) { st := SuperTest{t} rt, err := New( "Here is a long sentence with mostly small words.", ttf_fonts.Families("Arial"), 10, options.Options{"nobreak": true}) if err != nil { t.Fatal(err) } flags := make([]wordbreaking.Flags, rt.Len()) wordbreaking.MarkRuneAttributes(rt.String(), flags) rt.MarkNoBreak(flags) lines := rt.WrapToWidth(60, flags, false) st.Equal(1, len(lines), "Should return a single line.") }
func TestRichText_WordsToWidth_mixed(t *testing.T) { st := SuperTest{t} p := mixedText() flags := make([]wordbreaking.Flags, p.Len()) wordbreaking.MarkRuneAttributes(p.String(), flags) line, remainder, remainderFlags := p.WordsToWidth(60, flags, false) st.MustNot(line == nil, "Line must not be nil.") st.Equal("Here is some", line.String()) st.MustNot(remainder == nil, "There should be text left over.") st.Equal(" Russian, Неприкосновенность, and some Chinese, 表明你已明确同意你的回答接受评估.", remainder.String()) st.Equal(115, len(remainderFlags)) if remainderFlags == nil { t.Error("There should be flags remaining.") } }
func TestRichText_WrapToWidth_hardBreak(t *testing.T) { st := SuperTest{t} expected := []string{ "Supercalifrag", "ilisticexpialid", "ocious", } p := arialText("Supercalifragilisticexpialidocious") flags := make([]wordbreaking.Flags, p.Len()) wordbreaking.MarkRuneAttributes(p.String(), flags) lines := p.WrapToWidth(60, flags, true) st.Must(len(lines) == len(expected), "Unexpected number of lines.") for i := 0; i < len(expected); i++ { st.Equal(expected[i], lines[i].String(), "Unexpected text.") } }
func (pw *PageWriter) PrintWithOptions(text string, options options.Options) (err error) { var para []*rich_text.RichText rt, err := pw.richTextForString(text) if err != nil { return } if width := options.FloatDefault("width", 0); width > 0 { flags := make([]wordbreaking.Flags, rt.Len()) wordbreaking.MarkRuneAttributes(rt.String(), flags) para = rt.WrapToWidth(pw.units.toPts(width), flags, false) } else { para = []*rich_text.RichText{rt} } pw.PrintParagraph(para) return nil }
// Should not add hyphen-minus to end of string that does not end with a soft hyphen. func TestRichText_WrapToWidth_soft_hyphenated2(t *testing.T) { const hyphenatedText = "Super\u00ADbad example" expected := []string{ "Super\u00ADbad", "example", } rt := courierNewText(hyphenatedText) flags := make([]wordbreaking.Flags, rt.Len()) wordbreaking.MarkRuneAttributes(rt.String(), flags) lines := rt.WrapToWidth(62, flags, false) st := SuperTest{t} st.Must(len(lines) == len(expected), "Unexpected number of lines.") for i := range lines { st.Equal(expected[i], lines[i].String()) st.True(lines[i].Width() <= 62, "Line too long.") } }
func TestRichText_WrapToWidth_hyphenated(t *testing.T) { const hyphenatedText = "Super-cali-fragil-istic-expi-ali-do-cious" expected := []string{ "Super-cali-", "fragil-istic-", "expi-ali-do-", "cious", } rt := arialText(hyphenatedText) flags := make([]wordbreaking.Flags, rt.Len()) wordbreaking.MarkRuneAttributes(rt.String(), flags) lines := rt.WrapToWidth(60, flags, false) st := SuperTest{t} st.Must(len(lines) == len(expected), "Unexpected number of lines.") for i := range lines { st.Equal(expected[i], lines[i].String()) } }
func TestRichText_WrapToWidth_mixed(t *testing.T) { st := SuperTest{t} expected := []string{ "Here is some", "Russian,", "Неприкосновенность,", "and some", "Chinese,", "表明你已明确同意你的回答接受评估.", } p := mixedText() flags := make([]wordbreaking.Flags, p.Len()) wordbreaking.MarkRuneAttributes(p.String(), flags) lines := p.WrapToWidth(60, flags, false) st.Must(len(lines) == len(expected), "Unexpected number of lines.") for i := 0; i < len(expected); i++ { st.Equal(expected[i], lines[i].String(), "Unexpected text.") } }
func TestRichText_WrapToWidth_nobreak_complex(t *testing.T) { st := SuperTest{t} fonts := ttf_fonts.Families("Arial") var rt *RichText var err error rt, err = New("Here is a ", fonts, 10, options.Options{}) if err != nil { t.Fatal(err) } rt, err = rt.Add("long sentence with mostly", fonts, 10, options.Options{"nobreak": true}) if err != nil { t.Fatal(err) } rt, err = rt.Add(" small words.", fonts, 10, options.Options{}) if err != nil { t.Fatal(err) } flags := make([]wordbreaking.Flags, rt.Len()) wordbreaking.MarkRuneAttributes(rt.String(), flags) rt.MarkNoBreak(flags) lines := rt.WrapToWidth(60, flags, false) st.Equal(3, len(lines), "Should return 3 lines.") }
func (p *StdParagraph) Lines(w Writer, width float64) []*rich_text.RichText { rt := p.RichText(w) flags := make([]wordbreaking.Flags, rt.Len()) wordbreaking.MarkRuneAttributes(rt.String(), flags) return rt.WrapToWidth(width, flags, false) }