squib 0.0.3 → 0.0.4

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 (74) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +29 -29
  3. data/.travis.yml +6 -6
  4. data/.yardopts +7 -7
  5. data/CHANGELOG.md +19 -0
  6. data/Gemfile +2 -2
  7. data/LICENSE.txt +22 -22
  8. data/README.md +256 -244
  9. data/Rakefile +11 -11
  10. data/bin/squib +18 -18
  11. data/lib/squib.rb +31 -31
  12. data/lib/squib/api/background.rb +18 -18
  13. data/lib/squib/api/data.rb +52 -52
  14. data/lib/squib/api/image.rb +66 -66
  15. data/lib/squib/api/save.rb +43 -43
  16. data/lib/squib/api/settings.rb +37 -38
  17. data/lib/squib/api/shapes.rb +116 -116
  18. data/lib/squib/api/text.rb +53 -50
  19. data/lib/squib/api/units.rb +16 -16
  20. data/lib/squib/card.rb +41 -41
  21. data/lib/squib/commands/new.rb +43 -43
  22. data/lib/squib/constants.rb +108 -104
  23. data/lib/squib/deck.rb +170 -116
  24. data/lib/squib/graphics/background.rb +13 -13
  25. data/lib/squib/graphics/image.rb +47 -47
  26. data/lib/squib/graphics/save_doc.rb +54 -54
  27. data/lib/squib/graphics/save_images.rb +32 -32
  28. data/lib/squib/graphics/shapes.rb +59 -59
  29. data/lib/squib/graphics/text.rb +116 -113
  30. data/lib/squib/input_helpers.rb +193 -193
  31. data/lib/squib/progress.rb +37 -37
  32. data/lib/squib/project_template/.gitignore +4 -4
  33. data/lib/squib/project_template/ABOUT.md +19 -19
  34. data/lib/squib/project_template/Gemfile +2 -2
  35. data/lib/squib/project_template/PNP NOTES.md +3 -3
  36. data/lib/squib/project_template/config.yml +19 -19
  37. data/lib/squib/project_template/deck.rb +5 -5
  38. data/lib/squib/version.rb +6 -6
  39. data/samples/autoscale_font.rb +27 -0
  40. data/samples/basic.rb +19 -19
  41. data/samples/colors.rb +15 -15
  42. data/samples/custom-config.yml +5 -5
  43. data/samples/custom-layout.yml +59 -39
  44. data/samples/custom_config.rb +18 -18
  45. data/samples/customconfig-imgdir/spanner2.svg +91 -91
  46. data/samples/draw_shapes.rb +18 -18
  47. data/samples/excel.rb +19 -19
  48. data/samples/hello_world.rb +6 -6
  49. data/samples/load_images.rb +29 -29
  50. data/samples/offset.svg +71 -71
  51. data/samples/portrait-landscape.rb +22 -22
  52. data/samples/ranges.rb +55 -55
  53. data/samples/save_pdf.rb +14 -14
  54. data/samples/spanner.svg +91 -91
  55. data/samples/text_options.rb +67 -60
  56. data/samples/tgc_proofs.rb +19 -19
  57. data/samples/units.rb +12 -12
  58. data/samples/use_layout.rb +33 -28
  59. data/spec/api/api_text_spec.rb +43 -43
  60. data/spec/commands/new_spec.rb +47 -47
  61. data/spec/data/easy-circular-extends.yml +6 -0
  62. data/spec/data/hard-circular-extends.yml +9 -0
  63. data/spec/data/multi-extends-single-entry.yml +14 -13
  64. data/spec/data/multi-level-extends.yml +9 -9
  65. data/spec/data/no-extends.yml +5 -5
  66. data/spec/data/pre-extends.yml +7 -0
  67. data/spec/data/self-circular-extends.yml +3 -0
  68. data/spec/data/single-extends.yml +7 -7
  69. data/spec/data/single-level-multi-extends.yml +11 -11
  70. data/spec/deck_spec.rb +188 -147
  71. data/spec/input_helpers_spec.rb +116 -116
  72. data/spec/samples_run_spec.rb +19 -19
  73. data/squib.gemspec +46 -46
  74. metadata +17 -7
