thinreports 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CONTRIBUTING.md +2 -0
  3. data/.travis.yml +3 -3
  4. data/CHANGELOG.md +98 -1
  5. data/README.md +0 -9
  6. data/Rakefile +1 -2
  7. data/examples/character_spacing/character_spacing.tlf +293 -1
  8. data/examples/dynamic_image/dynamic_image.tlf +661 -1
  9. data/examples/dynamic_style/dynamic_style.rb +4 -4
  10. data/examples/dynamic_style/dynamic_style.tlf +1835 -1
  11. data/examples/dynamic_style/dynamic_style_in_list.tlf +344 -1
  12. data/examples/eudc/eudc.tlf +180 -1
  13. data/examples/hidden_shapes/hidden_shapes.tlf +449 -1
  14. data/examples/list_events/list_events.rb +1 -28
  15. data/examples/list_events/list_events.tlf +361 -0
  16. data/examples/list_manual_generation/list_manual_generation.tlf +132 -1
  17. data/examples/list_page_number/list_page_number.tlf +254 -1
  18. data/examples/page_number/page_number.tlf +215 -1
  19. data/examples/palleted_png/palleted_png.rb +2 -0
  20. data/examples/palleted_png/palleted_png.tlf +47 -1
  21. data/examples/password_setting/password_setting.tlf +45 -1
  22. data/examples/report_callbacks/report_callbacks.rb +1 -18
  23. data/examples/report_callbacks/report_callbacks.tlf +147 -1
  24. data/examples/single_line_tblock/single_line_tblock.tlf +170 -1
  25. data/examples/tblock_overflow/tblock_overflow.tlf +538 -1
  26. data/examples/tblock_styles/font_size.tlf +383 -1
  27. data/examples/tblock_styles/tblock_styles.tlf +889 -1
  28. data/examples/text_align/text_align.tlf +241 -1
  29. data/examples/typeB_page_size/B4_ISO.tlf +45 -1
  30. data/examples/typeB_page_size/B4_JIS.tlf +45 -1
  31. data/examples/word_wrap/word_wrap.tlf +297 -1
  32. data/lib/thinreports.rb +0 -1
  33. data/lib/thinreports/config.rb +3 -0
  34. data/lib/thinreports/core/errors.rb +0 -3
  35. data/lib/thinreports/core/format/base.rb +4 -4
  36. data/lib/thinreports/core/shape.rb +1 -9
  37. data/lib/thinreports/core/shape/basic.rb +3 -3
  38. data/lib/thinreports/core/shape/basic/block_format.rb +1 -1
  39. data/lib/thinreports/core/shape/basic/format.rb +2 -14
  40. data/lib/thinreports/core/shape/basic/internal.rb +2 -2
  41. data/lib/thinreports/core/shape/image_block.rb +3 -3
  42. data/lib/thinreports/core/shape/image_block/format.rb +0 -2
  43. data/lib/thinreports/core/shape/image_block/internal.rb +1 -1
  44. data/lib/thinreports/core/shape/list.rb +3 -7
  45. data/lib/thinreports/core/shape/list/format.rb +37 -24
  46. data/lib/thinreports/core/shape/list/manager.rb +1 -36
  47. data/lib/thinreports/core/shape/list/page.rb +0 -15
  48. data/lib/thinreports/core/shape/list/page_state.rb +1 -1
  49. data/lib/thinreports/core/shape/list/section_format.rb +14 -12
  50. data/lib/thinreports/core/shape/list/section_internal.rb +4 -7
  51. data/lib/thinreports/core/shape/manager/format.rb +0 -2
  52. data/lib/thinreports/core/shape/manager/internal.rb +1 -1
  53. data/lib/thinreports/core/shape/page_number.rb +1 -1
  54. data/lib/thinreports/core/shape/page_number/format.rb +9 -6
  55. data/lib/thinreports/core/shape/page_number/interface.rb +1 -1
  56. data/lib/thinreports/core/shape/page_number/internal.rb +2 -6
  57. data/lib/thinreports/core/shape/style/base.rb +11 -13
  58. data/lib/thinreports/core/shape/style/basic.rb +4 -4
  59. data/lib/thinreports/core/shape/style/graphic.rb +4 -10
  60. data/lib/thinreports/core/shape/style/text.rb +36 -69
  61. data/lib/thinreports/core/shape/text.rb +3 -3
  62. data/lib/thinreports/core/shape/text/format.rb +3 -13
  63. data/lib/thinreports/core/shape/text/internal.rb +2 -2
  64. data/lib/thinreports/core/shape/text_block.rb +3 -3
  65. data/lib/thinreports/core/shape/text_block/format.rb +9 -6
  66. data/lib/thinreports/core/shape/text_block/interface.rb +7 -7
  67. data/lib/thinreports/core/shape/text_block/internal.rb +1 -1
  68. data/lib/thinreports/generator/pdf.rb +0 -1
  69. data/lib/thinreports/generator/pdf/document.rb +2 -2
  70. data/lib/thinreports/generator/pdf/document/draw_shape.rb +25 -51
  71. data/lib/thinreports/generator/pdf/document/draw_template_items.rb +66 -0
  72. data/lib/thinreports/generator/pdf/document/font.rb +3 -8
  73. data/lib/thinreports/generator/pdf/document/graphics/attributes.rb +97 -77
  74. data/lib/thinreports/generator/pdf/document/graphics/basic.rb +18 -13
  75. data/lib/thinreports/generator/pdf/document/graphics/image.rb +17 -12
  76. data/lib/thinreports/generator/pdf/document/page.rb +1 -1
  77. data/lib/thinreports/generator/pdf/drawer/list.rb +5 -1
  78. data/lib/thinreports/generator/pdf/drawer/list_section.rb +1 -4
  79. data/lib/thinreports/generator/pdf/drawer/page.rb +9 -9
  80. data/lib/thinreports/layout.rb +3 -3
  81. data/lib/thinreports/layout/base.rb +0 -16
  82. data/lib/thinreports/layout/format.rb +38 -57
  83. data/lib/thinreports/layout/legacy_schema.rb +354 -0
  84. data/lib/thinreports/layout/version.rb +1 -1
  85. data/lib/thinreports/report.rb +0 -1
  86. data/lib/thinreports/report/base.rb +3 -17
  87. data/lib/thinreports/report/internal.rb +13 -55
  88. data/lib/thinreports/report/page.rb +1 -1
  89. data/lib/thinreports/version.rb +1 -1
  90. data/test/data/legacy_layout/all-items.tlf +1 -0
  91. data/test/schema_helper.rb +121 -0
  92. data/test/test_helper.rb +13 -50
  93. data/test/unit/core/format/test_base.rb +35 -31
  94. data/test/unit/core/shape/base/test_internal.rb +1 -1
  95. data/test/unit/core/shape/basic/test_block_format.rb +23 -0
  96. data/test/unit/core/shape/basic/{test_basic_interface.rb → test_block_interface.rb} +1 -1
  97. data/test/unit/core/shape/basic/{test_basic_internal.rb → test_block_internal.rb} +4 -7
  98. data/test/unit/core/shape/basic/test_format.rb +25 -47
  99. data/test/unit/core/shape/basic/test_interface.rb +5 -5
  100. data/test/unit/core/shape/basic/test_internal.rb +6 -11
  101. data/test/unit/core/shape/image_block/test_interface.rb +5 -5
  102. data/test/unit/core/shape/image_block/test_internal.rb +2 -2
  103. data/test/unit/core/shape/list/test_format.rb +86 -44
  104. data/test/unit/core/shape/list/test_manager.rb +10 -65
  105. data/test/unit/core/shape/list/test_page.rb +10 -61
  106. data/test/unit/core/shape/list/test_page_state.rb +1 -1
  107. data/test/unit/core/shape/list/test_section_format.rb +19 -28
  108. data/test/unit/core/shape/list/test_section_interface.rb +14 -6
  109. data/test/unit/core/shape/list/test_section_internal.rb +17 -23
  110. data/test/unit/core/shape/manager/test_format.rb +1 -1
  111. data/test/unit/core/shape/manager/test_internal.rb +59 -98
  112. data/test/unit/core/shape/manager/test_target.rb +21 -30
  113. data/test/unit/core/shape/page_number/test_format.rb +34 -52
  114. data/test/unit/core/shape/page_number/test_interface.rb +1 -1
  115. data/test/unit/core/shape/page_number/test_internal.rb +2 -2
  116. data/test/unit/core/shape/styles/test_base.rb +16 -41
  117. data/test/unit/core/shape/styles/test_basic.rb +7 -7
  118. data/test/unit/core/shape/styles/test_graphic.rb +27 -32
  119. data/test/unit/core/shape/styles/test_text.rb +68 -311
  120. data/test/unit/core/shape/text/test_format.rb +30 -59
  121. data/test/unit/core/shape/text/test_internal.rb +5 -5
  122. data/test/unit/core/shape/text_block/formatter/test_datetime.rb +13 -13
  123. data/test/unit/core/shape/text_block/test_format.rb +145 -146
  124. data/test/unit/core/shape/text_block/test_interface.rb +9 -9
  125. data/test/unit/core/shape/text_block/test_internal.rb +9 -9
  126. data/test/unit/core/test_shape.rb +19 -31
  127. data/test/unit/generator/pdf/document/graphics/test_attributes.rb +112 -122
  128. data/test/unit/generator/pdf/document/graphics/test_basic.rb +45 -0
  129. data/test/unit/generator/pdf/document/graphics/test_image.rb +5 -5
  130. data/test/unit/generator/pdf/document/test_font.rb +4 -10
  131. data/test/unit/generator/pdf/document/test_page.rb +13 -11
  132. data/test/unit/generator/pdf/document/test_parse_color.rb +5 -5
  133. data/test/unit/generator/test_base.rb +5 -5
  134. data/test/unit/generator/test_configuration.rb +4 -8
  135. data/test/unit/generator/test_pdf.rb +3 -2
  136. data/test/unit/layout/test_base.rb +16 -22
  137. data/test/unit/layout/test_format.rb +66 -86
  138. data/test/unit/layout/test_legacy_schema.rb +587 -0
  139. data/test/unit/layout/test_version.rb +20 -20
  140. data/test/unit/report/test_base.rb +32 -45
  141. data/test/unit/report/test_internal.rb +43 -99
  142. data/test/unit/test_config.rb +4 -2
  143. data/test/unit/test_layout.rb +1 -1
  144. metadata +20 -48
  145. data/examples/list_events/list_events_0_7_7.tlf +0 -1
  146. data/examples/list_events/list_events_0_8.tlf +0 -1
  147. data/lib/thinreports/core/events.rb +0 -69
  148. data/lib/thinreports/core/format/builder.rb +0 -62
  149. data/lib/thinreports/core/shape/list/configuration.rb +0 -54
  150. data/lib/thinreports/core/shape/list/events.rb +0 -47
  151. data/lib/thinreports/core/shape/list/store.rb +0 -33
  152. data/lib/thinreports/generator/pdf/configuration.rb +0 -33
  153. data/lib/thinreports/generator/pdf/document/parse_svg.rb +0 -104
  154. data/lib/thinreports/layout/configuration.rb +0 -29
  155. data/test/data/layout_block.tlf +0 -1
  156. data/test/data/layout_list.tlf +0 -1
  157. data/test/data/layout_list_noheader.tlf +0 -1
  158. data/test/data/layout_text1.tlf +0 -1
  159. data/test/data/layout_text2.tlf +0 -1
  160. data/test/unit/core/format/test_builder.rb +0 -109
  161. data/test/unit/core/shape/basic/test_basic_format.rb +0 -30
  162. data/test/unit/core/shape/image_block/test_format.rb +0 -58
  163. data/test/unit/core/shape/list/test_configuration.rb +0 -69
  164. data/test/unit/core/shape/list/test_events.rb +0 -36
  165. data/test/unit/core/shape/list/test_store.rb +0 -41
  166. data/test/unit/core/test_events.rb +0 -93
  167. data/test/unit/generator/pdf/document/test_draw_shape.rb +0 -44
  168. data/test/unit/generator/pdf/test_configuration.rb +0 -31
  169. data/test/unit/layout/test_configuration.rb +0 -21
  170. data/test/unit/report/test_events.rb +0 -22
