diff options
Diffstat (limited to 'src/tests/testFont.cc')
-rw-r--r-- | src/tests/testFont.cc | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/src/tests/testFont.cc b/src/tests/testFont.cc new file mode 100644 index 0000000..c450216 --- /dev/null +++ b/src/tests/testFont.cc | |||
@@ -0,0 +1,136 @@ | |||
1 | // testFont.cc for fbtk test suite | ||
2 | // Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id: testFont.cc,v 1.6 2003/09/11 16:51:03 fluxgen Exp $ | ||
23 | |||
24 | #include "App.hh" | ||
25 | #include "FbWindow.hh" | ||
26 | #include "Font.hh" | ||
27 | #include "EventHandler.hh" | ||
28 | #include "EventManager.hh" | ||
29 | #include "GContext.hh" | ||
30 | #include "Color.hh" | ||
31 | |||
32 | #include <X11/Xutil.h> | ||
33 | #include <X11/keysym.h> | ||
34 | |||
35 | #include <string> | ||
36 | #include <iostream> | ||
37 | using namespace std; | ||
38 | |||
39 | class App:public FbTk::App, public FbTk::EventHandler { | ||
40 | public: | ||
41 | App(const char *displayname): | ||
42 | FbTk::App(displayname), | ||
43 | m_win(DefaultScreen(display()), | ||
44 | 0, 0, 640, 480, KeyPressMask | ExposureMask) { | ||
45 | |||
46 | m_win.show(); | ||
47 | m_win.setBackgroundColor(FbTk::Color("white", m_win.screenNumber())); | ||
48 | FbTk::EventManager::instance()->add(*this, m_win); | ||
49 | } | ||
50 | ~App() { | ||
51 | } | ||
52 | void keyPressEvent(XKeyEvent &ke) { | ||
53 | KeySym ks; | ||
54 | char keychar[1]; | ||
55 | XLookupString(&ke, keychar, 1, &ks, 0); | ||
56 | if (ks == XK_Escape) | ||
57 | end(); | ||
58 | else { // toggle antialias | ||
59 | m_font.setAntialias(!m_font.isAntialias()); | ||
60 | cerr<<boolalpha; | ||
61 | cerr<<"antialias: "<<m_font.isAntialias()<<endl; | ||
62 | redraw(); | ||
63 | } | ||
64 | } | ||
65 | |||
66 | void exposeEvent(XExposeEvent &event) { | ||
67 | redraw(); | ||
68 | } | ||
69 | |||
70 | void redraw() { | ||
71 | size_t text_w = m_font.textWidth(m_text.c_str(), m_text.size()); | ||
72 | size_t text_h = m_font.height(); | ||
73 | int x = 640/2 - text_w/2; | ||
74 | int y = 480/2 - text_h/2; | ||
75 | m_win.clear(); | ||
76 | FbTk::GContext wingc(m_win.drawable()); | ||
77 | |||
78 | m_win.drawLine(wingc.gc(), | ||
79 | x, y + m_font.descent(), | ||
80 | x + text_w, y + m_font.descent()); | ||
81 | m_win.drawLine(wingc.gc(), | ||
82 | x, y - text_h, | ||
83 | x + text_w, y - text_h); | ||
84 | wingc.setForeground(FbTk::Color("red", m_win.screenNumber())); | ||
85 | m_win.drawLine(wingc.gc(), | ||
86 | x, y, x + text_w, y); | ||
87 | wingc.setForeground(FbTk::Color("black", m_win.screenNumber())); | ||
88 | m_font.drawText(m_win.drawable(), | ||
89 | 0, wingc.gc(), | ||
90 | m_text.c_str(), m_text.size(), | ||
91 | x, y); | ||
92 | |||
93 | } | ||
94 | |||
95 | FbTk::Font &font() { return m_font; } | ||
96 | void setText(const std::string& text) { m_text = text; } | ||
97 | |||
98 | private: | ||
99 | FbTk::FbWindow m_win; | ||
100 | FbTk::Font m_font; | ||
101 | string m_text; | ||
102 | }; | ||
103 | |||
104 | int main(int argc, char **argv) { | ||
105 | bool antialias = false; | ||
106 | bool rotate = false; | ||
107 | string fontname("fixed"); | ||
108 | string displayname(""); | ||
109 | string text("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,.-_¯åäöÅÄÖ^~+=`\"!#¤%&/()=¡@£$½¥{[]}¶½§±"); | ||
110 | for (int a=1; a<argc; ++a) { | ||
111 | if (strcmp("-font", argv[a])==0 && a + 1 < argc) { | ||
112 | fontname = argv[++a]; | ||
113 | } else if (strcmp("-antialias", argv[a]) == 0) { | ||
114 | antialias = true; | ||
115 | } else if (strcmp("-display", argv[a]) == 0 && a + 1 < argc) { | ||
116 | displayname = argv[++a]; | ||
117 | } else if (strcmp("-text", argv[a]) == 0 && a + 1 < argc) { | ||
118 | text = argv[++a]; | ||
119 | } else if (strcmp("-rotate", argv[a]) == 0) | ||
120 | rotate = true; | ||
121 | |||
122 | } | ||
123 | |||
124 | App app(displayname.c_str()); | ||
125 | app.font().setAntialias(antialias); | ||
126 | if (!app.font().load(fontname.c_str())) | ||
127 | cerr<<"Failed to load: "<<fontname<<endl; | ||
128 | |||
129 | app.setText(text); | ||
130 | if (rotate) | ||
131 | app.font().rotate(90); | ||
132 | |||
133 | app.redraw(); | ||
134 | app.eventLoop(); | ||
135 | |||
136 | } | ||