postsvg 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.rubocop.yml +19 -0
  3. data/.rubocop_todo.yml +141 -0
  4. data/Gemfile +15 -0
  5. data/LICENSE +25 -0
  6. data/README.adoc +473 -0
  7. data/Rakefile +10 -0
  8. data/docs/POSTSCRIPT.adoc +13 -0
  9. data/docs/postscript/fundamentals.adoc +356 -0
  10. data/docs/postscript/graphics-model.adoc +406 -0
  11. data/docs/postscript/implementation-notes.adoc +314 -0
  12. data/docs/postscript/index.adoc +153 -0
  13. data/docs/postscript/operators/arithmetic.adoc +461 -0
  14. data/docs/postscript/operators/control-flow.adoc +230 -0
  15. data/docs/postscript/operators/dictionary.adoc +191 -0
  16. data/docs/postscript/operators/graphics-state.adoc +528 -0
  17. data/docs/postscript/operators/index.adoc +288 -0
  18. data/docs/postscript/operators/painting.adoc +475 -0
  19. data/docs/postscript/operators/path-construction.adoc +553 -0
  20. data/docs/postscript/operators/stack-manipulation.adoc +374 -0
  21. data/docs/postscript/operators/transformations.adoc +479 -0
  22. data/docs/postscript/svg-mapping.adoc +369 -0
  23. data/exe/postsvg +6 -0
  24. data/lib/postsvg/cli.rb +103 -0
  25. data/lib/postsvg/colors.rb +33 -0
  26. data/lib/postsvg/converter.rb +214 -0
  27. data/lib/postsvg/errors.rb +11 -0
  28. data/lib/postsvg/graphics_state.rb +158 -0
  29. data/lib/postsvg/interpreter.rb +891 -0
  30. data/lib/postsvg/matrix.rb +106 -0
  31. data/lib/postsvg/parser/postscript_parser.rb +87 -0
  32. data/lib/postsvg/parser/transform.rb +21 -0
  33. data/lib/postsvg/parser.rb +18 -0
  34. data/lib/postsvg/path_builder.rb +101 -0
  35. data/lib/postsvg/svg_generator.rb +78 -0
  36. data/lib/postsvg/tokenizer.rb +161 -0
  37. data/lib/postsvg/version.rb +5 -0
  38. data/lib/postsvg.rb +78 -0
  39. data/postsvg.gemspec +38 -0
  40. data/scripts/regenerate_fixtures.rb +28 -0
  41. metadata +118 -0
