Skip to content

Commit 008df98

Browse files
committed
Add dot canvas support
1 parent 3bd0da9 commit 008df98

12 files changed

Lines changed: 142 additions & 44 deletions

lib/unicode_plot.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
require 'unicode_plot/renderer'
77

88
require 'unicode_plot/canvas'
9-
require 'unicode_plot/ascii_canvas'
109
require 'unicode_plot/braille_canvas'
1110
require 'unicode_plot/density_canvas'
11+
require 'unicode_plot/lookup_canvas'
12+
require 'unicode_plot/ascii_canvas'
13+
require 'unicode_plot/dot_canvas'
1214

1315
require 'unicode_plot/plot'
1416
require 'unicode_plot/grid_plot'

lib/unicode_plot/ascii_canvas.rb

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module UnicodePlot
2-
class AsciiCanvas < Canvas
2+
class AsciiCanvas < LookupCanvas
33
ASCII_SIGNS = [
44
[ 0b100_000_000, 0b000_100_000, 0b000_000_100 ].freeze,
55
[ 0b010_000_000, 0b000_010_000, 0b000_000_010 ].freeze,
@@ -105,47 +105,10 @@ class AsciiCanvas < Canvas
105105

106106
def initialize(width, height, **kw)
107107
super(width, height,
108-
width * PIXEL_PER_CHAR,
109-
height * PIXEL_PER_CHAR,
110-
0,
111-
x_pixel_per_char: PIXEL_PER_CHAR,
112-
y_pixel_per_char: PIXEL_PER_CHAR,
108+
PIXEL_PER_CHAR, PIXEL_PER_CHAR,
113109
**kw)
114110
end
115111

116-
def pixel!(pixel_x, pixel_y, color)
117-
unless 0 <= pixel_x && pixel_x <= pixel_width &&
118-
0 <= pixel_y && pixel_y <= pixel_height
119-
return color
120-
end
121-
pixel_x -= 1 unless pixel_x < pixel_width
122-
pixel_y -= 1 unless pixel_y < pixel_height
123-
124-
tx = pixel_x.fdiv(pixel_width) * width
125-
char_x = tx.floor + 1
126-
char_x_off = pixel_x % PIXEL_PER_CHAR + 1
127-
char_x += 1 if char_x < tx.round + 1 && char_x_off == 1
128-
129-
char_y = (pixel_y.fdiv(pixel_height) * height).floor + 1
130-
char_y_off = pixel_y % PIXEL_PER_CHAR + 1
131-
132-
index = index_at(char_x - 1, char_y - 1)
133-
if index
134-
@grid[index] |= lookup_encode(char_x_off - 1, char_y_off - 1)
135-
@colors[index] |= COLOR_ENCODE[color]
136-
end
137-
end
138-
139-
def print_row(out, row_index)
140-
unless 0 <= row_index && row_index < height
141-
raise ArgumentError, "row_index out of bounds"
142-
end
143-
y = row_index
144-
(0 ... width).each do |x|
145-
print_color(out, color_at(x, y), lookup_decode(char_at(x, y)))
146-
end
147-
end
148-
149112
def lookup_encode(x, y)
150113
ASCII_SIGNS[x][y]
151114
end

lib/unicode_plot/canvas.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ def self.create(canvas_type, width, height, **kw)
1010
BrailleCanvas.new(width, height, **kw)
1111
when :density
1212
DensityCanvas.new(width, height, **kw)
13+
when :dot
14+
DotCanvas.new(width, height, **kw)
1315
else
1416
raise ArgumentError, "unknown canvas type: #{canvas_type}"
1517
end

lib/unicode_plot/dot_canvas.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
module UnicodePlot
2+
class DotCanvas < LookupCanvas
3+
DOT_SIGNS = [
4+
[
5+
0b10,
6+
0b01
7+
].freeze
8+
].freeze
9+
10+
DOT_DECODE = [
11+
-' ', # 0b00
12+
-'.', # 0b01
13+
-"'", # 0b10
14+
-':', # 0b11
15+
].freeze
16+
17+
X_PIXEL_PER_CHAR = 1
18+
Y_PIXEL_PER_CHAR = 2
19+
20+
def initialize(width, height, **kw)
21+
super(width, height,
22+
X_PIXEL_PER_CHAR, Y_PIXEL_PER_CHAR,
23+
**kw)
24+
end
25+
26+
def lookup_encode(x, y)
27+
DOT_SIGNS[x][y]
28+
end
29+
30+
def lookup_decode(code)
31+
DOT_DECODE[code]
32+
end
33+
end
34+
end

lib/unicode_plot/lookup_canvas.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module UnicodePlot
2+
class LookupCanvas < Canvas
3+
def initialize(width, height, x_pixel_per_char, y_pixel_per_char, fill_char=0, **kw)
4+
super(width, height,
5+
width * x_pixel_per_char,
6+
height * y_pixel_per_char,
7+
fill_char,
8+
x_pixel_per_char: x_pixel_per_char,
9+
y_pixel_per_char: y_pixel_per_char,
10+
**kw)
11+
end
12+
13+
def pixel!(pixel_x, pixel_y, color)
14+
unless 0 <= pixel_x && pixel_x <= pixel_width &&
15+
0 <= pixel_y && pixel_y <= pixel_height
16+
return color
17+
end
18+
pixel_x -= 1 unless pixel_x < pixel_width
19+
pixel_y -= 1 unless pixel_y < pixel_height
20+
21+
tx = pixel_x.fdiv(pixel_width) * width
22+
char_x = tx.floor + 1
23+
char_x_off = pixel_x % x_pixel_per_char + 1
24+
char_x += 1 if char_x < tx.round + 1 && char_x_off == 1
25+
26+
char_y = (pixel_y.fdiv(pixel_height) * height).floor + 1
27+
char_y_off = pixel_y % y_pixel_per_char + 1
28+
29+
index = index_at(char_x - 1, char_y - 1)
30+
if index
31+
@grid[index] |= lookup_encode(char_x_off - 1, char_y_off - 1)
32+
@colors[index] |= COLOR_ENCODE[color]
33+
end
34+
end
35+
36+
def print_row(out, row_index)
37+
unless 0 <= row_index && row_index < height
38+
raise ArgumentError, "row_index out of bounds"
39+
end
40+
y = row_index
41+
(0 ... width).each do |x|
42+
print_color(out, color_at(x, y), lookup_decode(char_at(x, y)))
43+
end
44+
end
45+
end
46+
end

test/fixtures/canvas/dot_print.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
:.  '         '   .  '   .  '      ' :.'
2+
: '.               .        .    ..''  
3+
: ' '.   ' : :      .    '   ..''   '  
4+
:   ''':':''''''''''.......::...:...'' 
5+
:. '    ':           ..''   .   .'  '  
6+
:   .    .'.   ' ..:' .       .   .''  
7+
:      .    '..''   '      '     .     .
8+
: ' .    ..'' '.    .        ..        
9+
:  . ..''      ''.   '.         .'    '
10+
::.''  .          '..  ''.        . .  
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
:. ' ' . ' . ' ' :.'
2+
: '. . . ..''
3+
: ' '. ' : : . ' ..'' '
4+
: ''':':''''''''''.......::...:...''
5+
:. ' ': ..'' . .' '
6+
: . .'. ' ..:' . . .''
7+
: . '..'' ' ' . .
8+
: ' . ..'' '. . ..
9+
: . ..'' ''. '. .' '
10+
::.'' . '.. ''. . .
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
: ' '.   ' : :      .    '   ..''   '  

test/fixtures/canvas/dot_show.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
┌────────────────────────────────────────┐
2+
│:.  '         '   .  '   .  '      ' :.'│
3+
│: '.               .        .    ..''   │
4+
│: ' '.   ' : :      .    '   ..''   '   │
5+
│:   ''':':''''''''''.......::...:...''  │
6+
│:. '    ':           ..''   .   .'  '   │
7+
│:   .    .'.   ' ..:' .       .   .''   │
8+
│:      .    '..''   '      '     .     .│
9+
│: ' .    ..'' '.    .        ..         │
10+
│:  . ..''      ''.   '.         .'    ' │
11+
│::.''  .          '..  ''.        . .   │
12+
└────────────────────────────────────────┘
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
┌────────────────────────────────────────┐
2+
│:. ' ' . ' . ' ' :.'│
3+
│: '. . . ..'' │
4+
│: ' '. ' : : . ' ..'' ' │
5+
│: ''':':''''''''''.......::...:...'' │
6+
│:. ' ': ..'' . .' ' │
7+
│: . .'. ' ..:' . . .'' │
8+
│: . '..'' ' ' . .│
9+
│: ' . ..'' '. . .. │
10+
│: . ..'' ''. '. .' ' │
11+
│::.'' . '.. ''. . . │
12+
└────────────────────────────────────────┘

0 commit comments

Comments
 (0)