squib 0.14.1 → 0.14.2

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 (162) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +41 -41
  3. data/.travis.yml +19 -19
  4. data/CHANGELOG.md +357 -340
  5. data/CONTRIBUTING.md +40 -40
  6. data/Gemfile +2 -2
  7. data/LICENSE.txt +22 -22
  8. data/README.md +121 -121
  9. data/RELEASE TODO.md +21 -21
  10. data/Rakefile +48 -48
  11. data/appveyor.yml +24 -24
  12. data/bin/squib +4 -4
  13. data/lib/squib.rb +32 -32
  14. data/lib/squib/api/background.rb +15 -15
  15. data/lib/squib/api/data.rb +137 -137
  16. data/lib/squib/api/groups.rb +54 -54
  17. data/lib/squib/api/image.rb +49 -49
  18. data/lib/squib/api/save.rb +83 -83
  19. data/lib/squib/api/settings.rb +21 -21
  20. data/lib/squib/api/shapes.rb +124 -124
  21. data/lib/squib/api/text.rb +25 -25
  22. data/lib/squib/api/text_embed.rb +71 -71
  23. data/lib/squib/api/units.rb +27 -27
  24. data/lib/squib/args/arg_loader.rb +126 -126
  25. data/lib/squib/args/box.rb +55 -55
  26. data/lib/squib/args/card_range.rb +32 -32
  27. data/lib/squib/args/color_validator.rb +12 -12
  28. data/lib/squib/args/coords.rb +35 -35
  29. data/lib/squib/args/csv_opts.rb +25 -25
  30. data/lib/squib/args/dir_validator.rb +16 -16
  31. data/lib/squib/args/draw.rb +92 -92
  32. data/lib/squib/args/embed_adjust.rb +25 -25
  33. data/lib/squib/args/embed_key.rb +17 -17
  34. data/lib/squib/args/hand_special.rb +37 -37
  35. data/lib/squib/args/import.rb +40 -40
  36. data/lib/squib/args/input_file.rb +37 -37
  37. data/lib/squib/args/paint.rb +44 -44
  38. data/lib/squib/args/paragraph.rb +116 -116
  39. data/lib/squib/args/save_batch.rb +63 -63
  40. data/lib/squib/args/scale_box.rb +53 -53
  41. data/lib/squib/args/sheet.rb +172 -172
  42. data/lib/squib/args/showcase_special.rb +38 -38
  43. data/lib/squib/args/sprue_file.rb +44 -44
  44. data/lib/squib/args/svg_special.rb +37 -37
  45. data/lib/squib/args/transform.rb +61 -61
  46. data/lib/squib/args/typographer.rb +119 -119
  47. data/lib/squib/args/unit_conversion.rb +29 -29
  48. data/lib/squib/builtin/layouts/economy.yml +85 -85
  49. data/lib/squib/builtin/layouts/fantasy.yml +101 -101
  50. data/lib/squib/builtin/layouts/hand.yml +62 -62
  51. data/lib/squib/builtin/layouts/party.yml +94 -94
  52. data/lib/squib/builtin/layouts/playing-card.yml +35 -35
  53. data/lib/squib/builtin/layouts/tuck_box.yml +46 -46
  54. data/lib/squib/builtin/projects/advanced/.gitignore +4 -4
  55. data/lib/squib/builtin/projects/advanced/ABOUT.md +19 -19
  56. data/lib/squib/builtin/projects/advanced/Gemfile +11 -11
  57. data/lib/squib/builtin/projects/advanced/Guardfile +21 -21
  58. data/lib/squib/builtin/projects/advanced/IDEAS.md +22 -22
  59. data/lib/squib/builtin/projects/advanced/PLAYTESTING.md +26 -26
  60. data/lib/squib/builtin/projects/advanced/Rakefile +27 -27
  61. data/lib/squib/builtin/projects/advanced/config.yml +49 -49
  62. data/lib/squib/builtin/projects/advanced/docs/PNP NOTES.md +3 -3
  63. data/lib/squib/builtin/projects/advanced/docs/RULES.md +21 -21
  64. data/lib/squib/builtin/projects/advanced/img/example.svg +60 -60
  65. data/lib/squib/builtin/projects/advanced/layouts/deck.yml +27 -27
  66. data/lib/squib/builtin/projects/advanced/src/deck.rb +34 -34
  67. data/lib/squib/builtin/projects/advanced/src/version.rb +3 -3
  68. data/lib/squib/builtin/projects/basic/.gitignore +4 -4
  69. data/lib/squib/builtin/projects/basic/ABOUT.md +19 -19
  70. data/lib/squib/builtin/projects/basic/Gemfile +3 -3
  71. data/lib/squib/builtin/projects/basic/IDEAS.md +22 -22
  72. data/lib/squib/builtin/projects/basic/PLAYTESTING.md +26 -26
  73. data/lib/squib/builtin/projects/basic/PNP NOTES.md +3 -3
  74. data/lib/squib/builtin/projects/basic/RULES.md +21 -21
  75. data/lib/squib/builtin/projects/basic/Rakefile +7 -7
  76. data/lib/squib/builtin/projects/basic/config.yml +49 -49
  77. data/lib/squib/builtin/projects/basic/deck.rb +6 -6
  78. data/lib/squib/builtin/sprues/a4_euro_card.yml +42 -42
  79. data/lib/squib/builtin/sprues/a4_poker_card_8up.yml +40 -40
  80. data/lib/squib/builtin/sprues/a4_poker_card_9up.yml +42 -42
  81. data/lib/squib/builtin/sprues/a4_usa_card.yml +42 -42
  82. data/lib/squib/builtin/sprues/drivethrucards_1up.yml +10 -0
  83. data/lib/squib/builtin/sprues/letter_poker_card_9up.yml +25 -25
  84. data/lib/squib/builtin/sprues/letter_poker_foldable_8up.yml +52 -52
  85. data/lib/squib/builtin/sprues/printplaygames_18up.yml +68 -0
  86. data/lib/squib/card.rb +75 -74
  87. data/lib/squib/commands/cli.rb +39 -39
  88. data/lib/squib/commands/data/template_option.rb +109 -109
  89. data/lib/squib/commands/make_sprue.rb +275 -275
  90. data/lib/squib/commands/new.rb +77 -77
  91. data/lib/squib/conf.rb +139 -139
  92. data/lib/squib/constants.rb +17 -17
  93. data/lib/squib/deck.rb +116 -116
  94. data/lib/squib/graphics/background.rb +14 -14
  95. data/lib/squib/graphics/cairo_context_wrapper.rb +113 -113
  96. data/lib/squib/graphics/embedding_utils.rb +28 -28
  97. data/lib/squib/graphics/gradient_regex.rb +47 -47
  98. data/lib/squib/graphics/hand.rb +42 -42
  99. data/lib/squib/graphics/image.rb +108 -108
  100. data/lib/squib/graphics/save_doc.rb +61 -61
  101. data/lib/squib/graphics/save_images.rb +52 -52
  102. data/lib/squib/graphics/save_pdf.rb +90 -90
  103. data/lib/squib/graphics/save_sprue.rb +204 -205
  104. data/lib/squib/graphics/shapes.rb +143 -143
  105. data/lib/squib/graphics/showcase.rb +85 -85
  106. data/lib/squib/graphics/text.rb +174 -174
  107. data/lib/squib/import/data_frame.rb +108 -108
  108. data/lib/squib/layout_parser.rb +138 -126
  109. data/lib/squib/progress.rb +38 -38
  110. data/lib/squib/sample_helpers.rb +34 -34
  111. data/lib/squib/sprues/crop_line.rb +28 -28
  112. data/lib/squib/sprues/crop_line_dash.rb +35 -35
  113. data/lib/squib/sprues/invalid_sprue_definition.rb +9 -9
  114. data/lib/squib/sprues/sprue.rb +203 -203
  115. data/lib/squib/sprues/sprue_schema.rb +48 -48
  116. data/lib/squib/version.rb +10 -10
  117. data/samples/autoscale_font/_autoscale_font.rb +29 -29
  118. data/samples/backend/_backend.rb +26 -26
  119. data/samples/basic.rb +19 -19
  120. data/samples/build_groups/build_groups.rb +36 -36
  121. data/samples/colors/_colors.rb +38 -38
  122. data/samples/colors/_gradients.rb +34 -34
  123. data/samples/config/config_text_markup.rb +20 -20
  124. data/samples/config/custom_config.rb +18 -18
  125. data/samples/data/_csv.rb +33 -33
  126. data/samples/data/_excel.rb +55 -55
  127. data/samples/data/_yaml.rb +12 -12
  128. data/samples/hello_world.rb +6 -6
  129. data/samples/images/_cairo_access.rb +39 -39
  130. data/samples/images/_images.rb +104 -104
  131. data/samples/images/_more_load_images.rb +102 -102
  132. data/samples/intro/01_hello.rb +8 -8
  133. data/samples/intro/02_options.rb +14 -14
  134. data/samples/intro/03_layout.rb +11 -11
  135. data/samples/intro/04_arrays.rb +15 -15
  136. data/samples/intro/05_excel.rb +14 -14
  137. data/samples/layouts/builtin_layouts.rb +97 -97
  138. data/samples/layouts/layouts.rb +71 -71
  139. data/samples/project/src/characters.rb +8 -8
  140. data/samples/project/src/skills.rb +7 -7
  141. data/samples/proofs/_tgc_proofs.rb +16 -16
  142. data/samples/ranges/_ranges.rb +64 -64
  143. data/samples/saves/_hand.rb +23 -23
  144. data/samples/saves/_portrait_landscape.rb +23 -23
  145. data/samples/saves/_save_pdf.rb +29 -29
  146. data/samples/saves/_saves.rb +51 -51
  147. data/samples/saves/_showcase.rb +25 -25
  148. data/samples/shapes/_draw_shapes.rb +60 -58
  149. data/samples/shapes/_proofs.rb +22 -22
  150. data/samples/sprues/_advanced_sprues.rb +24 -24
  151. data/samples/sprues/_builtin_sprues.rb +21 -19
  152. data/samples/sprues/_fold_sheet.rb +27 -27
  153. data/samples/sprues/_hex_tiles.rb +15 -15
  154. data/samples/sprues/_mints.rb +11 -11
  155. data/samples/sprues/_sprue_example.rb +11 -11
  156. data/samples/text/_embed_text.rb +128 -128
  157. data/samples/text/_text.rb +47 -47
  158. data/samples/text/_text_options.rb +102 -102
  159. data/samples/text/bug134.rb +14 -14
  160. data/samples/units/_units.rb +32 -32
  161. data/squib.gemspec +52 -51
  162. metadata +66 -50
