[8fa65af0] | 1 | /*
|
---|
[d63623f] | 2 | * Copyright (c) 2021 Jiri Svoboda
|
---|
[8fa65af0] | 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 libgfxfont
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file Text rendering
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
[6a87f28] | 37 | #include <gfx/bitmap.h>
|
---|
[b433f68] | 38 | #include <gfx/color.h>
|
---|
[8fa65af0] | 39 | #include <gfx/font.h>
|
---|
| 40 | #include <gfx/glyph.h>
|
---|
[b433f68] | 41 | #include <gfx/render.h>
|
---|
[8fa65af0] | 42 | #include <gfx/text.h>
|
---|
[6a87f28] | 43 | #include <io/pixelmap.h>
|
---|
[8fa65af0] | 44 | #include <mem.h>
|
---|
[6a87f28] | 45 | #include <str.h>
|
---|
| 46 | #include "../private/font.h"
|
---|
| 47 | #include "../private/typeface.h"
|
---|
[8fa65af0] | 48 |
|
---|
| 49 | /** Initialize text formatting structure.
|
---|
| 50 | *
|
---|
| 51 | * Text formatting structure must always be initialized using this function
|
---|
| 52 | * first.
|
---|
| 53 | *
|
---|
| 54 | * @param fmt Text formatting structure
|
---|
| 55 | */
|
---|
| 56 | void gfx_text_fmt_init(gfx_text_fmt_t *fmt)
|
---|
| 57 | {
|
---|
| 58 | memset(fmt, 0, sizeof(gfx_text_fmt_t));
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /** Compute text width.
|
---|
| 62 | *
|
---|
| 63 | * @param font Font
|
---|
| 64 | * @param str String
|
---|
| 65 | * @return Text width
|
---|
| 66 | */
|
---|
| 67 | gfx_coord_t gfx_text_width(gfx_font_t *font, const char *str)
|
---|
| 68 | {
|
---|
| 69 | gfx_glyph_metrics_t gmetrics;
|
---|
| 70 | size_t stradv;
|
---|
| 71 | const char *cp;
|
---|
| 72 | gfx_glyph_t *glyph;
|
---|
| 73 | gfx_coord_t width;
|
---|
| 74 | errno_t rc;
|
---|
| 75 |
|
---|
[6a87f28] | 76 | if ((font->finfo->props.flags & gff_text_mode) != 0)
|
---|
| 77 | return str_width(str);
|
---|
| 78 |
|
---|
[8fa65af0] | 79 | width = 0;
|
---|
| 80 | cp = str;
|
---|
| 81 | while (*cp != '\0') {
|
---|
| 82 | rc = gfx_font_search_glyph(font, cp, &glyph, &stradv);
|
---|
| 83 | if (rc != EOK) {
|
---|
| 84 | ++cp;
|
---|
| 85 | continue;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | gfx_glyph_get_metrics(glyph, &gmetrics);
|
---|
| 89 |
|
---|
| 90 | cp += stradv;
|
---|
| 91 | width += gmetrics.advance;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | return width;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[6a87f28] | 97 | /** Print string using text characters in text mode.
|
---|
| 98 | *
|
---|
| 99 | * @param font Font
|
---|
| 100 | * @param pos Position of top-left corner of text
|
---|
[b433f68] | 101 | * @param color Text color
|
---|
[6a87f28] | 102 | * @param str String
|
---|
| 103 | * @return EOK on success or an error code
|
---|
| 104 | */
|
---|
| 105 | static errno_t gfx_puttext_textmode(gfx_font_t *font, gfx_coord2_t *pos,
|
---|
[b433f68] | 106 | gfx_color_t *color, const char *str)
|
---|
[6a87f28] | 107 | {
|
---|
| 108 | gfx_context_t *gc = font->typeface->gc;
|
---|
| 109 | gfx_bitmap_params_t params;
|
---|
| 110 | gfx_bitmap_t *bitmap;
|
---|
| 111 | gfx_bitmap_alloc_t alloc;
|
---|
[bc52b5b] | 112 | uint8_t attr;
|
---|
[6a87f28] | 113 | pixelmap_t pmap;
|
---|
| 114 | gfx_coord_t x;
|
---|
| 115 | pixel_t pixel;
|
---|
[bc52b5b] | 116 | char32_t c;
|
---|
| 117 | size_t off;
|
---|
[6a87f28] | 118 | errno_t rc;
|
---|
| 119 |
|
---|
| 120 | /*
|
---|
| 121 | * NOTE: Creating and destroying bitmap each time is not probably
|
---|
| 122 | * the most efficient way.
|
---|
| 123 | */
|
---|
| 124 |
|
---|
[bc52b5b] | 125 | gfx_color_get_ega(color, &attr);
|
---|
[b433f68] | 126 |
|
---|
[6a87f28] | 127 | gfx_bitmap_params_init(¶ms);
|
---|
| 128 | params.rect.p0.x = 0;
|
---|
| 129 | params.rect.p0.y = 0;
|
---|
| 130 | params.rect.p1.x = str_width(str);
|
---|
| 131 | params.rect.p1.y = 1;
|
---|
| 132 |
|
---|
[17fac946] | 133 | if (params.rect.p1.x == 0) {
|
---|
| 134 | /* Nothing to do. Avoid creating bitmap of zero width. */
|
---|
| 135 | return EOK;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
[6a87f28] | 138 | rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
|
---|
| 139 | if (rc != EOK)
|
---|
| 140 | return rc;
|
---|
| 141 |
|
---|
| 142 | rc = gfx_bitmap_get_alloc(bitmap, &alloc);
|
---|
| 143 | if (rc != EOK) {
|
---|
| 144 | gfx_bitmap_destroy(bitmap);
|
---|
| 145 | return rc;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | pmap.width = params.rect.p1.x;
|
---|
| 149 | pmap.height = 1;
|
---|
| 150 | pmap.data = alloc.pixels;
|
---|
| 151 |
|
---|
[bc52b5b] | 152 | off = 0;
|
---|
[6a87f28] | 153 | for (x = 0; x < params.rect.p1.x; x++) {
|
---|
[bc52b5b] | 154 | c = str_decode(str, &off, STR_NO_LIMIT);
|
---|
| 155 | pixel = PIXEL(attr,
|
---|
| 156 | (c >> 16) & 0xff,
|
---|
| 157 | (c >> 8) & 0xff,
|
---|
| 158 | c & 0xff);
|
---|
[6a87f28] | 159 | pixelmap_put_pixel(&pmap, x, 0, pixel);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | rc = gfx_bitmap_render(bitmap, NULL, pos);
|
---|
| 163 |
|
---|
| 164 | gfx_bitmap_destroy(bitmap);
|
---|
| 165 | return rc;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
[d63623f] | 168 | /** Get text starting position.
|
---|
[8fa65af0] | 169 | *
|
---|
| 170 | * @param font Font
|
---|
| 171 | * @param pos Anchor position
|
---|
| 172 | * @param fmt Text formatting
|
---|
| 173 | * @param str String
|
---|
[d63623f] | 174 | * @param spos Place to store starting position
|
---|
[8fa65af0] | 175 | */
|
---|
[d63623f] | 176 | void gfx_text_start_pos(gfx_font_t *font, gfx_coord2_t *pos,
|
---|
| 177 | gfx_text_fmt_t *fmt, const char *str, gfx_coord2_t *spos)
|
---|
[8fa65af0] | 178 | {
|
---|
| 179 | gfx_font_metrics_t fmetrics;
|
---|
| 180 | gfx_coord2_t cpos;
|
---|
| 181 | gfx_coord_t width;
|
---|
| 182 |
|
---|
[d63623f] | 183 | *spos = *pos;
|
---|
[8fa65af0] | 184 |
|
---|
| 185 | /* Adjust position for horizontal alignment */
|
---|
| 186 | if (fmt->halign != gfx_halign_left) {
|
---|
| 187 | width = gfx_text_width(font, str);
|
---|
| 188 | switch (fmt->halign) {
|
---|
| 189 | case gfx_halign_center:
|
---|
[d63623f] | 190 | spos->x -= width / 2;
|
---|
[8fa65af0] | 191 | break;
|
---|
| 192 | case gfx_halign_right:
|
---|
[400a16d] | 193 | spos->x -= width;
|
---|
[8fa65af0] | 194 | break;
|
---|
| 195 | default:
|
---|
| 196 | break;
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /* Adjust position for vertical alignment */
|
---|
[8bf9058] | 201 | gfx_font_get_metrics(font, &fmetrics);
|
---|
[8fa65af0] | 202 |
|
---|
[8bf9058] | 203 | if (fmt->valign != gfx_valign_baseline) {
|
---|
[8fa65af0] | 204 | switch (fmt->valign) {
|
---|
| 205 | case gfx_valign_top:
|
---|
[d63623f] | 206 | spos->y += fmetrics.ascent;
|
---|
[8fa65af0] | 207 | break;
|
---|
| 208 | case gfx_valign_center:
|
---|
[d63623f] | 209 | spos->y += fmetrics.ascent / 2;
|
---|
[8fa65af0] | 210 | break;
|
---|
[8bf9058] | 211 | case gfx_valign_bottom:
|
---|
[400a16d] | 212 | cpos.y -= fmetrics.descent + 1;
|
---|
[0d62c10] | 213 | break;
|
---|
[8fa65af0] | 214 | default:
|
---|
| 215 | break;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
[d63623f] | 218 | }
|
---|
| 219 |
|
---|
| 220 | /** Render text.
|
---|
| 221 | *
|
---|
| 222 | * @param font Font
|
---|
| 223 | * @param pos Anchor position
|
---|
| 224 | * @param fmt Text formatting
|
---|
| 225 | * @param str String
|
---|
| 226 | * @return EOK on success or an error code
|
---|
| 227 | */
|
---|
| 228 | errno_t gfx_puttext(gfx_font_t *font, gfx_coord2_t *pos,
|
---|
| 229 | gfx_text_fmt_t *fmt, const char *str)
|
---|
| 230 | {
|
---|
| 231 | gfx_glyph_metrics_t gmetrics;
|
---|
| 232 | size_t stradv;
|
---|
| 233 | const char *cp;
|
---|
| 234 | gfx_glyph_t *glyph;
|
---|
| 235 | gfx_coord2_t cpos;
|
---|
| 236 | errno_t rc;
|
---|
| 237 |
|
---|
| 238 | gfx_text_start_pos(font, pos, fmt, str, &cpos);
|
---|
[8fa65af0] | 239 |
|
---|
[6a87f28] | 240 | /* Text mode */
|
---|
| 241 | if ((font->finfo->props.flags & gff_text_mode) != 0)
|
---|
[b433f68] | 242 | return gfx_puttext_textmode(font, &cpos, fmt->color, str);
|
---|
| 243 |
|
---|
| 244 | rc = gfx_set_color(font->typeface->gc, fmt->color);
|
---|
| 245 | if (rc != EOK)
|
---|
| 246 | return rc;
|
---|
[6a87f28] | 247 |
|
---|
[8fa65af0] | 248 | cp = str;
|
---|
| 249 | while (*cp != '\0') {
|
---|
| 250 | rc = gfx_font_search_glyph(font, cp, &glyph, &stradv);
|
---|
| 251 | if (rc != EOK) {
|
---|
| 252 | ++cp;
|
---|
| 253 | continue;
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | gfx_glyph_get_metrics(glyph, &gmetrics);
|
---|
| 257 |
|
---|
| 258 | rc = gfx_glyph_render(glyph, &cpos);
|
---|
| 259 | if (rc != EOK)
|
---|
| 260 | return rc;
|
---|
| 261 |
|
---|
| 262 | cp += stradv;
|
---|
| 263 | cpos.x += gmetrics.advance;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | return EOK;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[d63623f] | 269 | /** Find character position in string by X coordinate.
|
---|
| 270 | *
|
---|
| 271 | * @param font Font
|
---|
| 272 | * @param pos Anchor position
|
---|
| 273 | * @param fmt Text formatting
|
---|
| 274 | * @param str String
|
---|
| 275 | * @param fpos Position for which we need to find offset
|
---|
| 276 | *
|
---|
| 277 | * @return Byte offset in @a str of character corresponding to position
|
---|
| 278 | * @a fpos. Note that the position is rounded, that is,
|
---|
| 279 | * if it is before the center of character A, it will return
|
---|
| 280 | * offset of A, if it is after the center of A, it will return
|
---|
| 281 | * offset of the following character.
|
---|
| 282 | */
|
---|
| 283 | size_t gfx_text_find_pos(gfx_font_t *font, gfx_coord2_t *pos,
|
---|
| 284 | gfx_text_fmt_t *fmt, const char *str, gfx_coord2_t *fpos)
|
---|
| 285 | {
|
---|
| 286 | gfx_glyph_metrics_t gmetrics;
|
---|
| 287 | size_t stradv;
|
---|
| 288 | const char *cp;
|
---|
| 289 | gfx_glyph_t *glyph;
|
---|
| 290 | gfx_coord2_t cpos;
|
---|
| 291 | size_t off;
|
---|
| 292 | size_t strsize;
|
---|
| 293 | errno_t rc;
|
---|
| 294 |
|
---|
| 295 | gfx_text_start_pos(font, pos, fmt, str, &cpos);
|
---|
| 296 |
|
---|
| 297 | /* Text mode */
|
---|
| 298 | if ((font->finfo->props.flags & gff_text_mode) != 0) {
|
---|
| 299 | off = 0;
|
---|
| 300 | strsize = str_size(str);
|
---|
| 301 | while (off < strsize) {
|
---|
| 302 | if (fpos->x <= cpos.x)
|
---|
| 303 | return off;
|
---|
| 304 | (void) str_decode(str, &off, strsize);
|
---|
| 305 | cpos.x++;
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | return off;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
| 311 | cp = str;
|
---|
| 312 | off = 0;
|
---|
| 313 | while (*cp != '\0') {
|
---|
| 314 | rc = gfx_font_search_glyph(font, cp, &glyph, &stradv);
|
---|
| 315 | if (rc != EOK) {
|
---|
| 316 | ++cp;
|
---|
| 317 | continue;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | gfx_glyph_get_metrics(glyph, &gmetrics);
|
---|
| 321 |
|
---|
| 322 | if (fpos->x < cpos.x + gmetrics.advance / 2)
|
---|
| 323 | return off;
|
---|
| 324 |
|
---|
| 325 | cp += stradv;
|
---|
| 326 | off += stradv;
|
---|
| 327 | cpos.x += gmetrics.advance;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | return off;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
[9eb8d12] | 333 | /** Get text continuation parameters.
|
---|
| 334 | *
|
---|
| 335 | * Return the anchor position and format needed to continue printing
|
---|
| 336 | * text after the specified string. It is allowed for the sources
|
---|
| 337 | * (@a pos, @a fmt) and destinations (@a cpos, @a cfmt) to point
|
---|
| 338 | * to the same objects, respectively.
|
---|
| 339 | *
|
---|
| 340 | * @param font Font
|
---|
| 341 | * @param pos Anchor position
|
---|
| 342 | * @param fmt Text formatting
|
---|
| 343 | * @param str String
|
---|
| 344 | * @param cpos Place to store anchor position for continuation
|
---|
| 345 | * @param cfmt Place to store format for continuation
|
---|
| 346 | */
|
---|
| 347 | void gfx_text_cont(gfx_font_t *font, gfx_coord2_t *pos,
|
---|
| 348 | gfx_text_fmt_t *fmt, const char *str, gfx_coord2_t *cpos,
|
---|
| 349 | gfx_text_fmt_t *cfmt)
|
---|
| 350 | {
|
---|
| 351 | gfx_coord2_t spos;
|
---|
| 352 | gfx_text_fmt_t tfmt;
|
---|
| 353 |
|
---|
| 354 | /* Continuation should start where the current string ends */
|
---|
| 355 | gfx_text_start_pos(font, pos, fmt, str, &spos);
|
---|
| 356 | cpos->x = spos.x + gfx_text_width(font, str);
|
---|
| 357 | cpos->y = spos.y;
|
---|
| 358 |
|
---|
| 359 | /*
|
---|
| 360 | * Formatting is the same, except the text should be aligned
|
---|
| 361 | * so that it starts at the anchor point.
|
---|
| 362 | */
|
---|
| 363 | tfmt = *fmt;
|
---|
| 364 | tfmt.halign = gfx_halign_left;
|
---|
| 365 | tfmt.valign = gfx_valign_baseline;
|
---|
| 366 |
|
---|
| 367 | *cfmt = tfmt;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | /** Get text bounding rectangle.
|
---|
| 371 | *
|
---|
| 372 | * @param font Font
|
---|
| 373 | * @param pos Anchor position
|
---|
| 374 | * @param fmt Text formatting
|
---|
| 375 | * @param str String
|
---|
| 376 | * @param rect Place to store bounding rectangle
|
---|
| 377 | */
|
---|
| 378 | void gfx_text_rect(gfx_font_t *font, gfx_coord2_t *pos,
|
---|
| 379 | gfx_text_fmt_t *fmt, const char *str, gfx_rect_t *rect)
|
---|
| 380 | {
|
---|
| 381 | gfx_coord2_t spos;
|
---|
| 382 |
|
---|
| 383 | gfx_text_start_pos(font, pos, fmt, str, &spos);
|
---|
| 384 |
|
---|
| 385 | rect->p0.x = spos.x;
|
---|
| 386 | rect->p0.y = spos.y - font->metrics.ascent;
|
---|
| 387 | rect->p1.x = spos.x + gfx_text_width(font, str);
|
---|
| 388 | rect->p1.y = spos.y + font->metrics.descent + 1;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[8fa65af0] | 391 | /** @}
|
---|
| 392 | */
|
---|