Last active
January 31, 2023 11:07
-
-
Save damywise/33f4565cb7fc229d0dcb399f5ddd40ae to your computer and use it in GitHub Desktop.
Not working as expected
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import x.ttf | |
import stbi | |
fn main() { | |
mut ttf_font := ttf.TTF_File{} | |
ttf_font.buf = os.read_bytes('assets/fonts/ALS-Script.ttf') or { panic(err) } | |
ttf_font.init() | |
// print font info | |
println(ttf_font.get_info_string()) | |
bmp_width := 200 | |
bmp_height := 64 | |
bmp_layers := 4 // number of planes for an RGBA buffer | |
// memory size of the buffer | |
bmp_size := bmp_width bmp_height * bmp_layers | |
font_size := 32 // font size in points | |
device_dpi := 72 // default screen DPI | |
// Formula for scale calculation | |
// scaler := (font_size * device dpi) / (72dpi * em_unit) | |
scale := f32(font_size * device_dpi) / f32(72 * ttf_font.units_per_em) | |
// height of the font to use in the buffer to separate the lines | |
y_base := int((ttf_font.y_max - ttf_font.y_min) * scale) | |
// declare the bitmap struct | |
mut bmp := ttf.BitMap{ | |
tf: &ttf_font | |
buf: unsafe { malloc(bmp_size) } | |
buf_size: bmp_size | |
width: bmp_width | |
height: bmp_height | |
bp: bmp_layers | |
color: 0x000000_FF // RGBA black | |
scale: scale | |
} | |
bmp.init_filler() | |
bmp.clear() | |
bmp.set_pos(10, y_base) | |
bmp.draw_text('Test Text!') | |
img := bmp.get_raw_bytes().data | |
stbi.stbi_write_png('test.png', bmp_width, bmp_height, bmp_layers, img, bmp_width * bmp_layers)! | |
stbi.stbi_write_jpg('test.jpg', bmp_width, bmp_height, bmp_layers, bmp.buf, 95)! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment