source: mainline/uspace/app/fontviewer/fontviewer.c@ 0016674

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0016674 was c1694b6b, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Add str_error() in numerous places.

  • Property mode set to 100644
File size: 7.6 KB
Line 
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_error.h>
41#include <window.h>
42#include <canvas.h>
43#include <surface.h>
44#include <codec/tga.h>
45#include <task.h>
46#include <drawctx.h>
47#include <font/embedded.h>
48#include <font/pcf.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
57static window_t *main_window;
58static surface_t *surface = NULL;
59static canvas_t *canvas = NULL;
60static surface_coord_t width, height;
61uint16_t points = 16;
62bool show_metrics = true;
63char *font_path = NULL;
64
65static int draw(void);
66
67static 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 }
85 else {
86 points-= increment;
87 }
88 }
89
90 if (points < 1)
91 points = 1;
92 }
93
94 if (event->c == 'm')
95 show_metrics = !show_metrics;
96 }
97
98 int rc = draw();
99 if (rc != EOK) {
100 printf("Failed drawing: %s.\n", str_error(rc));
101 exit(1);
102 }
103 update_canvas(canvas, surface);
104}
105
106static int create_font(font_t **font, uint16_t points)
107{
108 if (font_path == NULL) {
109 return embedded_font_create(font, points);
110 }
111
112 return pcf_font_create(font, font_path, points);
113}
114
115static source_t rgb(uint8_t r, uint8_t g, uint8_t b)
116{
117 source_t source;
118 source_init(&source);
119 source_set_color(&source, PIXEL(255, r, g, b));
120 return source;
121}
122
123static void horizontal_rectangle(drawctx_t *drawctx, surface_coord_t x1,
124 surface_coord_t y1, surface_coord_t x2, surface_coord_t y2,
125 source_t *source)
126{
127 if (y2 < y1)
128 return;
129
130 drawctx_set_source(drawctx, source);
131 drawctx_transfer(drawctx, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
132}
133
134static void horizontal_line(drawctx_t *drawctx, surface_coord_t y,
135 surface_coord_t x1, surface_coord_t x2, source_t *source)
136{
137 horizontal_rectangle(drawctx, x1, y, x2, y, source);
138}
139
140static int text(drawctx_t *, font_t *, source_t *, surface_coord_t x,
141 surface_coord_t , const char *, ...) PRINTF_ATTRIBUTE(6, 7);
142static int text(drawctx_t *drawctx, font_t *font, source_t *source,
143 surface_coord_t x, surface_coord_t y, const char *fmt, ...)
144{
145 char *str = NULL;
146 va_list args;
147 va_start(args, fmt);
148 int ret = vasprintf(&str, fmt, args);
149 va_end(args);
150
151 if (ret <= 0)
152 return ret;
153
154 drawctx_set_source(drawctx, source);
155 drawctx_set_font(drawctx, font);
156 drawctx_print(drawctx, str, x, y);
157
158 free(str);
159
160 return ret;
161}
162
163
164static int draw(void)
165{
166 source_t background = rgb(255, 255, 255);
167 source_t foreground = rgb(0, 0, 0);
168 source_t glyphs = rgb(0, 0, 255);
169 source_t ascender_bg = rgb(255, 230, 128);
170 source_t ascender_fg = rgb(255, 153, 85);
171 source_t descender_bg = rgb(204, 255, 170);
172 source_t descender_fg = rgb(85, 212, 0);
173 source_t leading_bg = rgb(170, 238, 255);
174 source_t leading_fg = rgb(0, 170, 212);
175
176 font_t *font;
177 int rc = create_font(&font, points);
178 if (rc != EOK) {
179 printf("Failed creating font\n");
180 return rc;
181 }
182
183 font_t *info_font;
184 rc = embedded_font_create(&info_font, 16);
185 if (rc != EOK) {
186 printf("Failed creating info font\n");
187 return rc;
188 }
189
190 font_metrics_t font_metrics;
191 rc = font_get_metrics(font, &font_metrics);
192 if (rc != EOK)
193 return rc;
194
195 surface_coord_t top = 50;
196 metric_t ascender_top = top;
197 metric_t descender_top = ascender_top + font_metrics.ascender;
198 metric_t leading_top = descender_top + font_metrics.descender;
199 metric_t line_bottom = leading_top + font_metrics.leading;
200
201 drawctx_t drawctx;
202 drawctx_init(&drawctx, surface);
203
204 drawctx_set_source(&drawctx, &background);
205 drawctx_transfer(&drawctx, 0, 0,
206 width, height);
207
208 if (show_metrics) {
209 horizontal_rectangle(&drawctx, 0, ascender_top, width,
210 descender_top - 1, &ascender_bg);
211 horizontal_line(&drawctx, ascender_top, 0, width,
212 &ascender_fg);
213
214 horizontal_rectangle(&drawctx, 0, descender_top, width,
215 leading_top - 1, &descender_bg);
216 horizontal_line(&drawctx, descender_top, 0, width,
217 &descender_fg);
218
219 horizontal_rectangle(&drawctx, 0, leading_top,
220 width, line_bottom - 1, &leading_bg);
221 horizontal_line(&drawctx, leading_top, 0, width,
222 &leading_fg);
223 }
224
225 drawctx_set_source(&drawctx, &glyphs);
226 drawctx_set_font(&drawctx, font);
227 drawctx_print(&drawctx, "Čaj'_", 0, top);
228
229 if (show_metrics) {
230 surface_coord_t infos_top = line_bottom + 10;
231 text(&drawctx, info_font, &ascender_fg, 0, infos_top,
232 "Ascender: %d", font_metrics.ascender);
233 text(&drawctx, info_font, &descender_fg, 0, infos_top + 16,
234 "Descender: %d", font_metrics.descender);
235 text(&drawctx, info_font, &foreground, 0, infos_top + 32,
236 "Line height: %d",
237 font_metrics.ascender + font_metrics.descender);
238 text(&drawctx, info_font, &leading_fg, 0, infos_top + 48,
239 "Leading: %d", font_metrics.leading);
240
241 }
242
243 font_release(font);
244 return EOK;
245}
246
247int main(int argc, char *argv[])
248{
249 if (argc < 2) {
250 printf("Compositor server not specified.\n");
251 return 1;
252 }
253
254 if (argc < 3) {
255 font_path = NULL;
256 }
257 else {
258 font_path = argv[2];
259 }
260
261 main_window = window_open(argv[1], NULL, WINDOW_MAIN, "fontviewer");
262 if (!main_window) {
263 printf("Cannot open main window.\n");
264 return 2;
265 }
266
267 surface = surface_create(WINDOW_WIDTH, WINDOW_HEIGHT, NULL,
268 SURFACE_FLAG_NONE);
269 if (surface == NULL) {
270 printf("Cannot create surface.\n");
271 return 2;
272 }
273
274 width = WINDOW_WIDTH;
275 height = WINDOW_HEIGHT;
276
277 int rc = draw();
278 if (rc != EOK) {
279 printf("Failed drawing: %s.\n", str_error(rc));
280 return 2;
281 }
282
283 canvas = create_canvas(window_root(main_window), NULL,
284 WINDOW_WIDTH, WINDOW_HEIGHT, surface);
285 if (canvas == NULL) {
286 printf("Cannot create canvas.\n");
287 return 2;
288 }
289 sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
290
291 window_resize(main_window, 200, 200, WINDOW_WIDTH, WINDOW_HEIGHT,
292 WINDOW_PLACEMENT_ABSOLUTE);
293 window_exec(main_window);
294
295 task_retval(0);
296 async_manager();
297
298 return 0;
299}
300
301/** @}
302 */
Note: See TracBrowser for help on using the repository browser.