Commit Graph

34 Commits

Author SHA1 Message Date
Chris Waldon 43c47f0883 go.*,text,font{,/opentype},app,gpu,widget{,/material}: [API] load system fonts
This commit updates the text package to be able to load system fonts. As a consequence,
application authors may choose to provide no fonts manually, and it's
also possible that the system provides none (WASM, for instance, currently provides no
system fonts). As such, the text stack needed some minor tweaks to handle this case by
displaying blank spaces where text should be rather than crashing when no faces are
available.

Internally, we are dropping the old method of choosing faces and instead relying solely
on the new font matching logic in go-text. I chose to do this because maintaining two
different sets of logic with a hierarchical relationship proved to be really complex,
and also the go-text logic seems to produce higher-quality choices.

The breaking API change from this commit is the new way of constructing a text shaper
using text.ShaperOptions. Providing no options will result in a shaper that uses solely
system fonts. The various options can be used to disable system font loading and to
provide an already-parsed collection of fonts as per Gio's old API.

The material.NewTheme function now accepts no arguments instead of a font collection.
Users wanting to provide a collection can simply provide a new shaper configured how
they would like:

    theme := material.NewTheme()
	theme.Shaper = text.NewShaper(text.NoSystemFonts(), text.WithCollection(gofont.Regular()))

This commit touches many packages to fix up their construction of text shapers, mostly in
test code. The changes to the tests in package widget deserve special note:
Changing our font resolution logic caused the tofu characters within the
test strings to use a different font's tofu. This isn't a problem, but shifted
the layout of the shaped text a little bit. I've updated the numbers to expect
the new glyph positions.

Fixes: https://todo.sr.ht/~eliasnaur/gio/309
Fixes: https://todo.sr.ht/~eliasnaur/gio/184
Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
2023-07-19 10:01:51 +02: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
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
Elias Naur bebc73db37 gpu: implement automatic mipmaps for images
All GPU APIs except OpenGL ES 2 can generate mipmaps for textures.
This trades 33% more GPU memory use for improved rendering quality
and speed for downscaled images.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-10-22 16:47:00 -06:00
Elias Naur 61b2e37691 all: format comments with go fmt ./... using Go 1.19
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-08-06 12:26:03 +02:00
Elias Naur b5f12c5f26 f32: [API] unexport Rectangle
There are no public API that uses f32.Rectangle anymore. Move Rectangle
to an internal package for internal use.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Elias Naur 48a8540a68 all: [API] change clip.RRect and UniformRRect to take integer coordinates
Like the change to op.Offset before this, clip.RRect and UniformRRect
is usually used with integer coordinates. Change to integer coordinates
to eliminate many useless conversions to float32.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Elias Naur a63e0cb44a all: [API] change op.Offset to take integer coordinates
op.Offset is a convenience function most often used by layouts. Layouts
usually operate in integer coordinates, and the float32 version of op.Offset
needlessly force conversions from int to float32. This change makes op.Offset
take integer coordinates, to better match its intended use.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-05-31 10:24:09 +02:00
Egon Elbre 3fd231367f internal/stroke: optimize arc drawing
Arc with a small angle doesn't need many segments.

Updates: https://todo.sr.ht/~eliasnaur/gio/313
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2022-03-14 11:42:59 +01:00
Elias Naur ce116abded op/clip: specify path for stroked Rects
Fixes: https://todo.sr.ht/~eliasnaur/gio/358
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2022-02-10 19:01:08 +01:00
Andy Balholm 44c9fb7448 internal/stroke: don't draw round join if angle is NaN
References: https://todo.sr.ht/~eliasnaur/gio/331
Signed-off-by: Andy Balholm <andy@balholm.com>
2022-02-06 11:07:16 +01:00
Pierre Curto 2ce9ad36af clip: check Path ops are not mixed with others
Multiple operations Op, such as clip.Path, cannot
be interleaved with other ops. This patch adds a
mechanism to ensure that is the case by starting
multi ops with ops.BeginMulti and ending them with
ops.EndMulti while operations are written to op.Ops
with ops.WriteMulti.
This mechanism is applied to clip.Path.

