~eliasnaur/gio

10 5

[beginner] Need help to draw filled polylines

Details
Message ID
<dfe3ad32-c26b-95b5-ea52-8ae4fb4dc605@meessen.net>
DKIM signature
pass
Download raw message
Hello,

I would need a little help to get some simple filled polylines to be 
drawn into a GIO window. My attempts failed so far.

Below is the code I have. Only the label is visible.

The function Convert() converts text into a set of polylines. The 
function drawText() is drawing it using the GIO drawing primitives. All 
my paths are closed. Is calling p.Close() still required ?

I also want to show a normal label using the GIO text drawing primitive 
with my text stacked just below.

Is there an example program for such simple use case that I could use as 
reference ?

funcmain() {
gofunc() {
w:= app.NewWindow()
iferr:= loop(w); err != nil{
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}
funcloop(w *app.Window) error{
f, err:= FontFromBytes(goregular.TTF)
iferr != nil{
log.Fatal("failed loading font:", err)
}
t, err:= Convert("ia", 12, false, f)
iferr != nil{
log.Fatal("failed making text:", err)
}
th:= material.NewTheme(gofont.Collection())
varopsop.Ops
for{
e:= <-w.Events()
switche:= e.(type) {
casesystem.DestroyEvent:
returne.Err
casesystem.FrameEvent:
gtx:= layout.NewContext(&ops, e)
l:= material.Body1(th, "ia")
l.Color= color.NRGBA{R: 0, G: 0, B: 0, A: 255}
l.Alignment= text.Middle
l.Layout(gtx)
// ... ?
drawText(gtx, t)
// ... ?
e.Frame(gtx.Ops)
}
}
}
functoF32Pt(x, y float64) f32.Point {
returnf32.Pt(float32(x), float32(y))
}
funcdrawText(gtx layout.Context, text *Text) layout.Dimensions {
deferop.Save(gtx.Ops).Load()
paint.ColorOp{Color: color.NRGBA{A: 0xFF}}.Add(gtx.Ops)
varpclip.Path
for_, char:= rangetext.Text {
p.Begin(gtx.Ops)
for_, path:= rangechar {
p.MoveTo(toF32Pt(path[0], path[1]))
fori:= 2; i < len(path); i += 2{
p.LineTo(toF32Pt(path[i], path[i+1]))
}
p.Close()
}
clip.Outline{Path: p.End()}.Op().Add(gtx.Ops)
paint.PaintOp{}.Add(gtx.Ops)
}
returnlayout.Dimensions{Size: image.Pt(int(math.Ceil(text.Width)), 
int(math.Ceil(text.Height)))}
}






-- 
Bien cordialement,
Ch.Meessen
Details
Message ID
<CANtNKfq0JupTkZ0F1+Z7yb6W4SxAZnhgXkaoaUDUDkbRvAQHXg@mail.gmail.com>
In-Reply-To
<dfe3ad32-c26b-95b5-ea52-8ae4fb4dc605@meessen.net> (view parent)
DKIM signature
pass
Download raw message
Hi,

The code got pretty messed up, but roughly looks correct.
Can you upload the code such that it can be easily run?

The issue is probably because your outline does not specify the line width.

+ Egon

On Tue, Apr 20, 2021 at 1:21 PM Christophe Meessen
<christophe@meessen.net> wrote:
>
> Hello,
>
> I would need a little help to get some simple filled polylines to be
> drawn into a GIO window. My attempts failed so far.
>
> Below is the code I have. Only the label is visible.
>
> The function Convert() converts text into a set of polylines. The
> function drawText() is drawing it using the GIO drawing primitives. All
> my paths are closed. Is calling p.Close() still required ?
>
> I also want to show a normal label using the GIO text drawing primitive
> with my text stacked just below.
>
> Is there an example program for such simple use case that I could use as
> reference ?
>
> funcmain() {
> gofunc() {
> w:= app.NewWindow()
> iferr:= loop(w); err != nil{
> log.Fatal(err)
> }
> os.Exit(0)
> }()
> app.Main()
> }
> funcloop(w *app.Window) error{
> f, err:= FontFromBytes(goregular.TTF)
> iferr != nil{
> log.Fatal("failed loading font:", err)
> }
> t, err:= Convert("ia", 12, false, f)
> iferr != nil{
> log.Fatal("failed making text:", err)
> }
> th:= material.NewTheme(gofont.Collection())
> varopsop.Ops
> for{
> e:= <-w.Events()
> switche:= e.(type) {
> casesystem.DestroyEvent:
> returne.Err
> casesystem.FrameEvent:
> gtx:= layout.NewContext(&ops, e)
> l:= material.Body1(th, "ia")
> l.Color= color.NRGBA{R: 0, G: 0, B: 0, A: 255}
> l.Alignment= text.Middle
> l.Layout(gtx)
> // ... ?
> drawText(gtx, t)
> // ... ?
> e.Frame(gtx.Ops)
> }
> }
> }
> functoF32Pt(x, y float64) f32.Point {
> returnf32.Pt(float32(x), float32(y))
> }
> funcdrawText(gtx layout.Context, text *Text) layout.Dimensions {
> deferop.Save(gtx.Ops).Load()
> paint.ColorOp{Color: color.NRGBA{A: 0xFF}}.Add(gtx.Ops)
> varpclip.Path
> for_, char:= rangetext.Text {
> p.Begin(gtx.Ops)
> for_, path:= rangechar {
> p.MoveTo(toF32Pt(path[0], path[1]))
> fori:= 2; i < len(path); i += 2{
> p.LineTo(toF32Pt(path[i], path[i+1]))
> }
> p.Close()
> }
> clip.Outline{Path: p.End()}.Op().Add(gtx.Ops)
> paint.PaintOp{}.Add(gtx.Ops)
> }
> returnlayout.Dimensions{Size: image.Pt(int(math.Ceil(text.Width)),
> int(math.Ceil(text.Height)))}
> }
>
>
>
>
>
>
> --
> Bien cordialement,
> Ch.Meessen
>
Christophe Meessen
Details
Message ID
<c83199e4-48b7-1361-d1eb-5f29bea00c53@cppm.in2p3.fr>
In-Reply-To
<CANtNKfq0JupTkZ0F1+Z7yb6W4SxAZnhgXkaoaUDUDkbRvAQHXg@mail.gmail.com> (view parent)
DKIM signature
missing
Download raw message
Thank you for offering your help. The code, in a readable and 
downloadable form, is here [1]. It is a single main.go file.

The intent is to draw the filled area defined by the clipping region 
which correspond to the text "ia" in this case.

drawText function was inspired by the game of life example. I assumed 
that squares were filled, not outlined.


[1] https://pastebin.com/jZNS7AXr

Le 20/04/2021 à 12:28, Egon Elbre a écrit :
> Hi,
>
> The code got pretty messed up, but roughly looks correct.
> Can you upload the code such that it can be easily run?
>
> The issue is probably because your outline does not specify the line width.
>
> + Egon
>
Details
Message ID
<CANtNKfpbhTaAn-aJA=fz6Z394pNkYyr0punzvbvZCV_s-Rx8Tg@mail.gmail.com>
In-Reply-To
<c83199e4-48b7-1361-d1eb-5f29bea00c53@cppm.in2p3.fr> (view parent)
DKIM signature
pass
Download raw message
Oh, right... I mistook clip.Outline for clip.Path (derp).

You need to reset the clip path after every `clip.Outline` otherwise
they will be AND-ed together.

Alternatively, construct only one clip path rather than multiple.

https://pastebin.com/JvNTkt0Z

I would construct only one path -- unless you want to color each
letter a different color.

+ Egon

On Tue, Apr 20, 2021 at 3:56 PM Christophe Meessen
<meessen@cppm.in2p3.fr> wrote:
>
> Thank you for offering your help. The code, in a readable and
> downloadable form, is here [1]. It is a single main.go file.
>
> The intent is to draw the filled area defined by the clipping region
> which correspond to the text "ia" in this case.
>
> drawText function was inspired by the game of life example. I assumed
> that squares were filled, not outlined.
>
>
> [1] https://pastebin.com/jZNS7AXr
>
> Le 20/04/2021 à 12:28, Egon Elbre a écrit :
> > Hi,
> >
> > The code got pretty messed up, but roughly looks correct.
> > Can you upload the code such that it can be easily run?
> >
> > The issue is probably because your outline does not specify the line width.
> >
> > + Egon
> >
Details
Message ID
<184c0190-fe22-9aa9-c01f-ac4e5ec3a18e@meessen.net>
In-Reply-To
<CANtNKfpbhTaAn-aJA=fz6Z394pNkYyr0punzvbvZCV_s-Rx8Tg@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
It works. I used version 1. Thank you very much. I can proceed with my 
tests.

Just another question. Is the p.Close() required ? I’m sure my paths are 
closed, but I wasn’t sure if it was doing something else.

Le 20/04/2021 à 15:23, Egon Elbre a écrit :
> Oh, right... I mistook clip.Outline for clip.Path (derp).
>
> You need to reset the clip path after every `clip.Outline` otherwise
> they will be AND-ed together.
>
> Alternatively, construct only one clip path rather than multiple.
>
> https://pastebin.com/JvNTkt0Z
>
> I would construct only one path -- unless you want to color each
> letter a different color.
>
> + Egon
>
> On Tue, Apr 20, 2021 at 3:56 PM Christophe Meessen
> <meessen@cppm.in2p3.fr> wrote:
>> Thank you for offering your help. The code, in a readable and
>> downloadable form, is here [1]. It is a single main.go file.
>>
>> The intent is to draw the filled area defined by the clipping region
>> which correspond to the text "ia" in this case.
>>
>> drawText function was inspired by the game of life example. I assumed
>> that squares were filled, not outlined.
>>
>>
>> [1] https://pastebin.com/jZNS7AXr
>>
>> Le 20/04/2021 à 12:28, Egon Elbre a écrit :
>>> Hi,
>>>
>>> The code got pretty messed up, but roughly looks correct.
>>> Can you upload the code such that it can be easily run?
>>>
>>> The issue is probably because your outline does not specify the line width.
>>>
>>> + Egon
>>>
-- 
Bien cordialement,
Ch.Meessen
Details
Message ID
<CANtNKfooo4VZ9UvC8NPEqcWmW+G23XBamw-d_yR8WrLjH-zuMg@mail.gmail.com>
In-Reply-To
<184c0190-fe22-9aa9-c01f-ac4e5ec3a18e@meessen.net> (view parent)
DKIM signature
pass
Download raw message
p.Close() is not required, however it's required that the paths are closed.

In your case, I don't think there's a potential issue, although the
cost of `p.Close` isn't that big.

When you use Line, Cube (instead of LineTo and CubeTo) you always want to have
p.Close to avoid potential issues with floating-point drift.

+ Egon

On Tue, Apr 20, 2021 at 5:24 PM Christophe Meessen
<christophe@meessen.net> wrote:
>
> It works. I used version 1. Thank you very much. I can proceed with my
> tests.
>
> Just another question. Is the p.Close() required ? I’m sure my paths are
> closed, but I wasn’t sure if it was doing something else.
>
> Le 20/04/2021 à 15:23, Egon Elbre a écrit :
> > Oh, right... I mistook clip.Outline for clip.Path (derp).
> >
> > You need to reset the clip path after every `clip.Outline` otherwise
> > they will be AND-ed together.
> >
> > Alternatively, construct only one clip path rather than multiple.
> >
> > https://pastebin.com/JvNTkt0Z
> >
> > I would construct only one path -- unless you want to color each
> > letter a different color.
> >
> > + Egon
> >
> > On Tue, Apr 20, 2021 at 3:56 PM Christophe Meessen
> > <meessen@cppm.in2p3.fr> wrote:
> >> Thank you for offering your help. The code, in a readable and
> >> downloadable form, is here [1]. It is a single main.go file.
> >>
> >> The intent is to draw the filled area defined by the clipping region
> >> which correspond to the text "ia" in this case.
> >>
> >> drawText function was inspired by the game of life example. I assumed
> >> that squares were filled, not outlined.
> >>
> >>
> >> [1] https://pastebin.com/jZNS7AXr
> >>
> >> Le 20/04/2021 à 12:28, Egon Elbre a écrit :
> >>> Hi,
> >>>
> >>> The code got pretty messed up, but roughly looks correct.
> >>> Can you upload the code such that it can be easily run?
> >>>
> >>> The issue is probably because your outline does not specify the line width.
> >>>
> >>> + Egon
> >>>
> --
> Bien cordialement,
> Ch.Meessen
>
Details
Message ID
<CASPP1L3A5IT.2629C1CCWA6BK@themachine>
In-Reply-To
<CANtNKfooo4VZ9UvC8NPEqcWmW+G23XBamw-d_yR8WrLjH-zuMg@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
On Tue Apr 20, 2021 at 18:05, Egon Elbre wrote:
> p.Close() is not required, however it's required that the paths are closed.
>
> In your case, I don't think there's a potential issue, although the
> cost of `p.Close` isn't that big.
>
> When you use Line, Cube (instead of LineTo and CubeTo) you always want to have
> p.Close to avoid potential issues with floating-point drift.
>

Should we remove the relative path commands? They don't seem worth the
gotcha.

Elias
Details
Message ID
<CANtNKfq3P8owWoQJAo0Pn2YG6ZvigOWP54xB6ebTJe_WxyLOTg@mail.gmail.com>
In-Reply-To
<CASPP1L3A5IT.2629C1CCWA6BK@themachine> (view parent)
DKIM signature
pass
Download raw message
I'm fine with removing them or keeping them.

My only concern with removing them is that people probably have
already heavily used them and it might be some effort to reimplement
things.

As an easier way to convert existing code there could be some external
temporary package that can be used instead of clip.Path that still
supports them for some time. e.g. `gioui.org/x/legacy/relclip` /
`relpath` or something -- that way fixing is just replacing the import
for Path.

+ Egon

On Tue, Apr 20, 2021 at 8:03 PM Elias Naur <mail@eliasnaur.com> wrote:
>
> On Tue Apr 20, 2021 at 18:05, Egon Elbre wrote:
> > p.Close() is not required, however it's required that the paths are closed.
> >
> > In your case, I don't think there's a potential issue, although the
> > cost of `p.Close` isn't that big.
> >
> > When you use Line, Cube (instead of LineTo and CubeTo) you always want to have
> > p.Close to avoid potential issues with floating-point drift.
> >
>
> Should we remove the relative path commands? They don't seem worth the
> gotcha.
>
> Elias
Details
Message ID
<CASQ2LNUJMRW.2TQPHD9O0UZKK@themachine>
In-Reply-To
<CANtNKfq3P8owWoQJAo0Pn2YG6ZvigOWP54xB6ebTJe_WxyLOTg@mail.gmail.com> (view parent)
DKIM signature
pass
Download raw message
On Tue Apr 20, 2021 at 20:11, Egon Elbre wrote:
> I'm fine with removing them or keeping them.
>
> My only concern with removing them is that people probably have
> already heavily used them and it might be some effort to reimplement
> things.
>
> As an easier way to convert existing code there could be some external
> temporary package that can be used instead of clip.Path that still
> supports them for some time. e.g. `gioui.org/x/legacy/relclip` /
> `relpath` or something -- that way fixing is just replacing the import
> for Path.
>

Good idea. Or perhaps this is a good opportunity to learn
https://github.com/rsc/rf.

Elias
Details
Message ID
<UO5mLn1yEDVtDMxxP9Or5sJ9VHqu9MJeQN8mZqdIoJ-dMzI9dNXTHIVa8nW8ofhVqDjx2MNg0fMdOz-91KDq4giDZ-BurNmjSdG3WkT19Ig=@protonmail.ch>
In-Reply-To
<CASQ2LNUJMRW.2TQPHD9O0UZKK@themachine> (view parent)
DKIM signature
pass
Download raw message
Perhaps if you change it to a fixed point format you can eliminate these rounding errors?

Sent with ProtonMail Secure Email.

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

On Tuesday, April 20th, 2021 at 19:21, Elias Naur <mail@eliasnaur.com> wrote:

> On Tue Apr 20, 2021 at 20:11, Egon Elbre wrote:
> 

> > I'm fine with removing them or keeping them.
> > 

> > My only concern with removing them is that people probably have
> > 

> > already heavily used them and it might be some effort to reimplement
> > 

> > things.
> > 

> > As an easier way to convert existing code there could be some external
> > 

> > temporary package that can be used instead of clip.Path that still
> > 

> > supports them for some time. e.g. `gioui.org/x/legacy/relclip` /
> > 

> > `relpath` or something -- that way fixing is just replacing the import
> > 

> > for Path.
> 

> Good idea. Or perhaps this is a good opportunity to learn
> 

> https://github.com/rsc/rf.
> 

> Elias
Details
Message ID
<CANtNKfrNfAeWPFygAOSTo9X3OWt4j95iUz=sSLkS2dvmz04SYw@mail.gmail.com>
In-Reply-To
<UO5mLn1yEDVtDMxxP9Or5sJ9VHqu9MJeQN8mZqdIoJ-dMzI9dNXTHIVa8nW8ofhVqDjx2MNg0fMdOz-91KDq4giDZ-BurNmjSdG3WkT19Ig=@protonmail.ch> (view parent)
DKIM signature
pass
Download raw message
Fixed point format won't help since the distance calculations
themselves may introduce an error.
As a simple example:

h := 10 //px
p.Begin(ops)
p.Move(0, 0)
p.Line(0, h/3)
p.Line(10, h/3)
p.Line(20, h/3)
p.Line(0, h/3)
p.Line(0, -h)

No matter how you try, you can find easy ways to introduce computation errors.
The easiest way to avoid them is to use absolute coordinates.

+ Egon

On Tue, Apr 20, 2021 at 10:45 PM Loki Verloren
<stalker.loki@protonmail.ch> wrote:
>
> Perhaps if you change it to a fixed point format you can eliminate these rounding errors?
>
> Sent with ProtonMail Secure Email.
>
> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
>
> On Tuesday, April 20th, 2021 at 19:21, Elias Naur <mail@eliasnaur.com> wrote:
>
> > On Tue Apr 20, 2021 at 20:11, Egon Elbre wrote:
> >
>
> > > I'm fine with removing them or keeping them.
> > >
>
> > > My only concern with removing them is that people probably have
> > >
>
> > > already heavily used them and it might be some effort to reimplement
> > >
>
> > > things.
> > >
>
> > > As an easier way to convert existing code there could be some external
> > >
>
> > > temporary package that can be used instead of clip.Path that still
> > >
>
> > > supports them for some time. e.g. `gioui.org/x/legacy/relclip` /
> > >
>
> > > `relpath` or something -- that way fixing is just replacing the import
> > >
>
> > > for Path.
> >
>
> > Good idea. Or perhaps this is a good opportunity to learn
> >
>
> > https://github.com/rsc/rf.
> >
>
> > Elias
Reply to thread Export thread (mbox)