thinreports 0.9.1 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +41 -0
  3. data/.travis.yml +4 -6
  4. data/CHANGELOG.md +21 -2
  5. data/Gemfile +2 -4
  6. data/README.md +4 -5
  7. data/Rakefile +7 -8
  8. data/examples/character_spacing/character_spacing.rb +1 -1
  9. data/examples/dynamic_image/dynamic_image.rb +3 -3
  10. data/examples/dynamic_style/dynamic_style.rb +1 -1
  11. data/examples/eudc/eudc.rb +1 -1
  12. data/examples/helper.rb +2 -2
  13. data/examples/hidden_shapes/hidden_shapes.rb +1 -1
  14. data/examples/list_events/list_events.rb +1 -1
  15. data/examples/list_manual_generation/list_manual_generation.rb +1 -1
  16. data/examples/list_page_number/list_page_number.rb +1 -1
  17. data/examples/page_number/page_number.rb +1 -1
  18. data/examples/palleted_png/palleted_png.rb +1 -3
  19. data/examples/password_setting/password_setting.rb +1 -1
  20. data/examples/report_callbacks/report_callbacks.rb +1 -1
  21. data/examples/single_line_tblock/single_line_tblock.rb +1 -1
  22. data/examples/tblock_overflow/tblock_overflow.rb +1 -1
  23. data/examples/tblock_styles/tblock_styles.rb +1 -1
  24. data/examples/text_align/text_align.rb +1 -1
  25. data/examples/typeB_page_size/typeB_page_size.rb +1 -1
  26. data/examples/word_wrap/word_wrap.rb +1 -1
  27. data/lib/thinreports.rb +16 -15
  28. data/lib/thinreports/config.rb +2 -24
  29. data/lib/thinreports/core/errors.rb +6 -17
  30. data/lib/thinreports/core/format/base.rb +62 -61
  31. data/lib/thinreports/core/shape.rb +13 -14
  32. data/lib/thinreports/core/shape/base.rb +3 -12
  33. data/lib/thinreports/core/shape/base/interface.rb +35 -32
  34. data/lib/thinreports/core/shape/base/internal.rb +45 -43
  35. data/lib/thinreports/core/shape/basic.rb +12 -12
  36. data/lib/thinreports/core/shape/basic/block_format.rb +8 -6
  37. data/lib/thinreports/core/shape/basic/block_interface.rb +24 -22
  38. data/lib/thinreports/core/shape/basic/block_internal.rb +28 -26
  39. data/lib/thinreports/core/shape/basic/format.rb +11 -9
  40. data/lib/thinreports/core/shape/basic/interface.rb +79 -77
  41. data/lib/thinreports/core/shape/basic/internal.rb +17 -14
  42. data/lib/thinreports/core/shape/image_block.rb +9 -9
  43. data/lib/thinreports/core/shape/image_block/format.rb +7 -5
  44. data/lib/thinreports/core/shape/image_block/interface.rb +15 -13
  45. data/lib/thinreports/core/shape/image_block/internal.rb +10 -8
  46. data/lib/thinreports/core/shape/list.rb +13 -13
  47. data/lib/thinreports/core/shape/list/format.rb +83 -77
  48. data/lib/thinreports/core/shape/list/manager.rb +182 -176
  49. data/lib/thinreports/core/shape/list/page.rb +94 -93
  50. data/lib/thinreports/core/shape/list/page_state.rb +42 -40
  51. data/lib/thinreports/core/shape/list/section_format.rb +28 -26
  52. data/lib/thinreports/core/shape/list/section_interface.rb +42 -40
  53. data/lib/thinreports/core/shape/list/section_internal.rb +21 -19
  54. data/lib/thinreports/core/shape/manager.rb +4 -13
  55. data/lib/thinreports/core/shape/manager/format.rb +21 -19
  56. data/lib/thinreports/core/shape/manager/internal.rb +88 -84
  57. data/lib/thinreports/core/shape/manager/target.rb +93 -95
  58. data/lib/thinreports/core/shape/page_number.rb +9 -9
  59. data/lib/thinreports/core/shape/page_number/format.rb +20 -18
  60. data/lib/thinreports/core/shape/page_number/interface.rb +20 -18
  61. data/lib/thinreports/core/shape/page_number/internal.rb +47 -44
  62. data/lib/thinreports/core/shape/style.rb +5 -14
  63. data/lib/thinreports/core/shape/style/base.rb +121 -120
  64. data/lib/thinreports/core/shape/style/basic.rb +12 -10
  65. data/lib/thinreports/core/shape/style/graphic.rb +35 -33
  66. data/lib/thinreports/core/shape/style/text.rb +113 -107
  67. data/lib/thinreports/core/shape/text.rb +9 -9
  68. data/lib/thinreports/core/shape/text/format.rb +10 -9
  69. data/lib/thinreports/core/shape/text/interface.rb +13 -11
  70. data/lib/thinreports/core/shape/text/internal.rb +14 -12
  71. data/lib/thinreports/core/shape/text_block.rb +10 -10
  72. data/lib/thinreports/core/shape/text_block/format.rb +35 -33
  73. data/lib/thinreports/core/shape/text_block/formatter.rb +26 -24
  74. data/lib/thinreports/core/shape/text_block/formatter/basic.rb +25 -25
  75. data/lib/thinreports/core/shape/text_block/formatter/datetime.rb +15 -12
  76. data/lib/thinreports/core/shape/text_block/formatter/number.rb +45 -39
  77. data/lib/thinreports/core/shape/text_block/formatter/padding.rb +17 -14
  78. data/lib/thinreports/core/shape/text_block/interface.rb +25 -23
  79. data/lib/thinreports/core/shape/text_block/internal.rb +68 -68
  80. data/lib/thinreports/core/utils.rb +4 -3
  81. data/lib/thinreports/generator/pdf.rb +24 -25
  82. data/lib/thinreports/generator/pdf/document.rb +109 -110
  83. data/lib/thinreports/generator/pdf/document/draw_shape.rb +58 -51
  84. data/lib/thinreports/generator/pdf/document/draw_template_items.rb +51 -47
  85. data/lib/thinreports/generator/pdf/document/font.rb +62 -59
  86. data/lib/thinreports/generator/pdf/document/graphics.rb +32 -33
  87. data/lib/thinreports/generator/pdf/document/graphics/attributes.rb +122 -121
  88. data/lib/thinreports/generator/pdf/document/graphics/basic.rb +113 -114
  89. data/lib/thinreports/generator/pdf/document/graphics/image.rb +58 -150
  90. data/lib/thinreports/generator/pdf/document/graphics/text.rb +152 -137
  91. data/lib/thinreports/generator/pdf/document/page.rb +63 -64
  92. data/lib/thinreports/generator/pdf/document/parse_color.rb +37 -37
  93. data/lib/thinreports/generator/pdf/drawer/base.rb +41 -41
  94. data/lib/thinreports/generator/pdf/drawer/list.rb +48 -45
  95. data/lib/thinreports/generator/pdf/drawer/list_section.rb +42 -40
  96. data/lib/thinreports/generator/pdf/drawer/page.rb +101 -96
  97. data/lib/thinreports/generator/pdf/prawn_ext.rb +4 -30
  98. data/lib/thinreports/generator/pdf/prawn_ext/calc_image_dimensions.rb +34 -0
  99. data/lib/thinreports/generator/pdf/prawn_ext/width_of.rb +29 -0
  100. data/lib/thinreports/layout.rb +5 -7
  101. data/lib/thinreports/layout/base.rb +5 -7
  102. data/lib/thinreports/layout/format.rb +18 -13
  103. data/lib/thinreports/layout/legacy_schema.rb +18 -10
  104. data/lib/thinreports/layout/version.rb +22 -25
  105. data/lib/thinreports/report.rb +4 -6
  106. data/lib/thinreports/report/base.rb +24 -51
  107. data/lib/thinreports/report/internal.rb +9 -11
  108. data/lib/thinreports/report/page.rb +5 -6
  109. data/lib/thinreports/version.rb +2 -2
  110. data/test/schema_helper.rb +4 -3
  111. data/test/test_helper.rb +9 -3
  112. data/test/unit/core/format/test_base.rb +1 -1
  113. data/test/unit/core/shape/base/test_internal.rb +1 -1
  114. data/test/unit/core/shape/basic/test_block_format.rb +1 -1
  115. data/test/unit/core/shape/basic/test_block_interface.rb +1 -1
  116. data/test/unit/core/shape/basic/test_block_internal.rb +1 -1
  117. data/test/unit/core/shape/basic/test_format.rb +1 -1
  118. data/test/unit/core/shape/basic/test_interface.rb +1 -1
  119. data/test/unit/core/shape/basic/test_internal.rb +1 -1
  120. data/test/unit/core/shape/image_block/test_interface.rb +1 -1
  121. data/test/unit/core/shape/image_block/test_internal.rb +1 -1
  122. data/test/unit/core/shape/list/test_format.rb +1 -1
  123. data/test/unit/core/shape/list/test_manager.rb +4 -4
  124. data/test/unit/core/shape/list/test_page.rb +1 -1
  125. data/test/unit/core/shape/list/test_page_state.rb +1 -1
  126. data/test/unit/core/shape/list/test_section_format.rb +1 -1
  127. data/test/unit/core/shape/list/test_section_interface.rb +2 -2
  128. data/test/unit/core/shape/list/test_section_internal.rb +1 -1
  129. data/test/unit/core/shape/manager/test_format.rb +2 -2
  130. data/test/unit/core/shape/manager/test_internal.rb +3 -3
  131. data/test/unit/core/shape/manager/test_target.rb +1 -1
  132. data/test/unit/core/shape/page_number/test_format.rb +1 -1
  133. data/test/unit/core/shape/page_number/test_interface.rb +1 -1
  134. data/test/unit/core/shape/page_number/test_internal.rb +1 -1
  135. data/test/unit/core/shape/styles/test_base.rb +1 -1
  136. data/test/unit/core/shape/styles/test_basic.rb +1 -1
  137. data/test/unit/core/shape/styles/test_graphic.rb +1 -1
  138. data/test/unit/core/shape/styles/test_text.rb +1 -1
  139. data/test/unit/core/shape/text/test_format.rb +1 -1
  140. data/test/unit/core/shape/text/test_internal.rb +1 -1
  141. data/test/unit/core/shape/text_block/formatter/test_basic.rb +1 -1
  142. data/test/unit/core/shape/text_block/formatter/test_datetime.rb +1 -1
  143. data/test/unit/core/shape/text_block/formatter/test_number.rb +1 -1
  144. data/test/unit/core/shape/text_block/formatter/test_padding.rb +2 -2
  145. data/test/unit/core/shape/text_block/test_format.rb +1 -1
  146. data/test/unit/core/shape/text_block/test_formatter.rb +1 -1
  147. data/test/unit/core/shape/text_block/test_interface.rb +1 -1
  148. data/test/unit/core/shape/text_block/test_internal.rb +3 -3
  149. data/test/unit/core/test_shape.rb +1 -1
  150. data/test/unit/core/test_utils.rb +2 -2
  151. data/test/unit/generator/pdf/document/graphics/test_attributes.rb +1 -1
  152. data/test/unit/generator/pdf/document/graphics/test_basic.rb +2 -1
  153. data/test/unit/generator/pdf/document/graphics/test_image.rb +27 -131
  154. data/test/unit/generator/pdf/document/graphics/test_text.rb +1 -1
  155. data/test/unit/generator/pdf/document/test_font.rb +2 -2
  156. data/test/unit/generator/pdf/document/test_graphics.rb +1 -1
  157. data/test/unit/generator/pdf/document/test_page.rb +7 -7
  158. data/test/unit/generator/pdf/document/test_parse_color.rb +1 -1
  159. data/test/unit/generator/pdf/prawn_ext/test_calc_image_dimensions.rb +41 -0
  160. data/test/unit/generator/pdf/prawn_ext/test_width_of.rb +22 -0
  161. data/test/unit/generator/pdf/test_document.rb +8 -6
  162. data/test/unit/generator/test_pdf.rb +12 -10
  163. data/test/unit/layout/test_base.rb +1 -1
  164. data/test/unit/layout/test_format.rb +2 -2
  165. data/test/unit/layout/test_legacy_schema.rb +4 -4
  166. data/test/unit/layout/test_version.rb +11 -80
  167. data/test/unit/report/test_base.rb +45 -59
  168. data/test/unit/report/test_internal.rb +1 -1
  169. data/test/unit/test_config.rb +1 -16
  170. data/test/unit/test_layout.rb +1 -1
  171. data/test/unit/test_report.rb +1 -1
  172. data/thinreports.gemspec +11 -14
  173. metadata +42 -103
  174. data/lib/thinreports/generator.rb +0 -29
  175. data/lib/thinreports/generator/base.rb +0 -40
  176. data/lib/thinreports/generator/configuration.rb +0 -31
  177. data/lib/thinreports/report/events.rb +0 -32
  178. data/test/unit/generator/test_base.rb +0 -40
  179. data/test/unit/generator/test_configuration.rb +0 -21
