[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>
|
---|
[fd11144] | 40 | #include <str.h>
|
---|
[c1694b6b] | 41 | #include <str_error.h>
|
---|
[a9763c6] | 42 | #include <window.h>
|
---|
| 43 | #include <canvas.h>
|
---|
[2bb6d04] | 44 | #include <draw/surface.h>
|
---|
| 45 | #include <draw/codec.h>
|
---|
[a9763c6] | 46 | #include <task.h>
|
---|
[2bb6d04] | 47 | #include <draw/drawctx.h>
|
---|
| 48 | #include <draw/font.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 |
|
---|
| 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;
|
---|
[c6c39d4f] | 63 | char *font_path = NULL;
|
---|
[a9763c6] | 64 |
|
---|
[b7fd2a0] | 65 | static errno_t draw(void);
|
---|
[a9763c6] | 66 |
|
---|
| 67 | static 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] | 105 | static 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 |
|
---|
| 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;
|
---|
[a35b458] | 128 |
|
---|
[a9763c6] | 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,
|
---|
[1433ecda] | 140 | surface_coord_t, const char *, ...) _HELENOS_PRINTF_ATTRIBUTE(6, 7);
|
---|
[a9763c6] | 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);
|
---|
[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 |
|
---|
[b7fd2a0] | 161 | static errno_t draw(void)
|
---|
[a9763c6] | 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);
|
---|
[103db908] | 172 | font_t *info_font = NULL;
|
---|
| 173 | font_t *font = NULL;
|
---|
[a35b458] | 174 |
|
---|
[b7fd2a0] | 175 | errno_t rc = create_font(&font, points);
|
---|
[a9763c6] | 176 | if (rc != EOK) {
|
---|
| 177 | printf("Failed creating font\n");
|
---|
[103db908] | 178 | goto out_err;
|
---|
[a9763c6] | 179 | }
|
---|
[a35b458] | 180 |
|
---|
[a9763c6] | 181 | rc = embedded_font_create(&info_font, 16);
|
---|
| 182 | if (rc != EOK) {
|
---|
| 183 | printf("Failed creating info font\n");
|
---|
[103db908] | 184 | goto out_err;
|
---|
[a9763c6] | 185 | }
|
---|
[a35b458] | 186 |
|
---|
[a9763c6] | 187 | font_metrics_t font_metrics;
|
---|
| 188 | rc = font_get_metrics(font, &font_metrics);
|
---|
| 189 | if (rc != EOK)
|
---|
[103db908] | 190 | goto out_err;
|
---|
[a35b458] | 191 |
|
---|
[a9763c6] | 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);
|
---|
[a35b458] | 200 |
|
---|
[a9763c6] | 201 | drawctx_set_source(&drawctx, &background);
|
---|
| 202 | drawctx_transfer(&drawctx, 0, 0,
|
---|
| 203 | width, height);
|
---|
[a35b458] | 204 |
|
---|
[a9763c6] | 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);
|
---|
[a35b458] | 210 |
|
---|
[a9763c6] | 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);
|
---|
[a35b458] | 215 |
|
---|
[a9763c6] | 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 | }
|
---|
[a35b458] | 221 |
|
---|
[a9763c6] | 222 | drawctx_set_source(&drawctx, &glyphs);
|
---|
| 223 | drawctx_set_font(&drawctx, font);
|
---|
| 224 | drawctx_print(&drawctx, "Čaj'_", 0, top);
|
---|
[a35b458] | 225 |
|
---|
[a9763c6] | 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 | }
|
---|
[a35b458] | 239 |
|
---|
[103db908] | 240 | out_err:
|
---|
| 241 | if (font)
|
---|
| 242 | font_release(font);
|
---|
| 243 | if (info_font)
|
---|
| 244 | font_release(info_font);
|
---|
| 245 | return rc;
|
---|
[a9763c6] | 246 | }
|
---|
| 247 |
|
---|
[fd11144] | 248 | static void print_syntax(void)
|
---|
| 249 | {
|
---|
[37d4c91] | 250 | printf("Syntax: %s [-d <display>] [<font-file>]\n", NAME);
|
---|
[fd11144] | 251 | }
|
---|
| 252 |
|
---|
[a9763c6] | 253 | int main(int argc, char *argv[])
|
---|
| 254 | {
|
---|
[fd11144] | 255 | const char *display_svc = DISPLAY_DEFAULT;
|
---|
| 256 | int i;
|
---|
| 257 |
|
---|
| 258 | i = 1;
|
---|
[37d4c91] | 259 | while (i < argc && argv[i][0] == '-') {
|
---|
[fd11144] | 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 | }
|
---|
[a9763c6] | 274 | }
|
---|
[a35b458] | 275 |
|
---|
[fd11144] | 276 | if (i < argc) {
|
---|
| 277 | font_path = argv[i];
|
---|
[37d4c91] | 278 | } else {
|
---|
| 279 | font_path = NULL;
|
---|
[a9763c6] | 280 | }
|
---|
[a35b458] | 281 |
|
---|
[fd11144] | 282 | main_window = window_open(display_svc, NULL, WINDOW_MAIN, "fontviewer");
|
---|
[a9763c6] | 283 | if (!main_window) {
|
---|
| 284 | printf("Cannot open main window.\n");
|
---|
| 285 | return 2;
|
---|
| 286 | }
|
---|
[a35b458] | 287 |
|
---|
[a9763c6] | 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 | }
|
---|
[a35b458] | 294 |
|
---|
[a9763c6] | 295 | width = WINDOW_WIDTH;
|
---|
| 296 | height = WINDOW_HEIGHT;
|
---|
[a35b458] | 297 |
|
---|
[b7fd2a0] | 298 | errno_t rc = draw();
|
---|
[a9763c6] | 299 | if (rc != EOK) {
|
---|
[c1694b6b] | 300 | printf("Failed drawing: %s.\n", str_error(rc));
|
---|
[a9763c6] | 301 | return 2;
|
---|
| 302 | }
|
---|
[a35b458] | 303 |
|
---|
[10cb47e] | 304 | canvas = create_canvas(window_root(main_window), NULL,
|
---|
[a9763c6] | 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);
|
---|
[a35b458] | 311 |
|
---|
[a9763c6] | 312 | window_resize(main_window, 200, 200, WINDOW_WIDTH, WINDOW_HEIGHT,
|
---|
| 313 | WINDOW_PLACEMENT_ABSOLUTE);
|
---|
| 314 | window_exec(main_window);
|
---|
[a35b458] | 315 |
|
---|
[a9763c6] | 316 | task_retval(0);
|
---|
| 317 | async_manager();
|
---|
[a35b458] | 318 |
|
---|
[a9763c6] | 319 | return 0;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | /** @}
|
---|
| 323 | */
|
---|