| 1 | module ttf |
| 2 | |
| 3 | /********************************************************************** |
| 4 | * |
| 5 | * Common data for the module |
| 6 | * |
| 7 | * Copyright (c) 2021 Dario Deledda. All rights reserved. |
| 8 | * Use of this source code is governed by an MIT license |
| 9 | * that can be found in the LICENSE file. |
| 10 | * |
| 11 | * Note: |
| 12 | * |
| 13 | * TODO: |
| 14 | **********************************************************************/ |
| 15 | import os |
| 16 | import math |
| 17 | |
| 18 | // text align |
| 19 | pub enum Text_align { |
| 20 | left |
| 21 | center |
| 22 | right |
| 23 | justify |
| 24 | } |
| 25 | |
| 26 | // draw style |
| 27 | pub enum Style { |
| 28 | outline |
| 29 | outline_aliased |
| 30 | filled |
| 31 | raw |
| 32 | } |
| 33 | |
| 34 | /****************************************************************************** |
| 35 | * |
| 36 | * DEBUG Utility |
| 37 | * |
| 38 | ******************************************************************************/ |
| 39 | |
| 40 | // dprintln prints the `txt` message, *only* when `-d debug_flag` is passed while compiling a program |
| 41 | @[if debug_flag ?] |
| 42 | pub fn dprintln(txt string) { |
| 43 | println(txt) |
| 44 | } |
| 45 | |
| 46 | /****************************************************************************** |
| 47 | * |
| 48 | * Utility |
| 49 | * |
| 50 | ******************************************************************************/ |
| 51 | // format_texture transforms the bitmap from one layer to color layers |
| 52 | pub fn (mut bmp BitMap) format_texture() { |
| 53 | r := u8(bmp.color >> 24) |
| 54 | g := u8((bmp.color >> 16) & 0xFF) |
| 55 | b := u8((bmp.color >> 8) & 0xFF) |
| 56 | a := u8(bmp.color & 0xFF) |
| 57 | |
| 58 | b_r := u8(bmp.bg_color >> 24) |
| 59 | b_g := u8((bmp.bg_color >> 16) & 0xFF) |
| 60 | b_b := u8((bmp.bg_color >> 8) & 0xFF) |
| 61 | b_a := u8(bmp.bg_color & 0xFF) |
| 62 | |
| 63 | // transform buffer in a texture |
| 64 | x := bmp.buf |
| 65 | unsafe { |
| 66 | mut i := 0 |
| 67 | for i < bmp.buf_size { |
| 68 | data := x[i] |
| 69 | if data > 0 { |
| 70 | x[i + 0] = r |
| 71 | x[i + 1] = g |
| 72 | x[i + 2] = b |
| 73 | // alpha |
| 74 | x[i + 3] = u8(u16(u16(a) * data) >> 8) |
| 75 | } else { |
| 76 | x[i + 0] = b_r |
| 77 | x[i + 1] = b_g |
| 78 | x[i + 2] = b_b |
| 79 | x[i + 3] = b_a |
| 80 | } |
| 81 | i += 4 |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // save_as_ppm saves the `BitMap` data in .ppm file format to `file_name`. |
| 87 | pub fn (mut bmp BitMap) save_as_ppm(file_name string) { |
| 88 | tmp_buf := bmp.buf |
| 89 | mut buf := unsafe { malloc_noscan(bmp.buf_size) } |
| 90 | unsafe { vmemcpy(buf, tmp_buf, bmp.buf_size) } |
| 91 | bmp.buf = buf |
| 92 | |
| 93 | bmp.format_texture() |
| 94 | npixels := bmp.width * bmp.height |
| 95 | mut f_out := os.create(file_name) or { panic(err) } |
| 96 | f_out.writeln('P3') or { panic(err) } |
| 97 | f_out.writeln('${bmp.width} ${bmp.height}') or { panic(err) } |
| 98 | f_out.writeln('255') or { panic(err) } |
| 99 | for i in 0 .. npixels { |
| 100 | pos := i * bmp.bp |
| 101 | unsafe { |
| 102 | c_r := bmp.buf[pos] |
| 103 | c_g := bmp.buf[pos + 1] |
| 104 | c_b := bmp.buf[pos + 2] |
| 105 | f_out.write_string('${c_r} ${c_g} ${c_b} ') or { panic(err) } |
| 106 | } |
| 107 | } |
| 108 | f_out.close() |
| 109 | |
| 110 | unsafe { |
| 111 | free(buf) |
| 112 | } |
| 113 | bmp.buf = tmp_buf |
| 114 | } |
| 115 | |
| 116 | // get_raw_bytes returns the raw bytes of the bitmap. |
| 117 | pub fn (mut bmp BitMap) get_raw_bytes() []u8 { |
| 118 | mut f_buf := []u8{len: bmp.buf_size / 4} |
| 119 | mut i := 0 |
| 120 | for i < bmp.buf_size { |
| 121 | unsafe { |
| 122 | f_buf[i >> 2] = *(bmp.buf + i) |
| 123 | } |
| 124 | i += 4 |
| 125 | } |
| 126 | return f_buf |
| 127 | } |
| 128 | |
| 129 | // save_raw_data saves the raw data to `file_name`. |
| 130 | pub fn (mut bmp BitMap) save_raw_data(file_name string) { |
| 131 | os.write_file_array(file_name, bmp.get_raw_bytes()) or { panic(err) } |
| 132 | } |
| 133 | |
| 134 | // |
| 135 | // Math functions |
| 136 | // |
| 137 | // integer part of x |
| 138 | @[inline] |
| 139 | fn ipart(x f32) f32 { |
| 140 | return f32(math.floor(x)) |
| 141 | } |
| 142 | |
| 143 | @[inline] |
| 144 | fn round(x f32) f32 { |
| 145 | return ipart(x + 0.5) |
| 146 | } |
| 147 | |
| 148 | // fractional part of x |
| 149 | @[inline] |
| 150 | fn fpart(x f32) f32 { |
| 151 | return x - f32(math.floor(x)) |
| 152 | } |
| 153 | |
| 154 | @[inline] |
| 155 | fn rfpart(x f32) f32 { |
| 156 | return 1 - fpart(x) |
| 157 | } |
| 158 | |
| 159 | /****************************************************************************** |
| 160 | * |
| 161 | * Colors |
| 162 | * |
| 163 | ******************************************************************************/ |
| 164 | |
| 165 | // @[inline] |
| 166 | // pub fn (mut dev BitMap) get_color(x int, y int) (int, int, int, int){ |
| 167 | // if x < 0 || x >= dev.width || y < 0 || y >= dev.height { |
| 168 | // return 0,0,0,0 |
| 169 | // } |
| 170 | // mut i := (x + y * dev.width)*dev.bp |
| 171 | // unsafe{ |
| 172 | // return dev.buf[i], dev.buf[i+1], dev.buf[i+2], dev.buf[i+3] |
| 173 | // } |
| 174 | // } |
| 175 | |
| 176 | // @[inline] |
| 177 | // pub fn (mut dev BitMap) get_color_u32(x int, y int) u32{ |
| 178 | // r, g, b, a := dev.get_color(x, y) |
| 179 | // unsafe{ |
| 180 | // return u32(r<<24) | u32(g<<16) | u32(b<<8) | u32(a) |
| 181 | // } |
| 182 | // } |
| 183 | |
| 184 | /****************************************************************************** |
| 185 | * |
| 186 | * Drawing |
| 187 | * |
| 188 | ******************************************************************************/ |
| 189 | |
| 190 | // color_multiply_alpha multiplies color `c`'s alpha channel with the `level` value. |
| 191 | @[inline] |
| 192 | pub fn color_multiply_alpha(c u32, level f32) u32 { |
| 193 | return u32(f32(c & 0xFF) * level) |
| 194 | } |
| 195 | |
| 196 | // color_multiply multiplies R,G,B channels of color `c` with the `level` value. |
| 197 | @[inline] |
| 198 | pub fn color_multiply(c u32, level f32) u32 { |
| 199 | mut r := (f32((c >> 24) & 0xFF) / 255.0) * level |
| 200 | mut g := (f32((c >> 16) & 0xFF) / 255.0) * level |
| 201 | mut b := (f32((c >> 8) & 0xFF) / 255.0) * level |
| 202 | mut a := (f32(c & 0xFF) / 255.0) * level |
| 203 | r = if r > 1.0 { 1.0 } else { r } |
| 204 | g = if g > 1.0 { 1.0 } else { g } |
| 205 | b = if b > 1.0 { 1.0 } else { b } |
| 206 | a = if a > 1.0 { 1.0 } else { a } |
| 207 | |
| 208 | return (u32(r * 255) << 24) | (u32(g * 255) << 16) | (u32(b * 255) << 8) | u32(a * 255) |
| 209 | } |
| 210 | |