| 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 <font/pcf.h> | 
|---|
| 50 | #include <stdarg.h> | 
|---|
| 51 | #include <io/verify.h> | 
|---|
| 52 |  | 
|---|
| 53 | #define NAME  "fontviewer" | 
|---|
| 54 |  | 
|---|
| 55 | #define WINDOW_WIDTH   640 | 
|---|
| 56 | #define WINDOW_HEIGHT  480 | 
|---|
| 57 |  | 
|---|
| 58 | static window_t *main_window; | 
|---|
| 59 | static surface_t *surface = NULL; | 
|---|
| 60 | static canvas_t *canvas = NULL; | 
|---|
| 61 | static surface_coord_t width, height; | 
|---|
| 62 | uint16_t points = 16; | 
|---|
| 63 | bool show_metrics = true; | 
|---|
| 64 | char *font_path = NULL; | 
|---|
| 65 |  | 
|---|
| 66 | static int draw(void); | 
|---|
| 67 |  | 
|---|
| 68 | static void on_keyboard_event(widget_t *widget, void *data) | 
|---|
| 69 | { | 
|---|
| 70 | kbd_event_t *event = (kbd_event_t *) data; | 
|---|
| 71 |  | 
|---|
| 72 | if (event->type == KEY_PRESS) { | 
|---|
| 73 | if (event->c == 'q') | 
|---|
| 74 | exit(0); | 
|---|
| 75 |  | 
|---|
| 76 | if (event->key == KC_UP || event->key == KC_DOWN) { | 
|---|
| 77 | uint16_t increment = (event->mods & KM_SHIFT) ? 10 : 1; | 
|---|
| 78 |  | 
|---|
| 79 | if (event->key == KC_UP) | 
|---|
| 80 | points += increment; | 
|---|
| 81 |  | 
|---|
| 82 | if ((event->key == KC_DOWN)) { | 
|---|
| 83 | if (points <= increment) { | 
|---|
| 84 | points = 1; | 
|---|
| 85 | } | 
|---|
| 86 | else { | 
|---|
| 87 | points-= increment; | 
|---|
| 88 | } | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | if (points < 1) | 
|---|
| 92 | points = 1; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | if (event->c == 'm') | 
|---|
| 96 | show_metrics = !show_metrics; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | int rc = draw(); | 
|---|
| 100 | if (rc != EOK) { | 
|---|
| 101 | printf("Failed drawing: %d.\n", rc); | 
|---|
| 102 | exit(1); | 
|---|
| 103 | } | 
|---|
| 104 | update_canvas(canvas, surface); | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | static int create_font(font_t **font, uint16_t points) | 
|---|
| 108 | { | 
|---|
| 109 | if (font_path == NULL) { | 
|---|
| 110 | return embedded_font_create(font, points); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | return pcf_font_create(font, font_path, points); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | static source_t rgb(uint8_t r, uint8_t g, uint8_t b) | 
|---|
| 117 | { | 
|---|
| 118 | source_t source; | 
|---|
| 119 | source_init(&source); | 
|---|
| 120 | source_set_color(&source, PIXEL(255, r, g, b)); | 
|---|
| 121 | return source; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | static void horizontal_rectangle(drawctx_t *drawctx, surface_coord_t x1, | 
|---|
| 125 | surface_coord_t y1, surface_coord_t x2, surface_coord_t y2, | 
|---|
| 126 | source_t *source) | 
|---|
| 127 | { | 
|---|
| 128 | if (y2 < y1) | 
|---|
| 129 | return; | 
|---|
| 130 |  | 
|---|
| 131 | drawctx_set_source(drawctx, source); | 
|---|
| 132 | drawctx_transfer(drawctx, x1, y1, x2 - x1 + 1, y2 - y1 + 1); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | static void horizontal_line(drawctx_t *drawctx, surface_coord_t y, | 
|---|
| 136 | surface_coord_t x1, surface_coord_t x2, source_t *source) | 
|---|
| 137 | { | 
|---|
| 138 | horizontal_rectangle(drawctx, x1, y, x2, y, source); | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | static int text(drawctx_t *, font_t *, source_t *, surface_coord_t x, | 
|---|
| 142 | surface_coord_t , const char *, ...) PRINTF_ATTRIBUTE(6, 7); | 
|---|
| 143 | static int text(drawctx_t *drawctx, font_t *font, source_t *source, | 
|---|
| 144 | surface_coord_t x, surface_coord_t y, const char *fmt, ...) | 
|---|
| 145 | { | 
|---|
| 146 | char *str = NULL; | 
|---|
| 147 | va_list args; | 
|---|
| 148 | va_start(args, fmt); | 
|---|
| 149 | int ret = vasprintf(&str, fmt, args); | 
|---|
| 150 | va_end(args); | 
|---|
| 151 |  | 
|---|
| 152 | if (ret <= 0) | 
|---|
| 153 | return ret; | 
|---|
| 154 |  | 
|---|
| 155 | drawctx_set_source(drawctx, source); | 
|---|
| 156 | drawctx_set_font(drawctx, font); | 
|---|
| 157 | drawctx_print(drawctx, str, x, y); | 
|---|
| 158 |  | 
|---|
| 159 | free(str); | 
|---|
| 160 |  | 
|---|
| 161 | return ret; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 |  | 
|---|
| 165 | static int draw(void) | 
|---|
| 166 | { | 
|---|
| 167 | source_t background = rgb(255, 255, 255); | 
|---|
| 168 | source_t foreground = rgb(0, 0, 0); | 
|---|
| 169 | source_t glyphs = rgb(0, 0, 255); | 
|---|
| 170 | source_t ascender_bg = rgb(255, 230, 128); | 
|---|
| 171 | source_t ascender_fg = rgb(255, 153, 85); | 
|---|
| 172 | source_t descender_bg = rgb(204, 255, 170); | 
|---|
| 173 | source_t descender_fg = rgb(85, 212, 0); | 
|---|
| 174 | source_t leading_bg = rgb(170, 238, 255); | 
|---|
| 175 | source_t leading_fg = rgb(0, 170, 212); | 
|---|
| 176 |  | 
|---|
| 177 | font_t *font; | 
|---|
| 178 | int rc = create_font(&font, points); | 
|---|
| 179 | if (rc != EOK) { | 
|---|
| 180 | printf("Failed creating font\n"); | 
|---|
| 181 | return rc; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | font_t *info_font; | 
|---|
| 185 | rc = embedded_font_create(&info_font, 16); | 
|---|
| 186 | if (rc != EOK) { | 
|---|
| 187 | printf("Failed creating info font\n"); | 
|---|
| 188 | return rc; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 | font_metrics_t font_metrics; | 
|---|
| 192 | rc = font_get_metrics(font, &font_metrics); | 
|---|
| 193 | if (rc != EOK) | 
|---|
| 194 | return rc; | 
|---|
| 195 |  | 
|---|
| 196 | surface_coord_t top = 50; | 
|---|
| 197 | metric_t ascender_top = top; | 
|---|
| 198 | metric_t descender_top = ascender_top + font_metrics.ascender; | 
|---|
| 199 | metric_t leading_top = descender_top + font_metrics.descender; | 
|---|
| 200 | metric_t line_bottom = leading_top + font_metrics.leading; | 
|---|
| 201 |  | 
|---|
| 202 | drawctx_t drawctx; | 
|---|
| 203 | drawctx_init(&drawctx, surface); | 
|---|
| 204 |  | 
|---|
| 205 | drawctx_set_source(&drawctx, &background); | 
|---|
| 206 | drawctx_transfer(&drawctx, 0, 0, | 
|---|
| 207 | width, height); | 
|---|
| 208 |  | 
|---|
| 209 | if (show_metrics) { | 
|---|
| 210 | horizontal_rectangle(&drawctx, 0, ascender_top, width, | 
|---|
| 211 | descender_top - 1, &ascender_bg); | 
|---|
| 212 | horizontal_line(&drawctx, ascender_top, 0, width, | 
|---|
| 213 | &ascender_fg); | 
|---|
| 214 |  | 
|---|
| 215 | horizontal_rectangle(&drawctx, 0, descender_top, width, | 
|---|
| 216 | leading_top - 1, &descender_bg); | 
|---|
| 217 | horizontal_line(&drawctx, descender_top, 0, width, | 
|---|
| 218 | &descender_fg); | 
|---|
| 219 |  | 
|---|
| 220 | horizontal_rectangle(&drawctx, 0, leading_top, | 
|---|
| 221 | width, line_bottom - 1, &leading_bg); | 
|---|
| 222 | horizontal_line(&drawctx, leading_top, 0, width, | 
|---|
| 223 | &leading_fg); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | drawctx_set_source(&drawctx, &glyphs); | 
|---|
| 227 | drawctx_set_font(&drawctx, font); | 
|---|
| 228 | drawctx_print(&drawctx, "Čaj'_", 0, top); | 
|---|
| 229 |  | 
|---|
| 230 | if (show_metrics) { | 
|---|
| 231 | surface_coord_t infos_top = line_bottom + 10; | 
|---|
| 232 | text(&drawctx, info_font, &ascender_fg, 0, infos_top, | 
|---|
| 233 | "Ascender: %d", font_metrics.ascender); | 
|---|
| 234 | text(&drawctx, info_font, &descender_fg, 0, infos_top + 16, | 
|---|
| 235 | "Descender: %d", font_metrics.descender); | 
|---|
| 236 | text(&drawctx, info_font, &foreground, 0, infos_top + 32, | 
|---|
| 237 | "Line height: %d", | 
|---|
| 238 | font_metrics.ascender + font_metrics.descender); | 
|---|
| 239 | text(&drawctx, info_font, &leading_fg, 0, infos_top + 48, | 
|---|
| 240 | "Leading: %d", font_metrics.leading); | 
|---|
| 241 |  | 
|---|
| 242 | } | 
|---|
| 243 |  | 
|---|
| 244 | font_release(font); | 
|---|
| 245 | return EOK; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | int main(int argc, char *argv[]) | 
|---|
| 249 | { | 
|---|
| 250 | if (argc < 2) { | 
|---|
| 251 | printf("Compositor server not specified.\n"); | 
|---|
| 252 | return 1; | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | if (argc < 3) { | 
|---|
| 256 | font_path = NULL; | 
|---|
| 257 | } | 
|---|
| 258 | else { | 
|---|
| 259 | font_path = argv[2]; | 
|---|
| 260 | } | 
|---|
| 261 |  | 
|---|
| 262 | main_window = window_open(argv[1], WINDOW_MAIN, "fontviewer"); | 
|---|
| 263 | if (!main_window) { | 
|---|
| 264 | printf("Cannot open main window.\n"); | 
|---|
| 265 | return 2; | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | surface = surface_create(WINDOW_WIDTH, WINDOW_HEIGHT, NULL, | 
|---|
| 269 | SURFACE_FLAG_NONE); | 
|---|
| 270 | if (surface == NULL) { | 
|---|
| 271 | printf("Cannot create surface.\n"); | 
|---|
| 272 | return 2; | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | width = WINDOW_WIDTH; | 
|---|
| 276 | height = WINDOW_HEIGHT; | 
|---|
| 277 |  | 
|---|
| 278 | int rc = draw(); | 
|---|
| 279 | if (rc != EOK) { | 
|---|
| 280 | printf("Failed drawing: %d.\n", rc); | 
|---|
| 281 | return 2; | 
|---|
| 282 | } | 
|---|
| 283 |  | 
|---|
| 284 | canvas = create_canvas(window_root(main_window), | 
|---|
| 285 | WINDOW_WIDTH, WINDOW_HEIGHT, surface); | 
|---|
| 286 | if (canvas == NULL) { | 
|---|
| 287 | printf("Cannot create canvas.\n"); | 
|---|
| 288 | return 2; | 
|---|
| 289 | } | 
|---|
| 290 | sig_connect(&canvas->keyboard_event, NULL, on_keyboard_event); | 
|---|
| 291 |  | 
|---|
| 292 | window_resize(main_window, 200, 200, WINDOW_WIDTH, WINDOW_HEIGHT, | 
|---|
| 293 | WINDOW_PLACEMENT_ABSOLUTE); | 
|---|
| 294 | window_exec(main_window); | 
|---|
| 295 |  | 
|---|
| 296 | task_retval(0); | 
|---|
| 297 | async_manager(); | 
|---|
| 298 |  | 
|---|
| 299 | return 0; | 
|---|
| 300 | } | 
|---|
| 301 |  | 
|---|
| 302 | /** @} | 
|---|
| 303 | */ | 
|---|