We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4515 - [PATCH] extend API for text measurement without rendering
Summary: [PATCH] extend API for text measurement without rendering
Status: RESOLVED FIXED
Alias: None
Product: SDL_ttf
Classification: Unclassified
Component: misc (show other bugs)
Version: unspecified
Hardware: All All
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-02-20 16:56 UTC by Dmitry Gapkalov
Modified: 2019-04-23 12:21 UTC (History)
1 user (show)

See Also:


Attachments
patch (5.96 KB, patch)
2019-02-20 16:56 UTC, Dmitry Gapkalov
Details | Diff
updated patch (6.00 KB, patch)
2019-02-22 18:55 UTC, Dmitry Gapkalov
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dmitry Gapkalov 2019-02-20 16:56:39 UTC
Created attachment 3641 [details]
patch

/* Get the measurement string of text without rendering 
   in:
   width  - in pixels to measure this text
   out:
   result - latest position in text inside width
   extent - latest calculated width(more or equal to width) 
*/
extern DECLSPEC int SDLCALL TTF_MeasureText(TTF_Font *font, const char *text, int width, int *extent);
extern DECLSPEC int SDLCALL TTF_MeasureUTF8(TTF_Font *font, const char *text, int width, int *extent);
extern DECLSPEC int SDLCALL TTF_MeasureUNICODE(TTF_Font *font, const Uint16 *text, int width, int *extent);
Comment 1 Sylvain 2019-02-22 14:17:21 UTC
Hey, thanks for the patch !

So this function gives the maximum of char that can fit in 'width' pixel.

Just wondering, why is this function needed ?
It seems like re-doing the SDL_ttf Wrap function ?
Comment 2 Dmitry Gapkalov 2019-02-22 16:12:15 UTC
For algorithm like:

- auto add "..." in the end of the line
- auto-text height for specific bounds

SDL_ttf Wrap spend time for rendering text, but need calculation
 first.
Comment 3 Dmitry Gapkalov 2019-02-22 18:55:16 UTC
Created attachment 3650 [details]
updated patch

updated version of patch
Comment 4 Sylvain 2019-02-22 20:06:16 UTC
Not sure I got it, so I've more questions:

If you want to add "...", why don't you string cat it as a suffix ? Then render ?

"- auto-text height for specific bounds", you mean a Wrap function with user defined height ?
-> that would be a TTF_SetLineSkip() ?



But I think this mesure functionnality is interesting for re-writing the wrap lenght algorithm which call to many times TTF_SetSize()
( https://discourse.libsdl.org/t/optimization-suggestion-for-sdl-ttf-on-long-paragraphs/25777 )
Comment 5 Dmitry Gapkalov 2019-02-25 16:14:40 UTC
1) >>If you want to add "...", why don't you string cat it as a suffix ? Then render ?

I have maximum width(in pixels) and font size, I need put text, and if some text not included, I need to put '...' in the end of the line. I need to calculate how many symbols I can put before "...".


2) >> "- auto-text height for specific bounds", you mean a Wrap function with user defined height ?
-> that would be a TTF_SetLineSkip() ?

Wrapped function in SDL is just for simple text rendering. It not cover real cases like html-css view.
Comment 6 Sylvain 2019-03-25 13:59:56 UTC
I've added the API, a little bit modified, here: 
https://hg.libsdl.org/SDL_ttf/rev/eb1d2358c50c

Together with changing the _Wrapped functions, which are good candidates to use it.

Can you check it ?


What is different:
- it returns the usual -1/0 upon success.
- the "result" is an output parameter.

I don't returns the extent as: 
  SDL_max(maxx, FT_FLOOR(x + prev_advance)) - minx;
but:
  maxx - minx
Not to forward what seems added for a corner case ..


Also, maybe it would be interesting to have more parameters for the measurement :

- prior_character The optionally-specified character that immediately precedes the string. This may have an impact on the string width due to kerning
(eg https://github.com/libRocket/libRocket/blob/master/Source/Core/FreeType/FontFaceHandle.h#L70 )
- extent_height ?
- advance width/height ?
Comment 7 Dmitry Gapkalov 2019-04-12 14:53:04 UTC
Hello, sorry for long delay.

Yes your solution looks perfect, except one thing, please modify this part:


        /* Measurement mode */
        if (measure_width) {
            int cw = maxx - minx;
            if (cw >= measure_width) {
                break;
            }
	    current_width = cw;
	    char_count += 1;
        }

Current width for result needed from previous calculation, because our task is insert some text in max width.
Comment 8 Sylvain 2019-04-12 19:34:22 UTC
Hello,

Thanks for checking! I've modified it:
https://hg.libsdl.org/SDL_ttf/rev/fd1e90b8cfca
Comment 9 Dmitry Gapkalov 2019-04-12 19:53:48 UTC
Thank you so much. Done.
Comment 10 Dmitry Gapkalov 2019-04-22 17:42:40 UTC
Hello, need additional fix: 

       /* Measurement mode */
        if (measure_width) {
	    int cw = SDL_max(maxx, FT_FLOOR(x + prev_advance)) - minx;
            if (cw >= measure_width) {
                break;
            }
	    current_width = cw;
	    char_count += 1;
        }

I return previous version of calculation cw, because current variant:

int cw = maxx - minx;

doesn't correct measure strings ended with more than one space at the end.
Comment 11 Sylvain 2019-04-23 12:21:25 UTC
Fixed https://hg.libsdl.org/SDL_ttf/rev/8be7bd6ac3d0 
Thanks !