Skip to content

Commit

Permalink
Bugfixes for QuantizeVert.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinne26 committed Jul 8, 2022
1 parent 33979a1 commit 68bfd8a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
39 changes: 39 additions & 0 deletions renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,45 @@ func TestGtxtMixModes(t *testing.T) {
}
}

func TestAlignBound(t *testing.T) {
renderer := NewStdRenderer()
renderer.SetFont(testFont)
renderer.SetAlign(YCenter, XCenter)
horzPadder := &esizer.HorzPaddingSizer{}
renderer.SetSizer(horzPadder)

for size := 7; size < 73; size += 3 {
renderer.SetSizePx(size)
prevWidth := fixed.Int26_6(0)
for i := 0; i < 64; i += 3 {
const text = "abcdefghijkl - mnopq0123456789"
horzPadder.SetHorzPaddingFract(fixed.Int26_6(i))

renderer.SetQuantizationMode(QuantizeFull)
rect1 := renderer.SelectionRect(text)
renderer.SetQuantizationMode(QuantizeVert)
rect2 := renderer.SelectionRect(text)
renderer.SetQuantizationMode(QuantizeNone)
rect3 := renderer.SelectionRect(text)

if rect1.Height != rect2.Height {
t.Fatalf("SelectionRect.Height different for QuantizeFull and QuantizeVert (%d vs %d)", rect1.Height, rect2.Height)
}
if rect2.Width != rect3.Width {
t.Fatalf("SelectionRect.Width different for QuantizeVert and QuantizeNone (%d vs %d)", rect2.Width, rect3.Width)
}
if rect3.Width <= prevWidth {
t.Fatalf("SelectionRect.Width didn't increase")
}
prevWidth = rect3.Width

// if rect1.Width == rect2.Width { // uncommon but this can happen legitimately
// t.Fatalf("SelectionRect.Width uncommon match for QuantizeFull and QuantizeVert (%d vs %d)", rect1.Width, rect2.Width)
// }
}
}
}

func debugExport(name string, img image.Image) {
file, err := os.Create(name)
if err != nil { log.Fatal(err) }
Expand Down
18 changes: 10 additions & 8 deletions renderer_traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (self *Renderer) Traverse(text string, xy fixed.Point26_6, operation func(f
// get the glyph index for the current character and traverse it
index := self.getGlyphIndex(codePoint)
dot = traverseFunc(dot, glyphPair{ index, previousIndex, hasPrevGlyph },
func(dot fixed.Point26_6) { operation(dot, codePoint, index) })
func(opDot fixed.Point26_6) { operation(opDot, codePoint, index) })
hasPrevGlyph = true
previousIndex = index
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (self *Renderer) TraverseGlyphs(glyphIndices []GlyphIndex, xy fixed.Point26
iterator := newGlyphsIterator(glyphIndices, traverseInReverse)
index, _ := iterator.Next()
dot = traverseFunc(dot, glyphPair{ index, previousIndex, false },
func(dot fixed.Point26_6) { operation(dot, index)} )
func(opDot fixed.Point26_6) { operation(opDot, index)} )
previousIndex = index

// iterate all remaining glyphs
Expand All @@ -170,7 +170,7 @@ func (self *Renderer) quantizeY(y fixed.Int26_6) fixed.Int26_6 {
}

func (self *Renderer) quantizeX(x fixed.Int26_6, dir Direction) fixed.Int26_6 {
if self.quantization == QuantizeNone { return x }
if self.quantization != QuantizeFull { return x }
if dir == LeftToRight {
return efixed.RoundHalfUp(x)
} else { // RightToLeft
Expand Down Expand Up @@ -233,11 +233,13 @@ func (self *Renderer) traverseGlyphLTR(dot fixed.Point26_6, glyphSeq glyphPair,
if glyphSeq.HasPrevious { // apply kerning
prev, curr := glyphSeq.PreviousIndex, glyphSeq.CurrentIndex
dot.X += self.sizer.Kern(self.font, prev, curr, self.sizePx)
if self.quantization == QuantizeFull {
dot.X = efixed.RoundHalfUp(dot.X)
} else if self.cacheHandler != nil {
self.cacheHandler.NotifyFractChange(dot)
}
}

// quantize
if self.quantization == QuantizeFull {
dot.X = efixed.RoundHalfUp(dot.X)
} else if self.cacheHandler != nil {
self.cacheHandler.NotifyFractChange(dot)
}

// operate
Expand Down

0 comments on commit 68bfd8a

Please sign in to comment.