Skip to content

Commit

Permalink
try fix Truncate
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiang-Red committed Jan 12, 2023
1 parent 6bda948 commit 732aae3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
6 changes: 3 additions & 3 deletions shadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func DrawShadowedRectangle(canvas *gg.Context, x, y, w, h, r, sigma float64, ox,
one.DrawRoundedRectangle(x, y, w, h, r)
one.SetColor(shadowcolor)
one.Fill()
canvas.DrawImage(imaging.Blur(one.Image(), sigma), ox, oy)
canvas.DrawImage(imaging.Blur(one.Image(), sigma), int(x)+ox, int(y)+oy)
}

// DrawShadowedString 绘制带阴影的文字
Expand All @@ -33,7 +33,7 @@ func DrawShadowedString(canvas *gg.Context, s, fontfile string, x, y, point, sig
}
one.SetColor(shadowcolor)
one.DrawString(s, x, y)
canvas.DrawImage(imaging.Blur(one.Image(), sigma), ox, oy)
canvas.DrawImage(imaging.Blur(one.Image(), sigma), int(x)+ox, int(y)+oy)
err = canvas.LoadFontFace(fontfile, point)
if err != nil {
return
Expand All @@ -52,7 +52,7 @@ func DrawShadowedStringAnchored(canvas *gg.Context, s, fontfile string, x, y, po
}
one.SetColor(shadowcolor)
one.DrawStringAnchored(s, x, y, ax, ay)
canvas.DrawImage(imaging.Blur(one.Image(), sigma), ox, oy)
canvas.DrawImage(imaging.Blur(one.Image(), sigma), int(x)+ox, int(y)+oy)
err = canvas.LoadFontFace(fontfile, point)
if err != nil {
return
Expand Down
34 changes: 14 additions & 20 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,34 @@ func Transparency(dst image.Image, magnification float64) image.Image {
}

// Truncate 截断文字
func Truncate(fontfile string, texts []string, maxW, fontsize float64) ([]string, error) {
func Truncate(fontfile string, texts []string, maxW, fontsize float64) (newtexts []string, err error) {
one := gg.NewContext(1, 1)
err := one.LoadFontFace(fontfile, fontsize)
err = one.LoadFontFace(fontfile, fontsize)
if err != nil {
return nil, err
return
}
newtexts := make([]string, 0, len(texts)*2)
newtexts = make([]string, 0, len(texts)*2)
for i := 0; i < len(texts); i++ {
newlinetext, textw, tmpw := "", 0.0, 0.0
text := texts[i]
for len(texts[i]) > 0 {
var tmp strings.Builder
tmp.Grow(len(text))
res := make([]rune, 0, len(text))
for _, r := range text {
tmp.WriteRune(r)
width, _ := one.MeasureString(tmp.String()) // 获取文字宽度
if width > maxW { // 如果宽度大于文字边距
break // 跳出
tmp.Grow(len(texts[i]))
res := make([]rune, 0, len(texts[i]))
for _, t := range texts[i] {
tmp.WriteRune(t)
width, _ := one.MeasureString(tmp.String())
if width > maxW {
break
} else {
tmpw = width
res = append(res, r) // 写入
res = append(res, t)
}
}
newlinetext = string(res)
newlinetext := string(res)
newtexts = append(newtexts, newlinetext)
if tmpw > textw {
textw = tmpw
}
if len(newlinetext) >= len(texts[i]) {
break
}
texts[i] = texts[i][len(newlinetext):]
}
}
return newtexts, nil
return
}

0 comments on commit 732aae3

Please sign in to comment.