-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.go
199 lines (183 loc) · 4.47 KB
/
text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package imagegenerator
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hmmftg/garabic"
"github.com/hmmftg/image/font"
"github.com/hmmftg/image/font/opentype"
"github.com/hmmftg/image/math/fixed"
ntw "moul.io/number-to-words"
)
type Text struct {
Text string
X float64
Y float64
// TODO:FixedWidth int
MaxWidth float64 // if result width is more than this value, library tries new font face with decreased size
RightAlign bool
NumbersToArabic bool
NumbersToPersian bool
NumberToWords bool
NumberToPersianWords bool
FontFace string
}
func NumToPersianWords(number string) string {
result := ""
for _, digit := range number {
i, _ := strconv.Atoi(string(digit))
result += ntw.IntegerToIrIr(i) + " "
}
return result
}
func NumToWords(number string) string {
result := ""
for _, digit := range number {
i, _ := strconv.Atoi(string(digit))
result += ntw.IntegerToEnUs(i) + " "
}
return result
}
func (tx *PrintTx) AddFace(fontName string, sz float64) (string, font.Face) {
name := fmt.Sprintf("%s:%f:%f", fontName, sz, tx.Dpi)
face, ok := (*tx.Faces)[name]
if ok {
return name, face
}
opts := opentype.FaceOptions{
Size: sz,
DPI: tx.Dpi,
Hinting: font.HintingFull,
}
opts.Hinting = font.HintingFull
fn := tx.Fonts[fontName]
face, _ = opentype.NewFace(&fn, &opts)
// fmt.Println("Adding font face:", name)
(*tx.Faces)[name] = face
return name, face
}
func (s Text) CheckFace(tx *PrintTx) (string, font.Face) {
name := s.Font(tx.Dpi)
face, ok := (*tx.Faces)[name]
if !ok {
faceName := s.Font(tx.Dpi)
if len(faceName) == 0 {
log.Println("invalid font face(text ignored)", s.FontFace, tx.Dpi)
return "", nil
}
return tx.AddFace(s.FontData())
}
return name, face
}
func (s Text) Font(dpi float64) string {
return fmt.Sprintf("%s:%f", s.FontFace, dpi)
}
func (s Text) FontData() (string, float64) {
fn := strings.Split(s.FontFace, ":")
if len(fn) == 0 {
return "", 0
}
sz, err := strconv.ParseFloat(fn[1], 64)
if err != nil {
return "", 0
}
return fn[0], sz
}
func Justify(input string, length int) string {
var shapedSentence []string
if len(input) > length {
return input
}
neededLen := length - len(input)
sections := strings.Fields(input)
lengths := make([]int, len(sections))
wordCount := len(sections) - 1
for i := 0; i < wordCount; i++ {
lengths[i] = neededLen / wordCount
if i <= neededLen%wordCount {
lengths[i]++
}
}
for id, word := range sections {
shapedSentence = append(shapedSentence, word+strings.Repeat(" ", lengths[id]))
}
return strings.Join(shapedSentence, " ")
}
func (s Text) Draw(tx *PrintTx) int {
// log.Println("drawing", s)
if len(s.Text) == 0 {
return 0
}
firstRune := []rune(s.Text)[0]
if s.NumberToPersianWords {
s.Text = NumToPersianWords(s.Text)
}
s.Text = garabic.Shape(s.Text)
if s.NumbersToArabic {
s.Text = garabic.ToArabicDigits(s.Text)
}
if s.NumbersToPersian {
s.Text = garabic.ToPersianDigits(s.Text)
}
if s.NumberToWords {
s.Text = NumToWords(s.Text)
}
_, face := s.CheckFace(tx)
if face == nil {
return 0
}
x := tx.RelationalX(s.X)
y := tx.RelationalY(s.Y)
d := &font.Drawer{
Dst: tx.Rgba,
Src: tx.Bg,
Face: face,
Dot: fixed.P(x, y),
}
textLen := d.MeasureString(s.Text)
advance := textLen.Round() + x
if s.MaxWidth > 0 {
fn, sz := s.FontData()
var adjustLog, faceName string
c := 0
for i := 0.0; tx.CoordinationX(advance) > s.MaxWidth; i += 0.02 {
faceName, d.Face = tx.AddFace(fn, sz-i)
textLen = d.MeasureString(s.Text)
advance = textLen.Round() + x
c++
adjustLog = fmt.Sprintf("adjusted face[%d|%f](%f,%f,%s,%.6s)=>%s\n", c, i, tx.CoordinationX(advance), s.MaxWidth, s.FontFace, s.Text, faceName)
}
if len(adjustLog) > 0 {
log.Print(adjustLog)
}
}
for advance > tx.Rgba.Bounds().Max.X-tx.RelationalX(tx.Margin) {
if len(s.Text) == 1 {
log.Println(
"adjusting text for width failed",
s.Text,
d.Face.Metrics(),
advance,
tx.Rgba.Bounds().Max.X,
tx.RelationalX(tx.Margin),
tx.Margin,
)
return 0
}
if garabic.IsArabicLetter(firstRune) {
s.Text = s.Text[1:]
} else {
s.Text = s.Text[:len(s.Text)-1]
}
textLen = d.MeasureString(s.Text)
advance = textLen.Round() + x
}
if s.RightAlign {
d.Dot = fixed.P(tx.Rgba.Bounds().Max.X-textLen.Round()-x, y)
advance = tx.Rgba.Bounds().Max.X - textLen.Round() - x
}
// fmt.Println("drawing", advance, tx.Dpi, d.Dot, s.Text)
d.DrawString(s.Text)
return advance
}