Class: UnicodePlot::BrailleCanvas

Inherits:
Canvas
  • Object
show all
Defined in:
lib/unicode_plot/braille_canvas.rb

Constant Summary collapse

X_PIXEL_PER_CHAR =
2
Y_PIXEL_PER_CHAR =
4
BRAILLE_SIGNS =
[
  [
    0x2801,
    0x2802,
    0x2804,
    0x2840,
  ].freeze,
  [
    0x2808,
    0x2810,
    0x2820,
    0x2880
  ].freeze
].freeze

Constants included from StyledPrinter

StyledPrinter::COLOR_DECODE, StyledPrinter::COLOR_ENCODE, StyledPrinter::DISABLE_TEXT_STYLE, StyledPrinter::TEXT_COLORS

Instance Attribute Summary

Attributes inherited from Canvas

#height, #origin_x, #origin_y, #pixel_height, #pixel_width, #plot_height, #plot_width, #width, #x_pixel_per_char, #y_pixel_per_char

Instance Method Summary collapse

Methods inherited from Canvas

#char_at, #color_at, create, #index_at, #line!, #lines!, #point!, #points!, #print, #show

Methods included from BorderPrinter

#print_border_bottom, #print_border_top

Methods included from StyledPrinter

#color?, #print_color, #print_styled

Constructor Details

#initialize(width, height, **kw) ⇒ BrailleCanvas

Returns a new instance of BrailleCanvas.



21
22
23
24
25
26
27
28
29
# File 'lib/unicode_plot/braille_canvas.rb', line 21

def initialize(width, height, **kw)
  super(width, height,
        width * X_PIXEL_PER_CHAR,
        height * Y_PIXEL_PER_CHAR,
        "\u{2800}",
        x_pixel_per_char: X_PIXEL_PER_CHAR,
        y_pixel_per_char: Y_PIXEL_PER_CHAR,
        **kw)
end

Instance Method Details

#pixel!(pixel_x, pixel_y, color) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/unicode_plot/braille_canvas.rb', line 31

def pixel!(pixel_x, pixel_y, color)
  unless 0 <= pixel_x && pixel_x <= pixel_width &&
         0 <= pixel_y && pixel_y <= pixel_height
    return color
  end
  pixel_x -= 1 unless pixel_x < pixel_width
  pixel_y -= 1 unless pixel_y < pixel_height
  tx = pixel_x.fdiv(pixel_width) * width
  char_x = tx.floor + 1
  char_x_off = pixel_x % X_PIXEL_PER_CHAR + 1
  char_x += 1 if char_x < tx.round + 1 && char_x_off == 1

  char_y = (pixel_y.fdiv(pixel_height) * height).floor + 1
  char_y_off = pixel_y % Y_PIXEL_PER_CHAR + 1

  index = index_at(char_x - 1, char_y - 1)
  if index
    @grid[index] = (@grid[index].ord | BRAILLE_SIGNS[char_x_off - 1][char_y_off - 1]).chr(Encoding::UTF_8)
    @colors[index] |= COLOR_ENCODE[color]
  end
  color
end


54
55
56
57
58
59
60
61
62
63
# File 'lib/unicode_plot/braille_canvas.rb', line 54

def print_row(out, row_index)
  unless 0 <= row_index && row_index < height
    $stderr.puts [row_index, height].inspect
    raise ArgumentError, "row_index out of bounds"
  end
  y = row_index
  (0 ... width).each do |x|
    print_color(out, color_at(x, y), char_at(x, y))
  end
end