@@ -1,22 +1,25 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape::TextBlock
4
+ module Core
5
+ module Shape
6
+ module TextBlock
7
+ module Formatter
8
+ class Padding < Formatter::Basic
9
+ private
5
10
 
6
- class Formatter::Padding < Formatter::Basic
11
+ def apply_format_to(value)
12
+ value.to_s.send(format.format_padding_rdir? ? :ljust : :rjust,
13
+ format.format_padding_length,
14
+ format.format_padding_char)
15
+ end
7
16
 
8
- private
9
-
10
- def apply_format_to(value)
11
- value.to_s.send(format.format_padding_rdir? ? :ljust : :rjust,
12
- format.format_padding_length,
13
- format.format_padding_char)
14
- end
15
-
16
- def applicable?(_value)
17
- !blank_value?(format.format_padding_char) && format.format_padding_length > 0
17
+ def applicable?(_value)
18
+ !blank_value?(format.format_padding_char) && format.format_padding_length > 0
19
+ end
20
+ end
21
+ end
18
22
  end
19
23
  end
20
-
21
24
  end
22
25
  end
@@ -1,33 +1,35 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape
4
+ module Core
5
+ module Shape
6
+ module TextBlock
7
+ class Interface < Basic::BlockInterface
8
+ internal_delegators :format_enabled?
5
9
 
