Add GetStringBounds API method.
This commit is contained in:
parent
80a6c6c182
commit
4cfd6dfa78
4 changed files with 65 additions and 19 deletions
|
@ -494,6 +494,13 @@ func TestFillString() {
|
||||||
gc.SetFontData(draw2d.FontData{"luxi", draw2d.FontFamilyMono, draw2d.FontStyleBold | draw2d.FontStyleItalic})
|
gc.SetFontData(draw2d.FontData{"luxi", draw2d.FontFamilyMono, draw2d.FontStyleBold | draw2d.FontStyleItalic})
|
||||||
width := gc.FillString("cou")
|
width := gc.FillString("cou")
|
||||||
gc.Translate(width+1, 0)
|
gc.Translate(width+1, 0)
|
||||||
|
left, top, right, bottom := gc.GetStringBounds("cou")
|
||||||
|
gc.SetStrokeColor(color.NRGBA{255, 0x33, 0x33, 0x80})
|
||||||
|
draw2d.Rect(gc, left, top, right, bottom)
|
||||||
|
gc.SetLineWidth(3.0)
|
||||||
|
gc.Stroke()
|
||||||
|
gc.SetStrokeColor(image.Black)
|
||||||
|
gc.SetLineWidth(1.0)
|
||||||
gc.StrokeString("cou")
|
gc.StrokeString("cou")
|
||||||
saveToPngFile("TestFillString", i)
|
saveToPngFile("TestFillString", i)
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,6 +43,7 @@ type GraphicContext interface {
|
||||||
ClearRect(x1, y1, x2, y2 int)
|
ClearRect(x1, y1, x2, y2 int)
|
||||||
SetDPI(dpi int)
|
SetDPI(dpi int)
|
||||||
GetDPI() int
|
GetDPI() int
|
||||||
|
GetStringBounds(s string) (left, top, right, bottom float64)
|
||||||
CreateStringPath(text string, x, y float64) (cursor float64)
|
CreateStringPath(text string, x, y float64) (cursor float64)
|
||||||
FillString(text string) (cursor float64)
|
FillString(text string) (cursor float64)
|
||||||
FillStringAt(text string, x, y float64) (cursor float64)
|
FillStringAt(text string, x, y float64) (cursor float64)
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
"image/color"
|
"image/color"
|
||||||
"image/draw"
|
"image/draw"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Painter interface {
|
type Painter interface {
|
||||||
|
@ -100,27 +101,17 @@ func (gc *ImageGraphicContext) StrokeStringAt(text string, x, y float64) (cursor
|
||||||
return width
|
return width
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateStringPath creates a path from the string s at x, y, and returns the string width.
|
func (gc *ImageGraphicContext) loadCurrentFont() (*truetype.Font, error) {
|
||||||
// The text is placed so that the left edge of the em square of the first character of s
|
|
||||||
// and the baseline intersect at x, y. The majority of the affected pixels will be
|
|
||||||
// above and to the right of the point, but some may be below or to the left.
|
|
||||||
// For example, drawing a string that starts with a 'J' in an italic font may
|
|
||||||
// affect pixels below and left of the point.
|
|
||||||
func (gc *ImageGraphicContext) CreateStringPath(text string, x, y float64) (width float64) {
|
|
||||||
font := GetFont(gc.Current.FontData)
|
font := GetFont(gc.Current.FontData)
|
||||||
if font == nil {
|
if font == nil {
|
||||||
font = GetFont(defaultFontData)
|
font = GetFont(defaultFontData)
|
||||||
}
|
}
|
||||||
if font == nil {
|
if font == nil {
|
||||||
return 0
|
return nil, errors.New("No font set, and no default font available.")
|
||||||
}
|
}
|
||||||
gc.SetFont(font)
|
gc.SetFont(font)
|
||||||
gc.SetFontSize(gc.Current.FontSize)
|
gc.SetFontSize(gc.Current.FontSize)
|
||||||
width, err := gc._createStringPath(text, 0, 0)
|
return font, nil
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
}
|
|
||||||
return width
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func fUnitsToFloat64(x int32) float64 {
|
func fUnitsToFloat64(x int32) float64 {
|
||||||
|
@ -183,10 +174,17 @@ func (gc *ImageGraphicContext) drawGlyph(glyph truetype.Index, dx, dy float64) e
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (gc *ImageGraphicContext) _createStringPath(s string, x, y float64) (float64, error) {
|
// CreateStringPath creates a path from the string s at x, y, and returns the string width.
|
||||||
font := gc.Current.font
|
// The text is placed so that the left edge of the em square of the first character of s
|
||||||
if font == nil {
|
// and the baseline intersect at x, y. The majority of the affected pixels will be
|
||||||
return 0.0, errors.New("draw2d: CreateStringPath called with a nil font")
|
// above and to the right of the point, but some may be below or to the left.
|
||||||
|
// For example, drawing a string that starts with a 'J' in an italic font may
|
||||||
|
// affect pixels below and left of the point.
|
||||||
|
func (gc *ImageGraphicContext) CreateStringPath(s string, x, y float64) float64 {
|
||||||
|
font, err := gc.loadCurrentFont()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return 0.0
|
||||||
}
|
}
|
||||||
startx := x
|
startx := x
|
||||||
prev, hasPrev := truetype.Index(0), false
|
prev, hasPrev := truetype.Index(0), false
|
||||||
|
@ -197,12 +195,52 @@ func (gc *ImageGraphicContext) _createStringPath(s string, x, y float64) (float6
|
||||||
}
|
}
|
||||||
err := gc.drawGlyph(index, x, y)
|
err := gc.drawGlyph(index, x, y)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return startx - x, err
|
log.Println(err)
|
||||||
|
return startx - x
|
||||||
}
|
}
|
||||||
x += fUnitsToFloat64(font.HMetric(gc.Current.scale, index).AdvanceWidth)
|
x += fUnitsToFloat64(font.HMetric(gc.Current.scale, index).AdvanceWidth)
|
||||||
prev, hasPrev = index, true
|
prev, hasPrev = index, true
|
||||||
}
|
}
|
||||||
return x - startx, nil
|
return x - startx
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetStringBounds returns the approximate pixel bounds of the string s at x, y.
|
||||||
|
// The the left edge of the em square of the first character of s
|
||||||
|
// and the baseline intersect at 0, 0 in the returned coordinates.
|
||||||
|
// Therefore the top and left coordinates may well be negative.
|
||||||
|
func (gc *ImageGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) {
|
||||||
|
font, err := gc.loadCurrentFont()
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return 0, 0, 0, 0
|
||||||
|
}
|
||||||
|
top, left, bottom, right = 10e6, 10e6, -10e6, -10e6
|
||||||
|
cursor := 0.0
|
||||||
|
prev, hasPrev := truetype.Index(0), false
|
||||||
|
for _, rune := range s {
|
||||||
|
index := font.Index(rune)
|
||||||
|
if hasPrev {
|
||||||
|
cursor += fUnitsToFloat64(font.Kerning(gc.Current.scale, prev, index))
|
||||||
|
}
|
||||||
|
if err := gc.glyphBuf.Load(gc.Current.font, gc.Current.scale, index, nil); err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
return 0, 0, 0, 0
|
||||||
|
}
|
||||||
|
e0 := 0
|
||||||
|
for _, e1 := range gc.glyphBuf.End {
|
||||||
|
ps := gc.glyphBuf.Point[e0:e1]
|
||||||
|
for _, p := range ps {
|
||||||
|
x, y := pointToF64Point(p)
|
||||||
|
top = math.Min(top, y)
|
||||||
|
bottom = math.Max(bottom, y)
|
||||||
|
left = math.Min(left, x+cursor)
|
||||||
|
right = math.Max(right, x+cursor)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cursor += fUnitsToFloat64(font.HMetric(gc.Current.scale, index).AdvanceWidth)
|
||||||
|
prev, hasPrev = index, true
|
||||||
|
}
|
||||||
|
return left, top, right, bottom
|
||||||
}
|
}
|
||||||
|
|
||||||
// recalc recalculates scale and bounds values from the font size, screen
|
// recalc recalculates scale and bounds values from the font size, screen
|
||||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 2.8 KiB After Width: | Height: | Size: 3.2 KiB |
Loading…
Reference in a new issue