ObjectivelyMVC
Object oriented MVC framework for SDL3 and GNU C
Loading...
Searching...
No Matches
Text Struct Reference

Text rendered with TrueType fonts. More...

#include <Text.h>

Inheritance diagram for Text:
View

Public Member Functions

Class * _Text (void)
 
Text * initWithText (Text *self, const char *text, Font *font)
 Initializes this Text with the given text and Font.
 
SDL_Size naturalSize (const Text *self)
 Resolves the rendered size of this Text.
 
void setFont (Text *self, Font *font)
 Sets this Text's font.
 
void setText (Text *self, const char *text)
 Sets this Text's text.
 
void setTextWithFormat (Text *self, const char *fmt,...)
 Sets this Text's text with the given format string.
 
void setTransform (Text *self, TextTransform transform)
 Sets the case transform this Text draws with. Text::text is left as set.
 
SDL_Size sizeText (const Text *self, const char *text)
 Measures the given text as this Text would render it: in its Font, with color escapes removed and registered icons from its window's Theme in place.
 
- Public Member Functions inherited from View
Class * _View (void)
 The View archetype.
 
bool acceptsKeyResponder (const View *self)
 
bool acceptsTouchResponder (const View *self)
 
void addClassName (View *self, const char *className)
 Adds the given class name to this View.
 
void addSubview (View *self, View *subview)
 Adds a subview to this view, to be drawn above its siblings.
 
void addSubviewRelativeTo (View *self, View *subview, View *other, ViewPosition position)
 Adds a subview to this view, positioned relatively to other.
 
 ancestorWithIdentifier (const View *self, const char *identifier)
 
void applyStyle (View *self, const Style *style)
 Applies the given Style to this View.
 
void applyTheme (View *self, const Theme *theme)
 Applies the given Theme to this View.
 
void applyThemeIfNeeded (View *self, const Theme *theme)
 Recursively applies the Theme to this View and its subviews.
 
void attachStylesheet (View *self, SDL_Window *window)
 Attaches this View's Stylesheet to the Theme associated with the given window.
 
void awakeWithCharacters (View *self, const char *chars)
 Wakes this View with the given null-terminated JSON C string.
 
void awakeWithData (View *self, const Data *data)
 Wakes this View with the specified JSON Data.
 
void awakeWithDictionary (View *, const Dictionary *)
 Wakes this View with the specified Dictionary.
 
void awakeWithResource (View *self, const Resource *resource)
 Wakes this View with the specified Resource.
 
void awakeWithResourceName (View *self, const char *name)
 Wakes this View with the Resource by the specified name.
 
void becomeKeyResponder (View *self)
 Become the key responder in the View hierarchy.
 
void becomeTouchResponder (View *self)
 Become the touch responder in the View hierarchy.
 
bool bind (View *self, const Inlet *inlets, const Dictionary *dictionary)
 Performs data binding for the Inlets described in dictionary.
 
SDL_Rect bounds (const View *self)
 
void bringSubviewToFront (View *self, View *subview)
 Brings the specified subview to the front.
 
void clearWarnings (const View *self, WarningType type)
 Clears this View's Warnings matching the given level.
 
SDL_Rect clippingFrame (const View *self)
 
bool containsPoint (const View *self, const SDL_Point *point)
 
int depth (const View *self)
 
 descendantWithIdentifier (const View *self, const char *identifier)
 
void detachStylesheet (View *self, SDL_Window *window)
 Detaches this View's Stylesheet from the Theme associated with the given window.
 
void didMoveToWindow (View *self, SDL_Window *window)
 Informs this View that it has been added to the View hierachy of the given window.
 
bool didReceiveEvent (const View *self, const SDL_Event *event)
 
void draw (View *self, Renderer *renderer)
 Draws this View.
 
void emitViewEvent (View *self, ViewEvent code, ident data)
 Emits a ViewEvent originating from this View.
 
void enumerate (const View *self, ViewEnumerator enumerator, ident data)
 
