thinreports 0.12.0 → 0.13.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +11 -0
- data/.github/workflows/test.yml +31 -40
- data/.gitignore +1 -1
- data/CHANGELOG.md +25 -1
- data/Gemfile +11 -0
- data/{MIT-LICENSE → LICENSE.txt} +2 -0
- data/README.md +50 -66
- data/Rakefile +17 -11
- data/lib/thinreports/basic_report/core/errors.rb +62 -0
- data/lib/thinreports/basic_report/core/format/base.rb +91 -0
- data/lib/thinreports/basic_report/core/shape/base/interface.rb +42 -0
- data/lib/thinreports/basic_report/core/shape/base/internal.rb +53 -0
- data/lib/thinreports/basic_report/core/shape/basic/block_format.rb +15 -0
- data/lib/thinreports/basic_report/core/shape/basic/block_interface.rb +34 -0
- data/lib/thinreports/basic_report/core/shape/basic/block_internal.rb +36 -0
- data/lib/thinreports/basic_report/core/shape/basic/format.rb +24 -0
- data/lib/thinreports/basic_report/core/shape/basic/interface.rb +87 -0
- data/lib/thinreports/basic_report/core/shape/basic/internal.rb +28 -0
- data/lib/thinreports/{core → basic_report/core}/shape/basic.rb +6 -4
- data/lib/thinreports/basic_report/core/shape/image_block/format.rb +14 -0
- data/lib/thinreports/basic_report/core/shape/image_block/interface.rb +25 -0
- data/lib/thinreports/basic_report/core/shape/image_block/internal.rb +19 -0
- data/lib/thinreports/{core → basic_report/core}/shape/image_block.rb +6 -4
- data/lib/thinreports/basic_report/core/shape/list/format.rb +92 -0
- data/lib/thinreports/basic_report/core/shape/list/manager.rb +231 -0
- data/lib/thinreports/basic_report/core/shape/list/page.rb +112 -0
- data/lib/thinreports/basic_report/core/shape/list/page_state.rb +50 -0
- data/lib/thinreports/basic_report/core/shape/list/section_format.rb +37 -0
- data/lib/thinreports/basic_report/core/shape/list/section_interface.rb +51 -0
- data/lib/thinreports/basic_report/core/shape/list/section_internal.rb +33 -0
- data/lib/thinreports/{core → basic_report/core}/shape/list.rb +6 -4
- data/lib/thinreports/basic_report/core/shape/manager/format.rb +33 -0
- data/lib/thinreports/basic_report/core/shape/manager/internal.rb +109 -0
- data/lib/thinreports/basic_report/core/shape/manager/target.rb +114 -0
- data/lib/thinreports/basic_report/core/shape/page_number/format.rb +32 -0
- data/lib/thinreports/basic_report/core/shape/page_number/interface.rb +31 -0
- data/lib/thinreports/basic_report/core/shape/page_number/internal.rb +57 -0
- data/lib/thinreports/{core → basic_report/core}/shape/page_number.rb +6 -4
- data/lib/thinreports/basic_report/core/shape/stack_view/format.rb +29 -0
- data/lib/thinreports/basic_report/core/shape/stack_view/interface.rb +19 -0
- data/lib/thinreports/basic_report/core/shape/stack_view/internal.rb +24 -0
- data/lib/thinreports/basic_report/core/shape/stack_view/row_format.rb +41 -0
- data/lib/thinreports/{core → basic_report/core}/shape/stack_view.rb +6 -4
- data/lib/thinreports/basic_report/core/shape/style/base.rb +149 -0
- data/lib/thinreports/basic_report/core/shape/style/basic.rb +24 -0
- data/lib/thinreports/basic_report/core/shape/style/graphic.rb +43 -0
- data/lib/thinreports/basic_report/core/shape/style/text.rb +122 -0
- data/lib/thinreports/basic_report/core/shape/text/format.rb +17 -0
- data/lib/thinreports/basic_report/core/shape/text/interface.rb +20 -0
- data/lib/thinreports/basic_report/core/shape/text/internal.rb +24 -0
- data/lib/thinreports/{core → basic_report/core}/shape/text.rb +6 -4
- data/lib/thinreports/basic_report/core/shape/text_block/format.rb +43 -0
- data/lib/thinreports/basic_report/core/shape/text_block/formatter/basic.rb +41 -0
- data/lib/thinreports/basic_report/core/shape/text_block/formatter/datetime.rb +25 -0
- data/lib/thinreports/basic_report/core/shape/text_block/formatter/number.rb +69 -0
- data/lib/thinreports/basic_report/core/shape/text_block/formatter/padding.rb +27 -0
- data/lib/thinreports/basic_report/core/shape/text_block/formatter.rb +38 -0
- data/lib/thinreports/basic_report/core/shape/text_block/interface.rb +37 -0
- data/lib/thinreports/basic_report/core/shape/text_block/internal.rb +75 -0
- data/lib/thinreports/{core → basic_report/core}/shape/text_block.rb +6 -4
- data/lib/thinreports/basic_report/core/shape.rb +44 -0
- data/lib/thinreports/basic_report/core/utils.rb +47 -0
- data/lib/thinreports/basic_report/generator/pdf/document/draw_shape.rb +108 -0
- data/lib/thinreports/basic_report/generator/pdf/document/draw_template_items.rb +71 -0
- data/lib/thinreports/basic_report/generator/pdf/document/font.rb +90 -0
- data/lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb +149 -0
- data/lib/thinreports/basic_report/generator/pdf/document/graphics/basic.rb +137 -0
- data/lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb +99 -0
- data/lib/thinreports/basic_report/generator/pdf/document/graphics/text.rb +180 -0
- data/lib/thinreports/basic_report/generator/pdf/document/graphics.rb +42 -0
- data/lib/thinreports/basic_report/generator/pdf/document/page.rb +96 -0
- data/lib/thinreports/basic_report/generator/pdf/document/parse_color.rb +51 -0
- data/lib/thinreports/basic_report/generator/pdf/document.rb +132 -0
- data/lib/thinreports/basic_report/generator/pdf/drawer/base.rb +50 -0
- data/lib/thinreports/basic_report/generator/pdf/drawer/list.rb +65 -0
- data/lib/thinreports/basic_report/generator/pdf/drawer/list_section.rb +51 -0
- data/lib/thinreports/basic_report/generator/pdf/drawer/page.rb +127 -0
- data/lib/thinreports/basic_report/generator/pdf/prawn_ext/calc_image_dimensions.rb +34 -0
- data/lib/thinreports/basic_report/generator/pdf/prawn_ext/width_of.rb +31 -0
- data/lib/thinreports/basic_report/generator/pdf.rb +66 -0
- data/lib/thinreports/basic_report/layout/base.rb +48 -0
- data/lib/thinreports/basic_report/layout/format.rb +67 -0
- data/lib/thinreports/basic_report/layout/legacy_schema.rb +383 -0
- data/lib/thinreports/basic_report/layout/version.rb +41 -0
- data/lib/thinreports/{layout.rb → basic_report/layout.rb} +6 -4
- data/lib/thinreports/basic_report/report/base.rb +173 -0
- data/lib/thinreports/basic_report/report/internal.rb +107 -0
- data/lib/thinreports/basic_report/report/page.rb +97 -0
- data/lib/thinreports/basic_report/report.rb +26 -0
- data/lib/thinreports/basic_report.rb +15 -0
- data/lib/thinreports/config.rb +1 -1
- data/lib/thinreports/section_report/builder/stack_view_builder.rb +0 -2
- data/lib/thinreports/section_report.rb +12 -0
- data/lib/thinreports/version.rb +1 -1
- data/lib/thinreports.rb +2 -13
- data/thinreports.gemspec +3 -7
- metadata +99 -157
- data/Dockerfile +0 -12
- data/gemfiles/prawn-2.2.gemfile +0 -5
- data/gemfiles/prawn-2.3.gemfile +0 -5
- data/gemfiles/prawn-2.4.gemfile +0 -5
- data/lib/thinreports/core/errors.rb +0 -60
- data/lib/thinreports/core/format/base.rb +0 -89
- data/lib/thinreports/core/shape/base/interface.rb +0 -40
- data/lib/thinreports/core/shape/base/internal.rb +0 -51
- data/lib/thinreports/core/shape/basic/block_format.rb +0 -13
- data/lib/thinreports/core/shape/basic/block_interface.rb +0 -32
- data/lib/thinreports/core/shape/basic/block_internal.rb +0 -34
- data/lib/thinreports/core/shape/basic/format.rb +0 -22
- data/lib/thinreports/core/shape/basic/interface.rb +0 -85
- data/lib/thinreports/core/shape/basic/internal.rb +0 -26
- data/lib/thinreports/core/shape/image_block/format.rb +0 -12
- data/lib/thinreports/core/shape/image_block/interface.rb +0 -23
- data/lib/thinreports/core/shape/image_block/internal.rb +0 -17
- data/lib/thinreports/core/shape/list/format.rb +0 -90
- data/lib/thinreports/core/shape/list/manager.rb +0 -229
- data/lib/thinreports/core/shape/list/page.rb +0 -110
- data/lib/thinreports/core/shape/list/page_state.rb +0 -48
- data/lib/thinreports/core/shape/list/section_format.rb +0 -35
- data/lib/thinreports/core/shape/list/section_interface.rb +0 -49
- data/lib/thinreports/core/shape/list/section_internal.rb +0 -31
- data/lib/thinreports/core/shape/manager/format.rb +0 -31
- data/lib/thinreports/core/shape/manager/internal.rb +0 -107
- data/lib/thinreports/core/shape/manager/target.rb +0 -112
- data/lib/thinreports/core/shape/page_number/format.rb +0 -30
- data/lib/thinreports/core/shape/page_number/interface.rb +0 -29
- data/lib/thinreports/core/shape/page_number/internal.rb +0 -55
- data/lib/thinreports/core/shape/stack_view/format.rb +0 -27
- data/lib/thinreports/core/shape/stack_view/interface.rb +0 -17
- data/lib/thinreports/core/shape/stack_view/internal.rb +0 -22
- data/lib/thinreports/core/shape/stack_view/row_format.rb +0 -39
- data/lib/thinreports/core/shape/style/base.rb +0 -146
- data/lib/thinreports/core/shape/style/basic.rb +0 -22
- data/lib/thinreports/core/shape/style/graphic.rb +0 -41
- data/lib/thinreports/core/shape/style/text.rb +0 -120
- data/lib/thinreports/core/shape/text/format.rb +0 -15
- data/lib/thinreports/core/shape/text/interface.rb +0 -18
- data/lib/thinreports/core/shape/text/internal.rb +0 -22
- data/lib/thinreports/core/shape/text_block/format.rb +0 -41
- data/lib/thinreports/core/shape/text_block/formatter/basic.rb +0 -39
- data/lib/thinreports/core/shape/text_block/formatter/datetime.rb +0 -23
- data/lib/thinreports/core/shape/text_block/formatter/number.rb +0 -67
- data/lib/thinreports/core/shape/text_block/formatter/padding.rb +0 -25
- data/lib/thinreports/core/shape/text_block/formatter.rb +0 -34
- data/lib/thinreports/core/shape/text_block/interface.rb +0 -35
- data/lib/thinreports/core/shape/text_block/internal.rb +0 -73
- data/lib/thinreports/core/shape.rb +0 -42
- data/lib/thinreports/core/utils.rb +0 -45
- data/lib/thinreports/generate.rb +0 -11
- data/lib/thinreports/generator/pdf/document/draw_shape.rb +0 -106
- data/lib/thinreports/generator/pdf/document/draw_template_items.rb +0 -69
- data/lib/thinreports/generator/pdf/document/font.rb +0 -88
- data/lib/thinreports/generator/pdf/document/graphics/attributes.rb +0 -147
- data/lib/thinreports/generator/pdf/document/graphics/basic.rb +0 -135
- data/lib/thinreports/generator/pdf/document/graphics/image.rb +0 -98
- data/lib/thinreports/generator/pdf/document/graphics/text.rb +0 -178
- data/lib/thinreports/generator/pdf/document/graphics.rb +0 -40
- data/lib/thinreports/generator/pdf/document/page.rb +0 -94
- data/lib/thinreports/generator/pdf/document/parse_color.rb +0 -49
- data/lib/thinreports/generator/pdf/document.rb +0 -130
- data/lib/thinreports/generator/pdf/drawer/base.rb +0 -48
- data/lib/thinreports/generator/pdf/drawer/list.rb +0 -63
- data/lib/thinreports/generator/pdf/drawer/list_section.rb +0 -49
- data/lib/thinreports/generator/pdf/drawer/page.rb +0 -125
- data/lib/thinreports/generator/pdf/prawn_ext/calc_image_dimensions.rb +0 -32
- data/lib/thinreports/generator/pdf/prawn_ext/width_of.rb +0 -29
- data/lib/thinreports/generator/pdf.rb +0 -63
- data/lib/thinreports/layout/base.rb +0 -46
- data/lib/thinreports/layout/format.rb +0 -65
- data/lib/thinreports/layout/legacy_schema.rb +0 -381
- data/lib/thinreports/layout/version.rb +0 -39
- data/lib/thinreports/report/base.rb +0 -169
- data/lib/thinreports/report/internal.rb +0 -105
- data/lib/thinreports/report/page.rb +0 -95
- data/lib/thinreports/report.rb +0 -24
- data/lib/thinreports/section_report/pdf/renderer/headers_renderer.rb +0 -24
- /data/lib/thinreports/{core → basic_report/core}/shape/base.rb +0 -0
- /data/lib/thinreports/{core → basic_report/core}/shape/manager.rb +0 -0
- /data/lib/thinreports/{core → basic_report/core}/shape/style.rb +0 -0
- /data/lib/thinreports/{generator → basic_report/generator}/pdf/prawn_ext.rb +0 -0
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bigdecimal'
|
4
|
+
|
5
|
+
module Thinreports
|
6
|
+
module BasicReport
|
7
|
+
module Core
|
8
|
+
module Shape
|
9
|
+
module TextBlock
|
10
|
+
module Formatter
|
11
|
+
class Number < Formatter::Basic
|
12
|
+
private
|
13
|
+
|
14
|
+
def apply_format_to(value)
|
15
|
+
precision = format.format_number_precision
|
16
|
+
delimiter = format.format_number_delimiter
|
17
|
+
|
18
|
+
if_applicable value do |val|
|
19
|
+
val = number_with_precision(val, precision) unless blank_value?(precision)
|
20
|
+
val = number_with_delimiter(val, delimiter) unless blank_value?(delimiter)
|
21
|
+
val
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def if_applicable(value, &block)
|
26
|
+
normalized_value = normalize(value)
|
27
|
+
normalized_value.nil? ? value : block.call(normalized_value)
|
28
|
+
end
|
29
|
+
|
30
|
+
def normalize(value)
|
31
|
+
if value.is_a?(String)
|
32
|
+
convert_to_integer(value) || convert_to_float(value)
|
33
|
+
else
|
34
|
+
value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def number_with_delimiter(value, delimiter = ',')
|
39
|
+
value_int, value_float = value.to_s.split('.')
|
40
|
+
|
41
|
+
[
|
42
|
+
value_int.gsub(/(\d)(?=(\d{3})+(?!\d))/) { "#{$1}#{delimiter}" },
|
43
|
+
value_float
|
44
|
+
].compact.join('.')
|
45
|
+
end
|
46
|
+
|
47
|
+
def number_with_precision(value, precision = 3)
|
48
|
+
value = BigDecimal(value.to_s).round(precision)
|
49
|
+
sprintf("%.#{precision}f", value)
|
50
|
+
end
|
51
|
+
|
52
|
+
def convert_to_integer(value)
|
53
|
+
Integer(value)
|
54
|
+
rescue ArgumentError
|
55
|
+
nil
|
56
|
+
end
|
57
|
+
|
58
|
+
def convert_to_float(value)
|
59
|
+
Float(value)
|
60
|
+
rescue ArgumentError
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thinreports
|
4
|
+
module BasicReport
|
5
|
+
module Core
|
6
|
+
module Shape
|
7
|
+
module TextBlock
|
8
|
+
module Formatter
|
9
|
+
class Padding < Formatter::Basic
|
10
|
+
private
|
11
|
+
|
12
|
+
def apply_format_to(value)
|
13
|
+
value.to_s.send(format.format_padding_rdir? ? :ljust : :rjust,
|
14
|
+
format.format_padding_length,
|
15
|
+
format.format_padding_char)
|
16
|
+
end
|
17
|
+
|
18
|
+
def applicable?(_value)
|
19
|
+
!blank_value?(format.format_padding_char) && format.format_padding_length > 0
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thinreports
|
4
|
+
module BasicReport
|
5
|
+
module Core
|
6
|
+
module Shape
|
7
|
+
module TextBlock
|
8
|
+
module Formatter
|
9
|
+
include Utils
|
10
|
+
|
11
|
+
# @param [Thinreports::BasicReport::Core::Shape::TextBlock::Format] format
|
12
|
+
# @return [Thinreports::BasicReport::Core::Shape::TextBlock::Formatter::Base]
|
13
|
+
def self.setup(format)
|
14
|
+
klass =
|
15
|
+
if blank_value?(format.format_type)
|
16
|
+
Basic
|
17
|
+
else
|
18
|
+
case format.format_type
|
19
|
+
when 'number' then Number
|
20
|
+
when 'datetime' then Datetime
|
21
|
+
when 'padding' then Padding
|
22
|
+
else
|
23
|
+
raise Thinreports::BasicReport::Errors::UnknownFormatterType
|
24
|
+
end
|
25
|
+
end
|
26
|
+
klass.new(format)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
require_relative 'formatter/basic'
|
36
|
+
require_relative 'formatter/datetime'
|
37
|
+
require_relative 'formatter/padding'
|
38
|
+
require_relative 'formatter/number'
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thinreports
|
4
|
+
module BasicReport
|
5
|
+
module Core
|
6
|
+
module Shape
|
7
|
+
module TextBlock
|
8
|
+
class Interface < Basic::BlockInterface
|
9
|
+
internal_delegators :format_enabled?
|
10
|
+
|
11
|
+
# @param [Boolean] enabled
|
12
|
+
# @return [self]
|
13
|
+
def format_enabled(enabled)
|
14
|
+
internal.format_enabled(enabled)
|
15
|
+
self
|
16
|
+
end
|
17
|
+
|
18
|
+
# @param [Object] val
|
19
|
+
# @param [Hash<Symbol, Object>] style_settings
|
20
|
+
# @return [self]
|
21
|
+
def set(val, style_settings = {})
|
22
|
+
value(val)
|
23
|
+
styles(style_settings) #=> self
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
# @see Thinreports::BasicReport::Core::Shape::Base::Interface#init_internal
|
29
|
+
def init_internal(parent, format)
|
30
|
+
TextBlock::Internal.new(parent, format)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thinreports
|
4
|
+
module BasicReport
|
5
|
+
module Core
|
6
|
+
module Shape
|
7
|
+
module TextBlock
|
8
|
+
class Internal < Basic::BlockInternal
|
9
|
+
format_delegators :multiple?
|
10
|
+
|
11
|
+
attr_reader :style
|
12
|
+
|
13
|
+
def initialize(*args)
|
14
|
+
super(*args)
|
15
|
+
|
16
|
+
@reference = nil
|
17
|
+
@formatter = nil
|
18
|
+
|
19
|
+
@style = Style::Text.new(format)
|
20
|
+
@style.accessible_styles.delete(:valign) unless multiple?
|
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
|
+
if states.key?(:format_enabled)
|
55
|
+
states[:format_enabled]
|
56
|
+
else
|
57
|
+
!blank_value?(format.format_base) || format.has_format?
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def type_of?(type_name)
|
62
|
+
type_name == TextBlock::TYPE_NAME || super
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def formatter
|
68
|
+
@formatter ||= TextBlock::Formatter.setup(format)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Thinreports
|
4
|
-
module
|
5
|
-
module
|
6
|
-
module
|
7
|
-
|
4
|
+
module BasicReport
|
5
|
+
module Core
|
6
|
+
module Shape
|
7
|
+
module TextBlock
|
8
|
+
TYPE_NAME = 'text-block'
|
9
|
+
end
|
8
10
|
end
|
9
11
|
end
|
10
12
|
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thinreports
|
4
|
+
module BasicReport
|
5
|
+
module Core
|
6
|
+
module Shape
|
7
|
+
def Interface(parent, format)
|
8
|
+
find_by_type(format.type)::Interface.new(parent, format)
|
9
|
+
end
|
10
|
+
|
11
|
+
def Format(type)
|
12
|
+
find_by_type(type)::Format
|
13
|
+
end
|
14
|
+
|
15
|
+
module_function :Interface, :Format
|
16
|
+
|
17
|
+
def self.find_by_type(type)
|
18
|
+
case type
|
19
|
+
when TextBlock::TYPE_NAME then TextBlock
|
20
|
+
when ImageBlock::TYPE_NAME then ImageBlock
|
21
|
+
when List::TYPE_NAME then List
|
22
|
+
when StackView::TYPE_NAME then StackView
|
23
|
+
when Text::TYPE_NAME then Text
|
24
|
+
when PageNumber::TYPE_NAME then PageNumber
|
25
|
+
when *Basic::TYPE_NAMES then Basic
|
26
|
+
else
|
27
|
+
raise Thinreports::BasicReport::Errors::UnknownShapeType
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
require_relative 'shape/style'
|
36
|
+
require_relative 'shape/manager'
|
37
|
+
require_relative 'shape/base'
|
38
|
+
require_relative 'shape/basic'
|
39
|
+
require_relative 'shape/text'
|
40
|
+
require_relative 'shape/text_block'
|
41
|
+
require_relative 'shape/image_block'
|
42
|
+
require_relative 'shape/list'
|
43
|
+
require_relative 'shape/stack_view'
|
44
|
+
require_relative 'shape/page_number'
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thinreports
|
4
|
+
module BasicReport
|
5
|
+
module Utils
|
6
|
+
def self.included(klass)
|
7
|
+
klass.extend self
|
8
|
+
end
|
9
|
+
|
10
|
+
def deep_copy(src)
|
11
|
+
case src
|
12
|
+
when Hash
|
13
|
+
src.each_with_object({}) do |(k, v), h|
|
14
|
+
h[k] = v.dup rescue v
|
15
|
+
end
|
16
|
+
when Array
|
17
|
+
src.map do |a|
|
18
|
+
a.dup rescue a
|
19
|
+
end
|
20
|
+
else
|
21
|
+
raise ArgumentError
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def blank_value?(value)
|
26
|
+
case value
|
27
|
+
when String then value.empty?
|
28
|
+
when NilClass then true
|
29
|
+
else false
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def call_block_in(scope, &block)
|
34
|
+
return scope unless block_given?
|
35
|
+
|
36
|
+
if block.arity == 1
|
37
|
+
block.call(scope)
|
38
|
+
else
|
39
|
+
scope.instance_eval(&block)
|
40
|
+
end
|
41
|
+
scope
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
extend Utils
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thinreports
|
4
|
+
module BasicReport
|
5
|
+
module Generator
|
6
|
+
class PDF
|
7
|
+
module DrawShape
|
8
|
+
# @param [Thinreports::BasicReport::Core::Shape::TextBlock::Internal] shape
|
9
|
+
# @param [Numeric] height (nil) It will be used as rendering height if present.
|
10
|
+
# Otherwise, the rendering height is the height of schema.
|
11
|
+
# @param [:truncate, :shrink_to_fit, :expand] overflow (nil) It will be set the overflow attribute if present.
|
12
|
+
def draw_shape_tblock(shape, height: nil, overflow: nil, &block)
|
13
|
+
x, y, w = shape.format.attributes.values_at('x', 'y', 'width')
|
14
|
+
|
15
|
+
h = height || shape.format.attributes['height']
|
16
|
+
|
17
|
+
content = shape.real_value.to_s
|
18
|
+
return if content.empty?
|
19
|
+
|
20
|
+
attrs = build_text_attributes(shape.style.finalized_styles)
|
21
|
+
attrs[:overflow] = overflow if overflow
|
22
|
+
|
23
|
+
unless shape.multiple?
|
24
|
+
content = content.tr("\n", ' ')
|
25
|
+
attrs[:single] = true
|
26
|
+
end
|
27
|
+
|
28
|
+
text_box(content, x, y, w, h, attrs, &block)
|
29
|
+
end
|
30
|
+
|
31
|
+
def draw_shape_pageno(shape, page_no, page_count)
|
32
|
+
x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
|
33
|
+
|
34
|
+
attrs = build_text_attributes(shape.style.finalized_styles)
|
35
|
+
text_box(shape.build_format(page_no, page_count), x, y, w, h, attrs)
|
36
|
+
end
|
37
|
+
|
38
|
+
# @param [Thinreports::BasicReport::Core::Shape::Basic::Internal] shape
|
39
|
+
def draw_shape_image(shape)
|
40
|
+
x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
|
41
|
+
|
42
|
+
image_data = shape.format.attributes['data']
|
43
|
+
base64image(image_data['base64'], x, y, w, h)
|
44
|
+
end
|
45
|
+
|
46
|
+
# @param [Thinreports::BasicReport::Core::Shape::ImageBlock::Internal] shape
|
47
|
+
def draw_shape_iblock(shape)
|
48
|
+
return if blank_value?(shape.src)
|
49
|
+
|
50
|
+
x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
|
51
|
+
style = shape.style.finalized_styles
|
52
|
+
|
53
|
+
image_box(
|
54
|
+
shape.src, x, y, w, h,
|
55
|
+
position_x: image_position_x(style['position-x']),
|
56
|
+
position_y: image_position_y(style['position-y']),
|
57
|
+
offset_x: style['offset-x'],
|
58
|
+
offset_y: style['offset-y']
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
def shape_iblock_dimenions(shape)
|
63
|
+
return nil if blank_value?(shape.src)
|
64
|
+
|
65
|
+
x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
|
66
|
+
style = shape.style.finalized_styles
|
67
|
+
|
68
|
+
image_dimensions(
|
69
|
+
shape.src, x, y, w, h,
|
70
|
+
position_x: image_position_x(style['position-x']),
|
71
|
+
position_y: image_position_y(style['position-y'])
|
72
|
+
)
|
73
|
+
end
|
74
|
+
|
75
|
+
# @param [Thinreports::BasicReport::Core::Shape::Text::Internal] shape
|
76
|
+
def draw_shape_text(shape, dheight = 0)
|
77
|
+
x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
|
78
|
+
text(
|
79
|
+
shape.texts.join("\n"), x, y, w, h + dheight,
|
80
|
+
build_text_attributes(shape.style.finalized_styles)
|
81
|
+
)
|
82
|
+
end
|
83
|
+
|
84
|
+
# @param [Thinreports::BasicReport::Core::Shape::Basic::Internal] shape
|
85
|
+
def draw_shape_ellipse(shape)
|
86
|
+
cx, cy, rx, ry = shape.format.attributes.values_at('cx', 'cy', 'rx', 'ry')
|
87
|
+
ellipse(cx, cy, rx, ry, build_graphic_attributes(shape.style.finalized_styles))
|
88
|
+
end
|
89
|
+
|
90
|
+
# @param [Thinreports::BasicReport::Core::Shape::Basic::Internal] shape
|
91
|
+
def draw_shape_line(shape, dy1 = 0, dy2 = 0)
|
92
|
+
x1, y1, x2, y2 = shape.format.attributes.values_at('x1', 'y1', 'x2', 'y2')
|
93
|
+
line(x1, y1 + dy1, x2, y2 + dy2, build_graphic_attributes(shape.style.finalized_styles))
|
94
|
+
end
|
95
|
+
|
96
|
+
# @param [Thinreports::BasicReport::Core::Shape::Basic::Internal] shape
|
97
|
+
def draw_shape_rect(shape, dheight = 0)
|
98
|
+
x, y, w, h = shape.format.attributes.values_at('x', 'y', 'width', 'height')
|
99
|
+
rect_attributes = build_graphic_attributes(shape.style.finalized_styles) do |attrs|
|
100
|
+
attrs[:radius] = shape.format.attributes['border-radius']
|
101
|
+
end
|
102
|
+
rect(x, y, w, h + dheight, rect_attributes)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thinreports
|
4
|
+
module BasicReport
|
5
|
+
module Generator
|
6
|
+
class PDF
|
7
|
+
module DrawTemplateItems
|
8
|
+
# @param [Array<Hash>] items
|
9
|
+
def draw_template_items(items)
|
10
|
+
items.each do |item_attributes|
|
11
|
+
next unless drawable?(item_attributes)
|
12
|
+
|
13
|
+
case item_attributes['type']
|
14
|
+
when 'text' then draw_text(item_attributes)
|
15
|
+
when 'image' then draw_image(item_attributes)
|
16
|
+
when 'rect' then draw_rect(item_attributes)
|
17
|
+
when 'ellipse' then draw_ellipse(item_attributes)
|
18
|
+
when 'line' then draw_line(item_attributes)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# @param [Hash] item_attributes
|
26
|
+
def draw_rect(item_attributes)
|
27
|
+
x, y, w, h = item_attributes.values_at('x', 'y', 'width', 'height')
|
28
|
+
graphic_attributes = build_graphic_attributes(item_attributes['style']) do |attrs|
|
29
|
+
attrs[:radius] = item_attributes['border-radius']
|
30
|
+
end
|
31
|
+
|
32
|
+
rect(x, y, w, h, graphic_attributes)
|
33
|
+
end
|
34
|
+
|
35
|
+
# @see #draw_rect
|
36
|
+
def draw_ellipse(item_attributes)
|
37
|
+
x, y, rx, ry = item_attributes.values_at('cx', 'cy', 'rx', 'ry')
|
38
|
+
ellipse(x, y, rx, ry, build_graphic_attributes(item_attributes['style']))
|
39
|
+
end
|
40
|
+
|
41
|
+
# @see #draw_rect
|
42
|
+
def draw_line(item_attributes)
|
43
|
+
x1, y1, x2, y2 = item_attributes.values_at('x1', 'y1', 'x2', 'y2')
|
44
|
+
line(x1, y1, x2, y2, build_graphic_attributes(item_attributes['style']))
|
45
|
+
end
|
46
|
+
|
47
|
+
# @see #draw_rect
|
48
|
+
def draw_text(item_attributes)
|
49
|
+
x, y, w, h = item_attributes.values_at('x', 'y', 'width', 'height')
|
50
|
+
text(
|
51
|
+
item_attributes['texts'].join("\n"), x, y, w, h,
|
52
|
+
build_text_attributes(item_attributes['style'])
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
# @see #draw_rect
|
57
|
+
def draw_image(item_attributes)
|
58
|
+
x, y, w, h = item_attributes.values_at('x', 'y', 'width', 'height')
|
59
|
+
image_data = item_attributes['data']
|
60
|
+
|
61
|
+
base64image(image_data['base64'], x, y, w, h)
|
62
|
+
end
|
63
|
+
|
64
|
+
def drawable?(item_attributes)
|
65
|
+
item_attributes['id'].empty? && item_attributes['display']
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Thinreports
|
4
|
+
module BasicReport
|
5
|
+
module Generator
|
6
|
+
class PDF
|
7
|
+
module Font
|
8
|
+
FONT_STORE = Thinreports.root.join('fonts')
|
9
|
+
|
10
|
+
BUILTIN_FONTS = {
|
11
|
+
'IPAMincho' => FONT_STORE.join('ipam.ttf').to_s,
|
12
|
+
'IPAPMincho' => FONT_STORE.join('ipamp.ttf').to_s,
|
13
|
+
'IPAGothic' => FONT_STORE.join('ipag.ttf').to_s,
|
14
|
+
'IPAPGothic' => FONT_STORE.join('ipagp.ttf').to_s
|
15
|
+
}.freeze
|
16
|
+
|
17
|
+
DEFAULT_FALLBACK_FONTS = %w[IPAMincho].freeze
|
18
|
+
|
19
|
+
PRAWN_BUINTIN_FONT_ALIASES = {
|
20
|
+
'Courier New' => 'Courier',
|
21
|
+
'Times New Roman' => 'Times-Roman'
|
22
|
+
}.freeze
|
23
|
+
|
24
|
+
def setup_fonts
|
25
|
+
# Install built-in fonts.
|
26
|
+
BUILTIN_FONTS.each do |font_name, font_path|
|
27
|
+
install_font(font_name, font_path)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Create aliases from the font list provided by Prawn.
|
31
|
+
PRAWN_BUINTIN_FONT_ALIASES.each do |alias_name, name|
|
32
|
+
pdf.font_families[alias_name] = pdf.font_families[name]
|
33
|
+
end
|
34
|
+
|
35
|
+
# Setup custom fallback fonts
|
36
|
+
fallback_fonts = Thinreports.config.fallback_fonts.uniq
|
37
|
+
fallback_fonts.map!.with_index do |font, i|
|
38
|
+
if pdf.font_families.key?(font)
|
39
|
+
font
|
40
|
+
else
|
41
|
+
install_font "Custom-fallback-font#{i}", font
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Set fallback fonts
|
46
|
+
pdf.fallback_fonts(fallback_fonts + DEFAULT_FALLBACK_FONTS)
|
47
|
+
end
|
48
|
+
|
49
|
+
# @param [String] name
|
50
|
+
# @param [String] file
|
51
|
+
# @return [String] installed font name
|
52
|
+
def install_font(name, file)
|
53
|
+
raise Errors::FontFileNotFound unless File.exist?(file)
|
54
|
+
|
55
|
+
pdf.font_families[name] = {
|
56
|
+
normal: file,
|
57
|
+
bold: file,
|
58
|
+
italic: file,
|
59
|
+
bold_italic: file
|
60
|
+
}
|
61
|
+
name
|
62
|
+
end
|
63
|
+
|
64
|
+
# @return [String]
|
65
|
+
def default_family
|
66
|
+
'Helvetica'
|
67
|
+
end
|
68
|
+
|
69
|
+
# @param [String] family
|
70
|
+
# @return [String]
|
71
|
+
def default_family_if_missing(family)
|
72
|
+
pdf.font_families.key?(family) ? family : default_family
|
73
|
+
end
|
74
|
+
|
75
|
+
# @param [String] font_name
|
76
|
+
# @param [:bold, :italic] font_style
|
77
|
+
# @return [Boolean]
|
78
|
+
def font_has_style?(font_name, font_style)
|
79
|
+
font = pdf.font_families[font_name]
|
80
|
+
|
81
|
+
return false unless font
|
82
|
+
return false unless font.key?(font_style)
|
83
|
+
|
84
|
+
font[font_style] != font[:normal]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|