Antra

Standardbibliothek

Antra::Raylib::Text

import Antra::Raylib::Text

raylib's text module for Antra: fonts, drawing text and measuring it.

Cheatsheet: module rtext, https://www.raylib.com/cheatsheet/cheatsheet.html.

Fonts are raylib's Font structs as maps; pass them back unchanged. Positions are [x, y]; colors are [r, g, b], [r, g, b, a] or raylib's names (:darkgray, ...). Text is UTF-8 - letters outside ASCII need a font loaded with them (load_font(..., [codepoints: "äöü€"])). raylib's string helpers (TextFormat, TextSplit, ...) are not here: Antra::String and string interpolation do that. Every function takes the app from Core.window; draw inside Core.run / Core.frame.

import Antra::Raylib::Core
import Antra::Raylib::Text             # or: import Antra::Raylib::Text as T

val app = Core.window("Text", 320, 200).!
Core.run(app, 0) |app, n, _dt| -> {
    app |> Core.clear(:raywhite)
        |> Text.text("Hello, raylib!", [20, 80], 30, :darkgray)
        |> Text.draw_fps([10, 10])
    n + 1
}
Core.close(app)

Funktionen#

fn default_font(app)

Returns raylib's built-in font (raylib: GetFontDefault), e.g. for text(..., [font:]) or measure_text.

Example:

val font = Text.default_font(app)
Text.text(app, "default", [10, 10], 20, :black, [font: font, spacing: 2])
fn draw_fps(app, [x, y])

Draws the current frames per second at [x, y] (raylib: DrawFPS); returns app.

Example:

Text.draw_fps(app, [10, 10])
fn font_from_bytes(app, bytes, file_type, [size: = 32, codepoints: = undef])

Loads a font from bytes in memory, a ByteString holding a font file of type file_type (".ttf", ".otf") (raylib: LoadFontFromMemory); options as in load_font. %Ok[font] or %Err[...].

Example:

import Antra::Bytes

val font = Text.font_from_bytes(app, Bytes.from_file("font.ttf").!, ".ttf", [size: 24]).!
fn font_from_image(app, image, key, [first_char: = 32])

Makes a font from an image of letters in a row, separated by pixels of key color, starting with the letter first_char: (32, the space) (raylib: LoadFontFromImage) - XNA-style sprite fonts. %Ok[font] or %Err[...].

Example:

val img = Textures.load_image(app, "custom_mecha.png").!
val font = Text.font_from_image(app, img, :magenta).!
fn glyph(app, font, char)

What font knows about one letter, a one-character string or a codepoint (raylib: GetGlyphIndex, GetGlyphInfo, GetGlyphAtlasRec): %{index:, codepoint:, rect: [x, y, w, h], offset: [x, y], advance:} - its place in the font, where it sits in the font's texture, how far it is drawn from the pen position and how far the pen moves on (0 means the width of rect, as in raylib's default font). A letter the font does not have gives the one for ?, or the font's first letter if it has no ? either.

Example:

val g = Text.glyph(app, Text.default_font(app), "A")
g.codepoint                    # => 65
fn line_spacing(app, pixels)

Sets the gap in pixels between lines of text with \n: the next line starts size + pixels below (raylib: SetTextLineSpacing); returns app.

Example:

Text.line_spacing(app, 24)
Text.text(app, "two\nlines", [10, 10], 20, :black)
fn load_font(app, path, [size: = 32, codepoints: = undef])

Loads a font file: %Ok[font] or %Err[...] when it cannot be loaded. TTF, OTF and BDF fonts are rendered at size: pixels (32) with the letters codepoints: - a string ("äöü€") or a list of codepoints, ASCII without it (raylib: LoadFontEx). BMFont files (.fnt with their .png) and image fonts (letters in a row, separated by magenta) come at their own size; size: and codepoints: do not apply (raylib: LoadFont).

Example:

val font = Text.load_font(app, "font.ttf", [size: 48, codepoints: "Grüße"]).!
Text.text(app, "Grüße", [20, 20], 48, :maroon, [font: font])
val pixel = Text.load_font(app, "pixantiqua.fnt").!
fn load_font_sdf(app, path, [size: = 32, codepoints: = undef, padding: = 0])

Loads a TTF/OTF font as a signed distance field (raylib: LoadFontData with FONT_SDF, GenImageFontAtlas): it stays sharp when drawn much larger than size: (32), but needs an SDF shader around the drawing (Core.with_shader, e.g. raylib's sdf.fs). codepoints: and the result as in load_font; padding: (0) adds room around each letter in the atlas. Free it with unload_font.

Example:

val sdf = Text.load_font_sdf(app, "anonymous_pro_bold.ttf", [size: 50]).!
val shader = Core.load_shader(app, undef, "sdf.fs").!
Core.with_shader(app, shader) |app| -> Text.text(app, "Big", [10, 10], 160, :black, [font: sdf])
fn measure_text(app, text, size, [font: = undef, spacing: = undef])

Size [width, height] of text at size, with the options of text (font:, spacing:) (raylib: MeasureTextEx).

Example:

val [w, h] = Text.measure_text(app, "Hi", 20)
h                              # => 20
fn text(app, text, pos, size, color, [font: = undef, spacing: = undef, rotation: = 0, origin: = [0, 0]])

Draws text with its top-left corner at pos [x, y], size pixels high. Options: font: (raylib's default font), spacing: between the letters (default font: size / 10, others: 1), rotation: in degrees (0) around origin: ([0, 0], relative to pos) (raylib: DrawText, DrawTextPro). \n starts a new line (see line_spacing). Returns app.

Example:

Text.text(app, "Score: 42", [10, 10], 20, :darkgray)
Text.text(app, "tilted", [100, 100], 20, :blue, [rotation: 30, spacing: 2])
fn text_width(app, text, size)

Width in pixels of text drawn with raylib's default font at size (raylib: MeasureText) - e.g. to center it.

Example:

val w = Text.text_width(app, "Centered", 20)
Text.text(app, "Centered", [(320 - w) / 2, 90], 20, :black)
fn unload_font(app, font)

Frees a font loaded with load_font (raylib: UnloadFont); returns app.

Quelle dieser Seite