void enumerate (View *self, ViewEnumerator enumerator, ident data)
 Enumerates this View and its descendants, applying enumerator to each.
 
void enumerateAdjacent (const View *self, ViewEnumerator enumerator, ident data)
 Enumerates adjacent siblings of this View, applying enumerator to each.
 
void enumerateAncestors (const View *self, ViewEnumerator enumerator, ident data)
 Enumerates all ancestors of this View, applying enumerator to each.
 
void enumerateDescendants (const View *self, ViewEnumerator enumerator, ident data)
 Enumerates all descendants of this View, applying enumerator to each.
 
void enumerateSelection (View *self, const char *rule, ViewEnumerator enumerator, ident data)
 Enumerates all Views in the selection matched by rule, applying enumerator to each.
 
void enumerateSiblings (const View *self, ViewEnumerator enumerator, ident data)
 Enumerates all siblings of this View, applying enumerator to each.
 
void enumerateSubviews (const View *self, ViewEnumerator enumerator, ident data)
 Enumerates all subviews of this View, applying enumerator to each.
 
void enumerateSuperview (const View *self, ViewEnumerator enumerator, ident data)
 Enumerates the superview of this View, if any, applying enumerator to it.
 
void enumerateVisible (const View *self, ViewEnumerator enumerator, ident data)
 
void enumerateVisible (View *self, ViewEnumerator enumerator, ident data)
 Enumerates this View and its visible descendants, applying enumerator to each.
 
bool hasClassName (const View *self, cosnt char *className)
 
bool hasOverflow (const View *self)
 
View * hitTest (const View *self, const SDL_Point *point)
 Performs a hit test against this View and its descendants for the given point.
 
View * init (View *self)
 Initializes this View.
 
View * initWithFrame (View *self, const SDL_Rect *frame)
 Initializes this View with the specified frame.
 
void invalidateStyle (View *self)
 Invalidates the computed Style for this View and its descendants.
 
bool isContainer (const View *self)
 
bool isDescendantOfView (const View *self, const View *view)
 
bool isKeyResponder (const View *self)
 
bool isTouchResponder (const View *self)
 
bool isVisible (const View *self)
 
void layoutIfNeeded (View *self)
 Recursively updates the layout of this View and its subviews.
 
 layoutSubviews (View *self)
 Arranges this View's immediate subviews within its (already final) frame.
 
void layoutWithConstraint (View *self, ViewConstraint width, ViewConstraint height)
 Resolves this View's size against the given ViewConstraints, then arranges it.
 
void layoutWithSize (View *self, const SDL_Size *size)
 Resizes this View to the given size, then arranges it.
 
bool matchesSelector (const View *self, const SimpleSelector *simpleSelector)
 
void moveToWindow (View *self, SDL_Window *window)
 Moves this View to the View hierarchy of the given window.
 
String * path (const View *self)
 
void removeAllClassNames (View *self)
 Removes all class names from this View.
 
void removeAllSubviews (View *self)
 Removes all subviews from this View.
 
void removeClassName (View *self, const char *className)
 Removes the given class name to this View.
 
void removeFromSuperview (View *self)
 Removes this View from its superview.
 
void removeSubview (View *self, View *subview)
 Removes the given subview from this View.
 
void render (View *self, Renderer *renderer)
 Renders this View using the given renderer.
 
void renderDeviceDidReset (View *self)
 Informs this View that the render device has reset.
 
void renderDeviceWillReset (View *self)
 Informs this View that the render device will reset.
 
SDL_Rect renderFrame (const View *self)
 
void replaceSubview (View *self, View *subview, View *replacement)
 Replaces the specified subview with the given replacement.
 
void resignKeyResponder (View *self)
 Resigns key responder priority.
 
void resignTouchResponder (View *self)
 Resigns touch responder priority.
 
void resize (View *self, const SDL_Size *size)
 Resizes this View to the specified size.
 
void resolve (View *self, Outlet *outlets)
 Resolves the given Outlets from this View's hierarchy.
 
void respondToEvent (View *self, const SDL_Event *event)
 Responds to the specified event.
 