@@ -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_color(color)
9
- cc.paint
10
- end
11
- end
12
-
13
- 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_color(color)
9
+ cc.paint
10
+ end
11
+ end
12
+
13
+ end
14
14
  end
@@ -1,47 +1,47 @@
1
- module Squib
2
-
3
- # Cache all pngs we've already loaded
4
- #
5
- # :nodoc:
6
- # @api private
7
- def cache_load_image(file)
8
- @img_cache ||= {}
9
- @img_cache[file] || @img_cache[file] = Cairo::ImageSurface.from_png(file)
10
- end
11
- module_function :cache_load_image
12
-
13
- class Card
14
-
15
- # :nodoc:
16
- # @api private
17
- def png(file, x, y, alpha, blend)
18
- return if file.nil? or file.eql? ''
19
- png = Squib.cache_load_image(file)
20
- use_cairo do |cc|
21
- cc.set_source(png, x, y)
22
- cc.operator = blend unless blend == :none
23
- cc.paint(alpha)
24
- end
25
- end
26
-
27
- # :nodoc:
28
- # @api private
29
- def svg(file, id, x, y, width, height, alpha, blend)
30
- Squib.logger.debug {"Rendering: #{file}, #{id} #{x}, #{y}, #{width}, #{height}, #{alpha}, #{blend}"}
31
- return if file.nil? or file.eql? ''
32
- svg = RSVG::Handle.new_from_file(file)
33
- width = svg.width if width == :native
34
- height = svg.height if height == :native
35
- tmp = Cairo::ImageSurface.new(width, height)
36
- tmp_cc = Cairo::Context.new(tmp)
37
- tmp_cc.scale(width.to_f / svg.width.to_f, height.to_f / svg.height.to_f)
38
- tmp_cc.render_rsvg_handle(svg, id)
39
- use_cairo do |cc|
40
- cc.set_source(tmp, x, y)
41
- cc.operator = blend unless blend == :none
42
- cc.paint(alpha)
43
- end
44
- end
45
-
46
- end
47
- end
1
+ module Squib
2
+
3
+ # Cache all pngs we've already loaded
4
+ #
5
+ # :nodoc:
6
+ # @api private
7
+ def cache_load_image(file)
8
+ @img_cache ||= {}
9
+ @img_cache[file] || @img_cache[file] = Cairo::ImageSurface.from_png(file)
10
+ end
11
+ module_function :cache_load_image
12
+
13
+ class Card
14
+
15
+ # :nodoc:
16
+ # @api private
17
+ def png(file, x, y, alpha, blend)
18
+ return if file.nil? or file.eql? ''
19
+ png = Squib.cache_load_image(file)
20
+ use_cairo do |cc|
21
+ cc.set_source(png, x, y)
22
+ cc.operator = blend unless blend == :none
23
+ cc.paint(alpha)
24
+ end
25
+ end
26
+
27
+ # :nodoc:
28
+ # @api private
29
+ def svg(file, id, x, y, width, height, alpha, blend)
30
+ Squib.logger.debug {"Rendering: #{file}, #{id} #{x}, #{y}, #{width}, #{height}, #{alpha}, #{blend}"}
31
+ return if file.nil? or file.eql? ''
32
+ svg = RSVG::Handle.new_from_file(file)
33
+ width = svg.width if width == :native
34
+ height = svg.height if height == :native
35
+ tmp = Cairo::ImageSurface.new(width, height)
36
+ tmp_cc = Cairo::Context.new(tmp)
37
+ tmp_cc.scale(width.to_f / svg.width.to_f, height.to_f / svg.height.to_f)
38
+ tmp_cc.render_rsvg_handle(svg, id)
39
+ use_cairo do |cc|
40
+ cc.set_source(tmp, x, y)
41
+ cc.operator = blend unless blend == :none
42
+ cc.paint(alpha)
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -1,55 +1,55 @@
1
- module Squib
2
- class Deck
3
-
4
- # Lays out the cards in range and renders a PDF
5
- #
6
- # @example
7
- # save_pdf file: 'deck.pdf', margin: 75, gap: 5, trim: 37
8
- #
9
- # @option opts file [String] the name of the PDF file to save. See {file:README.md#Specifying_Files Specifying Files}
10
- # @option opts dir [String] (_output) the directory to save to. Created if it doesn't exist.
11
- # @option opts margin [Integer] (75) the margin around the outside of the page
12
- # @option opts gap [Integer] (0) the space in pixels between the cards
13
- # @option opts trim [Integer] (0) the space around the edge of each card to trim (e.g. to cut off the bleed margin for print-and-play)
14
- # @return [nil]
15
- # @api public
16
- def save_pdf(opts = {})
17
- p = needs(opts, [:file_to_save, :creatable_dir, :margin, :gap, :trim])
18
- width = 11 * @dpi ; height = 8.5 * @dpi #TODO: allow this to be specified too
19
- cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", width, height))
20
- x = p[:margin] ; y = p[:margin]
21
-
22
- @progress_bar.start("Saving PDF to #{p[:dir]}/#{p[:file]}", p[:range].size) do |bar|
23
- @cards.each_with_index do |card, i|
24
- surface = trim(card.cairo_surface, p[:trim], @width, @height)
25
- cc.set_source(surface, x, y)
26
- cc.paint
27
- bar.increment
28
- x += surface.width + p[:gap]
29
- if x > (width - surface.width - p[:margin])
30
- x = p[:margin]
31
- y += surface.height + p[:gap]
32
- if y > (height - surface.height - p[:margin])
33
- x = p[:margin] ; y = p[:margin]
34
- cc.show_page #next page
35
- end
36
- end
37
- end
38
- end
39
- end
40
-
41
- # :nodoc:
42
- # @api private
43
- def trim(surface, trim, width, height)
44
- if trim > 0
45
- tmp = Cairo::ImageSurface.new(width-2*trim, height-2*trim)
46
- cc = Cairo::Context.new(tmp)
47
- cc.set_source(surface,-1*trim, -1*trim)
48
- cc.paint
49
- surface = tmp
50
- end
51
- surface
52
- end
53
-
54
- end
1
+ module Squib
2
+ class Deck
3
+
4
+ # Lays out the cards in range and renders a PDF
5
+ #
6
+ # @example
7
+ # save_pdf file: 'deck.pdf', margin: 75, gap: 5, trim: 37
8
+ #
9
+ # @option opts file [String] the name of the PDF file to save. See {file:README.md#Specifying_Files Specifying Files}
10
+ # @option opts dir [String] (_output) the directory to save to. Created if it doesn't exist.
11
+ # @option opts margin [Integer] (75) the margin around the outside of the page
12
+ # @option opts gap [Integer] (0) the space in pixels between the cards
13
+ # @option opts trim [Integer] (0) the space around the edge of each card to trim (e.g. to cut off the bleed margin for print-and-play)
14
+ # @return [nil]
15
+ # @api public
16
+ def save_pdf(opts = {})
17
+ p = needs(opts, [:file_to_save, :creatable_dir, :margin, :gap, :trim])
18
+ width = 11 * @dpi ; height = 8.5 * @dpi #TODO: allow this to be specified too
19
+ cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", width, height))
20
+ x = p[:margin] ; y = p[:margin]
21
+
22
+ @progress_bar.start("Saving PDF to #{p[:dir]}/#{p[:file]}", p[:range].size) do |bar|
23
+ @cards.each_with_index do |card, i|
24
+ surface = trim(card.cairo_surface, p[:trim], @width, @height)
25
+ cc.set_source(surface, x, y)
26
+ cc.paint
27
+ bar.increment
28
+ x += surface.width + p[:gap]
29
+ if x > (width - surface.width - p[:margin])
30
+ x = p[:margin]
31
+ y += surface.height + p[:gap]
32
+ if y > (height - surface.height - p[:margin])
33
+ x = p[:margin] ; y = p[:margin]
34
+ cc.show_page #next page
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ # :nodoc:
42
+ # @api private
43
+ def trim(surface, trim, width, height)
44
+ if trim > 0
45
+ tmp = Cairo::ImageSurface.new(width-2*trim, height-2*trim)
46
+ cc = Cairo::Context.new(tmp)
47
+ cc.set_source(surface,-1*trim, -1*trim)
48
+ cc.paint
49
+ surface = tmp
50
+ end
51
+ surface
52
+ end
53
+
54
+ end
55
55
  end
