-
Notifications
You must be signed in to change notification settings - Fork 7
/
operators_graphics.go
489 lines (426 loc) · 13.7 KB
/
operators_graphics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2010 The postscript-go Authors. All rights reserved.
// created: 13/12/2010 by Laurent Le Goff
// Package ps
package ps
import (
"image/color"
"log"
"math"
"github.com/llgcode/draw2d"
)
//Path Construction Operators
func newpath(interpreter *Interpreter) {
interpreter.GetGraphicContext().BeginPath()
}
func closepath(interpreter *Interpreter) {
interpreter.GetGraphicContext().Close()
}
func currentpoint(interpreter *Interpreter) {
x, y := interpreter.GetGraphicContext().LastPoint()
interpreter.Push(x)
interpreter.Push(y)
}
func moveto(interpreter *Interpreter) {
y := interpreter.PopFloat()
x := interpreter.PopFloat()
interpreter.GetGraphicContext().MoveTo(x, y)
}
func rmoveto(interpreter *Interpreter) {
y := interpreter.PopFloat()
x := interpreter.PopFloat()
sx, sy := interpreter.GetGraphicContext().LastPoint()
interpreter.GetGraphicContext().MoveTo(sx+x, sy+y)
}
func lineto(interpreter *Interpreter) {
y := interpreter.PopFloat()
x := interpreter.PopFloat()
interpreter.GetGraphicContext().LineTo(x, y)
}
func rlineto(interpreter *Interpreter) {
y := interpreter.PopFloat()
x := interpreter.PopFloat()
sx, sy := interpreter.GetGraphicContext().LastPoint()
interpreter.GetGraphicContext().LineTo(sx+x, sy+y)
}
func curveto(interpreter *Interpreter) {
cy3 := interpreter.PopFloat()
cx3 := interpreter.PopFloat()
cy2 := interpreter.PopFloat()
cx2 := interpreter.PopFloat()
cy1 := interpreter.PopFloat()
cx1 := interpreter.PopFloat()
interpreter.GetGraphicContext().CubicCurveTo(cx1, cy1, cx2, cy2, cx3, cy3)
}
func rcurveto(interpreter *Interpreter) {
cy3 := interpreter.PopFloat()
cx3 := interpreter.PopFloat()
cy2 := interpreter.PopFloat()
cx2 := interpreter.PopFloat()
cy1 := interpreter.PopFloat()
cx1 := interpreter.PopFloat()
sx, sy := interpreter.GetGraphicContext().LastPoint()
interpreter.GetGraphicContext().CubicCurveTo(sx+cx1, sy+cy1, sx+cx2, sy+cy2, sx+cx3, sy+cy3)
}
func arc(interpreter *Interpreter) {
angle2 := interpreter.PopFloat() * (math.Pi / 180.0)
angle1 := interpreter.PopFloat() * (math.Pi / 180.0)
r := interpreter.PopFloat()
y := interpreter.PopFloat()
x := interpreter.PopFloat()
interpreter.GetGraphicContext().ArcTo(x, y, r, r, angle1, angle2-angle1)
}
func clippath(interpreter *Interpreter) {
//log.Printf("clippath not yet implemented")
}
func stroke(interpreter *Interpreter) {
interpreter.GetGraphicContext().Stroke()
}
func fill(interpreter *Interpreter) {
interpreter.GetGraphicContext().Fill()
}
func gsave(interpreter *Interpreter) {
interpreter.GetGraphicContext().Save()
}
func grestore(interpreter *Interpreter) {
interpreter.GetGraphicContext().Restore()
}
func setgray(interpreter *Interpreter) {
gray := interpreter.PopFloat()
color := color.RGBA{uint8(gray * 0xff), uint8(gray * 0xff), uint8(gray * 0xff), 0xff}
interpreter.GetGraphicContext().SetStrokeColor(color)
interpreter.GetGraphicContext().SetFillColor(color)
}
func setrgbcolor(interpreter *Interpreter) {
blue := interpreter.PopFloat()
green := interpreter.PopFloat()
red := interpreter.PopFloat()
color := color.RGBA{uint8(red * 0xff), uint8(green * 0xff), uint8(blue * 0xff), 0xff}
interpreter.GetGraphicContext().SetStrokeColor(color)
interpreter.GetGraphicContext().SetFillColor(color)
}
func hsbtorgb(hue, saturation, brightness float64) (red, green, blue int) {
var fr, fg, fb float64
if saturation == 0 {
fr, fg, fb = brightness, brightness, brightness
} else {
H := (hue - math.Floor(hue)) * 6
I := int(math.Floor(H))
F := H - float64(I)
M := brightness * (1 - saturation)
N := brightness * (1 - saturation*F)
K := brightness * (1 - saturation*(1-F))
switch I {
case 0:
fr = brightness
fg = K
fb = M
case 1:
fr = N
fg = brightness
fb = M
case 2:
fr = M
fg = brightness
fb = K
case 3:
fr = M
fg = N
fb = brightness
case 4:
fr = K
fg = M
fb = brightness
case 5:
fr = brightness
fg = M
fb = N
default:
fr, fb, fg = 0, 0, 0
}
}
red = int(fr*255. + 0.5)
green = int(fg*255. + 0.5)
blue = int(fb*255. + 0.5)
return
}
func sethsbcolor(interpreter *Interpreter) {
brightness := interpreter.PopFloat()
saturation := interpreter.PopFloat()
hue := interpreter.PopFloat()
red, green, blue := hsbtorgb(hue, saturation, brightness)
color := color.RGBA{uint8(red), uint8(green), uint8(blue), 0xff}
interpreter.GetGraphicContext().SetStrokeColor(color)
interpreter.GetGraphicContext().SetFillColor(color)
}
func setcmybcolor(interpreter *Interpreter) {
black := interpreter.PopFloat()
yellow := interpreter.PopFloat()
magenta := interpreter.PopFloat()
cyan := interpreter.PopFloat()
/* cyan = cyan / 255.0;
magenta = magenta / 255.0;
yellow = yellow / 255.0;
black = black / 255.0; */
red := cyan*(1.0-black) + black
green := magenta*(1.0-black) + black
blue := yellow*(1.0-black) + black
red = (1.0-red)*255.0 + 0.5
green = (1.0-green)*255.0 + 0.5
blue = (1.0-blue)*255.0 + 0.5
color := color.RGBA{uint8(red), uint8(green), uint8(blue), 0xff}
interpreter.GetGraphicContext().SetStrokeColor(color)
interpreter.GetGraphicContext().SetFillColor(color)
}
func setdash(interpreter *Interpreter) {
interpreter.PopInt() // offset
interpreter.PopArray() // dash
//log.Printf("setdash not yet implemented dash: %v, offset: %d \n", dash, offset)
}
func setlinejoin(interpreter *Interpreter) {
linejoin := interpreter.PopInt()
switch linejoin {
case 0:
interpreter.GetGraphicContext().SetLineJoin(draw2d.MiterJoin)
case 1:
interpreter.GetGraphicContext().SetLineJoin(draw2d.RoundJoin)
case 2:
interpreter.GetGraphicContext().SetLineJoin(draw2d.BevelJoin)
}
}
func setlinecap(interpreter *Interpreter) {
linecap := interpreter.PopInt()
switch linecap {
case 0:
interpreter.GetGraphicContext().SetLineCap(draw2d.ButtCap)
case 1:
interpreter.GetGraphicContext().SetLineCap(draw2d.RoundCap)
case 2:
interpreter.GetGraphicContext().SetLineCap(draw2d.SquareCap)
}
}
func setmiterlimit(interpreter *Interpreter) {
interpreter.PopInt()
//log.Printf("setmiterlimit not yet implemented")
}
func setlinewidth(interpreter *Interpreter) {
interpreter.GetGraphicContext().SetLineWidth(interpreter.PopFloat())
}
func showpage(interpreter *Interpreter) {
//log.Printf("showpage may be an implementation specific, override show page to generate multi page images")
}
func show(interpreter *Interpreter) {
s := interpreter.PopString()
interpreter.GetGraphicContext().FillString(s)
log.Printf("show not really implemented")
}
//ax ay string ashow – -> Add (ax , ay) to width of each glyph while showing string
func ashow(interpreter *Interpreter) {
log.Printf("ashow not really implemented")
s := interpreter.PopString()
interpreter.PopFloat()
interpreter.PopFloat()
interpreter.GetGraphicContext().FillString(s)
}
func findfont(interpreter *Interpreter) {
log.Printf("findfont not yet implemented")
}
func scalefont(interpreter *Interpreter) {
log.Printf("scalefont not yet implemented")
}
func setfont(interpreter *Interpreter) {
log.Printf("setfont not yet implemented")
}
func stringwidth(interpreter *Interpreter) {
interpreter.Push(10.0)
interpreter.Push(10.0)
log.Printf("stringwidth not yet implemented")
}
func setflat(interpreter *Interpreter) {
interpreter.Pop()
//log.Printf("setflat not yet implemented")
}
func currentflat(interpreter *Interpreter) {
interpreter.Push(1.0)
//log.Printf("currentflat not yet implemented")
}
// Coordinate System and Matrix operators
func matrix(interpreter *Interpreter) {
interpreter.Push(draw2d.NewIdentityMatrix())
}
func initmatrix(interpreter *Interpreter) {
interpreter.Push(draw2d.NewIdentityMatrix())
}
func identmatrix(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.Matrix)
ident := draw2d.NewIdentityMatrix()
copy(tr[:], ident[:])
interpreter.Push(tr)
}
func defaultmatrix(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.Matrix)
ident := draw2d.NewIdentityMatrix()
copy(tr[:], ident[:])
interpreter.Push(tr)
}
func currentmatrix(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.Matrix)
ctm := interpreter.GetGraphicContext().GetMatrixTransform()
copy(tr[:], ctm[:])
interpreter.Push(tr)
}
func setmatrix(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.Matrix)
interpreter.GetGraphicContext().SetMatrixTransform(tr)
}
func concat(interpreter *Interpreter) {
tr := interpreter.Pop().(draw2d.Matrix)
interpreter.GetGraphicContext().ComposeMatrixTransform(tr)
}
func concatmatrix(interpreter *Interpreter) {
tr3 := interpreter.Pop().(draw2d.Matrix)
tr2 := interpreter.Pop().(draw2d.Matrix)
tr1 := interpreter.Pop().(draw2d.Matrix)
result := tr2.Copy()
result.Compose(tr1)
copy(tr3[:], result[:])
interpreter.Push(tr3)
}
func transform(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.Matrix)
var y float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
y = value.(float64)
} else {
y = interpreter.PopFloat()
}
x := interpreter.PopFloat()
x, y = matrix.TransformPoint(x, y)
interpreter.Push(x)
interpreter.Push(y)
}
func itransform(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.Matrix)
var y float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
y = value.(float64)
} else {
y = interpreter.PopFloat()
}
x := interpreter.PopFloat()
x, y = matrix.InverseTransformPoint(x, y)
interpreter.Push(x)
interpreter.Push(y)
}
func translate(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.Matrix)
var y float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
y = value.(float64)
} else {
y = interpreter.PopFloat()
}
x := interpreter.PopFloat()
if !ok {
interpreter.GetGraphicContext().Translate(x, y)
} else {
result := matrix.Copy()
result.Translate(x, y)
interpreter.Push(result)
}
}
func rotate(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.Matrix)
var angle float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
angle = value.(float64) * math.Pi / 180
} else {
angle = interpreter.PopFloat() * math.Pi / 180
}
if !ok {
interpreter.GetGraphicContext().Rotate(angle)
} else {
result := matrix.Copy()
result.Rotate(angle)
interpreter.Push(result)
}
}
func scale(interpreter *Interpreter) {
value := interpreter.Pop()
matrix, ok := value.(draw2d.Matrix)
var y float64
if !ok {
matrix = interpreter.GetGraphicContext().GetMatrixTransform()
y = value.(float64)
} else {
y = interpreter.PopFloat()
}
x := interpreter.PopFloat()
if !ok {
interpreter.GetGraphicContext().Scale(x, y)
} else {
result := matrix.Copy()
result.Scale(x, y)
interpreter.Push(result)
}
}
func initDrawingOperators(interpreter *Interpreter) {
interpreter.SystemDefine("stroke", NewOperator(stroke))
interpreter.SystemDefine("fill", NewOperator(fill))
interpreter.SystemDefine("show", NewOperator(show))
interpreter.SystemDefine("ashow", NewOperator(ashow))
interpreter.SystemDefine("showpage", NewOperator(showpage))
interpreter.SystemDefine("findfont", NewOperator(findfont))
interpreter.SystemDefine("scalefont", NewOperator(scalefont))
interpreter.SystemDefine("setfont", NewOperator(setfont))
interpreter.SystemDefine("stringwidth", NewOperator(stringwidth))
// Graphic state operators
interpreter.SystemDefine("gsave", NewOperator(gsave))
interpreter.SystemDefine("grestore", NewOperator(grestore))
interpreter.SystemDefine("setrgbcolor", NewOperator(setrgbcolor))
interpreter.SystemDefine("sethsbcolor", NewOperator(sethsbcolor))
interpreter.SystemDefine("setcmybcolor", NewOperator(setcmybcolor))
interpreter.SystemDefine("setcmykcolor", NewOperator(setcmybcolor))
interpreter.SystemDefine("setgray", NewOperator(setgray))
interpreter.SystemDefine("setdash", NewOperator(setdash))
interpreter.SystemDefine("setlinejoin", NewOperator(setlinejoin))
interpreter.SystemDefine("setlinecap", NewOperator(setlinecap))
interpreter.SystemDefine("setmiterlimit", NewOperator(setmiterlimit))
interpreter.SystemDefine("setlinewidth", NewOperator(setlinewidth))
// Graphic state operators device dependent
interpreter.SystemDefine("setflat", NewOperator(setflat))
interpreter.SystemDefine("currentflat", NewOperator(currentflat))
// Coordinate System and Matrix operators
interpreter.SystemDefine("matrix", NewOperator(matrix))
interpreter.SystemDefine("initmatrix", NewOperator(initmatrix))
interpreter.SystemDefine("identmatrix", NewOperator(identmatrix))
interpreter.SystemDefine("defaultmatrix", NewOperator(defaultmatrix))
interpreter.SystemDefine("currentmatrix", NewOperator(currentmatrix))
interpreter.SystemDefine("setmatrix", NewOperator(setmatrix))
interpreter.SystemDefine("concat", NewOperator(concat))
interpreter.SystemDefine("concatmatrix", NewOperator(concatmatrix))
interpreter.SystemDefine("transform", NewOperator(transform))
interpreter.SystemDefine("itransform", NewOperator(itransform))
interpreter.SystemDefine("translate", NewOperator(translate))
interpreter.SystemDefine("rotate", NewOperator(rotate))
interpreter.SystemDefine("scale", NewOperator(scale))
//Path Construction Operators
interpreter.SystemDefine("newpath", NewOperator(newpath))
interpreter.SystemDefine("closepath", NewOperator(closepath))
interpreter.SystemDefine("currentpoint", NewOperator(currentpoint))
interpreter.SystemDefine("moveto", NewOperator(moveto))
interpreter.SystemDefine("rmoveto", NewOperator(rmoveto))
interpreter.SystemDefine("lineto", NewOperator(lineto))
interpreter.SystemDefine("rlineto", NewOperator(rlineto))
interpreter.SystemDefine("curveto", NewOperator(curveto))
interpreter.SystemDefine("rcurveto", NewOperator(rcurveto))
interpreter.SystemDefine("arc", NewOperator(arc))
interpreter.SystemDefine("clippath", NewOperator(clippath))
}