Set * select (View *self, const char *rule)
 Resolves all descendants (including this View) that match the given Selector rule.
 
View * selectFirst (View *self, const char *rule)
 Resolves the first descendant View that matches the given Selector rule.
 
void setNeedsApplyTheme (View *self)
 Marks this View as needing Theme application before it is next drawn.
 
void setNeedsLayout (View *self)
 Marks this View as needing layout before it is next drawn.
 
void setVisibility (View *self, ViewVisibility visibility)
 Sets this View's visibility.
 
SDL_Size size (const View *self)
 
SDL_Size sizeThastFills (const View *self)
 
SDL_Size sizeThatContains (const View *self)
 
SDL_Size sizeThatFills (const View *self)
 
void sizeThatFits (const View *self)
 
SDL_Size sizeThatSatisfies (View *self, ViewConstraint width, ViewConstraint height)
 Resolves the size this View wants to be, within the offered ViewConstraints.
 
void sizeToContain (View *self)
 Resizes this View to contain its subviews.
 
void sizeToFill (View *self)
 Resizes this View to fill its superview.
 
void sizeToFit (View *self)
 Resizes this View to fit its subviews.
 
void sizeToSatisfy (View *self, ViewConstraint width, ViewConstraint height)
 Resizes this View to satisfy the given ViewConstraints.
 
 subviewWithIdentifier (const View *self, const char *identifier)
 
void updateBindings (View *self, ident data)
 Updates data bindings, prompting the appropriate layout changes.
 
SDL_Rect viewport (const View *self)
 
View * viewWithCharacters (const char *chars, Outlet *outlets)
 Instantiates a View initialized with the given null-terminated JSON C string.
 
View * viewWithData (const Data *data, Outlet *outlets)
 Instantiates a View initialized with the contents of data.
 
View * viewWithDictionary (const Dictionary *dictionary, Outlet *outlets)
 Instantiates a View initialized with the attributes described in dictionary.
 
View * viewWithResource (const Resource *resource, Outlet *outlets)
 Instantiates a View initialized with the JSON data in resource.
 
View * viewWithResourceName (const char *name, Outlet *outlets)
 Instantiates a View initialized with the JSON Resource with the specified name.
 
Array * visibleSubviews (const View *self)
 
void warn (View *self, ViewWarningType type, const char *fmt,...)
 
void warn (View *self, WarningType level, const char *fmt,...)
 Appends a warning for this View.
 
void willMoveToWindow (View *self, SDL_Window *window)
 Informs this View that it will be added to the View hierarchy for the given window.
 

Data Fields

SDL_Color color
 The text color.
 
Font * font
 The Font. A fixed-width Font carries a bitmap, and is drawn as one quad per glyph from its glyph sheet instead of being rasterized per string; color and color escapes then apply per vertex, for free.
 
bool lineWrap
 If true, wrap text along word boundaries to fit this Text's width.
 
char * text
 The text.
 
TextTransform transform
 The case transform applied when drawing: the text-transform style attribute, or textTransform in JSON. A computed style without text-transform resets it to none, as with color.
 
View view
 The superclass.
 
- Data Fields inherited from View
ViewAlignment alignment
 The alignment.
 
int autoresizingMask
 The ViewAutoresizing bitmask.
 
SDL_Color backgroundColor
 The background color.
 
int backgroundGradientAngle
 The angle of the background gradient, in degrees, clockwise from south: 0 (the default) fills downwards, 90 to the right, 180 upwards.
 
SDL_Color backgroundGradientColor
 The far color of the background gradient, which runs from View::backgroundColor.
 
SDL_Color borderColor
 The border color.
 
int borderRadius
 The corner radius applied to the background, border and bevel.
 
int borderWidth
 The border width.
 
Set * classNames
 The class names.
 
bool clipsSubviews
 If true, subviews will be clipped to this View's frame.
 
Style * computedStyle
 The computed Style of this View.
 
ViewCornerCut cornerCut
 The horizontal inset of each corner of the background and border.
 