@@ -2,11 +2,11 @@
2
2
 
3
3
  module Thinreports
4
4
  module Core::Shape
5
-
5
+
6
6
  module TextBlock
7
- TYPE_NAME = 's-tblock'
7
+ TYPE_NAME = 'text-block'
8
8
  end
9
-
9
+
10
10
  end
11
11
  end
12
12
 
@@ -4,9 +4,12 @@ module Thinreports
4
4
  module Core::Shape
5
5
 
6
6
  class TextBlock::Format < Basic::BlockFormat
7
- config_reader ref_id: %w( ref-id )
8
- config_reader :valign, :overflow
9
- config_reader line_height: %w( line-height )
7
+ # For saving compatible 0.8.x format API
8
+ config_reader ref_id: %w( reference-id )
9
+ config_reader valign: %w( style vertical-align )
10
+ config_reader overflow: %w( style overflow )
11
+ config_reader line_height: %w( style line-height )
12
+
10
13
  config_reader format_base: %w( format base ),
11
14
  format_type: %w( format type ),
12
15
  format_datetime_format: %w( format datetime format ),
@@ -14,9 +17,8 @@ module Thinreports
14
17
  format_number_precision: %w( format number precision ),
15
18
  format_padding_char: %w( format padding char ),
16
19
  format_padding_dir: %w( format padding direction )
