draw2d icon indicating copy to clipboard operation
draw2d copied to clipboard

Add the ability to underline text

Open kodawah opened this issue 9 years ago • 8 comments

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

kodawah avatar Nov 04 '15 16:11 kodawah

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.

llgcode avatar Nov 05 '15 08:11 llgcode

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.

kodawah avatar Nov 05 '15 11:11 kodawah

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.

llgcode avatar Nov 05 '15 13:11 llgcode

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.

redstarcoder avatar Oct 20 '16 16:10 redstarcoder

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.

xeoncross avatar Aug 21 '19 23:08 xeoncross

Hi @Xeoncross, yes you can use the length returned by FillString to draw the line that you need underneet the text.

llgcode avatar Sep 17 '19 08:09 llgcode

I've prototyped the function you need , Is it something like this? https://play.golang.org/p/MuxKhec9G9H

llgcode avatar Sep 17 '19 09:09 llgcode

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.

Screen Shot 2019-09-17 at 9 01 23 AM

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")

xeoncross avatar Sep 17 '19 14:09 xeoncross