| 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 |  | 
|---|
| 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; | 
|---|
| 63 | char *font_path = NULL; | 
|---|
| 64 |  | 
|---|
| 65 | static errno_t draw(void); | 
|---|
| 66 |  | 
|---|
| 67 | static 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 | } 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 | errno_t rc = draw(); | 
|---|
| 98 | if (rc != EOK) { | 
|---|
| 99 | printf("Failed drawing: %s.\n", str_error(rc)); | 
|---|
| 100 | exit(1); | 
|---|
| 101 | } | 
|---|
| 102 | update_canvas(canvas, surface); | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | static errno_t create_font(font_t **font, uint16_t points) | 
|---|
| 106 | { | 
|---|
| 107 | if (font_path == NULL) { | 
|---|
| 108 | return embedded_font_create(font, points); | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | return pcf_font_create(font, font_path, points); | 
|---|
| 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; | 
|---|
| 128 |  | 
|---|
| 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, | 
|---|
| 140 | surface_coord_t, const char *, ...) _HELENOS_PRINTF_ATTRIBUTE(6, 7); | 
|---|
| 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); | 
|---|
| 149 |  | 
|---|
| 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 | } | 
|---|
| 157 |  | 
|---|
| 158 | return ret; | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | static errno_t draw(void) | 
|---|
| 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); | 
|---|
| 172 |  | 
|---|
| 173 | font_t *font; | 
|---|
| 174 | errno_t rc = create_font(&font, points); | 
|---|
| 175 | if (rc != EOK) { | 
|---|
| 176 | printf("Failed creating font\n"); | 
|---|
| 177 | return rc; | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 | font_t *info_font; | 
|---|
| 181 | rc = embedded_font_create(&info_font, 16); | 
|---|
| 182 | if (rc != EOK) { | 
|---|
| 183 | printf("Failed creating info font\n"); | 
|---|
| 184 | return rc; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | font_metrics_t font_metrics; | 
|---|
| 188 | rc = font_get_metrics(font, &font_metrics); | 
|---|
| 189 | if (rc != EOK) | 
|---|
| 190 | return rc; | 
|---|
| 191 |  | 
|---|
| 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); | 
|---|
| 200 |  | 
|---|
| 201 | drawctx_set_source(&drawctx, &background); | 
|---|
| 202 | drawctx_transfer(&drawctx, 0, 0, | 
|---|
| 203 | width, height); | 
|---|
| 204 |  | 
|---|
| 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); | 
|---|
| 210 |  | 
|---|
| 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); | 
|---|
| 215 |  | 
|---|
| 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 | } | 
|---|
| 221 |  | 
|---|
| 222 | drawctx_set_source(&drawctx, &glyphs); | 
|---|
| 223 | drawctx_set_font(&drawctx, font); | 
|---|
| 224 | drawctx_print(&drawctx, "Čaj'_", 0, top); | 
|---|
| 225 |  | 
|---|
| 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 | } | 
|---|
| 239 |  | 
|---|
| 240 | font_release(font); | 
|---|
| 241 | return EOK; | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | int main(int argc, char *argv[]) | 
|---|
| 245 | { | 
|---|
| 246 | if (argc < 2) { | 
|---|
| 247 | printf("Compositor server not specified.\n"); | 
|---|
| 248 | return 1; | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | if (argc < 3) { | 
|---|
| 252 | font_path = NULL; | 
|---|
| 253 | } else { | 
|---|
| 254 | font_path = argv[2]; | 
|---|
| 255 | } | 
|---|
| 256 |  | 
|---|
| 257 | main_window = window_open(argv[1], NULL, WINDOW_MAIN, "fontviewer"); | 
|---|
| 258 | if (!main_window) { | 
|---|
| 259 | printf("Cannot open main window.\n"); | 
|---|
| 260 | return 2; | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | surface = surface_create(WINDOW_WIDTH, WINDOW_HEIGHT, NULL, | 
|---|
| 264 | SURFACE_FLAG_NONE); | 
|---|
| 265 | if (surface == NULL) { | 
|---|
| 266 | printf("Cannot create surface.\n"); | 
|---|
| 267 | return 2; | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 | width = WINDOW_WIDTH; | 
|---|
| 271 | height = WINDOW_HEIGHT; | 
|---|
| 272 |  | 
|---|
| 273 | errno_t rc = draw(); | 
|---|
| 274 | if (rc != EOK) { | 
|---|
| 275 | printf("Failed drawing: %s.\n", str_error(rc)); | 
|---|
| 276 | return 2; | 
|---|
| 277 | } | 
|---|
| 278 |  | 
|---|
| 279 | canvas = create_canvas(window_root(main_window), NULL, | 
|---|
| 280 | WINDOW_WIDTH, WINDOW_HEIGHT, surface); | 
|---|
| 281 | if (canvas == NULL) { | 
|---|
| 282 | printf("Cannot create canvas.\n"); | 
|---|
| 283 | return 2; | 
|---|
| 284 | } | 
|---|
| 285 | sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event); | 
|---|
| 286 |  | 
|---|
| 287 | window_resize(main_window, 200, 200, WINDOW_WIDTH, WINDOW_HEIGHT, | 
|---|
| 288 | WINDOW_PLACEMENT_ABSOLUTE); | 
|---|
| 289 | window_exec(main_window); | 
|---|
| 290 |  | 
|---|
| 291 | task_retval(0); | 
|---|
| 292 | async_manager(); | 
|---|
| 293 |  | 
|---|
| 294 | return 0; | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | /** @} | 
|---|
| 298 | */ | 
|---|