Skip to content

fontstash #

Description

fontstash is a thin wrapper over https://github.com/memononen/fontstash, which in turn is a light-weight online font texture atlas builder written in C. It uses stb_truetype to render fonts on demand to a texture atlas

Constants #

const used_import = 1

#flag -lfreetype

Todo: fontstash.used_import is used to keep v from warning about unused imports

const invalid = C.FONS_INVALID

fn create_internal #

fn create_internal(params &C.FONSparams) &Context

create_internal returns a fontstash Context allocated on the heap.

See also: delete_internal

fn delete_internal #

fn delete_internal(s &Context)

delete_internal deletes and free memory of s fontstash Context.

See also: create_internal

type Context #

type Context = C.FONScontext

fn (Context) set_error_callback #

fn (s &Context) set_error_callback(callback fn (voidptr, int, int), uptr voidptr)

set_error_callback sets callback as a function to be called if fontstash encounter any errors. uptr can be used to pass custom userdata.

fn (Context) get_atlas_size #

fn (s &Context) get_atlas_size() (int, int)

get_atlas_size returns the current size of the texture atlas which the font is rendered to.

fn (Context) expand_atlas #

fn (s &Context) expand_atlas(width int, height int) int

expand_atlas expands the font texture atlas size to width x height.

fn (Context) reset_atlas #

fn (s &Context) reset_atlas(width int, height int) int

reset_atlas resets width x height of the font texture atlas.

fn (Context) get_font_by_name #

fn (s &Context) get_font_by_name(name string) int

get_font_by_name returns the id of the font with name or fontstash.invalid if no font with name could be found.

fn (Context) add_fallback_font #

fn (s &Context) add_fallback_font(base int, fallback int) int

add_fallback_font adds a fallback font to the base font id in the Context. fallback is expected to be the id of a previous, successfully, added font. add_fallback_font returns 1 on success, 0 otherwise.

fn (Context) add_font_mem #

fn (s &Context) add_font_mem(name string, data []u8, free_data bool) int

add_font_mem adds the font data located in memory to the Context. name is the human readable name for the font. free_data indicates if data should be freed after the font is added. The function returns the id of the font on success, fontstash.invalid otherwise.

fn (Context) push_state #

fn (s &Context) push_state()

push_state pushes a new state on the state stack. A state holds the current attributes of the rendering, attributes are things like color, size, the font in use, blur effect etc.

See also: pop_state See also: clear_state See also: set_size See also: set_color See also: set_spacing See also: set_blur See also: set_align See also: set_font

fn (Context) pop_state #

fn (s &Context) pop_state()

pop_state pops the current state from the state stack.

See also: push_state See also: clear_state

fn (Context) clear_state #

fn (s &Context) clear_state()

clear_state clears the current state.

See also: push_state See also: pop_state

fn (Context) set_size #

fn (s &Context) set_size(size f32)

set_size sets the font size to size on the active state.

See also: push_state See also: pop_state See also: clear_state

fn (Context) set_color #

fn (s &Context) set_color(color u32)

set_color sets the font color to color on the active state.

See also: push_state See also: pop_state See also: clear_state

fn (Context) set_spacing #

fn (s &Context) set_spacing(spacing f32)

set_spacing sets the font spacing to spacing on the active state.

See also: push_state See also: pop_state See also: clear_state

fn (Context) set_blur #

fn (s &Context) set_blur(blur f32)

set_blur sets the font blur effect to blur on the active state.

See also: push_state See also: pop_state See also: clear_state

fn (Context) set_align #

fn (s &Context) set_align(align int)

set_align sets the font aligning to align on the active state.

See also: push_state See also: pop_state See also: clear_state

fn (Context) set_alignment #

fn (s &Context) set_alignment(align Align)

set_alignment sets the font aligning to the align flags.

See also: push_state See also: pop_state See also: clear_state

fn (Context) set_font #

fn (s &Context) set_font(font_id int)

set_font sets the font used for this render on the active state. font_id is the id of the loaded font.

See also: push_state See also: pop_state See also: clear_state

fn (Context) draw_text #

fn (s &Context) draw_text(x f32, y f32, text string) f32

draw_text draws the text string at position x,y. The function returns the x coordinate of the resulting render.

fn (Context) text_bounds #

fn (s &Context) text_bounds(x f32, y f32, text string, bounds &f32) f32

