Commit Graph

2724 Commits

Author SHA1 Message Date
Dominik Honnef 51b11486c5 widget: [API] correct default scaling of images
When no scale factor is set, scale by 1.0, mapping one image pixel to
one device-independent pixel. This matches the behavior of CSS and other
frameworks.

The old code attempted to convert to Dp while taking the image's DPI
into account. This was wrong in two ways:

- It assumed that the default display DPI is 160, but this is only true
  for Android. Other platforms use 96, 162, or leave it undefined. Thus
  image.Layout's idea of a dp didn't match that of Gio on most
  platforms.

- It tried to account for image DPI, and assumed a default of 72. This
  was wrong in that DPI in images is merely metadata meant for printing,
  not display. The vast majority of software such as image viewers and
  image editors do not take DPI into account, mapping one image pixel
  either to one physical pixel or to one device-independent pixel. That
  is, users would expect their images to either display 1 to 1, or scaled
  based on PxPerDp, but not scaled based on the image's DPI.

We default to a scale of 1 to stay consistent with other parts of Gio
that scale by default. Users who don't want any scaling can continue to
set the scale to the inverse of PxPerDp.

While we're here we clarify the documentation of the Scale field.

This change is backwards incompatible for users that relied on the
default scale.

Signed-off-by: Dominik Honnef <dominik@honnef.co>
2023-03-23 17:06:43 -06:00
Larry Clapp fa34121f00 text: fix sorting in faceOrderer.sorted
faceOrderer.sorted tried to put the "primary" font first by tweaking the
"less" function in sort.Slice, but it didn't work correctly.

If item i equaled the "primary" font, less() always returned true. This
did not take into account if item j was the "primary" font, in which
case it could easily be sorted differently.

Rather than adding another special case for that, which I couldn't
convince myself was actually correct in every case, I just searched for
the "primary" font and moved it to the front of the slice, and then
omitted the first item of the slice from the rest of the sorting.

Signed-off-by: Larry Clapp <larry@theclapp.org>
2023-03-23 17:02:43 -06:00
Chris Waldon b09ef80d9f widget: ensure proper modifiers on key events
This commit extends the key event handling for text widgets to always check for
appropriate modifier keys. Previously this wasn't necessary, as the text widgets
would only ever receive key events it registered for, but now it may be the top-level
key event handler and thus receive all key events that aren't handled elsewhere.

Fixes: https://todo.sr.ht/~eliasnaur/gio/487
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2023-03-23 16:57:52 -06:00
Dominik Honnef 107401cf07 layout: improve documentation for List.ScrollTo and List.ScrollBy
Signed-off-by: Dominik Honnef <dominik@honnef.co>
2023-03-23 16:44:32 -06:00
Dominik Honnef dc9a4a4009 layout: simplify implementation of List.ScrollTo
Signed-off-by: Dominik Honnef <dominik@honnef.co>
2023-03-23 16:44:12 -06:00
Serhat Sevki Dincer 4a1962e5e8 text: simplify font weights
Signed-off-by: Serhat Sevki Dincer <jfcgauss@gmail.com>
2023-03-23 16:37:46 -06:00
Serhat Sevki Dincer 35a8231963 text,widget: remove ineffective assignments
Signed-off-by: Serhat Sevki Dincer <jfcgauss@gmail.com>
2023-03-23 16:37:46 -06:00
Serhat Sevki Dincer 39b1158410 app,gpu{,/headless,/internal/rendertest}: replace io/ioutil with io & os
Signed-off-by: Serhat Sevki Dincer <jfcgauss@gmail.com>
2023-03-23 16:37:46 -06:00
Chris Waldon 1210bbb34a text: test maxlines with exported API
This commit changes _how_ the test for line wrapping is implemented to rely on the
exported API rather than internal symbols.

Thanks to https://github.com/gioui/gio/pull/109 for pointing this out.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2023-03-23 16:29:02 -06:00
Dominik Honnef 5f818bc5e7 widget/material: use more efficient way of scrolling lists
Signed-off-by: Dominik Honnef <dominik@honnef.co>
2023-02-23 18:43:50 -06:00
Dominik Honnef 8af4472672 layout: add API for efficiently scrolling to and by items
The majority of scrolling happens by manipulating the index of the first
displayed item instead of by just manipulating the offset. This lets us
avoid having to render all items that were scrolled past.

Instead of numbers of items we could've accepted a ratio in [0, 1] to
scroll by or to, to match the data we get from scrollbars. However,
there are more use cases for scrolling by items, such as keyboard
shortcuts, go-to dialogs, etc. And converting from [0, 1] to items is
trivial for the user as long as they know the number of items, and will
usually be handled for them by a theme.

