Commit Graph

28 Commits

Author SHA1 Message Date
Walter Werner SCHNEIDER 83d23ab507 all: sort and group imports
Signed-off-by: Walter Werner SCHNEIDER <contact@schnwalter.eu>
2020-12-17 08:55:09 +01:00
Sebastien Binet 8e7066ecd7 font/opentype: fix typo
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-12-14 15:52:39 +01:00
Sebastien Binet be89f8b945 all: introduce Outline and Stroke builders
This CL introduces 2 new path builders:
- Outline which takes a PathSpec to be outlined
- Stroke which takes a PathSpec and a stroke style, to stroke a path.

typically, code like this:

  var p clip.Path
  ...
  p.Outline().Add(o)

should be replaced with:

  var p clip.Path
  ...
  clip.Outline{Path: p.End()}.Op().Add(o)

similarly, stroking should be modified from:

  var p clip.Path
  ...
  p.Stroke(width, clip.StrokeStyle{...}).Add(o)

to:

  var p clip.Path
  ...
  clip.Stroke{Path: p.End(), Style: clip.StrokeStyle{Width:...}}.Op().Add(o)

here are tentative 'rf' scripts (see rsc.io/rf for more details):

  ```
  ex {
  	import "gioui.org/op";
  	import "gioui.org/op/clip";

  	var p clip.Path;
  	var o *op.Ops;

  	p.Outline().Add(o) -> clip.Outline{Path:p.End()}.Op().Add(o);
  }

  ex {
  	import "gioui.org/op";
  	import "gioui.org/op/clip";

  	var o *op.Ops;
  	var p clip.Path;
  	var sty clip.StrokeStyle;
  	var width float32;

  	p.Stroke(width, sty).Add(o) ->   \
	    clip.Stroke{                 \
		Path:p.End(),            \
		Style: clip.StrokeStyle{ \
		    Width: width,        \
	    }}.Op().Add(o);
  }
  ```

Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-12-09 09:44:15 +01:00
Elias Naur aee87baefe text: represent laid out text as strings to facilitate caching of layouts
Commit https://gioui.org/commit/b331407e81456 added text layout and shaping
based on io.Reader and changed Editor to use it. Unfortunately, as ~inkeliz
discovered, caching of shapes were also lost.

~inkeliz suggested fix,

https://lists.sr.ht/~eliasnaur/gio-patches/patches/15059

adds caching of shapes to Editor to regain lost performance.

This change repairs the cache to work on io.Reader API, in hope that the
already complicated Editor won't need additional caching.

Before this change, text layouts were represented as a slice of (rune, advance)
pairs. Unfortunately, this representation doesn't lend itself to caching of
shaping results, so change the representation of a line of text to be a pair
of text and advances:

	package text

	type Layout {
		Text string
		Advances []fixed.Int26_6
	}

The Text field can then be used in a cache key, assuming Advances is
consistent with it.

The end result is that the two shaper variants of text.Shaper is reduced to
just one, and the Len field field of text.Line is no longer needed.

The changed representation adds a bit of extra work to package opentype.
Cleaning that up is left as a future TODO.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-11-16 16:02:30 +01:00
Sebastien Binet 936eb52b7e all: rename clip.Path.End into clip.Path.Outline
Signed-off-by: Sebastien Binet <s@sbinet.org>
2020-11-10 15:58:07 +01:00
Elias Naur 188bfa9a0b font/opentype: report valid bounds from layoutText for the empty string
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-07-09 20:35:19 +02:00
tainted-bit 6c8dcbdb4b font/opentype: add tests for Collection as a Face
Added tests to make sure that opentype.Collection can be used as a
text.Face, and that it correctly implements fallback behavior for
glyph lookups.

Signed-off-by: tainted-bit <sourcehut@taintedbit.com>
2020-07-08 16:12:03 +02:00
tainted-bit a6afa86d85 font/opentype: support using Collection as a Face
This change allows font collection files (extensions .ttc or .otc)
to be used as a text.Face. These files contain an ordered list of
SFNT fonts, each supporting a maximum of 2^16 glyphs. When used as
a text.Face, each rune in the string to layout or render will be
assigned to the first font with a glyph for that rune, or to the
replacement character from the first font in the file otherwise.

With this change, it is possible to support multiple unicode planes
in a single text.Face by using a Collection with more than one
internal SFNT file. For example, it is now possible to display
characters from the basic multilingual plane and emoji in a single
widget.Label by loading an appropriate OTC file.

Fixes gio#104

Signed-off-by: tainted-bit <sourcehut@taintedbit.com>
2020-07-08 16:11:50 +02:00
Elias Naur 7bbe0da0c7 text,font/opentype: make text layout and shaping safe for concurrent use
Implementations of text.Face are reused across multiple windows for efficiency.
Make the opentype implementation safe for concurrent use and document it.

Updates gio#104

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-07-04 17:55:25 +02:00
Elias Naur 9e3d3b6f58 text,font/gofont: replace text.Collection with slice of FontFaces
A slice of FontFace pairs are simpler, and thread safe in case a client
wants to append or modify the font collection.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-21 21:44:28 +02:00
Elias Naur b07d34354e text,widget/material: make font collections explicit
Before this change, package font implemented a global font registry,
with the usual problems of package global state.