Fixes: https://todo.sr.ht/~eliasnaur/gio/336
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2022-01-15 19:16:07 +01:00
Andy Balholm 456f22c986 gpu/internal/rendertest: add regression test for issue 331
References: https://todo.sr.ht/~eliasnaur/gio/331
Signed-off-by: Andy Balholm <andy@balholm.com>
2022-01-12 11:34:37 +01:00
Pierre Curto 11bb86166a op/clip: automatically close Path in Outlines
Unclosed path segments in Path will be automatically
closed by a line.

Fixes: https://todo.sr.ht/~eliasnaur/gio/320
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2021-12-21 10:13:20 +01:00
Pierre Curto af63c089f6 gpu/headless: make Screenshot take an input image to tranfer into
When extracting headless.Window's content via screenshots,
it can be useful to keep reusing the same image for output,
as well as specify which area of the Window is to be
extracted.
The updated Screenshot method does this by using the supplied
image.

API change: users must pass an existing image to Window.Screenshot.

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2021-12-15 16:05:19 +01:00
Pierre Curto 9d05f35341 op/paint: fix subimage rendering by ImageOp
This change is a follow-up to bcf3ff77ff, fixing the two renderers to
properly render images with non-zero origins.

Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2021-11-11 10:13:26 +01:00
Pierre Curto bcf3ff77ff op/paint: remove unnecessary check in ImageOp
Signed-off-by: Pierre Curto <pierre.curto@gmail.com>
2021-11-10 17:31:50 +01:00
Elias Naur aac53ad0b5 gpu: don't alias stroked paths with outline paths
The default renderer caches pre-processed paths for efficient re-use,
but failed to distinguish outline paths from stroke paths.

Fixes gio#294

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-10-20 10:07:22 +02:00
Elias Naur bd1ef92dc4 op/clip: remove complex stroke support
In a discussion with Raph Levien, the author of our compute renderer
implementation, it became clear to me that it's not at all certain that
complex strokes will ever be efficiently supported by a GPU renderer.

At the same time, the machinery for converting a complex stroke to a
GPU-friendly outline has a significant maintenance cost. Further, it is
surprising to users that complex strokes are significantly slower and
allocate memory.

This change removes support for complex strokes, leaving only
round-capped, round-joined strokes supported by the compute renderer.
The default renderer still converts all strokes to outline, but it also
caches the result.

This is an API change. The complex stroke conversion code has been moved
to the external gioui.org/x/stroke package, with a similar API.

Updats gio#282 (Inkeliz brought up the allocation issue)

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-10-08 18:10:47 +02:00
Elias Naur 936c266b03 all: [API] split operation stack into per-state stacks
The op.Save and Load methods exist to support the need for
transformation, clip, pointer area state to behave as stacks. For
example, layout needs to apply an offset to its children but not
subsequent operations.

Before this change, op.Save and Load were used to save and restore the
state:

    ops := new(op.Ops)
    // Save state.
    state := op.Save(ops)
    // Apply offset.
    op.Offset(...).Add(ops)
    // Draw with offset applied.
    draw(ops)
    // Restore state.
    state.Load()

A drawback with the op.Save mechanism is that there is no direct
connection between the state change and the saving and loading of state.
This causes confusion as to when a Save/Load is needed and who is
responsible for performing them, which leads to subtle bugs and over-use
of Save/Loads.

This change gets rid of the general state stack and replaces it with
per-state stacks. There is now a stack for transformation, clip, pointer
areas, and they can only be restored by the code pushing state to them.
The example above now becomes:

    ops := new(op.Ops)
    // Push offset to the transformation stack.
    stack := op.Offset(...).Push(ops)
    // Draw with offset applied.
    draw(ops)
    // Restore state.
    stack.Pop()

For convenience, transformation also be Add'ed if the stack operation is
not required.

