Changeset 8fa65af0 in mainline


Ignore:
Timestamp:
2020-10-06T08:59:57Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
74f59b7
Parents:
25f2983b
git-author:
Jiri Svoboda <jiri@…> (2020-10-05 18:59:36)
git-committer:
Jiri Svoboda <jiri@…> (2020-10-06 08:59:57)
Message:

Add text rendering routine and demo

Location:
uspace
Files:
4 added
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/fontedit/fontedit.c

    r25f2983b r8fa65af0  
    4141#include <gfx/glyph.h>
    4242#include <gfx/render.h>
     43#include <gfx/text.h>
    4344#include <gfx/typeface.h>
    4445#include <stdbool.h>
     
    422423    gfx_coord_t x, gfx_coord_t y, const char *str)
    423424{
    424         gfx_glyph_metrics_t gmetrics;
    425         size_t stradv;
    426         const char *cp;
     425        gfx_text_fmt_t fmt;
    427426        gfx_coord2_t pos;
    428         gfx_glyph_t *glyph;
    429         errno_t rc;
     427
     428        gfx_text_fmt_init(&fmt);
    430429
    431430        pos.x = x;
    432431        pos.y = y;
    433         cp = str;
    434 
    435         while (*cp != '\0') {
    436                 rc = gfx_font_search_glyph(fedit->font, cp, &glyph, &stradv);
    437                 if (rc != EOK) {
    438                         ++cp;
    439                         continue;
    440                 }
    441 
    442                 gfx_glyph_get_metrics(glyph, &gmetrics);
    443 
    444                 rc = gfx_glyph_render(glyph, &pos);
    445                 if (rc != EOK)
    446                         return rc;
    447 
    448                 cp += stradv;
    449                 pos.x += gmetrics.advance;
    450         }
    451 
    452         return EOK;
     432
     433        return gfx_puttext(fedit->font, &pos, &fmt, str);
    453434}
    454435
  • uspace/app/gfxdemo/gfxdemo.c

    r25f2983b r8fa65af0  
    4242#include <gfx/color.h>
    4343#include <gfx/render.h>
     44#include <gfx/font.h>
     45#include <gfx/text.h>
     46#include <gfx/typeface.h>
    4447#include <io/console.h>
    4548#include <io/pixelmap.h>
     
    366369        return rc;
    367370}
     371
    368372/** Run bitmap color key demo on a graphic context.
    369373 *
     
    424428}
    425429
    426 /** Run demo loop on a graphic context.
     430/** Run text demo on a graphic context.
    427431 *
    428432 * @param gc Graphic context
     
    430434 * @param h Height
    431435 */
     436static errno_t demo_text(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
     437{
     438        gfx_color_t *color = NULL;
     439        gfx_rect_t rect;
     440        gfx_typeface_t *tface = NULL;
     441        gfx_font_info_t *finfo;
     442        gfx_font_t *font = NULL;
     443        gfx_coord2_t pos;
     444        gfx_text_fmt_t fmt;
     445        int i;
     446        errno_t rc;
     447
     448        rc = gfx_typeface_open(gc, "/data/font/helena.tpf", &tface);
     449        if (rc != EOK) {
     450                printf("Error opening typeface\n");
     451                goto error;
     452        }
     453
     454        finfo = gfx_typeface_first_font(tface);
     455        if (finfo == NULL) {
     456                printf("Typeface contains no font.\n");
     457                rc = ENOENT;
     458                goto error;
     459        }
     460
     461        rc = gfx_font_open(finfo, &font);
     462        if (rc != EOK) {
     463                printf("Error opening font.\n");
     464                goto error;
     465        }
     466
     467        rc = clear_scr(gc, w, h);
     468        if (rc != EOK)
     469                goto error;
     470
     471        /* Vertical bars */
     472
     473        for (i = 0; i < 20; i++) {
     474                rc = gfx_color_new_rgb_i16(0, 0x8000 * i / 20,
     475                    0x8000 * i / 20, &color);
     476                if (rc != EOK)
     477                        goto error;
     478
     479                rc = gfx_set_color(gc, color);
     480                if (rc != EOK)
     481                        goto error;
     482
     483                rect.p0.x = w * i / 20;
     484                rect.p0.y = 0;
     485                rect.p1.x = w * (i + 1) / 20;
     486                rect.p1.y = h;
     487
     488                rc = gfx_fill_rect(gc, &rect);
     489                if (rc != EOK)
     490                        goto error;
     491
     492                gfx_color_delete(color);
     493        }
     494
     495        rc = gfx_color_new_rgb_i16(0, 0, 0x8000, &color);
     496        if (rc != EOK)
     497                goto error;
     498
     499        rc = gfx_set_color(gc, color);
     500        if (rc != EOK)
     501                goto error;
     502
     503        rect.p0.x = w / 20;
     504        rect.p0.y = 3 * h / 15;
     505        rect.p1.x = w - w / 20;
     506        rect.p1.y = 5 * h / 15;
     507
     508        rc = gfx_fill_rect(gc, &rect);
     509        if (rc != EOK)
     510                goto error;
     511
     512        gfx_color_delete(color);
     513
     514        rc = gfx_color_new_rgb_i16(0xffff, 0xffff, 0xffff, &color);
     515        if (rc != EOK)
     516                goto error;
     517
     518        rc = gfx_set_color(gc, color);
     519        if (rc != EOK)
     520                goto error;
     521
     522        gfx_text_fmt_init(&fmt);
     523
     524        pos.x = rect.p0.x;
     525        pos.y = rect.p0.y;
     526        rc = gfx_puttext(font, &pos, &fmt, "Top left");
     527        if (rc != EOK) {
     528                printf("Error rendering text.\n");
     529                goto error;
     530        }
     531
     532        pos.x = (rect.p0.x + rect.p1.x) / 2;
     533        pos.y = rect.p0.y;
     534        fmt.halign = gfx_halign_center;
     535        rc = gfx_puttext(font, &pos, &fmt, "Top center");
     536        if (rc != EOK)
     537                goto error;
     538
     539        pos.x = rect.p1.x;
     540        pos.y = rect.p0.y;
     541        fmt.halign = gfx_halign_right;
     542        rc = gfx_puttext(font, &pos, &fmt, "Top right");
     543        if (rc != EOK)
     544                goto error;
     545
     546        fmt.valign = gfx_valign_center;
     547
     548        pos.x = rect.p0.x;
     549        pos.y = (rect.p0.y + rect.p1.y) / 2;
     550        fmt.halign = gfx_halign_left;
     551        rc = gfx_puttext(font, &pos, &fmt, "Center left");
     552        if (rc != EOK)
     553                goto error;
     554
     555        pos.x = (rect.p0.x + rect.p1.x) / 2;
     556        pos.y = (rect.p0.y + rect.p1.y) / 2;
     557        fmt.halign = gfx_halign_center;
     558        rc = gfx_puttext(font, &pos, &fmt, "Center");
     559        if (rc != EOK)
     560                goto error;
     561
     562        pos.x = rect.p1.x;
     563        pos.y = (rect.p0.y + rect.p1.y) / 2;
     564        fmt.halign = gfx_halign_right;
     565        rc = gfx_puttext(font, &pos, &fmt, "Center right");
     566        if (rc != EOK)
     567                goto error;
     568
     569        fmt.valign = gfx_valign_bottom;
     570
     571        pos.x = rect.p0.x;
     572        pos.y = rect.p1.y;
     573        fmt.halign = gfx_halign_left;
     574        rc = gfx_puttext(font, &pos, &fmt, "Bottom left");
     575        if (rc != EOK)
     576                goto error;
     577
     578        pos.x = (rect.p0.x + rect.p1.x) / 2;
     579        pos.y = rect.p1.y;
     580        fmt.halign = gfx_halign_center;
     581        rc = gfx_puttext(font, &pos, &fmt, "Bottom center");
     582        if (rc != EOK)
     583                goto error;
     584
     585        pos.x = rect.p1.x;
     586        pos.y = rect.p1.y;
     587        fmt.halign = gfx_halign_right;
     588        rc = gfx_puttext(font, &pos, &fmt, "Bottom right");
     589        if (rc != EOK)
     590                goto error;
     591
     592        gfx_text_fmt_init(&fmt);
     593
     594        for (i = 0; i < 8; i++) {
     595                pos.x = w / 20;
     596                pos.y = (7 + i) * h / 15;
     597                rc = gfx_puttext(font, &pos, &fmt, "The quick brown fox jumps over the lazy dog.");
     598                if (rc != EOK)
     599                        goto error;
     600        }
     601
     602        for (i = 0; i < 10; i++) {
     603                fibril_usleep(500 * 1000);
     604                if (quit)
     605                        break;
     606        }
     607
     608        gfx_color_delete(color);
     609
     610        gfx_font_close(font);
     611        gfx_typeface_destroy(tface);
     612        return EOK;
     613error:
     614        if (font != NULL)
     615                gfx_font_close(font);
     616        if (tface != NULL)
     617                gfx_typeface_destroy(tface);
     618        return rc;
     619}
     620
     621/** Run demo loop on a graphic context.
     622 *
     623 * @param gc Graphic context
     624 * @param w Width
     625 * @param h Height
     626 */
    432627static errno_t demo_loop(gfx_context_t *gc, gfx_coord_t w, gfx_coord_t h)
    433628{
     
    448643
    449644                rc = demo_bitmap_kc(gc, w, h);
     645                if (rc != EOK)
     646                        return rc;
     647
     648                rc = demo_text(gc, w, h);
    450649                if (rc != EOK)
    451650                        return rc;
  • uspace/app/gfxdemo/meson.build

    r25f2983b r8fa65af0  
    2727#
    2828
    29 deps = [ 'gfx', 'guigfx', 'congfx', 'ipcgfx', 'display' ]
     29deps = [ 'gfx', 'gfxfont', 'guigfx', 'congfx', 'ipcgfx', 'display' ]
    3030src = files(
    3131        'gfxdemo.c',
  • uspace/lib/gfxfont/include/gfx/font.h

    r25f2983b r8fa65af0  
    4242#include <types/gfx/font.h>
    4343#include <types/gfx/glyph.h>
     44#include <types/gfx/text.h>
    4445#include <types/gfx/typeface.h>
    4546
  • uspace/lib/gfxfont/meson.build

    r25f2983b r8fa65af0  
    3232        'src/glyph.c',
    3333        'src/glyph_bmp.c',
     34        'src/text.c',
    3435        'src/typeface.c',
    3536)
     
    4041        'test/glyph_bmp.c',
    4142        'test/main.c',
     43        'test/text.c',
    4244        'test/tpf.c',
    4345        'test/typeface.c',
  • uspace/lib/gfxfont/test/main.c

    r25f2983b r8fa65af0  
    3434PCUT_IMPORT(glyph);
    3535PCUT_IMPORT(glyph_bmp);
     36PCUT_IMPORT(text);
    3637PCUT_IMPORT(tpf);
    3738PCUT_IMPORT(typeface);
Note: See TracChangeset for help on using the changeset viewer.