char * identifier
 An optional identifier.
 
SDL_Size maxSize
 The maximum size this View may be resized to during layout.
 
SDL_Size minSize
 The minimum size this View may be resized to during layout.
 
bool needsApplyTheme
 If true, this View will apply the Theme before it is drawn.
 
bool needsLayout
 If true, this View will layout its subviews before it is drawn.
 
View * nextResponder
 The next responder, or event handler, in the chain.
 
Object object
 The superclass.
 
ViewPadding padding
 The padding.
 
ViewPointerEvents pointerEvents
 Whether this View is the result of a hit test; its subviews always are.
 
bool stretch
 Whether this View takes a share of the space its StackView leaves along its axis.
 
Style * style
 The element-level Style of this View.
 
Stylesheet * stylesheet
 An optional Stylesheet.
 
Array * subviews
 The immediate subviews.
 
View * superview
 The super View.
 
ViewController * viewController
 The ViewController.
 
ViewVisibility visibility
 Whether this View is drawn, and contributes to its superview's size.
 
Array * warnings
 The Warnings this View generated.
 
SDL_Window * window
 The window.
 

Protected Attributes

TextInterface * interface [0]
 The interface type.
 
size_t runCount
 The number of entries in runs.
 
TextRun * runs
 The runs of texture, one textured quad each, when the text contains escapes; otherwise NULL, and texture is drawn as a single quad in color.
 
Texture * texture
 The rendered GPU texture.
 
- Protected Attributes inherited from View
ViewInterface * interface [0]
 The interface type.
 

Detailed Description

Text rendered with TrueType fonts.

Definition at line 131 of file Text.h.

Member Function Documentation

◆ _Text()

Class * _Text ( void  )

Definition at line 988 of file Text.c.

988 {
989 static Class *clazz;
990 static Once once;
991
992 do_once(&once, {
993 clazz = _initialize(&(const ClassDef) {
994 .name = "Text",
995 .superclass = _View(),
996 .instanceSize = sizeof(Text),
997 .interfaceSize = sizeof(TextInterface),
998 .initialize = initialize,
999 });
1000 });
1001
1002 return clazz;
1003}
static void initialize(Class *clazz)
Definition AtlasImage.c:74
Text rendered with TrueType fonts.
Definition Text.h:131
Class * _View(void)
The View archetype.
Definition View.c:2336

◆ initWithText()

Text * initWithText ( Text *  self,
const char *  text,
Font *  font 
)

Initializes this Text with the given text and Font.

Parameters
selfThe Text.
textThe text.
fontThe Font (optional).
Returns
The initialized Text, or NULL on error.

Definition at line 808 of file Text.c.

808 {
809
810 self = (Text *) super(View, self, initWithFrame, NULL);
811 if (self) {
812 $(self, setFont, font);
813 $(self, setText, text);
814 }
815
816 return self;
817}
void setFont(Text *self, Font *font)
Sets this Text's font.
Definition Text.c:880
void setText(Text *self, const char *text)
Sets this Text's text.
Definition Text.c:899
Font * font
The Font. A fixed-width Font carries a bitmap, and is drawn as one quad per glyph from its glyph shee...
Definition Text.h:157
char * text
The text.
Definition Text.h:203
Views are the fundamental building blocks of ObjectivelyMVC user interfaces.
Definition View.h:225
View * initWithFrame(View *self, const SDL_Rect *frame)
Initializes this View with the specified frame.
Definition View.c:1047

◆ naturalSize()

SDL_Size naturalSize ( const Text *  self)

Resolves the rendered size of this Text.

Parameters
selfThe Text.

Definition at line 823 of file Text.c.