6
- class TextBlock::Interface < Basic::BlockInterface
7
- internal_delegators :format_enabled?
10
+ # @param [Boolean] enabled
11
+ # @return [self]
12
+ def format_enabled(enabled)
13
+ internal.format_enabled(enabled)
14
+ self
15
+ end
8
16
 
9
- # @param [Boolean] enabled
10
- # @return [self]
11
- def format_enabled(enabled)
12
- internal.format_enabled(enabled)
13
- self
14
- end
15
-
16
- # @param [Object] val
17
- # @param [Hash<Symbol, Object>] style_settings
18
- # @return [self]
19
- def set(val, style_settings = {})
20
- value(val)
21
- styles(style_settings) #=> self
22
- end
17
+ # @param [Object] val
18
+ # @param [Hash<Symbol, Object>] style_settings
19
+ # @return [self]
20
+ def set(val, style_settings = {})
21
+ value(val)
22
+ styles(style_settings) #=> self
23
+ end
23
24
 
24
- private
25
+ private
25
26
 
26
- # @see Thinreports::Core::Shape::Base::Interface#init_internal
27
- def init_internal(parent, format)
28
- TextBlock::Internal.new(parent, format)
27
+ # @see Thinreports::Core::Shape::Base::Interface#init_internal
28
+ def init_internal(parent, format)
29
+ TextBlock::Internal.new(parent, format)
30
+ end
31
+ end
29
32
  end