@@ -1,33 +1,33 @@
1
- module Squib
2
- class Card
3
-
4
- # :nodoc:
5
- # @api private
6
- def save_png(i, dir, prefix, do_rotate, angle)
7
- if [true, :clockwise, :counterclockwise].include?(do_rotate)
8
- surface = rotated_image(angle)
9
- else
10
- surface = @cairo_surface
11
- end
12
- write_png(surface, i, dir, prefix)
13
- end
14
-
15
- # :nodoc:
16
- # @api private
17
- def rotated_image(angle)
18
- rotated_cc = Cairo::Context.new(Cairo::ImageSurface.new(@height, @width) )
19
- rotated_cc.translate(@height * 0.5, @width * 0.5)
20
- rotated_cc.rotate(angle)
21
- rotated_cc.translate(@width * -0.5, @height * -0.5)
22
- rotated_cc.set_source(@cairo_surface)
23
- rotated_cc.paint
24
- rotated_cc.target
25
- end
26
- # :nodoc:
27
- # @api private
28
- def write_png(surface, i, dir, prefix)
29
- surface.write_to_png("#{dir}/#{prefix}#{i}.png")
30
- end
31
-
32
- end
1
+ module Squib
2
+ class Card
3
+
4
+ # :nodoc:
5
+ # @api private
6
+ def save_png(i, dir, prefix, do_rotate, angle)
7
+ if [true, :clockwise, :counterclockwise].include?(do_rotate)
8
+ surface = rotated_image(angle)
9
+ else
10
+ surface = @cairo_surface
11
+ end
12
+ write_png(surface, i, dir, prefix)
13
+ end
14
+
15
+ # :nodoc:
16
+ # @api private
17
+ def rotated_image(angle)
18
+ rotated_cc = Cairo::Context.new(Cairo::ImageSurface.new(@height, @width) )
19
+ rotated_cc.translate(@height * 0.5, @width * 0.5)
20
+ rotated_cc.rotate(angle)
21
+ rotated_cc.translate(@width * -0.5, @height * -0.5)
22
+ rotated_cc.set_source(@cairo_surface)
23
+ rotated_cc.paint
24
+ rotated_cc.target
25
+ end
26
+ # :nodoc:
27
+ # @api private
28
+ def write_png(surface, i, dir, prefix)
29
+ surface.write_to_png("#{dir}/#{prefix}#{i}.png")
30
+ end
31
+
32
+ end
33
33
  end
