~eliasnaur/gio

2 2

Underlined text?

Details
Message ID
<20191120034259.GA45604@larrymbp14.local>
DKIM signature
missing
Download raw message
How would you draw underlined text, like for a url in a browser?

At a guess: draw the label, capture its Dimensions, and then draw the
line at y-coordinate = top left + Baseline from the captured
Dimensions?

And now that I mention it: what's the current code to draw a line?  I
found the previous answer from circa Oct 10, and it mentions
paint.Path which doesn't seem to exist any more.

Thanks!

-- Larry
Details
Message ID
<BYKN19SVR54W.13BVZV8WOB0V9@testmac>
In-Reply-To
<20191120034259.GA45604@larrymbp14.local> (view parent)
DKIM signature
pass
Download raw message
On Tue Nov 19, 2019 at 10:42 PM Larry Clapp wrote:
> How would you draw underlined text, like for a url in a browser?
> 
> At a guess: draw the label, capture its Dimensions, and then draw the
> line at y-coordinate = top left + Baseline from the captured
> Dimensions?
> 

Sounds correct to me.

> And now that I mention it: what's the current code to draw a line?  I
> found the previous answer from circa Oct 10, and it mentions
> paint.Path which doesn't seem to exist any more.
> 

You can either use a straight up paint.PaintOp for thin lines. Use
clip.Rect with corner radii set to half the line width before the
PaintOp if you want rounded line caps.

-- elias
Details
Message ID
<20191120222514.GA49969@larrymbp14.local>
In-Reply-To
<BYKN19SVR54W.13BVZV8WOB0V9@testmac> (view parent)
DKIM signature
missing
Download raw message
On Wed, Nov 20, 2019 at 11:05:13AM +0100, Elias Naur wrote:
> On Tue Nov 19, 2019 at 10:42 PM Larry Clapp wrote:
> > How would you draw underlined text, like for a url in a browser?
> > 
> > At a guess: draw the label, capture its Dimensions, and then draw
> > the line at y-coordinate = top left + Baseline from the captured
> > Dimensions?
> > 
> 
> Sounds correct to me.
> 
> > And now that I mention it: what's the current code to draw a line?
> > I found the previous answer from circa Oct 10, and it mentions
> > paint.Path which doesn't seem to exist any more.
> > 
> 
> You can either use a straight up paint.PaintOp for thin lines. Use
> clip.Rect with corner radii set to half the line width before the
> PaintOp if you want rounded line caps.

This worked, no Baseline needed.

  // Layout the text, as a Label
  widget.Label{}.Layout(gtx, gd.th.Shaper, gd.font, fmt.Sprintf("%v", stuff))

  // Get the text dimensions
  dims := gtx.Dimensions

  // Draw in blue
  paint.ColorOp{Color: color.RGBA{A: 0xff, B: 0xff}}.Add(gtx.Ops)
  // Draw the line
  paint.PaintOp{
	  Rect: f32.Rectangle{
		  Min: f32.Point{Y: float32(dims.Size.Y)},
		  // You have to say at least dims.Size.Y+1, or you
		  // get a zero-height line, i.e. no line at all.
		  // Y+2 is thicker.
		  Max: f32.Point{X: float32(dims.Size.X), Y: float32(dims.Size.Y + 2)},
	  },
  }.Add(gtx.Ops)

Note that the retrieved dimensions are relative to the Inset the Label
is drawn in.

Thanks for the pointer!

-- L