30
33
  end
31
-
32
34
  end
33
35
  end
@@ -1,75 +1,75 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
- module Core::Shape
5
-
6
- class TextBlock::Internal < Basic::BlockInternal
7
- format_delegators :multiple?
8
-
9
- def initialize(*args)
10
- super(*args)
11
-
12
- @reference = nil
13
- @formatter = nil
14
-
15
- @style = Style::Text.new(format)
16
- @style.accessible_styles.delete(:valign) unless multiple?
17
- end
18
-
19
- def style
20
- @style
21
- end
22
-
23
- def read_value
24
- if format.has_reference?
25
- @reference ||= parent.item(format.ref_id)
26
- @reference.value
27
- else
28
- super
29
- end
30
- end
31
-
32
- def write_value(val)
33
- if format.has_reference?
34
- warn 'The set value was not saved, ' +
35
- "Because '#{format.id}' has reference to '#{format.ref_id}'."
36
- else
37
- super
38
- end
39
- end
40
-
41
- def real_value
42
- if format_enabled?
43
- formatter.apply(read_value)
44
- else
45
- super
46
- end
47
- end
48
-
49
- def format_enabled(enabled)
50
- states[:format_enabled] = enabled
51
- end
52
-
53
- def format_enabled?
54
- return false if multiple?
55
-
56
- if states.key?(:format_enabled)
57
- states[:format_enabled]
58
- else
59
- !blank_value?(format.format_base) || format.has_format?
4
+ module Core
5
+ module Shape
6
+ module TextBlock
7
+ class Internal < Basic::BlockInternal
8
+ format_delegators :multiple?
9
+
10
+ attr_reader :style
11
+
12
+ def initialize(*args)
13
+ super(*args)
14
+
15
+ @reference = nil
16
+ @formatter = nil
17
+
18
+ @style = Style::Text.new(format)
19
+ @style.accessible_styles.delete(:valign) unless multiple?
20
+ end
21
+
22
+ def read_value
23
+ if format.has_reference?
24
+ @reference ||= parent.item(format.ref_id)
25
+ @reference.value
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ def write_value(val)
32
+ if format.has_reference?
33
+ warn 'The set value was not saved, ' \
34
+ "Because '#{format.id}' has reference to '#{format.ref_id}'."
35
+ else
36
+ super
37
+ end
38
+ end
39
+
40
+ def real_value
41
+ if format_enabled?
42
+ formatter.apply(read_value)
43
+ else
44
+ super
45
+ end
46
+ end
47
+
48
+ def format_enabled(enabled)
49
+ states[:format_enabled] = enabled
50
+ end
51
+
52
+ def format_enabled?
53
+ return false if multiple?
54
+
55
+ if states.key?(:format_enabled)
56
+ states[:format_enabled]
57
+ else
58
+ !blank_value?(format.format_base) || format.has_format?
59
+ end
60
+ end
61
+
62
+ def type_of?(type_name)
63
+ type_name == TextBlock::TYPE_NAME || super
64
+ end
65
+
66
+ private
67
+
68
+ def formatter
69
+ @formatter ||= TextBlock::Formatter.setup(format)
70
+ end
60
71
  end