17
- config_reader word_wrap: %w( word-wrap )
18
20
 
19
- config_checker 'true', :multiple
21
+ config_checker true, multiple: %w( multiple-line )
20
22
  config_checker 'R', format_padding_rdir: %w( format padding direction )
21
23
 
22
24
  config_reader format_padding_length: %w( format padding length ) do |len|
@@ -27,7 +29,8 @@ module Thinreports
27
29
  %w( datetime number padding ).include?(type)
28
30
  end
29
31
 
30
- config_reader :has_reference? => %w( ref-id ) do |ref_id|
32
+ # For saving compatible 0.8.x format API
33
+ def has_reference?
31
34
  !blank_value?(ref_id)
32
35
  end
33
36
  end
@@ -2,17 +2,17 @@
2
2
 
3
3
  module Thinreports
4
4
  module Core::Shape
5
-
5
+
6
6
  class TextBlock::Interface < Basic::BlockInterface
7
7
  internal_delegators :format_enabled?
8
-
8
+
9
9
  # @param [Boolean] enabled
10
10
  # @return [self]
11
11
  def format_enabled(enabled)
12
12
  internal.format_enabled(enabled)
13
13
  self
14
14
  end
15
-
15
+
16
16
  # @param [Object] val
17
17
  # @param [Hash<Symbol, Object>] style_settings
