source: mainline/uspace/app/fontviewer/fontviewer.c@ 984a9ba

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 984a9ba was 1433ecda, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 7.6 KB
RevLine 
[a9763c6]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>
[c23275a]36#include <stdlib.h>
[a9763c6]37#include <errno.h>
[38d150e]38#include <stdlib.h>
[a9763c6]39#include <stdbool.h>
[c1694b6b]40#include <str_error.h>
[a9763c6]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>
[c6c39d4f]48#include <font/pcf.h>
[a9763c6]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;
[c6c39d4f]63char *font_path = NULL;
[a9763c6]64
[b7fd2a0]65static errno_t draw(void);
[a9763c6]66
67static void on_keyboard_event(widget_t *widget, void *data)
68{
69 kbd_event_t *event = (kbd_event_t *) data;
[a35b458]70
[a9763c6]71 if (event->type == KEY_PRESS) {
72 if (event->c == 'q')
73 exit(0);
[a35b458]74
[a9763c6]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;
[a35b458]80
[05b59393]81 if (event->key == KC_DOWN) {
[a9763c6]82 if (points <= increment) {
83 points = 1;
[1433ecda]84 } else {
85 points -= increment;
[a9763c6]86 }
87 }
[a35b458]88
[a9763c6]89 if (points < 1)
90 points = 1;
91 }
[a35b458]92
[a9763c6]93 if (event->c == 'm')
94 show_metrics = !show_metrics;
95 }
[a35b458]96
[b7fd2a0]97 errno_t rc = draw();
[a9763c6]98 if (rc != EOK) {
[c1694b6b]99 printf("Failed drawing: %s.\n", str_error(rc));
[a9763c6]100 exit(1);
101 }
102 update_canvas(canvas, surface);
103}
104
[b7fd2a0]105static errno_t create_font(font_t **font, uint16_t points)
[a9763c6]106{
[c6c39d4f]107 if (font_path == NULL) {
108 return embedded_font_create(font, points);
109 }
[a35b458]110
[c6c39d4f]111 return pcf_font_create(font, font_path, points);
[a9763c6]112}
113
114static 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
122static 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;
[a35b458]128
[a9763c6]129 drawctx_set_source(drawctx, source);
130 drawctx_transfer(drawctx, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
131}
132
133static 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
139static int text(drawctx_t *, font_t *, source_t *, surface_coord_t x,
[1433ecda]140 surface_coord_t, const char *, ...) _HELENOS_PRINTF_ATTRIBUTE(6, 7);
[a9763c6]141static 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);
[a35b458]149
[1569a9b]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 }
[a35b458]157
[a9763c6]158 return ret;
159}
160
161
[b7fd2a0]162static errno_t draw(void)
[a9763c6]163{
164 source_t background = rgb(255, 255, 255);
165 source_t foreground = rgb(0, 0, 0);
166 source_t glyphs = rgb(0, 0, 255);
167 source_t ascender_bg = rgb(255, 230, 128);
168 source_t ascender_fg = rgb(255, 153, 85);
169 source_t descender_bg = rgb(204, 255, 170);
170 source_t descender_fg = rgb(85, 212, 0);
171 source_t leading_bg = rgb(170, 238, 255);
172 source_t leading_fg = rgb(0, 170, 212);
[a35b458]173
[a9763c6]174 font_t *font;
[b7fd2a0]175 errno_t rc = create_font(&font, points);
[a9763c6]176 if (rc != EOK) {
177 printf("Failed creating font\n");
178 return rc;
179 }
[a35b458]180
[a9763c6]181 font_t *info_font;
182 rc = embedded_font_create(&info_font, 16);
183 if (rc != EOK) {
184 printf("Failed creating info font\n");
185 return rc;
186 }
[a35b458]187
[a9763c6]188 font_metrics_t font_metrics;
189 rc = font_get_metrics(font, &font_metrics);
190 if (rc != EOK)
191 return rc;
[a35b458]192
[a9763c6]193 surface_coord_t top = 50;
194 metric_t ascender_top = top;
195 metric_t descender_top = ascender_top + font_metrics.ascender;
196 metric_t leading_top = descender_top + font_metrics.descender;
197 metric_t line_bottom = leading_top + font_metrics.leading;
198
199 drawctx_t drawctx;
200 drawctx_init(&drawctx, surface);
[a35b458]201
[a9763c6]202 drawctx_set_source(&drawctx, &background);
203 drawctx_transfer(&drawctx, 0, 0,
204 width, height);
[a35b458]205
[a9763c6]206 if (show_metrics) {
207 horizontal_rectangle(&drawctx, 0, ascender_top, width,
208 descender_top - 1, &ascender_bg);
209 horizontal_line(&drawctx, ascender_top, 0, width,
210 &ascender_fg);
[a35b458]211
[a9763c6]212 horizontal_rectangle(&drawctx, 0, descender_top, width,
213 leading_top - 1, &descender_bg);
214 horizontal_line(&drawctx, descender_top, 0, width,
215 &descender_fg);
[a35b458]216
[a9763c6]217 horizontal_rectangle(&drawctx, 0, leading_top,
218 width, line_bottom - 1, &leading_bg);
219 horizontal_line(&drawctx, leading_top, 0, width,
220 &leading_fg);
221 }
[a35b458]222
[a9763c6]223 drawctx_set_source(&drawctx, &glyphs);
224 drawctx_set_font(&drawctx, font);
225 drawctx_print(&drawctx, "Čaj'_", 0, top);
[a35b458]226
[a9763c6]227 if (show_metrics) {
228 surface_coord_t infos_top = line_bottom + 10;
229 text(&drawctx, info_font, &ascender_fg, 0, infos_top,
230 "Ascender: %d", font_metrics.ascender);
231 text(&drawctx, info_font, &descender_fg, 0, infos_top + 16,
232 "Descender: %d", font_metrics.descender);
233 text(&drawctx, info_font, &foreground, 0, infos_top + 32,
234 "Line height: %d",
235 font_metrics.ascender + font_metrics.descender);
236 text(&drawctx, info_font, &leading_fg, 0, infos_top + 48,
237 "Leading: %d", font_metrics.leading);
238
239 }
[a35b458]240
[a9763c6]241 font_release(font);
242 return EOK;
243}
244
245int main(int argc, char *argv[])
246{
247 if (argc < 2) {
248 printf("Compositor server not specified.\n");
249 return 1;
250 }
[a35b458]251
[a9763c6]252 if (argc < 3) {
[c6c39d4f]253 font_path = NULL;
[1433ecda]254 } else {
[c6c39d4f]255 font_path = argv[2];
[a9763c6]256 }
[a35b458]257
[10cb47e]258 main_window = window_open(argv[1], NULL, WINDOW_MAIN, "fontviewer");
[a9763c6]259 if (!main_window) {
260 printf("Cannot open main window.\n");
261 return 2;
262 }
[a35b458]263
[a9763c6]264 surface = surface_create(WINDOW_WIDTH, WINDOW_HEIGHT, NULL,
265 SURFACE_FLAG_NONE);
266 if (surface == NULL) {
267 printf("Cannot create surface.\n");
268 return 2;
269 }
[a35b458]270
[a9763c6]271 width = WINDOW_WIDTH;
272 height = WINDOW_HEIGHT;
[a35b458]273
[b7fd2a0]274 errno_t rc = draw();
[a9763c6]275 if (rc != EOK) {
[c1694b6b]276 printf("Failed drawing: %s.\n", str_error(rc));
[a9763c6]277 return 2;
278 }
[a35b458]279
[10cb47e]280 canvas = create_canvas(window_root(main_window), NULL,
[a9763c6]281 WINDOW_WIDTH, WINDOW_HEIGHT, surface);
282 if (canvas == NULL) {
283 printf("Cannot create canvas.\n");
284 return 2;
285 }
286 sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event);
[a35b458]287
[a9763c6]288 window_resize(main_window, 200, 200, WINDOW_WIDTH, WINDOW_HEIGHT,
289 WINDOW_PLACEMENT_ABSOLUTE);
290 window_exec(main_window);
[a35b458]291
[a9763c6]292 task_retval(0);
293 async_manager();
[a35b458]294
[a9763c6]295 return 0;
296}
297
298/** @}
299 */
Note: See TracBrowser for help on using the repository browser.