61
72
  end
62
-
63
- def type_of?(type_name)
64
- type_name == TextBlock::TYPE_NAME || super
65
- end
66
-
67
- private
68
-
69
- def formatter
70
- @formatter ||= TextBlock::Formatter.setup(format)
71
- end
72
73
  end
73
-
74
74
  end
75
75
  end
@@ -1,4 +1,4 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  module Thinreports
4
4
  module Utils
@@ -6,12 +6,13 @@ module Thinreports
6
6
  klass.extend self
7
7
  end
8
8
 
9
+ # rubocop:disable Style/RescueModifier
9
10
  def deep_copy(src)
10
11
  case src
11
12
  when Hash
12
- src.inject({}) {|h, (k, v)| h[k] = (v.dup rescue v); h }
13
+ src.each_with_object({}) { |(k, v), h| h[k] = v.dup rescue v }
13
14
  when Array
14
- src.map {|a| a.dup rescue a }
15
+ src.map { |a| a.dup rescue a }
15
16
  else
16
17
  raise ArgumentError
17
18
  end
@@ -1,37 +1,37 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- begin
4
- gem 'prawn', '1.3.0'
5
- require 'prawn'
6
- rescue LoadError
7
- puts 'Thinreports requires Prawn = 1.3.0. ' +
8
- 'Please `gem install prawn -v 1.3.0` and try again.'
9
- end
3
+ require 'prawn'
10
4
 
11
5
  module Thinreports
12
6
  module Generator
7
+ class PDF
8
+ # @return [Thinreports::Report::Base]
9
+ attr_reader :report
13
10
 
14
- class PDF < Base
15
- # @param report (see Thinreports::Generator::Base#initialize)
16
- # @param [Hash] options
17
- # @option options [Hash] :security (nil)
18
- # See Prawn::Document#encrypt_document
19
- def initialize(report, options)
20
- super
11
+ # @param [Thinreports::Report::Base] report
12
+ # @param [Hash] security (nil)
13
+ def initialize(report, security: nil)
14
+ report.finalize
21
15
 
16
+ @report = report.internal
22
17
  title = default_layout ? default_layout.format.report_title : nil
23
18
 
24
- @document = Document.new(options, Title: title)
19
+ @document = Document.new(title: title, security: security)
25
20
  @drawers = {}
26
21
  end
27
22
 
28
- # @see Thinreports::Generator::Base#generate
23
+ # @param [String, nil] filename
24
+ # @return [String, nil]
29
25
  def generate(filename = nil)
30
26
  draw_report
31
27
  filename ? @document.render_file(filename) : @document.render
32
28
  end
33
29
 
34
- private
30
+ def default_layout
31
+ report.default_layout
32
+ end
33
+
34
+ private
35
35
 
36
36
  def draw_report
37
37
  report.pages.each do |page|
@@ -52,13 +52,12 @@ module Thinreports
52
52
  @drawers[format.identifier] ||= Drawer::Page.new(@document, format)
53
53
  end
54
54
  end
55
-
56
55
  end
57
56
  end
58
57
 
59
- require 'thinreports/generator/pdf/prawn_ext'
60
- require 'thinreports/generator/pdf/document'
61
- require 'thinreports/generator/pdf/drawer/base'
62
- require 'thinreports/generator/pdf/drawer/page'
63
- require 'thinreports/generator/pdf/drawer/list'
64
- require 'thinreports/generator/pdf/drawer/list_section'
58
+ require_relative 'pdf/prawn_ext'
59
+ require_relative 'pdf/document'
60
+ require_relative 'pdf/drawer/base'
61
+ require_relative 'pdf/drawer/page'
62
+ require_relative 'pdf/drawer/list'
63
+ require_relative 'pdf/drawer/list_section'
@@ -1,131 +1,130 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
- require 'thinreports/generator/pdf/document/font'
4
- require 'thinreports/generator/pdf/document/parse_color'
5
- require 'thinreports/generator/pdf/document/graphics'
6
- require 'thinreports/generator/pdf/document/draw_shape'
7
- require 'thinreports/generator/pdf/document/draw_template_items'
8
- require 'thinreports/generator/pdf/document/page'
3
+ require_relative 'document/font'
4
+ require_relative 'document/parse_color'
5
+ require_relative 'document/graphics'
6
+ require_relative 'document/draw_shape'
7
+ require_relative 'document/draw_template_items'
8
+ require_relative 'document/page'
9
9
 
