| 1 | module fontstash |
| 2 | |
| 3 | #flag -I @VEXEROOT/thirdparty/fontstash |
| 4 | |
| 5 | @[use_once] |
| 6 | #define FONTSTASH_IMPLEMENTATION |
| 7 | $if gcboehm ? { |
| 8 | #define FONTSTASH_MALLOC GC_MALLOC |
| 9 | #define FONTSTASH_REALLOC GC_REALLOC |
| 10 | #define FONTSTASH_FREE GC_FREE |
| 11 | #define FONTSTASH_MALLOC_ATOMIC GC_MALLOC_ATOMIC |
| 12 | } |
| 13 | #include "fontstash.h" |
| 14 | #flag darwin -I/usr/local/Cellar/freetype/2.10.2/include/freetype2 |
| 15 | |
| 16 | $if windows { |
| 17 | $if tinyc { |
| 18 | #flag @VEXEROOT/thirdparty/tcc/lib/openlibm.o |
| 19 | } |
| 20 | } $else { |
| 21 | #flag -lm |
| 22 | } |
| 23 | |
| 24 | pub type Context = C.FONScontext |
| 25 | |
| 26 | //#flag -lfreetype |
| 27 | pub const invalid = C.FONS_INVALID |
| 28 | |
| 29 | // create_internal returns a fontstash Context allocated on the heap. |
| 30 | // |
| 31 | // See also: delete_internal |
| 32 | @[inline] |
| 33 | pub fn create_internal(params &C.FONSparams) &Context { |
| 34 | return C.fonsCreateInternal(params) |
| 35 | } |
| 36 | |
| 37 | // delete_internal deletes and free memory of `s` fontstash Context. |
| 38 | // |
| 39 | // See also: create_internal |
| 40 | @[inline] |
| 41 | pub fn delete_internal(s &Context) { |
| 42 | C.fonsDeleteInternal(s) |
| 43 | } |
| 44 | |
| 45 | // set_error_callback sets `callback` as a function to be called if fontstash |
| 46 | // encounter any errors. `uptr` can be used to pass custom userdata. |
| 47 | @[inline] |
| 48 | pub fn (s &Context) set_error_callback(callback fn (voidptr, int, int), uptr voidptr) { |
| 49 | C.fonsSetErrorCallback(s, callback, uptr) |
| 50 | } |
| 51 | |
| 52 | // get_atlas_size returns the current size of the texture atlas which |
| 53 | // the font is rendered to. |
| 54 | @[inline] |
| 55 | pub fn (s &Context) get_atlas_size() (int, int) { |
| 56 | mut width := 0 |
| 57 | mut height := 0 |
| 58 | C.fonsGetAtlasSize(s, &width, &height) |
| 59 | return width, height |
| 60 | } |
| 61 | |
| 62 | // expand_atlas expands the font texture atlas size to `width` x `height`. |
| 63 | @[inline] |
| 64 | pub fn (s &Context) expand_atlas(width int, height int) int { |
| 65 | return C.fonsExpandAtlas(s, width, height) |
| 66 | } |
| 67 | |
| 68 | // reset_atlas resets `width` x `height` of the font texture atlas. |
| 69 | @[inline] |
| 70 | pub fn (s &Context) reset_atlas(width int, height int) int { |
| 71 | return C.fonsResetAtlas(s, width, height) |
| 72 | } |
| 73 | |
| 74 | // get_font_by_name returns the id of the font with `name` or |
| 75 | // `fontstash.invalid` if no font with `name` could be found. |
| 76 | @[inline] |
| 77 | pub fn (s &Context) get_font_by_name(name string) int { |
| 78 | return C.fonsGetFontByName(s, &char(name.str)) |
| 79 | } |
| 80 | |
| 81 | // add_fallback_font adds a fallback font to the `base` font id in the Context. |
| 82 | // `fallback` is expected to be the id of a previous, successfully, added font. |
| 83 | // add_fallback_font returns `1` on success, `0` otherwise. |
| 84 | @[inline] |
| 85 | pub fn (s &Context) add_fallback_font(base int, fallback int) int { |
| 86 | return C.fonsAddFallbackFont(s, base, fallback) |
| 87 | } |
| 88 | |
| 89 | // add_font_mem adds the font data located in memory to the Context. |
| 90 | // `name` is the human readable name for the font. |
| 91 | // `free_data` indicates if `data` should be freed after the font is added. |
| 92 | // The function returns the id of the font on success, `fontstash.invalid` otherwise. |
| 93 | @[inline] |
| 94 | pub fn (s &Context) add_font_mem(name string, data []u8, free_data bool) int { |
| 95 | return C.fonsAddFontMem(s, &char(name.str), data.data, data.len, int(free_data)) |
| 96 | } |
| 97 | |
| 98 | // push_state pushes a new state on the state stack. |
| 99 | // A state holds the current attributes of the rendering, |
| 100 | // attributes are things like color, size, the font in use, blur effect etc. |
| 101 | // |
| 102 | // See also: pop_state |
| 103 | // See also: clear_state |
| 104 | // See also: set_size |
| 105 | // See also: set_color |
| 106 | // See also: set_spacing |
| 107 | // See also: set_blur |
| 108 | // See also: set_align |
| 109 | // See also: set_font |
| 110 | @[inline] |
| 111 | pub fn (s &Context) push_state() { |
| 112 | C.fonsPushState(s) |
| 113 | } |
| 114 | |
| 115 | // pop_state pops the current state from the state stack. |
| 116 | // |
| 117 | // See also: push_state |
| 118 | // See also: clear_state |
| 119 | @[inline] |
| 120 | pub fn (s &Context) pop_state() { |
| 121 | C.fonsPopState(s) |
| 122 | } |
| 123 | |
| 124 | // clear_state clears the current state. |
| 125 | // |
| 126 | // See also: push_state |
| 127 | // See also: pop_state |
| 128 | @[inline] |
| 129 | pub fn (s &Context) clear_state() { |
| 130 | C.fonsClearState(s) |
| 131 | } |
| 132 | |
| 133 | // set_size sets the font size to `size` on the active state. |
| 134 | // |
| 135 | // See also: push_state |
| 136 | // See also: pop_state |
| 137 | // See also: clear_state |
| 138 | @[inline] |
| 139 | pub fn (s &Context) set_size(size f32) { |
| 140 | C.fonsSetSize(s, size) |
| 141 | } |
| 142 | |
| 143 | // set_color sets the font color to `color` on the active state. |
| 144 | // |
| 145 | // See also: push_state |
| 146 | // See also: pop_state |
| 147 | // See also: clear_state |
| 148 | @[inline] |
| 149 | pub fn (s &Context) set_color(color u32) { |
| 150 | C.fonsSetColor(s, color) |
| 151 | } |
| 152 | |
| 153 | // set_spacing sets the font spacing to `spacing` on the active state. |
| 154 | // |
| 155 | // See also: push_state |
| 156 | // See also: pop_state |
| 157 | // See also: clear_state |
| 158 | @[inline] |
| 159 | pub fn (s &Context) set_spacing(spacing f32) { |
| 160 | C.fonsSetSpacing(s, spacing) |
| 161 | } |
| 162 | |
| 163 | // set_blur sets the font blur effect to `blur` on the active state. |
| 164 | // |
| 165 | // See also: push_state |
| 166 | // See also: pop_state |
| 167 | // See also: clear_state |
| 168 | @[inline] |
| 169 | pub fn (s &Context) set_blur(blur f32) { |
| 170 | C.fonsSetBlur(s, blur) |
| 171 | } |
| 172 | |
| 173 | // set_align sets the font aligning to `align` on the active state. |
| 174 | // |
| 175 | // See also: push_state |
| 176 | // See also: pop_state |
| 177 | // See also: clear_state |
| 178 | @[inline] |
| 179 | pub fn (s &Context) set_align(align int) { |
| 180 | C.fonsSetAlign(s, int(align)) |
| 181 | } |
| 182 | |
| 183 | // set_alignment sets the font aligning to the `align` flags. |
| 184 | // |
| 185 | // See also: push_state |
| 186 | // See also: pop_state |
| 187 | // See also: clear_state |
| 188 | @[inline] |
| 189 | pub fn (s &Context) set_alignment(align Align) { |
| 190 | C.fonsSetAlign(s, int(align)) |
| 191 | } |
| 192 | |
| 193 | // set_font sets the font used for this render on the active state. |
| 194 | // `font_id` is the id of the loaded font. |
| 195 | // |
| 196 | // See also: push_state |
| 197 | // See also: pop_state |
| 198 | // See also: clear_state |
| 199 | @[inline] |
| 200 | pub fn (s &Context) set_font(font_id int) { |
| 201 | C.fonsSetFont(s, font_id) |
| 202 | } |
| 203 | |
| 204 | // draw_text draws the `text` string at position `x`,`y`. |
| 205 | // The function returns the `x` coordinate of the resulting render. |
| 206 | @[inline] |
| 207 | pub fn (s &Context) draw_text(x f32, y f32, text string) f32 { |
| 208 | return C.fonsDrawText(s, x, y, &char(text.str), &char(unsafe { nil })) |
| 209 | } |
| 210 | |
| 211 | // text_bounds fills the `bounds` argument with the pixel dimensions |
| 212 | // of the rendered `text` at position `x`,`y`. |
| 213 | // |
| 214 | // `bounds` is expected to be of type `mut bounds := [4]f32{}`. |
| 215 | // Call example: `ctx.text_bounds(0, 0, 'example', &bounds[0])`. |
| 216 | // `bounds[0]` is the `x` coordinate of the top-left point. |
| 217 | // `bounds[1]` is the `y` coordinate of the top-left point. |
| 218 | // `bounds[2]` is the `x` coordinate of the bottom-right point. |
| 219 | // `bounds[3]` is the `y` coordinate of the bottom-right point. |
| 220 | @[inline] |
| 221 | pub fn (s &Context) text_bounds(x f32, y f32, text string, bounds &f32) f32 { |
| 222 | return C.fonsTextBounds(s, x, y, &char(text.str), &char(unsafe { nil }), bounds) |
| 223 | } |
| 224 | |
| 225 | // line_bounds fills `miny` and `maxy` with the values of the `minimum` |
| 226 | // and `maximum` line bounds respectively. |
| 227 | @[inline] |
| 228 | pub fn (s &Context) line_bounds(y f32, miny &f32, maxy &f32) { |
| 229 | C.fonsLineBounds(s, y, miny, maxy) |
| 230 | } |
| 231 | |
| 232 | // vert_metrics assigns the respective values of `ascender`, `descender` and `lineh`. |
| 233 | @[inline] |
| 234 | pub fn (s &Context) vert_metrics(ascender &f32, descender &f32, lineh &f32) { |
| 235 | C.fonsVertMetrics(s, ascender, descender, lineh) |
| 236 | } |
| 237 | |
| 238 | // text_iter_init initializes the text iterator `iter`. |
| 239 | @[inline] |
| 240 | pub fn (s &Context) text_iter_init(iter &C.FONStextIter, x f32, y f32, str &char, end &char) int { |
| 241 | return C.fonsTextIterInit(s, iter, x, y, str, end) |
| 242 | } |
| 243 | |
| 244 | // text_iter_next advances `iter` to the next `quad`. |
| 245 | @[inline] |
| 246 | pub fn (s &Context) text_iter_next(iter &C.FONStextIter, quad &C.FONSquad) int { |
| 247 | return C.fonsTextIterNext(s, iter, quad) |
| 248 | } |
| 249 | |
| 250 | // get_texture_data returns the current Context's raw texture data. |
| 251 | // `width` and `height` is assigned the size of the texture dimensions. |
| 252 | @[inline] |
| 253 | pub fn (s &Context) get_texture_data(width &int, height &int) &u8 { |
| 254 | return &u8(C.fonsGetTextureData(s, width, height)) |
| 255 | } |
| 256 | |
| 257 | // validate_texture fills the `dirty` argument with the pixel dimensions |
| 258 | // of the dirty rectangle of the Context's raw texture, if any. |
| 259 | // |
| 260 | // `dirty` is expected to be of type `mut dirty := [4]int{}`. |
| 261 | // Call example: `is_dirty := ctx.validate_texture(&dirty[0])`. |
| 262 | // The function returns `1` if the texture has a dirty rectangle, `0` otherwise. |
| 263 | @[inline] |
| 264 | pub fn (s &Context) validate_texture(dirty &int) int { |
| 265 | return C.fonsValidateTexture(s, dirty) |
| 266 | } |
| 267 | |
| 268 | // draw_debug draws the stash texture for debugging. |
| 269 | @[inline] |
| 270 | pub fn (s &Context) draw_debug(x f32, y f32) { |
| 271 | C.fonsDrawDebug(s, x, y) |
| 272 | } |
| 273 | |