823 {
824
825 Font *font = self->font;
826 if (font == NULL) {
827 return MakeSize(0, 0);
828 }
829
830 // Nominally const, but a changed icon atlas invalidates the texture and runs here. Safe only
831 // because layout precedes drawing within a frame and the Renderer keeps unretained Textures.
832 Text *this = (Text *) self;
833
834 checkIcons(this);
835
836 if (self->naturalSizeCache.isValid && font->pixelDensity == self->naturalSizeCache.pixelDensity) {
837 return self->naturalSizeCache.size;
838 }
839
840 const SDL_Size size = $(self, sizeText, displayText(self) ?: "");
841
842 this->naturalSizeCache.size = size;
843 this->naturalSizeCache.pixelDensity = font->pixelDensity;
844 this->naturalSizeCache.isValid = true;
845
846 return size;
847}
static void checkIcons(Text *self)
Invalidates this Text if the icon atlas it was prepared against has changed: a Theme swap,...
Definition Text.c:368
static const char * displayText(const Text *self)
Definition Text.c:385
TrueType fonts.
Definition Font.h:88
float pixelDensity
The display pixel density scale, greater than 1.0 on high-density displays.
Definition Font.h:134
float pixelDensity
Definition Text.h:171
SDL_Size sizeText(const Text *self, const char *text)
Measures the given text as this Text would render it: in its Font, with color escapes removed and reg...
Definition Text.c:853
SDL_Size size
Definition Text.h:170
bool isValid
Definition Text.h:172

◆ setFont()

void setFont ( Text *  self,
Font *  font 
)

Sets this Text's font.

Parameters
selfThe Text.
fontThe Font to set.

Definition at line 880 of file Text.c.

880 {
881
882 font = font ?: $$(Font, defaultFont);
883
884 if (font != self->font) {
885
886 release(self->font);
887 self->font = retain(font);
888
889 invalidate(self);
890
891 $((View *) self, sizeToFit);
892 }
893}
static Font * defaultFont(void)
Definition Font.c:159
static void invalidate(Text *self)
Releases this Text's rendered texture and runs, and invalidates its cached size.
Definition Text.c:279
void sizeToFit(View *self)
Resizes this View to fit its subviews.
Definition View.c:2018

◆ setText()

void setText ( Text *  self,
const char *  text 
)

Sets this Text's text.

Parameters
selfThe Text.
textThe text to set.

Definition at line 899 of file Text.c.

899 {
900
901 if (strcmp(self->text ?: "", text ?: "")) {
902
903 free(self->text);
904
905 if (text && strlen(text)) {
906 self->text = strdup(text);
907 } else {
908 self->text = NULL;
909 }
910
911 applyTransform(self);
912 invalidate(self);
913
914 $((View *) self, sizeToFit);
915 }
916}
static void applyTransform(Text *self)
Rebuilds transformed from text and transform. Color escapes pass through without counting as letters,...
Definition Text.c:311

◆ setTextWithFormat()

void setTextWithFormat ( Text *  self,
const char *  fmt,
  ... 
)

Sets this Text's text with the given format string.

Parameters
selfThe Text.
fmtThe format string.

Definition at line 925 of file Text.c.

925 {
926
927 va_list args;
928 va_start(args, fmt);
929
930 char *text;
931 const int len = vasprintf(&text, fmt, args);
932 if (len >= 0) {
933 $(self, setText, text);
934 }
935
936 free(text);
937 va_end(args);
938}

◆ setTransform()

void setTransform ( Text *  self,
TextTransform  transform 
)

Sets the case transform this Text draws with. Text::text is left as set.

Parameters
selfThe Text.
transformThe TextTransform.

Definition at line 944 of file Text.c.

944 {
945
946 if (transform != self->transform) {
947 self->transform = transform;
948
949 applyTransform(self);
950 invalidate(self);
951
952 $((View *) self, sizeToFit);
953 }
954}
TextTransform transform
The case transform applied when drawing: the text-transform style attribute, or textTransform in JSON...
Definition Text.h:218

◆ sizeText()

SDL_Size sizeText ( const Text *  self,
const char *  text 
)

Measures the given text as this Text would render it: in its Font, with color escapes removed and registered icons from its window's Theme in place.

Parameters
selfThe Text.
textThe text.
Returns
The size, in logical pixels.

Definition at line 853 of file Text.c.

