1 | /*
|
---|
2 | * Copyright (c) 2014 Martin Sucha
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup fontviewer
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <malloc.h>
|
---|
39 | #include <stdbool.h>
|
---|
40 | #include <window.h>
|
---|
41 | #include <canvas.h>
|
---|
42 | #include <surface.h>
|
---|
43 | #include <codec/tga.h>
|
---|
44 | #include <task.h>
|
---|
45 | #include <drawctx.h>
|
---|
46 | #include <font/embedded.h>
|
---|
47 | #include <font/pcf.h>
|
---|
48 | #include <stdarg.h>
|
---|
49 | #include <io/verify.h>
|
---|
50 |
|
---|
51 | #define NAME "fontviewer"
|
---|
52 |
|
---|
53 | #define WINDOW_WIDTH 640
|
---|
54 | #define WINDOW_HEIGHT 480
|
---|
55 |
|
---|
56 | static window_t *main_window;
|
---|
57 | static surface_t *surface = NULL;
|
---|
58 | static canvas_t *canvas = NULL;
|
---|
59 | static surface_coord_t width, height;
|
---|
60 | uint16_t points = 16;
|
---|
61 | bool show_metrics = true;
|
---|
62 | char *font_path = NULL;
|
---|
63 |
|
---|
64 | static int draw(void);
|
---|
65 |
|
---|
66 | static void on_keyboard_event(widget_t *widget, void *data)
|
---|
67 | {
|
---|
68 | kbd_event_t *event = (kbd_event_t *) data;
|
---|
69 |
|
---|
70 | if (event->type == KEY_PRESS) {
|
---|
71 | if (event->c == 'q')
|
---|
72 | exit(0);
|
---|
73 |
|
---|
74 | if (event->key == KC_UP || event->key == KC_DOWN) {
|
---|
75 | uint16_t increment = (event->mods & KM_SHIFT) ? 10 : 1;
|
---|
76 |
|
---|
77 | if (event->key == KC_UP)
|
---|
78 | points += increment;
|
---|
79 |
|
---|
80 | if (event->key == KC_DOWN) {
|
---|
81 | if (points <= increment) {
|
---|
82 | points = 1;
|
---|
83 | }
|
---|
84 | else {
|
---|
85 | points-= increment;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (points < 1)
|
---|
90 | points = 1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (event->c == 'm')
|
---|
94 | show_metrics = !show_metrics;
|
---|
95 | }
|
---|
96 |
|
---|
97 | int rc = draw();
|
---|
98 | if (rc != EOK) {
|
---|
99 | printf("Failed drawing: %d.\n", rc);
|
---|
100 | exit(1);
|
---|
101 | }
|
---|
102 | update_canvas(canvas, surface);
|
---|
103 | }
|
---|
104 |
|
---|
105 | static int create_font(font_t **font, uint16_t points)
|
---|
106 | {
|
---|
107 | if (font_path == NULL) {
|
---|
108 | return embedded_font_create(font, points);
|
---|
109 | }
|
---|
110 |
|
---|
111 | return pcf_font_create(font, font_path, points);
|
---|
112 | }
|
---|
113 |
|
---|
114 | static source_t rgb(uint8_t r, uint8_t g, uint8_t b)
|
---|
115 | {
|
---|
116 | source_t source;
|
---|
117 | source_init(&source);
|
---|
118 | source_set_color(&source, PIXEL(255, r, g, b));
|
---|
119 | return source;
|
---|
120 | }
|
---|
121 |
|
---|
122 | static void horizontal_rectangle(drawctx_t *drawctx, surface_coord_t x1,
|
---|
123 | surface_coord_t y1, surface_coord_t x2, surface_coord_t y2,
|
---|
124 | source_t *source)
|
---|
125 | {
|
---|
126 | if (y2 < y1)
|
---|
127 | return;
|
---|
128 |
|
---|
129 | drawctx_set_source(drawctx, source);
|
---|
130 | drawctx_transfer(drawctx, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
|
---|
131 | }
|
---|
132 |
|
---|
133 | static void horizontal_line(drawctx_t *drawctx, surface_coord_t y,
|
---|
134 | surface_coord_t x1, surface_coord_t x2, source_t *source)
|
---|
135 | {
|
---|
136 | horizontal_rectangle(drawctx, x1, y, x2, y, source);
|
---|
137 | }
|
---|
138 |
|
---|
139 | static int text(drawctx_t *, font_t *, source_t *, surface_coord_t x,
|
---|
140 | surface_coord_t , const char *, ...) PRINTF_ATTRIBUTE(6, 7);
|
---|
141 | static int text(drawctx_t *drawctx, font_t *font, source_t *source,
|
---|
142 | surface_coord_t x, surface_coord_t y, const char *fmt, ...)
|
---|
143 | {
|
---|
144 | char *str = NULL;
|
---|
145 | va_list args;
|
---|
146 | va_start(args, fmt);
|
---|
147 | int ret = vasprintf(&str, fmt, args);
|
---|
148 | va_end(args);
|
---|
149 |
|
---|
150 | if (ret <= 0)
|
---|
151 | return ret;
|
---|
152 |
|
---|
153 | drawctx_set_source(drawctx, source);
|
---|
154 | drawctx_set_font(drawctx, font);
|
---|
155 | drawctx_print(drawctx, str, x, y);
|
---|
156 |
|
---|
157 | free(str);
|
---|
158 |
|
---|
159 | return ret;
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | static int draw(void)
|
---|
164 | {
|
---|
165 | source_t background = rgb(255, 255, 255);
|
---|
166 | source_t foreground = rgb(0, 0, 0);
|
---|
167 | source_t glyphs = rgb(0, 0, 255);
|
---|
168 | source_t ascender_bg = rgb(255, 230, 128);
|
---|
169 | source_t ascender_fg = rgb(255, 153, 85);
|
---|
170 | source_t descender_bg = rgb(204, 255, 170);
|
---|
171 | source_t descender_fg = rgb(85, 212, 0);
|
---|
172 | source_t leading_bg = rgb(170, 238, 255);
|
---|
173 | source_t leading_fg = rgb(0, 170, 212);
|
---|
174 |
|
---|
175 | font_t *font;
|
---|
176 | int rc = create_font(&font, points);
|
---|
177 | if (rc != EOK) {
|
---|
178 | printf("Failed creating font\n");
|
---|
179 | return rc;
|
---|
180 | }
|
---|
181 |
|
---|
182 | font_t *info_font;
|
---|
183 | rc = embedded_font_create(&info_font, 16);
|
---|
184 | if (rc != EOK) {
|
---|
185 | printf("Failed creating info font\n");
|
---|
186 | return rc;
|
---|
187 | }
|
---|
188 |
|
---|
189 | font_metrics_t font_metrics;
|
---|
190 | rc = font_get_metrics(font, &font_metrics);
|
---|
191 | if (rc != EOK)
|
---|
192 | return rc;
|
---|
193 |
|
---|
194 | surface_coord_t top = 50;
|
---|
195 | metric_t ascender_top = top;
|
---|
196 | metric_t descender_top = ascender_top + font_metrics.ascender;
|
---|
197 | metric_t leading_top = descender_top + font_metrics.descender;
|
---|
198 | metric_t line_bottom = leading_top + font_metrics.leading;
|
---|
199 |
|
---|
200 | drawctx_t drawctx;
|
---|
201 | drawctx_init(&drawctx, surface);
|
---|
202 |
|
---|
203 | drawctx_set_source(&drawctx, &background);
|
---|
204 | drawctx_transfer(&drawctx, 0, 0,
|
---|
205 | width, height);
|
---|
206 |
|
---|
207 | if (show_metrics) {
|
---|
208 | horizontal_rectangle(&drawctx, 0, ascender_top, width,
|
---|
209 | descender_top - 1, &ascender_bg);
|
---|
210 | horizontal_line(&drawctx, ascender_top, 0, width,
|
---|
211 | &ascender_fg);
|
---|
212 |
|
---|
213 | horizontal_rectangle(&drawctx, 0, descender_top, width,
|
---|
214 | leading_top - 1, &descender_bg);
|
---|
215 | horizontal_line(&drawctx, descender_top, 0, width,
|
---|
216 | &descender_fg);
|
---|
217 |
|
---|
218 | horizontal_rectangle(&drawctx, 0, leading_top,
|
---|
219 | width, line_bottom - 1, &leading_bg);
|
---|
220 | horizontal_line(&drawctx, leading_top, 0, width,
|
---|
221 | &leading_fg);
|
---|
222 | }
|
---|
223 |
|
---|
224 | drawctx_set_source(&drawctx, &glyphs);
|
---|
225 | drawctx_set_font(&drawctx, font);
|
---|
226 | drawctx_print(&drawctx, "Čaj'_", 0, top);
|
---|
227 |
|
---|
228 | if (show_metrics) {
|
---|
229 | surface_coord_t infos_top = line_bottom + 10;
|
---|
230 | text(&drawctx, info_font, &ascender_fg, 0, infos_top,
|
---|
231 | "Ascender: %d", font_metrics.ascender);
|
---|
232 | text(&drawctx, info_font, &descender_fg, 0, infos_top + 16,
|
---|
233 | "Descender: %d", font_metrics.descender);
|
---|
234 | text(&drawctx, info_font, &foreground, 0, infos_top + 32,
|
---|
235 | "Line height: %d",
|
---|
236 | font_metrics.ascender + font_metrics.descender);
|
---|
237 | text(&drawctx, info_font, &leading_fg, 0, infos_top + 48,
|
---|
238 | "Leading: %d", font_metrics.leading);
|
---|
239 |
|
---|
240 | }
|
---|
241 |
|
---|
242 | font_release(font);
|
---|
243 | return EOK;
|
---|
244 | }
|
---|
245 |
|
---|
246 | int main(int argc, char *argv[])
|
---|
247 | {
|
---|
248 | if (argc < 2) {
|
---|
249 | printf("Compositor server not specified.\n");
|
---|
250 | return 1;
|
---|
251 | }
|
---|
252 |
|
---|
253 | if (argc < 3) {
|
---|
254 | font_path = NULL;
|
---|
255 | }
|
---|
256 | else {
|
---|
257 | font_path = argv[2];
|
---|
258 | }
|
---|
259 |
|
---|
260 | main_window = window_open(argv[1], NULL, WINDOW_MAIN, "fontviewer");
|
---|
261 | if (!main_window) {
|
---|
262 | printf("Cannot open main window.\n");
|
---|
263 | return 2;
|
---|
264 | }
|
---|
265 |
|
---|
266 | surface = surface_create(WINDOW_WIDTH, WINDOW_HEIGHT, NULL,
|
---|
267 | SURFACE_FLAG_NONE);
|
---|
268 | if (surface == NULL) {
|
---|
269 | printf("Cannot create surface.\n");
|
---|
270 | return 2;
|
---|
271 | }
|
---|
272 |
|
---|
273 | width = WINDOW_WIDTH;
|
---|
274 | height = WINDOW_HEIGHT;
|
---|
275 |
|
---|
276 | int rc = draw();
|
---|
277 | if (rc != EOK) {
|
---|
278 | printf("Failed drawing: %d.\n", rc);
|
---|
279 | return 2;
|
---|
280 | }
|
---|
281 |
|
---|
282 | canvas = create_canvas(window_root(main_window), NULL,
|
---|
283 | WINDOW_WIDTH, WINDOW_HEIGHT, surface);
|
---|
284 | if (canvas == NULL) {
|
---|
285 | printf("Cannot create canvas.\n");
|
---|
286 | return 2;
|
---|
287 | }
|
---|
288 | sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
|
---|
289 |
|
---|
290 | window_resize(main_window, 200, 200, WINDOW_WIDTH, WINDOW_HEIGHT,
|
---|
291 | WINDOW_PLACEMENT_ABSOLUTE);
|
---|
292 | window_exec(main_window);
|
---|
293 |
|
---|
294 | task_retval(0);
|
---|
295 | async_manager();
|
---|
296 |
|
---|
297 | return 0;
|
---|
298 | }
|
---|
299 |
|
---|
300 | /** @}
|
---|
301 | */
|
---|