Changeset a130983 in mainline


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.

Location:
uspace
Files:
7 edited

Legend:

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

    r5d62130 ra130983  
    361361
    362362        ui_wnd_params_init(&params);
    363         params.caption = "";
     363        params.caption = "Barber Pole";
    364364        params.placement = ui_wnd_place_bottom_right;
    365365        /*
  • uspace/lib/gfxfont/include/types/gfx/text.h

    r5d62130 ra130983  
    7373        /** Horizontal alignment */
    7474        gfx_halign_t halign;
    75         /** Justification width (for gfx_halign_justify) */
    76         gfx_coord_t justify_width;
     75        /** Width available for the text */
     76        gfx_coord_t width;
    7777        /** Vertical alignment */
    7878        gfx_valign_t valign;
     79        /** Abbreviate the text with ellipsis if it does not fit @c width */
     80        bool abbreviate;
    7981        /** Underline */
    8082        bool underline;
  • 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}
  • uspace/lib/ui/include/types/ui/paint.h

    r5d62130 ra130983  
    9494        /** Horizontal alignment */
    9595        gfx_halign_t halign;
    96         /** Justification width (for gfx_halign_justify) */
    97         gfx_coord_t justify_width;
     96        /** Width available for the text */
     97        gfx_coord_t width;
    9898        /** Vertical alignment */
    9999        gfx_valign_t valign;
  • uspace/lib/ui/private/wdecor.h

    r5d62130 ra130983  
    8080        /** Title bar rectangle */
    8181        gfx_rect_t title_bar_rect;
     82        /** Window caption rectangle */
     83        gfx_rect_t caption_rect;
    8284        /** Maximize button rectangle */
    8385        gfx_rect_t btn_max_rect;
  • uspace/lib/ui/src/paint.c

    r5d62130 ra130983  
    922922        tfmt.color = fmt->color;
    923923        tfmt.halign = fmt->halign;
    924         tfmt.justify_width = fmt->justify_width;
     924        tfmt.width = fmt->width;
    925925        tfmt.valign = fmt->valign;
    926926        tfmt.underline = false;
  • uspace/lib/ui/src/wdecor.c

    r5d62130 ra130983  
    8888        /** Window frame width in text mode */
    8989        wdecor_frame_w_text = 1,
     90        /** Window caption horizontal margin */
     91        wdecor_cap_hmargin = 4,
     92        /** Window caption horizontal margin in text mode */
     93        wdecor_cap_hmargin_text = 1,
    9094        /** Close button cross leg length */
    9195        wdecor_close_cross_n = 5,
     
    326330                fmt.halign = gfx_halign_center;
    327331                fmt.valign = gfx_valign_center;
    328 
    329                 pos.x = (trect.p0.x + trect.p1.x) / 2;
    330                 pos.y = (trect.p0.y + trect.p1.y) / 2;
     332                fmt.abbreviate = true;
     333                fmt.width = geom.caption_rect.p1.x -
     334                    geom.caption_rect.p0.x;
     335
     336                pos.x = (geom.caption_rect.p0.x + geom.caption_rect.p1.x) / 2;
     337                pos.y = (geom.caption_rect.p0.y + geom.caption_rect.p1.y) / 2;
    331338
    332339                if (wdecor->res->textmode) {
     
    450457        gfx_coord_t btn_x;
    451458        gfx_coord_t btn_y;
     459        gfx_coord_t cap_hmargin;
    452460
    453461        /* Does window have a frame? */
     
    529537                        geom->btn_max_rect.p1.x = btn_x;
    530538                        geom->btn_max_rect.p1.y = btn_y + 20;
     539
     540                        btn_x -= 20;
    531541                } else {
    532542                        geom->btn_max_rect.p0.x = btn_x - 3;
     
    534544                        geom->btn_max_rect.p1.x = btn_x;
    535545                        geom->btn_max_rect.p1.y = btn_y + 1;
     546
     547                        btn_x -= 3;
    536548                }
    537549        } else {
     
    541553                geom->btn_max_rect.p1.y = 0;
    542554        }
     555
     556        if (wdecor->res->textmode == false)
     557                cap_hmargin = wdecor_cap_hmargin;
     558        else
     559                cap_hmargin = wdecor_cap_hmargin_text;
     560
     561        geom->caption_rect.p0.x = geom->title_bar_rect.p0.x +
     562            cap_hmargin;
     563        geom->caption_rect.p0.y = geom->title_bar_rect.p0.y;
     564        geom->caption_rect.p1.x = btn_x - cap_hmargin;
     565        geom->caption_rect.p1.y = geom->title_bar_rect.p1.y;
    543566}
    544567
Note: See TracChangeset for help on using the changeset viewer.