mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
examples: use fontstash
wrapper calls (#12718)
This commit is contained in:
@ -18,6 +18,8 @@ $if windows {
|
||||
#flag -lm
|
||||
}
|
||||
|
||||
pub type Context = C.FONScontext
|
||||
|
||||
//#flag -lfreetype
|
||||
pub const (
|
||||
// TODO: fontstash.used_import is used to keep v from warning about unused imports
|
||||
@ -26,147 +28,150 @@ pub const (
|
||||
|
||||
// Contructor and destructor.
|
||||
[inline]
|
||||
pub fn create_internal(params &C.FONSparams) &C.FONScontext {
|
||||
pub fn create_internal(params &C.FONSparams) &Context {
|
||||
return C.fonsCreateInternal(params)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn delete_internal(s &C.FONScontext) {
|
||||
pub fn delete_internal(s &Context) {
|
||||
C.fonsDeleteInternal(s)
|
||||
}
|
||||
|
||||
[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)
|
||||
}
|
||||
|
||||
// Returns current atlas size.
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) get_atlas_size(width &int, height &int) {
|
||||
C.fonsGetAtlasSize(s, width, height)
|
||||
pub fn (s &Context) get_atlas_size() (int, int) {
|
||||
mut width := 0
|
||||
mut height := 0
|
||||
C.fonsGetAtlasSize(s, &width, &height)
|
||||
return width, height
|
||||
}
|
||||
|
||||
// Expands the atlas size.
|
||||
[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)
|
||||
}
|
||||
|
||||
// Resets the whole stash.
|
||||
[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)
|
||||
}
|
||||
|
||||
// Add fonts
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) get_font_by_name(name &char) int {
|
||||
return C.fonsGetFontByName(s, name)
|
||||
pub fn (s &Context) get_font_by_name(name string) int {
|
||||
return C.fonsGetFontByName(s, &char(name.str))
|
||||
}
|
||||
|
||||
[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)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) add_font_mem(name &char, data &byte, data_size int, free_data int) int {
|
||||
return C.fonsAddFontMem(s, name, data, data_size, free_data)
|
||||
pub fn (s &Context) add_font_mem(name string, data []byte, free_data bool) int {
|
||||
return C.fonsAddFontMem(s, &char(name.str), data.data, data.len, int(free_data))
|
||||
}
|
||||
|
||||
// State handling
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) push_state() {
|
||||
pub fn (s &Context) push_state() {
|
||||
C.fonsPushState(s)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) pop_state() {
|
||||
pub fn (s &Context) pop_state() {
|
||||
C.fonsPopState(s)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) clear_state() {
|
||||
pub fn (s &Context) clear_state() {
|
||||
C.fonsClearState(s)
|
||||
}
|
||||
|
||||
// State setting
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) set_size(size f32) {
|
||||
pub fn (s &Context) set_size(size f32) {
|
||||
C.fonsSetSize(s, size)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) set_color(color u32) {
|
||||
pub fn (s &Context) set_color(color u32) {
|
||||
C.fonsSetColor(s, color)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) set_spacing(spacing f32) {
|
||||
pub fn (s &Context) set_spacing(spacing f32) {
|
||||
C.fonsSetSpacing(s, spacing)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) set_blur(blur f32) {
|
||||
pub fn (s &Context) set_blur(blur f32) {
|
||||
C.fonsSetBlur(s, blur)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) set_align(align int) {
|
||||
pub fn (s &Context) set_align(align int) {
|
||||
C.fonsSetAlign(s, align)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) set_font(font int) {
|
||||
pub fn (s &Context) set_font(font int) {
|
||||
C.fonsSetFont(s, font)
|
||||
}
|
||||
|
||||
// Draw text
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) draw_text(x f32, y f32, str &char, end &char) f32 {
|
||||
return C.fonsDrawText(s, x, y, str, end)
|
||||
pub fn (s &Context) draw_text(x f32, y f32, text string) f32 {
|
||||
return C.fonsDrawText(s, x, y, &char(text.str), &char(0))
|
||||
}
|
||||
|
||||
// Measure text
|
||||
[inline]
|
||||
pub fn (s &C.FONScontext) text_bounds(x f32, y f32, str &char, end &char, bounds &f32) f32 {
|
||||
return C.fonsTextBounds(s, x, y, str, end, bounds)
|
||||
pub fn (s &Context) text_bounds(x f32, y f32, text string, bounds &f32) f32 {
|
||||
return C.fonsTextBounds(s, x, y, &char(text.str), &char(0), bounds)
|
||||
}
|
||||
|
||||
[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)
|
||||
}
|
||||
|
||||
[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)
|
||||
}
|
||||
|
||||
// Text iterator
|
||||
[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)
|
||||
}
|
||||
|
||||
[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)
|
||||
}
|
||||
|
||||
// Pull texture changes
|
||||
[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))
|
||||
}
|
||||
|
||||
[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)
|
||||
}
|
||||
|
||||
// Draws the stash texture for debugging
|
||||
[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)
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
||||
module gg
|
||||
|
||||
import fontstash
|
||||
import sokol.sfons
|
||||
import sokol.sgl
|
||||
import gx
|
||||
@ -9,7 +10,7 @@ import os
|
||||
|
||||
struct FT {
|
||||
pub:
|
||||
fons &C.FONScontext
|
||||
fons &fontstash.Context
|
||||
font_normal int
|
||||
font_bold int
|
||||
font_mono int
|
||||
@ -43,14 +44,10 @@ fn new_ft(c FTConfig) ?&FT {
|
||||
|
||||
return &FT{
|
||||
fons: fons
|
||||
font_normal: C.fonsAddFontMem(fons, c'sans', bytes_normal.data, bytes_normal.len,
|
||||
false)
|
||||
font_bold: C.fonsAddFontMem(fons, c'sans', bytes_bold.data, bytes_bold.len,
|
||||
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)
|
||||
font_normal: fons.add_font_mem('sans', bytes_normal, false)
|
||||
font_bold: fons.add_font_mem('sans', bytes_bold, false)
|
||||
font_mono: fons.add_font_mem('sans', bytes_mono, false)
|
||||
font_italic: fons.add_font_mem('sans', bytes_italic, false)
|
||||
scale: c.scale
|
||||
}
|
||||
} else {
|
||||
@ -112,11 +109,10 @@ fn new_ft(c FTConfig) ?&FT {
|
||||
debug_font_println('Font used for font_italic : $italic_path')
|
||||
return &FT{
|
||||
fons: fons
|
||||
font_normal: C.fonsAddFontMem(fons, c'sans', bytes.data, bytes.len, false)
|
||||
font_bold: C.fonsAddFontMem(fons, c'sans', bytes_bold.data, bytes_bold.len, 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)
|
||||
font_normal: fons.add_font_mem('sans', bytes, false)
|
||||
font_bold: fons.add_font_mem('sans', bytes_bold, false)
|
||||
font_mono: fons.add_font_mem('sans', bytes_mono, false)
|
||||
font_italic: fons.add_font_mem('sans', bytes_italic, false)
|
||||
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 }
|
||||
size := if cfg.mono { cfg.size - 2 } else { cfg.size }
|
||||
ctx.ft.fons.set_size(scale * f32(size))
|
||||
C.fonsSetAlign(ctx.ft.fons, int(cfg.align) | int(cfg.vertical_align))
|
||||
color := C.sfons_rgba(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.a)
|
||||
ctx.ft.fons.set_align(int(cfg.align) | int(cfg.vertical_align))
|
||||
color := sfons.rgba(cfg.color.r, cfg.color.g, cfg.color.b, cfg.color.a)
|
||||
if cfg.color.a != 255 {
|
||||
sgl.load_pipeline(ctx.timage_pip)
|
||||
}
|
||||
C.fonsSetColor(ctx.ft.fons, color)
|
||||
ctx.ft.fons.set_color(color)
|
||||
ascender := f32(0.0)
|
||||
descender := 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)
|
||||
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) {
|
||||
@ -198,7 +194,7 @@ pub fn (ctx &Context) text_width(s string) int {
|
||||
return 0
|
||||
}
|
||||
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(' ') {
|
||||
return int((buf[2] - buf[0]) / ctx.scale) +
|
||||
ctx.text_width('i') // TODO fix this in fontstash?
|
||||
@ -219,7 +215,7 @@ pub fn (ctx &Context) text_height(s string) int {
|
||||
return 0
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@ -229,6 +225,6 @@ pub fn (ctx &Context) text_size(s string) (int, int) {
|
||||
return 0, 0
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
@ -6,12 +6,12 @@ import fontstash
|
||||
const used_import = fontstash.used_import + 1
|
||||
|
||||
[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)
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn destroy(ctx &C.FONScontext) {
|
||||
pub fn destroy(ctx &fontstash.Context) {
|
||||
C.sfons_destroy(ctx)
|
||||
}
|
||||
|
||||
@ -21,6 +21,6 @@ pub fn rgba(r byte, g byte, b byte, a byte) u32 {
|
||||
}
|
||||
|
||||
[inline]
|
||||
pub fn flush(ctx &C.FONScontext) {
|
||||
pub fn flush(ctx &fontstash.Context) {
|
||||
C.sfons_flush(ctx)
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
module sfons
|
||||
|
||||
fn C.sfons_create(width int, height int, flags int) &C.FONScontext
|
||||
fn C.sfons_destroy(ctx &C.FONScontext)
|
||||
import fontstash
|
||||
|
||||
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_flush(ctx &C.FONScontext)
|
||||
fn C.sfons_flush(ctx &fontstash.Context)
|
||||
|
Reference in New Issue
Block a user