source: mainline/uspace/app/fontviewer/fontviewer.c@ 948222e4

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

style: Remove trailing whitespace on _all_ lines, including empty ones, for particular file types.

Command used: tools/srepl '\s\+$' '' -- *.c *.h *.py *.sh *.s *.S *.ag

Currently, whitespace on empty lines is very inconsistent.
There are two basic choices: Either remove the whitespace, or keep empty lines
indented to the level of surrounding code. The former is AFAICT more common,
and also much easier to do automatically.

Alternatively, we could write script for automatic indentation, and use that
instead. However, if such a script exists, it's possible to use the indented
style locally, by having the editor apply relevant conversions on load/save,
without affecting remote repository. IMO, it makes more sense to adopt
the simpler rule.

  • 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 errno_t 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 errno_t 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 errno_t 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 *, ...) _HELENOS_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 drawctx_set_source(drawctx, source);
153 drawctx_set_font(drawctx, font);
154 drawctx_print(drawctx, str, x, y);
155
156 free(str);
157 }
158
159 return ret;
160}
161
162
163static errno_t 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 errno_t 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
246int 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 errno_t rc = draw();
277 if (rc != EOK) {
278 printf("Failed drawing: %s.\n", str_error(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 */
Note: See TracBrowser for help on using the repository browser.