Debian Codegraph @

drawing/util.go

raw symbols SCIP
1 package drawing
2
3 import (
4 "math"
5
6 "golang.org/x/image/math/fixed"
7
8 "github.com/golang/freetype/raster"
9 "github.com/golang/freetype/truetype"
10 )
11
12 // PixelsToPoints returns the points for a given number of pixels at a DPI.
13 func PixelsToPoints(dpi, pixels float64) (points float64) {
14 points = (pixels * 72.0) / dpi
15 return
16 }
17
18 // PointsToPixels returns the pixels for a given number of points at a DPI.
19 func PointsToPixels(dpi, points float64) (pixels float64) {
20 pixels = (points * dpi) / 72.0
21 return
22 }
23
24 func abs(i int) int {
25 if i < 0 {
26 return -i
27 }
28 return i
29 }
30
31 func distance(x1, y1, x2, y2 float64) float64 {
32 return vectorDistance(x2-x1, y2-y1)
33 }
34
35 func vectorDistance(dx, dy float64) float64 {
36 return float64(math.Sqrt(dx*dx + dy*dy))
37 }
38
39 func toFtCap(c LineCap) raster.Capper {
40 switch c {
41 case RoundCap:
42 return raster.RoundCapper
43 case ButtCap:
44 return raster.ButtCapper
45 case SquareCap:
46 return raster.SquareCapper
47 }
48 return raster.RoundCapper
49 }
50
51 func toFtJoin(j LineJoin) raster.Joiner {
52 switch j {
53 case RoundJoin:
54 return raster.RoundJoiner
55 case BevelJoin:
56 return raster.BevelJoiner
57 }
58 return raster.RoundJoiner
59 }
60
61 func pointToF64Point(p truetype.Point) (x, y float64) {
62 return fUnitsToFloat64(p.X), -fUnitsToFloat64(p.Y)
63 }
64
65 func fUnitsToFloat64(x fixed.Int26_6) float64 {
66 scaled := x << 2
67 return float64(scaled/256) + float64(scaled%256)/256.0
68 }