2021-01-10 21:14:41 +03:00
|
|
|
module ttf
|
2021-01-26 17:43:10 +03:00
|
|
|
|
2021-01-10 21:14:41 +03:00
|
|
|
/**********************************************************************
|
|
|
|
*
|
|
|
|
* Common data for the module
|
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*
|
2021-03-01 02:18:14 +03:00
|
|
|
* TODO:
|
2021-01-10 21:14:41 +03:00
|
|
|
**********************************************************************/
|
|
|
|
import os
|
|
|
|
import math
|
|
|
|
|
|
|
|
// text align
|
2021-01-26 17:43:10 +03:00
|
|
|
pub enum Text_align {
|
2021-01-10 21:14:41 +03:00
|
|
|
left
|
|
|
|
center
|
|
|
|
right
|
|
|
|
justify
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw style
|
2021-01-26 17:43:10 +03:00
|
|
|
pub enum Style {
|
2021-01-10 21:14:41 +03:00
|
|
|
outline
|
|
|
|
outline_aliased
|
|
|
|
filled
|
|
|
|
raw
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* DEBUG Utility
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
const debug_flag = false
|
2021-01-26 17:43:10 +03:00
|
|
|
|
|
|
|
fn dprintln(txt string) {
|
|
|
|
if ttf.debug_flag {
|
2021-01-10 21:14:41 +03:00
|
|
|
println(txt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Utility
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2021-01-17 05:11:47 +03:00
|
|
|
// transform the bitmap from one layer to color layers
|
2021-01-26 17:43:10 +03:00
|
|
|
fn (mut bmp BitMap) format_texture() {
|
2021-01-17 05:11:47 +03:00
|
|
|
r := byte(bmp.color >> 24)
|
|
|
|
g := byte((bmp.color >> 16) & 0xFF)
|
2021-01-26 17:43:10 +03:00
|
|
|
b := byte((bmp.color >> 8) & 0xFF)
|
2021-01-17 05:11:47 +03:00
|
|
|
a := byte(bmp.color & 0xFF)
|
|
|
|
|
|
|
|
b_r := byte(bmp.bg_color >> 24)
|
|
|
|
b_g := byte((bmp.bg_color >> 16) & 0xFF)
|
2021-01-26 17:43:10 +03:00
|
|
|
b_b := byte((bmp.bg_color >> 8) & 0xFF)
|
2021-01-17 05:11:47 +03:00
|
|
|
b_a := byte(bmp.bg_color & 0xFF)
|
2021-01-26 17:43:10 +03:00
|
|
|
|
2021-01-17 05:11:47 +03:00
|
|
|
// trasform buffer in a texture
|
|
|
|
x := byteptr(bmp.buf)
|
2021-01-26 17:43:10 +03:00
|
|
|
unsafe {
|
2021-01-17 05:11:47 +03:00
|
|
|
mut i := 0
|
2021-01-26 17:43:10 +03:00
|
|
|
for i < bmp.buf_size {
|
2021-01-17 05:11:47 +03:00
|
|
|
data := x[i]
|
2021-01-26 17:43:10 +03:00
|
|
|
if data > 0 {
|
|
|
|
x[i + 0] = r
|
|
|
|
x[i + 1] = g
|
|
|
|
x[i + 2] = b
|
2021-01-17 05:11:47 +03:00
|
|
|
// alpha
|
2021-01-26 17:43:10 +03:00
|
|
|
x[i + 3] = byte((a * data) >> 8)
|
2021-01-17 05:11:47 +03:00
|
|
|
} else {
|
2021-01-26 17:43:10 +03:00
|
|
|
x[i + 0] = b_r
|
|
|
|
x[i + 1] = b_g
|
|
|
|
x[i + 2] = b_b
|
|
|
|
x[i + 3] = b_a
|
2021-01-17 05:11:47 +03:00
|
|
|
}
|
|
|
|
i += 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 21:14:41 +03:00
|
|
|
// write out a .ppm file
|
2021-01-26 17:43:10 +03:00
|
|
|
pub fn (mut bmp BitMap) save_as_ppm(file_name string) {
|
2021-01-17 05:11:47 +03:00
|
|
|
tmp_buf := bmp.buf
|
2021-02-14 21:31:42 +03:00
|
|
|
mut buf := unsafe {malloc(bmp.buf_size)}
|
2021-01-17 05:11:47 +03:00
|
|
|
unsafe { C.memcpy(buf, tmp_buf, bmp.buf_size) }
|
|
|
|
bmp.buf = buf
|
2021-01-26 17:43:10 +03:00
|
|
|
|
2021-01-17 05:11:47 +03:00
|
|
|
bmp.format_texture()
|
2021-01-10 21:14:41 +03:00
|
|
|
npixels := bmp.width * bmp.height
|
2021-03-01 02:18:14 +03:00
|
|
|
mut f_out := os.create(file_name) or { panic(err) }
|
|
|
|
f_out.writeln('P3') or { panic(err) }
|
|
|
|
f_out.writeln('$bmp.width $bmp.height') or { panic(err) }
|
|
|
|
f_out.writeln('255') or { panic(err) }
|
2021-01-26 17:43:10 +03:00
|
|
|
for i in 0 .. npixels {
|
2021-01-10 21:14:41 +03:00
|
|
|
pos := i * bmp.bp
|
|
|
|
unsafe {
|
2021-01-17 05:11:47 +03:00
|
|
|
c_r := bmp.buf[pos]
|
2021-01-26 17:43:10 +03:00
|
|
|
c_g := bmp.buf[pos + 1]
|
2021-01-17 05:11:47 +03:00
|
|
|
c_b := bmp.buf[pos + 2]
|
2021-03-01 02:18:14 +03:00
|
|
|
f_out.write_str('$c_r $c_g $c_b ') or { panic(err) }
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f_out.close()
|
2021-01-26 17:43:10 +03:00
|
|
|
|
2021-02-14 21:31:42 +03:00
|
|
|
unsafe {
|
|
|
|
free(buf)
|
|
|
|
}
|
2021-01-17 05:11:47 +03:00
|
|
|
bmp.buf = tmp_buf
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
pub fn (mut bmp BitMap) get_raw_bytes() []byte {
|
|
|
|
mut f_buf := []byte{len: bmp.buf_size / 4}
|
|
|
|
mut i := 0
|
2021-01-10 21:14:41 +03:00
|
|
|
for i < bmp.buf_size {
|
2021-01-26 17:43:10 +03:00
|
|
|
unsafe {
|
|
|
|
f_buf[i >> 2] = *(bmp.buf + i)
|
|
|
|
}
|
2021-01-10 21:14:41 +03:00
|
|
|
i += 4
|
|
|
|
}
|
|
|
|
return f_buf
|
|
|
|
}
|
|
|
|
|
2021-01-26 17:43:10 +03:00
|
|
|
pub fn (mut bmp BitMap) save_raw_data(file_name string) {
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file_array(file_name, bmp.get_raw_bytes()) or { panic(err) }
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Math functions
|
|
|
|
//
|
|
|
|
// integer part of x
|
|
|
|
[inline]
|
|
|
|
fn ipart(x f32) f32 {
|
2021-01-12 02:49:58 +03:00
|
|
|
return f32(math.floor(x))
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn round(x f32) f32 {
|
2021-01-12 02:49:58 +03:00
|
|
|
return ipart(x + 0.5)
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// fractional part of x
|
|
|
|
[inline]
|
|
|
|
fn fpart(x f32) f32 {
|
2021-01-12 02:49:58 +03:00
|
|
|
return x - f32(math.floor(x))
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
|
|
|
fn rfpart(x f32) f32 {
|
2021-01-12 02:49:58 +03:00
|
|
|
return 1 - fpart(x)
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Colors
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
/*
|
|
|
|
[inline]
|
2021-01-26 17:43:10 +03:00
|
|
|
pub fn (mut dev BitMap) get_color(x int, y int) (int, int, int, int){
|
2021-01-10 21:14:41 +03:00
|
|
|
if x < 0 || x >= dev.width || y < 0 || y >= dev.height {
|
|
|
|
return 0,0,0,0
|
|
|
|
}
|
|
|
|
mut i := (x + y * dev.width)*dev.bp
|
|
|
|
unsafe{
|
|
|
|
return dev.buf[i], dev.buf[i+1], dev.buf[i+2], dev.buf[i+3]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-01-26 17:43:10 +03:00
|
|
|
pub fn (mut dev BitMap) get_color_u32(x int, y int) u32{
|
2021-01-10 21:14:41 +03:00
|
|
|
r, g, b, a := dev.get_color(x, y)
|
|
|
|
unsafe{
|
|
|
|
return u32(r<<24) | u32(g<<16) | u32(b<<8) | u32(a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Drawing
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
[inline]
|
2021-01-26 17:43:10 +03:00
|
|
|
pub fn color_multiply_alpha(c u32, level f32) u32 {
|
|
|
|
return u32(f32(c & 0xFF) * level)
|
2021-01-10 21:14:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
[inline]
|
2021-01-26 17:43:10 +03:00
|
|
|
pub fn color_multiply(c u32, level f32) u32 {
|
|
|
|
mut r := (f32((c >> 24) & 0xFF) / 255.0) * level
|
|
|
|
mut g := (f32((c >> 16) & 0xFF) / 255.0) * level
|
|
|
|
mut b := (f32((c >> 8) & 0xFF) / 255.0) * level
|
|
|
|
mut a := (f32(c & 0xFF) / 255.0) * level
|
2021-01-10 21:14:41 +03:00
|
|
|
r = if r > 1.0 { 1.0 } else { r }
|
|
|
|
g = if g > 1.0 { 1.0 } else { g }
|
|
|
|
b = if b > 1.0 { 1.0 } else { b }
|
|
|
|
a = if a > 1.0 { 1.0 } else { a }
|
|
|
|
|
|
|
|
return (u32(r * 255) << 24) | (u32(g * 255) << 16) | (u32(b * 255) << 8) | u32(a * 255)
|
2021-01-23 12:25:40 +03:00
|
|
|
}
|