![]() |
tinyimg 1.1.1
Lightweight image manipulation library for WebAssembly
|
Font loading, text measurement and text drawing. More...
#include <stddef.h>#include <stdint.h>#include "tinyimg/image.h"#include "tinyimg/memory.h"#include "tinyimg/tinyimg.h"Go to the source code of this file.
Classes | |
| struct | TinyFontAxis |
| One variation axis a face declares. More... | |
| struct | TinyFont |
| A loaded face. More... | |
| struct | TinyFontSet |
| An ordered chain of faces, tried in turn for each codepoint. More... | |
| struct | TinyFontMetrics |
| What a face says about itself, in pixels at a size. More... | |
| struct | TinyTextStyle |
| How a run of text is set. More... | |
| struct | TinyTextMetrics |
| What a run of text occupies. More... | |
| struct | TinyTextBox |
| The rectangle a run is set inside, and how it sits there. More... | |
| struct | TinyTextLine |
| What one line of a run occupies, and where it sits. More... | |
| struct | TinyTextRun |
| One styled span of a rich run. More... | |
Macros | |
| #define | TINYIMG_FONT_MAX_AXES 4 |
| #define | TINYIMG_AXIS_WEIGHT 0x77676874u |
| #define | TINYIMG_AXIS_WIDTH 0x77647468u |
| #define | TINYIMG_AXIS_SLANT 0x736C6E74u |
| #define | TINYIMG_AXIS_ITALIC 0x6974616Cu |
| #define | TINYIMG_AXIS_OPTICAL_SIZE 0x6F70737Au |
| #define | TINYIMG_FONT_MAX_FALLBACK 4 |
| #define | TINYIMG_TEXT_MAX_RUNS 16 |
Typedefs | |
| typedef enum TinyFontKind | TinyFontKind |
| Outline and bitmap face formats. | |
| typedef enum TinyTextAlign | TinyTextAlign |
| Where a line sits inside the width it was given. | |
| typedef enum TinyTextVAlign | TinyTextVAlign |
| Where a run sits inside the height it was given. | |
| typedef enum TinyTextOverflow | TinyTextOverflow |
| What happens to text the box cannot hold. | |
Enumerations | |
| enum | TinyFontKind { TINYIMG_FONT_TRUETYPE = 0 , TINYIMG_FONT_PSF = 1 , TINYIMG_FONT_BDF = 2 } |
| Outline and bitmap face formats. More... | |
| enum | TinyTextAlign { TINYIMG_ALIGN_LEFT = 0 , TINYIMG_ALIGN_CENTER = 1 , TINYIMG_ALIGN_RIGHT = 2 , TINYIMG_ALIGN_JUSTIFY = 3 } |
| Where a line sits inside the width it was given. More... | |
| enum | TinyTextVAlign { TINYIMG_VALIGN_TOP = 0 , TINYIMG_VALIGN_MIDDLE = 1 , TINYIMG_VALIGN_BOTTOM = 2 } |
| Where a run sits inside the height it was given. More... | |
| enum | TinyTextOverflow { TINYIMG_OVERFLOW_CLIP = 0 , TINYIMG_OVERFLOW_ELLIPSIS = 1 } |
| What happens to text the box cannot hold. More... | |
Functions | |
| int | tiny_font_set_init (TinyFontSet *set) |
| Starts an empty chain. | |
| int | tiny_font_set_add (TinyFontSet *set, const TinyFont *font) |
| Appends a face to the end of a chain. | |
| const TinyFont * | tiny_font_set_for (const TinyFontSet *set, uint32_t codepoint) |
| Which face in a chain covers a codepoint. | |
| uint32_t | tiny_font_axis_count (const TinyFont *font) |
| How many variation axes a face declares. | |
| int | tiny_font_axis (const TinyFont *font, uint32_t index, TinyFontAxis *out) |
| Reads one of a face's variation axes. | |
| int | tiny_font_set_axis (TinyFont *font, uint32_t tag, float value) |
| Sets one axis, in the axis' own units. | |
| int | tiny_font_reset_axes (TinyFont *font) |
| Returns a face to its default instance. | |
| int | tiny_font_load (TinyFont *font, const char *blob_id) |
| Loads a face from a resident blob. | |
| int | tiny_font_load_bytes (TinyFont *font, const uint8_t *data, size_t size) |
| Loads a face from bytes the caller keeps. | |
| void | tiny_font_free (TinyFont *font) |
| Releases whatever a face owns. | |
| int | tiny_font_metrics (const TinyFont *font, float size, TinyFontMetrics *out) |
| Reads a face's vertical metrics at an em size. | |
| int | tiny_font_has_glyph (const TinyFont *font, uint32_t codepoint) |
| Reports whether a face has a glyph for a codepoint. | |
| uint32_t | tiny_font_sizeof (void) |
| Size of a TinyFont, for a host allocating one across the wasm boundary. | |
| uint32_t | tiny_font_metrics_sizeof (void) |
| Size of a TinyFontMetrics, for the same reason. | |
| void | tiny_text_style (TinyTextStyle *style, float size) |
| Fills a style in with the defaults. | |
| int | tiny_image_draw_text (TinyImage *image, const TinyFont *font, const char *text, int32_t x, int32_t y, const TinyTextStyle *style, const uint8_t *color) |
| Draws a line of text. | |
| int | tiny_image_draw_text_box (TinyImage *image, const TinyFont *font, const char *text, int32_t x, int32_t y, uint32_t width, uint32_t height, const TinyTextStyle *style, TinyTextAlign align, const uint8_t *color) |
| Draws text wrapped and aligned inside a rectangle. | |
| int | tiny_image_draw_text_in (TinyImage *image, const TinyFont *font, const char *text, int32_t x, int32_t y, const TinyTextBox *box, const TinyTextStyle *style, const uint8_t *color) |
| Draws text inside a rectangle, with the box's own settings. | |
| int | tiny_image_draw_text_set (TinyImage *image, const TinyFontSet *set, const char *text, int32_t x, int32_t y, const TinyTextBox *box, const TinyTextStyle *style, const uint8_t *color) |
| Draws text through a fallback chain. | |
| int | tiny_text_measure_set (const TinyFontSet *set, const char *text, const TinyTextBox *box, const TinyTextStyle *style, TinyTextMetrics *out) |
| Measures text as a fallback chain would set it. | |
| int | tiny_image_draw_text_runs (TinyImage *image, const TinyTextRun *runs, uint32_t count, int32_t x, int32_t y, const TinyTextBox *box, const uint8_t *color) |
| Draws a sequence of styled spans as one run of text. | |
| int | tiny_text_measure_runs (const TinyTextRun *runs, uint32_t count, const TinyTextBox *box, TinyTextMetrics *out) |
| Measures a sequence of styled spans without drawing them. | |
| int | tiny_text_lines (const TinyFont *font, const char *text, const TinyTextBox *box, const TinyTextStyle *style, TinyTextLine *lines, uint32_t capacity, uint32_t *count) |
| Measures a run line by line, without drawing it. | |
| int | tiny_text_measure (const TinyFont *font, const char *text, const TinyTextStyle *style, TinyTextMetrics *out) |
| Measures a run of text without drawing it. | |
| int | tiny_text_measure_wrapped (const TinyFont *font, const char *text, uint32_t width, const TinyTextStyle *style, TinyTextMetrics *out) |
| Measures a run of text as it would be wrapped inside a width. | |
| uint32_t | tiny_text_style_sizeof (void) |
| Size of a TinyTextStyle, for a host allocating one across the wasm boundary. | |
| uint32_t | tiny_text_box_sizeof (void) |
| Size of a TinyTextBox, for the same reason. | |
| uint32_t | tiny_text_line_sizeof (void) |
| Size of a TinyTextLine, for the same reason. | |
| uint32_t | tiny_text_metrics_sizeof (void) |
| Size of a TinyTextMetrics, for the same reason. | |
Font loading, text measurement and text drawing.
| #define TINYIMG_AXIS_ITALIC 0x6974616Cu |
The ital axis, 0 or 1.
| #define TINYIMG_AXIS_OPTICAL_SIZE 0x6F70737Au |
The opsz axis, the size the design is optimized for.
| #define TINYIMG_AXIS_SLANT 0x736C6E74u |
The slnt axis, degrees clockwise from upright.
| #define TINYIMG_AXIS_WEIGHT 0x77676874u |
The wght axis, 1 to 1000 by convention.
| #define TINYIMG_AXIS_WIDTH 0x77647468u |
The wdth axis, a percentage.
| #define TINYIMG_FONT_MAX_AXES 4 |
How many variation axes one face may be set on at once.
| #define TINYIMG_FONT_MAX_FALLBACK 4 |
How many faces one fallback chain holds.
| #define TINYIMG_TEXT_MAX_RUNS 16 |
How many styled spans one rich run holds.
| typedef enum TinyFontKind TinyFontKind |
Outline and bitmap face formats.
Dispatched from the first four bytes, so a caller never says which one they have. The three are genuinely different shapes rather than variants of one: an outline face has a curve per glyph and scales to any size, and a bitmap face has a fixed grid and one size.
| typedef enum TinyTextVAlign TinyTextVAlign |
Where a run sits inside the height it was given.
Needs a box height to mean anything; with none, every value is the same as TINYIMG_VALIGN_TOP.
| enum TinyFontKind |
Outline and bitmap face formats.
Dispatched from the first four bytes, so a caller never says which one they have. The three are genuinely different shapes rather than variants of one: an outline face has a curve per glyph and scales to any size, and a bitmap face has a fixed grid and one size.
| enum TinyTextAlign |
Where a line sits inside the width it was given.
| enum TinyTextOverflow |
What happens to text the box cannot hold.
| enum TinyTextVAlign |
Where a run sits inside the height it was given.
Needs a box height to mean anything; with none, every value is the same as TINYIMG_VALIGN_TOP.
| Enumerator | |
|---|---|
| TINYIMG_VALIGN_TOP | First line box against the top edge. |
| TINYIMG_VALIGN_MIDDLE | The run centered in the height. |
| TINYIMG_VALIGN_BOTTOM | Last line box against the bottom edge. |
| int tiny_font_axis | ( | const TinyFont * | font, |
| uint32_t | index, | ||
| TinyFontAxis * | out ) |
Reads one of a face's variation axes.
| font | The face. |
| index | Zero based, below tiny_font_axis_count. |
| out | Receives the axis. |
| uint32_t tiny_font_axis_count | ( | const TinyFont * | font | ) |
How many variation axes a face declares.
Zero for a static face, which is every face that carries no fvar table.
| font | The face. |
| void tiny_font_free | ( | TinyFont * | font | ) |
Releases whatever a face owns.
Never the bytes, which belong to the blob table or to the caller. Only a BDF face owns anything, so this is a no-op for the other two; calling it always is still the right habit and costs a branch.
| font | The face, left safe to load into again. |
| int tiny_font_has_glyph | ( | const TinyFont * | font, |
| uint32_t | codepoint ) |
Reports whether a face has a glyph for a codepoint.
| font | The face. |
| codepoint | The Unicode codepoint. |
| int tiny_font_load | ( | TinyFont * | font, |
| const char * | blob_id ) |
Loads a face from a resident blob.
The blob is what a Worker has: imported as a wrangler Data module, or fetched from a bucket and handed to tiny_blob_load. Its bytes stay owned by the blob table, so the face borrows them and tiny_blob_free invalidates it.
| font | Receives the face. |
| blob_id | The id it was loaded under, or NULL for the first font blob. |
| int tiny_font_load_bytes | ( | TinyFont * | font, |
| const uint8_t * | data, | ||
| size_t | size ) |
Loads a face from bytes the caller keeps.
The escape hatch from the blob table, and the difference is ownership: tiny_blob_load takes the bytes and frees them, this borrows them and frees nothing. Use it when the bytes are already somewhere convenient, or when a face is wanted for one call and should not occupy a blob slot.
| font | Receives the face. |
| data | The face's bytes, which must outlive the face. |
| size | How many. |
| int tiny_font_metrics | ( | const TinyFont * | font, |
| float | size, | ||
| TinyFontMetrics * | out ) |
Reads a face's vertical metrics at an em size.
| font | The face. |
| size | Em size in pixels. Zero, or any size for a bitmap face, reports the face's own. |
| out | Receives the metrics. |
| uint32_t tiny_font_metrics_sizeof | ( | void | ) |
Size of a TinyFontMetrics, for the same reason.
| int tiny_font_reset_axes | ( | TinyFont * | font | ) |
Returns a face to its default instance.
| font | The face. |
| int tiny_font_set_add | ( | TinyFontSet * | set, |
| const TinyFont * | font ) |
Appends a face to the end of a chain.
| set | The chain. |
| font | The face, which the chain borrows. |
| int tiny_font_set_axis | ( | TinyFont * | font, |
| uint32_t | tag, | ||
| float | value ) |
Sets one axis, in the axis' own units.
tiny_font_set_axis(&font, TINYIMG_AXIS_WEIGHT, 700.0f) is a bold instance of one variable file, drawn from the same bytes as the regular one. The value is clamped to the axis' range, so 900 on an axis that stops at 800 is 800 rather than an error.
The outlines are interpolated from the face's gvar deltas, which is what the format defines rather than an approximation of it: the deltas of every region the coordinate falls in are scaled and summed, and points no region mentions are inferred from their neighbors along the contour. Advances vary too, from the same deltas' phantom points, so a bolder instance is spaced as its designer set it.
A face with no fvar reports TINYIMG_ERR_UNSUPPORTED_VARIANT, and a tag it does not declare reports TINYIMG_ERR_NOT_FOUND. Neither is a failure a caller has to prevent: ask the face what it has with tiny_font_axis first, or ignore the result and get the default instance.
| font | The face, whose variation state this changes. |
| tag | The axis tag. |
| value | The value, in the axis' units. |
| const TinyFont * tiny_font_set_for | ( | const TinyFontSet * | set, |
| uint32_t | codepoint ) |
Which face in a chain covers a codepoint.
| set | The chain. |
| codepoint | The character. |
| int tiny_font_set_init | ( | TinyFontSet * | set | ) |
Starts an empty chain.
| set | The chain to initialize. |
| uint32_t tiny_font_sizeof | ( | void | ) |
Size of a TinyFont, for a host allocating one across the wasm boundary.
| int tiny_image_draw_text | ( | TinyImage * | image, |
| const TinyFont * | font, | ||
| const char * | text, | ||
| int32_t | x, | ||
| int32_t | y, | ||
| const TinyTextStyle * | style, | ||
| const uint8_t * | color ) |
Draws a line of text.
x and y are the top left of the line box, not the baseline, which is what every other drawing entry point in this library takes and what a caller placing a label wants. The baseline is y + metrics.ascent, so a caller who wants to sit text on a baseline subtracts that.
A newline in text starts a new line at x, so a short multi-line string needs no box.
| image | The image to draw on. |
| font | The face. |
| text | UTF-8, NUL terminated. A malformed sequence draws the replacement glyph rather than failing. |
| x | Left edge of the line box; may be negative. |
| y | Top edge of the line box; may be negative. |
| style | How to set it, or NULL for the defaults at the face's own size. |
| color | The color, as many channels as the image has. |
| int tiny_image_draw_text_box | ( | TinyImage * | image, |
| const TinyFont * | font, | ||
| const char * | text, | ||
| int32_t | x, | ||
| int32_t | y, | ||
| uint32_t | width, | ||
| uint32_t | height, | ||
| const TinyTextStyle * | style, | ||
| TinyTextAlign | align, | ||
| const uint8_t * | color ) |
Draws text wrapped and aligned inside a rectangle.
Wraps on spaces where it can and mid-word where a single word is wider than the box, so a long unbroken string is clipped by the box rather than running out of it. A line whose box has run out of height is not drawn, and the metrics still report every line the text would have taken, so a caller can tell that it overflowed.
| image | The image to draw on. |
| font | The face. |
| text | UTF-8, NUL terminated. |
| x | Left edge of the box. |
| y | Top edge of the box. |
| width | Width to wrap inside. Zero draws nothing. |
| height | Height to fill. Zero means no limit. |
| style | How to set it, or NULL for the defaults. |
| align | Where each line sits inside width. |
| color | The color, as many channels as the image has. |
| int tiny_image_draw_text_in | ( | TinyImage * | image, |
| const TinyFont * | font, | ||
| const char * | text, | ||
| int32_t | x, | ||
| int32_t | y, | ||
| const TinyTextBox * | box, | ||
| const TinyTextStyle * | style, | ||
| const uint8_t * | color ) |
Draws text inside a rectangle, with the box's own settings.
What tiny_image_draw_text_box does, plus vertical alignment, justification and an ellipsis. The two share every line of layout, so a caller who measures with tiny_text_lines and then draws gets the lines the measurement described.
A stroke or a shadow makes this three passes over the run rather than one: every shadow, then every outline, then every fill. Drawing each glyph's three layers together would let one glyph's outline print over the previous glyph's fill, which is visible wherever two letters touch.
| image | The image to draw on. |
| font | The face. |
| text | UTF-8, NUL terminated. |
| x | Left edge of the box. |
| y | Top edge of the box. |
| box | The rectangle and how the run sits in it, or NULL for none. |
| style | How to set it, or NULL for the defaults. |
| color | The fill color, as many channels as the image has. |
| int tiny_image_draw_text_runs | ( | TinyImage * | image, |
| const TinyTextRun * | runs, | ||
| uint32_t | count, | ||
| int32_t | x, | ||
| int32_t | y, | ||
| const TinyTextBox * | box, | ||
| const uint8_t * | color ) |
Draws a sequence of styled spans as one run of text.
The spans flow into each other rather than being drawn one after another: a wrap can land in the middle of a span, and a line that mixes sizes sits on one baseline, which is the largest ascent on that line. Two spans of different sizes therefore share a baseline rather than each having their own, which is what makes a superscript or a larger first letter look right.
Every span carries its own face, size, tracking, kerning, outline and shadow. Kerning does not cross a span boundary, because a kern pair is a property of one face and the two glyphs either side of the boundary may be in different ones.
| image | The image to draw on. |
| runs | The spans, in order. |
| count | How many, up to TINYIMG_TEXT_MAX_RUNS. |
| x | Left edge of the box. |
| y | Top edge of the box. |
| box | The rectangle and how the run sits in it, or NULL for none. |
| color | The fill color for any span that does not name one. |
| int tiny_image_draw_text_set | ( | TinyImage * | image, |
| const TinyFontSet * | set, | ||
| const char * | text, | ||
| int32_t | x, | ||
| int32_t | y, | ||
| const TinyTextBox * | box, | ||
| const TinyTextStyle * | style, | ||
| const uint8_t * | color ) |
Draws text through a fallback chain.
The same layout as tiny_image_draw_text_in, with the face chosen per codepoint instead of once for the run. A chain of one face is exactly the single-face path, so nothing about a caller who does not need this changes.
| image | The image to draw on. |
| set | The chain. |
| text | UTF-8, NUL terminated. |
| x | Left edge of the box. |
| y | Top edge of the box. |
| box | The rectangle and how the run sits in it, or NULL for none. |
| style | How to set it, or NULL for the defaults. |
| color | The fill color, as many channels as the image has. |
| uint32_t tiny_text_box_sizeof | ( | void | ) |
Size of a TinyTextBox, for the same reason.
| uint32_t tiny_text_line_sizeof | ( | void | ) |
Size of a TinyTextLine, for the same reason.
| int tiny_text_lines | ( | const TinyFont * | font, |
| const char * | text, | ||
| const TinyTextBox * | box, | ||
| const TinyTextStyle * | style, | ||
| TinyTextLine * | lines, | ||
| uint32_t | capacity, | ||
| uint32_t * | count ) |
Measures a run line by line, without drawing it.
The layout a draw would produce, one entry per line, including the lines the box's height leaves out and which line an ellipsis lands on. What a caller needs to place a badge beside the second line, or to tell that a headline was truncated and pick a smaller size.
| font | The face. |
| text | UTF-8, NUL terminated. |
| box | The rectangle it would be set in, or NULL for none. |
| style | How it would be set, or NULL for the defaults. |
| lines | Receives up to capacity entries. May be NULL to count only. |
| capacity | How many entries lines holds. |
| count | Receives the number of lines the run takes, which may be more than capacity. |
| int tiny_text_measure | ( | const TinyFont * | font, |
| const char * | text, | ||
| const TinyTextStyle * | style, | ||
| TinyTextMetrics * | out ) |
Measures a run of text without drawing it.
Named for the text rather than for an image because it takes none: the size of a string is a property of the face and the style. It is the same layout the drawing entry points use, so measuring and then drawing at the measured position lands where the measurement said.
| font | The face. |
| text | UTF-8, NUL terminated. Newlines count as line breaks. |
| style | How it would be set, or NULL for the defaults. |
| out | Receives the metrics. |
| int tiny_text_measure_runs | ( | const TinyTextRun * | runs, |
| uint32_t | count, | ||
| const TinyTextBox * | box, | ||
| TinyTextMetrics * | out ) |
Measures a sequence of styled spans without drawing them.
The same layout the draw uses. ascent and line_height are the largest of the spans on the first line, which is what the run actually occupies.
| runs | The spans, in order. |
| count | How many, up to TINYIMG_TEXT_MAX_RUNS. |
| box | The rectangle they would be set in, or NULL for none. |
| out | Receives the metrics. |
| int tiny_text_measure_set | ( | const TinyFontSet * | set, |
| const char * | text, | ||
| const TinyTextBox * | box, | ||
| const TinyTextStyle * | style, | ||
| TinyTextMetrics * | out ) |
Measures text as a fallback chain would set it.
| set | The chain. |
| text | UTF-8, NUL terminated. |
| box | The rectangle it would be set in, or NULL for none. |
| style | How it would be set, or NULL for the defaults. |
| out | Receives the metrics. missing counts the codepoints no face in the chain had a glyph for. |
| int tiny_text_measure_wrapped | ( | const TinyFont * | font, |
| const char * | text, | ||
| uint32_t | width, | ||
| const TinyTextStyle * | style, | ||
| TinyTextMetrics * | out ) |
Measures a run of text as it would be wrapped inside a width.
| font | The face. |
| text | UTF-8, NUL terminated. |
| width | Width to wrap inside. Zero measures without wrapping. |
| style | How it would be set, or NULL for the defaults. |
| out | Receives the metrics, whose lines is what the wrap produced. |
| uint32_t tiny_text_metrics_sizeof | ( | void | ) |
Size of a TinyTextMetrics, for the same reason.
| void tiny_text_style | ( | TinyTextStyle * | style, |
| float | size ) |
Fills a style in with the defaults.
| style | Receives them. |
| size | Em size in pixels. |
| uint32_t tiny_text_style_sizeof | ( | void | ) |
Size of a TinyTextStyle, for a host allocating one across the wasm boundary.