2010-06-23 09:38:35 +02:00
|
|
|
// Copyright 2010 The Freetype-Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by your choice of either the
|
2010-08-03 03:07:23 +02:00
|
|
|
// FreeType License or the GNU General Public License version 2 (or
|
|
|
|
// any later version), both of which can be found in the LICENSE file.
|
2010-06-23 09:38:35 +02:00
|
|
|
|
|
|
|
package raster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
2018-12-06 17:08:43 +01:00
|
|
|
"git.gutmet.org/golang-image.git/math/fixed"
|
2015-08-17 02:26:09 +02:00
|
|
|
)
|
2010-06-30 07:10:17 +02:00
|
|
|
|
2010-06-23 09:38:35 +02:00
|
|
|
// maxAbs returns the maximum of abs(a) and abs(b).
|
2015-08-17 02:26:09 +02:00
|
|
|
func maxAbs(a, b fixed.Int26_6) fixed.Int26_6 {
|
2010-06-23 09:38:35 +02:00
|
|
|
if a < 0 {
|
|
|
|
a = -a
|
|
|
|
}
|
|
|
|
if b < 0 {
|
|
|
|
b = -b
|
|
|
|
}
|
|
|
|
if a < b {
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pNeg returns the vector -p, or equivalently p rotated by 180 degrees.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pNeg(p fixed.Point26_6) fixed.Point26_6 {
|
|
|
|
return fixed.Point26_6{-p.X, -p.Y}
|
2010-06-30 07:10:17 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pDot returns the dot product p·q.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pDot(p fixed.Point26_6, q fixed.Point26_6) fixed.Int52_12 {
|
2010-06-30 07:10:17 +02:00
|
|
|
px, py := int64(p.X), int64(p.Y)
|
|
|
|
qx, qy := int64(q.X), int64(q.Y)
|
2015-08-17 02:26:09 +02:00
|
|
|
return fixed.Int52_12(px*qx + py*qy)
|
2010-06-30 07:10:17 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pLen returns the length of the vector p.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pLen(p fixed.Point26_6) fixed.Int26_6 {
|
2010-06-23 09:38:35 +02:00
|
|
|
// TODO(nigeltao): use fixed point math.
|
|
|
|
x := float64(p.X)
|
|
|
|
y := float64(p.Y)
|
2015-08-17 02:26:09 +02:00
|
|
|
return fixed.Int26_6(math.Sqrt(x*x + y*y))
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pNorm returns the vector p normalized to the given length, or zero if p is
|
|
|
|
// degenerate.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pNorm(p fixed.Point26_6, length fixed.Int26_6) fixed.Point26_6 {
|
2015-08-13 13:17:25 +02:00
|
|
|
d := pLen(p)
|
2010-06-23 09:38:35 +02:00
|
|
|
if d == 0 {
|
2015-08-17 02:26:09 +02:00
|
|
|
return fixed.Point26_6{}
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
2010-06-30 07:10:17 +02:00
|
|
|
s, t := int64(length), int64(d)
|
|
|
|
x := int64(p.X) * s / t
|
|
|
|
y := int64(p.Y) * s / t
|
2015-08-17 02:26:09 +02:00
|
|
|
return fixed.Point26_6{fixed.Int26_6(x), fixed.Int26_6(y)}
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pRot45CW returns the vector p rotated clockwise by 45 degrees.
|
|
|
|
//
|
2010-06-30 07:10:17 +02:00
|
|
|
// Note that the Y-axis grows downwards, so {1, 0}.Rot45CW is {1/√2, 1/√2}.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pRot45CW(p fixed.Point26_6) fixed.Point26_6 {
|
2010-06-30 07:10:17 +02:00
|
|
|
// 181/256 is approximately 1/√2, or sin(π/4).
|
|
|
|
px, py := int64(p.X), int64(p.Y)
|
|
|
|
qx := (+px - py) * 181 / 256
|
|
|
|
qy := (+px + py) * 181 / 256
|
2015-08-17 02:26:09 +02:00
|
|
|
return fixed.Point26_6{fixed.Int26_6(qx), fixed.Int26_6(qy)}
|
2010-06-30 07:10:17 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pRot90CW returns the vector p rotated clockwise by 90 degrees.
|
|
|
|
//
|
2010-06-30 07:10:17 +02:00
|
|
|
// Note that the Y-axis grows downwards, so {1, 0}.Rot90CW is {0, 1}.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pRot90CW(p fixed.Point26_6) fixed.Point26_6 {
|
|
|
|
return fixed.Point26_6{-p.Y, p.X}
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pRot135CW returns the vector p rotated clockwise by 135 degrees.
|
|
|
|
//
|
2010-06-30 07:10:17 +02:00
|
|
|
// Note that the Y-axis grows downwards, so {1, 0}.Rot135CW is {-1/√2, 1/√2}.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pRot135CW(p fixed.Point26_6) fixed.Point26_6 {
|
2010-06-30 07:10:17 +02:00
|
|
|
// 181/256 is approximately 1/√2, or sin(π/4).
|
|
|
|
px, py := int64(p.X), int64(p.Y)
|
|
|
|
qx := (-px - py) * 181 / 256
|
|
|
|
qy := (+px - py) * 181 / 256
|
2015-08-17 02:26:09 +02:00
|
|
|
return fixed.Point26_6{fixed.Int26_6(qx), fixed.Int26_6(qy)}
|
2010-06-30 07:10:17 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pRot45CCW returns the vector p rotated counter-clockwise by 45 degrees.
|
|
|
|
//
|
2010-06-30 07:10:17 +02:00
|
|
|
// Note that the Y-axis grows downwards, so {1, 0}.Rot45CCW is {1/√2, -1/√2}.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pRot45CCW(p fixed.Point26_6) fixed.Point26_6 {
|
2010-06-30 07:10:17 +02:00
|
|
|
// 181/256 is approximately 1/√2, or sin(π/4).
|
|
|
|
px, py := int64(p.X), int64(p.Y)
|
|
|
|
qx := (+px + py) * 181 / 256
|
|
|
|
qy := (-px + py) * 181 / 256
|
2015-08-17 02:26:09 +02:00
|
|
|
return fixed.Point26_6{fixed.Int26_6(qx), fixed.Int26_6(qy)}
|
2010-06-30 07:10:17 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pRot90CCW returns the vector p rotated counter-clockwise by 90 degrees.
|
|
|
|
//
|
2010-06-30 07:10:17 +02:00
|
|
|
// Note that the Y-axis grows downwards, so {1, 0}.Rot90CCW is {0, -1}.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pRot90CCW(p fixed.Point26_6) fixed.Point26_6 {
|
|
|
|
return fixed.Point26_6{p.Y, -p.X}
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 13:17:25 +02:00
|
|
|
// pRot135CCW returns the vector p rotated counter-clockwise by 135 degrees.
|
|
|
|
//
|
2010-06-30 07:10:17 +02:00
|
|
|
// Note that the Y-axis grows downwards, so {1, 0}.Rot135CCW is {-1/√2, -1/√2}.
|
2015-08-17 02:26:09 +02:00
|
|
|
func pRot135CCW(p fixed.Point26_6) fixed.Point26_6 {
|
2010-06-30 07:10:17 +02:00
|
|
|
// 181/256 is approximately 1/√2, or sin(π/4).
|
|
|
|
px, py := int64(p.X), int64(p.Y)
|
|
|
|
qx := (-px + py) * 181 / 256
|
|
|
|
qy := (-px - py) * 181 / 256
|
2015-08-17 02:26:09 +02:00
|
|
|
return fixed.Point26_6{fixed.Int26_6(qx), fixed.Int26_6(qy)}
|
2010-06-30 07:10:17 +02:00
|
|
|
}
|
|
|
|
|
2010-06-23 09:38:35 +02:00
|
|
|
// An Adder accumulates points on a curve.
|
|
|
|
type Adder interface {
|
|
|
|
// Start starts a new curve at the given point.
|
2015-08-17 02:26:09 +02:00
|
|
|
Start(a fixed.Point26_6)
|
2010-06-23 09:38:35 +02:00
|
|
|
// Add1 adds a linear segment to the current curve.
|
2015-08-17 02:26:09 +02:00
|
|
|
Add1(b fixed.Point26_6)
|
2010-06-23 09:38:35 +02:00
|
|
|
// Add2 adds a quadratic segment to the current curve.
|
2015-08-17 02:26:09 +02:00
|
|
|
Add2(b, c fixed.Point26_6)
|
2010-06-23 09:38:35 +02:00
|
|
|
// Add3 adds a cubic segment to the current curve.
|
2015-08-17 02:26:09 +02:00
|
|
|
Add3(b, c, d fixed.Point26_6)
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Path is a sequence of curves, and a curve is a start point followed by a
|
|
|
|
// sequence of linear, quadratic or cubic segments.
|
2015-08-17 02:26:09 +02:00
|
|
|
type Path []fixed.Int26_6
|
2010-06-23 09:38:35 +02:00
|
|
|
|
|
|
|
// String returns a human-readable representation of a Path.
|
|
|
|
func (p Path) String() string {
|
|
|
|
s := ""
|
|
|
|
for i := 0; i < len(p); {
|
|
|
|
if i != 0 {
|
|
|
|
s += " "
|
|
|
|
}
|
|
|
|
switch p[i] {
|
|
|
|
case 0:
|
2015-08-17 02:26:09 +02:00
|
|
|
s += "S0" + fmt.Sprint([]fixed.Int26_6(p[i+1:i+3]))
|
2010-06-29 02:46:59 +02:00
|
|
|
i += 4
|
2010-06-23 09:38:35 +02:00
|
|
|
case 1:
|
2015-08-17 02:26:09 +02:00
|
|
|
s += "A1" + fmt.Sprint([]fixed.Int26_6(p[i+1:i+3]))
|
2010-06-29 02:46:59 +02:00
|
|
|
i += 4
|
2010-06-23 09:38:35 +02:00
|
|
|
case 2:
|
2015-08-17 02:26:09 +02:00
|
|
|
s += "A2" + fmt.Sprint([]fixed.Int26_6(p[i+1:i+5]))
|
2010-06-29 02:46:59 +02:00
|
|
|
i += 6
|
2010-06-23 09:38:35 +02:00
|
|
|
case 3:
|
2015-08-17 02:26:09 +02:00
|
|
|
s += "A3" + fmt.Sprint([]fixed.Int26_6(p[i+1:i+7]))
|
2010-06-29 02:46:59 +02:00
|
|
|
i += 8
|
2010-06-23 09:38:35 +02:00
|
|
|
default:
|
|
|
|
panic("freetype/raster: bad path")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear cancels any previous calls to p.Start or p.AddXxx.
|
|
|
|
func (p *Path) Clear() {
|
2015-02-24 00:52:08 +01:00
|
|
|
*p = (*p)[:0]
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts a new curve at the given point.
|
2015-08-17 02:26:09 +02:00
|
|
|
func (p *Path) Start(a fixed.Point26_6) {
|
2015-02-24 00:52:08 +01:00
|
|
|
*p = append(*p, 0, a.X, a.Y, 0)
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add1 adds a linear segment to the current curve.
|
2015-08-17 02:26:09 +02:00
|
|
|
func (p *Path) Add1(b fixed.Point26_6) {
|
2015-02-24 00:52:08 +01:00
|
|
|
*p = append(*p, 1, b.X, b.Y, 1)
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add2 adds a quadratic segment to the current curve.
|
2015-08-17 02:26:09 +02:00
|
|
|
func (p *Path) Add2(b, c fixed.Point26_6) {
|
2015-02-24 00:52:08 +01:00
|
|
|
*p = append(*p, 2, b.X, b.Y, c.X, c.Y, 2)
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add3 adds a cubic segment to the current curve.
|
2015-08-17 02:26:09 +02:00
|
|
|
func (p *Path) Add3(b, c, d fixed.Point26_6) {
|
2015-02-24 00:52:08 +01:00
|
|
|
*p = append(*p, 3, b.X, b.Y, c.X, c.Y, d.X, d.Y, 3)
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddPath adds the Path q to p.
|
|
|
|
func (p *Path) AddPath(q Path) {
|
2015-02-24 00:52:08 +01:00
|
|
|
*p = append(*p, q...)
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddStroke adds a stroked Path.
|
2015-08-17 02:26:09 +02:00
|
|
|
func (p *Path) AddStroke(q Path, width fixed.Int26_6, cr Capper, jr Joiner) {
|
2010-07-07 13:54:10 +02:00
|
|
|
Stroke(p, q, width, cr, jr)
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
|
|
|
|
2010-10-03 04:53:04 +02:00
|
|
|
// firstPoint returns the first point in a non-empty Path.
|
2015-08-17 02:26:09 +02:00
|
|
|
func (p Path) firstPoint() fixed.Point26_6 {
|
|
|
|
return fixed.Point26_6{p[1], p[2]}
|
2010-07-07 13:54:10 +02:00
|
|
|
}
|
|
|
|
|
2010-10-03 04:53:04 +02:00
|
|
|
// lastPoint returns the last point in a non-empty Path.
|
2015-08-17 02:26:09 +02:00
|
|
|
func (p Path) lastPoint() fixed.Point26_6 {
|
|
|
|
return fixed.Point26_6{p[len(p)-3], p[len(p)-2]}
|
2010-06-30 07:10:17 +02:00
|
|
|
}
|
|
|
|
|
2010-10-03 04:53:04 +02:00
|
|
|
// addPathReversed adds q reversed to p.
|
|
|
|
// For example, if q consists of a linear segment from A to B followed by a
|
|
|
|
// quadratic segment from B to C to D, then the values of q looks like:
|
|
|
|
// index: 01234567890123
|
|
|
|
// value: 0AA01BB12CCDD2
|
|
|
|
// So, when adding q backwards to p, we want to Add2(C, B) followed by Add1(A).
|
|
|
|
func addPathReversed(p Adder, q Path) {
|
|
|
|
if len(q) == 0 {
|
|
|
|
return
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|
2010-10-03 04:53:04 +02:00
|
|
|
i := len(q) - 1
|
2010-06-29 02:46:59 +02:00
|
|
|
for {
|
2010-10-03 04:53:04 +02:00
|
|
|
switch q[i] {
|
2010-06-29 02:46:59 +02:00
|
|
|
case 0:
|
2010-10-03 04:53:04 +02:00
|
|
|
return
|
2010-06-29 02:46:59 +02:00
|
|
|
case 1:
|
|
|
|
i -= 4
|
2015-08-17 02:26:09 +02:00
|
|
|
p.Add1(
|
|
|
|
fixed.Point26_6{q[i-2], q[i-1]},
|
|
|
|
)
|
2010-06-29 02:46:59 +02:00
|
|
|
case 2:
|
|
|
|
i -= 6
|
2015-08-17 02:26:09 +02:00
|
|
|
p.Add2(
|
|
|
|
fixed.Point26_6{q[i+2], q[i+3]},
|
|
|
|
fixed.Point26_6{q[i-2], q[i-1]},
|
|
|
|
)
|
2010-06-29 02:46:59 +02:00
|
|
|
case 3:
|
|
|
|
i -= 8
|
2015-08-17 02:26:09 +02:00
|
|
|
p.Add3(
|
|
|
|
fixed.Point26_6{q[i+4], q[i+5]},
|
|
|
|
fixed.Point26_6{q[i+2], q[i+3]},
|
|
|
|
fixed.Point26_6{q[i-2], q[i-1]},
|
|
|
|
)
|
2010-06-29 02:46:59 +02:00
|
|
|
default:
|
|
|
|
panic("freetype/raster: bad path")
|
|
|
|
}
|
|
|
|
}
|
2010-06-23 09:38:35 +02:00
|
|
|
}
|