@@ -1,116 +1,116 @@
1
- require 'forwardable'
2
- require 'pp'
3
- require_relative '../squib'
4
- require_relative 'args/unit_conversion'
5
- require_relative 'card'
6
- require_relative 'conf'
7
- require_relative 'constants'
8
- require_relative 'graphics/hand'
9
- require_relative 'graphics/showcase'
10
- require_relative 'layout_parser'
11
- require_relative 'progress'
12
-
13
-
14
- # The project module
15
- #
16
- # @api public
17
- module Squib
18
-
19
- # The main interface to Squib. Provides a front-end porcelain whereas the Card class interacts with the graphics plumbing.
20
- #
21
- # @api public
22
- class Deck
23
- include Enumerable
24
- extend Forwardable
25
-
26
- # Attributes for the width, height (in pixels) and number of cards
27
- # These are expected to be immuatble for the life of Deck
28
- # @api private
29
- attr_reader :width, :height, :cards, :progress_bar
30
-
31
- # Delegate these configuration options to the Squib::Conf object
32
- def_delegators :conf, :antialias, :backend, :count_format, :custom_colors, :dir,
33
- :img_dir, :prefix, :text_hint, :typographer
34
- # :nodoc:
35
- # @api private
36
- attr_reader :layout, :conf, :dpi, :font
37
-
38
- #
39
- # deck.size is really just @cards.size
40
- def_delegators :cards, :size
41
-
42
- # Squib's constructor that sets the immutable properties.
43
- #
44
- # This is the starting point for Squib. In providing a block to the constructor, you have access to all of Deck's instance methods.
45
- # The documented methods in Deck are the ones intended for use by most users.
46
- # If your game requires multiple different sizes or orientations, I recommend using multiple `Squib::Deck`s in your `deck.rb`. You can modify the internals of `Squib::Deck` (e.g. `@cards`), but that's not recommended.
47
- # @example
48
- # require 'squib'
49
- # Squib::Deck.new do
50
- # text str: 'Hello, World!"
51
- # end
52
- #
53
- # @param width [Integer] the width of each card in pixels. Supports unit conversion (e.g. '2.5in').
54
- # @param height [Integer] the height of each card in pixels. Supports unit conversion (e.g. '3.5in').
55
- # @param cards [Integer] the number of cards in the deck
56
- # @param dpi [Integer] the pixels per inch when rendering out to PDF or calculating using inches.
57
- # @param config [String] the file used for global settings of this deck
58
- # @param layout [String, Array] load a YML file of custom layouts. Multiple files are merged sequentially, redefining collisons. See README and sample for details.
59
- # @param block [Block] the main body of the script.
60
- # @api public
61
- def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block)
62
- @dpi = dpi
63
- @font = DEFAULT_FONT
64
- @cards = []
65
- @conf = Conf.load(config)
66
- @progress_bar = Progress.new(@conf.progress_bars) # FIXME this is evil. Using something different with @ and non-@
67
- show_info(config, layout)
68
- @width = Args::UnitConversion.parse width, dpi
69
- @height = Args::UnitConversion.parse height, dpi
70
- cards.times{ |i| @cards << Squib::Card.new(self, @width, @height, i) }
71
- @layout = LayoutParser.new(dpi).load_layout(layout)
72
- enable_groups_from_env!
73
- if block_given?
74
- instance_eval(&block) # here we go. wheeeee!
75
- end
76
- @cards.each { |c| c.finish! }
77
- end
78
-
79
- # Directly accesses the array of cards in the deck
80
- #
81
- # @api private
82
- def [](key)
83
- @cards[key]
84
- end
85
-
86
- # Iterates over each card in the deck
87
- #
88
- # @api private
89
- def each(&block)
90
- @cards.each { |card| block.call(card) }
91
- end
92
-
93
- # Use Logger to show more detail on the run
94
- # :nodoc:
95
- # @api private
96
- def show_info(config, layout)
97
- Squib::logger.info "Squib v#{Squib::VERSION}"
98
- Squib::logger.info " building #{@cards.size} #{@width}x#{@height} cards"
99
- Squib::logger.info " using #{@backend}"
100
- end
101
-
102
- ##################
103
- ### PUBLIC API ###
104
- ##################
105
- require_relative 'api/background'
106
- require_relative 'api/data'
107
- require_relative 'api/groups'
108
- require_relative 'api/image'
109
- require_relative 'api/save'
110
- require_relative 'api/settings'
111
- require_relative 'api/shapes'
112
- require_relative 'api/text'
113
- require_relative 'api/units'
114
-
115
- end
116
- end
1
+ require 'forwardable'
2
+ require 'pp'
3
+ require_relative '../squib'
4
+ require_relative 'args/unit_conversion'
5
+ require_relative 'card'
6
+ require_relative 'conf'
7
+ require_relative 'constants'
8
+ require_relative 'graphics/hand'
9
+ require_relative 'graphics/showcase'
10
+ require_relative 'layout_parser'
11
+ require_relative 'progress'
12
+
13
+
14
+ # The project module
15
+ #
16
+ # @api public
17
+ module Squib
18
+
19
+ # The main interface to Squib. Provides a front-end porcelain whereas the Card class interacts with the graphics plumbing.
20
+ #
21
+ # @api public
22
+ class Deck
23
+ include Enumerable
24
+ extend Forwardable
25
+
26
+ # Attributes for the width, height (in pixels) and number of cards
27
+ # These are expected to be immuatble for the life of Deck
28
+ # @api private
29
+ attr_reader :width, :height, :cards, :progress_bar
30
+
31
+ # Delegate these configuration options to the Squib::Conf object
32
+ def_delegators :conf, :antialias, :backend, :count_format, :custom_colors, :dir,
33
+ :img_dir, :prefix, :text_hint, :typographer
34
+ # :nodoc:
35
+ # @api private
36
+ attr_reader :layout, :conf, :dpi, :font
37
+
38
+ #
39
+ # deck.size is really just @cards.size
40
+ def_delegators :cards, :size
41
+
42
+ # Squib's constructor that sets the immutable properties.
43
+ #
44
+ # This is the starting point for Squib. In providing a block to the constructor, you have access to all of Deck's instance methods.
45
+ # The documented methods in Deck are the ones intended for use by most users.
46
+ # If your game requires multiple different sizes or orientations, I recommend using multiple `Squib::Deck`s in your `deck.rb`. You can modify the internals of `Squib::Deck` (e.g. `@cards`), but that's not recommended.
47
+ # @example
48
+ # require 'squib'
49
+ # Squib::Deck.new do
50
+ # text str: 'Hello, World!"
51
+ # end
52
+ #
53
+ # @param width [Integer] the width of each card in pixels. Supports unit conversion (e.g. '2.5in').
54
+ # @param height [Integer] the height of each card in pixels. Supports unit conversion (e.g. '3.5in').
55
+ # @param cards [Integer] the number of cards in the deck
56
+ # @param dpi [Integer] the pixels per inch when rendering out to PDF or calculating using inches.
57
+ # @param config [String] the file used for global settings of this deck
58
+ # @param layout [String, Array] load a YML file of custom layouts. Multiple files are merged sequentially, redefining collisons. See README and sample for details.
59
+ # @param block [Block] the main body of the script.
60
+ # @api public
61
+ def initialize(width: 825, height: 1125, cards: 1, dpi: 300, config: 'config.yml', layout: nil, &block)
62
+ @dpi = dpi
63
+ @font = DEFAULT_FONT
64
+ @cards = []
65
+ @conf = Conf.load(config)
66
+ @progress_bar = Progress.new(@conf.progress_bars) # FIXME this is evil. Using something different with @ and non-@
67
+ show_info(config, layout)
68
+ @width = Args::UnitConversion.parse width, dpi
69
+ @height = Args::UnitConversion.parse height, dpi
70
+ cards.times{ |i| @cards << Squib::Card.new(self, @width, @height, i) }
71
+ @layout = LayoutParser.new(dpi).load_layout(layout)
72
+ enable_groups_from_env!
73
+ if block_given?
74
+ instance_eval(&block) # here we go. wheeeee!
75
+ end
76
+ @cards.each { |c| c.finish! }
77
+ end
78
+
79
+ # Directly accesses the array of cards in the deck
80
+ #
81
+ # @api private
82
+ def [](key)
83
+ @cards[key]
84
+ end
85
+
86
+ # Iterates over each card in the deck
87
+ #
88
+ # @api private
89
+ def each(&block)
90
+ @cards.each { |card| block.call(card) }
91
+ end
92
+
93
+ # Use Logger to show more detail on the run
94
+ # :nodoc:
95
+ # @api private
96
+ def show_info(config, layout)
97
+ Squib::logger.info "Squib v#{Squib::VERSION}"
98
+ Squib::logger.info " building #{@cards.size} #{@width}x#{@height} cards"
99
+ Squib::logger.info " using #{@backend}"
100
+ end
101
+
102
+ ##################
103
+ ### PUBLIC API ###
104
+ ##################
105
+ require_relative 'api/background'
106
+ require_relative 'api/data'
107
+ require_relative 'api/groups'
108
+ require_relative 'api/image'
109
+ require_relative 'api/save'
110
+ require_relative 'api/settings'
111
+ require_relative 'api/shapes'
112
+ require_relative 'api/text'
113
+ require_relative 'api/units'
114
+
115
+ end
116
+ end
@@ -1,14 +1,14 @@
1
- module Squib
2
- class Card
3
-
4
- # :nodoc:
5
- # @api private
6
- def background(color)
7
- use_cairo do |cc|
8
- cc.set_source_squibcolor(color)
9
- cc.paint
10
- end
11
- end
12
-
13
- end
14
- end
1
+ module Squib
2
+ class Card
3
+
4
+ # :nodoc:
5
+ # @api private
6
+ def background(color)
7
+ use_cairo do |cc|
8
+ cc.set_source_squibcolor(color)
9
+ cc.paint
10
+ end
11
+ end
12
+
13
+ end
14
+ end
@@ -1,113 +1,113 @@
1
- require 'forwardable'
2
- require_relative 'gradient_regex'
3
-
4
- module Squib
5
- module Graphics
6
- # Wrapper class for the Cairo context. Private.
7
- # @api private
8
- class CairoContextWrapper
9
- extend Forwardable
10
-
11
- # :nodoc:
12
- # @api private
13
- attr_accessor :cairo_cxt
14
-
15
- # :nodoc:
16
- # @api private
17
- def initialize(cairo_cxt)
18
- @cairo_cxt = cairo_cxt
19
- end
20
-
21
- def_delegators :cairo_cxt, :save, :set_source_color, :paint, :restore,
22
- :translate, :rotate, :move_to, :update_pango_layout, :width, :height,
23
- :show_pango_layout, :rounded_rectangle, :set_line_width, :stroke, :fill,
24
- :set_source, :scale, :render_rsvg_handle, :circle, :triangle, :line_to,
25
- :operator=, :show_page, :clip, :transform, :mask, :create_pango_layout,
26
- :antialias=, :curve_to, :matrix, :matrix=, :identity_matrix, :pango_layout_path,
27
- :stroke_preserve, :target, :new_path, :fill_preserve, :close_path,
28
- :set_line_join, :set_line_cap, :set_dash, :arc, :arc_negative
29
-
30
- # :nodoc:
31
- # @api private
32
- def set_source_squibcolor(arg)
33
- raise 'nil is not a valid color' if arg.nil?
34
- if match = arg.match(LINEAR_GRADIENT)
35
- x1, y1, x2, y2 = match.captures
36
- linear = Cairo::LinearPattern.new(x1.to_f, y1.to_f, x2.to_f, y2.to_f)
37
- arg.scan(STOPS).each do |color, offset|
38
- linear.add_color_stop(offset.to_f, color)
39
- end
40
- linear.matrix = matrix # match the coordinate systems - see bug 127
41
- @cairo_cxt.set_source(linear)
42
- elsif match = arg.match(RADIAL_GRADIENT)
43
- x1, y1, r1, x2, y2, r2 = match.captures
44
- radial = Cairo::RadialPattern.new(x1.to_f, y1.to_f, r1.to_f,
45
- x2.to_f, y2.to_f, r2.to_f)
46
- radial.matrix = matrix # match the coordinate systems - see bug 127
47
- arg.scan(STOPS).each do |color, offset|
48
- radial.add_color_stop(offset.to_f, color)
49
- end
50
- @cairo_cxt.set_source(radial)
51
- else
52
- @cairo_cxt.set_source_color(arg)
53
- end
54
- end
55
-
56
- # Convenience method for a common task
57
- # @api private
58
- def fill_n_stroke(draw)
59
- return stroke_n_fill(draw) if draw.stroke_strategy == :stroke_first
60
- set_source_squibcolor draw.fill_color
61
- fill_preserve
62
- set_source_squibcolor draw.stroke_color
63
- set_line_width draw.stroke_width
64
- set_line_join draw.join
65
- set_line_cap draw.cap
66
- set_dash draw.dash
67
- stroke
68
- end
69
-
70
- def stroke_n_fill(draw)
71
- return fill_n_stroke(draw) if draw.stroke_strategy == :fill_first
72
- set_source_squibcolor draw.stroke_color
73
- set_line_width draw.stroke_width
74
- set_line_join draw.join
75
- set_line_cap draw.cap
76
- set_dash draw.dash
77
- stroke_preserve
78
- set_source_squibcolor draw.fill_color
79
- fill
80
- end
81
-
82
- # Convenience method for a common task
83
- # @api private
84
- def fancy_stroke(draw)
85
- set_source_squibcolor draw.stroke_color
86
- set_line_width draw.stroke_width
87
- set_line_join draw.join
88
- set_line_cap draw.cap
89
- set_dash draw.dash
90
- stroke
91
- end
92
-
93
- def rotate_about(x, y, angle)
94
- translate(x, y)
95
- rotate(angle)
96
- translate(-x, -y)
97
- end
98
-
99
- # Flip either vertical or horizontal depending
100
- # From the cairo website: http://cairographics.org/matrix_transform/
101
- # cairo.Matrix(fx, 0, 0,
102
- # fy, cx*(1-fx), cy*(fy-1))
103
- # fx/fy = 1 means 'no flip', fx/fy = -1 are used for horizontal/vertical flip
104
- def flip(vertical, horizontal, x, y)
105
- v = vertical ? -1.0 : 1.0
106
- h = horizontal ? -1.0 : 1.0
107
- transform Cairo::Matrix.new(v, 0.0, 0.0,
108
- h, x * (1 - v), y * (1 - h))
109
- end
110
-
111
- end
112
- end
113
- end
1
+ require 'forwardable'
2
+ require_relative 'gradient_regex'
3
+
4
+ module Squib
5
+ module Graphics
6
+ # Wrapper class for the Cairo context. Private.
7
+ # @api private
8
+ class CairoContextWrapper
9
+ extend Forwardable
10
+
11
+ # :nodoc:
12
+ # @api private
13
+ attr_accessor :cairo_cxt
14
+
15
+ # :nodoc:
16
+ # @api private
17
+ def initialize(cairo_cxt)
18
+ @cairo_cxt = cairo_cxt
19
+ end
20
+
21
+ def_delegators :cairo_cxt, :save, :set_source_color, :paint, :restore,
22
+ :translate, :rotate, :move_to, :update_pango_layout, :width, :height,
23
+ :show_pango_layout, :rounded_rectangle, :set_line_width, :stroke, :fill,
24
+ :set_source, :scale, :render_rsvg_handle, :circle, :triangle, :line_to,
25
+ :operator=, :show_page, :clip, :transform, :mask, :create_pango_layout,
26
+ :antialias=, :curve_to, :matrix, :matrix=, :identity_matrix, :pango_layout_path,
27
+ :stroke_preserve, :target, :new_path, :fill_preserve, :close_path,
28
+ :set_line_join, :set_line_cap, :set_dash, :arc, :arc_negative
29
+
30
+ # :nodoc:
31
+ # @api private
32
+ def set_source_squibcolor(arg)
33
+ raise 'nil is not a valid color' if arg.nil?
34
+ if match = arg.match(LINEAR_GRADIENT)
35
+ x1, y1, x2, y2 = match.captures
36
+ linear = Cairo::LinearPattern.new(x1.to_f, y1.to_f, x2.to_f, y2.to_f)
37
+ arg.scan(STOPS).each do |color, offset|
38
+ linear.add_color_stop(offset.to_f, color)
39
+ end
40
+ linear.matrix = matrix # match the coordinate systems - see bug 127
41
+ @cairo_cxt.set_source(linear)
42
+ elsif match = arg.match(RADIAL_GRADIENT)
43
+ x1, y1, r1, x2, y2, r2 = match.captures
44
+ radial = Cairo::RadialPattern.new(x1.to_f, y1.to_f, r1.to_f,
45
+ x2.to_f, y2.to_f, r2.to_f)
46
+ radial.matrix = matrix # match the coordinate systems - see bug 127
47
+ arg.scan(STOPS).each do |color, offset|
48
+ radial.add_color_stop(offset.to_f, color)
49
+ end
50
+ @cairo_cxt.set_source(radial)
51
+ else
52
+ @cairo_cxt.set_source_color(arg)
53
+ end
54
+ end
55
+
56
+ # Convenience method for a common task
57
+ # @api private
58
+ def fill_n_stroke(draw)
59
+ return stroke_n_fill(draw) if draw.stroke_strategy == :stroke_first
60
+ set_source_squibcolor draw.fill_color
61
+ fill_preserve
62
+ set_source_squibcolor draw.stroke_color
63
+ set_line_width draw.stroke_width
64
+ set_line_join draw.join
65
+ set_line_cap draw.cap
66
+ set_dash draw.dash
67
+ stroke
68
+ end
69
+
70
+ def stroke_n_fill(draw)
71
+ return fill_n_stroke(draw) if draw.stroke_strategy == :fill_first
72
+ set_source_squibcolor draw.stroke_color
73
+ set_line_width draw.stroke_width
74
+ set_line_join draw.join
75
+ set_line_cap draw.cap
76
+ set_dash draw.dash
77
+ stroke_preserve
78
+ set_source_squibcolor draw.fill_color
79
+ fill
80
+ end
81
+
82
+ # Convenience method for a common task
83
+ # @api private
84
+ def fancy_stroke(draw)
85
+ set_source_squibcolor draw.stroke_color
86
+ set_line_width draw.stroke_width
87
+ set_line_join draw.join
88
+ set_line_cap draw.cap
89
+ set_dash draw.dash
90
+ stroke
91
+ end
92
+
93
+ def rotate_about(x, y, angle)
94
+ translate(x, y)
95
+ rotate(angle)
96
+ translate(-x, -y)
97
+ end
98
+
99
+ # Flip either vertical or horizontal depending
100
+ # From the cairo website: http://cairographics.org/matrix_transform/
101
+ # cairo.Matrix(fx, 0, 0,
102
+ # fy, cx*(1-fx), cy*(fy-1))
103
+ # fx/fy = 1 means 'no flip', fx/fy = -1 are used for horizontal/vertical flip
104
+ def flip(vertical, horizontal, x, y)
105
+ v = vertical ? -1.0 : 1.0
106
+ h = horizontal ? -1.0 : 1.0
107
+ transform Cairo::Matrix.new(v, 0.0, 0.0,
108
+ h, x * (1 - v), y * (1 - h))
109
+ end
110
+
111
+ end
112
+ end
113
+ end