243 lines
86 KiB
V
243 lines
86 KiB
V
|
/**********************************************************************
|
||
|
*
|
||
|
* BMP render module utility functions
|
||
|
*
|
||
|
* Copyright (c) 2021 Dario Deledda. All rights reserved.
|
||
|
* Use of this source code is governed by an MIT license
|
||
|
* that can be found in the LICENSE file.
|
||
|
*
|
||
|
* Note:
|
||
|
*
|
||
|
* TODO:
|
||
|
* - manage text directions R to L
|
||
|
**********************************************************************/
|
||
|
import ttf
|
||
|
import os
|
||
|
import strings
|
||
|
|
||
|
const (
|
||
|
font_path = "Qarmic_sans_Abridged.ttf"
|
||
|
create_data = false // use true to generate binary data for this test file
|
||
|
)
|
||
|
|
||
|
fn save_raw_data_as_array(buf_bin []byte, file_name string) {
|
||
|
mut buf := strings.new_builder(buf_bin.len * 5)
|
||
|
for x in buf_bin {
|
||
|
buf.write("0x${x:02x},")
|
||
|
}
|
||
|
os.write_file_array(file_name, buf.buf)
|
||
|
}
|
||
|
|
||
|
fn test_main() {
|
||
|
mut tf := ttf.TTF_File{}
|
||
|
if create_data == true {
|
||
|
tf.buf = os.read_bytes(font_path) or { panic(err) }
|
||
|
println("TrueTypeFont file [$font_path] len: ${tf.buf.len}")
|
||
|
save_raw_data_as_array(tf.buf, "test_ttf_Font_arr.bin")
|
||
|
} else {
|
||
|
tf.buf = font_bytes
|
||
|
}
|
||
|
tf.init()
|
||
|
//println("Unit per EM: $tf.units_per_em")
|
||
|
|
||
|
w := 64
|
||
|
h := 32
|
||
|
bp := 4
|
||
|
sz := w * h* bp
|
||
|
|
||
|
font_size := 20
|
||
|
device_dpi := 72
|
||
|
scale := f32(font_size * device_dpi) / f32(72 * tf.units_per_em)
|
||
|
|
||
|
mut bmp := ttf.BitMap{
|
||
|
tf : &tf
|
||
|
buf : malloc(sz)
|
||
|
buf_size : sz
|
||
|
scale : scale
|
||
|
width : w
|
||
|
height : h
|
||
|
}
|
||
|
|
||
|
y_base := int((tf.y_max - tf.y_min) * bmp.scale)
|
||
|
bmp.clear()
|
||
|
bmp.set_pos(0,y_base)
|
||
|
bmp.init_filler()
|
||
|
bmp.draw_text("Test Text")
|
||
|
|
||
|
mut test_buf := get_raw_data(test_data)
|
||
|
if create_data == true {
|
||
|
bmp.save_as_ppm("test_ttf.ppm")
|
||
|
bmp.save_raw_data("test_ttf.bin")
|
||
|
test_buf = os.read_bytes("test_ttf.bin") or { panic(err) }
|
||
|
}
|
||
|
|
||
|
ram_buf := bmp.get_raw_bytes()
|
||
|
assert ram_buf.len == test_buf.len
|
||
|
for i in 0..ram_buf.len {
|
||
|
if test_buf[i] != ram_buf[i] {
|
||
|
assert false
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn get_raw_data(data string) []byte{
|
||
|
mut buf := []byte{}
|
||
|
mut c := 0
|
||
|
mut b := 0
|
||
|
for ch in data {
|
||
|
if ch >= `0` && ch <= `9` {
|
||
|
b = b << 4
|
||
|
b += int(ch - `0`)
|
||
|
c++
|
||
|
} else if ch >= `a` && ch <= `f` {
|
||
|
b = b << 4
|
||
|
b += int(ch - `a` + 10)
|
||
|
c++
|
||
|
}
|
||
|
|
||
|
if c == 2 {
|
||
|
buf << byte(b)
|
||
|
b = 0
|
||
|
c = 0
|
||
|
}
|
||
|
}
|
||
|
return buf
|
||
|
}
|
||
|
|
||
|
const(
|
||
|
test_data ="
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
00bf bfbf bfbf bfbf bfbf bfbf bf00 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
00bf bfbf bfbf bfbf bfbf bfbf bf00 0000
|
||
|
bfff ffff ffff ffff ffff ffff ffbf 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
bfff ffff ffff ffff ffff ffff ffbf 0000
|
||
|
00bf ffff ffbf ffff bfff ffff bf00 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
00bf ffff ffbf ffff bfff ffff bf00 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
bf00 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 00bf
|
||
|
ffbf 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 00bf
|
||
|
ffbf 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 bfbf
|
||
|
ffbf bfbf bf00 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0032 72bf
|
||
|
bfbf 0000 0000 bfbf bfbf 5400 00bf ffff
|
||
|
ffff ffff ffbf 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0000 0032 72bf
|
||
|
0000 0000 00bf ffff bf00 0065 9999 ffff
|
||
|
ffff bf00 00bf ffff ffff ff7f 0000 bfff
|
||
|
bfff bfff bf00 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 0065 9999 6500
|
||
|
0000 0000 00bf ffff bf00 bfff ffff ffbf
|
||
|
ffff ffbf bfff bfff bfbf ffff bf00 bfff
|
||
|
bf00 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 bf72 3300 7fbf
|
||
|
0000 0000 00bf ffff bf7f 5fff ffbf 3f7f
|
||
|
8fbf ffbf ffbf 5500 0000 5fbf 0000 bfff
|
||
|
bf00 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf7f 5fff ffbf 3f7f
|
||
|
0000 0000 00bf ffff bfbf ffbf bfbf ffff
|
||
|
ffff ffbf ffff ff7f 0000 0000 0000 bfff
|
||
|
bf00 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bfbf 00bf bfbf 8f5f
|
||
|
0000 0000 00bf ffff 7f5f ffff ffff ffff
|
||
|
ffff ffbf 5fbf ffff bfbf bfbf 0000 bfff
|
||
|
bf00 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff 7f5f 0000 0000 0000
|
||
|
0000 0000 00bf ffff bfff bfff ffbf ffff
|
||
|
ffff ffbf 0000 5fbf ffff ffff bf00 bfff
|
||
|
bf00 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bfff bfff ffbf ffff
|
||
|
0000 0000 00bf ffff bfff bf00 0000 0000
|
||
|
0000 0000 0000 0000 7f7f ffff bf00 bfff
|
||
|
bf00 0000 bf00 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bfff bf00 0000 0000
|
||
|
0000 0000 00bf ffff bfff bf00 0000 0000
|
||
|
0000 bf00 bf00 0000 0055 bfff ffbf bfff
|
||
|
ff7f 00bf ff5f 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bfff bf00 0000 0000
|
||
|
0000 0000 00bf ffff bfbf ffbf 0000 0055
|
||
|
7fbf ffbf ffbf 7f55 00bf ffff bf00 7f5f
|
||
|
ff7f 7f5f ffbf 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bfbf ffbf 0000 0055
|
||
|
0000 0000 00bf ffff bfbf ffff bfbf bfff
|
||
|
ffff bfbf ffff ffff ffff ffff bf00 00bf
|
||
|
ffff ffff ffbf 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bfbf 0000 bfbf bf7f
|
||
|
0000 0000 00bf ffff bf00 bfff ffff ffff
|
||
|
ffbf 0000 bfbf ffff ffff bfbf 0000 00bf
|
||
|
ffbf ffff bf00 0000 0000 0000 0000 0000
|
||
|
0000 0000 00bf ffff bf00 bf00 0000 3f7f
|
||
|
0000 0000 0000 5fbf 0000 00bf ffbf 8f5f
|
||
|
3f00 0000 0000 5fbf bf5f 0000 0000 0000
|
||
|
0000 bf5f 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 5fbf 0000 00bf ffbf 8f5f
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
0000 0000 0000 0000 0000 0000 0000 0000
|
||
|
"
|
||
|
|
||
|
font_bytes = [
|
||
|
byte(0x00),0x01,0x00,0x00,0x00,0x0c,0x00,0x80,0x00,0x03,0x00,0x40,0x4f,0x53,0x2f,0x32,0x41,0x98,0xb7,0x90,0x00,0x00,0x01,0x48,0x00,0x00,0x00,0x56,0x63,0x6d,0x61,0x70,0x53,0x38,0xf9,0xae,0x00,0x00,0x03,0x60,0x00,0x00,0x02,0xa4,0x67,0x61,0x73,0x70,0xff,0xff,0x00,0x03,0x00,0x00,0x3e,0xf4,0x00,0x00,0x00,0x08,0x67,0x6c,0x79,0x66,0x95,0xf3,0xc8,0x23,0x00,0x00,0x06,0xe8,0x00,0x00,0x2f,0x6c,0x68,0x65,0x61,0x64,0xf1,0x64,0x88,0x06,0x00,0x00,0x00,0xcc,0x00,0x00,0x00,0x36,0x68,0x68,0x65,0x61,0x11,0x4c,0x06,0xc9,0x00,0x00,0x01,0x04,0x00,0x00,0x00,0x24,0x68,0x6d,0x74,0x78,0x03,0x08,0x1c,0x4d,0x00,0x00,0x01,0xa0,0x00,0x00,0x01,0xc0,0x6b,0x65,0x72,0x6e,0xe3,0x4a,0xe3,0x1a,0x00,0x00,0x36,0x54,0x00,0x00,0x02,0x22,0x6c,0x6f,0x63,0x61,0xa1,0xec,0x96,0x76,0x00,0x00,0x06,0x04,0x00,0x00,0x00,0xe2,0x6d,0x61,0x78,0x70,0x00,0x7b,0x00,0x9a,0x00,0x00,0x01,0x28,0x00,0x00,0x00,0x20,0x6e,0x61,0x6d,0x65,0x86,0x7c,0x31,0x7d,0x00,0x00,0x38,0x78,0x00,0x00,0x05,0x5e,0x70,0x6f,0x73,0x74,0x21,0x0a,0xb6,0x4f,0x00,0x00,0x3d,0xd8,0x00,0x00,0x01,0x1a,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x17,0x61,0x7a,0x59,0x5f,0x0f,0x3c,0xf5,0x00,0x0b,0x08,0x00,0x00,0x00,0x00,0x00,0xc4,0x10,0x0e,0x69,0x00,0x00,0x00,0x00,0xc6,0x1e,0x2e,0xbf,0xfe,0xb5,0xfd,0xbd,0x09,0x63,0x08,0x26,0x00,0x00,0x00,0x06,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x09,0x31,0xfd,0xbd,0x00,0x00,0x09,0x62,0xfe,0xb5,0xff,0x39,0x09,0x63,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x01,0x00,0x00,0x00,0x70,0x00,0x68,0x00,0x05,0x00,0x31,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x01,0x00,0x01,0x04,0x34,0x01,0x90,0x00,0x05,0x00,0x08,0x05,0x9a,0x05,0x33,0x00,0x00,0x01,0x1b,0x05,0x9a,0x05,0x33,0x00,0x00,0x03,0xd1,0x00,0x66,0x02,0x12,0x00,0x00,0x02,0x00,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0xa7,0x50,0x00,0x00,0x4a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0x4c,0x20,0x20,0x00,0x40,0x00,0x20,0x22,0x19,0x07,0x47,0xfd,0xe7,0x00,0xcd,0x09,0x31,0x02,0x43,0x20,0x00,0x01,0x11,0x41,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x64,0x00,0x00,0x00,0x00,0x01,0xfc,0x00,0x00,0x03,0x7d,0x00,0x00,0x02,0xb1,0x00,0xce,0x02,0xd7,0x00,0x64,0x05,0x0a,0x00,0x41,0x04,0xf9,0x00,0x45,0x06,0xbd,0x00,0x20,0x07,0x03,0x00,0x92,0x01,0x89,0x00,0x64,0x02,0xaa,0x00,0x7d,0x02,0xaa,0x00,0x51,0x03,0x1d,0x00,0x4c,0x04,0xac,0x00,0xc0,0x02,0x39,0x00,0x9e,0x03,0x14,0x00,0x14,0x01,0xff,0x00,0x9e,0x03,0x3c,0xff,0xf7,0x04,0x73,0x00,0x0d,0x04,0x73,0x00,0xa9,0x04,0x73,0x00,0x28,0x04,0x73,0x00,0x20,0x04,0x73,0x00,0x00,0x04,0x73,0x00,0x35,0x04,0x73,0x00,0x0a,0x04,0x73,0x00,0x1a,0x04,0x73,0x00,0x11,0x04,0x73,0x00,0x21,0x01,0xff,0x00,0x9e,0x01,0xff,0x00,0x9e,0x04,0xac,0x00,0xc9,0x04,0xac,0x00,0xc0,0x04,0xac,0x00,0xc9,0x04,0x73,0x00,0xbc,0x06,0x44,0x00,0x40,0x06,0xa5,0x00,0x45,0x06,0x8f,0x00,0x57,0x06,0x12,0x00,0x3c,0x06,0x4a,0x00,0x34,0x05,0xbd,0x00,0x38,0x04,0xb2,0x00,0x35,0x07,0x0d,0x00,0x1e,0x06,0xde,0x00,0x1d,0x02,0xc6,0x00,0x33,0x06,0x92,0x00,0x51,0x06,0x61,0xff,0xf9,0x05,0xa0,0x00,0x43,0x09,0x26,0x00,0x3d,0x07,0x06,0x00,0x3d,0x07,0xa7,0x00,0x1a,0x06,0x50,0x00,0x3d,0x07,0x93,0x00,0x24,0x06,0x5c,0x00,0x27,0x06,0x28,0x00,0x2a,0x05,0x97,0x00,0x01,0x06,0x3f,0x00,0x16,0x06,0x4c,0x00,0x02,0x09,0x62,0x00,0x02,0x05,0x68,0x00,0x12,0x05,0xe9,0x00,0x21,0x05,0xce,0x00,0x31,0x03,0x20,0x00,0xaa,0x03,0x0b,0x00,0x0f,0x03,0x1a,0x00,0x50,0x04,0x2b,0x00,0x00,0x04,0x73,0xff,0xa6,0x02,0xaa,0x00,0x4a,0x05,0x02,0x00,0x3e,0x05,0x15,0x00,0x4b,0x04,0x49,0x00,0x39,0x05,0x35,0x00,0x34,0x04,0xc0,0x00,0x39,0x02,0x70,0xff,0xc6,0x05,0x02,0x00,0x27,0x04,0xc0,0x00,0x42,0x02,0x73,0x00,0x52,0x02,0x12,0xfe,0xb5,0x04,0x52,0x00,0x3e,0x02,0x3d,0x00,0x38,0x07,0x96,0x00,0x46,0x04,0xd4,0x00,0x46,0x05,0x51,0x00,0x2d,0x05,0x0a,0x00,0x45,0x05,0x43,0x00,0x27,0x03,0x28,0x00,0x33,0x04,0x49,0x00,0x45,0x03,0x94,0x00,0x12,0x04,0xa7,0x00,0x35,0x04,0x22,0x00,0x11,0x06,0x6d,0x00,0x08,0x04,0x22,0x00,0x1c,0x04,0x9e,0x00,0x32,0x03,0xca,0x00,0x32,0x03,0xf6,0x00,0x87,0x02,0x3b,0x00,0x9c,0x04,0x2d,0x00,0x33,0x05,0xc5,0x00,0x07,0x04,0x00,0x01,0x00,0x03,0x2f,0x00,0x16,0x03,0xf6
|
||
|
]
|
||
|
)
|