Signed-off-by: Dominik Honnef <dominik@honnef.co>
2023-02-23 18:43:43 -06:00
Elias Naur bb12508a8a go.*: bump golang.org/x/text
Avoids CVE-2022-32149.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-02-23 16:43:53 -06:00
Elias Naur 0dba85f52e io,app: route all unhandled key events to the topmost handler
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-02-06 17:10:44 -06:00
Elias Naur 32c6a9b10d gpu/internal/rendertest: add issue references to broken tests
References: https://todo.sr.ht/~eliasnaur/gio/479
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-02-06 12:08:04 -06:00
Egon Elbre bce4153640 internal/stroke: fix line overlap
When the line overlaps itself backtracking exactly, e.g.

   path.MoveTo(0, 100)
   path.LineTo(100, 0)
   path.LineTo(0, 100)

then acos calculation is relatively unstable. By using atan2 it avoids
some of such problems in the calculation. Additionally, it simpliflies
the round join calculation.

Fixes: https://todo.sr.ht/~eliasnaur/gio/474
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-02-06 12:01:52 -06:00
Egon Elbre 14a33f3cb7 gpu/internal/rendertest: fix alphaClose check
Apparently, alphaClose has been overflowing and giving the wrong answer
for a while and hence some of the tests are broken. I currently disabled
those tests, because I'm not quite sure where and how they broke.

Also, bumped alpha tolerance to 8, to ignore false positives.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-02-06 11:51:02 -06:00
Gordon Klaus db6b4de0f7 widget/material: [API] move widget.Float.{Axis,Invert} into material.SliderStyle
Signed-off-by: Gordon Klaus <gordon.klaus@gmail.com>
2023-01-27 21:04:32 -06:00
Gordon Klaus 22aa00f476 widget/material: add Float.Invert
Setting Float.Invert=true not only inverts the order of values (which was already easily done by swapping min and max), it also draws the widget inverted so that the track is darkened on the opposite side from usual.

This patch also fixes a bug wherein a vertical slider was drawn inverted by default.

Signed-off-by: Gordon Klaus <gordon.klaus@gmail.com>
2023-01-27 21:04:01 -06:00
Elias Naur ac2c284d16 app: [Android] sanitize IME snippet bounds
Fixes: https://todo.sr.ht/~eliasnaur/gio/473
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-01-27 20:36:12 -06:00
Larry Clapp e0cf570339 widget: add a Focus() method to widget.Clickable
Signed-off-by: Larry Clapp <larry@theclapp.org>
2023-01-18 16:28:18 -06:00
Elias Naur af7afea5a3 gpu: don't allocate null materials buffer when running on the CPU
References: https://todo.sr.ht/~eliasnaur/gio/469
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-01-14 09:59:55 -06:00
Chris Waldon 1eb5c7dbcd widget: use caller-provided buffers for reading out text
This commit alters the textView API to give callers the option to provide
their own buffers for reading text. This enables some widget usecases to
be zero-allocation if a widget simply needs to examine the contents of the
text without returning it as a string.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2023-01-14 09:54:06 -06:00
Chris Waldon 940f0f6021 widget: document MoveWord current limitations
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2023-01-14 09:53:22 -06:00
Chris Waldon 1c49532447 widget: update ByteOffset method docs
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2023-01-14 09:53:09 -06:00
Chris Waldon 044390c9df widget: document update and paint methods on textView
This commit updates the textView to better describe the expectations
and behaviors of the Update and Paint* methods.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2023-01-14 09:52:55 -06:00
Chris Waldon f6d56dba89 widget: drop obsolete comment fragment
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2023-01-14 09:52:51 -06:00
Elias Naur f8221bb2ab gpu/internal/opengl: don't query FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING on GLES2
It's not supported in OpenGL ES 2.

References: https://todo.sr.ht/~eliasnaur/gio/469
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-01-06 18:51:20 -06:00
Elias Naur 1a84517b12 gpu/internal/opengl: avoid UNPACK_ROW_LENGTH/PACK_ROW_LENGTH on GLES2
Similarly to WebGL1, they're not supported in OpenGL ES 2.0.

