Improve code documentation 'width' might imply something 'aligned horizontally'. The first parameter of maxTextLength() is given in pixels. To avoid confusion, the name of the parameter is changed. The comment before the binary search reflects better why and what we need to do.
Mathias Gumz akira@fluxbox.org
2 files changed,
24 insertions(+),
16 deletions(-)
M
src/FbTk/TextUtils.cc
→
src/FbTk/TextUtils.cc
@@ -28,26 +28,34 @@ #include <cstring>
namespace { -// calcs longest substring of 'text', fitting into 'max_width' +// calcs longest substring of 'text', fitting into 'n_pixels' // 'text_len' is an in-out parameter // 'text_width' is out parameter -void maxTextLength(int max_width, const FbTk::Font& font, const char* const text, +void maxTextLength(int n_pixels, const FbTk::Font& font, const char* const text, unsigned int& text_len, int& text_width) { text_width = font.textWidth(text, text_len); - // rendered text exceeds max_width. calculate 'len' to cut off 'text'. - if (text_width > max_width) { + // rendered text exceeds n_pixels. calculate 'len' to cut off 'text'. + if (text_width > n_pixels) { - // pick some good starting points for the search + // there is less room for thicker glyphs than for + // thin glyphs. 'text' contains usually a good mix of both. to decide + // upon where we cut off glyphs from 'text', we do a binary search + // over 'text' to find the optimal length that fits into 'n_pixels'. // + // by assuming a text that consists of only thick glyphs ("WW") and + // a text that consist of only thin glyphs (".") we find a good + // start to binary search: + // + // +---right // [...........|.R ] // [WWWWWWWWWWL| ] - // - // max_width + // +------left + // n_pixels - int right = max_width / (font.textWidth(".", 1) + 1); - int left = max_width / (font.textWidth("WW", 2) + 1); + int right = n_pixels / (font.textWidth(".", 1) + 1); + int left = n_pixels / (font.textWidth("WW", 2) + 1); int middle; // binary search for longest substring fitting into 'max_width' pixels@@ -56,7 +64,7 @@
middle = left + ((right - left) / 2); text_width = font.textWidth(text, middle); - if (text_width < max_width) { + if (text_width < n_pixels) { left = middle; } else { right = middle;@@ -71,7 +79,7 @@ }
namespace FbTk { -int doAlignment(int max_width, int bevel, FbTk::Justify justify, +int doAlignment(int n_pixels, int bevel, FbTk::Justify justify, const FbTk::Font &font, const char * const text, unsigned int textlen, unsigned int &newlen) {@@ -80,14 +88,14 @@ return 0;
int text_width; - maxTextLength(max_width - bevel, font, text, textlen, text_width); + maxTextLength(n_pixels - bevel, font, text, textlen, text_width); newlen = textlen; if (justify == FbTk::RIGHT) { - return max_width - text_width; + return n_pixels - text_width; } else if (justify == FbTk::CENTER) { - return (max_width - text_width + bevel)/2; + return (n_pixels - text_width + bevel)/2; } return bevel;
M
src/FbTk/TextUtils.hh
→
src/FbTk/TextUtils.hh
@@ -29,9 +29,9 @@
class Font; /** - Aligns the text after max width and bevel + Aligns the text after max_pixels and bevel */ -int doAlignment(int max_width, int bevel, FbTk::Justify justify, +int doAlignment(int max_pixels, int bevel, FbTk::Justify justify, const FbTk::Font &font, const char * const text, unsigned int textlen, unsigned int &newlen);