18
18
  # @return [self]
@@ -20,14 +20,14 @@ module Thinreports
20
20
  value(val)
21
21
  styles(style_settings) #=> self
22
22
  end
23
-
23
+
24
24
  private
25
-
25
+
26
26
  # @see Thinreports::Core::Shape::Base::Interface#init_internal
27
27
  def init_internal(parent, format)
28
28
  TextBlock::Internal.new(parent, format)
29
29
  end
30
30
  end
31
-
31
+
32
32
  end
33
- end
33
+ end
@@ -61,7 +61,7 @@ module Thinreports
61
61
  end
62
62
 
63
63
  def type_of?(type_name)
64
- type_name == :tblock || super
64
+ type_name == TextBlock::TYPE_NAME || super
65
65
  end
66
66
 
67
67
  private
@@ -56,7 +56,6 @@ module Thinreports
56
56
  end
57
57
  end
58
58
 
59
- require 'thinreports/generator/pdf/configuration'
60
59
  require 'thinreports/generator/pdf/prawn_ext'
61
60
  require 'thinreports/generator/pdf/document'
62
61
  require 'thinreports/generator/pdf/drawer/base'
@@ -4,7 +4,7 @@ require 'thinreports/generator/pdf/document/font'
4
4
  require 'thinreports/generator/pdf/document/parse_color'
5
5
  require 'thinreports/generator/pdf/document/graphics'
6
6
  require 'thinreports/generator/pdf/document/draw_shape'
7
- require 'thinreports/generator/pdf/document/parse_svg'
7
+ require 'thinreports/generator/pdf/document/draw_template_items'
8
8
  require 'thinreports/generator/pdf/document/page'
9
9
 
10
10
  module Thinreports
@@ -17,7 +17,7 @@ module Thinreports
17
17
  include PDF::ParseColor
18
18
  include PDF::Graphics
19
19
  include PDF::DrawShape
20
- include PDF::ParseSVG
20
+ include PDF::DrawTemplateItems
21
21
  include PDF::Page
22
22
 
23
23
  # @return [Prawn::Document]
@@ -6,11 +6,11 @@ module Thinreports
6
6
  module PDF::DrawShape
7
7
  # @param [Thinreports::Core::Shape::TextBlock::Internal] shape
8
8
  def draw_shape_tblock(shape)
9
- x, y, w, h = shape.box.values_at('x', 'y', 'width', 'height')
9
+ x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
10
10
 
11
11
  content = shape.real_value.to_s
12
12
  unless content.empty?
