Debian Codegraph @

drawing/raster_graphic_context.go

raw symbols SCIP
1 package drawing
2
3 import (
4 "errors"
5 "image"
6 "image/color"
7 "math"
8
9 "github.com/golang/freetype/raster"
10 "github.com/golang/freetype/truetype"
11 "golang.org/x/image/draw"
12 "golang.org/x/image/font"
13 "golang.org/x/image/math/fixed"
14 )
15
16 // NewRasterGraphicContext creates a new Graphic context from an image.
17 func NewRasterGraphicContext(img draw.Image) (*RasterGraphicContext, error) {
18 var painter Painter
19 switch selectImage := img.(type) {
20 case *image.RGBA:
21 painter = raster.NewRGBAPainter(selectImage)
22 default:
23 return nil, errors.New("NewRasterGraphicContext() :: invalid image type")
24 }
25 return NewRasterGraphicContextWithPainter(img, painter), nil
26 }
27
28 // NewRasterGraphicContextWithPainter creates a new Graphic context from an image and a Painter (see Freetype-go)
29 func NewRasterGraphicContextWithPainter(img draw.Image, painter Painter) *RasterGraphicContext {
30 width, height := img.Bounds().Dx(), img.Bounds().Dy()
31 return &RasterGraphicContext{
32 NewStackGraphicContext(),
33 img,
34 painter,
35 raster.NewRasterizer(width, height),
36 raster.NewRasterizer(width, height),
37 &truetype.GlyphBuf{},
38 DefaultDPI,
39 }
40 }
41
42 // RasterGraphicContext is the implementation of GraphicContext for a raster image
43 type RasterGraphicContext struct {
44 *StackGraphicContext
45 img draw.Image
46 painter Painter
47 fillRasterizer *raster.Rasterizer
48 strokeRasterizer *raster.Rasterizer
49 glyphBuf *truetype.GlyphBuf
50 DPI float64
51 }
52
53 // SetDPI sets the screen resolution in dots per inch.
54 func (rgc *RasterGraphicContext) SetDPI(dpi float64) {
55 rgc.DPI = dpi
56 rgc.recalc()
57 }
58
59 // GetDPI returns the resolution of the Image GraphicContext
60 func (rgc *RasterGraphicContext) GetDPI() float64 {
61 return rgc.DPI
62 }
63
64 // Clear fills the current canvas with a default transparent color
65 func (rgc *RasterGraphicContext) Clear() {
66 width, height := rgc.img.Bounds().Dx(), rgc.img.Bounds().Dy()
67 rgc.ClearRect(0, 0, width, height)
68 }
69
70 // ClearRect fills the current canvas with a default transparent color at the specified rectangle
71 func (rgc *RasterGraphicContext) ClearRect(x1, y1, x2, y2 int) {
72 imageColor := image.NewUniform(rgc.current.FillColor)
73 draw.Draw(rgc.img, image.Rect(x1, y1, x2, y2), imageColor, image.ZP, draw.Over)
74 }
75
76 // DrawImage draws the raster image in the current canvas
77 func (rgc *RasterGraphicContext) DrawImage(img image.Image) {
78 DrawImage(img, rgc.img, rgc.current.Tr, draw.Over, BilinearFilter)
79 }
80
81 // FillString draws the text at point (0, 0)
82 func (rgc *RasterGraphicContext) FillString(text string) (cursor float64, err error) {
83 cursor, err = rgc.FillStringAt(text, 0, 0)
84 return
85 }
86
87 // FillStringAt draws the text at the specified point (x, y)
88 func (rgc *RasterGraphicContext) FillStringAt(text string, x, y float64) (cursor float64, err error) {
89 cursor, err = rgc.CreateStringPath(text, x, y)
90 rgc.Fill()
91 return
92 }
93
94 // StrokeString draws the contour of the text at point (0, 0)
95 func (rgc *RasterGraphicContext) StrokeString(text string) (cursor float64, err error) {
96 cursor, err = rgc.StrokeStringAt(text, 0, 0)
97 return
98 }
99
100 // StrokeStringAt draws the contour of the text at point (x, y)
101 func (rgc *RasterGraphicContext) StrokeStringAt(text string, x, y float64) (cursor float64, err error) {
102 cursor, err = rgc.CreateStringPath(text, x, y)
103 rgc.Stroke()
104 return
105 }
106
107 func (rgc *RasterGraphicContext) drawGlyph(glyph truetype.Index, dx, dy float64) error {
108 if err := rgc.glyphBuf.Load(rgc.current.Font, fixed.Int26_6(rgc.current.Scale), glyph, font.HintingNone); err != nil {
109 return err
110 }
111 e0 := 0
112 for _, e1 := range rgc.glyphBuf.Ends {
113 DrawContour(rgc, rgc.glyphBuf.Points[e0:e1], dx, dy)
114 e0 = e1
115 }
116 return nil
117 }
118
119 // CreateStringPath creates a path from the string s at x, y, and returns the string width.
120 // The text is placed so that the left edge of the em square of the first character of s
121 // and the baseline intersect at x, y. The majority of the affected pixels will be
122 // above and to the right of the point, but some may be below or to the left.
123 // For example, drawing a string that starts with a 'J' in an italic font may
124 // affect pixels below and left of the point.
125 func (rgc *RasterGraphicContext) CreateStringPath(s string, x, y float64) (cursor float64, err error) {
126 f := rgc.GetFont()
127 if f == nil {
128 err = errors.New("No font loaded, cannot continue")
129 return
130 }
131 rgc.recalc()
132
133 startx := x
134 prev, hasPrev := truetype.Index(0), false
135 for _, rc := range s {
136 index := f.Index(rc)
137 if hasPrev {
138 x += fUnitsToFloat64(f.Kern(fixed.Int26_6(rgc.current.Scale), prev, index))
139 }
140 err = rgc.drawGlyph(index, x, y)
141 if err != nil {
142 cursor = x - startx
143 return
144 }
145 x += fUnitsToFloat64(f.HMetric(fixed.Int26_6(rgc.current.Scale), index).AdvanceWidth)
146 prev, hasPrev = index, true
147 }
148 cursor = x - startx
149 return
150 }
151
152 // GetStringBounds returns the approximate pixel bounds of a string.
153 func (rgc *RasterGraphicContext) GetStringBounds(s string) (left, top, right, bottom float64, err error) {
154 f := rgc.GetFont()
155 if f == nil {
156 err = errors.New("No font loaded, cannot continue")
157 return
158 }
159 rgc.recalc()
160
161 left = math.MaxFloat64
162 top = math.MaxFloat64
163
164 cursor := 0.0
165 prev, hasPrev := truetype.Index(0), false
166 for _, rc := range s {
167 index := f.Index(rc)
168 if hasPrev {
169 cursor += fUnitsToFloat64(f.Kern(fixed.Int26_6(rgc.current.Scale), prev, index))
170 }
171
172 if err = rgc.glyphBuf.Load(rgc.current.Font, fixed.Int26_6(rgc.current.Scale), index, font.HintingNone); err != nil {
173 return
174 }
175 e0 := 0
176 for _, e1 := range rgc.glyphBuf.Ends {
177 ps := rgc.glyphBuf.Points[e0:e1]
178 for _, p := range ps {
179 x, y := pointToF64Point(p)
180 top = math.Min(top, y)
181 bottom = math.Max(bottom, y)
182 left = math.Min(left, x+cursor)
183 right = math.Max(right, x+cursor)
184 }
185 e0 = e1
186 }
187 cursor += fUnitsToFloat64(f.HMetric(fixed.Int26_6(rgc.current.Scale), index).AdvanceWidth)
188 prev, hasPrev = index, true
189 }
190 return
191 }
192
193 // recalc recalculates scale and bounds values from the font size, screen
194 // resolution and font metrics, and invalidates the glyph cache.
195 func (rgc *RasterGraphicContext) recalc() {
196 rgc.current.Scale = rgc.current.FontSizePoints * float64(rgc.DPI)
197 }
198
199 // SetFont sets the font used to draw text.
200 func (rgc *RasterGraphicContext) SetFont(font *truetype.Font) {
201 rgc.current.Font = font
202 }
203
204 // GetFont returns the font used to draw text.
205 func (rgc *RasterGraphicContext) GetFont() *truetype.Font {
206 return rgc.current.Font
207 }
208
209 // SetFontSize sets the font size in points (as in ``a 12 point font'').
210 func (rgc *RasterGraphicContext) SetFontSize(fontSizePoints float64) {
211 rgc.current.FontSizePoints = fontSizePoints
212 rgc.recalc()
213 }
214
215 func (rgc *RasterGraphicContext) paint(rasterizer *raster.Rasterizer, color color.Color) {
216 rgc.painter.SetColor(color)
217 rasterizer.Rasterize(rgc.painter)
218 rasterizer.Clear()
219 rgc.current.Path.Clear()
220 }
221
222 // Stroke strokes the paths with the color specified by SetStrokeColor
223 func (rgc *RasterGraphicContext) Stroke(paths ...*Path) {
224 paths = append(paths, rgc.current.Path)
225 rgc.strokeRasterizer.UseNonZeroWinding = true
226
227 stroker := NewLineStroker(rgc.current.Cap, rgc.current.Join, Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.strokeRasterizer}})
228 stroker.HalfLineWidth = rgc.current.LineWidth / 2
229
230 var liner Flattener
231 if rgc.current.Dash != nil && len(rgc.current.Dash) > 0 {
232 liner = NewDashVertexConverter(rgc.current.Dash, rgc.current.DashOffset, stroker)
233 } else {
234 liner = stroker
235 }
236 for _, p := range paths {
237 Flatten(p, liner, rgc.current.Tr.GetScale())
238 }
239
240 rgc.paint(rgc.strokeRasterizer, rgc.current.StrokeColor)
241 }
242
243 // Fill fills the paths with the color specified by SetFillColor
244 func (rgc *RasterGraphicContext) Fill(paths ...*Path) {
245 paths = append(paths, rgc.current.Path)
246 rgc.fillRasterizer.UseNonZeroWinding = rgc.current.FillRule == FillRuleWinding
247
248 flattener := Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.fillRasterizer}}
249 for _, p := range paths {
250 Flatten(p, flattener, rgc.current.Tr.GetScale())
251 }
252
253 rgc.paint(rgc.fillRasterizer, rgc.current.FillColor)
254 }
255
256 // FillStroke first fills the paths and than strokes them
257 func (rgc *RasterGraphicContext) FillStroke(paths ...*Path) {
258 paths = append(paths, rgc.current.Path)
259 rgc.fillRasterizer.UseNonZeroWinding = rgc.current.FillRule == FillRuleWinding
260 rgc.strokeRasterizer.UseNonZeroWinding = true
261
262 flattener := Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.fillRasterizer}}
263
264 stroker := NewLineStroker(rgc.current.Cap, rgc.current.Join, Transformer{Tr: rgc.current.Tr, Flattener: FtLineBuilder{Adder: rgc.strokeRasterizer}})
265 stroker.HalfLineWidth = rgc.current.LineWidth / 2
266
267 var liner Flattener
268 if rgc.current.Dash != nil && len(rgc.current.Dash) > 0 {
269 liner = NewDashVertexConverter(rgc.current.Dash, rgc.current.DashOffset, stroker)
270 } else {
271 liner = stroker
272 }
273
274 demux := DemuxFlattener{Flatteners: []Flattener{flattener, liner}}
275 for _, p := range paths {
276 Flatten(p, demux, rgc.current.Tr.GetScale())
277 }
278
279 // Fill
280 rgc.paint(rgc.fillRasterizer, rgc.current.FillColor)
281 // Stroke
282 rgc.paint(rgc.strokeRasterizer, rgc.current.StrokeColor)
283 }