Adding upstream version 2.1.2.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
c8c64afc61
commit
41a2f19f12
220 changed files with 19814 additions and 0 deletions
5
drawing/README.md
Normal file
5
drawing/README.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
go-chart > drawing
|
||||
==================
|
||||
|
||||
The bulk of the code in this package is based on [draw2d](https://github.com/llgcode/draw2d), but
|
||||
with significant modifications to make the APIs more golang friendly and careful about units (points vs. pixels).
|
274
drawing/color.go
Normal file
274
drawing/color.go
Normal file
|
@ -0,0 +1,274 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Basic Colors from:
|
||||
// https://www.w3.org/wiki/CSS/Properties/color/keywords
|
||||
var (
|
||||
// ColorTransparent is a fully transparent color.
|
||||
ColorTransparent = Color{R: 255, G: 255, B: 255, A: 0}
|
||||
// ColorWhite is white.
|
||||
ColorWhite = Color{R: 255, G: 255, B: 255, A: 255}
|
||||
// ColorBlack is black.
|
||||
ColorBlack = Color{R: 0, G: 0, B: 0, A: 255}
|
||||
// ColorRed is red.
|
||||
ColorRed = Color{R: 255, G: 0, B: 0, A: 255}
|
||||
// ColorGreen is green.
|
||||
ColorGreen = Color{R: 0, G: 128, B: 0, A: 255}
|
||||
// ColorBlue is blue.
|
||||
ColorBlue = Color{R: 0, G: 0, B: 255, A: 255}
|
||||
// ColorSilver is a known color.
|
||||
ColorSilver = Color{R: 192, G: 192, B: 192, A: 255}
|
||||
// ColorMaroon is a known color.
|
||||
ColorMaroon = Color{R: 128, G: 0, B: 0, A: 255}
|
||||
// ColorPurple is a known color.
|
||||
ColorPurple = Color{R: 128, G: 0, B: 128, A: 255}
|
||||
// ColorFuchsia is a known color.
|
||||
ColorFuchsia = Color{R: 255, G: 0, B: 255, A: 255}
|
||||
// ColorLime is a known color.
|
||||
ColorLime = Color{R: 0, G: 255, B: 0, A: 255}
|
||||
// ColorOlive is a known color.
|
||||
ColorOlive = Color{R: 128, G: 128, B: 0, A: 255}
|
||||
// ColorYellow is a known color.
|
||||
ColorYellow = Color{R: 255, G: 255, B: 0, A: 255}
|
||||
// ColorNavy is a known color.
|
||||
ColorNavy = Color{R: 0, G: 0, B: 128, A: 255}
|
||||
// ColorTeal is a known color.
|
||||
ColorTeal = Color{R: 0, G: 128, B: 128, A: 255}
|
||||
// ColorAqua is a known color.
|
||||
ColorAqua = Color{R: 0, G: 255, B: 255, A: 255}
|
||||
)
|
||||
|
||||
func parseHex(hex string) uint8 {
|
||||
v, _ := strconv.ParseInt(hex, 16, 16)
|
||||
return uint8(v)
|
||||
}
|
||||
|
||||
// ParseColor parses a color from a string.
|
||||
func ParseColor(rawColor string) Color {
|
||||
if strings.HasPrefix(rawColor, "rgba") {
|
||||
return ColorFromRGBA(rawColor)
|
||||
}
|
||||
if strings.HasPrefix(rawColor, "rgb") {
|
||||
return ColorFromRGB(rawColor)
|
||||
}
|
||||
if strings.HasPrefix(rawColor, "#") {
|
||||
return ColorFromHex(rawColor)
|
||||
}
|
||||
return ColorFromKnown(rawColor)
|
||||
}
|
||||
|
||||
var rgbaexpr = regexp.MustCompile(`rgba\((?P<R>.+),(?P<G>.+),(?P<B>.+),(?P<A>.+)\)`)
|
||||
|
||||
// ColorFromRGBA returns a color from an `rgba()` css function.
|
||||
func ColorFromRGBA(rgba string) (output Color) {
|
||||
values := rgbaexpr.FindStringSubmatch(rgba)
|
||||
for i, name := range rgbaexpr.SubexpNames() {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
if i >= len(values) {
|
||||
break
|
||||
}
|
||||
switch name {
|
||||
case "R":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.R = uint8(parsed)
|
||||
case "G":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.G = uint8(parsed)
|
||||
case "B":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.B = uint8(parsed)
|
||||
case "A":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseFloat(value, 32)
|
||||
if parsed > 1 {
|
||||
parsed = 1
|
||||
} else if parsed < 0 {
|
||||
parsed = 0
|
||||
}
|
||||
output.A = uint8(parsed * 255)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
var rgbexpr = regexp.MustCompile(`rgb\((?P<R>.+),(?P<G>.+),(?P<B>.+)\)`)
|
||||
|
||||
// ColorFromRGB returns a color from an `rgb()` css function.
|
||||
func ColorFromRGB(rgb string) (output Color) {
|
||||
output.A = 255
|
||||
values := rgbexpr.FindStringSubmatch(rgb)
|
||||
for i, name := range rgbaexpr.SubexpNames() {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
if i >= len(values) {
|
||||
break
|
||||
}
|
||||
switch name {
|
||||
case "R":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.R = uint8(parsed)
|
||||
case "G":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.G = uint8(parsed)
|
||||
case "B":
|
||||
value := strings.TrimSpace(values[i])
|
||||
parsed, _ := strconv.ParseInt(value, 10, 16)
|
||||
output.B = uint8(parsed)
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// ColorFromHex returns a color from a css hex code.
|
||||
//
|
||||
// NOTE: it will trim a leading '#' character if present.
|
||||
func ColorFromHex(hex string) Color {
|
||||
if strings.HasPrefix(hex, "#") {
|
||||
hex = strings.TrimPrefix(hex, "#")
|
||||
}
|
||||
var c Color
|
||||
if len(hex) == 3 {
|
||||
c.R = parseHex(string(hex[0])) * 0x11
|
||||
c.G = parseHex(string(hex[1])) * 0x11
|
||||
c.B = parseHex(string(hex[2])) * 0x11
|
||||
} else {
|
||||
c.R = parseHex(string(hex[0:2]))
|
||||
c.G = parseHex(string(hex[2:4]))
|
||||
c.B = parseHex(string(hex[4:6]))
|
||||
}
|
||||
c.A = 255
|
||||
return c
|
||||
}
|
||||
|
||||
// ColorFromKnown returns an internal color from a known (basic) color name.
|
||||
func ColorFromKnown(known string) Color {
|
||||
switch strings.ToLower(known) {
|
||||
case "transparent":
|
||||
return ColorTransparent
|
||||
case "white":
|
||||
return ColorWhite
|
||||
case "black":
|
||||
return ColorBlack
|
||||
case "red":
|
||||
return ColorRed
|
||||
case "blue":
|
||||
return ColorBlue
|
||||
case "green":
|
||||
return ColorGreen
|
||||
case "silver":
|
||||
return ColorSilver
|
||||
case "maroon":
|
||||
return ColorMaroon
|
||||
case "purple":
|
||||
return ColorPurple
|
||||
case "fuchsia":
|
||||
return ColorFuchsia
|
||||
case "lime":
|
||||
return ColorLime
|
||||
case "olive":
|
||||
return ColorOlive
|
||||
case "yellow":
|
||||
return ColorYellow
|
||||
case "navy":
|
||||
return ColorNavy
|
||||
case "teal":
|
||||
return ColorTeal
|
||||
case "aqua":
|
||||
return ColorAqua
|
||||
default:
|
||||
return Color{}
|
||||
}
|
||||
}
|
||||
|
||||
// ColorFromAlphaMixedRGBA returns the system alpha mixed rgba values.
|
||||
func ColorFromAlphaMixedRGBA(r, g, b, a uint32) Color {
|
||||
fa := float64(a) / 255.0
|
||||
var c Color
|
||||
c.R = uint8(float64(r) / fa)
|
||||
c.G = uint8(float64(g) / fa)
|
||||
c.B = uint8(float64(b) / fa)
|
||||
c.A = uint8(a | (a >> 8))
|
||||
return c
|
||||
}
|
||||
|
||||
// ColorChannelFromFloat returns a normalized byte from a given float value.
|
||||
func ColorChannelFromFloat(v float64) uint8 {
|
||||
return uint8(v * 255)
|
||||
}
|
||||
|
||||
// Color is our internal color type because color.Color is bullshit.
|
||||
type Color struct {
|
||||
R, G, B, A uint8
|
||||
}
|
||||
|
||||
// RGBA returns the color as a pre-alpha mixed color set.
|
||||
func (c Color) RGBA() (r, g, b, a uint32) {
|
||||
fa := float64(c.A) / 255.0
|
||||
r = uint32(float64(uint32(c.R)) * fa)
|
||||
r |= r << 8
|
||||
g = uint32(float64(uint32(c.G)) * fa)
|
||||
g |= g << 8
|
||||
b = uint32(float64(uint32(c.B)) * fa)
|
||||
b |= b << 8
|
||||
a = uint32(c.A)
|
||||
a |= a << 8
|
||||
return
|
||||
}
|
||||
|
||||
// IsZero returns if the color has been set or not.
|
||||
func (c Color) IsZero() bool {
|
||||
return c.R == 0 && c.G == 0 && c.B == 0 && c.A == 0
|
||||
}
|
||||
|
||||
// IsTransparent returns if the colors alpha channel is zero.
|
||||
func (c Color) IsTransparent() bool {
|
||||
return c.A == 0
|
||||
}
|
||||
|
||||
// WithAlpha returns a copy of the color with a given alpha.
|
||||
func (c Color) WithAlpha(a uint8) Color {
|
||||
return Color{
|
||||
R: c.R,
|
||||
G: c.G,
|
||||
B: c.B,
|
||||
A: a,
|
||||
}
|
||||
}
|
||||
|
||||
// Equals returns true if the color equals another.
|
||||
func (c Color) Equals(other Color) bool {
|
||||
return c.R == other.R &&
|
||||
c.G == other.G &&
|
||||
c.B == other.B &&
|
||||
c.A == other.A
|
||||
}
|
||||
|
||||
// AverageWith averages two colors.
|
||||
func (c Color) AverageWith(other Color) Color {
|
||||
return Color{
|
||||
R: (c.R + other.R) >> 1,
|
||||
G: (c.G + other.G) >> 1,
|
||||
B: (c.B + other.B) >> 1,
|
||||
A: c.A,
|
||||
}
|
||||
}
|
||||
|
||||
// String returns a css string representation of the color.
|
||||
func (c Color) String() string {
|
||||
fa := float64(c.A) / float64(255)
|
||||
return fmt.Sprintf("rgba(%v,%v,%v,%.1f)", c.R, c.G, c.B, fa)
|
||||
}
|
114
drawing/color_test.go
Normal file
114
drawing/color_test.go
Normal file
|
@ -0,0 +1,114 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"image/color"
|
||||
|
||||
"github.com/wcharczuk/go-chart/v2/testutil"
|
||||
)
|
||||
|
||||
func TestColorFromHex(t *testing.T) {
|
||||
white := ColorFromHex("FFFFFF")
|
||||
testutil.AssertEqual(t, ColorWhite, white)
|
||||
|
||||
shortWhite := ColorFromHex("FFF")
|
||||
testutil.AssertEqual(t, ColorWhite, shortWhite)
|
||||
|
||||
black := ColorFromHex("000000")
|
||||
testutil.AssertEqual(t, ColorBlack, black)
|
||||
|
||||
shortBlack := ColorFromHex("000")
|
||||
testutil.AssertEqual(t, ColorBlack, shortBlack)
|
||||
|
||||
red := ColorFromHex("FF0000")
|
||||
testutil.AssertEqual(t, ColorRed, red)
|
||||
|
||||
shortRed := ColorFromHex("F00")
|
||||
testutil.AssertEqual(t, ColorRed, shortRed)
|
||||
|
||||
green := ColorFromHex("008000")
|
||||
testutil.AssertEqual(t, ColorGreen, green)
|
||||
|
||||
// shortGreen := ColorFromHex("0F0")
|
||||
// testutil.AssertEqual(t, ColorGreen, shortGreen)
|
||||
|
||||
blue := ColorFromHex("0000FF")
|
||||
testutil.AssertEqual(t, ColorBlue, blue)
|
||||
|
||||
shortBlue := ColorFromHex("00F")
|
||||
testutil.AssertEqual(t, ColorBlue, shortBlue)
|
||||
}
|
||||
|
||||
func TestColorFromHex_handlesHash(t *testing.T) {
|
||||
withHash := ColorFromHex("#FF0000")
|
||||
testutil.AssertEqual(t, ColorRed, withHash)
|
||||
|
||||
withoutHash := ColorFromHex("#FF0000")
|
||||
testutil.AssertEqual(t, ColorRed, withoutHash)
|
||||
}
|
||||
|
||||
func TestColorFromAlphaMixedRGBA(t *testing.T) {
|
||||
black := ColorFromAlphaMixedRGBA(color.Black.RGBA())
|
||||
testutil.AssertTrue(t, black.Equals(ColorBlack), black.String())
|
||||
|
||||
white := ColorFromAlphaMixedRGBA(color.White.RGBA())
|
||||
testutil.AssertTrue(t, white.Equals(ColorWhite), white.String())
|
||||
}
|
||||
|
||||
func Test_ColorFromRGBA(t *testing.T) {
|
||||
value := "rgba(192, 192, 192, 1.0)"
|
||||
parsed := ColorFromRGBA(value)
|
||||
testutil.AssertEqual(t, ColorSilver, parsed)
|
||||
|
||||
value = "rgba(192,192,192,1.0)"
|
||||
parsed = ColorFromRGBA(value)
|
||||
testutil.AssertEqual(t, ColorSilver, parsed)
|
||||
|
||||
value = "rgba(192,192,192,1.5)"
|
||||
parsed = ColorFromRGBA(value)
|
||||
testutil.AssertEqual(t, ColorSilver, parsed)
|
||||
}
|
||||
|
||||
func TestParseColor(t *testing.T) {
|
||||
testCases := [...]struct {
|
||||
Input string
|
||||
Expected Color
|
||||
}{
|
||||
{"", Color{}},
|
||||
{"white", ColorWhite},
|
||||
{"WHITE", ColorWhite}, // caps!
|
||||
{"black", ColorBlack},
|
||||
{"red", ColorRed},
|
||||
{"green", ColorGreen},
|
||||
{"blue", ColorBlue},
|
||||
{"silver", ColorSilver},
|
||||
{"maroon", ColorMaroon},
|
||||
{"purple", ColorPurple},
|
||||
{"fuchsia", ColorFuchsia},
|
||||
{"lime", ColorLime},
|
||||
{"olive", ColorOlive},
|
||||
{"yellow", ColorYellow},
|
||||
{"navy", ColorNavy},
|
||||
{"teal", ColorTeal},
|
||||
{"aqua", ColorAqua},
|
||||
|
||||
{"rgba(192, 192, 192, 1.0)", ColorSilver},
|
||||
{"rgba(192,192,192,1.0)", ColorSilver},
|
||||
{"rgb(192, 192, 192)", ColorSilver},
|
||||
{"rgb(192,192,192)", ColorSilver},
|
||||
|
||||
{"#FF0000", ColorRed},
|
||||
{"#008000", ColorGreen},
|
||||
{"#0000FF", ColorBlue},
|
||||
{"#F00", ColorRed},
|
||||
{"#080", Color{0, 136, 0, 255}},
|
||||
{"#00F", ColorBlue},
|
||||
}
|
||||
|
||||
for index, tc := range testCases {
|
||||
actual := ParseColor(tc.Input)
|
||||
testutil.AssertEqual(t, tc.Expected, actual, fmt.Sprintf("test case: %d -> %s", index, tc.Input))
|
||||
}
|
||||
}
|
6
drawing/constants.go
Normal file
6
drawing/constants.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package drawing
|
||||
|
||||
const (
|
||||
// DefaultDPI is the default image DPI.
|
||||
DefaultDPI = 96.0
|
||||
)
|
185
drawing/curve.go
Normal file
185
drawing/curve.go
Normal file
|
@ -0,0 +1,185 @@
|
|||
package drawing
|
||||
|
||||
import "math"
|
||||
|
||||
const (
|
||||
// CurveRecursionLimit represents the maximum recursion that is really necessary to subsivide a curve into straight lines
|
||||
CurveRecursionLimit = 32
|
||||
)
|
||||
|
||||
// Cubic
|
||||
// x1, y1, cpx1, cpy1, cpx2, cpy2, x2, y2 float64
|
||||
|
||||
// SubdivideCubic a Bezier cubic curve in 2 equivalents Bezier cubic curves.
|
||||
// c1 and c2 parameters are the resulting curves
|
||||
func SubdivideCubic(c, c1, c2 []float64) {
|
||||
// First point of c is the first point of c1
|
||||
c1[0], c1[1] = c[0], c[1]
|
||||
// Last point of c is the last point of c2
|
||||
c2[6], c2[7] = c[6], c[7]
|
||||
|
||||
// Subdivide segment using midpoints
|
||||
c1[2] = (c[0] + c[2]) / 2
|
||||
c1[3] = (c[1] + c[3]) / 2
|
||||
|
||||
midX := (c[2] + c[4]) / 2
|
||||
midY := (c[3] + c[5]) / 2
|
||||
|
||||
c2[4] = (c[4] + c[6]) / 2
|
||||
c2[5] = (c[5] + c[7]) / 2
|
||||
|
||||
c1[4] = (c1[2] + midX) / 2
|
||||
c1[5] = (c1[3] + midY) / 2
|
||||
|
||||
c2[2] = (midX + c2[4]) / 2
|
||||
c2[3] = (midY + c2[5]) / 2
|
||||
|
||||
c1[6] = (c1[4] + c2[2]) / 2
|
||||
c1[7] = (c1[5] + c2[3]) / 2
|
||||
|
||||
// Last Point of c1 is equal to the first point of c2
|
||||
c2[0], c2[1] = c1[6], c1[7]
|
||||
}
|
||||
|
||||
// TraceCubic generate lines subdividing the cubic curve using a Liner
|
||||
// flattening_threshold helps determines the flattening expectation of the curve
|
||||
func TraceCubic(t Liner, cubic []float64, flatteningThreshold float64) {
|
||||
// Allocation curves
|
||||
var curves [CurveRecursionLimit * 8]float64
|
||||
copy(curves[0:8], cubic[0:8])
|
||||
i := 0
|
||||
|
||||
// current curve
|
||||
var c []float64
|
||||
|
||||
var dx, dy, d2, d3 float64
|
||||
|
||||
for i >= 0 {
|
||||
c = curves[i*8:]
|
||||
dx = c[6] - c[0]
|
||||
dy = c[7] - c[1]
|
||||
|
||||
d2 = math.Abs((c[2]-c[6])*dy - (c[3]-c[7])*dx)
|
||||
d3 = math.Abs((c[4]-c[6])*dy - (c[5]-c[7])*dx)
|
||||
|
||||
// if it's flat then trace a line
|
||||
if (d2+d3)*(d2+d3) < flatteningThreshold*(dx*dx+dy*dy) || i == len(curves)-1 {
|
||||
t.LineTo(c[6], c[7])
|
||||
i--
|
||||
} else {
|
||||
// second half of bezier go lower onto the stack
|
||||
SubdivideCubic(c, curves[(i+1)*8:], curves[i*8:])
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Quad
|
||||
// x1, y1, cpx1, cpy2, x2, y2 float64
|
||||
|
||||
// SubdivideQuad a Bezier quad curve in 2 equivalents Bezier quad curves.
|
||||
// c1 and c2 parameters are the resulting curves
|
||||
func SubdivideQuad(c, c1, c2 []float64) {
|
||||
// First point of c is the first point of c1
|
||||
c1[0], c1[1] = c[0], c[1]
|
||||
// Last point of c is the last point of c2
|
||||
c2[4], c2[5] = c[4], c[5]
|
||||
|
||||
// Subdivide segment using midpoints
|
||||
c1[2] = (c[0] + c[2]) / 2
|
||||
c1[3] = (c[1] + c[3]) / 2
|
||||
c2[2] = (c[2] + c[4]) / 2
|
||||
c2[3] = (c[3] + c[5]) / 2
|
||||
c1[4] = (c1[2] + c2[2]) / 2
|
||||
c1[5] = (c1[3] + c2[3]) / 2
|
||||
c2[0], c2[1] = c1[4], c1[5]
|
||||
return
|
||||
}
|
||||
|
||||
func traceWindowIndices(i int) (startAt, endAt int) {
|
||||
startAt = i * 6
|
||||
endAt = startAt + 6
|
||||
return
|
||||
}
|
||||
|
||||
func traceCalcDeltas(c []float64) (dx, dy, d float64) {
|
||||
dx = c[4] - c[0]
|
||||
dy = c[5] - c[1]
|
||||
d = math.Abs(((c[2]-c[4])*dy - (c[3]-c[5])*dx))
|
||||
return
|
||||
}
|
||||
|
||||
func traceIsFlat(dx, dy, d, threshold float64) bool {
|
||||
return (d * d) < threshold*(dx*dx+dy*dy)
|
||||
}
|
||||
|
||||
func traceGetWindow(curves []float64, i int) []float64 {
|
||||
startAt, endAt := traceWindowIndices(i)
|
||||
return curves[startAt:endAt]
|
||||
}
|
||||
|
||||
// TraceQuad generate lines subdividing the curve using a Liner
|
||||
// flattening_threshold helps determines the flattening expectation of the curve
|
||||
func TraceQuad(t Liner, quad []float64, flatteningThreshold float64) {
|
||||
const curveLen = CurveRecursionLimit * 6
|
||||
const curveEndIndex = curveLen - 1
|
||||
const lastIteration = CurveRecursionLimit - 1
|
||||
|
||||
// Allocates curves stack
|
||||
curves := make([]float64, curveLen)
|
||||
|
||||
// copy 6 elements from the quad path to the stack
|
||||
copy(curves[0:6], quad[0:6])
|
||||
|
||||
var i int
|
||||
var c []float64
|
||||
var dx, dy, d float64
|
||||
|
||||
for i >= 0 {
|
||||
c = traceGetWindow(curves, i)
|
||||
dx, dy, d = traceCalcDeltas(c)
|
||||
|
||||
// bail early if the distance is 0
|
||||
if d == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
// if it's flat then trace a line
|
||||
if traceIsFlat(dx, dy, d, flatteningThreshold) || i == lastIteration {
|
||||
t.LineTo(c[4], c[5])
|
||||
i--
|
||||
} else {
|
||||
SubdivideQuad(c, traceGetWindow(curves, i+1), traceGetWindow(curves, i))
|
||||
i++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TraceArc trace an arc using a Liner
|
||||
func TraceArc(t Liner, x, y, rx, ry, start, angle, scale float64) (lastX, lastY float64) {
|
||||
end := start + angle
|
||||
clockWise := true
|
||||
if angle < 0 {
|
||||
clockWise = false
|
||||
}
|
||||
ra := (math.Abs(rx) + math.Abs(ry)) / 2
|
||||
da := math.Acos(ra/(ra+0.125/scale)) * 2
|
||||
//normalize
|
||||
if !clockWise {
|
||||
da = -da
|
||||
}
|
||||
angle = start + da
|
||||
var curX, curY float64
|
||||
for {
|
||||
if (angle < end-da/4) != clockWise {
|
||||
curX = x + math.Cos(end)*rx
|
||||
curY = y + math.Sin(end)*ry
|
||||
return curX, curY
|
||||
}
|
||||
curX = x + math.Cos(angle)*rx
|
||||
curY = y + math.Sin(angle)*ry
|
||||
|
||||
angle += da
|
||||
t.LineTo(curX, curY)
|
||||
}
|
||||
}
|
35
drawing/curve_test.go
Normal file
35
drawing/curve_test.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/wcharczuk/go-chart/v2/testutil"
|
||||
)
|
||||
|
||||
type point struct {
|
||||
X, Y float64
|
||||
}
|
||||
|
||||
type mockLine struct {
|
||||
inner []point
|
||||
}
|
||||
|
||||
func (ml *mockLine) LineTo(x, y float64) {
|
||||
ml.inner = append(ml.inner, point{x, y})
|
||||
}
|
||||
|
||||
func (ml mockLine) Len() int {
|
||||
return len(ml.inner)
|
||||
}
|
||||
|
||||
func TestTraceQuad(t *testing.T) {
|
||||
// replaced new assertions helper
|
||||
|
||||
// Quad
|
||||
// x1, y1, cpx1, cpy2, x2, y2 float64
|
||||
// do the 9->12 circle segment
|
||||
quad := []float64{10, 20, 20, 20, 20, 10}
|
||||
liner := &mockLine{}
|
||||
TraceQuad(liner, quad, 0.5)
|
||||
testutil.AssertNotZero(t, liner.Len())
|
||||
}
|
89
drawing/dasher.go
Normal file
89
drawing/dasher.go
Normal file
|
@ -0,0 +1,89 @@
|
|||
package drawing
|
||||
|
||||
// NewDashVertexConverter creates a new dash converter.
|
||||
func NewDashVertexConverter(dash []float64, dashOffset float64, flattener Flattener) *DashVertexConverter {
|
||||
var dasher DashVertexConverter
|
||||
dasher.dash = dash
|
||||
dasher.currentDash = 0
|
||||
dasher.dashOffset = dashOffset
|
||||
dasher.next = flattener
|
||||
return &dasher
|
||||
}
|
||||
|
||||
// DashVertexConverter is a converter for dash vertexes.
|
||||
type DashVertexConverter struct {
|
||||
next Flattener
|
||||
x, y, distance float64
|
||||
dash []float64
|
||||
currentDash int
|
||||
dashOffset float64
|
||||
}
|
||||
|
||||
// LineTo implements the pathbuilder interface.
|
||||
func (dasher *DashVertexConverter) LineTo(x, y float64) {
|
||||
dasher.lineTo(x, y)
|
||||
}
|
||||
|
||||
// MoveTo implements the pathbuilder interface.
|
||||
func (dasher *DashVertexConverter) MoveTo(x, y float64) {
|
||||
dasher.next.MoveTo(x, y)
|
||||
dasher.x, dasher.y = x, y
|
||||
dasher.distance = dasher.dashOffset
|
||||
dasher.currentDash = 0
|
||||
}
|
||||
|
||||
// LineJoin implements the pathbuilder interface.
|
||||
func (dasher *DashVertexConverter) LineJoin() {
|
||||
dasher.next.LineJoin()
|
||||
}
|
||||
|
||||
// Close implements the pathbuilder interface.
|
||||
func (dasher *DashVertexConverter) Close() {
|
||||
dasher.next.Close()
|
||||
}
|
||||
|
||||
// End implements the pathbuilder interface.
|
||||
func (dasher *DashVertexConverter) End() {
|
||||
dasher.next.End()
|
||||
}
|
||||
|
||||
func (dasher *DashVertexConverter) lineTo(x, y float64) {
|
||||
rest := dasher.dash[dasher.currentDash] - dasher.distance
|
||||
for rest < 0 {
|
||||
dasher.distance = dasher.distance - dasher.dash[dasher.currentDash]
|
||||
dasher.currentDash = (dasher.currentDash + 1) % len(dasher.dash)
|
||||
rest = dasher.dash[dasher.currentDash] - dasher.distance
|
||||
}
|
||||
d := distance(dasher.x, dasher.y, x, y)
|
||||
for d >= rest {
|
||||
k := rest / d
|
||||
lx := dasher.x + k*(x-dasher.x)
|
||||
ly := dasher.y + k*(y-dasher.y)
|
||||
if dasher.currentDash%2 == 0 {
|
||||
// line
|
||||
dasher.next.LineTo(lx, ly)
|
||||
} else {
|
||||
// gap
|
||||
dasher.next.End()
|
||||
dasher.next.MoveTo(lx, ly)
|
||||
}
|
||||
d = d - rest
|
||||
dasher.x, dasher.y = lx, ly
|
||||
dasher.currentDash = (dasher.currentDash + 1) % len(dasher.dash)
|
||||
rest = dasher.dash[dasher.currentDash]
|
||||
}
|
||||
dasher.distance = d
|
||||
if dasher.currentDash%2 == 0 {
|
||||
// line
|
||||
dasher.next.LineTo(x, y)
|
||||
} else {
|
||||
// gap
|
||||
dasher.next.End()
|
||||
dasher.next.MoveTo(x, y)
|
||||
}
|
||||
if dasher.distance >= dasher.dash[dasher.currentDash] {
|
||||
dasher.distance = dasher.distance - dasher.dash[dasher.currentDash]
|
||||
dasher.currentDash = (dasher.currentDash + 1) % len(dasher.dash)
|
||||
}
|
||||
dasher.x, dasher.y = x, y
|
||||
}
|
41
drawing/demux_flattener.go
Normal file
41
drawing/demux_flattener.go
Normal file
|
@ -0,0 +1,41 @@
|
|||
package drawing
|
||||
|
||||
// DemuxFlattener is a flattener
|
||||
type DemuxFlattener struct {
|
||||
Flatteners []Flattener
|
||||
}
|
||||
|
||||
// MoveTo implements the path builder interface.
|
||||
func (dc DemuxFlattener) MoveTo(x, y float64) {
|
||||
for _, flattener := range dc.Flatteners {
|
||||
flattener.MoveTo(x, y)
|
||||
}
|
||||
}
|
||||
|
||||
// LineTo implements the path builder interface.
|
||||
func (dc DemuxFlattener) LineTo(x, y float64) {
|
||||
for _, flattener := range dc.Flatteners {
|
||||
flattener.LineTo(x, y)
|
||||
}
|
||||
}
|
||||
|
||||
// LineJoin implements the path builder interface.
|
||||
func (dc DemuxFlattener) LineJoin() {
|
||||
for _, flattener := range dc.Flatteners {
|
||||
flattener.LineJoin()
|
||||
}
|
||||
}
|
||||
|
||||
// Close implements the path builder interface.
|
||||
func (dc DemuxFlattener) Close() {
|
||||
for _, flattener := range dc.Flatteners {
|
||||
flattener.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// End implements the path builder interface.
|
||||
func (dc DemuxFlattener) End() {
|
||||
for _, flattener := range dc.Flatteners {
|
||||
flattener.End()
|
||||
}
|
||||
}
|
148
drawing/drawing.go
Normal file
148
drawing/drawing.go
Normal file
|
@ -0,0 +1,148 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
|
||||
"github.com/golang/freetype/truetype"
|
||||
)
|
||||
|
||||
// FillRule defines the type for fill rules
|
||||
type FillRule int
|
||||
|
||||
const (
|
||||
// FillRuleEvenOdd determines the "insideness" of a point in the shape
|
||||
// by drawing a ray from that point to infinity in any direction
|
||||
// and counting the number of path segments from the given shape that the ray crosses.
|
||||
// If this number is odd, the point is inside; if even, the point is outside.
|
||||
FillRuleEvenOdd FillRule = iota
|
||||
// FillRuleWinding determines the "insideness" of a point in the shape
|
||||
// by drawing a ray from that point to infinity in any direction
|
||||
// and then examining the places where a segment of the shape crosses the ray.
|
||||
// Starting with a count of zero, add one each time a path segment crosses
|
||||
// the ray from left to right and subtract one each time
|
||||
// a path segment crosses the ray from right to left. After counting the crossings,
|
||||
// if the result is zero then the point is outside the path. Otherwise, it is inside.
|
||||
FillRuleWinding
|
||||
)
|
||||
|
||||
// LineCap is the style of line extremities
|
||||
type LineCap int
|
||||
|
||||
const (
|
||||
// RoundCap defines a rounded shape at the end of the line
|
||||
RoundCap LineCap = iota
|
||||
// ButtCap defines a squared shape exactly at the end of the line
|
||||
ButtCap
|
||||
// SquareCap defines a squared shape at the end of the line
|
||||
SquareCap
|
||||
)
|
||||
|
||||
// LineJoin is the style of segments joint
|
||||
type LineJoin int
|
||||
|
||||
const (
|
||||
// BevelJoin represents cut segments joint
|
||||
BevelJoin LineJoin = iota
|
||||
// RoundJoin represents rounded segments joint
|
||||
RoundJoin
|
||||
// MiterJoin represents peaker segments joint
|
||||
MiterJoin
|
||||
)
|
||||
|
||||
// StrokeStyle keeps stroke style attributes
|
||||
// that is used by the Stroke method of a Drawer
|
||||
type StrokeStyle struct {
|
||||
// Color defines the color of stroke
|
||||
Color color.Color
|
||||
// Line width
|
||||
Width float64
|
||||
// Line cap style rounded, butt or square
|
||||
LineCap LineCap
|
||||
// Line join style bevel, round or miter
|
||||
LineJoin LineJoin
|
||||
// offset of the first dash
|
||||
DashOffset float64
|
||||
// array represented dash length pair values are plain dash and impair are space between dash
|
||||
// if empty display plain line
|
||||
Dash []float64
|
||||
}
|
||||
|
||||
// SolidFillStyle define style attributes for a solid fill style
|
||||
type SolidFillStyle struct {
|
||||
// Color defines the line color
|
||||
Color color.Color
|
||||
// FillRule defines the file rule to used
|
||||
FillRule FillRule
|
||||
}
|
||||
|
||||
// Valign Vertical Alignment of the text
|
||||
type Valign int
|
||||
|
||||
const (
|
||||
// ValignTop top align text
|
||||
ValignTop Valign = iota
|
||||
// ValignCenter centered text
|
||||
ValignCenter
|
||||
// ValignBottom bottom aligned text
|
||||
ValignBottom
|
||||
// ValignBaseline align text with the baseline of the font
|
||||
ValignBaseline
|
||||
)
|
||||
|
||||
// Halign Horizontal Alignment of the text
|
||||
type Halign int
|
||||
|
||||
const (
|
||||
// HalignLeft Horizontally align to left
|
||||
HalignLeft = iota
|
||||
// HalignCenter Horizontally align to center
|
||||
HalignCenter
|
||||
// HalignRight Horizontally align to right
|
||||
HalignRight
|
||||
)
|
||||
|
||||
// TextStyle describe text property
|
||||
type TextStyle struct {
|
||||
// Color defines the color of text
|
||||
Color color.Color
|
||||
// Size font size
|
||||
Size float64
|
||||
// The font to use
|
||||
Font *truetype.Font
|
||||
// Horizontal Alignment of the text
|
||||
Halign Halign
|
||||
// Vertical Alignment of the text
|
||||
Valign Valign
|
||||
}
|
||||
|
||||
// ScalingPolicy is a constant to define how to scale an image
|
||||
type ScalingPolicy int
|
||||
|
||||
const (
|
||||
// ScalingNone no scaling applied
|
||||
ScalingNone ScalingPolicy = iota
|
||||
// ScalingStretch the image is stretched so that its width and height are exactly the given width and height
|
||||
ScalingStretch
|
||||
// ScalingWidth the image is scaled so that its width is exactly the given width
|
||||
ScalingWidth
|
||||
// ScalingHeight the image is scaled so that its height is exactly the given height
|
||||
ScalingHeight
|
||||
// ScalingFit the image is scaled to the largest scale that allow the image to fit within a rectangle width x height
|
||||
ScalingFit
|
||||
// ScalingSameArea the image is scaled so that its area is exactly the area of the given rectangle width x height
|
||||
ScalingSameArea
|
||||
// ScalingFill the image is scaled to the smallest scale that allow the image to fully cover a rectangle width x height
|
||||
ScalingFill
|
||||
)
|
||||
|
||||
// ImageScaling style attributes used to display the image
|
||||
type ImageScaling struct {
|
||||
// Horizontal Alignment of the image
|
||||
Halign Halign
|
||||
// Vertical Alignment of the image
|
||||
Valign Valign
|
||||
// Width Height used by scaling policy
|
||||
Width, Height float64
|
||||
// ScalingPolicy defines the scaling policy to applied to the image
|
||||
ScalingPolicy ScalingPolicy
|
||||
}
|
97
drawing/flattener.go
Normal file
97
drawing/flattener.go
Normal file
|
@ -0,0 +1,97 @@
|
|||
package drawing
|
||||
|
||||
// Liner receive segment definition
|
||||
type Liner interface {
|
||||
// LineTo Draw a line from the current position to the point (x, y)
|
||||
LineTo(x, y float64)
|
||||
}
|
||||
|
||||
// Flattener receive segment definition
|
||||
type Flattener interface {
|
||||
// MoveTo Start a New line from the point (x, y)
|
||||
MoveTo(x, y float64)
|
||||
// LineTo Draw a line from the current position to the point (x, y)
|
||||
LineTo(x, y float64)
|
||||
// LineJoin add the most recent starting point to close the path to create a polygon
|
||||
LineJoin()
|
||||
// Close add the most recent starting point to close the path to create a polygon
|
||||
Close()
|
||||
// End mark the current line as finished so we can draw caps
|
||||
End()
|
||||
}
|
||||
|
||||
// Flatten convert curves into straight segments keeping join segments info
|
||||
func Flatten(path *Path, flattener Flattener, scale float64) {
|
||||
// First Point
|
||||
var startX, startY float64
|
||||
// Current Point
|
||||
var x, y float64
|
||||
var i int
|
||||
for _, cmp := range path.Components {
|
||||
switch cmp {
|
||||
case MoveToComponent:
|
||||
x, y = path.Points[i], path.Points[i+1]
|
||||
startX, startY = x, y
|
||||
if i != 0 {
|
||||
flattener.End()
|
||||
}
|
||||
flattener.MoveTo(x, y)
|
||||
i += 2
|
||||
case LineToComponent:
|
||||
x, y = path.Points[i], path.Points[i+1]
|
||||
flattener.LineTo(x, y)
|
||||
flattener.LineJoin()
|
||||
i += 2
|
||||
case QuadCurveToComponent:
|
||||
// we include the previous point for the start of the curve
|
||||
TraceQuad(flattener, path.Points[i-2:], 0.5)
|
||||
x, y = path.Points[i+2], path.Points[i+3]
|
||||
flattener.LineTo(x, y)
|
||||
i += 4
|
||||
case CubicCurveToComponent:
|
||||
TraceCubic(flattener, path.Points[i-2:], 0.5)
|
||||
x, y = path.Points[i+4], path.Points[i+5]
|
||||
flattener.LineTo(x, y)
|
||||
i += 6
|
||||
case ArcToComponent:
|
||||
x, y = TraceArc(flattener, path.Points[i], path.Points[i+1], path.Points[i+2], path.Points[i+3], path.Points[i+4], path.Points[i+5], scale)
|
||||
flattener.LineTo(x, y)
|
||||
i += 6
|
||||
case CloseComponent:
|
||||
flattener.LineTo(startX, startY)
|
||||
flattener.Close()
|
||||
}
|
||||
}
|
||||
flattener.End()
|
||||
}
|
||||
|
||||
// SegmentedPath is a path of disparate point sectinos.
|
||||
type SegmentedPath struct {
|
||||
Points []float64
|
||||
}
|
||||
|
||||
// MoveTo implements the path interface.
|
||||
func (p *SegmentedPath) MoveTo(x, y float64) {
|
||||
p.Points = append(p.Points, x, y)
|
||||
// TODO need to mark this point as moveto
|
||||
}
|
||||
|
||||
// LineTo implements the path interface.
|
||||
func (p *SegmentedPath) LineTo(x, y float64) {
|
||||
p.Points = append(p.Points, x, y)
|
||||
}
|
||||
|
||||
// LineJoin implements the path interface.
|
||||
func (p *SegmentedPath) LineJoin() {
|
||||
// TODO need to mark the current point as linejoin
|
||||
}
|
||||
|
||||
// Close implements the path interface.
|
||||
func (p *SegmentedPath) Close() {
|
||||
// TODO Close
|
||||
}
|
||||
|
||||
// End implements the path interface.
|
||||
func (p *SegmentedPath) End() {
|
||||
// Nothing to do
|
||||
}
|
30
drawing/free_type_path.go
Normal file
30
drawing/free_type_path.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"github.com/golang/freetype/raster"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
// FtLineBuilder is a builder for freetype raster glyphs.
|
||||
type FtLineBuilder struct {
|
||||
Adder raster.Adder
|
||||
}
|
||||
|
||||
// MoveTo implements the path builder interface.
|
||||
func (liner FtLineBuilder) MoveTo(x, y float64) {
|
||||
liner.Adder.Start(fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)})
|
||||
}
|
||||
|
||||
// LineTo implements the path builder interface.
|
||||
func (liner FtLineBuilder) LineTo(x, y float64) {
|
||||
liner.Adder.Add1(fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)})
|
||||
}
|
||||
|
||||
// LineJoin implements the path builder interface.
|
||||
func (liner FtLineBuilder) LineJoin() {}
|
||||
|
||||
// Close implements the path builder interface.
|
||||
func (liner FtLineBuilder) Close() {}
|
||||
|
||||
// End implements the path builder interface.
|
||||
func (liner FtLineBuilder) End() {}
|
82
drawing/graphic_context.go
Normal file
82
drawing/graphic_context.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/golang/freetype/truetype"
|
||||
)
|
||||
|
||||
// GraphicContext describes the interface for the various backends (images, pdf, opengl, ...)
|
||||
type GraphicContext interface {
|
||||
// PathBuilder describes the interface for path drawing
|
||||
PathBuilder
|
||||
// BeginPath creates a new path
|
||||
BeginPath()
|
||||
// GetMatrixTransform returns the current transformation matrix
|
||||
GetMatrixTransform() Matrix
|
||||
// SetMatrixTransform sets the current transformation matrix
|
||||
SetMatrixTransform(tr Matrix)
|
||||
// ComposeMatrixTransform composes the current transformation matrix with tr
|
||||
ComposeMatrixTransform(tr Matrix)
|
||||
// Rotate applies a rotation to the current transformation matrix. angle is in radian.
|
||||
Rotate(angle float64)
|
||||
// Translate applies a translation to the current transformation matrix.
|
||||
Translate(tx, ty float64)
|
||||
// Scale applies a scale to the current transformation matrix.
|
||||
Scale(sx, sy float64)
|
||||
// SetStrokeColor sets the current stroke color
|
||||
SetStrokeColor(c color.Color)
|
||||
// SetFillColor sets the current fill color
|
||||
SetFillColor(c color.Color)
|
||||
// SetFillRule sets the current fill rule
|
||||
SetFillRule(f FillRule)
|
||||
// SetLineWidth sets the current line width
|
||||
SetLineWidth(lineWidth float64)
|
||||
// SetLineCap sets the current line cap
|
||||
SetLineCap(cap LineCap)
|
||||
// SetLineJoin sets the current line join
|
||||
SetLineJoin(join LineJoin)
|
||||
// SetLineDash sets the current dash
|
||||
SetLineDash(dash []float64, dashOffset float64)
|
||||
// SetFontSize sets the current font size
|
||||
SetFontSize(fontSize float64)
|
||||
// GetFontSize gets the current font size
|
||||
GetFontSize() float64
|
||||
// SetFont sets the font for the context
|
||||
SetFont(f *truetype.Font)
|
||||
// GetFont returns the current font
|
||||
GetFont() *truetype.Font
|
||||
// DrawImage draws the raster image in the current canvas
|
||||
DrawImage(image image.Image)
|
||||
// Save the context and push it to the context stack
|
||||
Save()
|
||||
// Restore remove the current context and restore the last one
|
||||
Restore()
|
||||
// Clear fills the current canvas with a default transparent color
|
||||
Clear()
|
||||
// ClearRect fills the specified rectangle with a default transparent color
|
||||
ClearRect(x1, y1, x2, y2 int)
|
||||
// SetDPI sets the current DPI
|
||||
SetDPI(dpi int)
|
||||
// GetDPI gets the current DPI
|
||||
GetDPI() int
|
||||
// GetStringBounds gets pixel bounds(dimensions) of given string
|
||||
GetStringBounds(s string) (left, top, right, bottom float64)
|
||||
// CreateStringPath creates a path from the string s at x, y
|
||||
CreateStringPath(text string, x, y float64) (cursor float64)
|
||||
// FillString draws the text at point (0, 0)
|
||||
FillString(text string) (cursor float64)
|
||||
// FillStringAt draws the text at the specified point (x, y)
|
||||
FillStringAt(text string, x, y float64) (cursor float64)
|
||||
// StrokeString draws the contour of the text at point (0, 0)
|
||||
StrokeString(text string) (cursor float64)
|
||||
// StrokeStringAt draws the contour of the text at point (x, y)
|
||||
StrokeStringAt(text string, x, y float64) (cursor float64)
|
||||
// Stroke strokes the paths with the color specified by SetStrokeColor
|
||||
Stroke(paths ...*Path)
|
||||
// Fill fills the paths with the color specified by SetFillColor
|
||||
Fill(paths ...*Path)
|
||||
// FillStroke first fills the paths and than strokes them
|
||||
FillStroke(paths ...*Path)
|
||||
}
|
13
drawing/image_filter.go
Normal file
13
drawing/image_filter.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package drawing
|
||||
|
||||
// ImageFilter defines the type of filter to use
|
||||
type ImageFilter int
|
||||
|
||||
const (
|
||||
// LinearFilter defines a linear filter
|
||||
LinearFilter ImageFilter = iota
|
||||
// BilinearFilter defines a bilinear filter
|
||||
BilinearFilter
|
||||
// BicubicFilter defines a bicubic filter
|
||||
BicubicFilter
|
||||
)
|
48
drawing/line.go
Normal file
48
drawing/line.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"image/draw"
|
||||
)
|
||||
|
||||
// PolylineBresenham draws a polyline to an image
|
||||
func PolylineBresenham(img draw.Image, c color.Color, s ...float64) {
|
||||
for i := 2; i < len(s); i += 2 {
|
||||
Bresenham(img, c, int(s[i-2]+0.5), int(s[i-1]+0.5), int(s[i]+0.5), int(s[i+1]+0.5))
|
||||
}
|
||||
}
|
||||
|
||||
// Bresenham draws a line between (x0, y0) and (x1, y1)
|
||||
func Bresenham(img draw.Image, color color.Color, x0, y0, x1, y1 int) {
|
||||
dx := abs(x1 - x0)
|
||||
dy := abs(y1 - y0)
|
||||
var sx, sy int
|
||||
if x0 < x1 {
|
||||
sx = 1
|
||||
} else {
|
||||
sx = -1
|
||||
}
|
||||
if y0 < y1 {
|
||||
sy = 1
|
||||
} else {
|
||||
sy = -1
|
||||
}
|
||||
err := dx - dy
|
||||
|
||||
var e2 int
|
||||
for {
|
||||
img.Set(x0, y0, color)
|
||||
if x0 == x1 && y0 == y1 {
|
||||
return
|
||||
}
|
||||
e2 = 2 * err
|
||||
if e2 > -dy {
|
||||
err = err - dy
|
||||
x0 = x0 + sx
|
||||
}
|
||||
if e2 < dx {
|
||||
err = err + dx
|
||||
y0 = y0 + sy
|
||||
}
|
||||
}
|
||||
}
|
220
drawing/matrix.go
Normal file
220
drawing/matrix.go
Normal file
|
@ -0,0 +1,220 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
// Matrix represents an affine transformation
|
||||
type Matrix [6]float64
|
||||
|
||||
const (
|
||||
epsilon = 1e-6
|
||||
)
|
||||
|
||||
// Determinant compute the determinant of the matrix
|
||||
func (tr Matrix) Determinant() float64 {
|
||||
return tr[0]*tr[3] - tr[1]*tr[2]
|
||||
}
|
||||
|
||||
// Transform applies the transformation matrix to points. It modify the points passed in parameter.
|
||||
func (tr Matrix) Transform(points []float64) {
|
||||
for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
|
||||
x := points[i]
|
||||
y := points[j]
|
||||
points[i] = x*tr[0] + y*tr[2] + tr[4]
|
||||
points[j] = x*tr[1] + y*tr[3] + tr[5]
|
||||
}
|
||||
}
|
||||
|
||||
// TransformPoint applies the transformation matrix to point. It returns the point the transformed point.
|
||||
func (tr Matrix) TransformPoint(x, y float64) (xres, yres float64) {
|
||||
xres = x*tr[0] + y*tr[2] + tr[4]
|
||||
yres = x*tr[1] + y*tr[3] + tr[5]
|
||||
return xres, yres
|
||||
}
|
||||
|
||||
func minMax(x, y float64) (min, max float64) {
|
||||
if x > y {
|
||||
return y, x
|
||||
}
|
||||
return x, y
|
||||
}
|
||||
|
||||
// TransformRectangle applies the transformation matrix to the rectangle represented by the min and the max point of the rectangle
|
||||
func (tr Matrix) TransformRectangle(x0, y0, x2, y2 float64) (nx0, ny0, nx2, ny2 float64) {
|
||||
points := []float64{x0, y0, x2, y0, x2, y2, x0, y2}
|
||||
tr.Transform(points)
|
||||
points[0], points[2] = minMax(points[0], points[2])
|
||||
points[4], points[6] = minMax(points[4], points[6])
|
||||
points[1], points[3] = minMax(points[1], points[3])
|
||||
points[5], points[7] = minMax(points[5], points[7])
|
||||
|
||||
nx0 = math.Min(points[0], points[4])
|
||||
ny0 = math.Min(points[1], points[5])
|
||||
nx2 = math.Max(points[2], points[6])
|
||||
ny2 = math.Max(points[3], points[7])
|
||||
return nx0, ny0, nx2, ny2
|
||||
}
|
||||
|
||||
// InverseTransform applies the transformation inverse matrix to the rectangle represented by the min and the max point of the rectangle
|
||||
func (tr Matrix) InverseTransform(points []float64) {
|
||||
d := tr.Determinant() // matrix determinant
|
||||
for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
|
||||
x := points[i]
|
||||
y := points[j]
|
||||
points[i] = ((x-tr[4])*tr[3] - (y-tr[5])*tr[2]) / d
|
||||
points[j] = ((y-tr[5])*tr[0] - (x-tr[4])*tr[1]) / d
|
||||
}
|
||||
}
|
||||
|
||||
// InverseTransformPoint applies the transformation inverse matrix to point. It returns the point the transformed point.
|
||||
func (tr Matrix) InverseTransformPoint(x, y float64) (xres, yres float64) {
|
||||
d := tr.Determinant() // matrix determinant
|
||||
xres = ((x-tr[4])*tr[3] - (y-tr[5])*tr[2]) / d
|
||||
yres = ((y-tr[5])*tr[0] - (x-tr[4])*tr[1]) / d
|
||||
return xres, yres
|
||||
}
|
||||
|
||||
// VectorTransform applies the transformation matrix to points without using the translation parameter of the affine matrix.
|
||||
// It modify the points passed in parameter.
|
||||
func (tr Matrix) VectorTransform(points []float64) {
|
||||
for i, j := 0, 1; j < len(points); i, j = i+2, j+2 {
|
||||
x := points[i]
|
||||
y := points[j]
|
||||
points[i] = x*tr[0] + y*tr[2]
|
||||
points[j] = x*tr[1] + y*tr[3]
|
||||
}
|
||||
}
|
||||
|
||||
// NewIdentityMatrix creates an identity transformation matrix.
|
||||
func NewIdentityMatrix() Matrix {
|
||||
return Matrix{1, 0, 0, 1, 0, 0}
|
||||
}
|
||||
|
||||
// NewTranslationMatrix creates a transformation matrix with a translation tx and ty translation parameter
|
||||
func NewTranslationMatrix(tx, ty float64) Matrix {
|
||||
return Matrix{1, 0, 0, 1, tx, ty}
|
||||
}
|
||||
|
||||
// NewScaleMatrix creates a transformation matrix with a sx, sy scale factor
|
||||
func NewScaleMatrix(sx, sy float64) Matrix {
|
||||
return Matrix{sx, 0, 0, sy, 0, 0}
|
||||
}
|
||||
|
||||
// NewRotationMatrix creates a rotation transformation matrix. angle is in radian
|
||||
func NewRotationMatrix(angle float64) Matrix {
|
||||
c := math.Cos(angle)
|
||||
s := math.Sin(angle)
|
||||
return Matrix{c, s, -s, c, 0, 0}
|
||||
}
|
||||
|
||||
// NewMatrixFromRects creates a transformation matrix, combining a scale and a translation, that transform rectangle1 into rectangle2.
|
||||
func NewMatrixFromRects(rectangle1, rectangle2 [4]float64) Matrix {
|
||||
xScale := (rectangle2[2] - rectangle2[0]) / (rectangle1[2] - rectangle1[0])
|
||||
yScale := (rectangle2[3] - rectangle2[1]) / (rectangle1[3] - rectangle1[1])
|
||||
xOffset := rectangle2[0] - (rectangle1[0] * xScale)
|
||||
yOffset := rectangle2[1] - (rectangle1[1] * yScale)
|
||||
return Matrix{xScale, 0, 0, yScale, xOffset, yOffset}
|
||||
}
|
||||
|
||||
// Inverse computes the inverse matrix
|
||||
func (tr *Matrix) Inverse() {
|
||||
d := tr.Determinant() // matrix determinant
|
||||
tr0, tr1, tr2, tr3, tr4, tr5 := tr[0], tr[1], tr[2], tr[3], tr[4], tr[5]
|
||||
tr[0] = tr3 / d
|
||||
tr[1] = -tr1 / d
|
||||
tr[2] = -tr2 / d
|
||||
tr[3] = tr0 / d
|
||||
tr[4] = (tr2*tr5 - tr3*tr4) / d
|
||||
tr[5] = (tr1*tr4 - tr0*tr5) / d
|
||||
}
|
||||
|
||||
// Copy copies the matrix.
|
||||
func (tr Matrix) Copy() Matrix {
|
||||
var result Matrix
|
||||
copy(result[:], tr[:])
|
||||
return result
|
||||
}
|
||||
|
||||
// Compose multiplies trToConcat x tr
|
||||
func (tr *Matrix) Compose(trToCompose Matrix) {
|
||||
tr0, tr1, tr2, tr3, tr4, tr5 := tr[0], tr[1], tr[2], tr[3], tr[4], tr[5]
|
||||
tr[0] = trToCompose[0]*tr0 + trToCompose[1]*tr2
|
||||
tr[1] = trToCompose[1]*tr3 + trToCompose[0]*tr1
|
||||
tr[2] = trToCompose[2]*tr0 + trToCompose[3]*tr2
|
||||
tr[3] = trToCompose[3]*tr3 + trToCompose[2]*tr1
|
||||
tr[4] = trToCompose[4]*tr0 + trToCompose[5]*tr2 + tr4
|
||||
tr[5] = trToCompose[5]*tr3 + trToCompose[4]*tr1 + tr5
|
||||
}
|
||||
|
||||
// Scale adds a scale to the matrix
|
||||
func (tr *Matrix) Scale(sx, sy float64) {
|
||||
tr[0] = sx * tr[0]
|
||||
tr[1] = sx * tr[1]
|
||||
tr[2] = sy * tr[2]
|
||||
tr[3] = sy * tr[3]
|
||||
}
|
||||
|
||||
// Translate adds a translation to the matrix
|
||||
func (tr *Matrix) Translate(tx, ty float64) {
|
||||
tr[4] = tx*tr[0] + ty*tr[2] + tr[4]
|
||||
tr[5] = ty*tr[3] + tx*tr[1] + tr[5]
|
||||
}
|
||||
|
||||
// Rotate adds a rotation to the matrix.
|
||||
func (tr *Matrix) Rotate(radians float64) {
|
||||
c := math.Cos(radians)
|
||||
s := math.Sin(radians)
|
||||
t0 := c*tr[0] + s*tr[2]
|
||||
t1 := s*tr[3] + c*tr[1]
|
||||
t2 := c*tr[2] - s*tr[0]
|
||||
t3 := c*tr[3] - s*tr[1]
|
||||
tr[0] = t0
|
||||
tr[1] = t1
|
||||
tr[2] = t2
|
||||
tr[3] = t3
|
||||
}
|
||||
|
||||
// GetTranslation gets the matrix traslation.
|
||||
func (tr Matrix) GetTranslation() (x, y float64) {
|
||||
return tr[4], tr[5]
|
||||
}
|
||||
|
||||
// GetScaling gets the matrix scaling.
|
||||
func (tr Matrix) GetScaling() (x, y float64) {
|
||||
return tr[0], tr[3]
|
||||
}
|
||||
|
||||
// GetScale computes a scale for the matrix
|
||||
func (tr Matrix) GetScale() float64 {
|
||||
x := 0.707106781*tr[0] + 0.707106781*tr[1]
|
||||
y := 0.707106781*tr[2] + 0.707106781*tr[3]
|
||||
return math.Sqrt(x*x + y*y)
|
||||
}
|
||||
|
||||
// ******************** Testing ********************
|
||||
|
||||
// Equals tests if a two transformation are equal. A tolerance is applied when comparing matrix elements.
|
||||
func (tr Matrix) Equals(tr2 Matrix) bool {
|
||||
for i := 0; i < 6; i = i + 1 {
|
||||
if !fequals(tr[i], tr2[i]) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// IsIdentity tests if a transformation is the identity transformation. A tolerance is applied when comparing matrix elements.
|
||||
func (tr Matrix) IsIdentity() bool {
|
||||
return fequals(tr[4], 0) && fequals(tr[5], 0) && tr.IsTranslation()
|
||||
}
|
||||
|
||||
// IsTranslation tests if a transformation is is a pure translation. A tolerance is applied when comparing matrix elements.
|
||||
func (tr Matrix) IsTranslation() bool {
|
||||
return fequals(tr[0], 1) && fequals(tr[1], 0) && fequals(tr[2], 0) && fequals(tr[3], 1)
|
||||
}
|
||||
|
||||
// fequals compares two floats. return true if the distance between the two floats is less than epsilon, false otherwise
|
||||
func fequals(float1, float2 float64) bool {
|
||||
return math.Abs(float1-float2) <= epsilon
|
||||
}
|
31
drawing/painter.go
Normal file
31
drawing/painter.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"golang.org/x/image/draw"
|
||||
"golang.org/x/image/math/f64"
|
||||
|
||||
"github.com/golang/freetype/raster"
|
||||
)
|
||||
|
||||
// Painter implements the freetype raster.Painter and has a SetColor method like the RGBAPainter
|
||||
type Painter interface {
|
||||
raster.Painter
|
||||
SetColor(color color.Color)
|
||||
}
|
||||
|
||||
// DrawImage draws an image into dest using an affine transformation matrix, an op and a filter
|
||||
func DrawImage(src image.Image, dest draw.Image, tr Matrix, op draw.Op, filter ImageFilter) {
|
||||
var transformer draw.Transformer
|
||||
switch filter {
|
||||
case LinearFilter:
|
||||
transformer = draw.NearestNeighbor
|
||||
case BilinearFilter:
|
||||
transformer = draw.BiLinear
|
||||
case BicubicFilter:
|
||||
transformer = draw.CatmullRom
|
||||
}
|
||||
transformer.Transform(dest, f64.Aff3{tr[0], tr[1], tr[4], tr[2], tr[3], tr[5]}, src, src.Bounds(), op, nil)
|
||||
}
|
186
drawing/path.go
Normal file
186
drawing/path.go
Normal file
|
@ -0,0 +1,186 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
// PathBuilder describes the interface for path drawing.
|
||||
type PathBuilder interface {
|
||||
// LastPoint returns the current point of the current sub path
|
||||
LastPoint() (x, y float64)
|
||||
// MoveTo creates a new subpath that start at the specified point
|
||||
MoveTo(x, y float64)
|
||||
// LineTo adds a line to the current subpath
|
||||
LineTo(x, y float64)
|
||||
// QuadCurveTo adds a quadratic Bézier curve to the current subpath
|
||||
QuadCurveTo(cx, cy, x, y float64)
|
||||
// CubicCurveTo adds a cubic Bézier curve to the current subpath
|
||||
CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64)
|
||||
// ArcTo adds an arc to the current subpath
|
||||
ArcTo(cx, cy, rx, ry, startAngle, angle float64)
|
||||
// Close creates a line from the current point to the last MoveTo
|
||||
// point (if not the same) and mark the path as closed so the
|
||||
// first and last lines join nicely.
|
||||
Close()
|
||||
}
|
||||
|
||||
// PathComponent represents component of a path
|
||||
type PathComponent int
|
||||
|
||||
const (
|
||||
// MoveToComponent is a MoveTo component in a Path
|
||||
MoveToComponent PathComponent = iota
|
||||
// LineToComponent is a LineTo component in a Path
|
||||
LineToComponent
|
||||
// QuadCurveToComponent is a QuadCurveTo component in a Path
|
||||
QuadCurveToComponent
|
||||
// CubicCurveToComponent is a CubicCurveTo component in a Path
|
||||
CubicCurveToComponent
|
||||
// ArcToComponent is a ArcTo component in a Path
|
||||
ArcToComponent
|
||||
// CloseComponent is a ArcTo component in a Path
|
||||
CloseComponent
|
||||
)
|
||||
|
||||
// Path stores points
|
||||
type Path struct {
|
||||
// Components is a slice of PathComponent in a Path and mark the role of each points in the Path
|
||||
Components []PathComponent
|
||||
// Points are combined with Components to have a specific role in the path
|
||||
Points []float64
|
||||
// Last Point of the Path
|
||||
x, y float64
|
||||
}
|
||||
|
||||
func (p *Path) appendToPath(cmd PathComponent, points ...float64) {
|
||||
p.Components = append(p.Components, cmd)
|
||||
p.Points = append(p.Points, points...)
|
||||
}
|
||||
|
||||
// LastPoint returns the current point of the current path
|
||||
func (p *Path) LastPoint() (x, y float64) {
|
||||
return p.x, p.y
|
||||
}
|
||||
|
||||
// MoveTo starts a new path at (x, y) position
|
||||
func (p *Path) MoveTo(x, y float64) {
|
||||
p.appendToPath(MoveToComponent, x, y)
|
||||
p.x = x
|
||||
p.y = y
|
||||
}
|
||||
|
||||
// LineTo adds a line to the current path
|
||||
func (p *Path) LineTo(x, y float64) {
|
||||
if len(p.Components) == 0 { //special case when no move has been done
|
||||
p.MoveTo(0, 0)
|
||||
}
|
||||
p.appendToPath(LineToComponent, x, y)
|
||||
p.x = x
|
||||
p.y = y
|
||||
}
|
||||
|
||||
// QuadCurveTo adds a quadratic bezier curve to the current path
|
||||
func (p *Path) QuadCurveTo(cx, cy, x, y float64) {
|
||||
if len(p.Components) == 0 { //special case when no move has been done
|
||||
p.MoveTo(0, 0)
|
||||
}
|
||||
p.appendToPath(QuadCurveToComponent, cx, cy, x, y)
|
||||
p.x = x
|
||||
p.y = y
|
||||
}
|
||||
|
||||
// CubicCurveTo adds a cubic bezier curve to the current path
|
||||
func (p *Path) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) {
|
||||
if len(p.Components) == 0 { //special case when no move has been done
|
||||
p.MoveTo(0, 0)
|
||||
}
|
||||
p.appendToPath(CubicCurveToComponent, cx1, cy1, cx2, cy2, x, y)
|
||||
p.x = x
|
||||
p.y = y
|
||||
}
|
||||
|
||||
// ArcTo adds an arc to the path
|
||||
func (p *Path) ArcTo(cx, cy, rx, ry, startAngle, delta float64) {
|
||||
endAngle := startAngle + delta
|
||||
clockWise := true
|
||||
if delta < 0 {
|
||||
clockWise = false
|
||||
}
|
||||
// normalize
|
||||
if clockWise {
|
||||
for endAngle < startAngle {
|
||||
endAngle += math.Pi * 2.0
|
||||
}
|
||||
} else {
|
||||
for startAngle < endAngle {
|
||||
startAngle += math.Pi * 2.0
|
||||
}
|
||||
}
|
||||
startX := cx + math.Cos(startAngle)*rx
|
||||
startY := cy + math.Sin(startAngle)*ry
|
||||
if len(p.Components) > 0 {
|
||||
p.LineTo(startX, startY)
|
||||
} else {
|
||||
p.MoveTo(startX, startY)
|
||||
}
|
||||
p.appendToPath(ArcToComponent, cx, cy, rx, ry, startAngle, delta)
|
||||
p.x = cx + math.Cos(endAngle)*rx
|
||||
p.y = cy + math.Sin(endAngle)*ry
|
||||
}
|
||||
|
||||
// Close closes the current path
|
||||
func (p *Path) Close() {
|
||||
p.appendToPath(CloseComponent)
|
||||
}
|
||||
|
||||
// Copy make a clone of the current path and return it
|
||||
func (p *Path) Copy() (dest *Path) {
|
||||
dest = new(Path)
|
||||
dest.Components = make([]PathComponent, len(p.Components))
|
||||
copy(dest.Components, p.Components)
|
||||
dest.Points = make([]float64, len(p.Points))
|
||||
copy(dest.Points, p.Points)
|
||||
dest.x, dest.y = p.x, p.y
|
||||
return dest
|
||||
}
|
||||
|
||||
// Clear reset the path
|
||||
func (p *Path) Clear() {
|
||||
p.Components = p.Components[0:0]
|
||||
p.Points = p.Points[0:0]
|
||||
return
|
||||
}
|
||||
|
||||
// IsEmpty returns true if the path is empty
|
||||
func (p *Path) IsEmpty() bool {
|
||||
return len(p.Components) == 0
|
||||
}
|
||||
|
||||
// String returns a debug text view of the path
|
||||
func (p *Path) String() string {
|
||||
s := ""
|
||||
j := 0
|
||||
for _, cmd := range p.Components {
|
||||
switch cmd {
|
||||
case MoveToComponent:
|
||||
s += fmt.Sprintf("MoveTo: %f, %f\n", p.Points[j], p.Points[j+1])
|
||||
j = j + 2
|
||||
case LineToComponent:
|
||||
s += fmt.Sprintf("LineTo: %f, %f\n", p.Points[j], p.Points[j+1])
|
||||
j = j + 2
|
||||
case QuadCurveToComponent:
|
||||
s += fmt.Sprintf("QuadCurveTo: %f, %f, %f, %f\n", p.Points[j], p.Points[j+1], p.Points[j+2], p.Points[j+3])
|
||||
j = j + 4
|
||||
case CubicCurveToComponent:
|
||||
s += fmt.Sprintf("CubicCurveTo: %f, %f, %f, %f, %f, %f\n", p.Points[j], p.Points[j+1], p.Points[j+2], p.Points[j+3], p.Points[j+4], p.Points[j+5])
|
||||
j = j + 6
|
||||
case ArcToComponent:
|
||||
s += fmt.Sprintf("ArcTo: %f, %f, %f, %f, %f, %f\n", p.Points[j], p.Points[j+1], p.Points[j+2], p.Points[j+3], p.Points[j+4], p.Points[j+5])
|
||||
j = j + 6
|
||||
case CloseComponent:
|
||||
s += "Close\n"
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
283
drawing/raster_graphic_context.go
Normal file
283
drawing/raster_graphic_context.go
Normal file
|
@ -0,0 +1,283 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
|
||||
"github.com/golang/freetype/raster"
|
||||
"github.com/golang/freetype/truetype"
|
||||
"golang.org/x/image/draw"
|
||||
"golang.org/x/image/font"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
// NewRasterGraphicContext creates a new Graphic context from an image.
|
||||
func NewRasterGraphicContext(img draw.Image) (*RasterGraphicContext, error) {
|
||||
var painter Painter
|
||||
switch selectImage := img.(type) {
|
||||
case *image.RGBA:
|
||||
painter = raster.NewRGBAPainter(selectImage)
|
||||
default:
|
||||
return nil, errors.New("NewRasterGraphicContext() :: invalid image type")
|
||||
}
|
||||
return NewRasterGraphicContextWithPainter(img, painter), nil
|
||||
}
|
||||
|
||||
// NewRasterGraphicContextWithPainter creates a new Graphic context from an image and a Painter (see Freetype-go)
|
||||
func NewRasterGraphicContextWithPainter(img draw.Image, painter Painter) *RasterGraphicContext {
|
||||
width, height := img.Bounds().Dx(), img.Bounds().Dy()
|
||||
return &RasterGraphicContext{
|
||||
NewStackGraphicContext(),
|
||||
img,
|
||||
painter,
|
||||
raster.NewRasterizer(width, height),
|
||||
raster.NewRasterizer(width, height),
|
||||
&truetype.GlyphBuf{},
|
||||
DefaultDPI,
|
||||
}
|
||||
}
|
||||
|
||||
// RasterGraphicContext is the implementation of GraphicContext for a raster image
|
||||
type RasterGraphicContext struct {
|
||||
*StackGraphicContext
|
||||
img draw.Image
|
||||
painter Painter
|
||||
fillRasterizer *raster.Rasterizer
|
||||
strokeRasterizer *raster.Rasterizer
|
||||
glyphBuf *truetype.GlyphBuf
|
||||
DPI float64
|
||||
}
|
||||
|
||||
// SetDPI sets the screen resolution in dots per inch.
|
||||
func (rgc *RasterGraphicContext) SetDPI(dpi float64) {
|
||||
rgc.DPI = dpi
|
||||
rgc.recalc()
|
||||
}
|
||||
|
||||
// GetDPI returns the resolution of the Image GraphicContext
|
||||
func (rgc *RasterGraphicContext) GetDPI() float64 {
|
||||
return rgc.DPI
|
||||
}
|
||||
|
||||
// Clear fills the current canvas with a default transparent color
|
||||
func (rgc *RasterGraphicContext) Clear() {
|
||||
width, height := rgc.img.Bounds().Dx(), rgc.img.Bounds().Dy()
|
||||
rgc.ClearRect(0, 0, width, height)
|
||||
}
|
||||
|
||||
// ClearRect fills the current canvas with a default transparent color at the specified rectangle
|
||||
func (rgc *RasterGraphicContext) ClearRect(x1, y1, x2, y2 int) {
|
||||
imageColor := image.NewUniform(rgc.current.FillColor)
|
||||
draw.Draw(rgc.img, image.Rect(x1, y1, x2, y2), imageColor, image.ZP, draw.Over)
|
||||
}
|
||||
|
||||
// DrawImage draws the raster image in the current canvas
|
||||
func (rgc *RasterGraphicContext) DrawImage(img image.Image) {
|
||||
DrawImage(img, rgc.img, rgc.current.Tr, draw.Over, BilinearFilter)
|
||||
}
|
||||
|
||||
// FillString draws the text at point (0, 0)
|
||||
func (rgc *RasterGraphicContext) FillString(text string) (cursor float64, err error) {
|
||||
cursor, err = rgc.FillStringAt(text, 0, 0)
|
||||
return
|
||||
}
|
||||
|
||||
// FillStringAt draws the text at the specified point (x, y)
|
||||
func (rgc *RasterGraphicContext) FillStringAt(text string, x, y float64) (cursor float64, err error) {
|
||||
cursor, err = rgc.CreateStringPath(text, x, y)
|
||||
rgc.Fill()
|
||||
return
|
||||
}
|
||||
|
||||
// StrokeString draws the contour of the text at point (0, 0)
|
||||
func (rgc *RasterGraphicContext) StrokeString(text string) (cursor float64, err error) {
|
||||
cursor, err = rgc.StrokeStringAt(text, 0, 0)
|
||||
return
|
||||
}
|
||||
|
||||
// StrokeStringAt draws the contour of the text at point (x, y)
|
||||
func (rgc *RasterGraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64, err error) {
|
||||
cursor, err = rgc.CreateStringPath(text, x, y)
|
||||
rgc.Stroke()
|
||||
return
|
||||
}
|
||||
|
||||
func (rgc *RasterGraphicContext) drawGlyph(glyph truetype.Index, dx, dy float64) error {
|
||||
if err := rgc.glyphBuf.Load(rgc.current.Font, fixed.Int26_6(rgc.current.Scale), glyph, font.HintingNone); err != nil {
|
||||
return err
|
||||
}
|
||||
e0 := 0
|
||||
for _, e1 := range rgc.glyphBuf.Ends {
|
||||
DrawContour(rgc, rgc.glyphBuf.Points[e0:e1], dx, dy)
|
||||
e0 = e1
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateStringPath creates a path from the string s at x, y, and returns the string width.
|
||||
// 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 (rgc *RasterGraphicContext) CreateStringPath(s string, x, y float64) (cursor float64, err error) {
|
||||
f := rgc.GetFont()
|
||||
if f == nil {
|
||||
err = errors.New("No font loaded, cannot continue")
|
||||
return
|
||||
}
|
||||
rgc.recalc()
|
||||
|
||||
startx := x
|
||||
prev, hasPrev := truetype.Index(0), false
|
||||
for _, rc := range s {
|
||||
index := f.Index(rc)
|
||||
if hasPrev {
|
||||
x += fUnitsToFloat64(f.Kern(fixed.Int26_6(rgc.current.Scale), prev, index))
|
||||
}
|
||||
err = rgc.drawGlyph(index, x, y)
|
||||
if err != nil {
|
||||
cursor = x - startx
|
||||
return
|
||||
}
|
||||
x += fUnitsToFloat64(f.HMetric(fixed.Int26_6(rgc.current.Scale), index).AdvanceWidth)
|
||||
prev, hasPrev = index, true
|
||||
}
|
||||
cursor = x - startx
|
||||
return
|
||||
}
|
||||
|
||||
// GetStringBounds returns the approximate pixel bounds of a string.
|
||||
func (rgc *RasterGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64, err error) {
|
||||
f := rgc.GetFont()
|
||||
if f == nil {
|
||||
err = errors.New("No font loaded, cannot continue")
|
||||
return
|
||||
}
|
||||
rgc.recalc()
|
||||
|
||||
left = math.MaxFloat64
|
||||
top = math.MaxFloat64
|
||||
|
||||
cursor := 0.0
|
||||
prev, hasPrev := truetype.Index(0), false
|
||||
for _, rc := range s {
|
||||
index := f.Index(rc)
|
||||
if hasPrev {
|
||||
cursor += fUnitsToFloat64(f.Kern(fixed.Int26_6(rgc.current.Scale), prev, index))
|
||||
}
|
||||
|
||||
if err = rgc.glyphBuf.Load(rgc.current.Font, fixed.Int26_6(rgc.current.Scale), index, font.HintingNone); err != nil {
|
||||
return
|
||||
}
|
||||
e0 := 0
|
||||
for _, e1 := range rgc.glyphBuf.Ends {
|
||||
ps := rgc.glyphBuf.Points[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)
|
||||
}
|
||||
e0 = e1
|
||||
}
|
||||
cursor += fUnitsToFloat64(f.HMetric(fixed.Int26_6(rgc.current.Scale), index).AdvanceWidth)
|
||||
prev, hasPrev = index, true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// recalc recalculates scale and bounds values from the font size, screen
|
||||
// resolution and font metrics, and invalidates the glyph cache.
|
||||
func (rgc *RasterGraphicContext) recalc() {
|
||||
rgc.current.Scale = rgc.current.FontSizePoints * float64(rgc.DPI)
|
||||
}
|
||||
|
||||
// SetFont sets the font used to draw text.
|
||||
func (rgc *RasterGraphicContext) SetFont(font *truetype.Font) {
|
||||
rgc.current.Font = font
|
||||
}
|
||||
|
||||
// GetFont returns the font used to draw text.
|
||||
func (rgc *RasterGraphicContext) GetFont() *truetype.Font {
|
||||
return rgc.current.Font
|
||||
}
|
||||
|
||||
// SetFontSize sets the font size in points (as in ``a 12 point font'').
|
||||
func (rgc *RasterGraphicContext) SetFontSize(fontSizePoints float64) {
|
||||
rgc.current.FontSizePoints = fontSizePoints
|
||||
rgc.recalc()
|
||||
}
|
||||
|
||||
func (rgc *RasterGraphicContext) paint(rasterizer *raster.Rasterizer, color color.Color) {
|
||||
rgc.painter.SetColor(color)
|
||||
rasterizer.Rasterize(rgc.painter)
|
||||
rasterizer.Clear()
|
||||
rgc.current.Path.Clear()
|
||||
}
|
||||
|
||||
// Stroke strokes the paths with the color specified by SetStrokeColor
|
||||
func (rgc *RasterGraphicContext) Stroke(paths ...*Path) {
|
||||
paths = append(paths, rgc.current.Path)
|
||||
rgc.strokeRasterizer.UseNonZeroWinding = true
|
||||
|
||||
stroker := NewLineStroker(rgc.current.Cap, rgc.current.Join, Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.strokeRasterizer}})
|
||||
stroker.HalfLineWidth = rgc.current.LineWidth / 2
|
||||
|
||||
var liner Flattener
|
||||
if rgc.current.Dash != nil && len(rgc.current.Dash) > 0 {
|
||||
liner = NewDashVertexConverter(rgc.current.Dash, rgc.current.DashOffset, stroker)
|
||||
} else {
|
||||
liner = stroker
|
||||
}
|
||||
for _, p := range paths {
|
||||
Flatten(p, liner, rgc.current.Tr.GetScale())
|
||||
}
|
||||
|
||||
rgc.paint(rgc.strokeRasterizer, rgc.current.StrokeColor)
|
||||
}
|
||||
|
||||
// Fill fills the paths with the color specified by SetFillColor
|
||||
func (rgc *RasterGraphicContext) Fill(paths ...*Path) {
|
||||
paths = append(paths, rgc.current.Path)
|
||||
rgc.fillRasterizer.UseNonZeroWinding = rgc.current.FillRule == FillRuleWinding
|
||||
|
||||
flattener := Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.fillRasterizer}}
|
||||
for _, p := range paths {
|
||||
Flatten(p, flattener, rgc.current.Tr.GetScale())
|
||||
}
|
||||
|
||||
rgc.paint(rgc.fillRasterizer, rgc.current.FillColor)
|
||||
}
|
||||
|
||||
// FillStroke first fills the paths and than strokes them
|
||||
func (rgc *RasterGraphicContext) FillStroke(paths ...*Path) {
|
||||
paths = append(paths, rgc.current.Path)
|
||||
rgc.fillRasterizer.UseNonZeroWinding = rgc.current.FillRule == FillRuleWinding
|
||||
rgc.strokeRasterizer.UseNonZeroWinding = true
|
||||
|
||||
flattener := Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.fillRasterizer}}
|
||||
|
||||
stroker := NewLineStroker(rgc.current.Cap, rgc.current.Join, Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.strokeRasterizer}})
|
||||
stroker.HalfLineWidth = rgc.current.LineWidth / 2
|
||||
|
||||
var liner Flattener
|
||||
if rgc.current.Dash != nil && len(rgc.current.Dash) > 0 {
|
||||
liner = NewDashVertexConverter(rgc.current.Dash, rgc.current.DashOffset, stroker)
|
||||
} else {
|
||||
liner = stroker
|
||||
}
|
||||
|
||||
demux := DemuxFlattener{Flatteners: []Flattener{flattener, liner}}
|
||||
for _, p := range paths {
|
||||
Flatten(p, demux, rgc.current.Tr.GetScale())
|
||||
}
|
||||
|
||||
// Fill
|
||||
rgc.paint(rgc.fillRasterizer, rgc.current.FillColor)
|
||||
// Stroke
|
||||
rgc.paint(rgc.strokeRasterizer, rgc.current.StrokeColor)
|
||||
}
|
211
drawing/stack_graphic_context.go
Normal file
211
drawing/stack_graphic_context.go
Normal file
|
@ -0,0 +1,211 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
|
||||
"github.com/golang/freetype/truetype"
|
||||
)
|
||||
|
||||
// StackGraphicContext is a context that does thngs.
|
||||
type StackGraphicContext struct {
|
||||
current *ContextStack
|
||||
}
|
||||
|
||||
// ContextStack is a graphic context implementation.
|
||||
type ContextStack struct {
|
||||
Tr Matrix
|
||||
Path *Path
|
||||
LineWidth float64
|
||||
Dash []float64
|
||||
DashOffset float64
|
||||
StrokeColor color.Color
|
||||
FillColor color.Color
|
||||
FillRule FillRule
|
||||
Cap LineCap
|
||||
Join LineJoin
|
||||
|
||||
FontSizePoints float64
|
||||
Font *truetype.Font
|
||||
|
||||
Scale float64
|
||||
|
||||
Previous *ContextStack
|
||||
}
|
||||
|
||||
// NewStackGraphicContext Create a new Graphic context from an image
|
||||
func NewStackGraphicContext() *StackGraphicContext {
|
||||
gc := &StackGraphicContext{}
|
||||
gc.current = new(ContextStack)
|
||||
gc.current.Tr = NewIdentityMatrix()
|
||||
gc.current.Path = new(Path)
|
||||
gc.current.LineWidth = 1.0
|
||||
gc.current.StrokeColor = image.Black
|
||||
gc.current.FillColor = image.White
|
||||
gc.current.Cap = RoundCap
|
||||
gc.current.FillRule = FillRuleEvenOdd
|
||||
gc.current.Join = RoundJoin
|
||||
gc.current.FontSizePoints = 10
|
||||
return gc
|
||||
}
|
||||
|
||||
// GetMatrixTransform returns the matrix transform.
|
||||
func (gc *StackGraphicContext) GetMatrixTransform() Matrix {
|
||||
return gc.current.Tr
|
||||
}
|
||||
|
||||
// SetMatrixTransform sets the matrix transform.
|
||||
func (gc *StackGraphicContext) SetMatrixTransform(tr Matrix) {
|
||||
gc.current.Tr = tr
|
||||
}
|
||||
|
||||
// ComposeMatrixTransform composes a transform into the current transform.
|
||||
func (gc *StackGraphicContext) ComposeMatrixTransform(tr Matrix) {
|
||||
gc.current.Tr.Compose(tr)
|
||||
}
|
||||
|
||||
// Rotate rotates the matrix transform by an angle in degrees.
|
||||
func (gc *StackGraphicContext) Rotate(angle float64) {
|
||||
gc.current.Tr.Rotate(angle)
|
||||
}
|
||||
|
||||
// Translate translates a transform.
|
||||
func (gc *StackGraphicContext) Translate(tx, ty float64) {
|
||||
gc.current.Tr.Translate(tx, ty)
|
||||
}
|
||||
|
||||
// Scale scales a transform.
|
||||
func (gc *StackGraphicContext) Scale(sx, sy float64) {
|
||||
gc.current.Tr.Scale(sx, sy)
|
||||
}
|
||||
|
||||
// SetStrokeColor sets the stroke color.
|
||||
func (gc *StackGraphicContext) SetStrokeColor(c color.Color) {
|
||||
gc.current.StrokeColor = c
|
||||
}
|
||||
|
||||
// SetFillColor sets the fill color.
|
||||
func (gc *StackGraphicContext) SetFillColor(c color.Color) {
|
||||
gc.current.FillColor = c
|
||||
}
|
||||
|
||||
// SetFillRule sets the fill rule.
|
||||
func (gc *StackGraphicContext) SetFillRule(f FillRule) {
|
||||
gc.current.FillRule = f
|
||||
}
|
||||
|
||||
// SetLineWidth sets the line width.
|
||||
func (gc *StackGraphicContext) SetLineWidth(lineWidth float64) {
|
||||
gc.current.LineWidth = lineWidth
|
||||
}
|
||||
|
||||
// SetLineCap sets the line cap.
|
||||
func (gc *StackGraphicContext) SetLineCap(cap LineCap) {
|
||||
gc.current.Cap = cap
|
||||
}
|
||||
|
||||
// SetLineJoin sets the line join.
|
||||
func (gc *StackGraphicContext) SetLineJoin(join LineJoin) {
|
||||
gc.current.Join = join
|
||||
}
|
||||
|
||||
// SetLineDash sets the line dash.
|
||||
func (gc *StackGraphicContext) SetLineDash(dash []float64, dashOffset float64) {
|
||||
gc.current.Dash = dash
|
||||
gc.current.DashOffset = dashOffset
|
||||
}
|
||||
|
||||
// SetFontSize sets the font size.
|
||||
func (gc *StackGraphicContext) SetFontSize(fontSizePoints float64) {
|
||||
gc.current.FontSizePoints = fontSizePoints
|
||||
}
|
||||
|
||||
// GetFontSize gets the font size.
|
||||
func (gc *StackGraphicContext) GetFontSize() float64 {
|
||||
return gc.current.FontSizePoints
|
||||
}
|
||||
|
||||
// SetFont sets the current font.
|
||||
func (gc *StackGraphicContext) SetFont(f *truetype.Font) {
|
||||
gc.current.Font = f
|
||||
}
|
||||
|
||||
// GetFont returns the font.
|
||||
func (gc *StackGraphicContext) GetFont() *truetype.Font {
|
||||
return gc.current.Font
|
||||
}
|
||||
|
||||
// BeginPath starts a new path.
|
||||
func (gc *StackGraphicContext) BeginPath() {
|
||||
gc.current.Path.Clear()
|
||||
}
|
||||
|
||||
// IsEmpty returns if the path is empty.
|
||||
func (gc *StackGraphicContext) IsEmpty() bool {
|
||||
return gc.current.Path.IsEmpty()
|
||||
}
|
||||
|
||||
// LastPoint returns the last point on the path.
|
||||
func (gc *StackGraphicContext) LastPoint() (x float64, y float64) {
|
||||
return gc.current.Path.LastPoint()
|
||||
}
|
||||
|
||||
// MoveTo moves the cursor for a path.
|
||||
func (gc *StackGraphicContext) MoveTo(x, y float64) {
|
||||
gc.current.Path.MoveTo(x, y)
|
||||
}
|
||||
|
||||
// LineTo draws a line.
|
||||
func (gc *StackGraphicContext) LineTo(x, y float64) {
|
||||
gc.current.Path.LineTo(x, y)
|
||||
}
|
||||
|
||||
// QuadCurveTo draws a quad curve.
|
||||
func (gc *StackGraphicContext) QuadCurveTo(cx, cy, x, y float64) {
|
||||
gc.current.Path.QuadCurveTo(cx, cy, x, y)
|
||||
}
|
||||
|
||||
// CubicCurveTo draws a cubic curve.
|
||||
func (gc *StackGraphicContext) CubicCurveTo(cx1, cy1, cx2, cy2, x, y float64) {
|
||||
gc.current.Path.CubicCurveTo(cx1, cy1, cx2, cy2, x, y)
|
||||
}
|
||||
|
||||
// ArcTo draws an arc.
|
||||
func (gc *StackGraphicContext) ArcTo(cx, cy, rx, ry, startAngle, delta float64) {
|
||||
gc.current.Path.ArcTo(cx, cy, rx, ry, startAngle, delta)
|
||||
}
|
||||
|
||||
// Close closes a path.
|
||||
func (gc *StackGraphicContext) Close() {
|
||||
gc.current.Path.Close()
|
||||
}
|
||||
|
||||
// Save pushes a context onto the stack.
|
||||
func (gc *StackGraphicContext) Save() {
|
||||
context := new(ContextStack)
|
||||
context.FontSizePoints = gc.current.FontSizePoints
|
||||
context.Font = gc.current.Font
|
||||
context.LineWidth = gc.current.LineWidth
|
||||
context.StrokeColor = gc.current.StrokeColor
|
||||
context.FillColor = gc.current.FillColor
|
||||
context.FillRule = gc.current.FillRule
|
||||
context.Dash = gc.current.Dash
|
||||
context.DashOffset = gc.current.DashOffset
|
||||
context.Cap = gc.current.Cap
|
||||
context.Join = gc.current.Join
|
||||
context.Path = gc.current.Path.Copy()
|
||||
context.Font = gc.current.Font
|
||||
context.Scale = gc.current.Scale
|
||||
copy(context.Tr[:], gc.current.Tr[:])
|
||||
context.Previous = gc.current
|
||||
gc.current = context
|
||||
}
|
||||
|
||||
// Restore restores the previous context.
|
||||
func (gc *StackGraphicContext) Restore() {
|
||||
if gc.current.Previous != nil {
|
||||
oldContext := gc.current
|
||||
gc.current = gc.current.Previous
|
||||
oldContext.Previous = nil
|
||||
}
|
||||
}
|
85
drawing/stroker.go
Normal file
85
drawing/stroker.go
Normal file
|
@ -0,0 +1,85 @@
|
|||
// Copyright 2010 The draw2d Authors. All rights reserved.
|
||||
// created: 13/12/2010 by Laurent Le Goff
|
||||
|
||||
package drawing
|
||||
|
||||
// NewLineStroker creates a new line stroker.
|
||||
func NewLineStroker(c LineCap, j LineJoin, flattener Flattener) *LineStroker {
|
||||
l := new(LineStroker)
|
||||
l.Flattener = flattener
|
||||
l.HalfLineWidth = 0.5
|
||||
l.Cap = c
|
||||
l.Join = j
|
||||
return l
|
||||
}
|
||||
|
||||
// LineStroker draws the stroke portion of a line.
|
||||
type LineStroker struct {
|
||||
Flattener Flattener
|
||||
HalfLineWidth float64
|
||||
Cap LineCap
|
||||
Join LineJoin
|
||||
vertices []float64
|
||||
rewind []float64
|
||||
x, y, nx, ny float64
|
||||
}
|
||||
|
||||
// MoveTo implements the path builder interface.
|
||||
func (l *LineStroker) MoveTo(x, y float64) {
|
||||
l.x, l.y = x, y
|
||||
}
|
||||
|
||||
// LineTo implements the path builder interface.
|
||||
func (l *LineStroker) LineTo(x, y float64) {
|
||||
l.line(l.x, l.y, x, y)
|
||||
}
|
||||
|
||||
// LineJoin implements the path builder interface.
|
||||
func (l *LineStroker) LineJoin() {}
|
||||
|
||||
func (l *LineStroker) line(x1, y1, x2, y2 float64) {
|
||||
dx := (x2 - x1)
|
||||
dy := (y2 - y1)
|
||||
d := vectorDistance(dx, dy)
|
||||
if d != 0 {
|
||||
nx := dy * l.HalfLineWidth / d
|
||||
ny := -(dx * l.HalfLineWidth / d)
|
||||
l.appendVertex(x1+nx, y1+ny, x2+nx, y2+ny, x1-nx, y1-ny, x2-nx, y2-ny)
|
||||
l.x, l.y, l.nx, l.ny = x2, y2, nx, ny
|
||||
}
|
||||
}
|
||||
|
||||
// Close implements the path builder interface.
|
||||
func (l *LineStroker) Close() {
|
||||
if len(l.vertices) > 1 {
|
||||
l.appendVertex(l.vertices[0], l.vertices[1], l.rewind[0], l.rewind[1])
|
||||
}
|
||||
}
|
||||
|
||||
// End implements the path builder interface.
|
||||
func (l *LineStroker) End() {
|
||||
if len(l.vertices) > 1 {
|
||||
l.Flattener.MoveTo(l.vertices[0], l.vertices[1])
|
||||
for i, j := 2, 3; j < len(l.vertices); i, j = i+2, j+2 {
|
||||
l.Flattener.LineTo(l.vertices[i], l.vertices[j])
|
||||
}
|
||||
}
|
||||
for i, j := len(l.rewind)-2, len(l.rewind)-1; j > 0; i, j = i-2, j-2 {
|
||||
l.Flattener.LineTo(l.rewind[i], l.rewind[j])
|
||||
}
|
||||
if len(l.vertices) > 1 {
|
||||
l.Flattener.LineTo(l.vertices[0], l.vertices[1])
|
||||
}
|
||||
l.Flattener.End()
|
||||
// reinit vertices
|
||||
l.vertices = l.vertices[0:0]
|
||||
l.rewind = l.rewind[0:0]
|
||||
l.x, l.y, l.nx, l.ny = 0, 0, 0, 0
|
||||
|
||||
}
|
||||
|
||||
func (l *LineStroker) appendVertex(vertices ...float64) {
|
||||
s := len(vertices) / 2
|
||||
l.vertices = append(l.vertices, vertices[:s]...)
|
||||
l.rewind = append(l.rewind, vertices[s:]...)
|
||||
}
|
67
drawing/text.go
Normal file
67
drawing/text.go
Normal file
|
@ -0,0 +1,67 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"github.com/golang/freetype/truetype"
|
||||
"golang.org/x/image/math/fixed"
|
||||
)
|
||||
|
||||
// DrawContour draws the given closed contour at the given sub-pixel offset.
|
||||
func DrawContour(path PathBuilder, ps []truetype.Point, dx, dy float64) {
|
||||
if len(ps) == 0 {
|
||||
return
|
||||
}
|
||||
startX, startY := pointToF64Point(ps[0])
|
||||
path.MoveTo(startX+dx, startY+dy)
|
||||
q0X, q0Y, on0 := startX, startY, true
|
||||
for _, p := range ps[1:] {
|
||||
qX, qY := pointToF64Point(p)
|
||||
on := p.Flags&0x01 != 0
|
||||
if on {
|
||||
if on0 {
|
||||
path.LineTo(qX+dx, qY+dy)
|
||||
} else {
|
||||
path.QuadCurveTo(q0X+dx, q0Y+dy, qX+dx, qY+dy)
|
||||
}
|
||||
} else if !on0 {
|
||||
midX := (q0X + qX) / 2
|
||||
midY := (q0Y + qY) / 2
|
||||
path.QuadCurveTo(q0X+dx, q0Y+dy, midX+dx, midY+dy)
|
||||
}
|
||||
q0X, q0Y, on0 = qX, qY, on
|
||||
}
|
||||
// Close the curve.
|
||||
if on0 {
|
||||
path.LineTo(startX+dx, startY+dy)
|
||||
} else {
|
||||
path.QuadCurveTo(q0X+dx, q0Y+dy, startX+dx, startY+dy)
|
||||
}
|
||||
}
|
||||
|
||||
// FontExtents contains font metric information.
|
||||
type FontExtents struct {
|
||||
// Ascent is the distance that the text
|
||||
// extends above the baseline.
|
||||
Ascent float64
|
||||
|
||||
// Descent is the distance that the text
|
||||
// extends below the baseline. The descent
|
||||
// is given as a negative value.
|
||||
Descent float64
|
||||
|
||||
// Height is the distance from the lowest
|
||||
// descending point to the highest ascending
|
||||
// point.
|
||||
Height float64
|
||||
}
|
||||
|
||||
// Extents returns the FontExtents for a font.
|
||||
// TODO needs to read this https://developer.apple.com/fonts/TrueType-Reference-Manual/RM02/Chap2.html#intro
|
||||
func Extents(font *truetype.Font, size float64) FontExtents {
|
||||
bounds := font.Bounds(fixed.Int26_6(font.FUnitsPerEm()))
|
||||
scale := size / float64(font.FUnitsPerEm())
|
||||
return FontExtents{
|
||||
Ascent: float64(bounds.Max.Y) * scale,
|
||||
Descent: float64(bounds.Min.Y) * scale,
|
||||
Height: float64(bounds.Max.Y-bounds.Min.Y) * scale,
|
||||
}
|
||||
}
|
39
drawing/transformer.go
Normal file
39
drawing/transformer.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
// Copyright 2010 The draw2d Authors. All rights reserved.
|
||||
// created: 13/12/2010 by Laurent Le Goff
|
||||
|
||||
package drawing
|
||||
|
||||
// Transformer apply the Matrix transformation tr
|
||||
type Transformer struct {
|
||||
Tr Matrix
|
||||
Flattener Flattener
|
||||
}
|
||||
|
||||
// MoveTo implements the path builder interface.
|
||||
func (t Transformer) MoveTo(x, y float64) {
|
||||
u := x*t.Tr[0] + y*t.Tr[2] + t.Tr[4]
|
||||
v := x*t.Tr[1] + y*t.Tr[3] + t.Tr[5]
|
||||
t.Flattener.MoveTo(u, v)
|
||||
}
|
||||
|
||||
// LineTo implements the path builder interface.
|
||||
func (t Transformer) LineTo(x, y float64) {
|
||||
u := x*t.Tr[0] + y*t.Tr[2] + t.Tr[4]
|
||||
v := x*t.Tr[1] + y*t.Tr[3] + t.Tr[5]
|
||||
t.Flattener.LineTo(u, v)
|
||||
}
|
||||
|
||||
// LineJoin implements the path builder interface.
|
||||
func (t Transformer) LineJoin() {
|
||||
t.Flattener.LineJoin()
|
||||
}
|
||||
|
||||
// Close implements the path builder interface.
|
||||
func (t Transformer) Close() {
|
||||
t.Flattener.Close()
|
||||
}
|
||||
|
||||
// End implements the path builder interface.
|
||||
func (t Transformer) End() {
|
||||
t.Flattener.End()
|
||||
}
|
68
drawing/util.go
Normal file
68
drawing/util.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package drawing
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"golang.org/x/image/math/fixed"
|
||||
|
||||
"github.com/golang/freetype/raster"
|
||||
"github.com/golang/freetype/truetype"
|
||||
)
|
||||
|
||||
// PixelsToPoints returns the points for a given number of pixels at a DPI.
|
||||
func PixelsToPoints(dpi, pixels float64) (points float64) {
|
||||
points = (pixels * 72.0) / dpi
|
||||
return
|
||||
}
|
||||
|
||||
// PointsToPixels returns the pixels for a given number of points at a DPI.
|
||||
func PointsToPixels(dpi, points float64) (pixels float64) {
|
||||
pixels = (points * dpi) / 72.0
|
||||
return
|
||||
}
|
||||
|
||||
func abs(i int) int {
|
||||
if i < 0 {
|
||||
return -i
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func distance(x1, y1, x2, y2 float64) float64 {
|
||||
return vectorDistance(x2-x1, y2-y1)
|
||||
}
|
||||
|
||||
func vectorDistance(dx, dy float64) float64 {
|
||||
return float64(math.Sqrt(dx*dx + dy*dy))
|
||||
}
|
||||
|
||||
func toFtCap(c LineCap) raster.Capper {
|
||||
switch c {
|
||||
case RoundCap:
|
||||
return raster.RoundCapper
|
||||
case ButtCap:
|
||||
return raster.ButtCapper
|
||||
case SquareCap:
|
||||
return raster.SquareCapper
|
||||
}
|
||||
return raster.RoundCapper
|
||||
}
|
||||
|
||||
func toFtJoin(j LineJoin) raster.Joiner {
|
||||
switch j {
|
||||
case RoundJoin:
|
||||
return raster.RoundJoiner
|
||||
case BevelJoin:
|
||||
return raster.BevelJoiner
|
||||
}
|
||||
return raster.RoundJoiner
|
||||
}
|
||||
|
||||
func pointToF64Point(p truetype.Point) (x, y float64) {
|
||||
return fUnitsToFloat64(p.X), -fUnitsToFloat64(p.Y)
|
||||
}
|
||||
|
||||
func fUnitsToFloat64(x fixed.Int26_6) float64 {
|
||||
scaled := x << 2
|
||||
return float64(scaled/256) + float64(scaled%256)/256.0
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue