some improvments

This commit is contained in:
Adrian Zürcher
2026-01-01 11:00:23 +01:00
parent 8f313c00f0
commit ef0778c8b3
20 changed files with 305 additions and 385 deletions

View File

@@ -3,6 +3,7 @@ package contentstream
import (
"bytes"
"fmt"
"strings"
"gitea.tecamino.com/paadi/pdfmerge/internal/pdf/core"
)
@@ -111,7 +112,7 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
}
inText := false
xPos, yPos := float64(-1), float64(-1)
txt := ""
var txt strings.Builder
for _, op := range *operations {
switch op.Operand {
case "BT":
@@ -122,7 +123,7 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
if op.Operand == "Td" || op.Operand == "TD" || op.Operand == "T*" {
// Move to next line...
txt += "\n"
txt.WriteString("\n")
}
if op.Operand == "Tm" {
if len(op.Params) != 6 {
@@ -147,7 +148,7 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
if yPos == -1 {
yPos = float64(*yfloat)
} else if yPos > float64(*yfloat) {
txt += "\n"
txt.WriteString("\n")
xPos = float64(*xfloat)
yPos = float64(*yfloat)
continue
@@ -155,7 +156,7 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
if xPos == -1 {
xPos = float64(*xfloat)
} else if xPos < float64(*xfloat) {
txt += "\t"
txt.WriteString("\t")
xPos = float64(*xfloat)
}
}
@@ -170,14 +171,14 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
for _, obj := range *paramList {
switch v := obj.(type) {
case *core.PdfObjectString:
txt += string(*v)
txt.WriteString(string(*v))
case *core.PdfObjectFloat:
if *v < -100 {
txt += " "
txt.WriteString(" ")
}
case *core.PdfObjectInteger:
if *v < -100 {
txt += " "
txt.WriteString(" ")
}
}
}
@@ -189,9 +190,9 @@ func (s *ContentStreamParser) ExtractText() (string, error) {
if !ok {
return "", fmt.Errorf("invalid parameter type, not string (%T)", op.Params[0])
}
txt += string(*param)
txt.WriteString(string(*param))
}
}
return txt, nil
return txt.String(), nil
}

View File

@@ -89,9 +89,9 @@ func NewCubicBezierPath() CubicBezierPath {
return bpath
}
func (this CubicBezierPath) AppendCurve(curve CubicBezierCurve) CubicBezierPath {
this.Curves = append(this.Curves, curve)
return this
func (c CubicBezierPath) AppendCurve(curve CubicBezierCurve) CubicBezierPath {
c.Curves = append(c.Curves, curve)
return c
}
func (bpath CubicBezierPath) Copy() CubicBezierPath {

View File

@@ -11,36 +11,36 @@ func NewPath() Path {
return path
}
func (this Path) AppendPoint(point Point) Path {
this.Points = append(this.Points, point)
return this
func (p Path) AppendPoint(point Point) Path {
p.Points = append(p.Points, point)
return p
}
func (this Path) RemovePoint(number int) Path {
if number < 1 || number > len(this.Points) {
return this
func (p Path) RemovePoint(number int) Path {
if number < 1 || number > len(p.Points) {
return p
}
idx := number - 1
this.Points = append(this.Points[:idx], this.Points[idx+1:]...)
return this
p.Points = append(p.Points[:idx], p.Points[idx+1:]...)
return p
}
func (this Path) Length() int {
return len(this.Points)
func (p Path) Length() int {
return len(p.Points)
}
func (this Path) GetPointNumber(number int) Point {
if number < 1 || number > len(this.Points) {
func (p Path) GetPointNumber(number int) Point {
if number < 1 || number > len(p.Points) {
return Point{}
}
return this.Points[number-1]
return p.Points[number-1]
}
func (path Path) Copy() Path {
func (p Path) Copy() Path {
pathcopy := Path{}
pathcopy.Points = []Point{}
for _, p := range path.Points {
for _, p := range p.Points {
pathcopy.Points = append(pathcopy.Points, p)
}
return pathcopy

View File

@@ -21,10 +21,10 @@ func (p Point) Add(dx, dy float64) Point {
}
// Add vector to a point.
func (this Point) AddVector(v Vector) Point {
this.X += v.Dx
this.Y += v.Dy
return this
func (p Point) AddVector(v Vector) Point {
p.X += v.Dx
p.Y += v.Dy
return p
}
func (p Point) String() string {

View File

@@ -43,13 +43,13 @@ func (v Vector) Rotate(phi float64) Vector {
}
// Change the sign of the vector: -vector.
func (this Vector) Flip() Vector {
mag := this.Magnitude()
theta := this.GetPolarAngle()
func (v Vector) Flip() Vector {
mag := v.Magnitude()
theta := v.GetPolarAngle()
this.Dx = mag * math.Cos(theta+math.Pi)
this.Dy = mag * math.Sin(theta+math.Pi)
return this
v.Dx = mag * math.Cos(theta+math.Pi)
v.Dy = mag * math.Sin(theta+math.Pi)
return v
}
func (v Vector) FlipY() Vector {
@@ -62,19 +62,19 @@ func (v Vector) FlipX() Vector {
return v
}
func (this Vector) Scale(factor float64) Vector {
mag := this.Magnitude()
theta := this.GetPolarAngle()
func (v Vector) Scale(factor float64) Vector {
mag := v.Magnitude()
theta := v.GetPolarAngle()
this.Dx = factor * mag * math.Cos(theta)
this.Dy = factor * mag * math.Sin(theta)
return this
v.Dx = factor * mag * math.Cos(theta)
v.Dy = factor * mag * math.Sin(theta)
return v
}
func (this Vector) Magnitude() float64 {
return math.Sqrt(math.Pow(this.Dx, 2.0) + math.Pow(this.Dy, 2.0))
func (v Vector) Magnitude() float64 {
return math.Sqrt(math.Pow(v.Dx, 2.0) + math.Pow(v.Dy, 2.0))
}
func (this Vector) GetPolarAngle() float64 {
return math.Atan2(this.Dy, this.Dx)
func (v Vector) GetPolarAngle() float64 {
return math.Atan2(v.Dy, v.Dx)
}