Ersa Wearable Platform
Embedded smartwatch firmware and application framework
Loading...
Searching...
No Matches
text_layout.h
Go to the documentation of this file.
1#pragma once
2#include "ersa/hal/display.h"
3#include <string.h>
4
5namespace WatchText {
6// Measure the selected font instead of guessing character counts on a 200px display.
7inline void line(ersa::hal::IDisplay& display, const char* text, int16_t x, int16_t y, uint16_t width) {
8 char buffer[96];
9 strncpy(buffer, text ? text : "", sizeof(buffer) - 4);
10 buffer[sizeof(buffer) - 4] = 0;
11 int16_t bx, by;
12 uint16_t w, h;
13 display.getTextBounds(buffer, 0, 0, &bx, &by, &w, &h);
14 if (w > width) {
15 size_t n = strlen(buffer);
16 do {
17 if (n == 0) break;
18 --n;
19 while (n && (uint8_t(buffer[n]) & 0xc0) == 0x80) --n;
20 strcpy(buffer + n, "...");
21 display.getTextBounds(buffer, 0, 0, &bx, &by, &w, &h);
22 } while (w > width);
23 }
24 display.setCursor(x, y);
25 display.print(buffer);
26}
27}
Definition display.h:40
virtual void setCursor(int16_t x, int16_t y)=0
virtual void getTextBounds(const char *text, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h)=0
virtual size_t print(const char *text)=0
Definition text_layout.h:5
void line(ersa::hal::IDisplay &display, const char *text, int16_t x, int16_t y, uint16_t width)
Definition text_layout.h:7