unipdf/pdf/extractor/point.go

72 lines
1.6 KiB
Go
Raw Normal View History

/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*
* Based on pdf/contentstream/draw/point.go
*/
2018-11-28 23:25:17 +00:00
// FIXME(peterwilliams97) Change to functional style. i.e. Return new value, don't mutate.
2018-10-09 19:05:38 +11:00
package extractor
import (
"fmt"
2018-11-26 17:26:27 +11:00
"github.com/unidoc/unidoc/common"
"github.com/unidoc/unidoc/pdf/contentstream"
)
2018-11-28 23:25:17 +00:00
// Point defines a point (X,Y) in Cartesian coordinates.
type Point struct {
X float64
Y float64
}
2018-11-28 23:25:17 +00:00
// NewPoint returns a Point at `x`, `y`.
func NewPoint(x, y float64) Point {
return Point{X: x, Y: y}
}
2018-11-28 23:25:17 +00:00
// Set sets `p` to coordinates `(x, y)`.
func (p *Point) Set(x, y float64) {
p.X, p.Y = x, y
}
// Transform transforms `p` by the affine transformation a, b, c, d, tx, ty.
func (p *Point) Transform(a, b, c, d, tx, ty float64) {
m := contentstream.NewMatrix(a, b, c, d, tx, ty)
p.transformByMatrix(m)
}
2018-11-28 23:25:17 +00:00
// Displace returns a new Point at location `p` + `delta`.
func (p Point) Displace(delta Point) Point {
return Point{p.X + delta.X, p.Y + delta.Y}
}
2018-11-28 23:25:17 +00:00
// Rotate rotates `p` by `theta` degrees and returns back.
2018-11-26 17:26:27 +11:00
func (p Point) Rotate(theta int) Point {
switch theta {
case 0:
p.X, p.Y = p.X, p.Y
case 90:
p.X, p.Y = -p.Y, p.X
case 180:
p.X, p.Y = -p.X, -p.Y
case 270:
p.X, p.Y = p.Y, -p.X
default:
common.Log.Debug("ERROR: Unsupported rotation %d", theta)
}
return p
}
// transformByMatrix transforms `p` by the affine transformation `m`.
func (p *Point) transformByMatrix(m contentstream.Matrix) {
p.X, p.Y = m.Transform(p.X, p.Y)
}
// String returns a string describing `p`.
func (p *Point) String() string {
2018-10-30 21:55:30 +11:00
return fmt.Sprintf("(%.2f,%.2f)", p.X, p.Y)
}