Skip to content

Text Rendering

Tara K edited this page Apr 10, 2018 · 5 revisions

Built-in Fonts

  • Droid Sans Fallback:

  • Droid Sans Fallback 11x12

  • Droid Sans Fallback 16x17

  • Droid Sans Fallback 25x25

  • Droid Sans Mono:

  • Droid Sans Mono 7x12

  • Droid Sans Mono 13x21

  • Droid Sans Mono 17x30

  • Liberation Mono:

  • Liberation Mono 8x11

  • Liberation Mono 13x20

  • Liberation Mono 17x27

Text Anchors:

  • TextAnchor_East
  • TextAnchor_West
  • TextAnchor_North
  • TextAnchor_South
  • TextAnchor_NorthEast
  • TextAnchor_NorthWest
  • TextAnchor_SouthEast
  • TextAnchor_SouthWest
  • TextAnchor_Center

Rather than write a description for each one of these, just imagine a compass on top of your display.
For example, text with TextAnchor_SouthEast will be drawn in the bottom right of the screen.

SSD1306_SetFont:

bool SSD1306_SetFont( struct SSD1306_Device* Display, const struct SSD1306_FontDef* Font )

Sets the current font to (Font) which will be used for all text operations until changed again.

Parameters:

  1. Pointer to display handle
  2. Pointer to font definition

Returns:

True if the display handle and font are valid

SSD1306_FontForceProportional:

void SSD1306_FontForceProportional( struct SSD1306_Device* Display, bool Force )

Forces the current font to be rendered as proportional even if it's defined as monospace.
No effect if the font is already proportional.

Parameters:

  1. Pointer to display handle
  2. True to force proportional font rendering

SSD1306_FontForceMonospace:

void SSD1306_FontForceMonospace( struct SSD1306_Device* Display, bool Force )

Forces a proportional font to be rendered as if it was monospace.
No effect if the font is already monospace.
Note: This looks weird in my opinion but eh, someone might use it :)

Parameters:

  1. Pointer to display handle
  2. True to force monospace font rendering

SSD1306_FontGetWidth:

int SSD1306_FontGetWidth( struct SSD1306_Device* Display )

For monospace fonts this returns the width of every character, for proportional fonts this returns the width of the widest character.

Parameters:

  1. Pointer to display handle

SSD1306_FontGetHeight:

int SSD1306_FontGetHeight( struct SSD1306_Device* Display )

Returns the height of the current font, it is the same for all characters.

Parameters:

  1. Pointer to display handle

SSD1306_FontGetCharWidth:

int SSD1306_FontGetCharWidth( struct SSD1306_Device* Display, char Character )

Returns the width in pixels of character (Character) in the current font.

Parameters:

  1. Pointer to display handle
  2. Character to get the width of

Returns:

Width of the given character (Character).
Note: Returns 0 if (Character) does not exist in the font or a font was not set.

SSD1306_FontGetMaxCharsPerRow:

int SSD1306_FontGetMaxCharsPerRow( struct SSD1306_Device* Display )

For monospace fonts this is pretty straightforward, it returns the width of the display divided by the font width.
For proportional fonts it returns the theoretical max number of characters if all characters were the widest in the font.

Parameters:

  1. Pointer to display handle

Returns:

Maximum characters per line that can be displayed with the current font.
Note: See above.

SSD1306_FontGetMaxCharsPerColumn:

int SSD1306_FontGetMaxCharsPerColumn( struct SSD1306_Device* Display )

Very straightforward, basically returns the screen's height divided by the height of the current font.

Parameters:

  1. Pointer to display handle

Returns:

Maximum number of characters per column that can be displayed with the current font.

SSD1306_FontDrawChar:

void SSD1306_FontDrawChar( struct SSD1306_Device* Display, char Character, int x, int y, int Color )

Draws character (Character) at location (x,y) in color (Color) with the current font.

Parameters:

  1. Pointer to display handle
  2. Character to draw
  3. X Coordinate
  4. Y Coordinate
  5. Color to draw character with

SSD1306_FontMeasureString:

int SSD1306_FontMeasureString( struct SSD1306_Device* Display, const char* Text )

Returns the width of the given string (Text) in pixels.

Parameters:

  1. Pointer to display handle
  2. String to measure

Returns:

Width in pixels of the given string (Text).
Note: Can return 0 if the font contains none of the characters contained in (Text)

SSD1306_FontDrawString:

void SSD1306_FontDrawString( struct SSD1306_Device* Display, int x, int y, const char* Text, int Color )

Draws the string (Text) at (x,y) in color (Color) using the current font.

Parameters:

  1. Pointer to display handle
  2. X Coordinate
  3. Y Coordinate
  4. String to draw
  5. Color to draw string with

SSD1306_FontDrawAnchoredString:

void SSD1306_FontDrawAnchoredString( struct SSD1306_Device* Display, TextAnchor Anchor, const char* Text, int Color )

Draws the given string (Text) aligned to (Anchor) with the color (Color) with the current font.

Parameters:

  1. Pointer to display handle
  2. TextAnchor Which tells us where to align (Text) to
  3. String to draw
  4. Color to draw the string with

SSD1306_FontGetAnchoredStringCoords:

void SSD1306_FontGetAnchoredStringCoords( struct SSD1306_Device* Display, int* OutX, int* OutY, TextAnchor Anchor, const char* Text )

It's kind of like SSD1306_FontDrawAnchoredString except it returns where it would draw the string at instead of actually drawing it.

Parameters:

  1. Pointer to display handle
  2. Pointer to integer which will receive x coordinate
  3. Pointer to integer which will receive y coordinate
  4. TextAnchor Which tells us where to align (Text) to
  5. String that would have been drawn