Changeset a130983 in mainline for uspace/lib/gfxfont/src/text.c


Ignore:
Timestamp:
2022-11-17T19:56:07Z (17 months ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
901b302
Parents:
5d62130
Message:

Implement text abbreviation

When rendering text, gfx_puttext can now abbreviate it with …
to fit within the specified width. Use on window captions.
Now Barber Pole can have a proper caption.

Note that now the caption is centered on the space between the left
edge and the buttons, instead on the entire title bar, so it
may look a little lopsided. It will look better once we put
something to the left side of the title bar.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/gfxfont/src/text.c

    r5d62130 ra130983  
    183183        /* Adjust position for horizontal alignment */
    184184        if (fmt->halign != gfx_halign_left) {
     185                /* Compute text width */
    185186                width = gfx_text_width(fmt->font, str);
     187                if (fmt->abbreviate && width > fmt->width)
     188                        width = fmt->width;
     189
    186190                switch (fmt->halign) {
    187191                case gfx_halign_center:
     
    233237        gfx_coord2_t spos;
    234238        gfx_rect_t rect;
     239        gfx_coord_t width;
     240        gfx_coord_t rmargin;
     241        bool ellipsis;
    235242        errno_t rc;
    236243
     
    244251        if (rc != EOK)
    245252                return rc;
     253
     254        width = gfx_text_width(fmt->font, str);
     255
     256        if (fmt->abbreviate && width > fmt->width) {
     257                /* Need to append ellipsis */
     258                ellipsis = true;
     259                rmargin = spos.x + fmt->width - gfx_text_width(fmt->font, "...");
     260        } else {
     261                ellipsis = false;
     262                rmargin = spos.x + width;
     263        }
    246264
    247265        cpos = spos;
     
    256274                gfx_glyph_get_metrics(glyph, &gmetrics);
    257275
     276                /* Stop if we would run over the right margin */
     277                if (fmt->abbreviate && cpos.x + gmetrics.advance > rmargin)
     278                        break;
     279
    258280                rc = gfx_glyph_render(glyph, &cpos);
    259281                if (rc != EOK)
     
    274296
    275297                rc = gfx_fill_rect(fmt->font->typeface->gc, &rect);
     298                if (rc != EOK)
     299                        return rc;
     300        }
     301
     302        /* Render ellipsis, if required */
     303        if (ellipsis) {
     304                rc = gfx_font_search_glyph(fmt->font, ".", &glyph, &stradv);
     305                if (rc != EOK)
     306                        return EOK;
     307
     308                gfx_glyph_get_metrics(glyph, &gmetrics);
     309
     310                rc = gfx_glyph_render(glyph, &cpos);
     311                if (rc != EOK)
     312                        return rc;
     313
     314                cpos.x += gmetrics.advance;
     315
     316                rc = gfx_glyph_render(glyph, &cpos);
     317                if (rc != EOK)
     318                        return rc;
     319
     320                cpos.x += gmetrics.advance;
     321
     322                rc = gfx_glyph_render(glyph, &cpos);
    276323                if (rc != EOK)
    277324                        return rc;
     
    376423        tfmt.valign = gfx_valign_baseline;
    377424
     425        /* Remaining available width */
     426        tfmt.width = fmt->width - (cpos->x - spos.x);
     427
    378428        *cfmt = tfmt;
    379429}
     
    390440{
    391441        gfx_coord2_t spos;
     442        gfx_coord_t width;
    392443
    393444        gfx_text_start_pos(pos, fmt, str, &spos);
     445        width = gfx_text_width(fmt->font, str);
     446        if (fmt->abbreviate && width > fmt->width)
     447                width = fmt->width;
    394448
    395449        rect->p0.x = spos.x;
    396450        rect->p0.y = spos.y - fmt->font->metrics.ascent;
    397         rect->p1.x = spos.x + gfx_text_width(fmt->font, str);
     451        rect->p1.x = spos.x + width;
    398452        rect->p1.y = spos.y +  fmt->font->metrics.descent + 1;
    399453}
Note: See TracChangeset for help on using the changeset viewer.