Class: SilkLayout::Render::Painter
- Inherits:
-
Object
- Object
- SilkLayout::Render::Painter
- Defined in:
- lib/silk_layout/render/painter.rb
Constant Summary collapse
- NAMED_COLORS =
{ black: [0, 0, 0], blue: [0, 0, 255], green: [0, 128, 0], lightblue: [173, 216, 230], red: [255, 0, 0], white: [255, 255, 255] }.freeze
Class Method Summary collapse
- .color_to_symbol(color) ⇒ Object
- .draw_background(canvas, box, page_height_pt) ⇒ Object
- .draw_borders(canvas, box, page_height_pt) ⇒ Object
- .draw_corner(canvas, x, y, w, h, vertical_color, horizontal_color, kind) ⇒ Object
- .fill_triangle(canvas, color, p1, p2, p3) ⇒ Object
- .hex_color(color) ⇒ Object
- .paint(canvas, box, page_height_pt) ⇒ Object
- .paint_text(canvas, box, page_height_pt) ⇒ Object
- .rgb_color(color) ⇒ Object
- .set_color(canvas, color) ⇒ Object
Class Method Details
.color_to_symbol(color) ⇒ Object
176 177 178 179 180 181 182 183 |
# File 'lib/silk_layout/render/painter.rb', line 176 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
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/silk_layout/render/painter.rb', line 40 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
56 57 58 59 60 61 62 63 64 65 66 67 68 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 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 |
# File 'lib/silk_layout/render/painter.rb', line 56 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
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/silk_layout/render/painter.rb', line 202 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
227 228 229 230 231 232 233 |
# File 'lib/silk_layout/render/painter.rb', line 227 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 |
.hex_color(color) ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/silk_layout/render/painter.rb', line 194 def self.hex_color(color) hex = color.delete_prefix("#") hex = hex.chars.flat_map { |char| [char, char] }.join if hex.length == 3 return nil unless hex.match?(/\A[0-9a-f]{6}\z/) hex.scan(/../).map { |component| component.to_i(16) } 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_text(canvas, box, page_height_pt) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/silk_layout/render/painter.rb', line 23 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
185 186 187 188 189 190 191 192 |
# File 'lib/silk_layout/render/painter.rb', line 185 def self.rgb_color(color) normalized = color.to_s.strip.downcase return nil if normalized.empty? || normalized == "transparent" return hex_color(normalized) if normalized.start_with?("#") NAMED_COLORS[normalized.to_sym] end |
.set_color(canvas, color) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/silk_layout/render/painter.rb', line 163 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 |