Skip to content

Commit

Permalink
Merge pull request #120 from redstarcoder/kerning_fix
Browse files Browse the repository at this point in the history
Factor in kerning when using cached glyphs. fix #119
  • Loading branch information
llgcode committed Nov 4, 2016
2 parents 0d961cd + c120708 commit 1286d3b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
30 changes: 26 additions & 4 deletions draw2dgl/gc.go
Expand Up @@ -204,13 +204,24 @@ func (gc *GraphicContext) FillString(text string) (width float64) {

// FillStringAt draws the text at the specified point (x, y)
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
xorig := x
f, err := gc.loadCurrentFont()
if err != nil {
log.Println(err)
return 0.0
}
startx := x
prev, hasPrev := truetype.Index(0), false
fontName := gc.GetFontName()
for _, r := range text {
index := f.Index(r)
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true
}
return x - xorig
return x - startx
}

// GetStringBounds returns the approximate pixel bounds of the string s at x, y.
Expand Down Expand Up @@ -259,13 +270,24 @@ func (gc *GraphicContext) StrokeString(text string) (width float64) {

// StrokeStringAt draws the contour of the text at point (x, y)
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
xorig := x
f, err := gc.loadCurrentFont()
if err != nil {
log.Println(err)
return 0.0
}
startx := x
prev, hasPrev := truetype.Index(0), false
fontName := gc.GetFontName()
for _, r := range text {
index := f.Index(r)
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
x += glyph.Stroke(gc, x, y)
prev, hasPrev = index, true
}
return x - xorig
return x - startx
}

// recalc recalculates scale and bounds values from the font size, screen
Expand Down
30 changes: 26 additions & 4 deletions draw2dimg/ftgc.go
Expand Up @@ -123,13 +123,24 @@ func (gc *GraphicContext) FillString(text string) (width float64) {

// FillStringAt draws the text at the specified point (x, y)
func (gc *GraphicContext) FillStringAt(text string, x, y float64) (width float64) {
xorig := x
f, err := gc.loadCurrentFont()
if err != nil {
log.Println(err)
return 0.0
}
startx := x
prev, hasPrev := truetype.Index(0), false
fontName := gc.GetFontName()
for _, r := range text {
index := f.Index(r)
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
x += glyph.Fill(gc, x, y)
prev, hasPrev = index, true
}
return x - xorig
return x - startx
}

// StrokeString draws the contour of the text at point (0, 0)
Expand All @@ -139,13 +150,24 @@ func (gc *GraphicContext) StrokeString(text string) (width float64) {

// StrokeStringAt draws the contour of the text at point (x, y)
func (gc *GraphicContext) StrokeStringAt(text string, x, y float64) (width float64) {
xorig := x
f, err := gc.loadCurrentFont()
if err != nil {
log.Println(err)
return 0.0
}
startx := x
prev, hasPrev := truetype.Index(0), false
fontName := gc.GetFontName()
for _, r := range text {
index := f.Index(r)
if hasPrev {
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index))
}
glyph := draw2dbase.FetchGlyph(gc, fontName, r)
x += glyph.Stroke(gc, x, y)
prev, hasPrev = index, true
}
return x - xorig
return x - startx
}

func (gc *GraphicContext) loadCurrentFont() (*truetype.Font, error) {
Expand Down

0 comments on commit 1286d3b

Please sign in to comment.