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());
58 void setScale(uint8_t scale) {
this->scale = scale; }
61 void setSpacing(uint8_t spacing) {
this->spacing = spacing; }
65 if (text ==
nullptr || *text ==
'\0') {
69 ensureCursorInitialized();
72 size_t pendingSpaces = 0;
73 size_t printedLength = 0;
75 for (
const char* current = text; *current !=
'\0'; ++current) {
76 if (*current ==
'\n') {
77 printWord(word, pendingSpaces);
84 if (*current ==
' ') {
85 printWord(word, pendingSpaces);
91 if (*current ==
'\t') {
92 printWord(word, pendingSpaces);
98 word.push_back(*current);
102 printWord(word, pendingSpaces);
103 flushPendingSpaces(pendingSpaces);
104 return printedLength;
109 const size_t printedLength = print(text);
111 return printedLength;
121 IFont<RGB_T>* font_ =
nullptr;
122 RGB_T backgroundColor_ = RGB_T();
123#if __cplusplus
>= 201703L
125 RGB_T foregroundColor_ = []{
126 if constexpr (std::is_same<RGB_T,
bool>::value)
return true;
127 else return RGB_T(255, 255, 255);
132 ISurface<RGB_T>* target_ =
nullptr;
133 size_t topBorder_ = 20;
134 size_t leftBorder_ = 20;
135 size_t rightBorder_ = 20;
136 size_t buttomBorder_ = 20;
137 size_t currentLine_ = 0;
138 size_t currentY_ = 0;
139 size_t currentX_ = 0;
143 void ensureCursorInitialized() {
144 if (currentX_ == 0 && currentY_ == 0) {
145 currentX_ = leftBorder_;
146 currentY_ = topBorder_;
150 size_t drawableRightEdge()
const {
151 if (target_ ==
nullptr) {
154 const size_t targetWidth = target_->width();
155 return targetWidth > rightBorder_ ? targetWidth - rightBorder_ : 0;
158 bool isAtLineStart()
const {
return currentX_ <= leftBorder_; }
160 size_t lineAdvance()
const {
161 return font_->measureTextHeight(
"A", scale, 0) + spacing;
165 currentX_ = leftBorder_;
166 currentY_ += lineAdvance();
170 void printChunk(
const std::string& chunk) {
175 target_->drawText(
static_cast<int16_t>(currentX_),
176 static_cast<int16_t>(currentY_), chunk.c_str(),
177 foregroundColor_, backgroundColor_,
false, scale,
179 currentX_ += font_->measureTextWidth(chunk.c_str(), scale, spacing);
182 size_t pendingSpaceWidth(size_t pendingSpaces)
const {
183 if (pendingSpaces == 0) {
187 const std::string spaces(pendingSpaces,
' ');
188 return font_->measureTextWidth(spaces.c_str(), scale, spacing);
191 void flushPendingSpaces(size_t& pendingSpaces) {
192 if (pendingSpaces == 0 || isAtLineStart()) {
197 const size_t spacesWidth = pendingSpaceWidth(pendingSpaces);
198 if ((currentX_ + spacesWidth) > drawableRightEdge()) {
204 printChunk(std::string(pendingSpaces,
' '));
208 void printWord(std::string& word, size_t& pendingSpaces) {
213 const std::string leadingSpaces =
214 isAtLineStart() ? std::string() : std::string(pendingSpaces,
' ');
215 const std::string chunk = leadingSpaces + word;
216 const size_t chunkWidth =
217 font_->measureTextWidth(chunk.c_str(), scale, spacing);
219 if (!isAtLineStart() && (currentX_ + chunkWidth) > drawableRightEdge()) {
223 if (!isAtLineStart() && pendingSpaces > 0) {
224 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:115
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:58
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
size_t print(const char *text)
Prints text with word wrapping.
Definition: LinePrinter.h:64
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:61
void setTarget(ISurface< RGB_T > &target)
Sets the TinyGPU target.
Definition: LinePrinter.h:32
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:108
void setButtomBorder(size_t border)
Sets the bottom border in pixels.
Definition: LinePrinter.h:41
RGB color stored in 16-bit RGB565 format.
Definition: RGB565.h:13
Definition: AVIWriter.h:9