10
10
  module Thinreports
11
11
  module Generator
12
-
13
- class PDF::Document
14
- include Utils
15
-
16
- include PDF::Font
17
- include PDF::ParseColor
18
- include PDF::Graphics
19
- include PDF::DrawShape
20
- include PDF::DrawTemplateItems
21
- include PDF::Page
22
-
23
- # @return [Prawn::Document]
24
- attr_reader :pdf
25
-
26
- # @param options (see Thinreports::Generator::PDF#initialize)
27
- # @param [Hash] metadata
28
- # @option metadata [String] :Title
29
- def initialize(options = {}, metadata = {})
30
- @pdf = Prawn::Document.new(
31
- skip_page_creation: true,
32
- margin: [0, 0],
33
- info: {CreationDate: Time.now,
34
- Creator: 'Thinreports Generator for Ruby ' +
35
- Thinreports::VERSION}.merge(metadata)
36
- )
37
- # Setup to Prawn::Document.
38
- setup_fonts
39
- setup_custom_graphic_states
40
-
41
- # Encrypts the document.
42
- if options[:security]
43
- @pdf.encrypt_document(options[:security])
12
+ class PDF
13
+ class Document
14
+ include Utils
15
+
16
+ include PDF::Font
17
+ include PDF::ParseColor
18
+ include PDF::Graphics
19
+ include PDF::DrawShape
20
+ include PDF::DrawTemplateItems
21
+ include PDF::Page
22
+
23
+ # @return [Prawn::Document]
24
+ attr_reader :pdf
25
+
26
+ # @param [String] title (nil)
27
+ # @param [Hash] security (nil)
28
+ def initialize(title: nil, security: nil)
29
+ @pdf = Prawn::Document.new(
30
+ skip_page_creation: true,
31
+ margin: [0, 0],
32
+ info: {
33
+ CreationDate: Time.now,
34
+ Creator: 'Thinreports Generator for Ruby ' + Thinreports::VERSION,
35
+ Title: title
36
+ }
37
+ )
38
+ # Setup to Prawn::Document.
39
+ setup_fonts
40
+ setup_custom_graphic_states
41
+
42
+ # Encrypts the document.
43
+ @pdf.encrypt_document(security) if security
44
44
  end
45
- end
46
45
 
47
- # Delegate to Prawn::Document#render
48
- # @see Prawn::Document#render
49
- def render
50
- result = pdf.render
51
- finalize
52
- result
53
- end
46
+ # Delegate to Prawn::Document#render
47
+ # @see Prawn::Document#render
48
+ def render
49
+ result = pdf.render
50
+ finalize
51
+ result
52
+ end
54
53
 
55
- # Delegate to Prawn::Document#render_file
56
- # @see Prawn::Document#render_file
57
- def render_file(*args)
58
- finalize
59
- pdf.render_file(*args)
60
- end
54
+ # Delegate to Prawn::Document#render_file
55
+ # @see Prawn::Document#render_file
56
+ def render_file(*args)
57
+ finalize
58
+ pdf.render_file(*args)
59
+ end
61
60
 
62
- # @param [Numeric, String] x
63
- # @param [Numeric, String] y
64
- def translate(x, y, &block)
65
- x, y = rpos(x, y)
66
- pdf.translate(x, y, &block)
67
- end
61
+ # @param [Numeric, String] x
62
+ # @param [Numeric, String] y
63
+ def translate(x, y, &block)
64
+ x, y = rpos(x, y)
65
+ pdf.translate(x, y, &block)
66
+ end
68
67
 
