Merge pull request #52 from rekby/multiline-measure
Add MeasureMultilineString method to Context.
This commit is contained in:
commit
77d18b88fe
27
context.go
27
context.go
@ -8,6 +8,7 @@ import (
|
|||||||
"image/png"
|
"image/png"
|
||||||
"io"
|
"io"
|
||||||
"math"
|
"math"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/golang/freetype/raster"
|
"github.com/golang/freetype/raster"
|
||||||
"golang.org/x/image/draw"
|
"golang.org/x/image/draw"
|
||||||
@ -737,8 +738,11 @@ func (dc *Context) DrawStringAnchored(s string, x, y, ax, ay float64) {
|
|||||||
// spacing and text alignment.
|
// spacing and text alignment.
|
||||||
func (dc *Context) DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align) {
|
func (dc *Context) DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing float64, align Align) {
|
||||||
lines := dc.WordWrap(s, width)
|
lines := dc.WordWrap(s, width)
|
||||||
|
|
||||||
|
// sync h formula with MeasureMultilineString
|
||||||
h := float64(len(lines)) * dc.fontHeight * lineSpacing
|
h := float64(len(lines)) * dc.fontHeight * lineSpacing
|
||||||
h -= (lineSpacing - 1) * dc.fontHeight
|
h -= (lineSpacing - 1) * dc.fontHeight
|
||||||
|
|
||||||
x -= ax * width
|
x -= ax * width
|
||||||
y -= ay * h
|
y -= ay * h
|
||||||
switch align {
|
switch align {
|
||||||
@ -758,6 +762,29 @@ func (dc *Context) DrawStringWrapped(s string, x, y, ax, ay, width, lineSpacing
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dc *Context) MeasureMultilineString(s string, lineSpacing float64) (width, height float64) {
|
||||||
|
lines := strings.Split(s, "\n")
|
||||||
|
|
||||||
|
// sync h formula with DrawStringWrapped
|
||||||
|
height = float64(len(lines)) * dc.fontHeight * lineSpacing
|
||||||
|
height -= (lineSpacing - 1) * dc.fontHeight
|
||||||
|
|
||||||
|
d := &font.Drawer{
|
||||||
|
Face: dc.fontFace,
|
||||||
|
}
|
||||||
|
|
||||||
|
// max width from lines
|
||||||
|
for _, line := range lines {
|
||||||
|
adv := d.MeasureString(line)
|
||||||
|
currentWidth := float64(adv >> 6) // from gg.Context.MeasureString
|
||||||
|
if currentWidth > width {
|
||||||
|
width = currentWidth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return width, height
|
||||||
|
}
|
||||||
|
|
||||||
// MeasureString returns the rendered width and height of the specified text
|
// MeasureString returns the rendered width and height of the specified text
|
||||||
// given the current font face.
|
// given the current font face.
|
||||||
func (dc *Context) MeasureString(s string) (w, h float64) {
|
func (dc *Context) MeasureString(s string) (w, h float64) {
|
||||||
|
Loading…
Reference in New Issue
Block a user