5#include "TinyGPU/Font/IFont.h"
6#include "TinyGPU/Surface/ISurface.h"
11
12
13
14
15
16template <
typename RGB_T =
RGB565>
21 LinePrinter(
IFont<RGB_T>& font, ISurface<RGB_T>& target) : font_(&font), target_(&target) {
22 currentX_ = leftBorder_;
23 currentY_ = topBorder_;
26 void setColor(RGB_T color) { foregroundColor_ = color; }
32 void setTarget(ISurface<RGB_T>& target) { target_ = ⌖ }
45 setLeftBorder(border);
46 setRightBorder(border);
47 setButtomBorder(border);
53 currentX_ = leftBorder_;
54 currentY_ = topBorder_ + (line * lineAdvance());
66 void setScale(uint8_t scale) {
this->scale = scale; }
69 void setSpacing(uint8_t spacing) {
this->spacing = spacing; }
76 if (font_ !=
nullptr) {
77 font_->setProportional(proportional);
82 return font_ !=
nullptr && font_->isProportional();
87 if (text ==
nullptr || *text ==
'\0') {
91 ensureCursorInitialized();
94 size_t pendingSpaces = 0;
95 size_t printedLength = 0;
97 for (
const char* current = text; *current !=
'\0'; ++current) {
98 if (*current ==
'\n') {
99 printWord(word, pendingSpaces);
106 if (*current ==
' ') {
107 printWord(word, pendingSpaces);
113 if (*current ==
'\t') {
114 printWord(word, pendingSpaces);
120 word.push_back(*current);
124 printWord(word, pendingSpaces);
125 flushPendingSpaces(pendingSpaces);
126 return printedLength;
131 const size_t printedLength = print(text);
133 return printedLength;
143 IFont<RGB_T>* font_ =
nullptr;
144 RGB_T backgroundColor_ = RGB_T();
145#if __cplusplus
>= 201703L
147 RGB_T foregroundColor_ = []{
148 if constexpr (std::is_same<RGB_T,
bool>::value)
return true;
149 else return RGB_T(255, 255, 255);
154 ISurface<RGB_T>* target_ =
nullptr;
155 size_t topBorder_ = 20;
156 size_t leftBorder_ = 20;
157 size_t rightBorder_ = 20;
158 size_t buttomBorder_ = 20;
159 size_t currentLine_ = 0;
160 size_t currentY_ = 0;
161 size_t currentX_ = 0;
165 void ensureCursorInitialized() {
166 if (currentX_ == 0 && currentY_ == 0) {
167 currentX_ = leftBorder_;
168 currentY_ = topBorder_;
172 size_t drawableRightEdge()
const {
173 if (target_ ==
nullptr) {
176 const size_t targetWidth = target_->width();
177 return targetWidth > rightBorder_ ? targetWidth - rightBorder_ : 0;
180 bool isAtLineStart()
const {
return currentX_ <= leftBorder_; }
182 size_t lineAdvance()
const {
183 return font_->measureTextHeight(
"A", scale, 0) + spacing;
187 currentX_ = leftBorder_;
188 currentY_ += lineAdvance();
192 void printChunk(
const std::string& chunk) {
197 target_->drawText(
static_cast<int16_t>(currentX_),
198 static_cast<int16_t>(currentY_), chunk.c_str(),
199 foregroundColor_, backgroundColor_,
false, scale,
201 currentX_ += font_->measureTextWidth(chunk.c_str(), scale, spacing);
204 size_t pendingSpaceWidth(size_t pendingSpaces)
const {
205 if (pendingSpaces == 0) {
209 const std::string spaces(pendingSpaces,
' ');
210 return font_->measureTextWidth(spaces.c_str(), scale, spacing);
213 void flushPendingSpaces(size_t& pendingSpaces) {
214 if (pendingSpaces == 0 || isAtLineStart()) {
219 const size_t spacesWidth = pendingSpaceWidth(pendingSpaces);
220 if ((currentX_ + spacesWidth) > drawableRightEdge()) {
226 printChunk(std::string(pendingSpaces,
' '));
230 void printWord(std::string& word, size_t& pendingSpaces) {
235 const std::string leadingSpaces =
236 isAtLineStart() ? std::string() : std::string(pendingSpaces,
' ');
237 const std::string chunk = leadingSpaces + word;
238 const size_t chunkWidth =
239 font_->measureTextWidth(chunk.c_str(), scale, spacing);
241 if (!isAtLineStart() && (currentX_ + chunkWidth) > drawableRightEdge()) {
245 if (!isAtLineStart() && pendingSpaces > 0) {
246 printChunk(leadingSpaces);
Font rendering interface for TinyGPU-compatible framebuffers.
Definition: IFont.h:19
Helper for printing wrapped lines of text onto a TinyGPU target.
Definition: LinePrinter.h:17
void setBackgroundColor(RGB_T color)
Definition: LinePrinter.h:27
size_t println()
Advances to the next line.
Definition: LinePrinter.h:137
void setBorders(size_t border)
Sets all borders to the same value.
Definition: LinePrinter.h:43
void setScale(uint8_t scale)
Sets the text scale factor.
Definition: LinePrinter.h:66
void setTopBorder(size_t border)
Sets the top border in pixels.
Definition: LinePrinter.h:35
void setLeftBorder(size_t border)
Sets the left border in pixels.
Definition: LinePrinter.h:37
void setProportional(bool proportional)
Definition: LinePrinter.h:75
size_t print(const char *text)
Prints text with word wrapping.
Definition: LinePrinter.h:86
void setFont(IFont< RGB_T > &font)
Sets the Font implementation.
Definition: LinePrinter.h:30
void setSpacing(uint8_t spacing)
Sets the spacing between glyphs.
Definition: LinePrinter.h:69
void setTarget(ISurface< RGB_T > &target)
Sets the TinyGPU target.
Definition: LinePrinter.h:32
size_t cursorY() const
Y pixel coordinate the next character would be printed at.
Definition: LinePrinter.h:63
size_t cursorX() const
Definition: LinePrinter.h:61
void setRightBorder(size_t border)
Sets the right border in pixels.
Definition: LinePrinter.h:39
void setActualLine(size_t line)
Sets the current line index.
Definition: LinePrinter.h:51
void setColor(RGB_T color)
Definition: LinePrinter.h:26
LinePrinter(IFont< RGB_T > &font, ISurface< RGB_T > &target)
Creates a line printer for the given font and target.
Definition: LinePrinter.h:21
size_t println(const char *text)
Prints text and advances to the next line.
Definition: LinePrinter.h:130
void setButtomBorder(size_t border)
Sets the bottom border in pixels.
Definition: LinePrinter.h:41
bool isProportional() const
Returns whether proportional spacing is enabled on the current font.
Definition: LinePrinter.h:81
RGB color stored in 16-bit RGB565 format, byte-swapped from the conventional bit layout.
Definition: RGB565.h:24
Definition: DSIBusESP32.h:19