aboutsummaryrefslogtreecommitdiff
path: root/src/tests
diff options
context:
space:
mode:
authorMathias Gumz <akira at fluxbox dot org>2012-10-02 12:24:47 (GMT)
committerMathias Gumz <akira at fluxbox dot org>2012-10-03 08:27:16 (GMT)
commit7b6ab828c7e5453a2720462156d165707935c9ef (patch)
tree3b641791b20b1994935e149fd7461decb11bef8c /src/tests
parent032a23d1e790c5224194562a837cc80fc157ce9b (diff)
downloadfluxbox-7b6ab828c7e5453a2720462156d165707935c9ef.zip
fluxbox-7b6ab828c7e5453a2720462156d165707935c9ef.tar.bz2
Improved vertical alignment of text in FbTk::TextButton
The old formula for vertical align text inside FbTk::TextButton ('height/2 + font_ascent/2 - 1') produced not always good looking results, escpecially when different fonts are involved (eg, ClockTool and WorkspaceName have different fonts and font-sizes). '(height - font_ascent) / 2 - 1' produces better results. Additional changes: * added ASCII-Art to document the involved entities when calculating the baseline * rewritten tests/testFont.cc to accept multiples texts and multiple fonts * removed some internal parts of FbTk::Font from the public interface
Diffstat (limited to 'src/tests')
-rw-r--r--src/tests/testFont.cc212
1 files changed, 106 insertions, 106 deletions
diff --git a/src/tests/testFont.cc b/src/tests/testFont.cc
index 1ba63f7..3fdc0a6 100644
--- a/src/tests/testFont.cc
+++ b/src/tests/testFont.cc
@@ -19,13 +19,16 @@
19// DEALINGS IN THE SOFTWARE. 19// DEALINGS IN THE SOFTWARE.
20 20
21#include "FbTk/App.hh" 21#include "FbTk/App.hh"
22#include "FbTk/FbString.hh"
22#include "FbTk/FbWindow.hh" 23#include "FbTk/FbWindow.hh"
23#include "FbTk/Font.hh" 24#include "FbTk/Font.hh"
25#include "FbTk/TextButton.hh"
24#include "FbTk/EventHandler.hh" 26#include "FbTk/EventHandler.hh"
25#include "FbTk/EventManager.hh" 27#include "FbTk/EventManager.hh"
26#include "FbTk/GContext.hh" 28#include "FbTk/GContext.hh"
27#include "FbTk/Color.hh" 29#include "FbTk/Color.hh"
28#include "FbTk/FbString.hh" 30#include "FbTk/FbString.hh"
31#include "FbTk/StringUtil.hh"
29 32
30#include <X11/Xutil.h> 33#include <X11/Xutil.h>
31#include <X11/keysym.h> 34#include <X11/keysym.h>
@@ -34,124 +37,103 @@
34#include <cstring> 37#include <cstring>
35#include <cstdlib> 38#include <cstdlib>
36#include <iostream> 39#include <iostream>
40#include <vector>
37using namespace std; 41using namespace std;
38 42
39class App:public FbTk::App, public FbTk::EventHandler { 43
44class App:public FbTk::App, public FbTk::FbWindow, public FbTk::EventHandler {
40public: 45public:
41 App(const char *displayname, const string &foreground, const string background): 46 App(const char *displayname, const string& foreground, const string& background):
42 FbTk::App(displayname), 47 FbTk::App(displayname),
43 m_win(DefaultScreen(display()), 48 FbTk::FbWindow(DefaultScreen(this->FbTk::App::display()), 0, 0, 640, 480, KeyPressMask|ExposureMask|StructureNotifyMask),
44 0, 0, 640, 480, KeyPressMask | ExposureMask) { 49 m_gc(drawable()),
45 m_background = background; 50 m_foreground(foreground.c_str(), screenNumber()),
46 m_foreground = foreground; 51 m_background(background.c_str(), screenNumber()) {
47 m_orient = FbTk::ROT0; 52
48 m_win.show(); 53 m_gc.setLineAttributes(1, FbTk::GContext::JOINMITER, FbTk::GContext::LINESOLID, FbTk::GContext::CAPNOTLAST);
49 m_win.setBackgroundColor(FbTk::Color(background.c_str(), m_win.screenNumber())); 54 m_gc.setForeground(m_foreground);
50 FbTk::EventManager::instance()->add(*this, m_win); 55 m_gc.setBackground(m_background);
51 } 56 setBackgroundColor(m_background);
52 ~App() { 57
58 FbTk::EventManager::instance()->add(*this, *this);
53 } 59 }
60
61 ~App() { }
62
54 void keyPressEvent(XKeyEvent &ke) { 63 void keyPressEvent(XKeyEvent &ke) {
55 KeySym ks; 64 KeySym ks;
56 char keychar[1]; 65 char keychar[1];
57 XLookupString(&ke, keychar, 1, &ks, 0); 66 XLookupString(&ke, keychar, 1, &ks, 0);
58 if (ks == XK_Escape) 67 if (ks == XK_Escape) {
59 end(); 68 end();
60 /* 69 }
61 else { // toggle antialias
62 m_font.setAntialias(!m_font.isAntialias());
63 cerr<<boolalpha;
64 cerr<<"antialias: "<<m_font.isAntialias()<<endl;
65 redraw();
66 } */
67 } 70 }
68 71
72 virtual void handleEvent(XEvent& event) {
73 if (event.type == ConfigureNotify) {
74 resize(event.xconfigure.width, event.xconfigure.height);
75 }
76 }
77
78
69 void exposeEvent(XExposeEvent &event) { 79 void exposeEvent(XExposeEvent &event) {
70 redraw(); 80 redraw();
71 } 81 }
72 82
73 void redraw() { 83 void redraw() {
74 size_t text_w = m_font.textWidth(m_text.c_str(), m_text.size()); 84 size_t i;
75 int mult = 1; 85 for (i = 0; i < m_buttons.size(); ++i) {
76 if (m_orient == FbTk::ROT180) 86 FbTk::TextButton* b = m_buttons[i];
77 mult = -1; 87 b->clear();
78 size_t text_h = m_font.height(); 88 b->drawLine(m_gc.gc(), 0, b->height() / 2, b->width(), b->height() / 2);
79 int x = 640/2 - mult* text_w/2;
80 int y = 480/2 - mult*text_h/2;
81 m_win.clear();
82 FbTk::GContext wingc(m_win.drawable());
83
84 int bx1 = 0;
85 int by1 = 0;
86 int bx2 = text_w;
87 int by2 = 0;
88
89 switch (m_orient) {
90 case FbTk::ROT90:
91 by2 = bx2;
92 bx2 = 0;
93 break;
94 case FbTk::ROT180:
95 bx2 = -bx2;
96 break;
97 case FbTk::ROT270:
98 by2 = -bx2;
99 bx2 = 0;
100 break;
101 default:
102 break;
103 } 89 }
90 this->clear();
91 }
104 92
105/* 93 void resize(unsigned int width, unsigned int height) {
106 m_win.drawLine(wingc.gc(), 94 FbTk::FbWindow::resize(width, height);
107 x, y + m_font.descent(), 95 unsigned w = width / m_buttons.size();
108 x + text_w, y + m_font.descent()); 96 size_t i;
109 m_win.drawLine(wingc.gc(), 97 for (i = 0; i < m_buttons.size(); ++i) {
110 x, y - text_h, 98 m_buttons[i]->moveResize(i * w, 0, w, height);
111 x + text_w, y - text_h); 99 }
112*/ 100 redraw();
113 // draw the baseline in red
114 wingc.setForeground(FbTk::Color("red", m_win.screenNumber()));
115 m_win.drawLine(wingc.gc(),
116 x + bx1, y + by1, x + bx2, y+by2);
117 wingc.setForeground(FbTk::Color(m_foreground.c_str(), m_win.screenNumber()));
118 cerr<<"text size "<<text_w<<"x"<<text_h<<endl;
119 m_font.drawText(m_win,
120 0, wingc.gc(),
121 m_text.c_str(), m_text.size(),
122 x, y, m_orient);
123
124 } 101 }
125 102
126 FbTk::Font &font() { return m_font; } 103 void addText(const FbTk::BiDiString& text, FbTk::Font& font, const FbTk::Orientation orient) {
127 void setText(const std::string& text, const FbTk::Orientation orient) { m_text = text; m_orient = orient; } 104
105 FbTk::FbWindow* win = this;
106 FbTk::TextButton* button = new FbTk::TextButton(*win, font, text);
107
108 button->setGC(m_gc.gc());
109 button->setOrientation(orient);
110 button->setBackgroundColor(m_background);
111 button->show();
112
113 m_buttons.push_back(button);
114 }
128 115
129private: 116private:
130 string m_foreground, m_background; 117 vector<FbTk::TextButton*> m_buttons;
131 FbTk::FbWindow m_win; 118 FbTk::GContext m_gc;
132 FbTk::Font m_font; 119 FbTk::Color m_foreground;
133 FbTk::Orientation m_orient; 120 FbTk::Color m_background;
134 string m_text;
135}; 121};
136 122
123
124
137int main(int argc, char **argv) { 125int main(int argc, char **argv) {
138 //bool antialias = false; 126
127 vector<string> texts_and_fonts;
139 FbTk::Orientation orient = FbTk::ROT0; 128 FbTk::Orientation orient = FbTk::ROT0;
140 bool xft = false;
141 string fontname("");
142 string displayname(""); 129 string displayname("");
143 string background("black"); 130 string background("white");
144 string foreground("white"); 131 string foreground("black");
145 string text("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-_¯åäöÅÄÖ^~+=`\"!#¤%&/()=¡@£$½¥{[]}¶½§±"); 132
146 for (int a=1; a<argc; ++a) { 133 int a;
147 if (strcmp("-font", argv[a])==0 && a + 1 < argc) { 134 for (a = 1; a < argc; ++a) {
148 fontname = argv[++a]; 135 if (strcmp("-display", argv[a]) == 0 && a + 1 < argc) {
149 } else if (strcmp("-xft", argv[a])==0) {
150 xft = true;
151 } else if (strcmp("-display", argv[a]) == 0 && a + 1 < argc) {
152 displayname = argv[++a]; 136 displayname = argv[++a];
153 } else if (strcmp("-text", argv[a]) == 0 && a + 1 < argc) {
154 text = argv[++a];
155 } else if (strcmp("-orient", argv[a]) == 0) { 137 } else if (strcmp("-orient", argv[a]) == 0) {
156 orient = (FbTk::Orientation) (atoi(argv[++a]) % 4); 138 orient = (FbTk::Orientation) (atoi(argv[++a]) % 4);
157 } else if (strcmp("-bg", argv[a]) == 0 && a + 1 < argc) { 139 } else if (strcmp("-bg", argv[a]) == 0 && a + 1 < argc) {
@@ -159,35 +141,53 @@ int main(int argc, char **argv) {
159 } else if (strcmp("-fg", argv[a]) == 0 && a + 1 < argc) { 141 } else if (strcmp("-fg", argv[a]) == 0 && a + 1 < argc) {
160 foreground = argv[++a]; 142 foreground = argv[++a];
161 } else if (strcmp("-h", argv[a]) == 0) { 143 } else if (strcmp("-h", argv[a]) == 0) {
162 cerr<<"Arguments: "<<endl; 144 cerr<<"Arguments: \"text|fontname\" [\"text|fontname2\"]"<<endl;
163 cerr<<"-font <fontname>"<<endl;
164 // cerr<<"-antialias"<<endl;
165 cerr<<"-display <display>"<<endl; 145 cerr<<"-display <display>"<<endl;
166 cerr<<"-text <text>"<<endl;
167 cerr<<"-orient"<<endl; 146 cerr<<"-orient"<<endl;
168 cerr<<"-fg <foreground color>"<<endl; 147 cerr<<"-fg <foreground color>"<<endl;
169 cerr<<"-bg <background color>"<<endl; 148 cerr<<"-bg <background color>"<<endl;
170 cerr<<"-h"<<endl; 149 cerr<<"-h"<<endl;
171 exit(0); 150 exit(0);
151 } else {
152 texts_and_fonts.push_back(argv[a]);
172 } 153 }
173
174 } 154 }
175
176 155
177 App app(displayname.c_str(), foreground, background); 156 if (texts_and_fonts.empty()) {
178 //app.font().setAntialias(antialias); 157 texts_and_fonts.push_back("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-_¯åäöÅÄÖ^~+=`\"!#¤%&/()=¡@£$½¥{[]}¶½§±|default");
179 if (!app.font().load(fontname.c_str()))
180 cerr<<"Failed to load: "<<fontname<<endl;
181 if (orient && !app.font().validOrientation(orient)) {
182 cerr<<"Orientation not valid ("<<orient<<")"<<endl;
183 orient = FbTk::ROT0;
184 } 158 }
185 // utf-8 it
186 159
187 cerr<<"Setting text: "<<text<<endl; 160 App app(displayname.c_str(), foreground, background);
188 app.setText(FbTk::FbStringUtil::XStrToFb(text), orient); 161 app.show();
162
163 for (a = 0; a < texts_and_fonts.size(); ++a) {
164
165 vector<string> tf;
166 FbTk::StringUtil::stringtok(tf, texts_and_fonts[a], "|");
167 if (tf.size() < 2) {
168 tf.push_back("default");
169 }
170
171 FbTk::Font* f = new FbTk::Font(0);
172 if (f->load(tf[1])) {
173
174 if (orient && !f->validOrientation(orient)) {
175 cerr<<"Orientation not valid ("<<orient<<")"<<endl;
176 orient = FbTk::ROT0;
177 }
189 178
179 app.addText(FbTk::FbStringUtil::XStrToFb(tf[0]), *f, orient);
180
181 } else {
182 cerr<<"Failed to load: "<<tf[1]<<endl;
183 delete f;
184 }
185 }
186
187 app.resize(app.width(), app.height());
190 app.redraw(); 188 app.redraw();
191 app.eventLoop(); 189 app.eventLoop();
192 190
191 return 0;
193} 192}
193