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>
This commit is contained in:
Chris Waldon
2022-10-10 16:44:22 -04:00
committed by Elias Naur
parent 513250122c
commit b7d126e24c
43 changed files with 3523 additions and 3980 deletions
+782
View File
@@ -0,0 +1,782 @@
// SPDX-License-Identifier: Unlicense OR MIT
package text
import (
"io"
"sort"
"github.com/benoitkugler/textlayout/fonts"
"github.com/benoitkugler/textlayout/language"
"github.com/go-text/typesetting/di"
"github.com/go-text/typesetting/font"
"github.com/go-text/typesetting/shaping"
"golang.org/x/exp/slices"
"golang.org/x/image/math/fixed"
"golang.org/x/text/unicode/bidi"
"gioui.org/f32"
"gioui.org/io/system"
"gioui.org/op"
"gioui.org/op/clip"
)
// document holds a collection of shaped lines and alignment information for
// those lines.
type document struct {
lines []line
alignment Alignment
// alignWidth is the width used when aligning text.
alignWidth int
}
// append adds the lines of other to the end of l and ensures they
// are aligned to the same width.
func (l *document) append(other document) {
l.lines = append(l.lines, other.lines...)
l.alignWidth = max(l.alignWidth, other.alignWidth)
calculateYOffsets(l.lines)
}
// reset empties the document in preparation to reuse its memory.
func (l *document) reset() {
l.lines = l.lines[:0]
l.alignment = Start
l.alignWidth = 0
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
// A line contains the measurements of a line of text.
type line struct {
// runs contains sequences of shaped glyphs with common attributes. The order
// of runs is logical, meaning that the first run will contain the glyphs
// corresponding to the first runes of data in the original text.
runs []runLayout
// visualOrder is a slice of indices into Runs that describes the visual positions
// of each run of text. Iterating this slice and accessing Runs at each
// of the values stored in this slice traverses the runs in proper visual
// order from left to right.
visualOrder []int
// width is the width of the line.
width fixed.Int26_6
// ascent is the height above the baseline.
ascent fixed.Int26_6
// descent is the height below the baseline, including
// the line gap.
descent fixed.Int26_6
// bounds is the visible bounds of the line.
bounds fixed.Rectangle26_6
// direction is the dominant direction of the line. This direction will be
// used to align the text content of the line, but may not match the actual
// direction of the runs of text within the line (such as an RTL sentence
// within an LTR paragraph).
direction system.TextDirection
// runeCount is the number of text runes represented by this line's runs.
runeCount int
xOffset fixed.Int26_6
yOffset int
}
// Range describes the position and quantity of a range of text elements
// within a larger slice. The unit is usually runes of unicode data or
// glyphs of shaped font data.
type Range struct {
// Count describes the number of items represented by the Range.
Count int
// Offset describes the start position of the represented
// items within a larger list.
Offset int
}
// glyph contains the metadata needed to render a glyph.
type glyph struct {
// id is this glyph's identifier within the font it was shaped with.
id GlyphID
// clusterIndex is the identifier for the text shaping cluster that
// this glyph is part of.
clusterIndex int
// glyphCount is the number of glyphs in the same cluster as this glyph.
glyphCount int
// runeCount is the quantity of runes in the source text that this glyph
// corresponds to.
runeCount int
// xAdvance and yAdvance describe the distance the dot moves when
// laying out the glyph on the X or Y axis.
xAdvance, yAdvance fixed.Int26_6
// xOffset and yOffset describe offsets from the dot that should be
// applied when rendering the glyph.
xOffset, yOffset fixed.Int26_6
// bounds describes the visual bounding box of the glyph relative to
// its dot.
bounds fixed.Rectangle26_6
}
type runLayout struct {
// VisualPosition describes the relative position of this run of text within
// its line. It should be a valid index into the containing line's VisualOrder
// slice.
VisualPosition int
// X is the visual offset of the dot for the first glyph in this run
// relative to the beginning of the line.
X fixed.Int26_6
// Glyphs are the actual font characters for the text. They are ordered
// from left to right regardless of the text direction of the underlying
// text.
Glyphs []glyph
// Runes describes the position of the text data this layout represents
// within the containing text.Line.
Runes Range
// Advance is the sum of the advances of all clusters in the Layout.
Advance fixed.Int26_6
// PPEM is the pixels-per-em scale used to shape this run.
PPEM fixed.Int26_6
// Direction is the layout direction of the glyphs.
Direction system.TextDirection
// face is the font face that the ID of each Glyph in the Layout refers to.
face font.Face
}
// faceOrderer chooses the order in which faces should be applied to text.
type faceOrderer struct {
def Font
faceScratch []font.Face
fontDefaultOrder map[Font]int
defaultOrderedFonts []Font
faces map[Font]font.Face
faceToIndex map[font.Face]int
fonts []Font
}
func (f *faceOrderer) insert(fnt Font, face font.Face) {
if len(f.fonts) == 0 {
f.def = fnt
}
if f.fontDefaultOrder == nil {
f.fontDefaultOrder = make(map[Font]int)
}
if f.faces == nil {
f.faces = make(map[Font]font.Face)
f.faceToIndex = make(map[font.Face]int)
}
f.fontDefaultOrder[fnt] = len(f.faceScratch)
f.defaultOrderedFonts = append(f.defaultOrderedFonts, fnt)
f.faceScratch = append(f.faceScratch, face)
f.fonts = append(f.fonts, fnt)
f.faces[fnt] = face
f.faceToIndex[face] = f.fontDefaultOrder[fnt]
}
// resetFontOrder restores the fonts to a predictable order. It should be invoked
// before any operation searching the fonts.
func (c *faceOrderer) resetFontOrder() {
copy(c.fonts, c.defaultOrderedFonts)
}
func (c *faceOrderer) indexFor(face font.Face) int {
return c.faceToIndex[face]
}
func (c *faceOrderer) faceFor(idx int) font.Face {
if idx < len(c.defaultOrderedFonts) {
return c.faces[c.defaultOrderedFonts[idx]]
}
panic("face index not found")
}
// TODO(whereswaldon): this function could sort all faces by appropriateness for the
// given font characteristics. This would ensure that (if possible) text using a
// fallback font would select similar weights and emphases to the primary font.
func (c *faceOrderer) sortedFacesForStyle(font Font) []font.Face {
c.resetFontOrder()
primary, ok := c.fontForStyle(font)
if !ok {
font.Typeface = c.def.Typeface
primary, ok = c.fontForStyle(font)
if !ok {
primary = c.def
}
}
return c.sorted(primary)
}
// fontForStyle returns the closest existing font to the requested font within the
// same typeface.
func (c *faceOrderer) fontForStyle(font Font) (Font, bool) {
if closest, ok := closestFont(font, c.fonts); ok {
return closest, true
}
font.Style = Regular
if closest, ok := closestFont(font, c.fonts); ok {
return closest, true
}
return font, false
}
// faces returns a slice of faces with primary as the first element and
// the remaining faces ordered by insertion order.
func (f *faceOrderer) sorted(primary Font) []font.Face {
sort.Slice(f.fonts, func(i, j int) bool {
if f.fonts[i] == primary {
return true
}
a := f.fonts[i]
b := f.fonts[j]
return f.fontDefaultOrder[a] < f.fontDefaultOrder[b]
})
for i, font := range f.fonts {
f.faceScratch[i] = f.faces[font]
}
return f.faceScratch
}
// shaperImpl implements the shaping and line-wrapping of opentype fonts.
type shaperImpl struct {
// Fields for tracking fonts/faces.
orderer faceOrderer
// Shaping and wrapping state.
shaper shaping.HarfbuzzShaper
wrapper shaping.LineWrapper
bidiParagraph bidi.Paragraph
// Scratch buffers used to avoid re-allocating slices during routine internal
// shaping operations.
splitScratch1, splitScratch2 []shaping.Input
outScratchBuf []shaping.Output
scratchRunes []rune
}
// Load registers the provided FontFace with the shaper, if it is compatible.
// It returns whether the face is now available for use. FontFaces are prioritized
// in the order in which they are loaded, with the first face being the default.
func (s *shaperImpl) Load(f FontFace) {
s.orderer.insert(f.Font, f.Face.Face())
}
// splitByScript divides the inputs into new, smaller inputs on script boundaries
// and correctly sets the text direction per-script. It will
// use buf as the backing memory for the returned slice if buf is non-nil.
func splitByScript(inputs []shaping.Input, documentDir di.Direction, buf []shaping.Input) []shaping.Input {
var splitInputs []shaping.Input
if buf == nil {
splitInputs = make([]shaping.Input, 0, len(inputs))
} else {
splitInputs = buf
}
for _, input := range inputs {
currentInput := input
if input.RunStart == input.RunEnd {
return []shaping.Input{input}
}
firstNonCommonRune := input.RunStart
for i := firstNonCommonRune; i < input.RunEnd; i++ {
if language.LookupScript(input.Text[i]) != language.Common {
firstNonCommonRune = i
break
}
}
currentInput.Script = language.LookupScript(input.Text[firstNonCommonRune])
for i := firstNonCommonRune + 1; i < input.RunEnd; i++ {
r := input.Text[i]
runeScript := language.LookupScript(r)
if runeScript == language.Common || runeScript == currentInput.Script {
continue
}
if i != input.RunStart {
currentInput.RunEnd = i
splitInputs = append(splitInputs, currentInput)
}
currentInput = input
currentInput.RunStart = i
currentInput.Script = runeScript
// In the future, it may make sense to try to guess the language of the text here as well,
// but this is a complex process.
}
// close and add the last input
currentInput.RunEnd = input.RunEnd
splitInputs = append(splitInputs, currentInput)
}
return splitInputs
}
func (s *shaperImpl) splitBidi(input shaping.Input) []shaping.Input {
var splitInputs []shaping.Input
if input.Direction.Axis() != di.Horizontal || input.RunStart == input.RunEnd {
return []shaping.Input{input}
}
def := bidi.LeftToRight
if input.Direction.Progression() == di.TowardTopLeft {
def = bidi.RightToLeft
}
s.bidiParagraph.SetString(string(input.Text), bidi.DefaultDirection(def))
out, err := s.bidiParagraph.Order()
if err != nil {
return []shaping.Input{input}
}
for i := 0; i < out.NumRuns(); i++ {
currentInput := input
run := out.Run(i)
dir := run.Direction()
_, endRune := run.Pos()
currentInput.RunEnd = endRune + 1
if dir == bidi.RightToLeft {
currentInput.Direction = di.DirectionRTL
} else {
currentInput.Direction = di.DirectionLTR
}
splitInputs = append(splitInputs, currentInput)
input.RunStart = currentInput.RunEnd
}
return splitInputs
}
// splitByFaces divides the inputs by font coverage in the provided faces. It will use the slice provided in buf
// as the backing storage of the returned slice if buf is non-nil.
func (s *shaperImpl) splitByFaces(inputs []shaping.Input, faces []font.Face, buf []shaping.Input) []shaping.Input {
var split []shaping.Input
if buf == nil {
split = make([]shaping.Input, 0, len(inputs))
} else {
split = buf
}
for _, input := range inputs {
split = append(split, shaping.SplitByFontGlyphs(input, faces)...)
}
return split
}
// shapeText invokes the text shaper and returns the raw text data in the shaper's native
// format. It does not wrap lines.
func (s *shaperImpl) shapeText(faces []font.Face, ppem fixed.Int26_6, lc system.Locale, txt []rune) []shaping.Output {
if len(faces) < 1 {
return nil
}
lcfg := langConfig{
Language: language.NewLanguage(lc.Language),
Direction: mapDirection(lc.Direction),
}
// Create an initial input.
input := toInput(faces[0], ppem, lcfg, txt)
// Break input on font glyph coverage.
inputs := s.splitBidi(input)
inputs = s.splitByFaces(inputs, faces, s.splitScratch1[:0])
inputs = splitByScript(inputs, lcfg.Direction, s.splitScratch2[:0])
// Shape all inputs.
if needed := len(inputs) - len(s.outScratchBuf); needed > 0 {
s.outScratchBuf = slices.Grow(s.outScratchBuf, needed)
}
s.outScratchBuf = s.outScratchBuf[:len(inputs)]
for i := range inputs {
s.outScratchBuf[i] = s.shaper.Shape(inputs[i])
}
return s.outScratchBuf
}
// shapeAndWrapText invokes the text shaper and returns wrapped lines in the shaper's native format.
func (s *shaperImpl) shapeAndWrapText(faces []font.Face, ppem fixed.Int26_6, maxWidth int, lc system.Locale, txt []rune) []shaping.Line {
// Wrap outputs into lines.
return s.wrapper.WrapParagraph(maxWidth, txt, s.shapeText(faces, ppem, lc, txt)...)
}
// replaceControlCharacters replaces problematic unicode
// code points with spaces to ensure proper rune accounting.
func replaceControlCharacters(in []rune) []rune {
for i, r := range in {
switch r {
// ASCII File separator.
case '\u001C':
// ASCII Group separator.
case '\u001D':
// ASCII Record separator.
case '\u001E':
case '\r':
case '\n':
// Unicode "next line" character.
case '\u0085':
// Unicode "paragraph separator".
case '\u2029':
default:
continue
}
in[i] = ' '
}
return in
}
// Layout shapes and wraps the text, and returns the result in Gio's shaped text format.
func (s *shaperImpl) LayoutString(params Parameters, minWidth, maxWidth int, lc system.Locale, txt string) document {
return s.LayoutRunes(params, minWidth, maxWidth, lc, []rune(txt))
}
// Layout shapes and wraps the text, and returns the result in Gio's shaped text format.
func (s *shaperImpl) Layout(params Parameters, minWidth, maxWidth int, lc system.Locale, txt io.RuneReader) document {
s.scratchRunes = s.scratchRunes[:0]
for r, _, err := txt.ReadRune(); err != nil; r, _, err = txt.ReadRune() {
s.scratchRunes = append(s.scratchRunes, r)
}
return s.LayoutRunes(params, minWidth, maxWidth, lc, s.scratchRunes)
}
func calculateYOffsets(lines []line) {
currentY := 0
prevDesc := fixed.I(0)
for i := range lines {
ascent, descent := lines[i].ascent, lines[i].descent
currentY += (prevDesc + ascent).Ceil()
lines[i].yOffset = currentY
prevDesc = descent
}
}
// LayoutRunes shapes and wraps the text, and returns the result in Gio's shaped text format.
func (s *shaperImpl) LayoutRunes(params Parameters, minWidth, maxWidth int, lc system.Locale, txt []rune) document {
hasNewline := len(txt) > 0 && txt[len(txt)-1] == '\n'
if hasNewline {
txt = txt[:len(txt)-1]
}
ls := s.shapeAndWrapText(s.orderer.sortedFacesForStyle(params.Font), params.PxPerEm, maxWidth, lc, replaceControlCharacters(txt))
// Convert to Lines.
textLines := make([]line, len(ls))
for i := range ls {
otLine := toLine(&s.orderer, ls[i], lc.Direction)
if i == len(ls)-1 && hasNewline {
// If there was a trailing newline update the rune counts to include
// it on the last line of the paragraph.
finalRunIdx := len(otLine.runs) - 1
otLine.runeCount += 1
otLine.runs[finalRunIdx].Runes.Count += 1
syntheticGlyph := glyph{
id: 0,
clusterIndex: len(txt),
glyphCount: 0,
runeCount: 1,
xAdvance: 0,
yAdvance: 0,
xOffset: 0,
yOffset: 0,
}
// Inset the synthetic newline glyph on the proper end of the run.
if otLine.runs[finalRunIdx].Direction.Progression() == system.FromOrigin {
otLine.runs[finalRunIdx].Glyphs = append(otLine.runs[finalRunIdx].Glyphs, syntheticGlyph)
} else {
// Ensure capacity.
otLine.runs[finalRunIdx].Glyphs = append(otLine.runs[finalRunIdx].Glyphs, glyph{})
copy(otLine.runs[finalRunIdx].Glyphs[1:], otLine.runs[finalRunIdx].Glyphs)
otLine.runs[finalRunIdx].Glyphs[0] = syntheticGlyph
}
}
textLines[i] = otLine
}
alignWidth := maxWidth
if len(textLines) == 1 {
alignWidth = max(minWidth, textLines[0].width.Ceil())
}
calculateYOffsets(textLines)
return document{
lines: textLines,
alignment: params.Alignment,
alignWidth: alignWidth,
}
}
// Shape converts the provided glyphs into a path.
func (s *shaperImpl) Shape(ops *op.Ops, gs []Glyph) clip.PathSpec {
var lastPos f32.Point
var x fixed.Int26_6
var builder clip.Path
builder.Begin(ops)
for i, g := range gs {
if i == 0 {
x = g.X
}
ppem, faceIdx, gid := splitGlyphID(g.ID)
face := s.orderer.faceFor(faceIdx)
ppemInt := ppem.Round()
ppem16 := uint16(ppemInt)
scaleFactor := float32(ppemInt) / float32(face.Upem())
outline, ok := face.GlyphData(gid, ppem16, ppem16).(fonts.GlyphOutline)
if !ok {
continue
}
// Move to glyph position.
pos := f32.Point{
X: float32(g.X-x)/64 - float32(g.Offset.X)/64,
Y: -float32(g.Offset.Y) / 64,
}
builder.Move(pos.Sub(lastPos))
lastPos = pos
var lastArg f32.Point
// Convert fonts.Segments to relative segments.
for _, fseg := range outline.Segments {
nargs := 1
switch fseg.Op {
case fonts.SegmentOpQuadTo:
nargs = 2
case fonts.SegmentOpCubeTo:
nargs = 3
}
var args [3]f32.Point
for i := 0; i < nargs; i++ {
a := f32.Point{
X: fseg.Args[i].X * scaleFactor,
Y: -fseg.Args[i].Y * scaleFactor,
}
args[i] = a.Sub(lastArg)
if i == nargs-1 {
lastArg = a
}
}
switch fseg.Op {
case fonts.SegmentOpMoveTo:
builder.Move(args[0])
case fonts.SegmentOpLineTo:
builder.Line(args[0])
case fonts.SegmentOpQuadTo:
builder.Quad(args[0], args[1])
case fonts.SegmentOpCubeTo:
builder.Cube(args[0], args[1], args[2])
default:
panic("unsupported segment op")
}
}
lastPos = lastPos.Add(lastArg)
}
return builder.End()
}
// langConfig describes the language and writing system of a body of text.
type langConfig struct {
// Language the text is written in.
language.Language
// Writing system used to represent the text.
language.Script
// Direction of the text, usually driven by the writing system.
di.Direction
}
// toInput converts its parameters into a shaping.Input.
func toInput(face font.Face, ppem fixed.Int26_6, lc langConfig, runes []rune) shaping.Input {
var input shaping.Input
input.Direction = lc.Direction
input.Text = runes
input.Size = ppem
input.Face = face
input.Language = lc.Language
input.Script = lc.Script
input.RunStart = 0
input.RunEnd = len(runes)
return input
}
func mapDirection(d system.TextDirection) di.Direction {
switch d {
case system.LTR:
return di.DirectionLTR
case system.RTL:
return di.DirectionRTL
}
return di.DirectionLTR
}
func unmapDirection(d di.Direction) system.TextDirection {
switch d {
case di.DirectionLTR:
return system.LTR
case di.DirectionRTL:
return system.RTL
}
return system.LTR
}
// toGioGlyphs converts text shaper glyphs into the minimal representation
// that Gio needs.
func toGioGlyphs(in []shaping.Glyph, ppem fixed.Int26_6, faceIdx int) []glyph {
out := make([]glyph, 0, len(in))
for _, g := range in {
// To better understand how to calculate the bounding box, see here:
// https://freetype.org/freetype2/docs/glyphs/glyph-metrics-3.svg
var bounds fixed.Rectangle26_6
bounds.Min.X = g.XBearing
bounds.Min.Y = -g.YBearing
bounds.Max = bounds.Min.Add(fixed.Point26_6{X: g.Width, Y: -g.Height})
out = append(out, glyph{
id: newGlyphID(ppem, faceIdx, g.GlyphID),
clusterIndex: g.ClusterIndex,
runeCount: g.RuneCount,
glyphCount: g.GlyphCount,
xAdvance: g.XAdvance,
yAdvance: g.YAdvance,
xOffset: g.XOffset,
yOffset: g.YOffset,
bounds: bounds,
})
}
return out
}
// toLine converts the output into a Line with the provided dominant text direction.
func toLine(orderer *faceOrderer, o shaping.Line, dir system.TextDirection) line {
if len(o) < 1 {
return line{}
}
line := line{
runs: make([]runLayout, len(o)),
direction: dir,
}
for i := range o {
run := o[i]
line.runs[i] = runLayout{
Glyphs: toGioGlyphs(run.Glyphs, run.Size, orderer.indexFor(run.Face)),
Runes: Range{
Count: run.Runes.Count,
Offset: line.runeCount,
},
Direction: unmapDirection(run.Direction),
face: run.Face,
Advance: run.Advance,
PPEM: run.Size,
}
line.runeCount += run.Runes.Count
if line.bounds.Min.Y > -run.LineBounds.Ascent {
line.bounds.Min.Y = -run.LineBounds.Ascent
}
if line.bounds.Max.Y < -run.LineBounds.Ascent+run.LineBounds.LineHeight() {
line.bounds.Max.Y = -run.LineBounds.Ascent + run.LineBounds.LineHeight()
}
line.bounds.Max.X += run.Advance
line.width += run.Advance
if line.ascent < run.LineBounds.Ascent {
line.ascent = run.LineBounds.Ascent
}
if line.descent < -run.LineBounds.Descent+run.LineBounds.Gap {
line.descent = -run.LineBounds.Descent + run.LineBounds.Gap
}
}
computeVisualOrder(&line)
// Account for glyphs hanging off of either side in the bounds.
if len(line.visualOrder) > 0 {
runIdx := line.visualOrder[0]
run := o[runIdx]
if len(run.Glyphs) > 0 {
line.bounds.Min.X = run.Glyphs[0].LeftSideBearing()
}
runIdx = line.visualOrder[len(line.visualOrder)-1]
run = o[runIdx]
if len(run.Glyphs) > 0 {
lastGlyphIdx := len(run.Glyphs) - 1
line.bounds.Max.X += run.Glyphs[lastGlyphIdx].RightSideBearing()
}
}
return line
}
// computeVisualOrder will populate the Line's VisualOrder field and the
// VisualPosition field of each element in Runs.
func computeVisualOrder(l *line) {
l.visualOrder = make([]int, len(l.runs))
const none = -1
bidiRangeStart := none
// visPos returns the visual position for an individual logically-indexed
// run in this line, taking only the line's overall text direction into
// account.
visPos := func(logicalIndex int) int {
if l.direction.Progression() == system.TowardOrigin {
return len(l.runs) - 1 - logicalIndex
}
return logicalIndex
}
// resolveBidi populated the line's VisualOrder fields for the elements in the
// half-open range [bidiRangeStart:bidiRangeEnd) indicating that those elements
// should be displayed in reverse-visual order.
resolveBidi := func(bidiRangeStart, bidiRangeEnd int) {
firstVisual := bidiRangeEnd - 1
// Just found the end of a bidi range.
for startIdx := bidiRangeStart; startIdx < bidiRangeEnd; startIdx++ {
pos := visPos(firstVisual)
l.runs[startIdx].VisualPosition = pos
l.visualOrder[pos] = startIdx
firstVisual--
}
bidiRangeStart = none
}
for runIdx, run := range l.runs {
if run.Direction.Progression() != l.direction.Progression() {
if bidiRangeStart == none {
bidiRangeStart = runIdx
}
continue
} else if bidiRangeStart != none {
// Just found the end of a bidi range.
resolveBidi(bidiRangeStart, runIdx)
bidiRangeStart = none
}
pos := visPos(runIdx)
l.runs[runIdx].VisualPosition = pos
l.visualOrder[pos] = runIdx
}
if bidiRangeStart != none {
// We ended iteration within a bidi segment, resolve it.
resolveBidi(bidiRangeStart, len(l.runs))
}
// Iterate and resolve the X of each run.
x := fixed.Int26_6(0)
for _, runIdx := range l.visualOrder {
l.runs[runIdx].X = x
x += l.runs[runIdx].Advance
}
}
// closestFont returns the closest Font in available by weight.
// In case of equality the lighter weight will be returned.
func closestFont(lookup Font, available []Font) (Font, bool) {
found := false
var match Font
for _, cf := range available {
if cf == lookup {
return lookup, true
}
if cf.Typeface != lookup.Typeface || cf.Variant != lookup.Variant || cf.Style != lookup.Style {
continue
}
if !found {
found = true
match = cf
continue
}
cDist := weightDistance(lookup.Weight, cf.Weight)
mDist := weightDistance(lookup.Weight, match.Weight)
if cDist < mDist {
match = cf
} else if cDist == mDist && cf.Weight < match.Weight {
match = cf
}
}
return match, found
}
// weightDistance returns the distance value between two font weights.
func weightDistance(wa Weight, wb Weight) int {
// Avoid dealing with negative Weight values.
a := int(wa) + 400
b := int(wb) + 400
diff := a - b
if diff < 0 {
return -diff
}
return diff
}
+650
View File
@@ -0,0 +1,650 @@
package text
import (
"math"
"reflect"
"testing"
nsareg "eliasnaur.com/font/noto/sans/arabic/regular"
"github.com/go-text/typesetting/shaping"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/math/fixed"
"gioui.org/font/opentype"
"gioui.org/io/system"
)
var english = system.Locale{
Language: "EN",
Direction: system.LTR,
}
var arabic = system.Locale{
Language: "AR",
Direction: system.RTL,
}
func testShaper(faces ...Face) *shaperImpl {
shaper := shaperImpl{}
for _, face := range faces {
shaper.Load(FontFace{Face: face})
}
return &shaper
}
func TestEmptyString(t *testing.T) {
ppem := fixed.I(200)
ltrFace, _ := opentype.Parse(goregular.TTF)
shaper := testShaper(ltrFace)
lines := shaper.LayoutRunes(Parameters{PxPerEm: ppem}, 0, 2000, english, []rune{})
if len(lines.lines) == 0 {
t.Fatalf("Layout returned no lines for empty string; expected 1")
}
l := lines.lines[0]
exp := fixed.Rectangle26_6{
Min: fixed.Point26_6{
Y: fixed.Int26_6(-12094),
},
Max: fixed.Point26_6{
Y: fixed.Int26_6(2700),
},
}
if got := l.bounds; got != exp {
t.Errorf("got bounds %+v for empty string; expected %+v", got, exp)
}
}
// TestNewlineSynthesis ensures that the shaper correctly inserts synthetic glyphs
// representing newline runes.
func TestNewlineSynthesis(t *testing.T) {
ppem := fixed.I(10)
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
shaper := testShaper(ltrFace, rtlFace)
type testcase struct {
name string
locale system.Locale
txt string
}
for _, tc := range []testcase{
{
name: "ltr bidi newline in rtl segment",
locale: english,
txt: "The quick سماء שלום لا fox تمط שלום\n",
},
{
name: "ltr bidi newline in ltr segment",
locale: english,
txt: "The quick سماء שלום لا fox\n",
},
{
name: "rtl bidi newline in ltr segment",
locale: arabic,
txt: "الحب سماء brown привет fox تمط jumps\n",
},
{
name: "rtl bidi newline in rtl segment",
locale: arabic,
txt: "الحب سماء brown привет fox تمط\n",
},
} {
t.Run(tc.name, func(t *testing.T) {
doc := shaper.LayoutRunes(Parameters{PxPerEm: ppem}, 0, 200, tc.locale, []rune(tc.txt))
for lineIdx, line := range doc.lines {
lastRunIdx := len(line.runs) - 1
lastRun := line.runs[lastRunIdx]
lastGlyphIdx := len(lastRun.Glyphs) - 1
if lastRun.Direction.Progression() == system.TowardOrigin {
lastGlyphIdx = 0
}
glyph := lastRun.Glyphs[lastGlyphIdx]
if glyph.glyphCount != 0 {
t.Errorf("expected synthetic newline on line %d, run %d, glyph %d", lineIdx, lastRunIdx, lastGlyphIdx)
}
for runIdx, run := range line.runs {
for glyphIdx, glyph := range run.Glyphs {
if runIdx == lastRunIdx && glyphIdx == lastGlyphIdx {
continue
}
if glyph.glyphCount == 0 {
t.Errorf("found invalid synthetic newline on line %d, run %d, glyph %d", lineIdx, runIdx, glyphIdx)
}
}
}
}
if t.Failed() {
printLinePositioning(t, doc.lines, nil)
}
})
}
}
// simpleGlyph returns a simple square glyph with the provided cluster
// value.
func simpleGlyph(cluster int) shaping.Glyph {
return complexGlyph(cluster, 1, 1)
}
// ligatureGlyph returns a simple square glyph with the provided cluster
// value and number of runes.
func ligatureGlyph(cluster, runes int) shaping.Glyph {
return complexGlyph(cluster, runes, 1)
}
// expansionGlyph returns a simple square glyph with the provided cluster
// value and number of glyphs.
func expansionGlyph(cluster, glyphs int) shaping.Glyph {
return complexGlyph(cluster, 1, glyphs)
}
// complexGlyph returns a simple square glyph with the provided cluster
// value, number of associated runes, and number of glyphs in the cluster.
func complexGlyph(cluster, runes, glyphs int) shaping.Glyph {
return shaping.Glyph{
Width: fixed.I(10),
Height: fixed.I(10),
XAdvance: fixed.I(10),
YAdvance: fixed.I(10),
YBearing: fixed.I(10),
ClusterIndex: cluster,
GlyphCount: glyphs,
RuneCount: runes,
}
}
// makeTestText creates a simple and complex(bidi) sample of shaped text at the given
// font size and wrapped to the given line width. The runeLimit, if nonzero,
// truncates the sample text to ensure shorter output for expensive tests.
func makeTestText(shaper *shaperImpl, primaryDir system.TextDirection, fontSize, lineWidth, runeLimit int) (simpleSample, complexSample []shaping.Line) {
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
if shaper == nil {
shaper = testShaper(ltrFace, rtlFace)
}
ltrSource := "The quick brown fox jumps over the lazy dog."
rtlSource := "الحب سماء لا تمط غير الأحلام"
// bidiSource is crafted to contain multiple consecutive RTL runs (by
// changing scripts within the RTL).
bidiSource := "The quick سماء שלום لا fox تمط שלום غير the lazy dog."
// bidi2Source is crafted to contain multiple consecutive LTR runs (by
// changing scripts within the LTR).
bidi2Source := "الحب سماء brown привет fox تمط jumps привет over غير الأحلام"
locale := english
simpleSource := ltrSource
complexSource := bidiSource
if primaryDir == system.RTL {
simpleSource = rtlSource
complexSource = bidi2Source
locale = arabic
}
if runeLimit != 0 {
simpleRunes := []rune(simpleSource)
complexRunes := []rune(complexSource)
if runeLimit < len(simpleRunes) {
ltrSource = string(simpleRunes[:runeLimit])
}
if runeLimit < len(complexRunes) {
rtlSource = string(complexRunes[:runeLimit])
}
}
simpleText := shaper.shapeAndWrapText(shaper.orderer.sortedFacesForStyle(Font{}), fixed.I(fontSize), lineWidth, locale, []rune(simpleSource))
complexText := shaper.shapeAndWrapText(shaper.orderer.sortedFacesForStyle(Font{}), fixed.I(fontSize), lineWidth, locale, []rune(complexSource))
shaper = testShaper(rtlFace, ltrFace)
return simpleText, complexText
}
func fixedAbs(a fixed.Int26_6) fixed.Int26_6 {
if a < 0 {
a = -a
}
return a
}
func TestToLine(t *testing.T) {
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
shaper := testShaper(ltrFace, rtlFace)
ltr, bidi := makeTestText(shaper, system.LTR, 16, 100, 0)
rtl, bidi2 := makeTestText(shaper, system.RTL, 16, 100, 0)
_, bidiWide := makeTestText(shaper, system.LTR, 16, 200, 0)
_, bidi2Wide := makeTestText(shaper, system.RTL, 16, 200, 0)
type testcase struct {
name string
lines []shaping.Line
// Dominant text direction.
dir system.TextDirection
}
for _, tc := range []testcase{
{
name: "ltr",
lines: ltr,
dir: system.LTR,
},
{
name: "rtl",
lines: rtl,
dir: system.RTL,
},
{
name: "bidi",
lines: bidi,
dir: system.LTR,
},
{
name: "bidi2",
lines: bidi2,
dir: system.RTL,
},
{
name: "bidi_wide",
lines: bidiWide,
dir: system.LTR,
},
{
name: "bidi2_wide",
lines: bidi2Wide,
dir: system.RTL,
},
} {
t.Run(tc.name, func(t *testing.T) {
// We expect:
// - Line dimensions to be populated.
// - Line direction to be populated.
// - Runs to be ordered from lowest runes first.
// - Runs to have widths matching the input.
// - Runs to have the same total number of glyphs/runes as the input.
runesSeen := Range{}
shaper := testShaper(ltrFace, rtlFace)
for i, input := range tc.lines {
seenRun := make([]bool, len(input))
inputLowestRuneOffset := math.MaxInt
totalInputGlyphs := 0
totalInputRunes := 0
for _, run := range input {
if run.Runes.Offset < inputLowestRuneOffset {
inputLowestRuneOffset = run.Runes.Offset
}
totalInputGlyphs += len(run.Glyphs)
totalInputRunes += run.Runes.Count
}
output := toLine(&shaper.orderer, input, tc.dir)
if output.bounds.Min == (fixed.Point26_6{}) {
t.Errorf("line %d: Bounds.Min not populated", i)
}
if output.bounds.Max == (fixed.Point26_6{}) {
t.Errorf("line %d: Bounds.Max not populated", i)
}
if output.direction != tc.dir {
t.Errorf("line %d: expected direction %v, got %v", i, tc.dir, output.direction)
}
totalRunWidth := fixed.I(0)
totalLineGlyphs := 0
totalLineRunes := 0
for k, run := range output.runs {
seenRun[run.VisualPosition] = true
if output.visualOrder[run.VisualPosition] != k {
t.Errorf("line %d, run %d: run.VisualPosition=%d, but line.VisualOrder[%d]=%d(should be %d)", i, k, run.VisualPosition, run.VisualPosition, output.visualOrder[run.VisualPosition], k)
}
if run.Runes.Offset != totalLineRunes {
t.Errorf("line %d, run %d: expected Runes.Offset to be %d, got %d", i, k, totalLineRunes, run.Runes.Offset)
}
runGlyphCount := len(run.Glyphs)
if inputGlyphs := len(input[k].Glyphs); runGlyphCount != inputGlyphs {
t.Errorf("line %d, run %d: expected %d glyphs, found %d", i, k, inputGlyphs, runGlyphCount)
}
runRuneCount := 0
currentCluster := -1
for _, g := range run.Glyphs {
if g.clusterIndex != currentCluster {
runRuneCount += g.runeCount
currentCluster = g.clusterIndex
}
}
if run.Runes.Count != runRuneCount {
t.Errorf("line %d, run %d: expected %d runes, counted %d", i, k, run.Runes.Count, runRuneCount)
}
runesSeen.Count += run.Runes.Count
totalRunWidth += fixedAbs(run.Advance)
totalLineGlyphs += len(run.Glyphs)
totalLineRunes += run.Runes.Count
}
if output.runeCount != totalInputRunes {
t.Errorf("line %d: input had %d runes, only counted %d", i, totalInputRunes, output.runeCount)
}
if totalLineGlyphs != totalInputGlyphs {
t.Errorf("line %d: input had %d glyphs, only counted %d", i, totalInputRunes, totalLineGlyphs)
}
if totalRunWidth != output.width {
t.Errorf("line %d: expected width %d, got %d", i, totalRunWidth, output.width)
}
for runIndex, seen := range seenRun {
if !seen {
t.Errorf("line %d, run %d missing from runs VisualPosition fields", i, runIndex)
}
}
}
lastLine := tc.lines[len(tc.lines)-1]
maxRunes := 0
for _, run := range lastLine {
if run.Runes.Count+run.Runes.Offset > maxRunes {
maxRunes = run.Runes.Count + run.Runes.Offset
}
}
if runesSeen.Count != maxRunes {
t.Errorf("input covered %d runes, output only covers %d", maxRunes, runesSeen.Count)
}
})
}
}
func TestComputeVisualOrder(t *testing.T) {
type testcase struct {
name string
input line
expectedVisualOrder []int
}
for _, tc := range []testcase{
{
name: "ltr",
input: line{
direction: system.LTR,
runs: []runLayout{
{Direction: system.LTR},
{Direction: system.LTR},
{Direction: system.LTR},
},
},
expectedVisualOrder: []int{0, 1, 2},
},
{
name: "rtl",
input: line{
direction: system.RTL,
runs: []runLayout{
{Direction: system.RTL},
{Direction: system.RTL},
{Direction: system.RTL},
},
},
expectedVisualOrder: []int{2, 1, 0},
},
{
name: "bidi-ltr",
input: line{
direction: system.LTR,
runs: []runLayout{
{Direction: system.LTR},
{Direction: system.RTL},
{Direction: system.RTL},
{Direction: system.RTL},
{Direction: system.LTR},
},
},
expectedVisualOrder: []int{0, 3, 2, 1, 4},
},
{
name: "bidi-ltr-complex",
input: line{
direction: system.LTR,
runs: []runLayout{
{Direction: system.RTL},
{Direction: system.RTL},
{Direction: system.LTR},
{Direction: system.RTL},
{Direction: system.RTL},
{Direction: system.LTR},
{Direction: system.RTL},
{Direction: system.RTL},
{Direction: system.LTR},
{Direction: system.RTL},
{Direction: system.RTL},
},
},
expectedVisualOrder: []int{1, 0, 2, 4, 3, 5, 7, 6, 8, 10, 9},
},
{
name: "bidi-rtl",
input: line{
direction: system.RTL,
runs: []runLayout{
{Direction: system.RTL},
{Direction: system.LTR},
{Direction: system.LTR},
{Direction: system.LTR},
{Direction: system.RTL},
},
},
expectedVisualOrder: []int{4, 1, 2, 3, 0},
},
{
name: "bidi-rtl-complex",
input: line{
direction: system.RTL,
runs: []runLayout{
{Direction: system.LTR},
{Direction: system.LTR},
{Direction: system.RTL},
{Direction: system.LTR},
{Direction: system.LTR},
{Direction: system.RTL},
{Direction: system.LTR},
{Direction: system.LTR},
{Direction: system.RTL},
{Direction: system.LTR},
{Direction: system.LTR},
},
},
expectedVisualOrder: []int{9, 10, 8, 6, 7, 5, 3, 4, 2, 0, 1},
},
} {
t.Run(tc.name, func(t *testing.T) {
computeVisualOrder(&tc.input)
if !reflect.DeepEqual(tc.input.visualOrder, tc.expectedVisualOrder) {
t.Errorf("expected visual order %v, got %v", tc.expectedVisualOrder, tc.input.visualOrder)
}
for i, visualIndex := range tc.input.visualOrder {
if pos := tc.input.runs[visualIndex].VisualPosition; pos != i {
t.Errorf("line.VisualOrder[%d]=%d, but line.Runs[%d].VisualPosition=%d", i, visualIndex, visualIndex, pos)
}
}
})
}
}
func FuzzLayout(f *testing.F) {
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
f.Add("د عرمثال dstي met لم aqل جدmوpمg lرe dرd لو عل ميrةsdiduntut lab renنيتذدagلaaiua.ئPocttأior رادرsاي mيrbلmnonaيdتد ماةعcلخ.", true, uint8(10), uint16(200))
shaper := testShaper(ltrFace, rtlFace)
f.Fuzz(func(t *testing.T, txt string, rtl bool, fontSize uint8, width uint16) {
locale := system.Locale{
Direction: system.LTR,
}
if rtl {
locale.Direction = system.RTL
}
if fontSize < 1 {
fontSize = 1
}
lines := shaper.LayoutRunes(Parameters{PxPerEm: fixed.I(int(fontSize))}, 0, int(width), locale, []rune(txt))
validateLines(t, lines.lines, len([]rune(txt)))
})
}
func validateLines(t *testing.T, lines []line, expectedRuneCount int) {
t.Helper()
runesSeen := 0
for i, line := range lines {
if line.bounds.Min == (fixed.Point26_6{}) {
t.Errorf("line %d: Bounds.Min not populated", i)
}
if line.bounds.Max == (fixed.Point26_6{}) {
t.Errorf("line %d: Bounds.Max not populated", i)
}
totalRunWidth := fixed.I(0)
totalLineGlyphs := 0
lineRunesSeen := 0
for k, run := range line.runs {
if line.visualOrder[run.VisualPosition] != k {
t.Errorf("line %d, run %d: run.VisualPosition=%d, but line.VisualOrder[%d]=%d(should be %d)", i, k, run.VisualPosition, run.VisualPosition, line.visualOrder[run.VisualPosition], k)
}
if run.Runes.Offset != lineRunesSeen {
t.Errorf("line %d, run %d: expected Runes.Offset to be %d, got %d", i, k, lineRunesSeen, run.Runes.Offset)
}
runRuneCount := 0
currentCluster := -1
for _, g := range run.Glyphs {
if g.clusterIndex != currentCluster {
runRuneCount += g.runeCount
currentCluster = g.clusterIndex
}
}
if run.Runes.Count != runRuneCount {
t.Errorf("line %d, run %d: expected %d runes, counted %d", i, k, run.Runes.Count, runRuneCount)
}
lineRunesSeen += run.Runes.Count
totalRunWidth += fixedAbs(run.Advance)
totalLineGlyphs += len(run.Glyphs)
}
if totalRunWidth != line.width {
t.Errorf("line %d: expected width %d, got %d", i, line.width, totalRunWidth)
}
runesSeen += lineRunesSeen
}
if runesSeen != expectedRuneCount {
t.Errorf("input covered %d runes, output only covers %d", expectedRuneCount, runesSeen)
}
}
// TestTextAppend ensures that appending two texts together correctly updates the new lines'
// y offsets.
func TestTextAppend(t *testing.T) {
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
shaper := testShaper(ltrFace, rtlFace)
text1 := shaper.LayoutString(Parameters{
PxPerEm: fixed.I(14),
}, 0, 200, english, "د عرمثال dstي met لم aqل جدmوpمg lرe dرd لو عل ميrةsdiduntut lab renنيتذدagلaaiua.ئPocttأior رادرsاي mيrbلmnonaيdتد ماةعcلخ.")
text2 := shaper.LayoutString(Parameters{
PxPerEm: fixed.I(14),
}, 0, 200, english, "د عرمثال dstي met لم aqل جدmوpمg lرe dرd لو عل ميrةsdiduntut lab renنيتذدagلaaiua.ئPocttأior رادرsاي mيrbلmnonaيdتد ماةعcلخ.")
text1.append(text2)
curY := math.MinInt
for lineNum, line := range text1.lines {
yOff := line.yOffset
if yOff <= curY {
t.Errorf("lines[%d] has y offset %d, <= to previous %d", lineNum, yOff, curY)
}
curY = yOff
}
}
func TestClosestFontByWeight(t *testing.T) {
const (
testTF1 Typeface = "MockFace"
testTF2 Typeface = "TestFace"
testTF3 Typeface = "AnotherFace"
)
fonts := []Font{
{Typeface: testTF1, Style: Regular, Weight: Normal},
{Typeface: testTF1, Style: Regular, Weight: Light},
{Typeface: testTF1, Style: Regular, Weight: Bold},
{Typeface: testTF1, Style: Italic, Weight: Thin},
}
weightOnlyTests := []struct {
Lookup Weight
Expected Weight
}{
// Test for existing weights.
{Lookup: Normal, Expected: Normal},
{Lookup: Light, Expected: Light},
{Lookup: Bold, Expected: Bold},
// Test for missing weights.
{Lookup: Thin, Expected: Light},
{Lookup: ExtraLight, Expected: Light},
{Lookup: Medium, Expected: Normal},
{Lookup: SemiBold, Expected: Bold},
{Lookup: ExtraBlack, Expected: Bold},
}
for _, test := range weightOnlyTests {
got, ok := closestFont(Font{Typeface: testTF1, Weight: test.Lookup}, fonts)
if !ok {
t.Errorf("expected closest font for %v to exist", test.Lookup)
}
if got.Weight != test.Expected {
t.Errorf("got weight %v, expected %v", got.Weight, test.Expected)
}
}
fonts = []Font{
{Typeface: testTF1, Style: Regular, Weight: Light},
{Typeface: testTF1, Style: Regular, Weight: Bold},
{Typeface: testTF1, Style: Italic, Weight: Normal},
{Typeface: testTF3, Style: Italic, Weight: Bold},
}
otherTests := []struct {
Lookup Font
Expected Font
ExpectedToFail bool
}{
// Test for existing fonts.
{
Lookup: Font{Typeface: testTF1, Weight: Light},
Expected: Font{Typeface: testTF1, Style: Regular, Weight: Light},
},
{
Lookup: Font{Typeface: testTF1, Style: Italic, Weight: Normal},
Expected: Font{Typeface: testTF1, Style: Italic, Weight: Normal},
},
// Test for missing fonts.
{
Lookup: Font{Typeface: testTF1, Weight: Normal},
Expected: Font{Typeface: testTF1, Style: Regular, Weight: Light},
},
{
Lookup: Font{Typeface: testTF3, Style: Italic, Weight: Normal},
Expected: Font{Typeface: testTF3, Style: Italic, Weight: Bold},
},
{
Lookup: Font{Typeface: testTF1, Style: Italic, Weight: Thin},
Expected: Font{Typeface: testTF1, Style: Italic, Weight: Normal},
},
{
Lookup: Font{Typeface: testTF1, Style: Italic, Weight: Bold},
Expected: Font{Typeface: testTF1, Style: Italic, Weight: Normal},
},
{
Lookup: Font{Typeface: testTF2, Weight: Normal},
ExpectedToFail: true,
},
{
Lookup: Font{Typeface: testTF2, Style: Italic, Weight: Normal},
ExpectedToFail: true,
},
}
for _, test := range otherTests {
got, ok := closestFont(test.Lookup, fonts)
if test.ExpectedToFail {
if ok {
t.Errorf("expected closest font for %v to not exist", test.Lookup)
} else {
continue
}
}
if !ok {
t.Errorf("expected closest font for %v to exist", test.Lookup)
}
if got != test.Expected {
t.Errorf("got %v, expected %v", got, test.Expected)
}
}
}
+69 -26
View File
@@ -3,9 +3,11 @@
package text
import (
"encoding/binary"
"hash/maphash"
"gioui.org/io/system"
"gioui.org/op/clip"
"github.com/benoitkugler/textlayout/fonts"
"golang.org/x/image/math/fixed"
)
@@ -15,47 +17,54 @@ type layoutCache struct {
}
type pathCache struct {
m map[pathKey]*path
seed maphash.Seed
m map[uint64]*path
head, tail *path
}
type layoutElem struct {
next, prev *layoutElem
key layoutKey
layout []Line
layout document
}
type path struct {
next, prev *path
key pathKey
key uint64
val clip.PathSpec
gids []fonts.GID
glyphs []glyphInfo
}
type glyphInfo struct {
ID GlyphID
X fixed.Int26_6
}
type layoutKey struct {
ppem fixed.Int26_6
maxWidth int
str string
locale system.Locale
ppem fixed.Int26_6
maxWidth, minWidth int
maxLines int
str string
locale system.Locale
font Font
}
type pathKey struct {
ppem fixed.Int26_6
gidHash uint64
}
const maxSize = 1000
func (l *layoutCache) Get(k layoutKey) ([]Line, bool) {
func (l *layoutCache) Get(k layoutKey) (document, bool) {
if lt, ok := l.m[k]; ok {
l.remove(lt)
l.insert(lt)
return lt.layout, true
}
return nil, false
return document{}, false
}
func (l *layoutCache) Put(k layoutKey, lt []Line) {
func (l *layoutCache) Put(k layoutKey, lt document) {
if l.m == nil {
l.m = make(map[layoutKey]*layoutElem)
l.head = new(layoutElem)
@@ -85,20 +94,49 @@ func (l *layoutCache) insert(lt *layoutElem) {
lt.next.prev = lt
}
func gidsMatch(gids []fonts.GID, l Layout) bool {
if len(gids) != len(l.Glyphs) {
// hashGlyphs computes a hash key based on the ID and X offset of
// every glyph in the slice.
func (c *pathCache) hashGlyphs(gs []Glyph) uint64 {
if c.seed == (maphash.Seed{}) {
c.seed = maphash.MakeSeed()
}
var h maphash.Hash
h.SetSeed(c.seed)
var b [8]byte
firstX := fixed.Int26_6(0)
for i, g := range gs {
if i == 0 {
firstX = g.X
}
// Cache glyph X offsets relative to the first glyph.
binary.LittleEndian.PutUint32(b[:4], uint32(g.X-firstX))
h.Write(b[:4])
binary.LittleEndian.PutUint64(b[:], uint64(g.ID))
h.Write(b[:])
}
sum := h.Sum64()
return sum
}
func gidsEqual(a []glyphInfo, glyphs []Glyph) bool {
if len(a) != len(glyphs) {
return false
}
for i := range gids {
if gids[i] != l.Glyphs[i].ID {
firstX := fixed.Int26_6(0)
for i := range a {
if i == 0 {
firstX = glyphs[i].X
}
// Cache glyph X offsets relative to the first glyph.
if a[i].ID != glyphs[i].ID || a[i].X != (glyphs[i].X-firstX) {
return false
}
}
return true
}
func (c *pathCache) Get(k pathKey, l Layout) (clip.PathSpec, bool) {
if v, ok := c.m[k]; ok && gidsMatch(v.gids, l) {
func (c *pathCache) Get(key uint64, gs []Glyph) (clip.PathSpec, bool) {
if v, ok := c.m[key]; ok && gidsEqual(v.glyphs, gs) {
c.remove(v)
c.insert(v)
return v.val, true
@@ -106,20 +144,25 @@ func (c *pathCache) Get(k pathKey, l Layout) (clip.PathSpec, bool) {
return clip.PathSpec{}, false
}
func (c *pathCache) Put(k pathKey, l Layout, v clip.PathSpec) {
func (c *pathCache) Put(key uint64, glyphs []Glyph, v clip.PathSpec) {
if c.m == nil {
c.m = make(map[pathKey]*path)
c.m = make(map[uint64]*path)
c.head = new(path)
c.tail = new(path)
c.head.prev = c.tail
c.tail.next = c.head
}
gids := make([]fonts.GID, len(l.Glyphs))
for i := range l.Glyphs {
gids[i] = l.Glyphs[i].ID
gids := make([]glyphInfo, len(glyphs))
firstX := fixed.I(0)
for i, glyph := range glyphs {
if i == 0 {
firstX = glyph.X
}
// Cache glyph X offsets relative to the first glyph.
gids[i] = glyphInfo{ID: glyph.ID, X: glyph.X - firstX}
}
val := &path{key: k, val: v, gids: gids}
c.m[k] = val
val := &path{key: key, val: v, glyphs: gids}
c.m[key] = val
c.insert(val)
if len(c.m) > maxSize {
oldest := c.tail.next
+4 -3
View File
@@ -12,7 +12,7 @@ import (
func TestLayoutLRU(t *testing.T) {
c := new(layoutCache)
put := func(i int) {
c.Put(layoutKey{str: strconv.Itoa(i)}, nil)
c.Put(layoutKey{str: strconv.Itoa(i)}, document{})
}
get := func(i int) bool {
_, ok := c.Get(layoutKey{str: strconv.Itoa(i)})
@@ -23,11 +23,12 @@ func TestLayoutLRU(t *testing.T) {
func TestPathLRU(t *testing.T) {
c := new(pathCache)
shaped := []Glyph{{ID: 1}}
put := func(i int) {
c.Put(pathKey{gidHash: uint64(i)}, Layout{Runes: Range{Count: i}}, clip.PathSpec{})
c.Put(uint64(i), shaped, clip.PathSpec{})
}
get := func(i int) bool {
_, ok := c.Get(pathKey{gidHash: uint64(i)}, Layout{Runes: Range{Count: i}})
_, ok := c.Get(uint64(i), shaped)
return ok
}
testLRU(t, put, get)
+377 -147
View File
@@ -3,25 +3,29 @@
package text
import (
"encoding/binary"
"hash/maphash"
"fmt"
"io"
"strings"
"golang.org/x/image/math/fixed"
"unicode/utf8"
"gioui.org/io/system"
"gioui.org/op"
"gioui.org/op/clip"
"github.com/go-text/typesetting/font"
"golang.org/x/image/math/fixed"
)
// Shaper implements layout and shaping of text.
type Shaper interface {
// Layout a text according to a set of options.
Layout(font Font, size fixed.Int26_6, maxWidth int, lc system.Locale, txt io.RuneReader) ([]Line, error)
// LayoutString is Layout for strings.
LayoutString(font Font, size fixed.Int26_6, maxWidth int, lc system.Locale, str string) []Line
// Shape a line of text and return a clipping operation for its outline.
Shape(font Font, size fixed.Int26_6, layout Layout) clip.PathSpec
// Parameters are static text shaping attributes applied to the entire shaped text.
type Parameters struct {
// Font describes the preferred typeface.
Font Font
// Alignment characterizes the positioning of text within the line. It does not directly
// impact shaping, but is provided in order to allow efficient offset computation.
Alignment Alignment
// PxPerEm is the pixels-per-em to shape the text with.
PxPerEm fixed.Int26_6
// MaxLines limits the quantity of shaped lines. Zero means no limit.
MaxLines int
}
// A FontFace is a Font and a matching Face.
@@ -30,152 +34,378 @@ type FontFace struct {
Face Face
}
// Cache implements cached layout and shaping of text from a set of
// registered fonts.
// Glyph describes a shaped font glyph. Many fields are distances relative
// to the "dot", which is a point on the baseline (the line upon which glyphs
// visually rest) for the line of text containing the glyph.
//
// If a font matches no registered shape, Cache falls back to the
// first registered face.
// Glyphs are organized into "glyph clusters," which are sequences that
// may represent an arbitrary number of runes.
//
// The LayoutString and ShapeString results are cached and re-used if
// possible.
type Cache struct {
def Typeface
faces map[Font]*faceCache
// Sequences of glyph clusters that share style parameters are grouped into "runs."
//
// "Document coordinates" are pixel values relative to the text's origin at (0,0)
// in the upper-left corner" Displaying each shaped glyph at the document
// coordinates of its dot will correctly visualize the text.
type Glyph struct {
// ID is a unique, per-shaper identifier for the shape of the glyph.
// Glyphs from the same shaper will share an ID when they are from
// the same face and represent the same glyph at the same size.
ID GlyphID
// X is the x coordinate of the dot for this glyph in document coordinates.
X fixed.Int26_6
// Y is the y coordinate of the dot for this glyph in document coordinates.
Y int32
// Advance is the logical width of the glyph. The glyph may be visually
// wider than this.
Advance fixed.Int26_6
// Ascent is the distance from the dot to the logical top of glyphs in
// this glyph's face. The specific glyph may be shorter than this.
Ascent fixed.Int26_6
// Descent is the distance from the dot to the logical bottom of glyphs
// in this glyph's face. The specific glyph may descend less than this.
Descent fixed.Int26_6
// Offset encodes the origin of the drawing coordinate space for this glyph
// relative to the dot. This value is used when converting glyphs to paths.
Offset fixed.Point26_6
// Bounds encodes the visual dimensions of the glyph relative to the dot.
Bounds fixed.Rectangle26_6
// Runes is the number of runes represented by the glyph cluster this glyph
// belongs to. If Flags does not contain FlagClusterBreak, this value will
// always be zero. The final glyph in the cluster contains the runes count
// for the entire cluster.
Runes byte
// Flags encode special properties of this glyph.
Flags Flags
}
type faceCache struct {
face Face
layoutCache layoutCache
type Flags uint16
const (
// FlagTowardOrigin is set for glyphs in runs that flow
// towards the origin (RTL).
FlagTowardOrigin Flags = 1 << iota
// FlagLineBreak is set for the last glyph in a line.
FlagLineBreak
// FlagRunBreak is set for the last glyph in a run. A run is a sequence of
// glyphs sharing constant style properties (same size, same face, same
// direction, etc...).
FlagRunBreak
// FlagClusterBreak is set for the last glyph in a glyph cluster. A glyph cluster is a
// sequence of glyphs which are logically a single unit, but require multiple
// symbols from a font to display.
FlagClusterBreak
// FlagSynthetic indicates that the glyph cluster does not represent actual
// font glyphs, but was inserted by the shaper to represent line-breaking
// whitespace characters.
FlagSynthetic
)
func (f Flags) String() string {
var b strings.Builder
if f&FlagSynthetic > 0 {
b.WriteString("S")
} else {
b.WriteString("_")
}
if f&FlagTowardOrigin > 0 {
b.WriteString("T")
} else {
b.WriteString("_")
}
if f&FlagLineBreak > 0 {
b.WriteString("L")
} else {
b.WriteString("_")
}
if f&FlagRunBreak > 0 {
b.WriteString("R")
} else {
b.WriteString("_")
}
if f&FlagClusterBreak > 0 {
b.WriteString("C")
} else {
b.WriteString("_")
}
return b.String()
}
type GlyphID uint64
// Shaper converts strings of text into glyphs that can be displayed.
type Shaper struct {
shaper shaperImpl
pathCache pathCache
seed maphash.Seed
layoutCache layoutCache
paragraph []rune
reader strings.Reader
// Iterator state.
txt document
line int
run int
glyph int
// advance is the width of glyphs from the current run that have already been displayed.
advance fixed.Int26_6
// done tracks whether iteration is over.
done bool
err error
}
func (c *Cache) lookup(font Font) *faceCache {
f := c.faceForStyle(font)
if f == nil {
font.Typeface = c.def
f = c.faceForStyle(font)
// NewShaper constructs a shaper with the provided collection of font faces
// available.
func NewShaper(collection []FontFace) *Shaper {
l := &Shaper{}
for _, f := range collection {
l.shaper.Load(f)
}
return f
}
func (c *Cache) faceForStyle(font Font) *faceCache {
if closest, ok := c.closestFont(font); ok {
return c.faces[closest]
}
font.Style = Regular
if closest, ok := c.closestFont(font); ok {
return c.faces[closest]
}
return nil
}
// closestFont returns the closest Font by weight, in case of equality the
// lighter weight will be returned.
func (c *Cache) closestFont(lookup Font) (Font, bool) {
if c.faces[lookup] != nil {
return lookup, true
}
found := false
var match Font
for cf := range c.faces {
if cf.Typeface != lookup.Typeface || cf.Variant != lookup.Variant || cf.Style != lookup.Style {
continue
}
if !found {
found = true
match = cf
continue
}
cDist := weightDistance(lookup.Weight, cf.Weight)
mDist := weightDistance(lookup.Weight, match.Weight)
if cDist < mDist {
match = cf
} else if cDist == mDist && cf.Weight < match.Weight {
match = cf
}
}
return match, found
}
func NewCache(collection []FontFace) *Cache {
c := &Cache{
faces: make(map[Font]*faceCache),
}
for i, ff := range collection {
if i == 0 {
c.def = ff.Font.Typeface
}
c.faces[ff.Font] = &faceCache{face: ff.Face}
}
return c
}
// Layout implements the Shaper interface.
func (c *Cache) Layout(font Font, size fixed.Int26_6, maxWidth int, lc system.Locale, txt io.RuneReader) ([]Line, error) {
cache := c.lookup(font)
return cache.face.Layout(size, maxWidth, lc, txt)
}
// LayoutString is a caching implementation of the Shaper interface.
func (c *Cache) LayoutString(font Font, size fixed.Int26_6, maxWidth int, lc system.Locale, str string) []Line {
cache := c.lookup(font)
return cache.layout(size, maxWidth, lc, str)
}
// Shape is a caching implementation of the Shaper interface. Shape assumes that the layout
// argument is unchanged from a call to Layout or LayoutString.
func (c *Cache) Shape(font Font, size fixed.Int26_6, layout Layout) clip.PathSpec {
cache := c.lookup(font)
return cache.shape(size, layout)
}
func (f *faceCache) layout(ppem fixed.Int26_6, maxWidth int, lc system.Locale, str string) []Line {
if f == nil {
return nil
}
lk := layoutKey{
ppem: ppem,
maxWidth: maxWidth,
str: str,
locale: lc,
}
if l, ok := f.layoutCache.Get(lk); ok {
return l
}
l, _ := f.face.Layout(ppem, maxWidth, lc, strings.NewReader(str))
f.layoutCache.Put(lk, l)
return l
}
// hashGIDs returns a 64-bit hash value of the font GIDs contained
// within the provided layout.
func (f *faceCache) hashGIDs(layout Layout) uint64 {
if f.seed == (maphash.Seed{}) {
f.seed = maphash.MakeSeed()
}
var h maphash.Hash
h.SetSeed(f.seed)
var b [4]byte
for _, g := range layout.Glyphs {
binary.LittleEndian.PutUint32(b[:], uint32(g.ID))
h.Write(b[:])
}
return h.Sum64()
// Layout a text according to a set of options. Results can be retrieved by
// iteratively calling NextGlyph.
func (l *Shaper) Layout(params Parameters, minWidth, maxWidth int, lc system.Locale, txt io.RuneReader) {
l.layoutText(params, minWidth, maxWidth, lc, txt, "")
}
func (f *faceCache) shape(ppem fixed.Int26_6, layout Layout) clip.PathSpec {
if f == nil {
return clip.PathSpec{}
}
pk := pathKey{
ppem: ppem,
gidHash: f.hashGIDs(layout),
}
if clip, ok := f.pathCache.Get(pk, layout); ok {
return clip
}
clip := f.face.Shape(ppem, layout)
f.pathCache.Put(pk, layout, clip)
return clip
// LayoutString is Layout for strings.
func (l *Shaper) LayoutString(params Parameters, minWidth, maxWidth int, lc system.Locale, str string) {
l.layoutText(params, minWidth, maxWidth, lc, nil, str)
}
func (l *Shaper) reset(align Alignment) {
l.line, l.run, l.glyph, l.advance = 0, 0, 0, 0
l.done = false
l.txt.reset()
l.txt.alignment = align
}
// layoutText lays out a large text document by breaking it into paragraphs and laying
// out each of them separately. This allows the shaping results to be cached independently
// by paragraph. Only one of txt and str should be provided.
func (l *Shaper) layoutText(params Parameters, minWidth, maxWidth int, lc system.Locale, txt io.RuneReader, str string) {
l.reset(params.Alignment)
if txt == nil && len(str) == 0 {
l.txt.append(l.layoutParagraph(params, minWidth, maxWidth, lc, "", nil))
return
}
var done bool
var startByte int
var endByte int
for !done {
var runes int
l.paragraph = l.paragraph[:0]
if txt != nil {
for r, _, re := txt.ReadRune(); !done; r, _, re = txt.ReadRune() {
if re != nil {
done = true
continue
}
l.paragraph = append(l.paragraph, r)
runes++
if r == '\n' {
break
}
}
} else {
for endByte = startByte; endByte < len(str); {
r, width := utf8.DecodeRuneInString(str[endByte:])
endByte += width
runes++
if r == '\n' {
break
}
}
done = endByte == len(str)
}
l.txt.append(l.layoutParagraph(params, minWidth, maxWidth, lc, str[startByte:endByte], l.paragraph))
if done {
return
}
startByte = endByte
}
}
func (l *Shaper) layoutParagraph(params Parameters, minWidth, maxWidth int, lc system.Locale, asStr string, asRunes []rune) document {
if l == nil {
return document{}
}
if len(asStr) == 0 && len(asRunes) > 0 {
asStr = string(asRunes)
}
// Alignment is not part of the cache key because changing it does not impact shaping.
lk := layoutKey{
ppem: params.PxPerEm,
maxWidth: maxWidth,
minWidth: minWidth,
maxLines: params.MaxLines,
str: asStr,
locale: lc,
font: params.Font,
}
if l, ok := l.layoutCache.Get(lk); ok {
return l
}
if len(asRunes) == 0 && len(asStr) > 0 {
asRunes = []rune(asStr)
}
lines := l.shaper.LayoutRunes(params, minWidth, maxWidth, lc, asRunes)
l.layoutCache.Put(lk, lines)
return lines
}
// NextGlyph returns the next glyph from the most recent shaping operation, if
// any. If there are no more glyphs, ok will be false.
func (l *Shaper) NextGlyph() (_ Glyph, ok bool) {
if l.done {
return Glyph{}, false
}
for {
if l.line == len(l.txt.lines) {
if l.err == nil {
l.err = io.EOF
}
return Glyph{}, false
}
line := l.txt.lines[l.line]
if l.run == len(line.runs) {
l.line++
l.run = 0
continue
}
run := line.runs[l.run]
align := l.txt.alignment.Align(line.direction, line.width, l.txt.alignWidth)
if l.line == 0 && l.run == 0 && len(run.Glyphs) == 0 {
// The very first run is empty, which will only happen when the
// entire text is a shaped empty string. Return a single synthetic
// glyph to provide ascent/descent information to the caller.
l.done = true
return Glyph{
X: align,
Y: int32(line.yOffset),
Runes: 0,
Flags: FlagLineBreak | FlagClusterBreak | FlagRunBreak | FlagSynthetic,
Ascent: line.ascent,
Descent: line.descent,
}, true
}
if l.glyph == len(run.Glyphs) {
l.run++
l.glyph = 0
l.advance = 0
continue
}
glyphIdx := l.glyph
rtl := run.Direction.Progression() == system.TowardOrigin
if rtl {
// If RTL, traverse glyphs backwards to ensure rune order.
glyphIdx = len(run.Glyphs) - 1 - glyphIdx
}
g := run.Glyphs[glyphIdx]
if rtl {
// Modify the advance prior to computing runOffset to ensure that the
// current glyph's width is subtracted in RTL.
l.advance += g.xAdvance
}
// runOffset computes how far into the run the dot should be positioned.
runOffset := l.advance
if rtl {
runOffset = run.Advance - l.advance
}
glyph := Glyph{
ID: g.id,
X: align + line.xOffset + run.X + runOffset,
Y: int32(line.yOffset),
Ascent: line.ascent,
Descent: line.descent,
Advance: g.xAdvance,
Runes: byte(g.runeCount),
Offset: fixed.Point26_6{
X: g.xOffset,
Y: g.yOffset,
},
Bounds: g.bounds,
}
l.glyph++
if !rtl {
l.advance += g.xAdvance
}
endOfRun := l.glyph == len(run.Glyphs)
if endOfRun {
glyph.Flags |= FlagRunBreak
}
endOfLine := endOfRun && l.run == len(line.runs)-1
if endOfLine {
glyph.Flags |= FlagLineBreak
}
nextGlyph := l.glyph
if rtl {
nextGlyph = len(run.Glyphs) - 1 - nextGlyph
}
endOfCluster := endOfRun || run.Glyphs[nextGlyph].clusterIndex != g.clusterIndex
if endOfCluster {
glyph.Flags |= FlagClusterBreak
} else {
glyph.Runes = 0
}
if run.Direction.Progression() == system.TowardOrigin {
glyph.Flags |= FlagTowardOrigin
}
if g.glyphCount == 0 {
glyph.Flags |= FlagSynthetic
}
return glyph, true
}
}
const (
facebits = 16
sizebits = 16
gidbits = 64 - facebits - sizebits
)
// newGlyphID encodes a face and a glyph id into a GlyphID.
func newGlyphID(ppem fixed.Int26_6, faceIdx int, gid font.GID) GlyphID {
if gid&^((1<<gidbits)-1) != 0 {
fmt.Println(gid)
panic("glyph id out of bounds")
}
if faceIdx&^((1<<facebits)-1) != 0 {
panic("face index out of bounds")
}
if ppem&^((1<<sizebits)-1) != 0 {
panic("ppem out of bounds")
}
// Mask off the upper 16 bits of ppem. This still allows values up to
// 1023.
ppem &= ((1 << sizebits) - 1)
return GlyphID(faceIdx)<<(gidbits+sizebits) | GlyphID(ppem)<<(gidbits) | GlyphID(gid)
}
// splitGlyphID is the opposite of newGlyphID.
func splitGlyphID(g GlyphID) (fixed.Int26_6, int, font.GID) {
faceIdx := int(g) >> (gidbits + sizebits)
ppem := fixed.Int26_6((g & ((1<<sizebits - 1) << gidbits)) >> gidbits)
gid := font.GID(g) & (1<<gidbits - 1)
return ppem, faceIdx, gid
}
// Shape converts a slice of glyphs into a path describing their collective
// shape. All glyphs are expected to be from a single line of text (their
// Y offsets are ignored).
func (l *Shaper) Shape(gs []Glyph) clip.PathSpec {
key := l.pathCache.hashGlyphs(gs)
shape, ok := l.pathCache.Get(key, gs)
if ok {
return shape
}
ops := new(op.Ops)
shape = l.shaper.Shape(ops, gs)
l.pathCache.Put(key, gs, shape)
return shape
}
+189 -104
View File
@@ -2,117 +2,202 @@ package text
import (
"testing"
nsareg "eliasnaur.com/font/noto/sans/arabic/regular"
"gioui.org/font/opentype"
"gioui.org/io/system"
"golang.org/x/image/font/gofont/goregular"
"golang.org/x/image/math/fixed"
)
var (
testTF1 Typeface = "MockFace"
testTF2 Typeface = "TestFace"
testTF3 Typeface = "AnotherFace"
)
// TestCacheEmptyString ensures that shaping the empty string returns a
// single synthetic glyph with ascent/descent info.
func TestCacheEmptyString(t *testing.T) {
ltrFace, _ := opentype.Parse(goregular.TTF)
collection := []FontFace{{Face: ltrFace}}
cache := NewShaper(collection)
cache.LayoutString(Parameters{
Alignment: Middle,
PxPerEm: fixed.I(10),
}, 200, 200, english, "")
glyphs := make([]Glyph, 0, 1)
for g, ok := cache.NextGlyph(); ok; g, ok = cache.NextGlyph() {
glyphs = append(glyphs, g)
}
if len(glyphs) != 1 {
t.Errorf("expected %d glyphs, got %d", 1, len(glyphs))
}
glyph := glyphs[0]
checkFlag(t, true, FlagClusterBreak, glyph, 0)
checkFlag(t, true, FlagRunBreak, glyph, 0)
checkFlag(t, true, FlagLineBreak, glyph, 0)
checkFlag(t, true, FlagSynthetic, glyph, 0)
if glyph.Ascent == 0 {
t.Errorf("expected non-zero ascent")
}
if glyph.Descent == 0 {
t.Errorf("expected non-zero descent")
}
if glyph.Y == 0 {
t.Errorf("expected non-zero y offset")
}
if glyph.X == 0 {
t.Errorf("expected non-zero x offset")
}
}
func TestClosestFontByWeight(t *testing.T) {
c := newTestCache(
Font{Style: Regular, Weight: Normal},
Font{Style: Regular, Weight: Light},
Font{Style: Regular, Weight: Bold},
Font{Style: Italic, Weight: Thin},
)
weightOnlyTests := []struct {
Lookup Weight
Expected Weight
}{
// Test for existing weights.
{Lookup: Normal, Expected: Normal},
{Lookup: Light, Expected: Light},
{Lookup: Bold, Expected: Bold},
// Test for missing weights.
{Lookup: Thin, Expected: Light},
{Lookup: ExtraLight, Expected: Light},
{Lookup: Medium, Expected: Normal},
{Lookup: SemiBold, Expected: Bold},
{Lookup: ExtraBlack, Expected: Bold},
// TestCacheAlignment ensures that shaping with different alignments or dominant
// text directions results in different X offsets.
func TestCacheAlignment(t *testing.T) {
ltrFace, _ := opentype.Parse(goregular.TTF)
collection := []FontFace{{Face: ltrFace}}
cache := NewShaper(collection)
params := Parameters{Alignment: Start, PxPerEm: fixed.I(10)}
cache.LayoutString(params, 200, 200, english, "A")
glyph, _ := cache.NextGlyph()
startX := glyph.X
params.Alignment = Middle
cache.LayoutString(params, 200, 200, english, "A")
glyph, _ = cache.NextGlyph()
middleX := glyph.X
params.Alignment = End
cache.LayoutString(params, 200, 200, english, "A")
glyph, _ = cache.NextGlyph()
endX := glyph.X
if startX == middleX || startX == endX || endX == middleX {
t.Errorf("[LTR] shaping with with different alignments should not produce the same X, start %d, middle %d, end %d", startX, middleX, endX)
}
for _, test := range weightOnlyTests {
got, ok := c.closestFont(Font{Typeface: testTF1, Weight: test.Lookup})
if !ok {
t.Fatalf("expected closest font for %v to exist", test.Lookup)
}
if got.Weight != test.Expected {
t.Fatalf("got weight %v, expected %v", got.Weight, test.Expected)
}
params.Alignment = Start
cache.LayoutString(params, 200, 200, arabic, "A")
glyph, _ = cache.NextGlyph()
rtlStartX := glyph.X
params.Alignment = Middle
cache.LayoutString(params, 200, 200, arabic, "A")
glyph, _ = cache.NextGlyph()
rtlMiddleX := glyph.X
params.Alignment = End
cache.LayoutString(params, 200, 200, arabic, "A")
glyph, _ = cache.NextGlyph()
rtlEndX := glyph.X
if rtlStartX == rtlMiddleX || rtlStartX == rtlEndX || rtlEndX == rtlMiddleX {
t.Errorf("[RTL] shaping with with different alignments should not produce the same X, start %d, middle %d, end %d", rtlStartX, rtlMiddleX, rtlEndX)
}
c = newTestCache(
Font{Style: Regular, Weight: Light},
Font{Style: Regular, Weight: Bold},
Font{Style: Italic, Weight: Normal},
Font{Typeface: testTF3, Style: Italic, Weight: Bold},
)
otherTests := []struct {
Lookup Font
Expected Font
ExpectedToFail bool
}{
// Test for existing fonts.
{
Lookup: Font{Typeface: testTF1, Weight: Light},
Expected: Font{Typeface: testTF1, Style: Regular, Weight: Light},
},
{
Lookup: Font{Typeface: testTF1, Style: Italic, Weight: Normal},
Expected: Font{Typeface: testTF1, Style: Italic, Weight: Normal},
},
// Test for missing fonts.
{
Lookup: Font{Typeface: testTF1, Weight: Normal},
Expected: Font{Typeface: testTF1, Style: Regular, Weight: Light},
},
{
Lookup: Font{Typeface: testTF3, Style: Italic, Weight: Normal},
Expected: Font{Typeface: testTF3, Style: Italic, Weight: Bold},
},
{
Lookup: Font{Typeface: testTF1, Style: Italic, Weight: Thin},
Expected: Font{Typeface: testTF1, Style: Italic, Weight: Normal},
},
{
Lookup: Font{Typeface: testTF1, Style: Italic, Weight: Bold},
Expected: Font{Typeface: testTF1, Style: Italic, Weight: Normal},
},
{
Lookup: Font{Typeface: testTF2, Weight: Normal},
ExpectedToFail: true,
},
{
Lookup: Font{Typeface: testTF2, Style: Italic, Weight: Normal},
ExpectedToFail: true,
},
if startX == rtlStartX || endX == rtlEndX {
t.Errorf("shaping with with different dominant text directions and the same alignment should not produce the same X unless it's middle-aligned")
}
for _, test := range otherTests {
got, ok := c.closestFont(test.Lookup)
if test.ExpectedToFail {
if ok {
t.Fatalf("expected closest font for %v to not exist", test.Lookup)
} else {
continue
}
func TestCacheGlyphConverstion(t *testing.T) {
ltrFace, _ := opentype.Parse(goregular.TTF)
rtlFace, _ := opentype.Parse(nsareg.TTF)
collection := []FontFace{{Face: ltrFace}, {Face: rtlFace}}
type testcase struct {
name string
text string
locale system.Locale
expected []Glyph
}
for _, tc := range []testcase{
{
name: "bidi ltr",
text: "The quick سماء שלום لا fox تمط שלום\nغير the\nlazy dog.",
locale: english,
},
{
name: "bidi rtl",
text: "الحب سماء brown привет fox تمط jumps\nпривет over\nغير الأحلام.",
locale: arabic,
},
} {
t.Run(tc.name, func(t *testing.T) {
cache := NewShaper(collection)
cache.LayoutString(Parameters{
PxPerEm: fixed.I(10),
}, 0, 200, tc.locale, tc.text)
doc := cache.txt
glyphs := make([]Glyph, 0, len(tc.expected))
for g, ok := cache.NextGlyph(); ok; g, ok = cache.NextGlyph() {
glyphs = append(glyphs, g)
}
glyphCursor := 0
for _, line := range doc.lines {
for runIdx, run := range line.runs {
lastRun := runIdx == len(line.runs)-1
start := 0
end := len(run.Glyphs) - 1
inc := 1
towardOrigin := false
if run.Direction.Progression() == system.TowardOrigin {
start = len(run.Glyphs) - 1
end = 0
inc = -1
towardOrigin = true
}
for glyphIdx := start; ; glyphIdx += inc {
endOfRun := glyphIdx == end
glyph := run.Glyphs[glyphIdx]
endOfCluster := glyphIdx == end || run.Glyphs[glyphIdx+inc].clusterIndex != glyph.clusterIndex
actual := glyphs[glyphCursor]
if actual.ID != glyph.id {
t.Errorf("glyphs[%d] expected id %d, got id %d", glyphCursor, glyph.id, actual.ID)
}
// Synthetic glyphs should only ever show up at the end of lines.
endOfLine := lastRun && endOfRun
synthetic := glyph.glyphCount == 0 && endOfLine
checkFlag(t, endOfLine, FlagLineBreak, actual, glyphCursor)
checkFlag(t, endOfRun, FlagRunBreak, actual, glyphCursor)
checkFlag(t, towardOrigin, FlagTowardOrigin, actual, glyphCursor)
checkFlag(t, synthetic, FlagSynthetic, actual, glyphCursor)
checkFlag(t, endOfCluster, FlagClusterBreak, actual, glyphCursor)
glyphCursor++
if glyphIdx == end {
break
}
}
}
}
printLinePositioning(t, doc.lines, glyphs)
})
}
}
func checkFlag(t *testing.T, shouldHave bool, flag Flags, actual Glyph, glyphCursor int) {
t.Helper()
if shouldHave && actual.Flags&flag == 0 {
t.Errorf("glyphs[%d] should have %s set", glyphCursor, flag)
} else if !shouldHave && actual.Flags&flag != 0 {
t.Errorf("glyphs[%d] should not have %s set", glyphCursor, flag)
}
}
func printLinePositioning(t *testing.T, lines []line, glyphs []Glyph) {
t.Helper()
glyphCursor := 0
for i, line := range lines {
t.Logf("line %d, dir %s, width %d, visual %v, runeCount: %d", i, line.direction, line.width, line.visualOrder, line.runeCount)
for k, run := range line.runs {
t.Logf("run: %d, dir %s, width %d, runes {count: %d, offset: %d}", k, run.Direction, run.Advance, run.Runes.Count, run.Runes.Offset)
start := 0
end := len(run.Glyphs) - 1
inc := 1
if run.Direction.Progression() == system.TowardOrigin {
start = len(run.Glyphs) - 1
end = 0
inc = -1
}
for g := start; ; g += inc {
glyph := run.Glyphs[g]
if glyphCursor < len(glyphs) {
t.Logf("glyph %2d, adv %3d, runes %2d, glyphs %d - glyphs[%2d] flags %s", g, glyph.xAdvance, glyph.runeCount, glyph.glyphCount, glyphCursor, glyphs[glyphCursor].Flags)
t.Logf("glyph %2d, adv %3d, runes %2d, glyphs %d - n/a", g, glyph.xAdvance, glyph.runeCount, glyph.glyphCount)
}
glyphCursor++
if g == end {
break
}
}
}
if !ok {
t.Fatalf("expected closest font for %v to exist", test.Lookup)
}
if got != test.Expected {
t.Fatalf("got %v, expected %v", got, test.Expected)
}
}
}
func newTestCache(fonts ...Font) *Cache {
c := &Cache{faces: make(map[Font]*faceCache)}
c.def = testTF1
for _, font := range fonts {
if font.Typeface == "" {
font.Typeface = testTF1
}
c.faces[font] = &faceCache{face: nil}
}
return c
}
@@ -0,0 +1,5 @@
go test fuzz v1
string("\x1d")
bool(true)
byte('\x1c')
uint16(227)
@@ -0,0 +1,5 @@
go test fuzz v1
string("0")
bool(true)
uint8(27)
uint16(200)
@@ -0,0 +1,5 @@
go test fuzz v1
string("\u2029")
bool(false)
byte('*')
uint16(72)
@@ -0,0 +1,5 @@
go test fuzz v1
string("Aͮ000000000000000")
bool(false)
byte('\u0087')
uint16(111)
@@ -0,0 +1,5 @@
go test fuzz v1
string("\x1e")
bool(true)
byte('\n')
uint16(254)
@@ -0,0 +1,5 @@
go test fuzz v1
string("\r")
bool(false)
byte('T')
uint16(200)
@@ -0,0 +1,5 @@
go test fuzz v1
string("\u0085")
bool(true)
byte('\x10')
uint16(271)
@@ -0,0 +1,5 @@
go test fuzz v1
string("0")
bool(false)
byte('\x00')
uint16(142)
@@ -0,0 +1,5 @@
go test fuzz v1
string("\n")
bool(true)
byte('\t')
uint16(200)
@@ -0,0 +1,5 @@
go test fuzz v1
string("ع0 ׂ0")
bool(false)
byte('\u0098')
uint16(198)
@@ -0,0 +1,5 @@
go test fuzz v1
string("\x1c")
bool(true)
byte('\u009c')
uint16(200)
+29 -135
View File
@@ -3,131 +3,13 @@
package text
import (
"io"
"fmt"
"gioui.org/io/system"
"gioui.org/op/clip"
"github.com/go-text/typesetting/font"
"golang.org/x/image/math/fixed"
)
// A Line contains the measurements of a line of text.
type Line struct {
Layout Layout
// Width is the width of the line.
Width fixed.Int26_6
// Ascent is the height above the baseline.
Ascent fixed.Int26_6
// Descent is the height below the baseline, including
// the line gap.
Descent fixed.Int26_6
// Bounds is the visible bounds of the line.
Bounds fixed.Rectangle26_6
}
// Range describes the position and quantity of a range of text elements
// within a larger slice. The unit is usually runes of unicode data or
// glyphs of shaped font data.
type Range struct {
// Count describes the number of items represented by the Range.
Count int
// Offset describes the start position of the represented
// items within a larger list.
Offset int
}
// GlyphID uniquely identifies a glyph within a specific font.
type GlyphID = font.GID
// Glyph contains the metadata needed to render a glyph.
type Glyph struct {
// ID is this glyph's identifier within the font it was shaped with.
ID GlyphID
// ClusterIndex is the identifier for the text shaping cluster that
// this glyph is part of.
ClusterIndex int
// GlyphCount is the number of glyphs in the same cluster as this glyph.
GlyphCount int
// RuneCount is the quantity of runes in the source text that this glyph
// corresponds to.
RuneCount int
// XAdvance and YAdvance describe the distance the dot moves when
// laying out the glyph on the X or Y axis.
XAdvance, YAdvance fixed.Int26_6
// XOffset and YOffset describe offsets from the dot that should be
// applied when rendering the glyph.
XOffset, YOffset fixed.Int26_6
}
// GlyphCluster provides metadata about a sequence of indivisible shaped
// glyphs.
type GlyphCluster struct {
// Advance is the cumulative advance of all glyphs in the cluster.
Advance fixed.Int26_6
// Runes indicates the position and quantity of the runes represented by
// this cluster within the text.
Runes Range
// Glyphs indicates the position and quantity of the glyphs within this
// cluster in a Layout's Glyphs slice.
Glyphs Range
}
// RuneWidth returns the effective width of one rune for this cluster.
// If the cluster contains multiple runes, the width of the glyphs of
// the cluster is divided evenly among the runes.
func (c GlyphCluster) RuneWidth() fixed.Int26_6 {
if c.Runes.Count == 0 {
return 0
}
return c.Advance / fixed.Int26_6(c.Runes.Count)
}
type Layout struct {
// Glyphs are the actual font characters for the text. They are ordered
// from left to right regardless of the text direction of the underlying
// text.
Glyphs []Glyph
// Clusters are metadata about the shaped glyphs. They are mostly useful for
// interactive text widgets like editors. The order of clusters is logical,
// so the first cluster will describe the beginning of the text and may
// refer to the final glyphs in the Glyphs field if the text is RTL.
Clusters []GlyphCluster
// Runes describes the position of the text data this layout represents
// within the overall body of text being shaped.
Runes Range
// Direction is the layout direction of the text.
Direction system.TextDirection
}
// Slice returns a layout starting at the glyph cluster index start
// and running through the glyph cluster index end. The Offsets field
// of the returned layout is adjusted to reflect the new rune range
// covered by the layout. The returned layout will have no Clusters.
func (l Layout) Slice(start, end int) Layout {
if start == end || end == 0 || start == len(l.Clusters) {
return Layout{}
}
newRuneStart := l.Clusters[start].Runes.Offset
runesBefore := newRuneStart - l.Runes.Offset
endCluster := l.Clusters[end-1]
startCluster := l.Clusters[start]
runesAfter := l.Runes.Offset + l.Runes.Count - (endCluster.Runes.Offset + endCluster.Runes.Count)
if l.Direction.Progression() == system.TowardOrigin {
startCluster, endCluster = endCluster, startCluster
}
glyphStart := startCluster.Glyphs.Offset
glyphEnd := endCluster.Glyphs.Offset + endCluster.Glyphs.Count
out := l
out.Clusters = nil
out.Glyphs = out.Glyphs[glyphStart:glyphEnd]
out.Runes.Offset = newRuneStart
out.Runes.Count -= runesBefore + runesAfter
return out
}
// Style is the font style.
type Style int
@@ -144,11 +26,10 @@ type Font struct {
Weight Weight
}
// Face implements text layout and shaping for a particular font. All
// methods must be safe for concurrent use.
// Face is an opaque handle to a typeface. The concrete implementation depends
// upon the kind of font and shaper in use.
type Face interface {
Layout(ppem fixed.Int26_6, maxWidth int, lc system.Locale, txt io.RuneReader) ([]Line, error)
Shape(ppem fixed.Int26_6, str Layout) clip.PathSpec
Face() font.Face
}
// Typeface identifies a particular typeface design. The empty
@@ -203,6 +84,31 @@ func (a Alignment) String() string {
}
}
// Align returns the x offset that should be applied to text with width so that it
// appears correctly aligned within a space of size maxWidth and with the primary
// text direction dir.
func (a Alignment) Align(dir system.TextDirection, width fixed.Int26_6, maxWidth int) fixed.Int26_6 {
mw := fixed.I(maxWidth)
if dir.Progression() == system.TowardOrigin {
switch a {
case Start:
a = End
case End:
a = Start
}
}
switch a {
case Middle:
return fixed.I(((mw - width) / 2).Floor())
case End:
return fixed.I((mw - width).Floor())
case Start:
return 0
default:
panic(fmt.Errorf("unknown alignment %v", a))
}
}
func (s Style) String() string {
switch s {
case Regular:
@@ -240,15 +146,3 @@ func (w Weight) String() string {
panic("invalid Weight")
}
}
// weightDistance returns the distance value between two font weights.
func weightDistance(wa Weight, wb Weight) int {
// Avoid dealing with negative Weight values.
a := int(wa) + 400
b := int(wb) + 400
diff := a - b
if diff < 0 {
return -diff
}
return diff
}