text_bounds fills the bounds argument with the pixel dimensions of the rendered text at position x,y.

bounds is expected to be of type mut bounds := [4]f32{}. Call example: ctx.text_bounds(0, 0, 'example', &bounds[0]). bounds[0] is the x coordinate of the top-left point. bounds[1] is the y coordinate of the top-left point. bounds[2] is the x coordinate of the bottom-right point. bounds[3] is the y coordinate of the bottom-right point.

fn (Context) line_bounds #

fn (s &Context) line_bounds(y f32, miny &f32, maxy &f32)

line_bounds fills miny and maxy with the values of the minimum and maximum line bounds respectively.

fn (Context) vert_metrics #

fn (s &Context) vert_metrics(ascender &f32, descender &f32, lineh &f32)

vert_metrics assigns the respective values of ascender, descender and lineh.

fn (Context) text_iter_init #

fn (s &Context) text_iter_init(iter &C.FONStextIter, x f32, y f32, str &char, end &char) int

text_iter_init initalizes the text iterator iter.

fn (Context) text_iter_next #

fn (s &Context) text_iter_next(iter &C.FONStextIter, quad &C.FONSquad) int

text_iter_next advances iter to the next quad.

fn (Context) get_texture_data #

fn (s &Context) get_texture_data(width &int, height &int) &u8

get_texture_data returns the current Context's raw texture data. width and height is assigned the size of the texture dimensions.

fn (Context) validate_texture #

fn (s &Context) validate_texture(dirty &int) int

validate_texture fills the dirty argument with the pixel dimensions of the dirty rectangle of the Context's raw texture, if any.

dirty is expected to be of type mut dirty := [4]int{}. Call example: is_dirty := ctx.validate_texture(&dirty[0]). The function returns 1 if the texture has a dirty rectangle, 0 otherwise.

fn (Context) draw_debug #

fn (s &Context) draw_debug(x f32, y f32)

draw_debug draws the stash texture for debugging.

enum Align #

@[flag]
enum Align {
	// Horizontal align
	left // Default
	center
	right
	// Vertical align
	top
	middle
	bottom
	baseline // Default
}

enum ErrorCode #

enum ErrorCode {
	// Font atlas is full.
	atlas_full       = 1
	// Scratch memory used to render glyphs is full, requested size reported in 'val', you may need to bump up FONS_SCRATCH_BUF_SIZE.
	scratch_full     = 2
	// Calls to fonsPushState has created too large stack, if you need deep state stack bump up FONS_MAX_STATES.
	states_overflow  = 3
	// Trying to pop too many states fonsPopState().
	states_underflow = 4
}

enum Flags #

@[flag]
enum Flags {
	top_left
	bottom_left
}

struct C.FONScontext #

struct C.FONScontext {}

struct C.FONSfont #

struct C.FONSfont {}

struct C.FONSparams #

struct C.FONSparams {
	width   int
	height  int
	flags   char
	userPtr voidptr
	// int (*renderCreate)(void* uptr, int width, int height)
	renderCreate fn (uptr voidptr, width int, height int) int = unsafe { nil }
	// int (*renderResize)(void* uptr, int width, int height)
	renderResize fn (uptr voidptr, width int, height int) int = unsafe { nil }
	// void (*renderUpdate)(void* uptr, int* rect, const unsigned char* data)
	renderUpdate fn (uptr voidptr, rect &int, data &u8) = unsafe { nil }
	// void (*renderDraw)(void* uptr, const float* verts, const float* tcoords, const unsigned int* colors, int nverts)
	renderDraw fn (uptr voidptr, verts &f32, tcoords &f32, colors &u32, nverts int) = unsafe { nil }
	// void (*renderDelete)(void* uptr)
	renderDelete fn (uptr voidptr) = unsafe { nil }
}

struct C.FONSquad #

struct C.FONSquad {
	x0 f32
	y0 f32
	s0 f32
	t0 f32
	x1 f32
	y1 f32
	s1 f32
	t1 f32
}

struct C.FONStextIter #

struct C.FONStextIter {
	x              f32
	y              f32
	nextx          f32
	nexty          f32
	scale          f32
	spacing        f32
	codepoint      u32
	isize          i16
	iblur          i16
	font           &C.FONSfont = unsafe { nil }
	prevGlyphIndex int
	str            &u8
	next           &u8
	end            &u8
	utf8state      u32
}