Changes in uspace/lib/gfxfont/src/text.c [a0aeb8f:17fac946] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/gfxfont/src/text.c
ra0aeb8f r17fac946 1 1 /* 2 * Copyright (c) 202 1Jiri Svoboda2 * Copyright (c) 2020 Jiri Svoboda 3 3 * All rights reserved. 4 4 * … … 110 110 gfx_bitmap_t *bitmap; 111 111 gfx_bitmap_alloc_t alloc; 112 uint 8_t attr;112 uint16_t r, g, b; 113 113 pixelmap_t pmap; 114 114 gfx_coord_t x; 115 115 pixel_t pixel; 116 char32_t c;117 size_t off;118 116 errno_t rc; 119 117 … … 123 121 */ 124 122 125 gfx_color_get_ega(color, &attr); 123 gfx_color_get_rgb_i16(color, &r, &g, &b); 124 125 /* 126 * We are setting the *background* color, the foreground color 127 * will be set to its complement. 128 */ 129 r = 0xff ^ (r >> 8); 130 g = 0xff ^ (g >> 8); 131 b = 0xff ^ (b >> 8); 126 132 127 133 gfx_bitmap_params_init(¶ms); … … 150 156 pmap.data = alloc.pixels; 151 157 152 off = 0;153 158 for (x = 0; x < params.rect.p1.x; x++) { 154 c = str_decode(str, &off, STR_NO_LIMIT); 155 pixel = PIXEL(attr, 156 (c >> 16) & 0xff, 157 (c >> 8) & 0xff, 158 c & 0xff); 159 pixel = PIXEL(str[x], r, g, b); 159 160 pixelmap_put_pixel(&pmap, x, 0, pixel); 160 161 } … … 166 167 } 167 168 168 /** Get text starting position.169 /** Render text. 169 170 * 170 171 * @param font Font … … 172 173 * @param fmt Text formatting 173 174 * @param str String 174 * @ param spos Place to store starting position175 */ 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)175 * @return EOK on success or an error code 176 */ 177 errno_t gfx_puttext(gfx_font_t *font, gfx_coord2_t *pos, 178 gfx_text_fmt_t *fmt, const char *str) 178 179 { 179 180 gfx_font_metrics_t fmetrics; 181 gfx_glyph_metrics_t gmetrics; 182 size_t stradv; 183 const char *cp; 184 gfx_glyph_t *glyph; 185 gfx_coord2_t cpos; 180 186 gfx_coord_t width; 181 182 *spos = *pos; 187 errno_t rc; 188 189 cpos = *pos; 183 190 184 191 /* Adjust position for horizontal alignment */ … … 187 194 switch (fmt->halign) { 188 195 case gfx_halign_center: 189 spos->x -= width / 2;196 cpos.x -= width / 2; 190 197 break; 191 198 case gfx_halign_right: 192 spos->x -= width;199 cpos.x -= width - 1; 193 200 break; 194 201 default: … … 203 210 switch (fmt->valign) { 204 211 case gfx_valign_top: 205 spos->y += fmetrics.ascent;212 cpos.y += fmetrics.ascent; 206 213 break; 207 214 case gfx_valign_center: 208 spos->y += fmetrics.ascent / 2;215 cpos.y += fmetrics.ascent / 2; 209 216 break; 210 217 case gfx_valign_bottom: 211 spos->y -= fmetrics.descent + 1;218 cpos.y -= fmetrics.descent; 212 219 break; 213 220 default: … … 215 222 } 216 223 } 217 }218 219 /** Render text.220 *221 * @param font Font222 * @param pos Anchor position223 * @param fmt Text formatting224 * @param str String225 * @return EOK on success or an error code226 */227 errno_t gfx_puttext(gfx_font_t *font, gfx_coord2_t *pos,228 gfx_text_fmt_t *fmt, const char *str)229 {230 gfx_glyph_metrics_t gmetrics;231 size_t stradv;232 const char *cp;233 gfx_glyph_t *glyph;234 gfx_coord2_t cpos;235 errno_t rc;236 237 gfx_text_start_pos(font, pos, fmt, str, &cpos);238 224 239 225 /* Text mode */ … … 266 252 } 267 253 268 /** Find character position in string by X coordinate.269 *270 * @param font Font271 * @param pos Anchor position272 * @param fmt Text formatting273 * @param str String274 * @param fpos Position for which we need to find offset275 *276 * @return Byte offset in @a str of character corresponding to position277 * @a fpos. Note that the position is rounded, that is,278 * if it is before the center of character A, it will return279 * offset of A, if it is after the center of A, it will return280 * offset of the following character.281 */282 size_t gfx_text_find_pos(gfx_font_t *font, gfx_coord2_t *pos,283 gfx_text_fmt_t *fmt, const char *str, gfx_coord2_t *fpos)284 {285 gfx_glyph_metrics_t gmetrics;286 size_t stradv;287 const char *cp;288 gfx_glyph_t *glyph;289 gfx_coord2_t cpos;290 size_t off;291 size_t strsize;292 errno_t rc;293 294 gfx_text_start_pos(font, pos, fmt, str, &cpos);295 296 /* Text mode */297 if ((font->finfo->props.flags & gff_text_mode) != 0) {298 off = 0;299 strsize = str_size(str);300 while (off < strsize) {301 if (fpos->x <= cpos.x)302 return off;303 (void) str_decode(str, &off, strsize);304 cpos.x++;305 }306 307 return off;308 }309 310 cp = str;311 off = 0;312 while (*cp != '\0') {313 rc = gfx_font_search_glyph(font, cp, &glyph, &stradv);314 if (rc != EOK) {315 ++cp;316 continue;317 }318 319 gfx_glyph_get_metrics(glyph, &gmetrics);320 321 if (fpos->x < cpos.x + gmetrics.advance / 2)322 return off;323 324 cp += stradv;325 off += stradv;326 cpos.x += gmetrics.advance;327 }328 329 return off;330 }331 332 /** Get text continuation parameters.333 *334 * Return the anchor position and format needed to continue printing335 * text after the specified string. It is allowed for the sources336 * (@a pos, @a fmt) and destinations (@a cpos, @a cfmt) to point337 * to the same objects, respectively.338 *339 * @param font Font340 * @param pos Anchor position341 * @param fmt Text formatting342 * @param str String343 * @param cpos Place to store anchor position for continuation344 * @param cfmt Place to store format for continuation345 */346 void gfx_text_cont(gfx_font_t *font, gfx_coord2_t *pos,347 gfx_text_fmt_t *fmt, const char *str, gfx_coord2_t *cpos,348 gfx_text_fmt_t *cfmt)349 {350 gfx_coord2_t spos;351 gfx_text_fmt_t tfmt;352 353 /* Continuation should start where the current string ends */354 gfx_text_start_pos(font, pos, fmt, str, &spos);355 cpos->x = spos.x + gfx_text_width(font, str);356 cpos->y = spos.y;357 358 /*359 * Formatting is the same, except the text should be aligned360 * so that it starts at the anchor point.361 */362 tfmt = *fmt;363 tfmt.halign = gfx_halign_left;364 tfmt.valign = gfx_valign_baseline;365 366 *cfmt = tfmt;367 }368 369 /** Get text bounding rectangle.370 *371 * @param font Font372 * @param pos Anchor position373 * @param fmt Text formatting374 * @param str String375 * @param rect Place to store bounding rectangle376 */377 void gfx_text_rect(gfx_font_t *font, gfx_coord2_t *pos,378 gfx_text_fmt_t *fmt, const char *str, gfx_rect_t *rect)379 {380 gfx_coord2_t spos;381 382 gfx_text_start_pos(font, pos, fmt, str, &spos);383 384 rect->p0.x = spos.x;385 rect->p0.y = spos.y - font->metrics.ascent;386 rect->p1.x = spos.x + gfx_text_width(font, str);387 rect->p1.y = spos.y + font->metrics.descent + 1;388 }389 390 254 /** @} 391 255 */
Note:
See TracChangeset
for help on using the changeset viewer.