From 099bd245561c1acba5fd808376b6fa2787d6e103 Mon Sep 17 00:00:00 2001 From: Nebuleon Fumika Date: Wed, 18 Jun 2014 20:07:09 +0000 Subject: [PATCH] Use the font's suggested line height instead of its ascent and descent. In well-described fonts, this enables multi-line text (e.g. in manuals) to be more readable. The term "height" is also replaced with "line spacing" in Font's code. --- src/browsedialog.cpp | 2 +- src/contextmenu.cpp | 6 +++--- src/font.cpp | 10 +++++----- src/font.h | 6 +++--- src/iconbutton.cpp | 2 +- src/inputdialog.cpp | 2 +- src/link.cpp | 2 +- src/menu.cpp | 2 +- src/messagebox.cpp | 10 +++++----- src/selector.cpp | 2 +- src/settingsdialog.cpp | 2 +- src/textdialog.cpp | 4 ++-- src/textmanualdialog.cpp | 2 +- src/wallpaperdialog.cpp | 2 +- 14 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/browsedialog.cpp b/src/browsedialog.cpp index c96b39a..bd3207e 100644 --- a/src/browsedialog.cpp +++ b/src/browsedialog.cpp @@ -59,7 +59,7 @@ bool BrowseDialog::exec() fl->browse(); const int topBarHeight = gmenu2x->skinConfInt["topBarHeight"]; - rowHeight = gmenu2x->font->getHeight() + 1; // gp2x=15+1 / pandora=19+1 + rowHeight = gmenu2x->font->getLineSpacing() + 1; // gp2x=15+1 / pandora=19+1 rowHeight = constrain(rowHeight, 20, 40); numRows = (gmenu2x->resY - topBarHeight - 20) / rowHeight; clipRect = (SDL_Rect) { diff --git a/src/contextmenu.cpp b/src/contextmenu.cpp index 728b04e..fc69f8b 100644 --- a/src/contextmenu.cpp +++ b/src/contextmenu.cpp @@ -81,7 +81,7 @@ ContextMenu::ContextMenu(GMenu2X &gmenu2x, Menu &menu) w = std::max(w, font->getTextWidth(option->text)); } w += 23; - const int h = (font->getHeight() + 2) * options.size() + 8; + const int h = (font->getLineSpacing() + 2) * options.size() + 8; box = { static_cast((gmenu2x.resX - w) / 2), static_cast((gmenu2x.resY - h) / 2), @@ -116,7 +116,7 @@ void ContextMenu::paint(Surface &s) gmenu2x.skinConfColors[COLOR_MESSAGE_BOX_BORDER]); // Draw selection background. - const int h = font->getHeight(); + const int h = font->getLineSpacing(); SDL_Rect selbox = { static_cast(box.x + 4), static_cast(box.y + 4 + (h + 2) * selected), @@ -158,7 +158,7 @@ bool ContextMenu::handleButtonPress(InputManager::Button button) { bool ContextMenu::handleTouchscreen(Touchscreen &ts) { if (ts.inRect(box)) { int i = std::max(0, std::min(static_cast(options.size()) - 1, - (ts.getY() - (box.y + 4)) / (gmenu2x.font->getHeight() + 2))); + (ts.getY() - (box.y + 4)) / (gmenu2x.font->getLineSpacing() + 2))); if (ts.released()) { options[i]->action(); dismiss(); diff --git a/src/font.cpp b/src/font.cpp index 049a518..5710af7 100644 --- a/src/font.cpp +++ b/src/font.cpp @@ -22,7 +22,7 @@ Font *Font::defaultFont() Font::Font(const std::string &path, unsigned int size) { font = nullptr; - fontheight = 1; + lineSpacing = 1; /* Note: TTF_Init and TTF_Quit perform reference counting, so call them * both unconditionally for each font. */ @@ -38,7 +38,7 @@ Font::Font(const std::string &path, unsigned int size) return; } - fontheight = TTF_FontHeight(font); + lineSpacing = TTF_FontLineSkip(font); } Font::~Font() @@ -76,7 +76,7 @@ void Font::write(Surface *surface, const string &text, for (vector::const_iterator it = v.begin(); it != v.end(); it++) { writeLine(surface, it->c_str(), x, y, halign, valign); - y += fontheight; + y += lineSpacing; } } @@ -102,10 +102,10 @@ void Font::writeLine(Surface *surface, const char *text, case VAlignTop: break; case VAlignMiddle: - y -= fontheight / 2; + y -= lineSpacing / 2; break; case VAlignBottom: - y -= fontheight; + y -= lineSpacing; break; } diff --git a/src/font.h b/src/font.h index e7e8dd3..dd59941 100644 --- a/src/font.h +++ b/src/font.h @@ -29,9 +29,9 @@ public: return getTextWidth(text.c_str()); } - int getHeight() + int getLineSpacing() { - return fontheight; + return lineSpacing; } void write(Surface *surface, @@ -45,7 +45,7 @@ private: int x, int y, HAlign halign, VAlign valign); TTF_Font *font; - unsigned int fontheight; + int lineSpacing; }; #endif /* FONT_H */ diff --git a/src/iconbutton.cpp b/src/iconbutton.cpp index 643ef70..8254fef 100644 --- a/src/iconbutton.cpp +++ b/src/iconbutton.cpp @@ -47,7 +47,7 @@ void IconButton::recalcRects() { static_cast(iconRect.x + iconRect.w + margin), static_cast(rect.y + h / 2), static_cast(gmenu2x->font->getTextWidth(label)), - static_cast(gmenu2x->font->getHeight()) + static_cast(gmenu2x->font->getLineSpacing()) }; w += margin + labelRect.w; } diff --git a/src/inputdialog.cpp b/src/inputdialog.cpp index bc0776e..99047ad 100644 --- a/src/inputdialog.cpp +++ b/src/inputdialog.cpp @@ -145,7 +145,7 @@ void InputDialog::setKeyboard(int kb) { bool InputDialog::exec() { SDL_Rect box = { - 0, 60, 0, static_cast(gmenu2x->font->getHeight() + 4) + 0, 60, 0, static_cast(gmenu2x->font->getLineSpacing() + 4) }; Uint32 caretTick = 0, curTick; diff --git a/src/link.cpp b/src/link.cpp index eba4710..75aef50 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -158,7 +158,7 @@ void Link::setPosition(int x, int y) { void Link::recalcCoordinates() { iconX = rect.x+(rect.w-32)/2; - padding = (gmenu2x->skinConfInt["linkHeight"] - 32 - gmenu2x->font->getHeight()) / 3; + padding = (gmenu2x->skinConfInt["linkHeight"] - 32 - gmenu2x->font->getLineSpacing()) / 3; } void Link::run() { diff --git a/src/menu.cpp b/src/menu.cpp index 87c5651..df5d368 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -205,7 +205,7 @@ void Menu::paint(Surface &s) { // Paint section headers. s.box(width / 2 - linkWidth / 2, 0, linkWidth, topBarHeight, selectionBgColor); - const uint sectionLinkPadding = (topBarHeight - 32 - font.getHeight()) / 3; + const uint sectionLinkPadding = (topBarHeight - 32 - font.getLineSpacing()) / 3; const uint numSections = sections.size(); for (int i = leftSection; i <= rightSection; i++) { uint j = (centerSection + numSections + i) % numSections; diff --git a/src/messagebox.cpp b/src/messagebox.cpp index 82e169f..d714cbe 100644 --- a/src/messagebox.cpp +++ b/src/messagebox.cpp @@ -36,7 +36,7 @@ MessageBox::MessageBox(GMenu2X *gmenu2x, const string &text, const string &icon) for (uint i = 0; i < BUTTON_TYPE_SIZE; i++) { buttons[i] = ""; buttonLabels[i] = ""; - buttonPositions[i].h = gmenu2x->font->getHeight(); + buttonPositions[i].h = gmenu2x->font->getLineSpacing(); } //Default enabled button @@ -65,7 +65,7 @@ int MessageBox::exec() { bg.box(0, 0, gmenu2x->resX, gmenu2x->resY, 0,0,0,200); SDL_Rect box; - box.h = gmenu2x->font->getHeight()*3 +4; + box.h = gmenu2x->font->getLineSpacing()*3 +4; box.w = gmenu2x->font->getTextWidth(text) + 24 + (gmenu2x->sc[icon] != NULL ? 37 : 0); box.x = gmenu2x->halfX - box.w/2 -2; box.y = gmenu2x->halfY - box.h/2 -2; @@ -73,12 +73,12 @@ int MessageBox::exec() { //outer box bg.box(box, gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BG]); //draw inner rectangle - bg.rectangle(box.x+2, box.y+2, box.w-4, box.h-gmenu2x->font->getHeight(), + bg.rectangle(box.x+2, box.y+2, box.w-4, box.h-gmenu2x->font->getLineSpacing(), gmenu2x->skinConfColors[COLOR_MESSAGE_BOX_BORDER]); //icon+text if (gmenu2x->sc[icon] != NULL) - gmenu2x->sc[icon]->blitCenter( &bg, box.x+25, box.y+gmenu2x->font->getHeight()+3 ); - bg.write( gmenu2x->font, text, box.x+(gmenu2x->sc[icon] != NULL ? 47 : 10), box.y+gmenu2x->font->getHeight()+3, Font::HAlignLeft, Font::VAlignMiddle ); + gmenu2x->sc[icon]->blitCenter( &bg, box.x+25, box.y+gmenu2x->font->getLineSpacing()+3 ); + bg.write( gmenu2x->font, text, box.x+(gmenu2x->sc[icon] != NULL ? 47 : 10), box.y+gmenu2x->font->getLineSpacing()+3, Font::HAlignLeft, Font::VAlignMiddle ); int btnX = gmenu2x->halfX+box.w/2-6; for (uint i = 0; i < BUTTON_TYPE_SIZE; i++) { diff --git a/src/selector.cpp b/src/selector.cpp index 42011e3..e2cee95 100644 --- a/src/selector.cpp +++ b/src/selector.cpp @@ -79,7 +79,7 @@ int Selector::exec(int startSelection) { unsigned int top, height; tie(top, height) = gmenu2x->getContentArea(); - int fontheight = gmenu2x->font->getHeight(); + int fontheight = gmenu2x->font->getLineSpacing(); if (link->getSelectorBrowser()) fontheight = constrain(fontheight, 20, 40); unsigned int nb_elements = height / fontheight; diff --git a/src/settingsdialog.cpp b/src/settingsdialog.cpp index 479a41a..a7ffb32 100644 --- a/src/settingsdialog.cpp +++ b/src/settingsdialog.cpp @@ -70,7 +70,7 @@ bool SettingsDialog::exec() { static_cast(gmenu2x->resX - 12), static_cast(clipRect.h) }; - uint rowHeight = gmenu2x->font->getHeight() + 1; // gp2x=15+1 / pandora=19+1 + uint rowHeight = gmenu2x->font->getLineSpacing() + 1; // gp2x=15+1 / pandora=19+1 uint numRows = (gmenu2x->resY - topBarHeight - 20) / rowHeight; while (!close) { diff --git a/src/textdialog.cpp b/src/textdialog.cpp index 093131d..dc79799 100644 --- a/src/textdialog.cpp +++ b/src/textdialog.cpp @@ -80,7 +80,7 @@ void TextDialog::preProcess() { void TextDialog::drawText(vector *text, unsigned int y, unsigned int firstRow, unsigned int rowsPerPage) { - const int fontHeight = gmenu2x->font->getHeight(); + const int fontHeight = gmenu2x->font->getLineSpacing(); for (unsigned i = firstRow; i < firstRow + rowsPerPage && i < text->size(); i++) { const string &line = text->at(i); @@ -117,7 +117,7 @@ void TextDialog::exec() { bg.convertToDisplayFormat(); - const int fontHeight = gmenu2x->font->getHeight(); + const int fontHeight = gmenu2x->font->getLineSpacing(); unsigned int contentY, contentHeight; tie(contentY, contentHeight) = gmenu2x->getContentArea(); const unsigned rowsPerPage = contentHeight / fontHeight; diff --git a/src/textmanualdialog.cpp b/src/textmanualdialog.cpp index fc1d8c7..2921329 100644 --- a/src/textmanualdialog.cpp +++ b/src/textmanualdialog.cpp @@ -89,7 +89,7 @@ void TextManualDialog::exec() { bg.convertToDisplayFormat(); - uint firstRow = 0, rowsPerPage = 180/gmenu2x->font->getHeight(); + uint firstRow = 0, rowsPerPage = 180/gmenu2x->font->getLineSpacing(); stringstream ss; ss << pages.size(); string spagecount; diff --git a/src/wallpaperdialog.cpp b/src/wallpaperdialog.cpp index 8bb32f7..155daf1 100644 --- a/src/wallpaperdialog.cpp +++ b/src/wallpaperdialog.cpp @@ -84,7 +84,7 @@ bool WallpaperDialog::exec() unsigned int top, height; tie(top, height) = gmenu2x->getContentArea(); - int fontheight = gmenu2x->font->getHeight(); + int fontheight = gmenu2x->font->getLineSpacing(); unsigned int nb_elements = height / fontheight; while (!close) {