Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 54451b8

Browse files
author
Kirk
committedMar 25, 2022
added Font names, method to get font names. Methods to get a string size in pixels - so user does not deal with font objects. updated examples
1 parent 0b9a1c9 commit 54451b8

18 files changed

+190
-36
lines changed
 

‎docs/api_draw.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,44 @@ QwiicFont * getFont(void)
5858
| :--- | :--- | :--- |
5959
| return value | `QwiicFont*` | A pointer to the current font. See `setFont()` for font object details.|
6060
61+
### getFontName()
62+
63+
This method returns the height in pixels of a provided String based on the current device font.
64+
65+
```c++
66+
String getFontName(void)
67+
```
68+
69+
| Parameter | Type | Description |
70+
| :--- | :--- | :--- |
71+
| return value | String | The name of the current font.|
72+
73+
### getStringWidth()
74+
75+
This method returns the width in pixels of a provided String based on the current device font.
76+
77+
```c++
78+
unsigned int getStringWidth(String text)
79+
```
80+
81+
| Parameter | Type | Description |
82+
| :--- | :--- | :--- |
83+
| text | `String` | The string used to determine width |
84+
| return value | `unsigned int` | The width of the provide string, as determined using the current font.|
85+
86+
### getStringHeight()
87+
88+
This method returns the height in pixels of a provided String based on the current device font.
89+
90+
```c++
91+
unsigned int getStringHeight(String text)
92+
```
93+
94+
| Parameter | Type | Description |
95+
| :--- | :--- | :--- |
96+
| text | `String` | The string used to determine height |
97+
| return value | `unsigned int` | The height of the provide string, as determined using the current font.|
98+
6199
### setDrawMode()
62100
This method sets the current draw mode for the library. The draw mode determines how pixels are set on the screen during drawing operations.
63101

‎examples/Example-01_Hello/Example-01_Hello.ino

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222

2323
#include <SparkFun_Qwiic_OLED.h> //http://librarymanager/All#SparkFun_Qwiic_Graphic_OLED
2424

25-
// Add in our font descriptor -- so we can center the text on the scren
26-
#include <res/qw_fnt_5x7.h>
27-
2825
// The Library supports three different types of SparkFun boards. The demo uses the following
2926
// defines to determine which device is being used. Uncomment the device being used for this demo.
3027

@@ -56,13 +53,14 @@ void setup()
5653

5754
String hello = "hello"; // our message
5855

59-
// Center our message on the screen. Use our Font Descriptor: QW_FONT_5X7, the default
60-
// font of the system.
56+
// Center our message on the screen. Get the screen size of the "hello" string,
57+
// calling the getStringWidth() and getStringHeight() methods on the oled
6158

62-
// starting x position - width minus string length (font width * number of characters) / 2
63-
int x0 = (myOLED.getWidth() - QW_FONT_5X7.width * hello.length()) / 2;
59+
// starting x position - screen width minus string width / 2
60+
int x0 = (myOLED.getWidth() - myOLED.getStringWidth(hello)) / 2;
6461

65-
int y0 = (myOLED.getHeight() - QW_FONT_5X7.height) / 2;
62+
// starting y position - screen height minus string height / 2
63+
int y0 = (myOLED.getHeight() - myOLED.getStringHeight(hello)) / 2;
6664

6765
// Draw the text - color of black (0)
6866
myOLED.text(x0, y0, hello, 0);

‎examples/Example-04_Text/Example-04_Text.ino

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ int nFONTS = sizeof(demoFonts) / sizeof(demoFonts[0]);
5252
int iFont = 0;
5353

5454
// Some vars for the title.
55-
int xTitle, yTitle;
5655
String strTitle = "<<Font>>";
5756
QwiicFont *pFntTitle = &QW_FONT_5X7;
5857

@@ -72,25 +71,19 @@ void setup()
7271
}
7372
Serial.println("Begin success");
7473

75-
// Position to use for the time/banner displayed before each font
76-
77-
// starting x position - width minus string length (font width * number of characters) / 2
78-
xTitle = (myOLED.getWidth() - (pFntTitle->width + 1) * strTitle.length()) / 2;
79-
80-
yTitle = (myOLED.getHeight() - pFntTitle->height) / 2;
8174
}
8275