13
- attrs = shape_text_attrs(shape)
13
+ attrs = build_text_attributes(shape.style.finalized_styles)
14
14
 
15
15
  unless shape.multiple?
16
16
  content = content.gsub(/\n/, ' ')
@@ -21,83 +21,57 @@ module Thinreports
21
21
  end
22
22
 
23
23
  def draw_shape_pageno(shape, page_no, page_count)
24
- x, y, w, h = shape.box.values_at('x', 'y', 'width', 'height')
24
+ x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
25
25
 
26
26
  text_box(shape.build_format(page_no, page_count), x, y, w, h,
27
- common_text_attrs(shape.style.svg_attrs))
27
+ build_text_attributes(shape.style.finalized_styles))
28
28
  end
29
29
 
30
30
  # @param [Thinreports::Core::Shape::Basic::Internal] shape
31
31
  def draw_shape_image(shape)
32
- x, y, w, h = shape.style.svg_attrs.values_at('x', 'y', 'width', 'height')
32
+ x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
33
33
 
34
- image_type, image_data = extract_base64_string(shape.style.svg_attrs['xlink:href'])
35
- base64image(image_type, image_data, x, y, w, h)
34
+ image_data = shape.format.attributes['data']
35
+ base64image(image_data['mime-type'], image_data['base64'], x, y, w, h)
36
36
  end
37
37
 
38
38
  # @param [Thinreports::Core::Shape::ImageBlock::Internal] shape
39
39
  def draw_shape_iblock(shape)
40
- x, y, w, h = shape.box.values_at('x', 'y', 'width', 'height')
41
- unless blank_value?(shape.src)
42
- posx = shape.format.position_x
43
- posy = shape.format.position_y
44
-
45
- image_box(shape.src, x, y, w, h,
46
- position_x: posx ? posx.to_sym : nil,
47
- position_y: posy ? posy.to_sym : nil)
48
- end
40
+ return if blank_value?(shape.src)
41
+
42
+ x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
43
+ style = shape.style.finalized_styles
44
+
45
+ image_box(shape.src, x, y, w, h,
46
+ position_x: image_position_x(style['position-x']),
47
+ position_y: image_position_y(style['position-y']))
49
48
  end
50
49
 
51
50
  # @param [Thinreports::Core::Shape::Text::Internal] shape
52
51
  def draw_shape_text(shape)
53
- x, y, w, h = shape.box.values_at('x', 'y', 'width', 'height')
54
- text(shape.text.join("\n"), x, y, w, h,
55
- shape_text_attrs(shape))
52
+ x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
53
+ text(shape.texts.join("\n"), x, y, w, h, build_text_attributes(shape.style.finalized_styles))
56
54
  end
57
55
 
58
56
  # @param [Thinreports::Core::Shape::Basic::Internal] shape
59
57
  def draw_shape_ellipse(shape)
60
- args = shape.style.svg_attrs.values_at('cx', 'cy', 'rx', 'ry')
61
- args << common_graphic_attrs(shape.style.svg_attrs)
62
- ellipse(*args)
58
+ cx, cy, rx, ry = shape.format.attributes.values_at('cx', 'cy', 'rx', 'ry')
59
+ ellipse(cx, cy, rx, ry, build_graphic_attributes(shape.style.finalized_styles))
63
60
  end
64
61
 
65
62
  # @param [Thinreports::Core::Shape::Basic::Internal] shape
66
63
  def draw_shape_line(shape)
67
- args = shape.style.svg_attrs.values_at('x1', 'y1', 'x2', 'y2')
68
- args << common_graphic_attrs(shape.style.svg_attrs)
69
- line(*args)
64
+ x1, y1, x2, y2 = shape.format.attributes.values_at('x1', 'y1', 'x2', 'y2')
65
+ line(x1, y1, x2, y2, build_graphic_attributes(shape.style.finalized_styles))
70
66
  end
71
67
 
72
68
  # @param [Thinreports::Core::Shape::Basic::Internal] shape
73
69
  def draw_shape_rect(shape)