Simple state such as the current material no longer has a way to be
restored; it is assumed the client of a PaintOp adds their desired
material operation before it.

API change: replace op.Save/Load with explicit Push/Pop scopes for
op.TransformOps, pointer.AreaOps, clip.Ops.

To ease porting, this change retains a version of op.Save/Load that
saves and restores the transformation and clip stacks. It also retains
an Add method for clip.Op.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-10-08 17:21:56 +02:00
Elias Naur 794b7854ea gpu/internal/rendertest: add missing license headers
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-10-07 17:03:28 +02:00
Elias Naur 3434bf8af1 internal/ops: don't run deferred macros twice
Discovered while debugging #277.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-09-24 15:14:45 +02:00
Elias Naur 9e6ed3cb96 gpu/internal/rendertest: clean up headless context immediately
GPU contexts can hold on to a significant amount of resources. Clean
them up immediately instead of at test cleanup.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-09-20 15:13:00 +02:00
Elias Naur da1c4a60e0 gpu/internal/rendertest: only dump images when -saveimages is set
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-09-20 15:13:00 +02:00
Elias Naur 75c9736708 op/paint: skip zero images
Updates gio#235

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-06-16 12:01:42 +02:00
Elias Naur 2c07b2dc0e headless: clear background to transparent, not white
The clear background is the most useful, and the old behaviour can
be achieved by filling the entire viewport with a white paint.ColorOp.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-05-03 11:31:45 +02:00
Elias Naur 60bab15164 gpu/internal/rendertest: test textured strokes, including clipping
The new compute renderer can draw simple strokes. Test that clipping
works with a stroke basis, and that images can be drawn into strokes.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-03-19 21:46:31 +01:00
Egon Elbre 9cb9e67a8e op/clip: remove Border
Border can be expressed in-terms of clip.Stroke:

  clip.Stroke{
      Path: clip.UniformRRect(r, rr).Ops(ops),
      Style: clip.StrokeStyle{Width: width},
  }.Add(ops)

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2021-03-14 13:28:21 +01:00
Elias Naur c5fb759aef op/clip: make RoundCap and RoundJoin the default stroke style
There's an argument that rounded caps and joins are the simplest stroke,
in that it can be defined to cover all pixels within lineWidth distance
from the supporting path.

However, the more important reason is that the compute renderer natively
supports this stroke style (without dashes), and users that don't care
(much) about the particular stroke style should get the efficient
implementation. A good example is op/clip.Border that strokes a closed
path, where the StrokeCap is irrelevant.

This is a (subtle) API change. If you have code that relies on the
default values of clip.StrokeStyle you may want to set Cap and Join
explicitly. See the test changes for examples. On the other hand, you
will get much better performance from the default Cap and Join values
once the compute renderer becomes the default.

Disablethe TestPaintClippedBorder test; dashes round-capped,
round-joined strokes doesn't seem to work correctly.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-03-12 12:19:39 +01:00
Elias Naur 884e7d27e2 op/clip: don't accept open Paths for Outline
Outline represents a clipping operations that clips all drawing outside
a closed path. Before this change, paths not closed we're patched up by
adding an implicit line from the endpoint to the beginning.

These fixups are inefficient for a rare case, but acceptable because the
old renderer post-processes all paths anyway. However, the new compute
renderer don't need post-processing in most cases, making fixups too
expensive.

Given that clipping to an open path is fundamentally undefined and that
implicit fixup with a closing line segment is merely a way to force the
clip to be well-defined, this change adds a panic to Outline for Paths
that are not closed.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-03-11 08:57:27 +01:00
Egon Elbre 5393a05110 gpu/internal/rendertest: test depth buffer overlaps
Regression test for 44991f355c

Signed-off-by: Egon Elbre <egonelbre@gmail.com>
2021-03-06 15:43:37 +01:00
Elias Naur cb1defbc63 gpu/internal/rendertest: move rendertest package below gpu
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2021-03-06 14:32:43 +01:00