8376
void loop()
8477
{
85-
// Write out a title
86-
writeTitle();
87-
88-
delay(1000);
89-
9078
// next font for display
9179
iFont = (iFont + 1) % nFONTS;
9280
myOLED.setFont(demoFonts[iFont]);
9381

82+
// Write font name to screen
83+
writeTitle();
84+
85+
delay(1000);
86+
9487
// Write out the full font char set
9588
writeFontChars();
9689

@@ -101,16 +94,16 @@ void loop()
10194
void writeFontChars()
10295
{
10396
// get the font
104-
QwiicFont *pFont = myOLED.getFont();
97+
QwiicFont * currFont = myOLED.getFont();
10598

10699
// how many chars can a screen handle? (x * y)
107-
uint16_t screenChars = myOLED.getWidth() / (pFont->width + 1); // X
108-
uint8_t nY = myOLED.getHeight() / pFont->height; // Y
100+
uint16_t screenChars = myOLED.getWidth() / (currFont->width + 1); // X
101+
uint8_t nY = myOLED.getHeight() / currFont->height; // Y
109102

110103
screenChars *= (nY == 0 ? 1 : nY); // need at least 1 row
111104

112105
// Loop over the characters in the font.
113-
for (int i = 0; i < pFont->n_chars; i++)
106+
for (int i = 0; i < currFont->n_chars; i++)
114107
{
115108

116109
if (i % screenChars == 0)
@@ -122,7 +115,7 @@ void writeFontChars()
122115

123116
// if the character is a carriage return, send a blank - otherwise the
124117
// write routine will perform a CR and lead to a confusing display.
125-
myOLED.write((i + pFont->start != '\n') ? i + pFont->start : ' ');
118+
myOLED.write((i + currFont->start != '\n') ? i + currFont->start : ' ');
126119

127120
myOLED.display(); // show the added char
128121

@@ -133,14 +126,35 @@ void writeFontChars()
133126
// Simple title for a font
134127
void writeTitle()
135128
{
129+
130+
// Get the current font name
131+
String strTitle = myOLED.getFontName();
132+
133+
// save our current font, then switch to font for title
134+
QwiicFont * currFont = myOLED.getFont();
135+
136136
// Set title font font
137137
myOLED.setFont(pFntTitle);
138138

139+
// Position to use for the time/banner displayed before each font
140+
141+
// Get the width of the title in screen size
142+
int width = myOLED.getStringWidth(strTitle);
143+
144+
// if the string is wider than the screen, set x at 0, otherwise center text
145+
146+
int xTitle = (width >= myOLED.getWidth() ? 0 : (myOLED.getWidth() - width) / 2);
147+
148+
// starting y position - width minus string height / 2. No need to chech height, 5x7 font fits everything
149+
int yTitle = (myOLED.getHeight() - myOLED.getStringHeight(strTitle)) / 2;
150+
139151
myOLED.erase();
140152

141153
// Draw the text
142154
myOLED.text(xTitle, yTitle, strTitle);
143155

144156
// There's nothing on the screen yet - Now send the graphics to the device
145157
myOLED.display();
158+
159+
myOLED.setFont(currFont);
146160
}

‎examples/Example-05_ScrollFlip/Example-05_ScrollFlip.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void setup()
129129
}
130130
Serial.println("Begin success");
131131

132-
yoffset = (myOLED.getHeight() - myOLED.getFont()->height) / 2;
132+
yoffset = (myOLED.getHeight() - myOLED.getStringHeight("a")) / 2;
133133

134134
delay(1000);
135135
}

‎src/SparkFun_Qwiic_OLED.h

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,66 @@ class QwiicOLEDBaseClass : public Print
402402
return _device.get_font();
403403
}
404404

