Skip to content

Commit

Permalink
[update] Support vertical alignment (#15, #18)
Browse files Browse the repository at this point in the history
- Support vertical alignment (Top, Middle, Bottom)
  • Loading branch information
takkaO committed Feb 2, 2023
1 parent 1633fb2 commit 4105a32
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
30 changes: 29 additions & 1 deletion src/OpenFontRender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ FT_Error OpenFontRender::loadFont(const unsigned char *data, size_t size, uint8_
FT_Error OpenFontRender::loadFont(const char *fpath, uint8_t target_face_index) {
size_t len = strlen(fpath);

_face_id.filepath = new char[len + 1]; // Release on unloadFont method
_face_id.filepath = new char[len + 1]; // Release on unloadFont method
strncpy(_face_id.filepath, fpath, len);
_face_id.filepath[len] = '\0';

Expand Down Expand Up @@ -382,13 +382,41 @@ uint16_t OpenFontRender::drawHString(const char *str,
// Calculate alignment offset
switch (align) {
case Align::Left:
// Fallthrough
case Align::TopLeft:
// Nothing to do
break;
case Align::MiddleLeft:
offset.y += ((bbox.yMax - bbox.yMin) / 2);
break;
case Align::BottomLeft:
offset.y += (bbox.yMax - bbox.yMin);
break;
case Align::Center:
// Fallthrough
case Align::TopCenter:
offset.x += ((bbox.xMax - bbox.xMin) / 2);
break;
case Align::MiddleCenter:
offset.x += ((bbox.xMax - bbox.xMin) / 2);
offset.y += ((bbox.yMax - bbox.yMin) / 2);
break;
case Align::BottomCenter:
offset.x += ((bbox.xMax - bbox.xMin) / 2);
offset.y += (bbox.yMax - bbox.yMin);
break;
case Align::Right:
// Fallthrough
case Align::TopRight:
offset.x += (bbox.xMax - bbox.xMin);
break;
case Align::MiddleRight:
offset.x += (bbox.xMax - bbox.xMin);
offset.y += ((bbox.yMax - bbox.yMin) / 2);
break;
case Align::BottomRight:
offset.x += (bbox.xMax - bbox.xMin);
offset.y += (bbox.yMax - bbox.yMin);
break;
default:
break;
Expand Down
15 changes: 12 additions & 3 deletions src/OpenFontRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,18 @@ enum OFR_DEBUG_LEVEL {
};

enum class Align {
Left,
Center,
Right
Left, // = TopLeft
Center, // = TopCenter
Right, // = TopRight
TopLeft,
TopCenter,
TopRight,
MiddleLeft,
MiddleCenter,
MiddleRight,
BottomLeft,
BottomCenter,
BottomRight,
};

enum class Layout {
Expand Down

0 comments on commit 4105a32

Please sign in to comment.