Practice/Stripe/Bit Font Renderer
CodingMust
You are building a text rendering system that displays characters using bitmap fonts. In a bitmap font, each character is represented as a grid of pixels, where each pixel is either "on" (filled) or "off" (empty).
The problem is divided into three progressive parts:
. for empty pixels and # for filled pixelsImplement a function render_character(grid) that takes a binary grid (list of strings containing 0s and 1s) and returns the rendered output where:
0 is replaced with . (empty pixel)1 is replaced with # (filled pixel)Each character is represented as a list of strings, where each string is a row of the character grid containing only 0 and 1.
`
letter_j = [ "0000000", "0001000", "0001000", "0101000", "0010000" ] `
` grid = [ "0000000", "0001000", "0001000", "0101000", "0010000" ]
render_character(grid)
`
When printed line by line:
....... ...#... ...#... .#.#... ..#....
0 and 1 characters will appear in the inputExtend your solution to implement render_word(text, font) that renders multiple characters side by side to form a word or sentence. Each character's columns should be concatenated horizontally.
The font is a dictionary with a "face" name and a "chars" mapping from character to its bitmap representation:
font = { "face": "Simple Font", "chars": { "H": ["10001", "10001", "11111", "10001", "10001"], "I": ["111", "010", "010", "010", "111"] } }
` font = { "face": "Simple Font", "chars": { "H": ["10001", "10001", "11111", "10001", "10001"], "I": ["111", "010", "010", "010", "111"] } }
render_word("HI", font)
`
When printed:
#...#### #...#.#. #####.#. #...#.#. #...####
Some fonts use run-length encoding (RLE) for compact storage. Instead of storing each pixel individually, RLE stores the length of consecutive runs of the same value.
Implement decode_rle(encoded_rows) that decodes RLE-encoded character data.
0) and "on" (1) pixels, starting with "off"0-9 represent run lengths 0-9a-z represent run lengths 10-35 (where a=10, b=11, ..., z=35)`
encoded_char = [ "532", # 5 off, 3 on, 2 off -> "0000011100" "343", # 3 off, 4 on, 3 off -> "0001111000" "a0", # 10 off, 0 on -> "0000000000" "19", # 1 off, 9 on -> "0111111111" ]
decode_rle(encoded_char)
`
After rendering with render_character:
.....###.. ...####... .......... .#########
Once decode_rle is implemented, you can combine it with the earlier functions to render words from RLE-encoded fonts:
` rle_font = { "face": "Compressed Font", "encoding": "rle", "chars": { "A": ["212", "11111", "05", "0131", "0131"] # "212" -> ..#.. (2 off, 1 on, 2 off) # "11111" -> .#.#. (1 off, 1 on, 1 off, 1 on, 1 off) # "05" -> ##### (0 off, 5 on) # "0131" -> #...# (0 off, 1 on, 3 off, 1 on) } }
decoded = decode_rle(rle_font["chars"]["A"]) rendered = render_character(decoded)
`
0) pixels0-9 represent their numeric valuea-z represent values 10-35