References: https://todo.sr.ht/~eliasnaur/gio/469
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2023-01-06 18:40:22 -06:00
Egon Elbre 827e20d84d gpu: optimize pack.tryAdd
name       old time/op  new time/op  delta
Packer-32   559µs ± 2%   295µs ± 1%  -47.18%  (p=0.008 n=5+5)

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-01-06 18:26:42 -06:00
Egon Elbre 8bc6737dea gpu: optimize encodeQuadTo
name             old time/op  new time/op  delta
EncodeQuadTo-32  35.4ns ± 1%  11.9ns ± 3%  -66.34%  (p=0.008 n=5+5)

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-01-06 18:26:38 -06:00
Egon Elbre c81a1f9671 widget: fix build for go1.17
go.mod specifies 1.18, due to go.mod behavior and to avoid some issues
with updating the dependencies. However, we can still support older go
version, as long as it compiles with the older version.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-01-06 18:26:10 -06:00
Egon Elbre e9bce02b24 unit: add PxToDp and PxToSp
PxToDp and PxToSp are useful when you are trying to calculate
text-size or widget size based on dynamically sized container.

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2023-01-01 10:19:50 -06:00
Chris Waldon aa2a948b86 text,widget: [API] drop runereader based shaping API
The io.Reader based API has the potential to be significantly more
efficient, and there are very few users of the runereader API. This
commit simply drops it entirely in favor of the reader API.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-23 09:31:52 -06:00
Chris Waldon dc6fbf07f0 widget: expose text region resolution
This commit adds exported methods to both LabelState and Editor
allowing callers to locate the text regions representing a range
of runes. This can be used to build interactive subregions of text,
like (for instance) hyperlinks.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-23 09:31:48 -06:00
Chris Waldon e98c8955bb widget{,/material}: rebuild label and editor with textView
This commit rebuilds the editor and label types on the common
foundation provided by textView. This enables labels to have
optional state that makes them selectable, and allows the
two widgets to share the code for managing cursor positions,
displaying selections, and soforth. Labels now have an additional
Layout function which can be invoked if they have a Selectable.
It accepts a layout.Widget used to paint their contents. Stateless
labels should still use the old Layout method.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-23 09:31:45 -06:00
Chris Waldon f99aff96ee widget: create standalone textView
This commit adds a standalone state type for manipulating
and displaying text. It reads text from a minimal interface,
shapes it, tracks valid cursor positions, and provides sizing
and scrolling services to higher-level widgets. My long term
goal with these types is to export them to allow non-core widgets
to build atop them, but I've left them private for now.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-23 09:31:40 -06:00
Chris Waldon 5d6cc2892d text: consume io.Reader in shaper
io.Reader is actually a more efficient interface than io.RuneReader,
as we can pull bytes out and check for cache hits without doing
redundant rune<->string conversions. This isn't implemented yet,
however.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-23 09:31:36 -06:00
Chris Waldon 0b456579a9 widget: add ReadOnly mode to editor
This commit provides a new ReadOnly boolean on the editor. If set, the
editor functions as a selectable label. User interaction cannot change
the contents of the editor (though application code can still use the
API).

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-20 11:08:02 -06:00
Chris Waldon c455f0f342 text,widget: test and fix minWidth alignment
This commit unifies and fixes the shaper's handling of the alignment
minimum width. Previously it was only considered when the text was
a single line, but in hindsight that was clearly a mistake. Now the
maximum width of all shaped lines and the minimum width is used to
set the text alignment.

This commit also fixes an index test in package widget that was
relying on the old (incorrect) alignment behavior.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-19 11:17:16 -06:00
Chris Waldon fe5878bc63 widget: track minWidth of editor for alignment
This commit extends the editor to keep track of its own minimum constraint
and to provide that value to the text shaper for the purpose of aligning
text. Without this, the shaper does not know how much of the width of the
editor to use for alignment purposes.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-19 11:17:12 -06:00
Elias Naur 5d1d1df206 text,widget: use != for flag tests
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-12-16 17:32:30 -06:00
Chris Waldon 12da71821a widget: update glyph iteration
This commit updates the textIterator and glyphIndex types to consume
new flag information provided on glyphs. These changes allow widget.Label
and widget.Editor to correctly compute text bounding boxes and to
generate valid cursor positions at the end of text.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-16 17:27:16 -06:00
Chris Waldon 5b40d3cd47 text: provide start of paragraph glyph marker
This commit adds a new flag to glyphs indicating that they are the
beginning of a new paragraph, as well as adding a guarantee that a
glyph with this flag will always follow a glyph with FlagParagraphBreak,
even if a paragraph break is the last rune in the text. This helps
widgets to find the boundaries and positions of text ending with
newlines reliably.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-16 17:27:08 -06:00
Chris Waldon b0483975b7 text: drop unused field on line
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-15 08:41:40 -06:00
Chris Waldon bfb47538aa text: ensure runereader behaves same as string
This commit fixes a subtle discrepancy in the handling of text input
within the shaper. Text provided as an io.RuneReader with a trailing
newline would generate an extra (empty) line of text, whereas the
same input provided as a string would not.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-15 08:34:58 -06:00
Chris Waldon 2db1a7bfb9 go.*,text: implement shaper-driven line truncation
This commit pushes limiting the maximum number of lines of text into
the shaper implementation. This is more efficient than doing it in
widgets, and also opens the door for future use of the shaper to
insert ellipsis and other truncating characters as appropriate.

I realized that we lost the implementation of limiting the number of
lines of text in my text stack overhaul, so this fixes a regression
from that work.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-14 11:44:03 -06:00
Chris Waldon 719278bb36 widget: unify text painting and fix premature termination
This commit unifies all widget text painting to use a single function
and fixes two bugs that could result in visible glyphs failing to be
painted.

