examples: use `fontstash` wrapper calls (#12718)

pull/12746/head
Larpon 2021-12-06 21:39:43 +01:00 committed by GitHub
parent 1cd703d96b
commit 047f059fb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 145 additions and 143 deletions

View File

@ -75,7 +75,7 @@ fn main() {
width: win_width width: win_width
height: win_height height: win_height
create_window: true create_window: true
window_title: 'Empty window' window_title: 'Raven text'
user_data: app user_data: app
bg_color: bg_color bg_color: bg_color
frame_fn: frame frame_fn: frame

View File

@ -2,13 +2,14 @@ import sokol
import sokol.sapp import sokol.sapp
import sokol.gfx import sokol.gfx
import sokol.sgl import sokol.sgl
import fontstash
import sokol.sfons import sokol.sfons
import os import os
struct AppState { struct AppState {
mut: mut:
pass_action C.sg_pass_action pass_action C.sg_pass_action
fons &C.FONScontext fons &fontstash.Context
font_normal int font_normal int
} }
@ -27,7 +28,7 @@ fn main() {
pass_action.colors[0] = color_action pass_action.colors[0] = color_action
state := &AppState{ state := &AppState{
pass_action: pass_action pass_action: pass_action
fons: &C.FONScontext(0) fons: voidptr(0) // &fontstash.Context(0)
} }
title := 'V Metal/GL Text Rendering' title := 'V Metal/GL Text Rendering'
desc := C.sapp_desc{ desc := C.sapp_desc{
@ -51,8 +52,7 @@ fn init(mut state AppState) {
'RobotoMono-Regular.ttf'))) 'RobotoMono-Regular.ttf')))
{ {
println('loaded font: $bytes.len') println('loaded font: $bytes.len')
state.font_normal = C.fonsAddFontMem(state.fons, c'sans', bytes.data, bytes.len, state.font_normal = state.fons.add_font_mem('sans', bytes, false)
false)
} }
} }
@ -66,96 +66,97 @@ fn frame(user_data voidptr) {
} }
fn (state &AppState) render_font() { fn (state &AppState) render_font() {
mut sx := 0.0 mut sx := f32(0.0)
mut sy := 0.0 mut sy := f32(0.0)
mut dx := 0.0 mut dx := f32(0.0)
mut dy := 0.0 mut dy := f32(0.0)
lh := f32(0.0) lh := f32(0.0)
white := C.sfons_rgba(255, 255, 255, 255) white := sfons.rgba(255, 255, 255, 255)
black := C.sfons_rgba(0, 0, 0, 255) black := sfons.rgba(0, 0, 0, 255)
brown := C.sfons_rgba(192, 128, 0, 128) brown := sfons.rgba(192, 128, 0, 128)
blue := C.sfons_rgba(0, 192, 255, 255) blue := sfons.rgba(0, 192, 255, 255)
state.fons.clear_state()
fons := state.fons
fons.clear_state()
sgl.defaults() sgl.defaults()
sgl.matrix_mode_projection() sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(C.sapp_width()), f32(C.sapp_height()), 0.0, -1.0, 1.0) sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
sx = 0 sx = 0
sy = 50 sy = 50
dx = sx dx = sx
dy = sy dy = sy
state.fons.set_font(state.font_normal) fons.set_font(state.font_normal)
state.fons.set_size(100.0) fons.set_size(100.0)
ascender := f32(0.0) ascender := f32(0.0)
descender := f32(0.0) descender := f32(0.0)
state.fons.vert_metrics(&ascender, &descender, &lh) fons.vert_metrics(&ascender, &descender, &lh)
dx = sx dx = sx
dy += lh dy += lh
C.fonsSetColor(state.fons, white) fons.set_color(white)
dx = C.fonsDrawText(state.fons, dx, dy, c'The quick ', C.NULL) dx = fons.draw_text(dx, dy, 'The quick ')
C.fonsSetFont(state.fons, state.font_normal) fons.set_font(state.font_normal)
C.fonsSetSize(state.fons, 48.0) fons.set_size(48.0)
C.fonsSetColor(state.fons, brown) fons.set_color(brown)
dx = C.fonsDrawText(state.fons, dx, dy, c'brown ', C.NULL) dx = fons.draw_text(dx, dy, 'brown ')
C.fonsSetFont(state.fons, state.font_normal) fons.set_font(state.font_normal)
C.fonsSetSize(state.fons, 24.0) fons.set_size(24.0)
C.fonsSetColor(state.fons, white) fons.set_color(white)
dx = C.fonsDrawText(state.fons, dx, dy, c'fox ', C.NULL) dx = fons.draw_text(dx, dy, 'fox ')
dx = sx dx = sx
dy += lh * 1.2 dy += lh * 1.2
C.fonsSetSize(state.fons, 20.0) fons.set_size(20.0)
C.fonsSetFont(state.fons, state.font_normal) fons.set_font(state.font_normal)
C.fonsSetColor(state.fons, blue) fons.set_color(blue)
C.fonsDrawText(state.fons, dx, dy, c'Now is the time for all good men to come to the aid of the party.', fons.draw_text(dx, dy, 'Now is the time for all good men to come to the aid of the party.')
C.NULL)
dx = 300 dx = 300
dy = 350 dy = 350
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE) fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
C.fonsSetSize(state.fons, 60.0) fons.set_size(60.0)
C.fonsSetFont(state.fons, state.font_normal) fons.set_font(state.font_normal)
C.fonsSetColor(state.fons, white) fons.set_color(white)
C.fonsSetSpacing(state.fons, 5.0) fons.set_spacing(5.0)
C.fonsSetBlur(state.fons, 6.0) fons.set_blur(6.0)
C.fonsDrawText(state.fons, dx, dy, c'Blurry...', C.NULL) fons.draw_text(dx, dy, 'Blurry...')
dx = 300 dx = 300
dy += 50.0 dy += 50.0
C.fonsSetSize(state.fons, 28.0) fons.set_size(28.0)
C.fonsSetFont(state.fons, state.font_normal) fons.set_font(state.font_normal)
C.fonsSetColor(state.fons, white) fons.set_color(white)
C.fonsSetSpacing(state.fons, 0.0) fons.set_spacing(0.0)
C.fonsSetBlur(state.fons, 3.0) fons.set_blur(3.0)
C.fonsDrawText(state.fons, dx, dy + 2, c'DROP SHADOW', C.NULL) fons.draw_text(dx, dy + 2, 'DROP SHADOW')
C.fonsSetColor(state.fons, black) fons.set_color(black)
C.fonsSetBlur(state.fons, 0) fons.set_blur(0)
C.fonsDrawText(state.fons, dx, dy, c'DROP SHADOW', C.NULL) fons.draw_text(dx, dy, 'DROP SHADOW')
C.fonsSetSize(state.fons, 18.0) fons.set_size(18.0)
C.fonsSetFont(state.fons, state.font_normal) fons.set_font(state.font_normal)
C.fonsSetColor(state.fons, white) fons.set_color(white)
dx = 50 dx = 50
dy = 350 dy = 350
line(f32(dx - 10), f32(dy), f32(dx + 250), f32(dy)) line(f32(dx - 10), f32(dy), f32(dx + 250), f32(dy))
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP) fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_TOP)
dx = C.fonsDrawText(state.fons, dx, dy, c'Top', C.NULL) dx = fons.draw_text(dx, dy, 'Top')
dx += 10 dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_MIDDLE) fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_MIDDLE)
dx = C.fonsDrawText(state.fons, dx, dy, c'Middle', C.NULL) dx = fons.draw_text(dx, dy, 'Middle')
dx += 10 dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE) fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
dx = C.fonsDrawText(state.fons, dx, dy, c'Baseline', C.NULL) dx = fons.draw_text(dx, dy, 'Baseline')
dx += 10 dx += 10
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BOTTOM) fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BOTTOM)
C.fonsDrawText(state.fons, dx, dy, c'Bottom', C.NULL) fons.draw_text(dx, dy, 'Bottom')
dx = 150 dx = 150
dy = 400 dy = 400
line(f32(dx), f32(dy - 30), f32(dx), f32(dy + 80.0)) line(f32(dx), f32(dy - 30), f32(dx), f32(dy + 80.0))
C.fonsSetAlign(state.fons, C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE) fons.set_align(C.FONS_ALIGN_LEFT | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx, dy, c'Left', C.NULL) fons.draw_text(dx, dy, 'Left')
dy += 30 dy += 30
C.fonsSetAlign(state.fons, C.FONS_ALIGN_CENTER | C.FONS_ALIGN_BASELINE) fons.set_align(C.FONS_ALIGN_CENTER | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx, dy, c'Center', C.NULL) fons.draw_text(dx, dy, 'Center')
dy += 30 dy += 30
C.fonsSetAlign(state.fons, C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_BASELINE) fons.set_align(C.FONS_ALIGN_RIGHT | C.FONS_ALIGN_BASELINE)
C.fonsDrawText(state.fons, dx, dy, c'Right', C.NULL) fons.draw_text(dx, dy, 'Right')
C.sfons_flush(state.fons) C.sfons_flush(fons)
} }
fn line(sx f32, sy f32, ex f32, ey f32) { fn line(sx f32, sy f32, ex f32, ey f32) {

View File

@ -2,9 +2,9 @@ import sokol
import sokol.sapp import sokol.sapp
import sokol.gfx import sokol.gfx
import sokol.sgl import sokol.sgl
import fontstash
import sokol.sfons import sokol.sfons
import os import os
import time
const ( const (
text = ' text = '
@ -56,7 +56,7 @@ Let my heart be still a moment and this mystery explore;—
struct AppState { struct AppState {
mut: mut:
pass_action C.sg_pass_action pass_action C.sg_pass_action
fons &C.FONScontext fons &fontstash.Context
font_normal int font_normal int
inited bool inited bool
} }
@ -76,7 +76,7 @@ fn main() {
pass_action.colors[0] = color_action pass_action.colors[0] = color_action
state := &AppState{ state := &AppState{
pass_action: pass_action pass_action: pass_action
fons: &C.FONScontext(0) fons: voidptr(0) // &fontstash.Context(0)
} }
title := 'V Metal/GL Text Rendering' title := 'V Metal/GL Text Rendering'
desc := C.sapp_desc{ desc := C.sapp_desc{
@ -104,47 +104,45 @@ fn init(user_data voidptr) {
'RobotoMono-Regular.ttf'))) 'RobotoMono-Regular.ttf')))
{ {
println('loaded font: $bytes.len') println('loaded font: $bytes.len')
state.font_normal = C.fonsAddFontMem(state.fons, c'sans', bytes.data, bytes.len, state.font_normal = state.fons.add_font_mem('sans', bytes, false)
false)
} }
} }
fn frame(user_data voidptr) { fn frame(user_data voidptr) {
t := time.ticks()
mut state := &AppState(user_data) mut state := &AppState(user_data)
state.render_font() state.render_font()
gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height()) gfx.begin_default_pass(&state.pass_action, sapp.width(), sapp.height())
sgl.draw() sgl.draw()
gfx.end_pass() gfx.end_pass()
gfx.commit() gfx.commit()
println(time.ticks() - t)
} }
const ( const (
black = C.sfons_rgba(0, 0, 0, 255) black = sfons.rgba(0, 0, 0, 255)
) )
fn (mut state AppState) render_font() { fn (mut state AppState) render_font() {
lh := 30 lh := 30
mut dy := lh mut dy := lh
mut fons := state.fons
if !state.inited { if !state.inited {
state.fons.clear_state() fons.clear_state()
sgl.defaults() sgl.defaults()
sgl.matrix_mode_projection() sgl.matrix_mode_projection()
sgl.ortho(0.0, f32(C.sapp_width()), f32(C.sapp_height()), 0.0, -1.0, 1.0) sgl.ortho(0.0, f32(sapp.width()), f32(sapp.height()), 0.0, -1.0, 1.0)
state.fons.set_font(state.font_normal) fons.set_font(state.font_normal)
state.fons.set_size(100.0) fons.set_size(100.0)
C.fonsSetColor(state.fons, black) fons.set_color(black)
C.fonsSetFont(state.fons, state.font_normal) fons.set_font(state.font_normal)
C.fonsSetSize(state.fons, 35.0) fons.set_size(35.0)
state.inited = true state.inited = true
} }
for line in lines { for line in lines {
C.fonsDrawText(state.fons, 40, dy, line.str, C.NULL) fons.draw_text(40, dy, line)
dy += lh dy += lh
} }
C.sfons_flush(state.fons) sfons.flush(fons)
} }
fn line(sx f32, sy f32, ex f32, ey f32) { fn line(sx f32, sy f32, ex f32, ey f32) {

View File

@ -18,6 +18,8 @@ $if windows {
#flag -lm #flag -lm
} }
pub type Context = C.FONScontext
//#flag -lfreetype //#flag -lfreetype
pub const ( pub const (
// TODO: fontstash.used_import is used to keep v from warning about unused imports // TODO: fontstash.used_import is used to keep v from warning about unused imports
@ -26,147 +28,150 @@ pub const (
// Contructor and destructor. // Contructor and destructor.
[inline] [inline]
pub fn create_internal(params &C.FONSparams) &C.FONScontext { pub fn create_internal(params &C.FONSparams) &Context {
return C.fonsCreateInternal(params) return C.fonsCreateInternal(params)
} }
[inline] [inline]
pub fn delete_internal(s &C.FONScontext) { pub fn delete_internal(s &Context) {
C.fonsDeleteInternal(s) C.fonsDeleteInternal(s)
} }
[inline] [inline]
pub fn (s &C.FONScontext) set_error_callback(callback fn (voidptr, int, int), uptr voidptr) { pub fn (s &Context) set_error_callback(callback fn (voidptr, int, int), uptr voidptr) {
C.fonsSetErrorCallback(s, callback, uptr) C.fonsSetErrorCallback(s, callback, uptr)
} }
// Returns current atlas size. // Returns current atlas size.
[inline] [inline]
pub fn (s &C.FONScontext) get_atlas_size(width &int, height &int) { pub fn (s &Context) get_atlas_size() (int, int) {
C.fonsGetAtlasSize(s, width, height) mut width := 0
mut height := 0
C.fonsGetAtlasSize(s, &width, &height)
return width, height
} }
// Expands the atlas size. // Expands the atlas size.
[inline] [inline]
pub fn (s &C.FONScontext) expand_atlas(width int, height int) int { pub fn (s &Context) expand_atlas(width int, height int) int {
return C.fonsExpandAtlas(s, width, height) return C.fonsExpandAtlas(s, width, height)
} }
// Resets the whole stash. // Resets the whole stash.
[inline] [inline]
pub fn (s &C.FONScontext) reset_atlas(width int, height int) int { pub fn (s &Context) reset_atlas(width int, height int) int {
return C.fonsResetAtlas(s, width, height) return C.fonsResetAtlas(s, width, height)
} }
// Add fonts // Add fonts
[inline] [inline]
pub fn (s &C.FONScontext) get_font_by_name(name &char) int { pub fn (s &Context) get_font_by_name(name string) int {
return C.fonsGetFontByName(s, name) return C.fonsGetFontByName(s, &char(name.str))
} }
[inline] [inline]
pub fn (s &C.FONScontext) add_fallback_font(base int, fallback int) int { pub fn (s &Context) add_fallback_font(base int, fallback int) int {
return C.fonsAddFallbackFont(s, base, fallback) return C.fonsAddFallbackFont(s, base, fallback)
} }
[inline] [inline]
pub fn (s &C.FONScontext) add_font_mem(name &char, data &byte, data_size int, free_data int) int { pub fn (s &Context) add_font_mem(name string, data []byte, free_data bool) int {
return C.fonsAddFontMem(s, name, data, data_size, free_data) return C.fonsAddFontMem(s, &char(name.str), data.data, data.len, int(free_data))
} }
// State handling // State handling
[inline] [inline]
pub fn (s &C.FONScontext) push_state() { pub fn (s &Context) push_state() {
C.fonsPushState(s) C.fonsPushState(s)
} }
[inline] [inline]
pub fn (s &C.FONScontext) pop_state() { pub fn (s &Context) pop_state() {
C.fonsPopState(s) C.fonsPopState(s)
} }
[inline] [inline]
pub fn (s &C.FONScontext) clear_state() { pub fn (s &Context) clear_state() {
C.fonsClearState(s) C.fonsClearState(s)
} }
// State setting // State setting
[inline] [inline]
pub fn (s &C.FONScontext) set_size(size f32) { pub fn (s &Context) set_size(size f32) {
C.fonsSetSize(s, size) C.fonsSetSize(s, size)
} }
[inline] [inline]
pub fn (s &C.FONScontext) set_color(color u32) { pub fn (s &Context) set_color(color u32) {
C.fonsSetColor(s, color) C.fonsSetColor(s, color)
} }
[inline] [inline]
pub fn (s &C.FONScontext) set_spacing(spacing f32) { pub fn (s &Context) set_spacing(spacing f32) {
C.fonsSetSpacing(s, spacing) C.fonsSetSpacing(s, spacing)
} }
[inline] [inline]
pub fn (s &C.FONScontext) set_blur(blur f32) { pub fn (s &Context) set_blur(blur f32) {
C.fonsSetBlur(s, blur) C.fonsSetBlur(s, blur)
} }
[inline] [inline]
pub fn (s &C.FONScontext) set_align(align int) { pub fn (s &Context) set_align(align int) {
C.fonsSetAlign(s, align) C.fonsSetAlign(s, align)
} }
[inline] [inline]
pub fn (s &C.FONScontext) set_font(font int) { pub fn (s &Context) set_font(font int) {
C.fonsSetFont(s, font) C.fonsSetFont(s, font)
} }
// Draw text // Draw text
[inline] [inline]
pub fn (s &C.FONScontext) draw_text(x f32, y f32, str &char, end &char) f32 { pub fn (s &Context) draw_text(x f32, y f32, text string) f32 {
return C.fonsDrawText(s, x, y, str, end) return C.fonsDrawText(s, x, y, &char(text.str), &char(0))
} }
// Measure text // Measure text
[inline] [inline]
pub fn (s &C.FONScontext) text_bounds(x f32, y f32, str &char, end &char, bounds &f32) f32 { pub fn (s &Context) text_bounds(x f32, y f32, text string, bounds &f32) f32 {
return C.fonsTextBounds(s, x, y, str, end, bounds) return C.fonsTextBounds(s, x, y, &char(text.str), &char(0), bounds)
} }
[inline] [inline]
pub fn (s &C.FONScontext) line_bounds(y f32, miny &f32, maxy &f32) { pub fn (s &Context) line_bounds(y f32, miny &f32, maxy &f32) {
C.fonsLineBounds(s, y, miny, maxy) C.fonsLineBounds(s, y, miny, maxy)
} }
[inline] [inline]
pub fn (s &C.FONScontext) vert_metrics(ascender &f32, descender &f32, lineh &f32) { pub fn (s &Context) vert_metrics(ascender &f32, descender &f32, lineh &f32) {
C.fonsVertMetrics(s, ascender, descender, lineh) C.fonsVertMetrics(s, ascender, descender, lineh)
} }
// Text iterator // Text iterator
[inline] [inline]
pub fn (s &C.FONScontext) text_iter_init(iter &C.FONStextIter, x f32, y f32, str &char, end &char) int { pub fn (s &Context) text_iter_init(iter &C.FONStextIter, x f32, y f32, str &char, end &char) int {
return C.fonsTextIterInit(s, iter, x, y, str, end) return C.fonsTextIterInit(s, iter, x, y, str, end)
} }
[inline] [inline]
pub fn (s &C.FONScontext) text_iter_next(iter &C.FONStextIter, quad &C.FONSquad) int { pub fn (s &Context) text_iter_next(iter &C.FONStextIter, quad &C.FONSquad) int {
return C.fonsTextIterNext(s, iter, quad) return C.fonsTextIterNext(s, iter, quad)
} }
// Pull texture changes // Pull texture changes
[inline] [inline]
pub fn (s &C.FONScontext) get_texture_data(width &int, height &int) &byte { pub fn (s &Context) get_texture_data(width &int, height &int) &byte {
return &byte(C.fonsGetTextureData(s, width, height)) return &byte(C.fonsGetTextureData(s, width, height))
} }
[inline] [inline]
pub fn (s &C.FONScontext) validate_texture(dirty &int) int { pub fn (s &Context) validate_texture(dirty &int) int {
return C.fonsValidateTexture(s, dirty) return C.fonsValidateTexture(s, dirty)
} }
// Draws the stash texture for debugging // Draws the stash texture for debugging
[inline] [inline]
pub fn (s &C.FONScontext) draw_debug(x f32, y f32) { pub fn (s &Context) draw_debug(x f32, y f32) {
C.fonsDrawDebug(s, x, y) C.fonsDrawDebug(s, x, y)
} }

View File

@ -2,6 +2,7 @@
// Use of this source code is governed by an MIT license that can be found in the LICENSE file. // Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module gg module gg
import fontstash
import sokol.sfons import sokol.sfons
import sokol.sgl import sokol.sgl
import gx import gx
@ -9,7 +10,7 @@ import os
struct FT { struct FT {
pub: pub:
fons &C.FONScontext fons &fontstash.Context
font_normal int font_normal int
font_bold int font_bold int
font_mono int font_mono int
@ -43,14 +44,10 @@ fn new_ft(c FTConfig) ?&FT {
return &FT{ return &FT{
fons: fons fons: fons
font_normal: C.fonsAddFontMem(fons, c'sans', bytes_normal.data, bytes_normal.len, font_normal: fons.add_font_mem('sans', bytes_normal, false)
false) font_bold: fons.add_font_mem('sans', bytes_bold, false)
font_bold: C.fonsAddFontMem(fons, c'sans', bytes_bold.data, bytes_bold.len, font_mono: fons.add_font_mem('sans', bytes_mono, false)
false) font_italic: fons.add_font_mem('sans', bytes_italic, false)
font_mono: C.fonsAddFontMem(fons, c'sans', bytes_mono.data, bytes_mono.len,
false)
font_italic: C.fonsAddFontMem(fons, c'sans', bytes_italic.data, bytes_italic.len,
false)
scale: c.scale scale: c.scale
} }
} else { } else {
@ -112,11 +109,10 @@ fn new_ft(c FTConfig) ?&FT {
debug_font_println('Font used for font_italic : $italic_path') debug_font_println('Font used for font_italic : $italic_path')
return &FT{ return &FT{
fons: fons fons: fons
font_normal: C.fonsAddFontMem(fons, c'sans', bytes.data, bytes.len, false) font_normal: fons.add_font_mem('sans', bytes, false)
font_bold: C.fonsAddFontMem(fons, c'sans', bytes_bold.data, bytes_bold.len, false) font_bold: fons.add_font_mem('sans', bytes_bold, false)
font_mono: C.fonsAddFontMem(fons, c'sans', bytes_mono.data, bytes_mono.len, false) font_mono: fons.add_font_mem('sans', bytes_mono, false)
font_italic: C.fonsAddFontMem(fons, c'sans', bytes_italic.data, bytes_italic.len, font_italic: fons.add_font_mem('sans', bytes_italic, false)
false)
scale: c.scale scale: c.scale
} }
} }
@ -137,12 +133,12 @@ pub fn (ctx &Context) set_cfg(cfg gx.TextCfg) {
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale } scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
size := if cfg.mono { cfg.size - 2 } else { cfg.size } size := if cfg.mono { cfg.size - 2 } else { cfg.size }
ctx.ft.fons.set_size(scale * f32(size)) ctx.ft.fons.set_size(scale * f32(size))
C.fonsSetAlign(ctx.ft.fons, int(cfg.align) | int(cfg.vertical_align)) ctx.ft.fons.set_align(int(cfg.align) | int(cfg.vertical_align))
color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.a) color := sfons.rgba(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.a)
if cfg.color.a != 255 { if cfg.color.a != 255 {
sgl.load_pipeline(ctx.timage_pip) sgl.load_pipeline(ctx.timage_pip)
} }
C.fonsSetColor(ctx.ft.fons, color) ctx.ft.fons.set_color(color)
ascender := f32(0.0) ascender := f32(0.0)
descender := f32(0.0) descender := f32(0.0)
lh := f32(0.0) lh := f32(0.0)
@ -172,7 +168,7 @@ pub fn (ctx &Context) draw_text(x int, y int, text_ string, cfg gx.TextCfg) {
// } // }
ctx.set_cfg(cfg) ctx.set_cfg(cfg)
scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale } scale := if ctx.ft.scale == 0 { f32(1) } else { ctx.ft.scale }
C.fonsDrawText(ctx.ft.fons, x * scale, y * scale, &char(text_.str), 0) // TODO: check offsets/alignment ctx.ft.fons.draw_text(x * scale, y * scale, text_) // TODO: check offsets/alignment
} }
pub fn (ctx &Context) draw_text_def(x int, y int, text string) { pub fn (ctx &Context) draw_text_def(x int, y int, text string) {
@ -198,7 +194,7 @@ pub fn (ctx &Context) text_width(s string) int {
return 0 return 0
} }
mut buf := [4]f32{} mut buf := [4]f32{}
C.fonsTextBounds(ctx.ft.fons, 0, 0, &char(s.str), 0, &buf[0]) ctx.ft.fons.text_bounds(0, 0, s, &buf[0])
if s.ends_with(' ') { if s.ends_with(' ') {
return int((buf[2] - buf[0]) / ctx.scale) + return int((buf[2] - buf[0]) / ctx.scale) +
ctx.text_width('i') // TODO fix this in fontstash? ctx.text_width('i') // TODO fix this in fontstash?
@ -219,7 +215,7 @@ pub fn (ctx &Context) text_height(s string) int {
return 0 return 0
} }
mut buf := [4]f32{} mut buf := [4]f32{}
C.fonsTextBounds(ctx.ft.fons, 0, 0, &char(s.str), 0, &buf[0]) ctx.ft.fons.text_bounds(0, 0, s, &buf[0])
return int((buf[3] - buf[1]) / ctx.scale) return int((buf[3] - buf[1]) / ctx.scale)
} }
@ -229,6 +225,6 @@ pub fn (ctx &Context) text_size(s string) (int, int) {
return 0, 0 return 0, 0
} }
mut buf := [4]f32{} mut buf := [4]f32{}
C.fonsTextBounds(ctx.ft.fons, 0, 0, &char(s.str), 0, &buf[0]) ctx.ft.fons.text_bounds(0, 0, s, &buf[0])
return int((buf[2] - buf[0]) / ctx.scale), int((buf[3] - buf[1]) / ctx.scale) return int((buf[2] - buf[0]) / ctx.scale), int((buf[3] - buf[1]) / ctx.scale)
} }

