Skip to content

Commit db79f79

Browse files
committed
Implement PBMFont::getCenteredXPos.
When PBMXpos is set to -1, LCDPlugin::writeStringOnFrame() will write string horizontaly centered on LCD screen.
1 parent 692e773 commit db79f79

File tree

7 files changed

+69
-23
lines changed

7 files changed

+69
-23
lines changed

src/bin/daemon/LCDPlugins/LCDPlugin.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,24 @@ void LCDPlugin::writeStringOnFrame(
194194
FontsManager* const pFonts,
195195
const FontID fontID,
196196
const std::string & string,
197-
uint16_t PBMXPos,
197+
const int16_t PBMXPos,
198198
const uint16_t PBMYPos)
199199
{
200200
try {
201201
#if DEBUGGING_ON && DEBUG_LCD_PLUGINS
202202
LOG(DEBUG2) << this->getPluginName() << " PBM # " << _frameIndex << " - writing string : " << string;
203203
#endif
204+
uint16_t XPos = 0;
205+
if( PBMXPos < 0 ) { /* centered */
206+
XPos = pFonts->getCenteredXPos(fontID, string);
207+
}
208+
else {
209+
XPos = static_cast<uint16_t>(PBMXPos);
210+
}
211+
204212
for(const char & c : string) {
205213
const std::string character(1, c);
206-
pFonts->printCharacterOnFrame( fontID, (*_itCurrentFrame)._PBMData, character, PBMXPos, PBMYPos );
214+
pFonts->printCharacterOnFrame( fontID, (*_itCurrentFrame)._PBMData, character, XPos, PBMYPos );
207215
} /* for each character in the string */
208216
}
209217
catch (const GLogiKExcept & e) {
@@ -215,7 +223,7 @@ void LCDPlugin::writeStringOnLastFrame(
215223
FontsManager* const pFonts,
216224
const FontID fontID,
217225
const std::string & string,
218-
uint16_t PBMXPos,
226+
const int16_t PBMXPos,
219227
const uint16_t PBMYPos)
220228
{
221229
try {

src/bin/daemon/LCDPlugins/LCDPlugin.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ class LCDPlugin
121121
FontsManager* const pFonts,
122122
const FontID fontID,
123123
const std::string & string,
124-
uint16_t PBMXPos,
124+
const int16_t PBMXPos,
125125
const uint16_t PBMYPos
126126
);
127127

128128
void writeStringOnLastFrame(
129129
FontsManager* const pFonts,
130130
const FontID fontID,
131131
const std::string & string,
132-
uint16_t PBMXPos,
132+
const int16_t PBMXPos,
133133
const uint16_t PBMYPos
134134
);
135135

src/bin/daemon/LCDPlugins/PBMFont.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const charactersMap_type PBMFont::defaultCharsMap =
5959
{";", {15,2} }, {"/", {16,2} }, {"-", {17,2} }, {"%", {18,2} },
6060
};
6161

62+
const std::string PBMFont::hackstring("mGMW%");
63+
6264
PBMFont::PBMFont(
6365
const std::string & PBMName,
6466
const uint16_t PBMWidth,
@@ -108,6 +110,21 @@ PBMFont::~PBMFont()
108110
#endif
109111
}
110112

113+
const uint16_t PBMFont::getCenteredXPos(const std::string & string)
114+
{
115+
uint16_t XPos = PBM_WIDTH;
116+
for(const char & c : string) {
117+
XPos -= (_charWidth - _fontLeftShift);
118+
// XXX
119+
if(_extraLeftShift > 0) {
120+
if(hackstring.find(c) == std::string::npos) {
121+
XPos += _extraLeftShift;
122+
}
123+
}
124+
}
125+
return static_cast<uint16_t>(XPos/2);
126+
}
127+
111128
void PBMFont::printCharacterOnFrame(
112129
PBMDataArray & frame,
113130
const std::string & character,
@@ -181,7 +198,7 @@ void PBMFont::printCharacterOnFrame(
181198
// applying another leftshift, except for those characters
182199
// which are very wide compared to others
183200
if(_extraLeftShift > 0) {
184-
if(std::string("mGMW%").find(character) == std::string::npos) {
201+
if(hackstring.find(character) == std::string::npos) {
185202
PBMXPos -= _extraLeftShift;
186203
}
187204
}

src/bin/daemon/LCDPlugins/PBMFont.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class PBMFont
4444
public:
4545
virtual ~PBMFont(void);
4646

47+
const uint16_t getCenteredXPos(const std::string & string);
48+
4749
void printCharacterOnFrame(
4850
PBMDataArray & frame,
4951
const std::string & character,
@@ -80,6 +82,7 @@ class PBMFont
8082
std::map<const std::string, std::pair<uint16_t, uint16_t>> _charsMap;
8183

8284
static const charactersMap_type defaultCharsMap;
85+
static const std::string hackstring;
8386

8487
const unsigned char getCharacterLine(
8588
const uint16_t line,

src/bin/daemon/LCDPlugins/endscreen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void Endscreen::init(FontsManager* const pFonts, const std::string & product)
5656
PBMDirectory /= _plugin.getName();
5757

5858
this->addPBMClearedFrame(); /* frame #0 */
59-
this->writeStringOnLastFrame(pFonts, FontID::DEJAVUSANSBOLD1616, product, 32, 10);
59+
this->writeStringOnLastFrame(pFonts, FontID::DEJAVUSANSBOLD1616, product, -1, 10);
6060

6161
//this->addPBMFrame(PBMDirectory, "endscreen.pbm", 1); /* frame #0 */
6262

src/bin/daemon/LCDPlugins/fontsManager.cpp

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,35 @@ FontsManager::~FontsManager()
4242
_fonts.clear();
4343
}
4444

45+
const uint16_t FontsManager::getCenteredXPos(
46+
const FontID fontID,
47+
const std::string & string
48+
) {
49+
try {
50+
return _fonts.at(fontID)->getCenteredXPos(string);
51+
}
52+
catch (const std::out_of_range& oor) {
53+
this->initializeFont(fontID);
54+
return _fonts.at(fontID)->getCenteredXPos(string);
55+
}
56+
}
57+
58+
void FontsManager::printCharacterOnFrame(
59+
const FontID fontID,
60+
PBMDataArray & frame,
61+
const std::string & c,
62+
uint16_t & PBMXPos,
63+
const uint16_t PBMYPos)
64+
{
65+
try {
66+
_fonts.at(fontID)->printCharacterOnFrame(frame, c, PBMXPos, PBMYPos);
67+
}
68+
catch (const std::out_of_range& oor) {
69+
this->initializeFont(fontID);
70+
_fonts.at(fontID)->printCharacterOnFrame(frame, c, PBMXPos, PBMYPos);
71+
}
72+
}
73+
4574
void FontsManager::initializeFont(const FontID fontID)
4675
{
4776
#if DEBUGGING_ON
@@ -70,21 +99,5 @@ void FontsManager::initializeFont(const FontID fontID)
7099
_fonts[fontID] = font;
71100
}
72101

73-
void FontsManager::printCharacterOnFrame(
74-
const FontID fontID,
75-
PBMDataArray & frame,
76-
const std::string & c,
77-
uint16_t & PBMXPos,
78-
const uint16_t PBMYPos)
79-
{
80-
try {
81-
_fonts.at(fontID)->printCharacterOnFrame(frame, c, PBMXPos, PBMYPos);
82-
}
83-
catch (const std::out_of_range& oor) {
84-
this->initializeFont(fontID);
85-
_fonts.at(fontID)->printCharacterOnFrame(frame, c, PBMXPos, PBMYPos);
86-
}
87-
}
88-
89102
} // namespace GLogiK
90103

src/bin/daemon/LCDPlugins/fontsManager.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class FontsManager
3939
FontsManager(void);
4040
~FontsManager(void);
4141

42+
const uint16_t getCenteredXPos(
43+
const FontID fontID,
44+
const std::string & string
45+
);
46+
4247
void printCharacterOnFrame(
4348
const FontID fontID,
4449
PBMDataArray & frame,

0 commit comments

Comments
 (0)