Skip to content

Commit

Permalink
Fix issues from golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Mar 30, 2023
1 parent 30ef6b6 commit 2ffe71f
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 68 deletions.
10 changes: 5 additions & 5 deletions canvas.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"sort"
)

//const mmPerPx = 25.4 / 96.0
//const pxPerMm = 96.0 / 25.4
// const mmPerPx = 25.4 / 96.0
// const pxPerMm = 96.0 / 25.4
const mmPerPt = 25.4 / 72.0
const ptPerMm = 72.0 / 25.4
const mmPerInch = 25.4
Expand Down Expand Up @@ -106,10 +106,10 @@ type Paint struct {
// TODO: add hatch image and hatch path
}

func (a Paint) Equal(b Paint) bool {
if a.IsColor() && b.IsColor() && a.Color == b.Color {
func (paint Paint) Equal(other Paint) bool {
if paint.IsColor() && other.IsColor() && paint.Color == other.Color {
return true
} else if a.IsPattern() && b.IsPattern() && reflect.DeepEqual(a, b) {
} else if paint.IsPattern() && other.IsPattern() && reflect.DeepEqual(paint, other) {
return true
}
return false
Expand Down
5 changes: 2 additions & 3 deletions colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func (g *RadialGradient) At(x, y float64) color.RGBA {
return Transparent
}

// ColorSpace defines the color space within the RGB color model. All colors passed to this library are assumed to be in the sRGB color space, which is a ubiquitous assumption in most software. This works great for most applications, but fails when blending semi-transparent layers. See an elaborate explaination at https://blog.johnnovak.net/2016/09/21/what-every-coder-should-know-about-gamma/, which goes into depth of the problems of using sRGB for blending and the need for gamma correction. In short, we need to transform the colors, which are in the sRGB color space, to the linear color space, perform blending, and then transform them back to the sRGB color space.
// ColorSpace defines the color space within the RGB color model. All colors passed to this library are assumed to be in the sRGB color space, which is a ubiquitous assumption in most software. This works great for most applications, but fails when blending semi-transparent layers. See an elaborate explanation at https://blog.johnnovak.net/2016/09/21/what-every-coder-should-know-about-gamma/, which goes into depth of the problems of using sRGB for blending and the need for gamma correction. In short, we need to transform the colors, which are in the sRGB color space, to the linear color space, perform blending, and then transform them back to the sRGB color space.
// Unfortunately, almost all software does blending the wrong way (all PDF renderers and browsers I've tested), so by default this library will do the same by using LinearColorSpace which does no conversion from sRGB to linear and back but blends directly in sRGB. Or in other words, it assumes that colors are given in the linear color space and that the output image is expected to be in the linear color space as well. For technical correctness we should really be using the SRGBColorSpace, which will convert from sRGB to linear space, do blending in linear space, and then go back to sRGB space.
type ColorSpace interface {
ToLinear(color.Color) color.RGBA
Expand Down Expand Up @@ -233,9 +233,8 @@ func (SRGBColorSpace) ToLinear(col color.Color) color.RGBA {
// Formula from EXT_sRGB.
if c <= 0.04045 {
return c / 12.92
} else {
return math.Pow((c+0.055)/1.055, 2.4)
}
return math.Pow((c+0.055)/1.055, 2.4)
}

R, G, B, A := col.RGBA()
Expand Down
2 changes: 1 addition & 1 deletion font.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ func (family *FontFamily) Face(size float64, args ...interface{}) *FontFace {
}
if face.Style&FontItalic != 0 {
if face.Font.Post.ItalicAngle != 0 {
face.FauxItalic = math.Tan(float64(-face.Font.Post.ItalicAngle))
face.FauxItalic = math.Tan(-face.Font.Post.ItalicAngle)
} else {
face.FauxItalic = 0.3
}
Expand Down
7 changes: 4 additions & 3 deletions latex.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !latex
// +build !latex

package canvas
Expand Down Expand Up @@ -288,9 +289,9 @@ func (fs *dviFonts) Get(name string, scale float64) DVIFont {

// calculate size correction if the found font has a different font size than requested
fsize := scale * fontsize * mmPerPt / float64(sfnt.Head.UnitsPerEm)
fsizeCorr := fontsize / size
//fsizeCorr := fontsize / size
isItalic := 0 < len(fontname) && fontname[len(fontname)-1] == 'i'
fsizeCorr = 1.0
fsizeCorr := 1.0

f = &dviFont{sfnt, cmap, fsizeCorr * fsize, isItalic}
fs.font[name] = f
Expand All @@ -302,7 +303,7 @@ func (f *dviFont) Draw(p canvasFont.Pather, x, y float64, cid uint32) float64 {
r := f.cmap[cid]
gid := f.sfnt.GlyphIndex(r)
if f.italic {
x -= f.size * float64(f.sfnt.OS2.SxHeight) / 2.0 * math.Tan(-float64(f.sfnt.Post.ItalicAngle)*math.Pi/180.0)
x -= f.size * float64(f.sfnt.OS2.SxHeight) / 2.0 * math.Tan(-f.sfnt.Post.ItalicAngle*math.Pi/180.0)
}
_ = f.sfnt.GlyphPath(p, gid, 0, x, y, f.size, canvasFont.NoHinting)
return f.size * float64(f.sfnt.GlyphAdvance(gid)) // in mm
Expand Down
37 changes: 19 additions & 18 deletions latex_dvi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@ import (
)

// DVIFonts gets a font according to its font name and font size in points. Font names include:
// cmr: Roman (5--10pt)
// cmmi: Math Italic (5--10pt)
// cmsy: Math Symbols (5--10pt)
// cmex: Math Extension (10pt)
// cmss: Sans serif (10pt)
// cmssqi: Sans serif quote italic (8pt)
// cmssi: Sans serif Italic (10pt)
// cmbx: Bold Extended (10pt)
// cmtt: Typewriter (8--10pt)
// cmsltt: Slanted typewriter (10pt)
// cmsl: Slanted roman (8--10pt)
// cmti: Text italic (7--10pt)
// cmu: Unslanted text italic (10pt)
// cmmib: Bold math italic (10pt)
// cmbsy: Bold math symbols (10pt)
// cmcsc: Caps and Small caps (10pt)
// cmssbx: Sans serif bold extended (10pt)
// cmdunh: Dunhill style (10pt)
//
// cmr: Roman (5--10pt)
// cmmi: Math Italic (5--10pt)
// cmsy: Math Symbols (5--10pt)
// cmex: Math Extension (10pt)
// cmss: Sans serif (10pt)
// cmssqi: Sans serif quote italic (8pt)
// cmssi: Sans serif Italic (10pt)
// cmbx: Bold Extended (10pt)
// cmtt: Typewriter (8--10pt)
// cmsltt: Slanted typewriter (10pt)
// cmsl: Slanted roman (8--10pt)
// cmti: Text italic (7--10pt)
// cmu: Unslanted text italic (10pt)
// cmmib: Bold math italic (10pt)
// cmbsy: Bold math symbols (10pt)
// cmcsc: Caps and Small caps (10pt)
// cmssbx: Sans serif bold extended (10pt)
// cmdunh: Dunhill style (10pt)
type DVIFonts interface {
Get(string, float64) DVIFont
}
Expand Down
8 changes: 4 additions & 4 deletions path.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,11 @@ func (p *Path) interiorPoint() Point {

// Filling returns whether each subpath gets filled or not. A path may not be filling when it negates another path and depends on the FillRule. If a subpath is not closed, it is implicitly assumed to be closed. Subpaths may not (self-)intersect, use Settle to remove (self-)intersections.
func (p *Path) Filling(fillRule FillRule) []bool {
var filling []bool
for _, pi := range p.Split() {
ps := p.Split()
filling := make([]bool, len(ps))
for i, pi := range ps {
pos := pi.interiorPoint()
interior := p.Interior(pos.X, pos.Y, fillRule)
filling = append(filling, interior)
filling[i] = p.Interior(pos.X, pos.Y, fillRule)
}
return filling
}
Expand Down
23 changes: 6 additions & 17 deletions path_intersection.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,8 +1023,6 @@ func (zs intersections) appendSegment(segA int, a0 Point, a []float64, segB int,
} else if b[0] == ArcToCmd {
panic("unsupported intersection for arc-arc")
}
} else {
// MoveCmd
}

// swap A and B in the intersection found to match segments A and B of this function
Expand Down Expand Up @@ -1125,19 +1123,16 @@ func (v intersectionKind) String() string {
} else {
s = "AintoB"
}
if v&Tangent != 0 {
s += " Tangent"
}
return s
}

type intersectionParallel int

const (
NoParallel intersectionParallel = iota
AParallel // parallel along A
BParallel // parallel along B
Parallel // parallel along both
NoParallel intersectionParallel = 0
AParallel intersectionParallel = 1 // parallel along A
BParallel intersectionParallel = 2 // parallel along B
Parallel intersectionParallel = 3 // parallel along both
)

func (v intersectionParallel) String() string {
Expand Down Expand Up @@ -1270,10 +1265,7 @@ func (a intersectionASort) Less(i, j int) bool {
if Equal(zi.TA, zj.TA) {
// A intersects B twice at the same point, sort in case of parallel parts
// TODO: is this valid?? make sure that sorting is consistent to match with order when intersections are slightly separated. That is, you have outer and inner intersection pairs related to the parallel parts in between, that should be sorted as such (outer incoming, inner incoming, inner outgoing, outer outgoing) over A
if zi.Kind&BintoA != 0 {
return true
}
return false
return zi.Kind&BintoA != 0
}
return zi.TA < zj.TA
}
Expand Down Expand Up @@ -1307,10 +1299,7 @@ func (a intersectionArgBSort) Less(i, j int) bool {
if Equal(zi.TB, zj.TB) {
// A intersects B twice at the same point, sort in case of parallel parts
// TODO: is this valid?? make sure that sorting is consistent to match with order when intersections are slightly separated. That is, you have outer and inner intersection pairs related to the parallel parts in between, that should be sorted as such (outer incoming, inner incoming, inner outgoing, outer outgoing) over B
if zi.Kind&BintoA == 0 {
return true
}
return false
return zi.Kind&BintoA == 0
}
return zi.TB < zj.TB
}
Expand Down
10 changes: 5 additions & 5 deletions path_stroke.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,9 @@ func closeInnerBends(p *Path, indices []int, closed bool) {
p.d = append(p.d[:i:i], p.d[i+cmdLen(cmd):]...)
di += cmdLen(cmd)
}
} else if p.d[iPrev] == LineToCmd && p.d[iNext] == ArcToCmd {
} else if p.d[iPrev] == ArcToCmd && p.d[iNext] == LineToCmd {
} else if p.d[iPrev] == ArcToCmd && p.d[iNext] == ArcToCmd {
//} else if p.d[iPrev] == LineToCmd && p.d[iNext] == ArcToCmd {
//} else if p.d[iPrev] == ArcToCmd && p.d[iNext] == LineToCmd {
//} else if p.d[iPrev] == ArcToCmd && p.d[iNext] == ArcToCmd {
}
}
}
Expand Down Expand Up @@ -531,7 +531,7 @@ func optimizeMoveTo(p *Path) {
}
}

// Offset offsets the path to expand by w and returns a new path. If w is negative it will contract. Path must be closed. The tolerance is the maximum deviation from the actual offset when flattening Béziers and optimizing the path.
// Offset offsets the path to expand by w and returns a new path. If w is negative it will contract. Path must be closed. The tolerance is the maximum deviation from the actual offset when flattening Béziers and optimizing the path. Subpaths may not (self-)intersect, use Settle to remove (self-)intersections.
func (p *Path) Offset(w float64, fillRule FillRule, tolerance float64) *Path {
if Equal(w, 0.0) {
return p
Expand All @@ -548,7 +548,7 @@ func (p *Path) Offset(w float64, fillRule FillRule, tolerance float64) *Path {
if ps.CCW() {
useRHS = !useRHS
}
if w > 0.0 {
if 0.0 < w {
useRHS = !useRHS
}
if filling[i] {
Expand Down
3 changes: 1 addition & 2 deletions preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package canvas

import (
"bytes"
"fmt"
"image/color"
"io/ioutil"
"os"
Expand Down Expand Up @@ -144,7 +143,7 @@ func DrawPreviewWithAssets(ctx *Context, latin, arabic, devanagari, lenna []byte
}

// Draw an elliptic arc being dashed
ellipse, err := ParseSVG(fmt.Sprintf("A10 30 30 1 0 30 0z"))
ellipse, err := ParseSVG("A10 30 30 1 0 30 0z")
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion text.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (wm WritingMode) String() string {
return "Invalid(" + strconv.Itoa(int(wm)) + ")"
}

// TextOrientation specifies how horizontal text should be oriented within vertical text, or how vertical-only text should be layed out in horizontal text.
// TextOrientation specifies how horizontal text should be oriented within vertical text, or how vertical-only text should be laid out in horizontal text.
type TextOrientation int

// see TextOrientation
Expand Down
3 changes: 0 additions & 3 deletions text/linebreak_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ func TestLinebreak(t *testing.T) {
tt.items = append(tt.items, Penalty(0.0, -Infinity, true))

breakpoints := Linebreak(tt.items, lineWidth, 0)
for _, br := range breakpoints {
fmt.Println(br.Position, br.Ratio)
}
test.String(t, breakpoints[len(breakpoints)-2].String(), tt.breaks)
for i, ratio := range tt.ratios {
test.T(t, breakpoints[i].Ratio, ratio, fmt.Sprintf("ratio of break %d", i))
Expand Down
10 changes: 4 additions & 6 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,7 @@ func angleBetween(theta, lower, upper float64) bool {
}
theta = angleNorm(theta - lower + Epsilon)
upper = angleNorm(upper - lower + 2.0*Epsilon)
if theta <= upper {
return true
}
return false
return theta <= upper
}

// angleBetweenExclusive is true when theta is in range (lower,upper) excluding the end points. Angles can be outside the [0,2PI) range.
Expand Down Expand Up @@ -123,7 +120,7 @@ func (color CSSColor) String() string {
////////////////////////////////////////////////////////////////

func toP26_6(p Point) fixed.Point26_6 {
return fixed.Point26_6{toI26_6(p.X), toI26_6(p.Y)}
return fixed.Point26_6{X: toI26_6(p.X), Y: toI26_6(p.Y)}
}

func fromP26_6(f fixed.Point26_6) Point {
Expand Down Expand Up @@ -634,7 +631,8 @@ func solveQuadraticFormula(a, b, c float64) (float64, float64) {
// see https://www.geometrictools.com/Documentation/LowDegreePolynomialRoots.pdf
// see https://github.com/thelonious/kld-polynomial/blob/development/lib/Polynomial.js
func solveCubicFormula(a, b, c, d float64) (float64, float64, float64) {
x1, x2, x3 := math.NaN(), math.NaN(), math.NaN()
var x1, x2, x3 float64
x2, x3 = math.NaN(), math.NaN() // x1 is always set to a number below
if Equal(a, 0.0) {
x1, x2 = solveQuadraticFormula(b, c, d)
} else {
Expand Down

0 comments on commit 2ffe71f

Please sign in to comment.