source: mainline/uspace/app/fontviewer/fontviewer.c@ 794e368

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 794e368 was a9763c6, checked in by Martin Sucha <sucha14@…>, 11 years ago

Add a very simple font viewer application

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