View File

@ -6,12 +6,12 @@ import fontstash
const used_import = fontstash.used_import + 1 const used_import = fontstash.used_import + 1
[inline] [inline]
pub fn create(width int, height int, flags int) &C.FONScontext { pub fn create(width int, height int, flags int) &fontstash.Context {
return C.sfons_create(width, height, flags) return C.sfons_create(width, height, flags)
} }
[inline] [inline]
pub fn destroy(ctx &C.FONScontext) { pub fn destroy(ctx &fontstash.Context) {
C.sfons_destroy(ctx) C.sfons_destroy(ctx)
} }
@ -21,6 +21,6 @@ pub fn rgba(r byte, g byte, b byte, a byte) u32 {
} }
[inline] [inline]
pub fn flush(ctx &C.FONScontext) { pub fn flush(ctx &fontstash.Context) {
C.sfons_flush(ctx) C.sfons_flush(ctx)
} }

View File

@ -1,6 +1,8 @@
module sfons module sfons
fn C.sfons_create(width int, height int, flags int) &C.FONScontext import fontstash
fn C.sfons_destroy(ctx &C.FONScontext)
fn C.sfons_create(width int, height int, flags int) &fontstash.Context
fn C.sfons_destroy(ctx &fontstash.Context)
fn C.sfons_rgba(r byte, g byte, b byte, a byte) u32 fn C.sfons_rgba(r byte, g byte, b byte, a byte) u32
fn C.sfons_flush(ctx &C.FONScontext) fn C.sfons_flush(ctx &fontstash.Context)