Skip to content

Instantly share code, notes, and snippets.

@tommy351
Created November 7, 2016 06:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommy351/a5baa9d22d2022ea82d79f5d407d5c02 to your computer and use it in GitHub Desktop.
Save tommy351/a5baa9d22d2022ea82d79f5d407d5c02 to your computer and use it in GitHub Desktop.
func drawLine(gc *draw2dimg.GraphicContext, str string, x float64, y float64) {
gc.FillStringAt(str, x, y)
}
func drawText(gc *draw2dimg.GraphicContext, str string, width float64, height float64, x float64, y float64) {
str = strings.Trim(str, " \n\r\t")
lines := strings.Split(str, "\n")
fontSize := gc.Current.FontSize
offsetY := y
wrapLines := []string{}
runePerLine := int(width / fontSize)
for _, line := range lines {
len := utf8.RuneCountInString(line)
chunks := []string{}
list := []rune(line)
for i := 0; i < len; i += runePerLine {
max := i + runePerLine
if max > len {
chunks = append(chunks, string(list[i:len]))
} else {
chunks = append(chunks, string(list[i:max]))
}
}
wrapLines = append(wrapLines, chunks...)
}
for _, line := range wrapLines {
_, top, _, bottom := gc.GetStringBounds(line)
drawLine(gc, line, x, offsetY)
offsetY += (bottom - top) * lineHeight
if offsetY > y+height {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment