Module: Thinreports::BasicReport::Generator::PDF::Graphics
- Included in:
- Document
- Defined in:
- lib/thinreports/basic_report/generator/pdf/document/graphics.rb,
lib/thinreports/basic_report/generator/pdf/document/graphics/text.rb,
lib/thinreports/basic_report/generator/pdf/document/graphics/basic.rb,
lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb,
lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb
Constant Summary collapse
- BASE_LINE_WIDTH =
0.9
- STROKE_DASH =
{ dashed: [2, 2], dotted: [1, 2] }.freeze
Instance Method Summary collapse
- #base64image(base64_data, x, y, w, h) ⇒ Object
- #build_fill_styles(styles) ⇒ Hash?
- #build_graphic_attributes(style) {|attrs| ... } ⇒ Hash
- #build_stroke_styles(styles) ⇒ Hash?
- #build_text_attributes(style) {|attrs| ... } ⇒ Hash
- #clean_temp_images ⇒ Object
-
#create_temp_imagefile(image_id, image_data) ⇒ String
Path to imagefile.
- #ellipse(x, y, rx, ry, attrs = {}) ⇒ Object
- #font_family(font_names) ⇒ String
- #font_styles(styles) ⇒ Array<Symbol>
- #image(filename_or_io, x, y, w, h) ⇒ Object
- #image_box(filename_or_io, x, y, w, h, options = {}) ⇒ Object
- #image_dimensions(filename_or_io, x, y, w, h, options = {}) ⇒ Object
- #image_position_x(position) ⇒ :left, ...
- #image_position_y(position) ⇒ :left, ...
- #letter_spacing(spacing) ⇒ Float?
- #line(x1, y1, x2, y2, attrs = {}) ⇒ Object
- #line_height(height) ⇒ Float?
- #migrate_overflow_wrap_from_word_wrap(computed_word_wrap) ⇒ Object
- #overflow_wrap(style, computed_word_wrap) ⇒ Object
- #rect(x, y, w, h, attrs = {}) ⇒ Object
- #temp_image_registry ⇒ Object
- #text(content, x, y, w, h, attrs = {}) ⇒ Object
- #text_align(align) ⇒ :left, ...
- #text_box(content, x, y, w, h, attrs = {}, &block) ⇒ Object
- #text_overflow(overflow) ⇒ :truncate, ...
- #text_valign(valign) ⇒ :top, ...
- #with_graphic_styles(attrs, &block) ⇒ Object
- #word_wrap(word_wrap) ⇒ :break_word, :none
Instance Method Details
#base64image(base64_data, x, y, w, h) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb', line 27 def base64image(base64_data, x, y, w, h) image_data = Base64.decode64(base64_data) image_id = Digest::MD5.hexdigest(base64_data) image_path = create_temp_imagefile(image_id, image_data) image(image_path, x, y, w, h) end |
#build_fill_styles(styles) ⇒ Hash?
127 128 129 130 131 132 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/basic.rb', line 127 def build_fill_styles(styles) color = styles[:fill] return nil unless color && color != 'none' { color: parse_color(color) } end |
#build_graphic_attributes(style) {|attrs| ... } ⇒ Hash
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 12 def build_graphic_attributes(style, &block) graphic_attributes = { stroke: style['border-color'], stroke_width: style['border-width'], stroke_type: style['border-style'], fill: style['fill-color'] } block.call(graphic_attributes) if block_given? graphic_attributes end |
#build_stroke_styles(styles) ⇒ Hash?
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/basic.rb', line 112 def build_stroke_styles(styles) color = styles[:stroke] width = styles[:stroke_width] return nil unless color && color != 'none' return nil unless width && width != 0 { color: parse_color(color), width: s2f(width), dash: STROKE_DASH[styles[:stroke_type].to_sym] } end |
#build_text_attributes(style) {|attrs| ... } ⇒ Hash
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 27 def build_text_attributes(style, &block) word_wrap = word_wrap(style['word-wrap']) text_attributes = { font: font_family(style['font-family']), size: style['font-size'], color: style['color'], align: text_align(style['text-align']), valign: text_valign(style['vertical-align']), styles: font_styles(style['font-style']), letter_spacing: letter_spacing(style['letter-spacing']), line_height: line_height(style['line-height']), overflow: text_overflow(style['overflow']), word_wrap: word_wrap, # Deprecated: Use overflow_wrap instead of word_wrap overflow_wrap: overflow_wrap(style['overflow-wrap'], word_wrap) } block.call(text_attributes) if block_given? text_attributes end |
#clean_temp_images ⇒ Object
73 74 75 76 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb', line 73 def clean_temp_images temp_image_registry.each_value(&:close!) temp_image_registry.clear end |
#create_temp_imagefile(image_id, image_data) ⇒ String
Returns Path to imagefile.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb', line 85 def create_temp_imagefile(image_id, image_data) temp_image_registry[image_id] ||= begin file = Tempfile.new('temp-image') file.binmode file.write(image_data) file.open file end temp_image_registry[image_id].path end |
#ellipse(x, y, rx, ry, attrs = {}) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/basic.rb', line 60 def ellipse(x, y, rx, ry, attrs = {}) rx, ry = s2f(rx, ry) with_graphic_styles(attrs) do pdf.ellipse(pos(x, y), rx, ry) end end |
#font_family(font_names) ⇒ String
68 69 70 71 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 68 def font_family(font_names) font_name = font_names.first default_family_if_missing(font_name) end |
#font_styles(styles) ⇒ Array<Symbol>
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 75 def font_styles(styles) styles.map do |font_style| case font_style when 'bold' then :bold when 'italic' then :italic when 'underline' then :underline when 'linethrough' then :strikethrough end end end |
#image(filename_or_io, x, y, w, h) ⇒ Object
17 18 19 20 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb', line 17 def image(filename_or_io, x, y, w, h) w, h = s2f(w, h) pdf.image(filename_or_io, at: pos(x, y), width: w, height: h) end |
#image_box(filename_or_io, x, y, w, h, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb', line 45 def image_box(filename_or_io, x, y, w, h, = {}) w, h = s2f(w, h) computed_position = pos( x + ([:offset_x] || 0), y + ([:offset_y] || 0) ) pdf.bounding_box(computed_position, width: w, height: h) do pdf.image( filename_or_io, position: [:position_x] || :left, vposition: [:position_y] || :top, auto_fit: [w, h] ) end end |
#image_dimensions(filename_or_io, x, y, w, h, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb', line 62 def image_dimensions(filename_or_io, x, y, w, h, = {}) w, h = s2f(w, h) # XXX: Calling @private method _pdf_obj, info = pdf.build_image_object(filename_or_io) info.calc_image_dimensions( position: [:position_x] || :left, vposition: [:position_y] || :top, auto_fit: [w, h] ) end |
#image_position_x(position) ⇒ :left, ...
146 147 148 149 150 151 152 153 154 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 146 def image_position_x(position) case position when 'left' then :left when 'center' then :center when 'right' then :right when '' then :left else :left end end |
#image_position_y(position) ⇒ :left, ...
158 159 160 161 162 163 164 165 166 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 158 def image_position_y(position) case position when 'top' then :top when 'middle' then :center when 'bottom' then :bottom when '' then :top else :top end end |
#letter_spacing(spacing) ⇒ Float?
88 89 90 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 88 def letter_spacing(spacing) blank_value?(spacing) ? nil : spacing end |
#line(x1, y1, x2, y2, attrs = {}) ⇒ Object
21 22 23 24 25 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/basic.rb', line 21 def line(x1, y1, x2, y2, attrs = {}) with_graphic_styles(attrs) do pdf.line(pos(x1, y1), pos(x2, y2)) end end |
#line_height(height) ⇒ Float?
140 141 142 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 140 def line_height(height) blank_value?(height) ? nil : height end |
#migrate_overflow_wrap_from_word_wrap(computed_word_wrap) ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 58 def migrate_overflow_wrap_from_word_wrap(computed_word_wrap) case computed_word_wrap when :none then 'disable-break-word-by-space' when :break_word then 'normal' else raise ArgumentError, 'Invalid computed word_wrap value' end end |
#overflow_wrap(style, computed_word_wrap) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 48 def overflow_wrap(style, computed_word_wrap) case style || migrate_overflow_wrap_from_word_wrap(computed_word_wrap) when 'normal', nil then :normal when 'anywhere' then :anywhere # Deprecated: This is a temporary value for migrating from word_wrap. when 'disable-break-word-by-space' then :disable_break_word_by_space else :normal end end |
#rect(x, y, w, h, attrs = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/basic.rb', line 37 def rect(x, y, w, h, attrs = {}) w, h = s2f(w, h) radius = s2f(attrs[:radius]) with_graphic_styles(attrs) do if radius && !radius.zero? pdf.rounded_rectangle(pos(x, y), w, h, radius) else pdf.rectangle(pos(x, y), w, h) end end end |
#temp_image_registry ⇒ Object
78 79 80 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/image.rb', line 78 def temp_image_registry @temp_image_registry ||= {} end |
#text(content, x, y, w, h, attrs = {}) ⇒ Object
58 59 60 61 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/text.rb', line 58 def text(content, x, y, w, h, attrs = {}) # Set the :overflow property to :shirink_to_fit. text_box(content, x, y, w, h, { overflow: :shirink_to_fit }.merge(attrs)) end |
#text_align(align) ⇒ :left, ...
94 95 96 97 98 99 100 101 102 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 94 def text_align(align) case align when 'left' then :left when 'center' then :center when 'right' then :right when '' then :left else :left end end |
#text_box(content, x, y, w, h, attrs = {}, &block) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/text.rb', line 27 def text_box(content, x, y, w, h, attrs = {}, &block) w, h = s2f(w, h) box_attrs = text_box_attrs( x, y, w, h, single: attrs.delete(:single), overflow: attrs[:overflow] ) content = replace_space_to_nbsp(content) if attrs[:overflow_wrap] == :disable_break_word_by_space with_text_styles(attrs) do |built_attrs, font_styles| if block block.call [{ text: content, styles: font_styles }], built_attrs.merge(box_attrs) else pdf.formatted_text_box( [{ text: content, styles: font_styles }], built_attrs.merge(box_attrs) ) end end rescue Prawn::Errors::CannotFit # Nothing to do. # # When the area is too small compared # with the content and the style of the text. # (See prawn/core/text/formatted/line_wrap.rb#L185) end |
#text_overflow(overflow) ⇒ :truncate, ...
118 119 120 121 122 123 124 125 126 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 118 def text_overflow(overflow) case overflow when 'truncate' then :truncate when 'fit' then :shrink_to_fit when 'expand' then :expand when '' then :truncate else :truncate end end |
#text_valign(valign) ⇒ :top, ...
106 107 108 109 110 111 112 113 114 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 106 def text_valign(valign) case valign when 'top' then :top when 'middle' then :center when 'bottom' then :bottom when '' then :top else :top end end |
#with_graphic_styles(attrs, &block) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/basic.rb', line 69 def with_graphic_styles(attrs, &block) stroke = build_stroke_styles(attrs) fill = build_fill_styles(attrs) # Do not draw if no colors given. return unless fill || stroke save_graphics_state # Apply stroke-dashed. if stroke && stroke[:dash] length, space = stroke[:dash] pdf.dash(length, space: space) end # Draw with fill and stroke. if fill && stroke pdf.fill_and_stroke do line_width(stroke[:width]) pdf.fill_color(fill[:color]) pdf.stroke_color(stroke[:color]) block.call end # Draw only with fill. elsif fill pdf.fill do pdf.fill_color(fill[:color]) block.call end # Draw only with stroke. elsif stroke pdf.stroke do line_width(stroke[:width]) pdf.stroke_color(stroke[:color]) block.call end end restore_graphics_state end |
#word_wrap(word_wrap) ⇒ :break_word, :none
130 131 132 133 134 135 136 |
# File 'lib/thinreports/basic_report/generator/pdf/document/graphics/attributes.rb', line 130 def word_wrap(word_wrap) case word_wrap when 'break-word' then :break_word when 'none' then :none else :none end end |