Class: Rpdfium::Table::Canvas

Inherits:
Object
  • Object
show all
Defined in:
lib/rpdfium/table/debugger.rb

Overview

Mini canvas RGBA per disegnare sopra il rendering. Niente di sofisticato: linee Bresenham, dots, rect fill con alpha blending semplice.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, rgba_bytes) ⇒ Canvas

Returns a new instance of Canvas.



56
57
58
59
60
61
# File 'lib/rpdfium/table/debugger.rb', line 56

def initialize(width, height, rgba_bytes)
  @width  = width
  @height = height
  # Lavoriamo su una stringa mutabile (binstring)
  @bytes = rgba_bytes.dup.force_encoding(Encoding::ASCII_8BIT)
end

Instance Attribute Details

#bytesObject (readonly)

Returns the value of attribute bytes.



54
55
56
# File 'lib/rpdfium/table/debugger.rb', line 54

def bytes
  @bytes
end

#heightObject (readonly)

Returns the value of attribute height.



54
55
56
# File 'lib/rpdfium/table/debugger.rb', line 54

def height
  @height
end

#widthObject (readonly)

Returns the value of attribute width.



54
55
56
# File 'lib/rpdfium/table/debugger.rb', line 54

def width
  @width
end

Instance Method Details

#dot(cx, cy, color, radius) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/rpdfium/table/debugger.rb', line 105

def dot(cx, cy, color, radius)
  (-radius..radius).each do |dy|
    (-radius..radius).each do |dx|
      set_pixel(cx + dx, cy + dy, color) if dx * dx + dy * dy <= radius * radius
    end
  end
end

#line(x0, y0, x1, y1, color) ⇒ Object

Bresenham



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rpdfium/table/debugger.rb', line 84

def line(x0, y0, x1, y1, color)
  dx = (x1 - x0).abs
  dy = -(y1 - y0).abs
  sx = x0 < x1 ? 1 : -1
  sy = y0 < y1 ? 1 : -1
  err = dx + dy
  x = x0; y = y0
  loop do
    set_pixel(x, y, color)
    break if x == x1 && y == y1

    e2 = 2 * err
    if e2 >= dy
      err += dy; x += sx
    end
    if e2 <= dx
      err += dx; y += sy
    end
  end
end

#rect_fill(x0, y0, x1, y1, color) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/rpdfium/table/debugger.rb', line 113

def rect_fill(x0, y0, x1, y1, color)
  (y0..y1).each do |y|
    (x0..x1).each do |x|
      set_pixel(x, y, color)
    end
  end
end

#set_pixel(x, y, color) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rpdfium/table/debugger.rb', line 63

def set_pixel(x, y, color)
  return if x < 0 || x >= @width || y < 0 || y >= @height

  idx = (y * @width + x) * 4
  r, g, b, a = color
  if a >= 255
    @bytes.setbyte(idx, r)
    @bytes.setbyte(idx + 1, g)
    @bytes.setbyte(idx + 2, b)
    @bytes.setbyte(idx + 3, 255)
  else
    # Alpha blending semplice (over operator)
    src_a = a / 255.0
    inv = 1 - src_a
    @bytes.setbyte(idx,     (r * src_a + @bytes.getbyte(idx)     * inv).to_i)
    @bytes.setbyte(idx + 1, (g * src_a + @bytes.getbyte(idx + 1) * inv).to_i)
    @bytes.setbyte(idx + 2, (b * src_a + @bytes.getbyte(idx + 2) * inv).to_i)
  end
end