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 <stdlib.h>
|
---|
39 | #include <stdbool.h>
|
---|
40 | #include <str.h>
|
---|
41 | #include <str_error.h>
|
---|
42 | #include <window.h>
|
---|
43 | #include <canvas.h>
|
---|
44 | #include <draw/surface.h>
|
---|
45 | #include <draw/codec.h>
|
---|
46 | #include <task.h>
|
---|
47 | #include <draw/drawctx.h>
|
---|
48 | #include <draw/font.h>
|
---|
49 | #include <stdarg.h>
|
---|
50 | #include <io/verify.h>
|
---|
51 |
|
---|
52 | #define NAME "fontviewer"
|
---|
53 |
|
---|
54 | #define WINDOW_WIDTH 640
|
---|
55 | #define WINDOW_HEIGHT 480
|
---|
56 |
|
---|
57 | static window_t *main_window;
|
---|
58 | static surface_t *surface = NULL;
|
---|
59 | static canvas_t *canvas = NULL;
|
---|
60 | static surface_coord_t width, height;
|
---|
61 | uint16_t points = 16;
|
---|
62 | bool show_metrics = true;
|
---|
63 | char *font_path = NULL;
|
---|
64 |
|
---|
65 | static errno_t draw(void);
|
---|
66 |
|
---|
67 | static void on_keyboard_event(widget_t *widget, void *data)
|
---|
68 | {
|
---|
69 | kbd_event_t *event = (kbd_event_t *) data;
|
---|
70 |
|
---|
71 | if (event->type == KEY_PRESS) {
|
---|
72 | if (event->c == 'q')
|
---|
73 | exit(0);
|
---|
74 |
|
---|
75 | if (event->key == KC_UP || event->key == KC_DOWN) {
|
---|
76 | uint16_t increment = (event->mods & KM_SHIFT) ? 10 : 1;
|
---|
77 |
|
---|
78 | if (event->key == KC_UP)
|
---|
79 | points += increment;
|
---|
80 |
|
---|
81 | if (event->key == KC_DOWN) {
|
---|
82 | if (points <= increment) {
|
---|
83 | points = 1;
|
---|
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 | errno_t rc = draw();
|
---|
98 | if (rc != EOK) {
|
---|
99 | printf("Failed drawing: %s.\n", str_error(rc));
|
---|
100 | exit(1);
|
---|
101 | }
|
---|
102 | update_canvas(canvas, surface);
|
---|
103 | }
|
---|
104 |
|
---|
105 | static errno_t 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 *, ...) _HELENOS_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 | drawctx_set_source(drawctx, source);
|
---|
152 | drawctx_set_font(drawctx, font);
|
---|
153 | drawctx_print(drawctx, str, x, y);
|
---|
154 |
|
---|
155 | free(str);
|
---|
156 | }
|
---|
157 |
|
---|
158 | return ret;
|
---|
159 | }
|
---|
160 |
|
---|
161 | static errno_t draw(void)
|
---|
162 | {
|
---|
163 | source_t background = rgb(255, 255, 255);
|
---|
164 | source_t foreground = rgb(0, 0, 0);
|
---|
165 | source_t glyphs = rgb(0, 0, 255);
|
---|
166 | source_t ascender_bg = rgb(255, 230, 128);
|
---|
167 | source_t ascender_fg = rgb(255, 153, 85);
|
---|
168 | source_t descender_bg = rgb(204, 255, 170);
|
---|
169 | source_t descender_fg = rgb(85, 212, 0);
|
---|
170 | source_t leading_bg = rgb(170, 238, 255);
|
---|
171 | source_t leading_fg = rgb(0, 170, 212);
|
---|
172 | font_t *info_font = NULL;
|
---|
173 | font_t *font = NULL;
|
---|
174 |
|
---|
175 | errno_t rc = create_font(&font, points);
|
---|
176 | if (rc != EOK) {
|
---|
177 | printf("Failed creating font\n");
|
---|
178 | goto out_err;
|
---|
179 | }
|
---|
180 |
|
---|
181 | rc = embedded_font_create(&info_font, 16);
|
---|
182 | if (rc != EOK) {
|
---|
183 | printf("Failed creating info font\n");
|
---|
184 | goto out_err;
|
---|
185 | }
|
---|
186 |
|
---|
187 | font_metrics_t font_metrics;
|
---|
188 | rc = font_get_metrics(font, &font_metrics);
|
---|
189 | if (rc != EOK)
|
---|
190 | goto out_err;
|
---|
191 |
|
---|
192 | surface_coord_t top = 50;
|
---|
193 | metric_t ascender_top = top;
|
---|
194 | metric_t descender_top = ascender_top + font_metrics.ascender;
|
---|
195 | metric_t leading_top = descender_top + font_metrics.descender;
|
---|
196 | metric_t line_bottom = leading_top + font_metrics.leading;
|
---|
197 |
|
---|
198 | drawctx_t drawctx;
|
---|
199 | drawctx_init(&drawctx, surface);
|
---|
200 |
|
---|
201 | drawctx_set_source(&drawctx, &background);
|
---|
202 | drawctx_transfer(&drawctx, 0, 0,
|
---|
203 | width, height);
|
---|
204 |
|
---|
205 | if (show_metrics) {
|
---|
206 | horizontal_rectangle(&drawctx, 0, ascender_top, width,
|
---|
207 | descender_top - 1, &ascender_bg);
|
---|
208 | horizontal_line(&drawctx, ascender_top, 0, width,
|
---|
209 | &ascender_fg);
|
---|
210 |
|
---|
211 | horizontal_rectangle(&drawctx, 0, descender_top, width,
|
---|
212 | leading_top - 1, &descender_bg);
|
---|
213 | horizontal_line(&drawctx, descender_top, 0, width,
|
---|
214 | &descender_fg);
|
---|
215 |
|
---|
216 | horizontal_rectangle(&drawctx, 0, leading_top,
|
---|
217 | width, line_bottom - 1, &leading_bg);
|
---|
218 | horizontal_line(&drawctx, leading_top, 0, width,
|
---|
219 | &leading_fg);
|
---|
220 | }
|
---|
221 |
|
---|
222 | drawctx_set_source(&drawctx, &glyphs);
|
---|
223 | drawctx_set_font(&drawctx, font);
|
---|
224 | drawctx_print(&drawctx, "Čaj'_", 0, top);
|
---|
225 |
|
---|
226 | if (show_metrics) {
|
---|
227 | surface_coord_t infos_top = line_bottom + 10;
|
---|
228 | text(&drawctx, info_font, &ascender_fg, 0, infos_top,
|
---|
229 | "Ascender: %d", font_metrics.ascender);
|
---|
230 | text(&drawctx, info_font, &descender_fg, 0, infos_top + 16,
|
---|
231 | "Descender: %d", font_metrics.descender);
|
---|
232 | text(&drawctx, info_font, &foreground, 0, infos_top + 32,
|
---|
233 | "Line height: %d",
|
---|
234 | font_metrics.ascender + font_metrics.descender);
|
---|
235 | text(&drawctx, info_font, &leading_fg, 0, infos_top + 48,
|
---|
236 | "Leading: %d", font_metrics.leading);
|
---|
237 |
|
---|
238 | }
|
---|
239 |
|
---|
240 | out_err:
|
---|
241 | if (font)
|
---|
242 | font_release(font);
|
---|
243 | if (info_font)
|
---|
244 | font_release(info_font);
|
---|
245 | return rc;
|
---|
246 | }
|
---|
247 |
|
---|
248 | static void print_syntax(void)
|
---|
249 | {
|
---|
250 | printf("Syntax: %s [-d <display>] [<font-file>]\n", NAME);
|
---|
251 | }
|
---|
252 |
|
---|
253 | int main(int argc, char *argv[])
|
---|
254 | {
|
---|
255 | const char *display_svc = DISPLAY_DEFAULT;
|
---|
256 | int i;
|
---|
257 |
|
---|
258 | i = 1;
|
---|
259 | while (i < argc && argv[i][0] == '-') {
|
---|
260 | if (str_cmp(argv[i], "-d") == 0) {
|
---|
261 | ++i;
|
---|
262 | if (i >= argc) {
|
---|
263 | printf("Argument missing.\n");
|
---|
264 | print_syntax();
|
---|
265 | return 1;
|
---|
266 | }
|
---|
267 |
|
---|
268 | display_svc = argv[i++];
|
---|
269 | } else {
|
---|
270 | printf("Invalid option '%s'.\n", argv[i]);
|
---|
271 | print_syntax();
|
---|
272 | return 1;
|
---|
273 | }
|
---|
274 | }
|
---|
275 |
|
---|
276 | if (i < argc) {
|
---|
277 | font_path = argv[i];
|
---|
278 | } else {
|
---|
279 | font_path = NULL;
|
---|
280 | }
|
---|
281 |
|
---|
282 | main_window = window_open(display_svc, NULL, WINDOW_MAIN, "fontviewer");
|
---|
283 | if (!main_window) {
|
---|
284 | printf("Cannot open main window.\n");
|
---|
285 | return 2;
|
---|
286 | }
|
---|
287 |
|
---|
288 | surface = surface_create(WINDOW_WIDTH, WINDOW_HEIGHT, NULL,
|
---|
289 | SURFACE_FLAG_NONE);
|
---|
290 | if (surface == NULL) {
|
---|
291 | printf("Cannot create surface.\n");
|
---|
292 | return 2;
|
---|
293 | }
|
---|
294 |
|
---|
295 | width = WINDOW_WIDTH;
|
---|
296 | height = WINDOW_HEIGHT;
|
---|
297 |
|
---|
298 | errno_t rc = draw();
|
---|
299 | if (rc != EOK) {
|
---|
300 | printf("Failed drawing: %s.\n", str_error(rc));
|
---|
301 | return 2;
|
---|
302 | }
|
---|
303 |
|
---|
304 | canvas = create_canvas(window_root(main_window), NULL,
|
---|
305 | WINDOW_WIDTH, WINDOW_HEIGHT, surface);
|
---|
306 | if (canvas == NULL) {
|
---|
307 | printf("Cannot create canvas.\n");
|
---|
308 | return 2;
|
---|
309 | }
|
---|
310 | sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
|
---|
311 |
|
---|
312 | window_resize(main_window, 200, 200, WINDOW_WIDTH, WINDOW_HEIGHT,
|
---|
313 | WINDOW_PLACEMENT_ABSOLUTE);
|
---|
314 | window_exec(main_window);
|
---|
315 |
|
---|
316 | task_retval(0);
|
---|
317 | async_manager();
|
---|
318 |
|
---|
319 | return 0;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /** @}
|
---|
323 | */
|
---|