Skip to content

Commit 9ac4f73

Browse files
nanobowersmrkn
authored andcommitted
adding support for block canvas
1 parent d81bc88 commit 9ac4f73

4 files changed

Lines changed: 50 additions & 0 deletions

File tree

example/ex_canvases.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/env ruby
2+
$LOAD_PATH << "#{__dir__}/../lib"
3+
require "unicode_plot"
4+
5+
# example of line plots using different canvases
6+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Default Lineplot").render
7+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Ascii Lineplot", canvas: :ascii).render
8+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Dot Lineplot", canvas: :dot).render
9+
UnicodePlot.lineplot([1, 2, 7], [9, -6, 8], title: "Block Lineplot", canvas: :block).render

lib/unicode_plot.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
require 'unicode_plot/lookup_canvas'
1414
require 'unicode_plot/ascii_canvas'
1515
require 'unicode_plot/dot_canvas'
16+
require 'unicode_plot/block_canvas'
1617

1718
require 'unicode_plot/plot'
1819
require 'unicode_plot/grid_plot'

lib/unicode_plot/block_canvas.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# coding: utf-8
2+
3+
=begin
4+
The `BlockCanvas` is also Unicode-based.
5+
It has half the resolution of the `BrailleCanvas`.
6+
In contrast to BrailleCanvas, the pixels don't
7+
have visible spacing between them.
8+
This canvas effectively turns every character
9+
into 4 pixels that can individually be manipulated
10+
using binary operations.
11+
=end
12+
13+
module UnicodePlot
14+
class BlockCanvas < LookupCanvas
15+
X_PIXEL_PER_CHAR = 2
16+
Y_PIXEL_PER_CHAR = 2
17+
18+
def initialize(width, height, fill_char=0, **kw)
19+
super(width, height,
20+
X_PIXEL_PER_CHAR,
21+
Y_PIXEL_PER_CHAR,
22+
fill_char,
23+
**kw)
24+
end
25+
26+
BLOCK_SIGNS = [ [0b1000, 0b0010].freeze,
27+
[0b0100, 0b0001].freeze
28+
].freeze
29+
30+
BLOCK_DECODE = [' ', '▗', '▖', '▄',
31+
'▝', '▐', '▞', '▟',
32+
'▘', '▚', '▌', '▙',
33+
'▀', '▜', '▛', '█' ].freeze
34+
35+
def lookup_encode(x,y) ; BLOCK_SIGNS[x][y] ; end
36+
def lookup_decode(x) ; BLOCK_DECODE[x] ; end
37+
end
38+
end

lib/unicode_plot/canvas.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ def self.create(canvas_type, width, height, **kw)
1212
DensityCanvas.new(width, height, **kw)
1313
when :dot
1414
DotCanvas.new(width, height, **kw)
15+
when :block
16+
BlockCanvas.new(width, height, **kw)
1517
else
1618
raise ArgumentError, "unknown canvas type: #{canvas_type}"
1719
end

0 commit comments

Comments
 (0)