74
- args = shape.style.svg_attrs.values_at('x', 'y', 'width', 'height')
75
- args << common_graphic_attrs(shape.style.svg_attrs) do |attrs|
76
- attrs[:radius] = shape.style.svg_attrs['rx']
77
- end
78
- rect(*args)
79
- end
80
-
81
- private
82
-
83
- # @param [Thinreports::Core::Shape::Text::Internal, Thinreports::Core::Shape::TextBlock::Internal] shape
84
- # @return [Hash]
85
- def shape_text_attrs(shape)
86
- format = shape.format
87
-
88
- common_text_attrs(shape.style.svg_attrs) do |attrs|
89
- # Set the :line_height option.
90
- attrs[:line_height] = format.line_height unless blank_value?(format.line_height)
91
- # Set the :valign option.
92
- attrs[:valign] = shape.style.valign
93
-
94
- if shape.type_of?(:tblock)
95
- # Set the :overflow option.
96
- attrs[:overflow] = text_overflow(format.overflow)
97
- # Set the :word_wrap option
98
- attrs[:word_wrap] = text_word_wrap(format.word_wrap)
99
- end
70
+ x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
71
+ rect_attributes = build_graphic_attributes(shape.style.finalized_styles) do |attrs|
72
+ attrs[:radius] = shape.format.attributes['rx']
100
73
  end
74
+ rect(x, y, w, h, rect_attributes)
101
75
  end
102
76
  end
103
77
 
@@ -0,0 +1,66 @@
1
+ # coding: utf-8
2
+
3
+ module Thinreports
4
+ module Generator
5
+
6
+ module PDF::DrawTemplateItems
7
+ # @param [Array<Hash>] items
8
+ def draw_template_items(items)
9
+ items.each do |item_attributes|
10
+ next unless drawable?(item_attributes)
11
+
12
+ case item_attributes['type']
13
+ when 'text' then draw_text(item_attributes)
14
+ when 'image' then draw_image(item_attributes)
15
+ when 'rect' then draw_rect(item_attributes)
16
+ when 'ellipse' then draw_ellipse(item_attributes)
17
+ when 'line' then draw_line(item_attributes)
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ # @param [Hash] item_attributes
25
+ def draw_rect(item_attributes)
26
+ x, y, w, h = item_attributes.values_at('x', 'y', 'width', 'height')
27
+ graphic_attributes = build_graphic_attributes(item_attributes['style']) do |attrs|
28
+ attrs[:radius] = item_attributes['rx']
29
+ end
30
+
31
+ rect(x, y, w, h, graphic_attributes)
32
+ end
33
+
34
+ # @see #draw_rect
35
+ def draw_ellipse(item_attributes)
36
+ x, y, rx, ry = item_attributes.values_at('cx', 'cy', 'rx', 'ry')
37
+ ellipse(x, y, rx, ry, build_graphic_attributes(item_attributes['style']))
38
+ end
39
+
40
+ # @see #draw_rect
41
+ def draw_line(item_attributes)
42
+ x1, y1, x2, y2 = item_attributes.values_at('x1', 'y1', 'x2', 'y2')
43
+ line(x1, y1, x2, y2, build_graphic_attributes(item_attributes['style']))
44
+ end
45
+
46
+ # @see #draw_rect
47
+ def draw_text(item_attributes)
48
+ x, y, w, h = item_attributes.values_at('x', 'y', 'width', 'height')
49
+ text(item_attributes['texts'].join("\n"), x, y, w, h, build_text_attributes(item_attributes['style']))
50
+ end
51
+
52
+ # @see #draw_rect
53
+ def draw_image(item_attributes)
54
+ x, y, w, h = item_attributes.values_at('x', 'y', 'width', 'height')
55
+ image_data = item_attributes['data']
56
+
57
+ base64image(image_data['mime-type'], image_data['base64'], x, y, w, h)
58
+ end
59
+
60
+ def drawable?(item_attributes)
61
+ item_attributes['id'].empty? && item_attributes['display']
62
+ end
63
+ end
64
+
65
+ end
66
+ end
@@ -15,7 +15,7 @@ module Thinreports
15
15
 
16
16
  DEFAULT_FALLBACK_FONTS = %w( Helvetica IPAMincho )
17
17
 
