aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/TextUtils.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/TextUtils.cc')
-rw-r--r--src/FbTk/TextUtils.cc36
1 files changed, 22 insertions, 14 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
29namespace { 29namespace {
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
34void maxTextLength(int max_width, const FbTk::Font& font, const char* const text, 34void 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
72namespace FbTk { 80namespace FbTk {
73 81
74int doAlignment(int max_width, int bevel, FbTk::Justify justify, 82int 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;