Merge pull request #13 from wcharczuk/text-rotation

Adds `TextRotationDegrees`
This commit is contained in:
Will Charczuk 2016-10-21 12:53:01 -07:00 committed by GitHub
commit 27a5efdd2d
23 changed files with 546 additions and 114 deletions

View file

@ -28,11 +28,16 @@ type rasterRenderer struct {
i *image.RGBA
gc *drawing.RasterGraphicContext
rotateRadians float64
rotateRadians *float64
s Style
}
func (rr *rasterRenderer) ResetStyle() {
rr.s = Style{Font: rr.s.Font}
rr.ClearTextRotation()
}
// GetDPI returns the dpi.
func (rr *rasterRenderer) GetDPI() float64 {
return rr.gc.GetDPI()
@ -177,35 +182,40 @@ func (rr *rasterRenderer) MeasureText(body string) Box {
t = 0
}
return Box{
textBox := Box{
Top: int(math.Ceil(t)),
Left: int(math.Ceil(l)),
Right: int(math.Ceil(r)),
Bottom: int(math.Ceil(b)),
}
if rr.rotateRadians == nil {
return textBox
}
return textBox.Corners().Rotate(Math.RadiansToDegrees(*rr.rotateRadians)).Box()
}
// SetTextRotation sets a text rotation.
func (rr *rasterRenderer) SetTextRotation(radians float64) {
rr.rotateRadians = radians
rr.rotateRadians = &radians
}
func (rr *rasterRenderer) getCoords(x, y int) (xf, yf int) {
if rr.rotateRadians == 0 {
if rr.rotateRadians == nil {
xf = x
yf = y
return
}
rr.gc.Translate(float64(x), float64(y))
rr.gc.Rotate(rr.rotateRadians)
rr.gc.Rotate(*rr.rotateRadians)
return
}
// ClearTextRotation clears text rotation.
func (rr *rasterRenderer) ClearTextRotation() {
rr.gc.SetMatrixTransform(drawing.NewIdentityMatrix())
rr.rotateRadians = 0
rr.rotateRadians = nil
}
// Save implements the interface method.