853 {
854
855 SDL_Size size = MakeSize(0, 0);
856
857 if (self->font == NULL || text == NULL) {
858 return size;
859 }
860
861 const ImageAtlas *icons = iconsFor(self);
862
863 if (self->font->bitmap.surface) {
864 $(self->font, sizeBitmapCharacters, text, 0, icons, &size.w, &size.h);
865 } else if (hasEscapes(text)) {
866 char *layout = MVC_LayoutText(self->font, text, self->color, icons, NULL, NULL);
867 $(self->font, sizeCharacters, layout, &size.w, &size.h);
868 free(layout);
869 } else {
870 $(self->font, sizeCharacters, text, &size.w, &size.h);
871 }
872
873 return size;
874}
static void sizeCharacters(const Font *self, const char *chars, int *w, int *h)
Definition Font.c:352
static bool hasEscapes(const char *text)
Definition Text.c:392
static ImageAtlas * iconsFor(const Text *self)
Definition Text.c:297
OBJECTIVELYMVC_EXPORT char * MVC_LayoutText(const Font *font, const char *text, SDL_Color color, const ImageAtlas *icons, TextSpan **spans, size_t *count)
Resolves the escapes in text into the string to lay out and the spans to draw it as.
Definition Text.c:220
SDL_Surface * surface
The glyph sheet: a grid of FONT_BITMAP_COLUMNS by FONT_BITMAP_COLUMNS cells of cellSize,...
FontBitmap bitmap
The glyphs of a fixed-width Font, baked into one sheet; bitmap.surface is NULL for a proportional Fon...
Definition Font.h:107
Packs many Images into one sheet, backed by one Texture.
Definition ImageAtlas.h:49
SDL_Color color
The text color.
Definition Text.h:148

Field Documentation

◆ atlas

const ImageAtlas* Text::atlas

Definition at line 181 of file Text.h.

◆ color

SDL_Color Text::color

The text color.

Remarks
Styled. Set from a Style, and overwritten by the next one applied. Assign View::style, or a stylesheet rule, rather than this field.

Definition at line 148 of file Text.h.

◆ font

Font* Text::font

The Font. A fixed-width Font carries a bitmap, and is drawn as one quad per glyph from its glyph sheet instead of being rasterized per string; color and color escapes then apply per vertex, for free.

Remarks
Do not set this property directly.
See also
Text::setFont(Text *, Font *)

Definition at line 157 of file Text.h.

◆ generation

unsigned Text::generation

Definition at line 182 of file Text.h.

◆ interface

TextInterface* Text::interface[0]
protected

The interface type.

Definition at line 142 of file Text.h.

◆ isValid

bool Text::isValid

Definition at line 172 of file Text.h.

◆ lineWrap

bool Text::lineWrap

If true, wrap text along word boundaries to fit this Text's width.

Definition at line 162 of file Text.h.

◆ pixelDensity

float Text::pixelDensity

Definition at line 171 of file Text.h.

◆ runCount

size_t Text::runCount
protected

The number of entries in runs.

Definition at line 196 of file Text.h.

◆ runs

TextRun* Text::runs
protected

The runs of texture, one textured quad each, when the text contains escapes; otherwise NULL, and texture is drawn as a single quad in color.

Definition at line 190 of file Text.h.

◆ size

SDL_Size Text::size

Definition at line 170 of file Text.h.

◆ text

char* Text::text

The text.

Remarks
Do not set this property directly.
See also
Text::setText(Text *, const char *)

Definition at line 203 of file Text.h.

◆ texture

Texture* Text::texture
protected

The rendered GPU texture.

Definition at line 209 of file Text.h.

◆ transform

TextTransform Text::transform

The case transform applied when drawing: the text-transform style attribute, or textTransform in JSON. A computed style without text-transform resets it to none, as with color.

Remarks
Do not set this property directly.
See also
Text::setTransform(Text *, TextTransform)

Definition at line 218 of file Text.h.

◆ view

View Text::view

The superclass.

Definition at line 136 of file Text.h.


The documentation for this struct was generated from the following files: