[6d5e378] | 1 | /*
|
---|
| 2 | * Copyright (c) 2012 Petr Koupy
|
---|
[2cc1ec0] | 3 | * Copyright (c) 2014 Martin Sucha
|
---|
[6d5e378] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup draw
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
| 35 | */
|
---|
| 36 |
|
---|
[2cc1ec0] | 37 | #include <errno.h>
|
---|
[38d150e] | 38 | #include <stdlib.h>
|
---|
[2cc1ec0] | 39 | #include <str.h>
|
---|
[6d5e378] | 40 |
|
---|
[2bb6d04] | 41 | #include <draw/font.h>
|
---|
| 42 | #include <draw/drawctx.h>
|
---|
[6d5e378] | 43 |
|
---|
[2cc1ec0] | 44 | font_t *font_create(font_backend_t *backend, void *backend_data)
|
---|
[6d5e378] | 45 | {
|
---|
[2cc1ec0] | 46 | font_t *font = malloc(sizeof(font_t));
|
---|
| 47 | if (font == NULL)
|
---|
| 48 | return NULL;
|
---|
[a35b458] | 49 |
|
---|
[2cc1ec0] | 50 | font->backend = backend;
|
---|
| 51 | font->backend_data = backend_data;
|
---|
[a35b458] | 52 |
|
---|
[2cc1ec0] | 53 | return font;
|
---|
[6d5e378] | 54 | }
|
---|
| 55 |
|
---|
| 56 | void font_release(font_t *font)
|
---|
| 57 | {
|
---|
[2cc1ec0] | 58 | font->backend->release(font->backend_data);
|
---|
[6d5e378] | 59 | }
|
---|
| 60 |
|
---|
[1433ecda] | 61 | errno_t font_get_metrics(font_t *font, font_metrics_t *metrics)
|
---|
| 62 | {
|
---|
[2cc1ec0] | 63 | return font->backend->get_font_metrics(font->backend_data, metrics);
|
---|
| 64 | }
|
---|
[6d5e378] | 65 |
|
---|
[28a5ebd] | 66 | errno_t font_resolve_glyph(font_t *font, char32_t c, glyph_id_t *glyph_id)
|
---|
[1433ecda] | 67 | {
|
---|
[2cc1ec0] | 68 | return font->backend->resolve_glyph(font->backend_data, c, glyph_id);
|
---|
| 69 | }
|
---|
[6d5e378] | 70 |
|
---|
[b7fd2a0] | 71 | errno_t font_get_glyph_metrics(font_t *font, glyph_id_t glyph_id,
|
---|
[2cc1ec0] | 72 | glyph_metrics_t *glyph_metrics)
|
---|
| 73 | {
|
---|
| 74 | return font->backend->get_glyph_metrics(font->backend_data,
|
---|
| 75 | glyph_id, glyph_metrics);
|
---|
| 76 | }
|
---|
[6d5e378] | 77 |
|
---|
[b7fd2a0] | 78 | errno_t font_render_glyph(font_t *font, drawctx_t *context, source_t *source,
|
---|
[2cc1ec0] | 79 | sysarg_t x, sysarg_t y, glyph_id_t glyph_id)
|
---|
| 80 | {
|
---|
| 81 | return font->backend->render_glyph(font->backend_data, context, source,
|
---|
| 82 | x, y, glyph_id);
|
---|
| 83 | }
|
---|
[6d5e378] | 84 |
|
---|
[2cc1ec0] | 85 | /* TODO this is bad interface */
|
---|
[b7fd2a0] | 86 | errno_t font_get_box(font_t *font, char *text, sysarg_t *width, sysarg_t *height)
|
---|
[2cc1ec0] | 87 | {
|
---|
| 88 | font_metrics_t fm;
|
---|
[b7fd2a0] | 89 | errno_t rc = font_get_metrics(font, &fm);
|
---|
[2cc1ec0] | 90 | if (rc != EOK)
|
---|
| 91 | return rc;
|
---|
| 92 |
|
---|
| 93 | native_t x = 0;
|
---|
| 94 |
|
---|
| 95 | size_t off = 0;
|
---|
| 96 | while (true) {
|
---|
[28a5ebd] | 97 | char32_t c = str_decode(text, &off, STR_NO_LIMIT);
|
---|
[2cc1ec0] | 98 | if (c == 0)
|
---|
| 99 | break;
|
---|
[a35b458] | 100 |
|
---|
[2cc1ec0] | 101 | glyph_id_t glyph_id;
|
---|
| 102 | rc = font_resolve_glyph(font, c, &glyph_id);
|
---|
| 103 | if (rc != EOK) {
|
---|
[b7fd2a0] | 104 | errno_t rc2 = font_resolve_glyph(font, U_SPECIAL, &glyph_id);
|
---|
[2cc1ec0] | 105 | if (rc2 != EOK) {
|
---|
| 106 | return rc;
|
---|
[6d5e378] | 107 | }
|
---|
| 108 | }
|
---|
[a35b458] | 109 |
|
---|
[2cc1ec0] | 110 | glyph_metrics_t glyph_metrics;
|
---|
| 111 | rc = font_get_glyph_metrics(font, glyph_id, &glyph_metrics);
|
---|
| 112 | if (rc != EOK)
|
---|
| 113 | return rc;
|
---|
[a35b458] | 114 |
|
---|
[2cc1ec0] | 115 | x += glyph_metrics_get_advancement(&glyph_metrics);
|
---|
[6d5e378] | 116 | }
|
---|
[2cc1ec0] | 117 |
|
---|
| 118 | *width = x;
|
---|
| 119 | *height = fm.ascender + fm.descender;
|
---|
| 120 | return EOK;
|
---|
[6d5e378] | 121 | }
|
---|
| 122 |
|
---|
[2cc1ec0] | 123 | /* TODO this is bad interface */
|
---|
[b7fd2a0] | 124 | errno_t font_draw_text(font_t *font, drawctx_t *context, source_t *source,
|
---|
[2cc1ec0] | 125 | const char *text, sysarg_t sx, sysarg_t sy)
|
---|
[6d5e378] | 126 | {
|
---|
| 127 | drawctx_save(context);
|
---|
| 128 | drawctx_set_compose(context, compose_over);
|
---|
| 129 |
|
---|
[2cc1ec0] | 130 | font_metrics_t fm;
|
---|
[b7fd2a0] | 131 | errno_t rc = font_get_metrics(font, &fm);
|
---|
[2cc1ec0] | 132 | if (rc != EOK)
|
---|
| 133 | return rc;
|
---|
| 134 |
|
---|
| 135 | native_t baseline = sy + fm.ascender;
|
---|
| 136 | native_t x = sx;
|
---|
| 137 |
|
---|
| 138 | size_t off = 0;
|
---|
| 139 | while (true) {
|
---|
[28a5ebd] | 140 | char32_t c = str_decode(text, &off, STR_NO_LIMIT);
|
---|
[2cc1ec0] | 141 | if (c == 0)
|
---|
| 142 | break;
|
---|
[a35b458] | 143 |
|
---|
[2cc1ec0] | 144 | glyph_id_t glyph_id;
|
---|
| 145 | rc = font_resolve_glyph(font, c, &glyph_id);
|
---|
| 146 | if (rc != EOK) {
|
---|
[b7fd2a0] | 147 | errno_t rc2 = font_resolve_glyph(font, U_SPECIAL, &glyph_id);
|
---|
[2cc1ec0] | 148 | if (rc2 != EOK) {
|
---|
| 149 | return rc;
|
---|
[6d5e378] | 150 | }
|
---|
[2cc1ec0] | 151 | }
|
---|
[a35b458] | 152 |
|
---|
[2cc1ec0] | 153 | glyph_metrics_t glyph_metrics;
|
---|
| 154 | rc = font_get_glyph_metrics(font, glyph_id, &glyph_metrics);
|
---|
| 155 | if (rc != EOK)
|
---|
| 156 | return rc;
|
---|
[6d5e378] | 157 |
|
---|
[2cc1ec0] | 158 | rc = font_render_glyph(font, context, source, x, baseline,
|
---|
| 159 | glyph_id);
|
---|
| 160 | if (rc != EOK)
|
---|
| 161 | return rc;
|
---|
[6d5e378] | 162 |
|
---|
[2cc1ec0] | 163 | x += glyph_metrics_get_advancement(&glyph_metrics);
|
---|
[6d5e378] | 164 |
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | drawctx_restore(context);
|
---|
| 168 | source_set_mask(source, NULL, false);
|
---|
[2cc1ec0] | 169 |
|
---|
| 170 | return EOK;
|
---|
[6d5e378] | 171 | }
|
---|
| 172 |
|
---|
| 173 | /** @}
|
---|
| 174 | */
|
---|