The first bug was that we checked whether a particular glyph's
outline was visible within the viewport and terminated iteration the
first time that we found a glyph that wasn't visible. If the very top
of the next line of text was visible within the viewport, taller glyphs
should be painted since part of them is visible. We would stop as soon
as we got to a short glyph, preventing the rest of the line (and any
tall glyphs it contained) from being painted.

I fixed this first problem by using the ascent/descent of the line containing
a glyph to determine whether it's "visible". While this will conclude that
a small glyph is visible when it may be entirely off-screen, the net result
will be that we will paint the entire line containing the glyph rather than
constructing a special version of the line with only the tall glyphs. This
has better path caching performance, as we don't need a bespoke path for when
the line is partially visible.

The second bug was that when the glyph iterator concluded that the
current glyph was out of the viewport, we would immediately terminate
the loop for painting glyphs without painting any buffered glyphs that
had been determined to be visible.

This second bug was easily fixed by ensuring that we always paint all buffered
glyphs when terminating iteration.

As part of this work, I pulled the (fairly complex) logic of buffering and
painting glyphs into the glyph iterator so that label and editor can share
a single implementation.

I was unable to completely encapsulate the array storing buffered glyphs within
the iterator without it being moved to the heap, so the current glyph iteration
API requires the caller to juggle a slice of glyphs. Hopefully someone in
the future can find a structure that the compiler's escape analysis understands.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-14 09:00:31 -06:00
Chris Waldon b7d126e24c font/{gofont,opentype},text,widget{,/material}: [API] add font fallback and bidi support
This commit restructures the entire text shaping stack to enable lines of shaped text to
have non-homogeneous properties like which font face they belong to and which direction
a segment of text is going.

The text package now provides a concrete type text.Shaper which can be used to convert
strings into sequences of renderable text.Glyphs. At a high level, the API is used
like this:

    // Prepare some fonts.
    var collection []text.FontFace
    // Make a shaper with those fonts loaded.
    shaper := text.NewShaper(collection)
    // Shape a string.
    shaper.LayoutString(text.Parameters{
		PxPerEm: fixed.I(12),
    }, 0, 100, system.Locale{}, "Hello")
    // Iterate the glyphs from that string.
    for glyph, ok := shaper.NextGlyph(); ok; glyph, ok = shaper.NextGlyph() {
    	// Convert the glyph data into a path. In real uses, convert batches of glyphs
    	// rather than single glyphs to reduce the number of individual paths and offsets
    	// required to display your text.
    	shape := shaper.Shape([]text.Glyph{glyph})
    	// Offset the glyph to the position it declares within its fields. This will
    	// automatically handle correct bidirectional text glyph positioning.
    	offset := op.Offset(image.Pt(glyph.X.Floor(), int(glyph.Y))).Push(gtx.Ops)
    	// Create a clip area from the shape of the glyph.
    	area := clip.Outline{Path: shape}.Push(gtx.Ops)
    	// Paint whatever the current color is within the glyph's shape.
    	paint.PaintOp{}.Add(gtx.Ops)
    	area.Pop()
        offset.Pop()
    }

This API will transparently handle both font fallback (choosing appropriate fonts
from those loaded when the primary font doesn't contain a required glyph) and
bidirectional text (mixed left-to-right and right-to-left text). Glyphs are
iterated in order of the input runes, not their visual order, but proper use
of the provided offsets will ensure that text always displays correctly.

Thanks to Elias Naur for suggesting this glyph iterator strategy. It let us cut
through a lot of accumulated complexity from trying to match our old text APIs,
meaning that this change actually is a net negative change in lines of code.

This commit consumes the upstream github.com/go-text/typesetting/shaping API
now that my prior work is merged there, removing the need for the font/opentype/internal
package entirely.

As part of my efforts, I fuzzed both the low-level text shaping stack and the
editor widget extensively. I've committed regression tests found that way into
the appropriate testdata files to ensure the fuzzer re-checks them.

Fixes: https://todo.sr.ht/~eliasnaur/gio/425
Fixes: https://todo.sr.ht/~eliasnaur/gio/211
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2022-12-13 22:06:57 -06:00
Elias Naur 513250122c app: [macOS] defer Window destroy to after window close
The windowWillClose callback is too soon to destroy our Window:
at least draw callbacks may be called after windowWillClose but
before the window is gone. This change moves cleanup to the
viewDidMoveToWindow callback where we're sure the NSView is no longer
active.

Fixes: https://todo.sr.ht/~eliasnaur/gio/466
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-12-13 22:06:57 -06:00
Elias Naur 98f098f53f app: [macOS] properly handle middle mouse button up event
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-12-13 22:06:57 -06:00