module BannerFont
Constants
- CAP_GLYPHS
- CAP_HEIGHT
- CHAR_FULL
- CHAR_LOWER
- CHAR_UPPER
- FILL
- GAP
- GLYPHS
- HEIGHT
- PIXEL_ROWS
- WIDTH
Public Instance Methods
Source
# File lib/banner_font.rb, line 131 def align_rows(rows, width) rows.map { |row| TextAlign.pad(row, width, fill: FILL) } end
Source
# File lib/banner_font.rb, line 47 def decode_irc_row(row, width: nil) w = width || glyph_row_width([row]) top = [] bottom = [] normalize_glyph_row(row, w).each_char do |char| case char when FILL then top << 0; bottom << 0 when CHAR_UPPER then top << 1; bottom << 0 when CHAR_LOWER then top << 0; bottom << 1 when CHAR_FULL then top << 1; bottom << 1 else raise ArgumentError, "Glyph rows may only use █▀▄. and spaces." end end [top, bottom] end
Source
# File lib/banner_font.rb, line 87 def empty_ascender_row(width = WIDTH) Array.new(width, 0) end
Source
# File lib/banner_font.rb, line 36 def encode_irc_row(top, bottom) top.zip(bottom).map do |t, b| case [t.to_i, b.to_i] when [0, 0] then FILL when [1, 0] then CHAR_UPPER when [0, 1] then CHAR_LOWER when [1, 1] then CHAR_FULL end end.join end
Source
# File lib/banner_font.rb, line 99 def ensure_height(rows, width: nil) w = width || glyph_row_width(rows) list = Array(rows).map { |row| normalize_glyph_row(row, w) } return expand_cap_glyph(list, width: w) if list.length == CAP_HEIGHT raise ArgumentError, "Glyph needs exactly #{HEIGHT} rows." unless list.length == HEIGHT list end
Source
# File lib/banner_font.rb, line 91 def expand_cap_glyph(rows, width: nil) w = width || glyph_row_width(rows) cap_rows = Array(rows).first(CAP_HEIGHT).map { |row| normalize_glyph_row(row, w) } pixels = glyph_to_pixels(cap_rows, width: w) pixels.unshift(empty_ascender_row(w), empty_ascender_row(w).dup) pixels_to_glyph(pixels, width: w) end
Source
# File lib/banner_font.rb, line 114 def expected_line_width(text, glyphs: GLYPHS) chars = text.to_s.upcase.chars return 0 if chars.empty? total = chars.sum do |char| glyph = glyphs[char] || glyphs[" "] || GLYPHS[char] || GLYPHS[" "] glyph_width(glyph) end total + (chars.length - 1) * GAP.length end
Source
# File lib/banner_font.rb, line 125 def expected_width(char_count) return 0 if char_count.to_i <= 0 char_count * (WIDTH + GAP.length) - GAP.length end
Source
# File lib/banner_font.rb, line 32 def glyph_row_width(rows) glyph_width(rows) end
Source
# File lib/banner_font.rb, line 64 def glyph_to_pixels(rows, width: nil) pixels = [] w = width || glyph_row_width(rows) Array(rows).each do |row| top, bottom = decode_irc_row(row, width: w) pixels << top pixels << bottom end pixels end
Source
# File lib/banner_font.rb, line 28 def glyph_width(glyph) Array(glyph).map(&:length).max.to_i end
Source
# File lib/banner_font.rb, line 24 def normalize_glyph_row(row, width = WIDTH) TextAlign.pad(row.to_s.tr(" ", FILL), width, fill: FILL)[0, width] end
Source
# File lib/banner_font.rb, line 75 def pixels_to_glyph(grid, width: nil) raise ArgumentError, "expected #{PIXEL_ROWS} pixel rows" unless grid.length == PIXEL_ROWS w = width || (grid.first&.length || WIDTH) [ encode_irc_row(grid[0], grid[1]), encode_irc_row(grid[2], grid[3]), encode_irc_row(grid[4], grid[5]), encode_irc_row(grid[6], grid[7]) ].map { |row| normalize_glyph_row(row, w) } end
Source
# File lib/banner_font.rb, line 135 def render(text, glyphs: GLYPHS) chars = text.to_s.upcase.chars return Array.new(HEIGHT, "") if chars.empty? rows = Array.new(HEIGHT) { +"" } chars.each_with_index do |char, index| glyph = glyphs[char] || glyphs[" "] || GLYPHS[char] || GLYPHS[" "] w = glyph_width(glyph) glyph.each_with_index do |row, row_index| rows[row_index] << row[0, w] rows[row_index] << GAP unless index == chars.length - 1 end end align_rows(rows, expected_line_width(text, glyphs: glyphs)) end