18
- PRAWN_BUINTIN_FONT_ARIASES = {
18
+ PRAWN_BUINTIN_FONT_ALIASES = {
19
19
  'Courier New' => 'Courier',
20
20
  'Times New Roman' => 'Times-Roman'
21
21
  }
@@ -25,17 +25,12 @@ module Thinreports
25
25
  pdf.font_families.update(BUILTIN_FONTS)
26
26
 
27
27
  # Create aliases from the font list provided by Prawn.
28
- PRAWN_BUINTIN_FONT_ARIASES.each do |alias_name, name|
28
+ PRAWN_BUINTIN_FONT_ALIASES.each do |alias_name, name|
29
29
  pdf.font_families[alias_name] = pdf.font_families[name]
30
30
  end
31
31
 
32
32
  # Setup custom fallback fonts
33
- fallback_fonts = []
34
- # For compatible to v0.7 or less
35
- fallback_fonts |= Thinreports.config.generator.pdf.eudc_fonts
36
- fallback_fonts |= Thinreports.config.fallback_fonts
37
- fallback_fonts.uniq!
38
-
33
+ fallback_fonts = Thinreports.config.fallback_fonts.uniq
39
34
  fallback_fonts.map!.with_index do |font, i|
40
35
  unless pdf.font_families.key?(font)
41
36
  install_font "Custom-fallback-font#{i}", font
@@ -4,122 +4,142 @@ module Thinreports
4
4
  module Generator
5
5
 
6
6
  module PDF::Graphics
7
- # @param [Hash] svg_attrs
7
+ # @param [Hash] style
8
8
  # @yield [attrs]
9
9
  # @yieldparam [Hash] attrs
10
10
  # @return [Hash]
11
- def common_graphic_attrs(svg_attrs, &block)
12
- attrs = {stroke: svg_attrs['stroke'],
13
- stroke_width: svg_attrs['stroke-width'],
14
- fill: svg_attrs['fill']}
15
-
16
- # Set 0 to stroke_width if stroke_opacity is '0'.
17
- if svg_attrs['stroke-opacity'] == '0'
18
- attrs[:stroke_width] = 0
19
- end
20
-
21
- # Setting for stroke dash.
22
- if (dash = svg_attrs['stroke-dasharray']) && dash != 'none'
23
- attrs[:stroke_dash] = dash.split(',')
24
- end
25
- block.call(attrs) if block_given?
26
- attrs
11
+ def build_graphic_attributes(style, &block)
12
+ graphic_attributes = {
13
+ stroke: style['border-color'],
14
+ stroke_width: style['border-width'],
15
+ stroke_type: style['border-style'],
16
+ fill: style['fill-color']
17
+ }
18
+ block.call(graphic_attributes) if block_given?
19
+ graphic_attributes
27
20
  end
28
21
 
29
- # @param [Hash] svg_attrs
22
+ # @param [Hash] style
30
23
  # @yield [attrs]
31
24
  # @yieldparam [Hash] attrs
32
25
  # @return [Hash]
33
- def common_text_attrs(svg_attrs, &block)
34
- attrs = {font: default_family_if_missing(svg_attrs['font-family']),
35
- size: svg_attrs['font-size'],
36
- color: svg_attrs['fill'],
37
- align: text_align(svg_attrs['text-anchor']),
38
- styles: font_styles(svg_attrs)}
39
-
40
- # The Letter Spacing Property.
41
- #
42
- # When the version of Layout is
43
- # smaller then 0.6.0:
44
- # Use letter-spacing attribute (normal is none).
45
- # 0.6.0 or more:
46
- # Use kerning attribute (auto is none).
47
- #
48
- spacing = text_letter_spacing(svg_attrs['kerning'] || svg_attrs['letter-spacing'])
49
- attrs[:letter_spacing] = spacing if spacing
26
+ def build_text_attributes(style, &block)
27
+ text_attributes = {
28
+ font: font_family(style['font-family']),
29
+ size: style['font-size'],
30
+ color: style['color'],
31
+ align: text_align(style['text-align']),
32
+ valign: text_valign(style['vertical-align']),
33
+ styles: font_styles(style['font-style']),
34
+ letter_spacing: letter_spacing(style['letter-spacing']),
35
+ line_height: line_height(style['line-height']),
36
+ overflow: text_overflow(style['overflow']),
37
+ word_wrap: word_wrap(style['word-wrap'])
38
+ }
39
+ block.call(text_attributes) if block_given?
40
+ text_attributes
41
+ end
50
42
 
51
- block.call(attrs) if block_given?
52
- attrs
43
+ # @param [Array<String>] font_names
44
+ # @return [String]
45
+ def font_family(font_names)
46
+ font_name = font_names.first
47
+ default_family_if_missing(font_name)
53
48
  end
54
49
 
55
- # @param [Hash] svg_attrs
50
+ # @param [Array<String>] styles
56
51
  # @return [Array<Symbol>]
57
- def font_styles(svg_attrs)
58
- styles = []
59
- styles << :bold if svg_attrs['font-weight'] == 'bold'
60
- styles << :italic if svg_attrs['font-style'] == 'italic'
61
-
62
- if (deco = svg_attrs['text-decoration']) && deco != 'none'
63
- deco = deco.split(' ')
64
- styles << :underline if deco.include?('underline')
65
- styles << :strikethrough if deco.include?('line-through')
52
+ def font_styles(styles)
53
+ styles.map do |font_style|
54
+ case font_style
55
+ when 'bold' then :bold
56
+ when 'italic' then :italic
57
+ when 'underline' then :underline
58
+ when 'linethrough' then :strikethrough
59
+ end
66
60
  end
67
- styles
68
61
  end
69
62
 
70
- # @param [String] space
71
- # @return [String, nil]
72
- def text_letter_spacing(space)
73
- %w( normal auto ).include?(space) ? nil : space
63
+ # @param [Float, "", nil] spacing
64
+ # @return [Float, nil]
65
+ def letter_spacing(spacing)
66
+ blank_value?(spacing) ? nil : spacing
74
67
  end
75
68
 
76
- # @param [String] svg_align
77
- # @return [Symbol]
78
- def text_align(svg_align)
79
- case svg_align
80
- when 'start' then :left
81
- when 'middle' then :center
82
- when 'end' then :right
69
+ # @param ["left", "center", "right", ""] align
70
+ # @return [:left, :center, :right]
71
+ def text_align(align)
72
+ case align
73
+ when 'left' then :left
74
+ when 'center' then :center
75
+ when 'right' then :right
76
+ when '' then :left
83
77
  else :left
84
78
  end
85
79
  end
86
80
 
87
- # @param [String] svg_valign
88
- # @return [Symbol]
89
- def text_valign(svg_valign)
90
- case svg_valign
91
- when 'top' then :top
92
- when 'center' then :center
81
+ # @param ["top", "middle", "bottom", "", nil] valign
82
+ # @return [:top, :center, :bottom]
83
+ def text_valign(valign)
84
+ case valign
85
+ when 'top' then :top
86
+ when 'middle' then :center
93
87
  when 'bottom' then :bottom
88
+ when '' then :top
94
89
  else :top
95
90
  end
96
91
  end
97
92
 
98
- # @param [String] overflow
99
- # @return [Symbol]
93
+ # @param ["truncate", "fit", "expand", "", nil] overflow
94
+ # @return [:truncate, :shrink_to_fit, :expand]
100
95
  def text_overflow(overflow)
101
96
  case overflow
102
97
  when 'truncate' then :truncate
103
- when 'fit' then :shrink_to_fit
104
- when 'expand' then :expand
98
+ when 'fit' then :shrink_to_fit
99
+ when 'expand' then :expand
100
+ when '' then :truncate
105
101
  else :truncate
106
102
  end
107
103
  end
108
104
 
109
- # @param [String] word_wrap
110
- # @return [Symbol]
111
- def text_word_wrap(word_wrap)
105
+ # @param ["break-word", "none", "", nil] word_wrap
106
+ # @return [:break_word, :none]
107
+ def word_wrap(word_wrap)
112
108
  case word_wrap
113
109
  when 'break-word' then :break_word
110
+ when 'none' then :none
114
111
  else :none
115
112
  end
116
113
  end
117
114
 
118
- # @param [String] xlink
119
- # @return [Array<string>] returns ['image/png', '<base64 data>']
120
- def extract_base64_string(xlink)
121
- _, type, data = xlink.match(%r|^data:(image/[a-z]+?);base64,(.+)|).to_a
122
- [type, data]
115
+ # @param [Float, "", nil] height
116
+ # @return [Float, nil]
117
+ def line_height(height)
118
+ blank_value?(height) ? nil : height
119
+ end
120
+
121
+ # @param ["left", "center", "right", ""] position
122
+ # @return [:left, :center, :right]
123
+ def image_position_x(position)
124
+ case position
125
+ when 'left' then :left
126
+ when 'center' then :center
127
+ when 'right' then :right
128
+ when '' then :left
129
+ else :left
130
+ end
131
+ end
132
+
133
+ # @param ["top", "middle", "bottom", ""] position
134
+ # @return [:left, :center, :right]
135
+ def image_position_y(position)
136
+ case position
137
+ when 'top' then :top
138
+ when 'middle' then :center
139
+ when 'bottom' then :bottom
140
+ when '' then :top
141
+ else :top
142
+ end
123
143
  end
124
144
  end
125
145