405+
///////////////////////////////////////////////////////////////////////
406+
// getFontName()
407+
//
408+
// This method returns the name of the current font for the device.
409+
410+
String getFontName(void)
411+
{
412+
QwiicFont *pFont = _device.get_font();
413+
414+
if(!pFont)
415+
return String("");
416+
417+
return String(pFont->name);
418+
}
419+
///////////////////////////////////////////////////////////////////////
420+
// getStringWidth()
421+
//
422+
// Returns the width of the provide string using the current font.
423+
//
424+
// Parameter Description
425+
// --------- -----------------------------
426+
// text The string used to determine width
427+
// retval The width of the provide string, as determined using the current font.
428+
429+
unsigned int getStringWidth(String &text)
430+
{
431+
return getStringWidth(text.c_str());
432+
}
433+
unsigned int getStringWidth(const char *text)
434+
{
435+
436+
uint16_t height, width;
437+
438+
return (_device.get_string_size(text, width, height) ? width : 0);
439+
440+
}
441+
442+
///////////////////////////////////////////////////////////////////////
443+
// getStringHeight()
444+
//
445+
// Returns the height of the provide string using the current font.
446+
//
447+
// Parameter Description
448+
// --------- -----------------------------
449+
// text The string used to determine height
450+
// retval The height of the provide string, as determined using the current font.
451+
452+
unsigned int getStringHeight(String &text)
453+
{
454+
return getStringHeight(text.c_str());
455+
}
456+
457+
unsigned int getStringHeight(const char *text)
458+
{
459+
460+
uint16_t height, width;
461+
462+
return (_device.get_string_size(text, width, height) ? height : 0);
463+
464+
}
405465
///////////////////////////////////////////////////////////////////////
406466
// setDrawMode()
407467
//
@@ -700,7 +760,7 @@ class QwiicOLEDBaseClass : public Print
700760

701761
QwiicFont *pFont = _device.get_font();
702762

703-
if (!pFont) // no Font?! No dice?
763+
if (!pFont) // no Font?! No dice
704764
return 0;
705765

706766
switch (theChar)

‎src/qwiic_grbuffer.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ bool QwGrBufferDevice::init(void)
163163
return init_draw_functions();
164164
}
165165
////////////////////////////////////////////////////////////////////////////////////
166-
// Font things
166+
// Font related things
167167
////////////////////////////////////////////////////////////////////////////////////
168168
// init_font()
169169
//
@@ -214,6 +214,36 @@ void QwGrBufferDevice::set_font(const QwFont *font)
214214
init_font();
215215
}
216216

217+
////////////////////////////////////////////////////////////////////////////////////////
218+
// get_string_size()
219+
//
220+
// Return the size (width, height) in pixels of the given string using the current font
221+
222+
bool QwGrBufferDevice::get_string_size(const char *text, uint16_t &width, uint16_t &height)
223+
{
224+
225+
if(!_currFont) // no font?
226+
init_font();
227+
228+
// no font, no text - no dice
229+
if(!_currFont || !text)
230+
return false;
231+
232+
int sLen = strlen(text);
233+
234+
width = sLen * _currFont->width;
235+
236+
// The 5x7 font width is off by one - historical - was same in orig lib, which fonts
237+
// are used.
238+
if(_currFont == &QW_FONT_5X7)
239+
width += sLen;
240+
241+
height = (sLen > 0 ? _currFont->height : 0 );
242+
243+
return true;
244+
245+
}
246+
217247
////////////////////////////////////////////////////////////////////////////////////////
218248
// Public Graphics Methods
219249
////////////////////////////////////////////////////////////////////////////////////////

‎src/qwiic_grbuffer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,15 @@ class QwGrBufferDevice : protected _QwIDraw
175175
// Lifecycle
176176
virtual bool init(void);
177177

178-
// current font methods
178+
// Font methods
179179
void init_font(void);
180180
void set_font(QwFont &font);
181181
void set_font(const QwFont *font);
182182
QwFont *get_font(void);
183183

184+
// Returns the size of a string - in pixels - using current font
185+
bool get_string_size(const char *text, uint16_t &width, uint16_t &height);
186+
184187
// Public Interface - Graphics interface
185188
void line(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, uint8_t clr = 1);
186189

‎src/res/_fnt_31x48.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ August 13, 2015
2626
#define FONT_31X48_START 65
2727
#define FONT_31X48_NCHAR 58
2828
#define FONT_31X48_MAP_WIDTH 62
29+
#define FONT_31X48_NAME "31 x 48"
2930

3031
#if defined(ARDUINO_ARCH_MBED)
3132
// ARDUINO_ARCH_MBED (APOLLO3 v2) does not support or require pgmspace.h / PROGMEM

‎src/res/_fnt_5x7.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ July 27, 2015
3030
#define FONT_5X7_START 0
3131
#define FONT_5X7_NCHAR 255
3232
#define FONT_5X7_MAP_WIDTH 1275
33+
#define FONT_5X7_NAME "5 x 7"
3334

3435
// Standard ASCII 5x7 font
3536
#if defined(ARDUINO_ARCH_MBED)

‎src/res/_fnt_7segment.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ July 27, 2015
3131
#define FONT_7SEG_START 46
3232
#define FONT_7SEG_NCHAR 13
3333
#define FONT_7SEG_MAP_WIDTH 130
34+
#define FONT_7SEG_NAME "7 Segment"
3435

