Skip to content

UXFont

UXFont is a font descriptor: a family, a point size, and the bold and italic traits. A font chooser edits this value and text drawing carries it.

Glyph rasterisation and the list of available families belong to the backend. This type touches neither, so it works the same on GEM and macOS, and it can be compared, stored and passed around freely.

#use <UXKit> // or #import "UXFont.xc"
UXFont* base = UXFont.make((u8*)"Helvetica", 12);
UXFont* head = base.bolded().withSize(18);

Every derivation returns a new font. Nothing mutates:

base.bolded(); // Helvetica 12 Bold
base; // Helvetica 12 — unchanged

The type is built around this property. Whoever else holds a reference to a font handed to a label cannot change it underneath the label, so there is no defensive copying and no question about who owns a style.

Derivations chain, which is how a style menu composes:

base.bolded().italicized().withSize(14); // Helvetica 14 Bold Italic

make · makeTraits · dup · withSize · withFamily · bolded · unbolded · italicized · togglingBold · togglingItalic · scaledBy · isBold · isItalic · isEqualTo · description

static UXFont* make(u8* family, i16 size)

Family and point size, no traits. The default font is System 12.

static UXFont* makeTraits(u8* family, i16 size, bool bold, bool italic)

Sets all four at once. Use it to restore a saved font, where the traits are known rather than derived.

UXFont* dup(void)

An independent copy. Every derivation below is dup plus one change, so none of them can affect the receiver.

UXFont* withSize(i16 s)
UXFont* withFamily(u8* fam)
UXFont* bolded(void)
UXFont* unbolded(void)

Set the trait, regardless of its previous value.

UXFont* italicized(void)

Sets italic on, regardless of its previous value.

UXFont* togglingBold(void)
UXFont* togglingItalic(void)

Flip the trait. A Bold or Italic menu item uses this pair: the menu does not need to know the current state, and applying it twice returns the original font.

UXFont* b = base.togglingBold(); // Helvetica 12 Bold
b.togglingBold(); // Helvetica 12
UXFont* scaledBy(i16 pct)

A percentage of the current size, integer-rounded: scaledBy(150) on 12 gives 18. Use it to make text one step larger without knowing the base size.

bool isBold(void)
bool isItalic(void)
bool isEqualTo(UXFont* o)

Value equality: family, size and both traits. Two separately constructed Helvetica 12s are equal:

UXFont* a = UXFont.make((u8*)"Helvetica", 12);
UXFont* b = UXFont.make((u8*)"Helvetica", 12);
a.isEqualTo(b) // true
a.isEqualTo(a.bolded()) // false
u8* description(void)

A human-readable label such as "Helvetica 12 Bold Italic". Traits appear only when set, so a plain font is "Helvetica 12". A chooser’s preview line and a font menu item show this string.

#import <Stdio.xc>
#import "UXFont.xc"
void main(void) {
UXFont* base = UXFont.make((u8*)"Helvetica", 12);
base.bolded(); // Helvetica 12 Bold
base.italicized(); // Helvetica 12 Italic
base.withSize(18); // Helvetica 18
base.scaledBy(150); // Helvetica 18
base; // Helvetica 12 — untouched
base.bolded().italicized().withSize(14); // Helvetica 14 Bold Italic
UXFont* b = base.togglingBold(); // Helvetica 12 Bold
b.togglingBold(); // Helvetica 12
UXFont* other = UXFont.make((u8*)"Helvetica", 12);
base.isEqualTo(other); // true
base.isEqualTo(base.bolded()); // false
}

The full program is website/site/examples/uxkit/font.xc. The doc-examples gate compiles it, and the comments are its real output.

  • A plain class (not an Object subclass)