Class: SilkLayout::Render::Painter

Inherits:
Object
  • Object
show all
Defined in:
lib/silk_layout/render/painter.rb

Class Method Summary collapse

Class Method Details

.color_to_symbol(color) ⇒ Object



192
193
194
195
196
197
198
199
# File 'lib/silk_layout/render/painter.rb', line 192

def self.color_to_symbol(color)
  return nil unless color

  normalized = color.to_s.strip
  return nil if normalized.empty?

  normalized.to_sym
end

.draw_background(canvas, box, page_height_pt) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/silk_layout/render/painter.rb', line 56

def self.draw_background(canvas, box, page_height_pt)
  return if box.is_a?(SilkLayout::Layout::AnonymousBlockBox)
  return unless box.respond_to?(:background_color) && box.background_color

  rgb = rgb_color(box.background_color)
  return unless rgb

  x = PdfRenderer.px_to_pt(box.border_box_x)
  y = page_height_pt - PdfRenderer.px_to_pt(box.border_box_y + box.border_box_height)
  w = PdfRenderer.px_to_pt(box.border_box_width)
  h = PdfRenderer.px_to_pt(box.border_box_height)

  canvas.fill_color(*rgb)
  canvas.rectangle(x, y, w, h).fill
end

.draw_borders(canvas, box, page_height_pt) ⇒ Object



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/silk_layout/render/painter.rb', line 72

def self.draw_borders(canvas, box, page_height_pt)
  return if box.is_a?(SilkLayout::Layout::AnonymousBlockBox)
  return unless box.has_border

  x = PdfRenderer.px_to_pt(box.border_box_x)
  y = page_height_pt - PdfRenderer.px_to_pt(box.border_box_y + box.border_box_height)
  w = PdfRenderer.px_to_pt(box.border_box_width)
  h = PdfRenderer.px_to_pt(box.border_box_height)

  top = PdfRenderer.px_to_pt(box.border[:top])
  bottom = PdfRenderer.px_to_pt(box.border[:bottom])
  left = PdfRenderer.px_to_pt(box.border[:left])
  right = PdfRenderer.px_to_pt(box.border[:right])

  colors = [box.border_color[:top], box.border_color[:right], box.border_color[:bottom], box.border_color[:left]]
  if colors.uniq.length <= 1
    if top > 0
      canvas = set_color(canvas, box.border_color[:top])
      canvas.rectangle(x, y + h - top, w, top).fill
    end

    if bottom > 0
      canvas = set_color(canvas, box.border_color[:bottom])
      canvas.rectangle(x, y, w, bottom).fill
    end

    if left > 0
      canvas = set_color(canvas, box.border_color[:left])
      canvas.rectangle(x, y, left, h).fill
    end

    if right > 0
      canvas = set_color(canvas, box.border_color[:right])
      canvas.rectangle(x + w - right, y, right, h).fill
    end

    return
  end

  inner_w = [w - left - right, 0].max
  inner_h = [h - top - bottom, 0].max

  if top > 0 && inner_w > 0
    canvas = set_color(canvas, box.border_color[:top])
    canvas.rectangle(x + left, y + h - top, inner_w, top).fill
  end

  if bottom > 0 && inner_w > 0
    canvas = set_color(canvas, box.border_color[:bottom])
    canvas.rectangle(x + left, y, inner_w, bottom).fill
  end

  if left > 0 && inner_h > 0
    canvas = set_color(canvas, box.border_color[:left])
    canvas.rectangle(x, y + bottom, left, inner_h).fill
  end

  if right > 0 && inner_h > 0
    canvas = set_color(canvas, box.border_color[:right])
    canvas.rectangle(x + w - right, y + bottom, right, inner_h).fill
  end

  draw_corner(
    canvas,
    x,
    y + h - top,
    left,
    top,
    box.border_color[:left],
    box.border_color[:top],
    :top_left
  )

  draw_corner(
    canvas,
    x + w - right,
    y + h - top,
    right,
    top,
    box.border_color[:right],
    box.border_color[:top],
    :top_right
  )

  draw_corner(
    canvas,
    x,
    y,
    left,
    bottom,
    box.border_color[:left],
    box.border_color[:bottom],
    :bottom_left
  )

  draw_corner(
    canvas,
    x + w - right,
    y,
    right,
    bottom,
    box.border_color[:right],
    box.border_color[:bottom],
    :bottom_right
  )
