diff options
author | Mathias Gumz <akira@fluxbox.org> | 2015-01-24 09:35:21 (GMT) |
---|---|---|
committer | Mathias Gumz <akira@fluxbox.org> | 2015-01-24 09:35:21 (GMT) |
commit | e0c5436f44c262967c5934265ef68d07b4048b45 (patch) | |
tree | 84a711738f9391c0339712e1ad5989323289d4ae | |
parent | 00cca1284fb436afd43b0569d56302f7c78e3d8d (diff) | |
download | fluxbox-e0c5436f44c262967c5934265ef68d07b4048b45.zip fluxbox-e0c5436f44c262967c5934265ef68d07b4048b45.tar.bz2 |
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.
-rw-r--r-- | src/FbTk/TextUtils.cc | 36 | ||||
-rw-r--r-- | src/FbTk/TextUtils.hh | 4 |
2 files changed, 24 insertions, 16 deletions
diff --git a/src/FbTk/TextUtils.cc b/src/FbTk/TextUtils.cc index ca141d1..c0e1cfc 100644 --- a/src/FbTk/TextUtils.cc +++ b/src/FbTk/TextUtils.cc | |||
@@ -28,26 +28,34 @@ | |||
28 | 28 | ||
29 | namespace { | 29 | namespace { |
30 | 30 | ||
31 | // calcs longest substring of 'text', fitting into 'max_width' | 31 | // calcs longest substring of 'text', fitting into 'n_pixels' |
32 | // 'text_len' is an in-out parameter | 32 | // 'text_len' is an in-out parameter |
33 | // 'text_width' is out parameter | 33 | // 'text_width' is out parameter |
34 | void maxTextLength(int max_width, const FbTk::Font& font, const char* const text, | 34 | void maxTextLength(int n_pixels, const FbTk::Font& font, const char* const text, |
35 | unsigned int& text_len, int& text_width) { | 35 | unsigned int& text_len, int& text_width) { |
36 | 36 | ||
37 | text_width = font.textWidth(text, text_len); | 37 | text_width = font.textWidth(text, text_len); |
38 | 38 | ||
39 | // rendered text exceeds max_width. calculate 'len' to cut off 'text'. | 39 | // rendered text exceeds n_pixels. calculate 'len' to cut off 'text'. |
40 | if (text_width > max_width) { | 40 | if (text_width > n_pixels) { |
41 | 41 | ||
42 | // pick some good starting points for the search | 42 | // there is less room for thicker glyphs than for |
43 | // thin glyphs. 'text' contains usually a good mix of both. to decide | ||
44 | // upon where we cut off glyphs from 'text', we do a binary search | ||
45 | // over 'text' to find the optimal length that fits into 'n_pixels'. | ||
43 | // | 46 | // |
47 | // by assuming a text that consists of only thick glyphs ("WW") and | ||
48 | // a text that consist of only thin glyphs (".") we find a good | ||
49 | // start to binary search: | ||
50 | // | ||
51 | // +---right | ||
44 | // [...........|.R ] | 52 | // [...........|.R ] |
45 | // [WWWWWWWWWWL| ] | 53 | // [WWWWWWWWWWL| ] |
46 | // | 54 | // +------left |
47 | // max_width | 55 | // n_pixels |
48 | 56 | ||
49 | int right = max_width / (font.textWidth(".", 1) + 1); | 57 | int right = n_pixels / (font.textWidth(".", 1) + 1); |
50 | int left = max_width / (font.textWidth("WW", 2) + 1); | 58 | int left = n_pixels / (font.textWidth("WW", 2) + 1); |
51 | int middle; | 59 | int middle; |
52 | 60 | ||
53 | // binary search for longest substring fitting into 'max_width' pixels | 61 | // binary search for longest substring fitting into 'max_width' pixels |
@@ -56,7 +64,7 @@ void maxTextLength(int max_width, const FbTk::Font& font, const char* const text | |||
56 | middle = left + ((right - left) / 2); | 64 | middle = left + ((right - left) / 2); |
57 | text_width = font.textWidth(text, middle); | 65 | text_width = font.textWidth(text, middle); |
58 | 66 | ||
59 | if (text_width < max_width) { | 67 | if (text_width < n_pixels) { |
60 | left = middle; | 68 | left = middle; |
61 | } else { | 69 | } else { |
62 | right = middle; | 70 | right = middle; |
@@ -71,7 +79,7 @@ void maxTextLength(int max_width, const FbTk::Font& font, const char* const text | |||
71 | 79 | ||
72 | namespace FbTk { | 80 | namespace FbTk { |
73 | 81 | ||
74 | int doAlignment(int max_width, int bevel, FbTk::Justify justify, | 82 | int doAlignment(int n_pixels, int bevel, FbTk::Justify justify, |
75 | const FbTk::Font &font, const char * const text, | 83 | const FbTk::Font &font, const char * const text, |
76 | unsigned int textlen, unsigned int &newlen) { | 84 | unsigned int textlen, unsigned int &newlen) { |
77 | 85 | ||
@@ -80,14 +88,14 @@ int doAlignment(int max_width, int bevel, FbTk::Justify justify, | |||
80 | 88 | ||
81 | int text_width; | 89 | int text_width; |
82 | 90 | ||
83 | maxTextLength(max_width - bevel, font, text, textlen, text_width); | 91 | maxTextLength(n_pixels - bevel, font, text, textlen, text_width); |
84 | 92 | ||
85 | newlen = textlen; | 93 | newlen = textlen; |
86 | 94 | ||
87 | if (justify == FbTk::RIGHT) { | 95 | if (justify == FbTk::RIGHT) { |
88 | return max_width - text_width; | 96 | return n_pixels - text_width; |
89 | } else if (justify == FbTk::CENTER) { | 97 | } else if (justify == FbTk::CENTER) { |
90 | return (max_width - text_width + bevel)/2; | 98 | return (n_pixels - text_width + bevel)/2; |
91 | } | 99 | } |
92 | 100 | ||
93 | return bevel; | 101 | return bevel; |
diff --git a/src/FbTk/TextUtils.hh b/src/FbTk/TextUtils.hh index dcc222c..01fd8a9 100644 --- a/src/FbTk/TextUtils.hh +++ b/src/FbTk/TextUtils.hh | |||
@@ -29,9 +29,9 @@ namespace FbTk { | |||
29 | class Font; | 29 | class Font; |
30 | 30 | ||
31 | /** | 31 | /** |
32 | Aligns the text after max width and bevel | 32 | Aligns the text after max_pixels and bevel |
33 | */ | 33 | */ |
34 | int doAlignment(int max_width, int bevel, FbTk::Justify justify, | 34 | int doAlignment(int max_pixels, int bevel, FbTk::Justify justify, |
35 | const FbTk::Font &font, const char * const text, | 35 | const FbTk::Font &font, const char * const text, |
36 | unsigned int textlen, unsigned int &newlen); | 36 | unsigned int textlen, unsigned int &newlen); |
37 | 37 | ||