@@ -1,60 +1,60 @@
1
- module Squib
2
- class Card
3
-
4
- # :nodoc:
5
- # @api private
6
- def rect(x, y, width, height, x_radius, y_radius, fill_color, stroke_color, stroke_width)
7
- width=@width if width==:native; height=@height if height==:native
8
- use_cairo do |cc|
9
- cc.rounded_rectangle(x, y, width, height, x_radius, y_radius)
10
- cc.set_source_color(stroke_color)
11
- cc.set_line_width(stroke_width)
12
- cc.stroke
13
- cc.rounded_rectangle(x, y, width, height, x_radius, y_radius)
14
- cc.set_source_color(fill_color)
15
- cc.fill
16
- end
17
- end
18
-
19
- # :nodoc:
20
- # @api private
21
- def circle(x, y, radius, fill_color, stroke_color, stroke_width)
22
- use_cairo do |cc|
23
- cc.circle(x, y, radius)
24
- cc.set_source_color(stroke_color)
25
- cc.set_line_width(stroke_width)
26
- cc.stroke
27
- cc.circle(x, y, radius)
28
- cc.set_source_color(fill_color)
29
- cc.fill
30
- end
31
- end
32
-
33
- # :nodoc:
34
- # @api private
35
- def triangle(x1, y1, x2, y2, x3, y3, fill_color, stroke_color, stroke_width)
36
- use_cairo do |cc|
37
- cc.triangle(x1, y1, x2, y2, x3, y3)
38
- cc.set_source_color(stroke_color)
39
- cc.set_line_width(stroke_width)
40
- cc.stroke
41
- cc.triangle(x1, y1, x2, y2, x3, y3)
42
- cc.set_source_color(fill_color)
43
- cc.fill
44
- end
45
- end
46
-
47
- # :nodoc:
48
- # @api private
49
- def line(x1, y1, x2, y2, stroke_color, stroke_width)
50
- use_cairo do |cc|
51
- cc.move_to(x1, y1)
52
- cc.line_to(x2, y2)
53
- cc.set_source_color(stroke_color)
54
- cc.set_line_width(stroke_width)
55
- cc.stroke
56
- end
57
- end
58
-
59
- end
1
+ module Squib
2
+ class Card
3
+
4
+ # :nodoc:
5
+ # @api private
6
+ def rect(x, y, width, height, x_radius, y_radius, fill_color, stroke_color, stroke_width)
7
+ width=@width if width==:native; height=@height if height==:native
8
+ use_cairo do |cc|
9
+ cc.rounded_rectangle(x, y, width, height, x_radius, y_radius)
10
+ cc.set_source_color(stroke_color)
11
+ cc.set_line_width(stroke_width)
12
+ cc.stroke
13
+ cc.rounded_rectangle(x, y, width, height, x_radius, y_radius)
14
+ cc.set_source_color(fill_color)
15
+ cc.fill
16
+ end
17
+ end
18
+
19
+ # :nodoc:
20
+ # @api private
21
+ def circle(x, y, radius, fill_color, stroke_color, stroke_width)
22
+ use_cairo do |cc|
23
+ cc.circle(x, y, radius)
24
+ cc.set_source_color(stroke_color)
25
+ cc.set_line_width(stroke_width)
26
+ cc.stroke
27
+ cc.circle(x, y, radius)
28
+ cc.set_source_color(fill_color)
29
+ cc.fill
30
+ end
31
+ end
32
+
33
+ # :nodoc:
34
+ # @api private
35
+ def triangle(x1, y1, x2, y2, x3, y3, fill_color, stroke_color, stroke_width)
36
+ use_cairo do |cc|
37
+ cc.triangle(x1, y1, x2, y2, x3, y3)
38
+ cc.set_source_color(stroke_color)
39
+ cc.set_line_width(stroke_width)
40
+ cc.stroke
41
+ cc.triangle(x1, y1, x2, y2, x3, y3)
42
+ cc.set_source_color(fill_color)
43
+ cc.fill
44
+ end
45
+ end
46
+
47
+ # :nodoc:
48
+ # @api private
49
+ def line(x1, y1, x2, y2, stroke_color, stroke_width)
50
+ use_cairo do |cc|
51
+ cc.move_to(x1, y1)
52
+ cc.line_to(x2, y2)
53
+ cc.set_source_color(stroke_color)
54
+ cc.set_line_width(stroke_width)
55
+ cc.stroke
56
+ end
57
+ end
58
+
59
+ end
60
60
  end