end

.draw_corner(canvas, x, y, w, h, vertical_color, horizontal_color, kind) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/silk_layout/render/painter.rb', line 205

def self.draw_corner(canvas, x, y, w, h, vertical_color, horizontal_color, kind)
  return if w <= 0 || h <= 0

  if vertical_color == horizontal_color
    canvas = set_color(canvas, vertical_color)
    canvas.rectangle(x, y, w, h).fill
    return
  end

  case kind
  when :top_left
    fill_triangle(canvas, horizontal_color, [x, y + h], [x + w, y + h], [x + w, y])
    fill_triangle(canvas, vertical_color, [x, y + h], [x + w, y], [x, y])
  when :top_right
    fill_triangle(canvas, horizontal_color, [x, y + h], [x + w, y + h], [x, y])
    fill_triangle(canvas, vertical_color, [x + w, y + h], [x + w, y], [x, y])
  when :bottom_left
    fill_triangle(canvas, horizontal_color, [x, y], [x + w, y], [x + w, y + h])
    fill_triangle(canvas, vertical_color, [x, y], [x + w, y + h], [x, y + h])
  when :bottom_right
    fill_triangle(canvas, horizontal_color, [x, y], [x + w, y], [x, y + h])
    fill_triangle(canvas, vertical_color, [x + w, y], [x + w, y + h], [x, y + h])
  end
end

.fill_triangle(canvas, color, p1, p2, p3) ⇒ Object



230
231
232
233
234
235
236
# File 'lib/silk_layout/render/painter.rb', line 230

def self.fill_triangle(canvas, color, p1, p2, p3)
  canvas = set_color(canvas, color)
  canvas.move_to(*p1)
  canvas.line_to(*p2)
  canvas.line_to(*p3)
  canvas.close_subpath.fill
end

.paint(canvas, box, page_height_pt) ⇒ Object



6
7
8
# File 'lib/silk_layout/render/painter.rb', line 6

def self.paint(canvas, box, page_height_pt)
  paint_box(canvas, box, page_height_pt)
end

.paint_image(canvas, box, page_height_pt) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/silk_layout/render/painter.rb', line 41

def self.paint_image(canvas, box, page_height_pt)
  return unless box.image_path && File.file?(box.image_path)

  width = box.content_box_width
  height = box.content_box_height
  return unless width.positive? && height.positive?

  x_pt = PdfRenderer.px_to_pt(box.content_box_x)
  y_pt = page_height_pt - PdfRenderer.px_to_pt(box.content_box_y + height)
  width_pt = PdfRenderer.px_to_pt(width)
  height_pt = PdfRenderer.px_to_pt(height)

  canvas.image(box.image_path, at: [x_pt, y_pt], width: width_pt, height: height_pt)
end

.paint_text(canvas, box, page_height_pt) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/silk_layout/render/painter.rb', line 24

def self.paint_text(canvas, box, page_height_pt)
  font_size_px = box.font_size || 16
  font_name = box.font_name || box.font_family || "Helvetica"

  font_size_pt = PdfRenderer.px_to_pt(font_size_px)
  ascent_pt = PdfRenderer.px_to_pt(box.respond_to?(:ascender) ? box.ascender : font_size_px * 0.8)

  canvas.font(font_name, size: font_size_pt)

  canvas = set_color(canvas, color_to_symbol(box.respond_to?(:color) ? box.color : nil) || :black)

  x_pt = PdfRenderer.px_to_pt(box.x)
  y_pt = page_height_pt - PdfRenderer.px_to_pt(box.y) - ascent_pt

  canvas.text(box.text, at: [x_pt, y_pt])
end

.rgb_color(color) ⇒ Object



201
202
203
# File 'lib/silk_layout/render/painter.rb', line 201

def self.rgb_color(color)
  SilkLayout::CSS::Color.rgb(color)
end

.set_color(canvas, color) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/silk_layout/render/painter.rb', line 179

def self.set_color(canvas, color)
  return canvas unless color

  rgb = rgb_color(color)
  if rgb
    canvas.fill_color(*rgb)
  else
    canvas.fill_color(0, 0, 0)
  end

  canvas
end