3536
#if defined(ARDUINO_ARCH_MBED)
3637
// ARDUINO_ARCH_MBED (APOLLO3 v2) does not support or require pgmspace.h / PROGMEM

‎src/res/_fnt_8x16.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ July 27, 2015
3232
#define FONT_8X16_START 32
3333
#define FONT_8X16_NCHAR 96
3434
#define FONT_8X16_MAP_WIDTH 256
35+
#define FONT_8X16_NAME "8 x 16"
3536

3637
#if defined(ARDUINO_ARCH_MBED)
3738
// ARDUINO_ARCH_MBED (APOLLO3 v2) does not support or require pgmspace.h / PROGMEM

‎src/res/_fnt_largenum.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ July 27, 2015
3131
#define FONT_LARGENUM_START 48
3232
#define FONT_LARGENUM_NCHAR 11
3333
#define FONT_LARGENUM_MAP_WIDTH 132
34+
#define FONT_LARGENUM_NAME "Large Number"
3435

3536
#if defined(ARDUINO_ARCH_MBED)
3637
// ARDUINO_ARCH_MBED (APOLLO3 v2) does not support or require pgmspace.h / PROGMEM

‎src/res/qw_fnt_31x48.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class QwFont31x48 final : public fontSingleton<QwFont31x48> {
6565
FONT_31X48_HEIGHT,
6666
FONT_31X48_START,
6767
FONT_31X48_NCHAR,
68-
FONT_31X48_MAP_WIDTH){}
68+
FONT_31X48_MAP_WIDTH,
69+
FONT_31X48_NAME){}
6970

7071
};
7172

‎src/res/qw_fnt_5x7.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class QwFont5x7 final : public fontSingleton<QwFont5x7> {
6565
FONT_5X7_HEIGHT,
6666
FONT_5X7_START,
6767
FONT_5X7_NCHAR,
68-
FONT_5X7_MAP_WIDTH){}
68+
FONT_5X7_MAP_WIDTH,
69+
FONT_5X7_NAME){}
6970

7071
};
7172

‎src/res/qw_fnt_7segment.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class QwFont7Segment final : public fontSingleton<QwFont7Segment> {
6565
FONT_7SEG_HEIGHT,
6666
FONT_7SEG_START,
6767
FONT_7SEG_NCHAR,
68-
FONT_7SEG_MAP_WIDTH){}
68+
FONT_7SEG_MAP_WIDTH,
69+
FONT_7SEG_NAME){}
6970

7071
};
7172

‎src/res/qw_fnt_8x16.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class QwFont8x16 final : public fontSingleton<QwFont8x16> {
6565
FONT_8X16_HEIGHT,
6666
FONT_8X16_START,
6767
FONT_8X16_NCHAR,
68-
FONT_8X16_MAP_WIDTH){}
68+
FONT_8X16_MAP_WIDTH,
69+
FONT_8X16_NAME){}
6970

7071
};
7172

‎src/res/qw_fnt_largenum.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class QwFontLargeNum final : public fontSingleton<QwFontLargeNum> {
6565
FONT_LARGENUM_HEIGHT,
6666
FONT_LARGENUM_START,
6767
FONT_LARGENUM_NCHAR,
68-
FONT_LARGENUM_MAP_WIDTH){}
68+
FONT_LARGENUM_MAP_WIDTH,
69+
FONT_LARGENUM_NAME){}
6970

7071
};
7172

‎src/res/qwiic_resdef.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,13 @@ class QwFont{
168168
uint8_t start;
169169
uint8_t n_chars;
170170
uint16_t map_width;
171+
const char * name;
171172

172173
virtual const uint8_t * data(void){return nullptr;};
173174

174175
protected:
175-
QwFont(uint8_t w, uint8_t h, uint8_t st_chr, uint8_t n_chr, uint16_t m_w):
176-
width{w}, height{h}, start{st_chr}, n_chars{n_chr}, map_width{m_w}{}
176+
QwFont(uint8_t w, uint8_t h, uint8_t st_chr, uint8_t n_chr, uint16_t m_w, const char *f_name):
177+
width{w}, height{h}, start{st_chr}, n_chars{n_chr}, map_width{m_w}, name{f_name}{}
177178
};
178179

179180

0 commit comments

Comments
 (0)
Please sign in to comment.