1919
2020using namespace tcgfx ;
2121
22-
23- #if DISPLAY_HAS_MEMBUFFER == true
24- #define refreshDisplayIfNeeded (gr, needUpd ) {if (needUpd) reinterpret_cast <Adafruit_ILI9341*>(gr)->display ();}
25- #else
26- #define refreshDisplayIfNeeded (g, n )
27- #endif
28-
2922#ifndef COOKIE_CUT_MEMBUFFER_SIZE
3023#ifdef __AVR__
3124#define COOKIE_CUT_MEMBUFFER_SIZE 16
@@ -37,9 +30,10 @@ using namespace tcgfx;
3730uint16_t memBuffer[COOKIE_CUT_MEMBUFFER_SIZE];
3831
3932void AdafruitDrawable::transaction (bool isStarting, bool redrawNeeded) {
40- if (!isStarting) refreshDisplayIfNeeded (graphics, redrawNeeded);
33+
4134}
4235
36+
4337void AdafruitDrawable::internalDrawText (const Coord &where, const void *font, int mag, const char *sz) {
4438 graphics->setTextWrap (false );
4539 int baseline=0 ;
@@ -50,6 +44,59 @@ void AdafruitDrawable::internalDrawText(const Coord &where, const void *font, in
5044 graphics->print (sz);
5145}
5246
47+ Coord AdafruitDrawable::internalTextExtents (const void *f, int mag, const char *text, int *baseline) {
48+ if (mag == 0 ) mag = 1 ; // never allow 0 magnification
49+
50+ graphics->setFont (static_cast <const GFXfont *>(f));
51+ graphics->setTextSize (mag);
52+ auto * font = (GFXfont *) f;
53+ int16_t x1, y1;
54+ uint16_t w, h;
55+ graphics->getTextBounds ((char *)text, 3 , font?30 :2 , &x1, &y1, &w, &h);
56+
57+ if (font == nullptr ) {
58+ // for the default font, the starting offset is 0, and we calculate the height.
59+ if (baseline) *baseline = 0 ;
60+ return Coord (w, h);
61+ }
62+ else {
63+ computeBaselineIfNeeded (font);
64+ if (baseline) *baseline = (computedBaseline * mag);
65+ return Coord (int (w), (computedHeight * mag));
66+ }
67+ }
68+
69+ void AdafruitDrawable::computeBaselineIfNeeded (const GFXfont* font) {
70+ // we cache the last baseline, if the font is unchanged, don't calculate again
71+ if (computedFont == font && computedBaseline > 0 ) return ;
72+
73+ // we need to work out the biggest glyph and maximum extent beyond the baseline, we use 4 chars 'Agj(' for this
74+ const char sz[] = " Agj(" ;
75+ int height = 0 ;
76+ int bl = 0 ;
77+ const char * current = sz;
78+ auto fontLast = pgm_read_word (&font->last );
79+ auto fontFirst = pgm_read_word (&font->first );
80+ while (*current && (*current < fontLast)) {
81+ size_t glIdx = *current - fontFirst;
82+ auto allGlyphs = (GFXglyph*)pgm_read_ptr (&font->glyph );
83+ int glyphHeight = int (pgm_read_byte (&allGlyphs[glIdx].height ));
84+ if (glyphHeight > height) height = glyphHeight;
85+ auto yOffset = int8_t (pgm_read_byte (&allGlyphs[glIdx].yOffset ));
86+ bl += glyphHeight + yOffset;
87+ current++;
88+ }
89+ computedFont = font;
90+ computedBaseline = bl / 4 ;
91+ computedHeight = height;
92+ }
93+
94+ UnicodeFontHandler *AdafruitDrawable::createFontHandler () {
95+ return new UnicodeFontHandler (newAdafruitTextPipeline (graphics), ENCMODE_UTF8);
96+ }
97+
98+
99+
53100void AdafruitDrawable::drawBitmap (const Coord &where, const DrawableIcon *icon, bool selected) {
54101 if (icon->getIconType () == DrawableIcon::ICON_XBITMAP) {
55102 graphics->fillRect (where.x , where.y , icon->getDimensions ().x , icon->getDimensions ().y , backgroundColor);
@@ -142,61 +189,10 @@ void AdafruitDrawable::drawPolygon(const Coord points[], int numPoints, bool fil
142189 }
143190}
144191
145- void AdafruitDrawable::computeBaselineIfNeeded (const GFXfont* font) {
146- // we cache the last baseline, if the font is unchanged, don't calculate again
147- if (computedFont == font && computedBaseline > 0 ) return ;
148-
149- // we need to work out the biggest glyph and maximum extent beyond the baseline, we use 4 chars 'Agj(' for this
150- const char sz[] = " Agj(" ;
151- int height = 0 ;
152- int bl = 0 ;
153- const char * current = sz;
154- auto fontLast = pgm_read_word (&font->last );
155- auto fontFirst = pgm_read_word (&font->first );
156- while (*current && (*current < fontLast)) {
157- size_t glIdx = *current - fontFirst;
158- auto allGlyphs = (GFXglyph*)pgm_read_ptr (&font->glyph );
159- int glyphHeight = int (pgm_read_byte (&allGlyphs[glIdx].height ));
160- if (glyphHeight > height) height = glyphHeight;
161- auto yOffset = int8_t (pgm_read_byte (&allGlyphs[glIdx].yOffset ));
162- bl += glyphHeight + yOffset;
163- current++;
164- }
165- computedFont = font;
166- computedBaseline = bl / 4 ;
167- computedHeight = height;
168- }
169-
170- Coord AdafruitDrawable::internalTextExtents (const void *f, int mag, const char *text, int *baseline) {
171- if (mag == 0 ) mag = 1 ; // never allow 0 magnification
172-
173- graphics->setFont (static_cast <const GFXfont *>(f));
174- graphics->setTextSize (mag);
175- auto * font = (GFXfont *) f;
176- int16_t x1, y1;
177- uint16_t w, h;
178- graphics->getTextBounds ((char *)text, 3 , font?30 :2 , &x1, &y1, &w, &h);
179-
180- if (font == nullptr ) {
181- // for the default font, the starting offset is 0, and we calculate the height.
182- if (baseline) *baseline = 0 ;
183- return Coord (w, h);
184- }
185- else {
186- computeBaselineIfNeeded (font);
187- if (baseline) *baseline = (computedBaseline * mag);
188- return Coord (int (w), (computedHeight * mag));
189- }
190- }
191-
192192void AdafruitDrawable::drawPixel (uint16_t x, uint16_t y) {
193193 graphics->writePixel (x, y, drawColor);
194194}
195195
196- UnicodeFontHandler *AdafruitDrawable::createFontHandler () {
197- return new UnicodeFontHandler (newAdafruitTextPipeline (graphics), ENCMODE_UTF8);
198- }
199-
200196//
201197// helper functions
202198//
0 commit comments