This change deletes the global registry and introduces the text.Collection
type for representing a list of fonts and their faces. Collection exports
Lookup that finds the closest match and its face.

The existing FontRegistry is renamed to Cache to reflect its new limited
functionality: a cache of shapes and measurements on top of a Collection.

Then, material.NewTheme is changed to take a Collection and initialize
a Cache.

Updates gio#19 because multiple windows require a separate (writable) Cache per
window, while (read-only) Collections may be shared.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-07 16:27:43 +02:00
Elias Naur c19ed05342 op: change CallOp to be a return value from MacroOp.Stop
Converting

	macro := op.Record(ops)
	...
	macro.Stop()

	macro.Add()

to

	macro := op.Record(ops)
	...
	call := macro.Stop()

	call.Add(ops)

Which is more general (call.Add can take a different ops than the op.Record
that started it), and enforced the order between Stop and the subsequent Add.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-06-02 12:07:20 +02:00
Elias Naur 4c220f4554 text: simplify text layout and shaping API
First, replace LayoutOptions with an explicit maximum width parameter.  The
single-field option struct doesn't carry its weight, and I don't think we'll
see more global layout options in the future. Rather, I expect options to cover
spans of text or be part of a Font.

Second, replace the unit.Converter with an scaled text size. It's simpler and
allow the Editor and similar widgets to easily detect whether their cached
layouts are stale. Package text no longer depends on package unit, which is
now dealt with at the widget-level only.

Finally, remove the Size field from Font. It was a design mistake: a Font is
assumed to cover all sizes, as evidenced by the FontRegistry disregarding
Size when looking up fonts.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-02-03 23:32:55 +01:00
Elias Naur b331407e81 text: add io.Reader Layout method to Shaper
use them for Editor, which is no longer required to construct a string
for laying out its content.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-01-13 21:38:54 +01:00
Elias Naur 16d2a3ac0a text: remove String, Layout and add Glyph
In preparation for using Shaper with an io.Reader, rework the API to not refer
to strings. In particular, introduce Glyph for holding the rune in addition to
the advance. For fast traversing of the underlying text, add Len to Line with
the UTF8 length.

Layout is a useless wrapper around []Line; remove it while we're
here.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-01-13 19:54:11 +01:00
Elias Naur e25b1639b9 text: make Shaper an interface
And rename out the caching implementation to FontRegistry.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2020-01-13 14:48:31 +01:00
Greg Pomerantz 3dd7c8121c font/opentype: add Collection type and methods
The Collection type wraps sfnt.Collection and allows lazy-loading
of fonts from SFNT collections.

Signed-off-by: Greg Pomerantz <gmp.gio@wow.st>
2019-12-18 19:07:54 +01:00
Elias Naur c9f1f59c40 font/opentype: fix typo
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-12-16 20:06:33 +01:00
Elias Naur 0768fbe590 text: convert clip.Ops to op.CallOp
MacroOp is about to lose the ability to run a different operation list
than the one it was recorded on. Text shape caches rely on that property,
and must use the new CallOp operation added for purpose.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-12-12 00:45:36 +01:00
Werner Laurensse 4bcb4ec8b6 text: add Metrics method to Face interface. font/opentype: implement Metrics method for Font struct.
Signed-off-by: Werner Laurensse <werner@alman.ax>
2019-11-24 20:01:33 +01:00
Elias Naur f3f079df32 font/opentype: treat invalid characters less specially
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-11-20 20:15:31 +01:00
Elias Naur c072a7eb87 font/opentype: fix kerning
Fixes gio#69

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-11-20 19:57:38 +01:00
Elias Naur 682d2810d3 text: remove SingleLine from LayoutOptions
Low level text layout should not deal with filtering newlines.

Updates gio#61

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-11-09 20:01:40 +01:00
Elias Naur e864ac3fc3 op/clip: split clip operations into its own package
Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-11-09 19:07:00 +01:00
Elias Naur 143d2aae95 font/gofont: add explicit Register
Registering the font as a side effect of importing the gofont package
was too magic. Require an explicit Register call instead. As a side
effect, it is more clear which font is the default (the first one
registered).

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-10-23 10:18:11 +02:00
Elias Naur 445d85efe0 font/gofont: add Go font convenience package
To use the Go font in a Gio program, import it:

import _ "gioui.org/font/gofont"

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-10-13 18:23:30 +02:00
Elias Naur edf0d8ef99 font/opentype,text/opentype: move package
Opentype parsing, layout and shaping will be used by subpackages to package
font. Move the opentype package accordingly.

Remove the Must helper function; programs will no longer use the opentype
package in the normal case.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-10-13 18:23:30 +02:00
Elias Naur 40822ef26b font: add a central font registry
Package font holds a singleton text.Shaper for general use. Subpackages
call Register to add fonts to the registry.

The intention is that programs can add a typeface by importing a package:

import _ "gioui.org/font/gofont"

Signed-off-by: Elias Naur <mail@eliasnaur.com>
2019-10-13 18:23:30 +02:00