Created
November 7, 2016 06:20
-
-
Save tommy351/a5baa9d22d2022ea82d79f5d407d5c02 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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