69
- # @param [String] stamp_id
70
- # @param [Array<Numeric>] at (nil)
71
- def stamp(stamp_id, at = nil)
72
- unless at.nil?
73
- pdf.stamp_at(stamp_id, rpos(*at))
74
- else
75
- pdf.stamp(stamp_id)
68
+ # @param [String] stamp_id
69
+ # @param [Array<Numeric>] at (nil)
70
+ def stamp(stamp_id, at = nil)
71
+ if at.nil?
72
+ pdf.stamp(stamp_id)
73
+ else
74
+ pdf.stamp_at(stamp_id, rpos(*at))
75
+ end
76
76
  end
77
- end
78
77
 
79
- # Delegate to Prawn::Document#create_stamp
80
- # @param [String] id
81
- # @see Prawn::Document#create_stamp
82
- def create_stamp(id, &block)
83
- pdf.create_stamp(id, &block)
84
- end
78
+ # Delegate to Prawn::Document#create_stamp
79
+ # @param [String] id
80
+ # @see Prawn::Document#create_stamp
81
+ def create_stamp(id, &block)
82
+ pdf.create_stamp(id, &block)
83
+ end
85
84
 
86
- # @see #pdf
87
- def internal
88
- @pdf
89
- end
85
+ # @see #pdf
86
+ def internal
87
+ @pdf
88
+ end
90
89
 
91
- private
90
+ private
92
91
 
93
- def finalize
94
- clean_temp_images
95
- end
92
+ def finalize
93
+ clean_temp_images
94
+ end
96
95
 
97
- # @param [Array<String, Numeric>] values
98
- # @return [Numeric, Array<Numeric>, nil]
99
- def s2f(*values)
100
- return nil if values.empty?
101
-
102
- if values.size == 1
103
- value = values.first
104
- return nil unless value
105
- value.is_a?(::Numeric) ? value : value.to_f
106
- else
107
- values.map {|v| s2f(v) }
96
+ # @param [Array<String, Numeric>] values
97
+ # @return [Numeric, Array<Numeric>, nil]
98
+ def s2f(*values)
99
+ return nil if values.empty?
100
+
101
+ if values.size == 1
102
+ value = values.first
103
+ return nil unless value
104
+ value.is_a?(::Numeric) ? value : value.to_f
105
+ else
106
+ values.map { |v| s2f(v) }
107
+ end
108
108
  end
109
- end
110
109
 
111
- # @param [Numeric, String] x
112
- # @param [Numeric, String] y
113
- # @return [Array<Float>]
114
- def map_to_upper_left_relative_position(x, y)
115
- x, y = s2f(x, y)
116
- [x, -y]
117
- end
118
- alias_method :rpos, :map_to_upper_left_relative_position
119
-
120
- # @param [Numeric, String] x
121
- # @param [Numeric, String] y
122
- # @return [Array<Float>]
123
- def map_to_upper_left_position(x, y)
124
- x, y = s2f(x, y)
125
- [x, pdf.bounds.height - y]
110
+ # @param [Numeric, String] x
111
+ # @param [Numeric, String] y
112
+ # @return [Array<Float>]
113
+ def map_to_upper_left_relative_position(x, y)
114
+ x, y = s2f(x, y)
115
+ [x, -y]
116
+ end
117
+ alias rpos map_to_upper_left_relative_position
118
+
119
+ # @param [Numeric, String] x
120
+ # @param [Numeric, String] y
121
+ # @return [Array<Float>]
122
+ def map_to_upper_left_position(x, y)
123
+ x, y = s2f(x, y)
124
+ [x, pdf.bounds.height - y]
125
+ end
126
+ alias pos map_to_upper_left_position
126
127
  end
127
- alias_method :pos, :map_to_upper_left_position
128
128
  end
129
-
130
129
  end
131
130
  end