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

Instance Method Details

#base64image(base64_data, x, y, w, h) ⇒ Object

Parameters:

  • base64_data (String)
  • x (Numeric, Strng)
  • y (Numeric, Strng)
  • w (Numeric, Strng)
  • h (Numeric, Strng)


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?

Parameters:

  • styles (Hash)

Returns:

  • (Hash, nil)


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

Parameters:

  • style (Hash)

Yields:

  • (attrs)

Yield Parameters:

  • attrs (Hash)

Returns:

  • (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?

Parameters:

  • styles (Hash)

Returns:

  • (Hash, nil)


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

Parameters:

  • style (Hash)

Yields:

  • (attrs)

Yield Parameters:

  • attrs (Hash)

Returns:

  • (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_imagesObject



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.

Parameters:

  • image_id (String)
  • image_data (String)

Returns:

  • (String)

    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

Parameters:

  • x (Numeric, String)

    center-x

  • y (Numeric, String)

    center-y

  • rx (Numeric, String)
  • ry (Numeric, String)
  • attrs (Hash) (defaults to: {})

    ({})

Options Hash (attrs):

  • :stroke (String)
  • :stroke_width (Numeric, String)
  • :stroke_dash (Array<Integer, String>)
  • :stroke_type ("solid", "dashed", "dotted")
  • :fill (String)


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

Parameters:

  • font_names (Array<String>)

Returns:

  • (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>

Parameters:

  • styles (Array<String>)

Returns:

  • (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

Parameters:

  • filename_or_io (String, IO)
  • x (Numeric, Strng)
  • y (Numeric, Strng)
  • w (Numeric, Strng)
  • h (Numeric, Strng)


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

Parameters:

  • filename_or_io (String, IO)
  • x (Numeric, Strng)
  • y (Numeric, Strng)
  • w (Numeric, Strng)
  • h (Numeric, Strng)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :position_x (:left, :center, :right) — default: :left
  • :position_y (:top, :center, :bottom) — default: :top
  • :offset_x (Numeric)
  • :offset_y (Numeric)


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, options = {})
  w, h = s2f(w, h)

  computed_position = pos(
    x + (options[:offset_x] || 0),
    y + (options[:offset_y] || 0)
  )
  pdf.bounding_box(computed_position, width: w, height: h) do
    pdf.image(
      filename_or_io,
      position: options[:position_x] || :left,
      vposition: options[: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, options = {})
  w, h = s2f(w, h)
  # XXX: Calling @private method
  _pdf_obj, info = pdf.build_image_object(filename_or_io)
  info.calc_image_dimensions(
    position: options[:position_x] || :left,
    vposition: options[:position_y] || :top,
    auto_fit: [w, h]
  )
end

#image_position_x(position) ⇒ :left, ...

Parameters:

  • position ("left", "center", "right", "")

Returns:

  • (:left, :center, :right)


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, ...

Parameters:

  • position ("top", "middle", "bottom", "")

Returns:

  • (:left, :center, :right)


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?

Parameters:

  • spacing (Float, "", nil)

Returns:

  • (Float, nil)


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

Parameters:

  • x1 (Numeric, String)
  • y1 (Numeric, String)
  • x2 (Numeric, String)
  • y2 (Numeric, String)
  • attrs (Hash) (defaults to: {})

    ({})

Options Hash (attrs):

  • :stroke (String)
  • :stroke_width (Numeric, String)
  • :stroke_type ("solid", "dashed", "dotted")


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?

Parameters:

  • height (Float, "", nil)

Returns:

  • (Float, nil)


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

Parameters:

  • x (Numeric, String)
  • y (Numeric, String)
  • w (Numeric, String)

    width

  • h (Numeric, String)

    height

  • attrs (Hash) (defaults to: {})

    ({})

Options Hash (attrs):

  • :radius (Integer, String)
  • :stroke (String)
  • :stroke_width (Numeric, String)
  • :stroke_type ("solid", "dashed", "dotted")
  • :fill (String)


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_registryObject



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

See Also:



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, ...

Parameters:

  • align ("left", "center", "right", "")

Returns:

  • (:left, :center, :right)


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

Parameters:

  • content (String)
  • x (Numeric, String)
  • y (Numeric, String)
  • w (Numeric, String)
  • h (Numeric, String)
  • attrs (Hash) (defaults to: {})

    ({})

Options Hash (attrs):

  • :font (String)
  • :size (Numeric, String)
  • :color (String)
  • :styles (Array<:bold, :italic, :underline, :strikethrough>) — default: nil
  • :align (:left, :center, :right) — default: :left
  • :valign (:top, :center, :bottom) — default: :top
  • :line_height (Numeric, String)

    The total height of an text line.

  • :letter_spacing (Numeric, String)
  • :single (Boolean) — default: false
  • :overflow (:trancate, :shrink_to_fit, :expand) — default: :trancate
  • :word_wrap (:none, :break_word) — default: :none
  • :overflow_wrap (:normal, :anywhere, :disable_break_word_by_space) — default: :normal


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, ...

Parameters:

  • overflow ("truncate", "fit", "expand", "", nil)

Returns:

  • (:truncate, :shrink_to_fit, :expand)


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, ...

Parameters:

  • valign ("top", "middle", "bottom", "", nil)

Returns:

  • (:top, :center, :bottom)


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

Parameters:

  • attrs (Hash)


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

Parameters:

  • word_wrap ("break-word", "none", "", nil)

Returns:

  • (: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