@@ -1,114 +1,117 @@
1
- require 'pango'
2
-
3
- module Squib
4
- class Card
5
-
6
- # :nodoc:
7
- # @api private
8
- def draw_text_hint(x,y,layout, color)
9
- color ||= @deck.text_hint
10
- return if color.to_s.eql? 'off'
11
- # when w,h < 0, it was never set. extents[1] are ink extents
12
- w = layout.width / Pango::SCALE
13
- w = layout.extents[1].width / Pango::SCALE if w < 0
14
- h = layout.height / Pango::SCALE
15
- h = layout.extents[1].height / Pango::SCALE if h < 0
16
- rect(x,y,w,h,0,0,'#0000',color, 2.0)
17
- end
18
-
19
- # :nodoc:
20
- # @api private
21
- def ellipsize(layout, ellipsize)
22
- unless ellipsize.nil?
23
- h = { :none => Pango::Layout::ELLIPSIZE_NONE,
24
- :start => Pango::Layout::ELLIPSIZE_START,
25
- :middle => Pango::Layout::ELLIPSIZE_MIDDLE,
26
- :end => Pango::Layout::ELLIPSIZE_END,
27
- true => Pango::Layout::ELLIPSIZE_END,
28
- false => Pango::Layout::ELLIPSIZE_NONE
29
- }
30
- layout.ellipsize = h[ellipsize]
31
- end
32
- layout
33
- end
34
-
35
- # :nodoc:
36
- # @api private
37
- def wrap(layout, wrap)
38
- unless wrap.nil?
39
- h = { :word => Pango::Layout::WRAP_WORD,
40
- :char => Pango::Layout::WRAP_CHAR,
41
- :word_char => Pango::Layout::WRAP_WORD_CHAR,
42
- true => Pango::Layout::WRAP_WORD_CHAR,
43
- false => nil,
44
- :none => nil
45
- }
46
- layout.wrap = h[wrap]
47
- end
48
- layout
49
- end
50
-
51
- # :nodoc:
52
- # @api private
53
- def align(layout, align)
54
- unless align.nil?
55
- h = { :left => Pango::ALIGN_LEFT,
56
- :right => Pango::ALIGN_RIGHT,
57
- :center => Pango::ALIGN_CENTER
58
- }
59
- layout.alignment = h[align]
60
- end
61
- layout
62
- end
63
-
64
- # :nodoc:
65
- # @api private
66
- def valign(cc, layout, x, y, valign)
67
- if layout.height > 0
68
- ink_extents = layout.extents[1]
69
- case valign
70
- when :middle
71
- cc.move_to(x, y + (layout.height - ink_extents.height) / (2 * Pango::SCALE))
72
- when :bottom
73
- cc.move_to(x, y + (layout.height - ink_extents.height) / Pango::SCALE)
74
- end
75
- end
76
- end
77
-
78
- # :nodoc:
79
- # @api private
80
- def setwh(layout, width, height)
81
- layout.width = width * Pango::SCALE unless width.nil? || width == :native
82
- layout.height = height * Pango::SCALE unless height.nil? || height == :native
83
- layout
84
- end
85
-
86
- # :nodoc:
87
- # @api private
88
- def text(str, font, color,
89
- x, y, width, height,
90
- markup, justify, wrap, ellipsize,
91
- spacing, align, valign, hint)
92
- Squib.logger.debug {"Placing '#{str}'' with font '#{font}' @ #{x}, #{y}, color: #{color}, etc."}
93
- use_cairo do |cc|
94
- cc.set_source_color(color)
95
- cc.move_to(x,y)
96
- layout = cc.create_pango_layout
97
- layout.font_description = Pango::FontDescription.new(font)
98
- layout.text = str.to_s
99
- layout.markup = str.to_s if markup
100
- layout = setwh(layout, width, height)
101
- layout = wrap(layout, wrap)
102
- layout = ellipsize(layout, ellipsize)
103
- layout = align(layout, align)
104
- layout.justify = justify unless justify.nil?
105
- layout.spacing = spacing * Pango::SCALE unless spacing.nil?
106
- cc.update_pango_layout(layout)
107
- valign(cc, layout, x,y, valign)
108
- cc.update_pango_layout(layout) ; cc.show_pango_layout(layout)
109
- draw_text_hint(x,y,layout,hint)
110
- end
111
- end
112
-
113
- end
1
+ require 'pango'
2
+
3
+ module Squib
4
+ class Card
5
+
6
+ # :nodoc:
7
+ # @api private
8
+ def draw_text_hint(x,y,layout, color)
9
+ color = @deck.text_hint if color.to_s.eql? 'off' and not @deck.text_hint.to_s.eql? 'off'
10
+ return if color.to_s.eql? 'off'
11
+ # when w,h < 0, it was never set. extents[1] are ink extents
12
+ w = layout.width / Pango::SCALE
13
+ w = layout.extents[1].width / Pango::SCALE if w < 0
14
+ h = layout.height / Pango::SCALE
15
+ h = layout.extents[1].height / Pango::SCALE if h < 0
16
+ rect(x,y,w,h,0,0,'#0000',color, 2.0)
17
+ end
18
+
19
+ # :nodoc:
20
+ # @api private
21
+ def ellipsize(layout, ellipsize)
22
+ unless ellipsize.nil?
23
+ h = { :none => Pango::Layout::ELLIPSIZE_NONE,
24
+ :start => Pango::Layout::ELLIPSIZE_START,
25
+ :middle => Pango::Layout::ELLIPSIZE_MIDDLE,
26
+ :end => Pango::Layout::ELLIPSIZE_END,
27
+ true => Pango::Layout::ELLIPSIZE_END,
28
+ false => Pango::Layout::ELLIPSIZE_NONE
29
+ }
30
+ layout.ellipsize = h[ellipsize]
31
+ end
32
+ layout
33
+ end
34
+
35
+ # :nodoc:
36
+ # @api private
37
+ def wrap(layout, wrap)
38
+ unless wrap.nil?
39
+ h = { :word => Pango::Layout::WRAP_WORD,
40
+ :char => Pango::Layout::WRAP_CHAR,
41
+ :word_char => Pango::Layout::WRAP_WORD_CHAR,
42
+ true => Pango::Layout::WRAP_WORD_CHAR,
43
+ false => nil,
44
+ :none => nil
45
+ }
46
+ layout.wrap = h[wrap]
47
+ end
48
+ layout
49
+ end
50
+
51
+ # :nodoc:
52
+ # @api private
53
+ def align(layout, align)
54
+ unless align.nil?
55
+ h = { :left => Pango::ALIGN_LEFT,
56
+ :right => Pango::ALIGN_RIGHT,
57
+ :center => Pango::ALIGN_CENTER
58
+ }
59
+ layout.alignment = h[align]
60
+ end
61
+ layout
62
+ end
63
+
64
+ # :nodoc:
65
+ # @api private
66
+ def valign(cc, layout, x, y, valign)
67
+ if layout.height > 0
68
+ ink_extents = layout.extents[1]
69
+ case valign
70
+ when :middle
71
+ cc.move_to(x, y + (layout.height - ink_extents.height) / (2 * Pango::SCALE))
72
+ when :bottom
73
+ cc.move_to(x, y + (layout.height - ink_extents.height) / Pango::SCALE)
74
+ end
75
+ end
76
+ end
77
+
78
+ # :nodoc:
79
+ # @api private
80
+ def setwh(layout, width, height)
81
+ layout.width = width * Pango::SCALE unless width.nil? || width == :native
82
+ layout.height = height * Pango::SCALE unless height.nil? || height == :native
83
+ layout
84
+ end
85
+
86
+ # :nodoc:
87
+ # @api private
88
+ def text(str, font, font_size, color,
89
+ x, y, width, height,
90
+ markup, justify, wrap, ellipsize,
91
+ spacing, align, valign, hint, angle)
92
+ Squib.logger.debug {"Placing '#{str}'' with font '#{font}' @ #{x}, #{y}, color: #{color}, rotate: #{rotation} etc."}
93
+ use_cairo do |cc|
94
+ cc.set_source_color(color)
95
+ cc.move_to(x,y)
96
+ cc.rotate(angle)
97
+ layout = cc.create_pango_layout
98
+ font_desc = Pango::FontDescription.new(font)
99
+ font_desc.size = font_size * Pango::SCALE unless font_size.nil?
100
+ layout.font_description = font_desc
101
+ layout.text = str.to_s
102
+ layout.markup = str.to_s if markup
103
+ layout = setwh(layout, width, height)
104
+ layout = wrap(layout, wrap)
105
+ layout = ellipsize(layout, ellipsize)
106
+ layout = align(layout, align)
107
+ layout.justify = justify unless justify.nil?
108
+ layout.spacing = spacing * Pango::SCALE unless spacing.nil?
109
+ cc.update_pango_layout(layout)
110
+ valign(cc, layout, x,y, valign)
111
+ cc.update_pango_layout(layout) ; cc.show_pango_layout(layout)
112
+ draw_text_hint(x,y,layout,hint)
113
+ end
114
+ end
115
+
116
+ end
114
117
  end