@@ -0,0 +1,528 @@
1
+ = Graphics State Operators
2
+
3
+ == General
4
+
5
+ Graphics state operators manage the parameters that control how graphics
6
+ operations are performed. The graphics state includes colors, line attributes,
7
+ clipping paths, and the current transformation matrix (CTM).
8
+
9
+ The graphics state can be saved and restored using `gsave` and `grestore`,
10
+ enabling temporary state changes without losing the previous configuration.
11
+
12
+ [[gsave]]
13
+ == gsave
14
+
15
+ === General
16
+
17
+ The `gsave` operator saves a copy of the current graphics state on the graphics
18
+ state stack. This allows temporary changes to graphics parameters, which can
19
+ later be restored using `grestore`.
20
+
21
+ The graphics state stack is separate from the operand stack and the dictionary
22
+ stack.
23
+
24
+ === Syntax
25
+
26
+ [source,postscript]
27
+ ----
28
+ gsave <1>
29
+ ----
30
+ <1> Save current graphics state
31
+
32
+ Where,
33
+
34
+ Stack effect: `-- `
35
+
36
+ === Examples
37
+
38
+ [example]
39
+ ====
40
+ [source,postscript]
41
+ ----
42
+ 1 setlinewidth % Set line width to 1
43
+ 0 0 0 setrgbcolor % Black color
44
+
45
+ gsave % Save state
46
+ 5 setlinewidth % Change to width 5
47
+ 1 0 0 setrgbcolor % Red color
48
+ % Draw with thick red lines
49
+ newpath
50
+ 50 50 moveto
51
+ 150 150 lineto
52
+ stroke
53
+ grestore % Restore: back to width 1, black
54
+
55
+ % Now using original settings
56
+ newpath
57
+ 50 150 moveto
58
+ 150 50 lineto
59
+ stroke
60
+ ----
61
+
62
+ The first line is thick and red, the second is thin and black.
63
+ ====
64
+
65
+ [[grestore]]
66
+ == grestore
67
+
68
+ === General
69
+
70
+ The `grestore` operator restores the graphics state to the state saved by the
71
+ most recent matching `gsave`. It pops the saved state from the graphics state
72
+ stack.
73
+
74
+ === Syntax
75
+
76
+ [source,postscript]
77
+ ----
78
+ grestore <1>
79
+ ----
80
+ <1> Restore saved graphics state
81
+
82
+ Where,
83
+
84
+ Stack effect: `-- `
85
+
86
+ === Examples
87
+
88
+ [example]
89
+ ====
90
+ [source,postscript]
91
+ ----
92
+ gsave
93
+ 100 100 translate % Translate origin
94
+ 45 rotate % Rotate coordinate system
95
+ 2 2 scale % Scale up
96
+ % Draw transformed graphics
97
+ newpath
98
+ 0 0 moveto
99
+ 50 0 lineto
100
+ stroke
101
+ grestore % Restore original coordinate system
102
+
103
+ % Back to untransformed coordinates
104
+ ----
105
+
106
+ Commonly used to isolate coordinate transformations.
107
+ ====
108
+
109
+ [[setlinewidth]]
110
+ == setlinewidth
111
+
112
+ === General
113
+
114
+ The `setlinewidth` operator sets the line width for subsequent stroke
115
+ operations. The width is specified in user space units.
116
+
117
+ The default line width is 1.0 unit.
118
+
119
+ === Syntax
120
+
121
+ [source,postscript]
122
+ ----
123
+ width setlinewidth <1> <2>
124
+ ----
125
+ <1> Line width value (number)
126
+ <2> Operator that sets line width
127
+
128
+ Where,
129
+
130
+ `width`:: Line width in user space units (number, must be non-negative)
131
+
132
+ Stack effect: `width -- `
133
+
134
+ === Examples
135
+
136
+ [example]
137
+ ====
138
+ [source,postscript]
139
+ ----
140
+ newpath
141
+ 50 50 moveto
142
+ 150 150 lineto
143
+ 3 setlinewidth % 3-unit width
144
+ stroke
145
+ ----
146
+
147
+ Creates a line with 3-unit width.
148
+ ====
149
+
150
+ [example]
151
+ ====
152
+ [source,postscript]
153
+ ----
154
+ 0.5 setlinewidth % Thin line
155
+ newpath
156
+ 10 10 moveto 90 10 lineto
157
+ stroke
158
+
159
+ 5 setlinewidth % Thick line
160
+ newpath
161
+ 10 30 moveto 90 30 lineto
162
+ stroke
163
+ ----
164
+
165
+ Demonstrates varying line widths.
166
+ ====
167
+
168
+ [[setrgbcolor]]
169
+ == setrgbcolor
170
+
171
+ === General
172
+
173
+ The `setrgbcolor` operator sets both the fill and stroke colors using RGB
174
+ (Red-Green-Blue) color values. Each component ranges from 0.0 (no intensity)
175
+ to 1.0 (full intensity).
176
+
177
+ === Syntax
178
+
179
+ [source,postscript]
180
+ ----
181
+ red green blue setrgbcolor <1> <2> <3> <4>
182
+ ----
183
+ <1> Red component (0.0 to 1.0)
184
+ <2> Green component (0.0 to 1.0)
185
+ <3> Blue component (0.0 to 1.0)
186
+ <4> Operator that sets RGB color
187
+
188
+ Where,
189
+
190
+ `red`:: Red color component (number, 0.0 to 1.0)
191
+ `green`:: Green color component (number, 0.0 to 1.0)
192
+ `blue`:: Blue color component (number, 0.0 to 1.0)
193
+
194
+ Stack effect: `red green blue -- `
195
+
196
+ === Examples
197
+
198
+ [example]
199
+ ====
200
+ [source,postscript]
201
+ ----
202
+ 1 0 0 setrgbcolor % Pure red
203
+ newpath
204
+ 50 50 moveto
205
+ 150 50 lineto
206
+ stroke
207
+ ----
208
+
209
+ Draws a red line.
210
+ ====
211
+
212
+ [example]
213
+ ====
214
+ [source,postscript]
215
+ ----
216
+ 0 0 0 setrgbcolor % Black (0, 0, 0)
217
+ 1 1 1 setrgbcolor % White (1, 1, 1)
218
+ 0.5 0.5 0.5 setrgbcolor % 50% gray
219
+ 1 0 0 setrgbcolor % Pure red
220
+ 0 1 0 setrgbcolor % Pure green
221
+ 0 0 1 setrgbcolor % Pure blue
222
+ 1 1 0 setrgbcolor % Yellow (red + green)
223
+ 1 0 1 setrgbcolor % Magenta (red + blue)
224
+ 0 1 1 setrgbcolor % Cyan (green + blue)
225
+ ----
226
+
227
+ Common RGB color values.
228
+ ====
229
+
230
+ [[setgray]]
231
+ == setgray
232
+
233
+ === General
234
+
235
+ The `setgray` operator sets both fill and stroke colors to a grayscale value.
236
+ A value of 0.0 is black, 1.0 is white, and intermediate values are shades of
237
+ gray.
238
+
239
+ This is equivalent to calling `setrgbcolor` with identical RGB components.
240
+
241
+ === Syntax
242
+
243
+ [source,postscript]
244
+ ----
245
+ gray setgray <1> <2>
246
+ ----
247
+ <1> Grayscale value (0.0 to 1.0)
248
+ <2> Operator that sets gray level
249
+
250
+ Where,
251
+
252
+ `gray`:: Grayscale intensity (number, 0.0=black, 1.0=white)
253
+
254
+ Stack effect: `gray -- `
255
+
256
+ === Examples
257
+
258
+ [example]
259
+ ====
260
+ [source,postscript]
261
+ ----
262
+ 0 setgray % Black
263
+ newpath
264
+ 10 10 moveto 90 10 lineto
265
+ stroke
266
+
267
+ 0.5 setgray % 50% gray
268
+ newpath
269
+ 10 30 moveto 90 30 lineto
270
+ stroke
271
+
272
+ 1 setgray % White
273
+ newpath
274
+ 10 50 moveto 90 50 lineto
275
+ stroke
276
+ ----
277
+
278
+ Three lines with different gray levels.
279
+ ====
280
+
281
+ [[setlinecap]]
282
+ == setlinecap
283
+
284
+ === General
285
+
286
+ The `setlinecap` operator sets the shape of line ends (caps) for stroke
287
+ operations. Three cap styles are available:
288
+
289
+ * 0 - Butt cap: line ends exactly at endpoint
290
+ * 1 - Round cap: semicircular cap extending beyond endpoint
291
+ * 2 - Projecting square cap: square cap extending beyond endpoint
292
+
293
+ The default cap style is 0 (butt).
294
+
295
+ === Syntax
296
+
297
+ [source,postscript]
298
+ ----
299
+ capstyle setlinecap <1> <2>
300
+ ----
301
+ <1> Cap style code (0, 1, or 2)
302
+ <2> Operator that sets line cap style
303
+
304
+ Where,
305
+
306
+ `capstyle`:: Integer code for cap style (0=butt, 1=round, 2=square)
307
+
308
+ Stack effect: `capstyle -- `
309
+
310
+ === Examples
311
+
312
+ [example]
313
+ ====
314
+ [source,postscript]
315
+ ----
316
+ 10 setlinewidth
317
+
318
+ 0 setlinecap % Butt caps
319
+ newpath
320
+ 20 20 moveto 180 20 lineto
321
+ stroke
322
+
323
+ 1 setlinecap % Round caps
324
+ newpath
325
+ 20 50 moveto 180 50 lineto
326
+ stroke
327
+
328
+ 2 setlinecap % Square caps
329
+ newpath
330
+ 20 80 moveto 180 80 lineto
331
+ stroke
332
+ ----
333
+
334
+ Demonstrates three cap styles with thick lines.
335
+ ====
336
+
337
+ [[setlinejoin]]
338
+ == setlinejoin
339
+
340
+ === General
341
+
342
+ The `setlinejoin` operator sets how line segments are joined at corners when
343
+ stroking paths. Three join styles are available:
344
+
345
+ * 0 - Miter join: sharp pointed corner
346
+ * 1 - Round join: rounded corner
347
+ * 2 - Bevel join: flattened corner
348
+
349
+ The default join style is 0 (miter).
350
+
351
+ === Syntax
352
+
353
+ [source,postscript]
354
+ ----
355
+ joinstyle setlinejoin <1> <2>
356
+ ----
357
+ <1> Join style code (0, 1, or 2)
358
+ <2> Operator that sets line join style
359
+
360
+ Where,
361
+
362
+ `joinstyle`:: Integer code for join style (0=miter, 1=round, 2=bevel)
363
+
364
+ Stack effect: `joinstyle -- `
365
+
366
+ === Examples
367
+
368
+ [example]
369
+ [source,postscript]
370
+ ----
371
+ 10 setlinewidth
372
+
373
+ 0 setlinejoin % Miter joins (sharp corners)
374
+ newpath
375
+ 20 20 moveto 60 50 lineto 100 20 lineto
376
+ stroke
377
+
378
+ 1 setlinejoin % Round joins
379
+ newpath
380
+ 20 60 moveto 60 90 lineto 100 60 lineto
381
+ stroke
382
+
383
+ 2 setlinejoin % Bevel joins
384
+ newpath
385
+ 20 100 moveto 60 130 lineto 100 100 lineto
386
+ stroke
387
+ ----
388
+
389
+ The join style is most visible at acute angles with thick lines.
390
+
391
+ [[setmiterlimit]]
392
+ == setmiterlimit
393
+
394
+ === General
395
+
396
+ The `setmiterlimit` operator sets the limit for miter joins. When two line
397
+ segments meet at a sharp angle with miter joins, the point can extend far
398
+ beyond the join point. The miter limit controls when to switch from a miter
399
+ join to a bevel join.
400
+
401
+ The miter limit is the ratio of miter length to line width. The default is 10.
402
+
403
+ === Syntax
404
+
405
+ [source,postscript]
406
+ ----
407
+ limit setmiterlimit <1> <2>
408
+ ----
409
+ <1> Miter limit value (number)
410
+ <2> Operator that sets miter limit
411
+
412
+ Where,
413
+
414
+ `limit`:: Miter limit ratio (number, must be >= 1.0)
415
+
416
+ Stack effect: `limit -- `
417
+
418
+ === Examples
419
+
420
+ [example]
421
+ [source,postscript]
422
+ ----
423
+ 10 setlinewidth
424
+ 0 setlinejoin % Use miter joins
425
+
426
+ 1.5 setmiterlimit % Low limit, bevels easily
427
+ newpath
428
+ 20 20 moveto 60 50 lineto 100 20 lineto
429
+ stroke
430
+
431
+ 10 setmiterlimit % Higher limit, sharp miters
432
+ newpath
433
+ 20 60 moveto 60 90 lineto 100 60 lineto
434
+ stroke
435
+ ----
436
+
437
+ Lower miter limits cause more joins to be beveled at acute angles.
438
+
439
+ [[setdash]]
440
+ == setdash
441
+
442
+ === General
443
+
444
+ The `setdash` operator establishes a dash pattern for stroke operations. The
445
+ pattern is specified as an array of on/off lengths and an offset value.
446
+
447
+ An empty array produces solid (unbroken) lines.
448
+
449
+ === Syntax
450
+
451
+ [source,postscript]
452
+ ----
453
+ array offset setdash <1> <2> <3>
454
+ ----
455
+ <1> Array of dash lengths (array)
456
+ <2> Offset into dash pattern (number)
457
+ <3> Operator that sets dash pattern
458
+
459
+ Where,
460
+
461
+ `array`:: Array of alternating on/off lengths (empty array = solid line)
462
+ `offset`:: Starting offset into the pattern (number)
463
+
464
+ Stack effect: `array offset -- `
465
+
466
+ === Examples
467
+
468
+ [example]
469
+ [source,postscript]
470
+ ----
471
+ [5 5] 0 setdash % 5 on, 5 off
472
+ newpath
473
+ 10 10 moveto 190 10 lineto
474
+ stroke
475
+
476
+ [10 5 2 5] 0 setdash % Long dash, gap, dot, gap
477
+ newpath
478
+ 10 30 moveto 190 30 lineto
479
+ stroke
480
+
481
+ [] 0 setdash % Solid line (reset)
482
+ newpath
483
+ 10 50 moveto 190 50 lineto
484
+ stroke
485
+ ----
486
+
487
+ Dash patterns can have any number of elements.
488
+
489
+ == Graphics State Stack Management
490
+
491
+ === General
492
+
493
+ The graphics state stack enables nested state changes without losing previous
494
+ settings. Each `gsave` pushes a complete copy of the current state.
495
+
496
+ === Nesting example
497
+
498
+ [example]
499
+ [source,postscript]
500
+ ----
501
+ 1 setlinewidth
502
+ 0 0 0 setrgbcolor
503
+
504
+ gsave % Level 1
505
+ 2 setlinewidth
506
+ 1 0 0 setrgbcolor
507
+
508
+ gsave % Level 2
509
+ 5 setlinewidth
510
+ 0 0 1 setrgbcolor
511
+ % Draw with width 5, blue
512
+ grestore % Back to level 1
513
+
514
+ % Now width 2, red
515
+ grestore % Back to level 0
516
+
517
+ % Now width 1, black
518
+ ----
519
+
520
+ Multiple levels of state saving enable complex graphics compositions.
521
+
522
+ == See Also
523
+
524
+ * link:painting.adoc[Painting Operators] - Using colors and line widths
525
+ * link:transformations.adoc[Transformations] - Also saved in graphics state
526
+ * link:../graphics-model.adoc#graphics-state[Graphics State] - Conceptual
527
+ overview
528
+ * link:index.adoc[Back to Operator Reference]