draw2d
draw2d copied to clipboard
Add the ability to underline text
If I get things correctly, there is no way to draw underlined text, as https://godoc.org/github.com/llgcode/draw2d#FontStyle shows only Bold and Italics
Is there a way to underline text via the API? What about mixed styles, such as Boldltalics? Would it be possible to implement these features in some way? thanks
I thought you would combine bold and italic like this style = FontStyleBold | FontStyleItalic need more test on this. For underline I'm not sure it's a property of a font that we could set. Otherwise drawing a line underneath a text could be sufficient.
Thanks, I confirm combining the values work, I've documented it here http://sprunge.us/PdIK
I had thought about just drawing a line below the text for underlined style, but the size, the width, the strokes are dependent on the font, and style applied. I think it'd be nicer if this is done in the library rather than the user application.
Thx for testing Yes, For underline, it will be nicer to have it implemented in draw2d or freetype but I was thinking of a utility function. I'm not sure I wanted to see as a font style.
To clarify this issue in its present state, I believe it should be renamed to "Add the ability to underline text" and have the label changed to "Enhancement".
To add to discussion, I believe adding underline as a style would be how most users would expect underlining to be done, so that's how we should do it. A utility function should be fine too though as most text functions return the width anyways (and the font size is usually the height of the glyphs) so I don't believe it'd be difficult. I just think isolating it to a utility function will make text styling code look inconsistent.
It's been a while, but this is still a desired feature. Based on what I read here, I'm going to look at matching text width while keeping line-height and font-size in mind for wrapping text.
Hi @Xeoncross, yes you can use the length returned by FillString to draw the line that you need underneet the text.
I've prototyped the function you need , Is it something like this? https://play.golang.org/p/MuxKhec9G9H
Thanks @llgcode! I'm going to play with what you posted. I actually ended up playing with the http://github.com/fogleman/gg library for underline text but was never able to calculate the correct line-height for multi-line text.

Here is some partial code I was looking at but my calculations are incorrect.
dc := gg.NewContext(W, H)
// ...
err := dc.LoadFontFace(fontPath, 16);
// ...
var maxTextWidth float64 = 120
dc.DrawStringWrapped("Hello world ...", P, H/2, 0, 0.5, maxTextWidth, 2, gg.AlignLeft)
// Trying to calculate the line height of each row so we can add an underline!
var lineStrokeWidth float64 = .5
var lineSpacing float64 = 2
// How many lines does the text take-up?
textWidth, textHeight := dc.MeasureMultilineString(strings.Join(lines, "\n"), lineSpacing)
lineHeight := textHeight / float64(len(lines))
dc.SetLineWidth(lineStrokeWidth)
baseTextOffset := H/2 - (textHeight / 2)
for i := 1; i <= len(lines); i++ {
textY := float64(i) * lineHeight
dc.DrawLine(P, baseTextOffset+textY, P+textWidth, baseTextOffset+textY)
}
dc.Stroke()
dc.SavePNG("out.png")