Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PATCH] extend API for text measurement without rendering #107

Closed
SDLBugzilla opened this issue Feb 11, 2021 · 0 comments
Closed

[PATCH] extend API for text measurement without rendering #107

SDLBugzilla opened this issue Feb 11, 2021 · 0 comments

Comments

@SDLBugzilla
Copy link
Collaborator

This bug report was migrated from our old Bugzilla tracker.

These attachments are available in the static archive:

Reported in version: unspecified
Reported for operating system, platform: All, All

Comments on the original bug report:

On 2019-02-20 16:56:39 +0000, Dmitry Gapkalov wrote:

Created attachment 3641
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);

On 2019-02-22 14:17:21 +0000, Sylvain wrote:

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 ?

On 2019-02-22 16:12:15 +0000, Dmitry Gapkalov wrote:

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.

On 2019-02-22 18:55:16 +0000, Dmitry Gapkalov wrote:

Created attachment 3650
updated patch

updated version of patch

On 2019-02-22 20:06:16 +0000, Sylvain wrote:

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 )

On 2019-02-25 16:14:40 +0000, Dmitry Gapkalov wrote:

  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 "...".

  1. "- 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.

On 2019-03-25 13:59:56 +0000, Sylvain wrote:

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 :

On 2019-04-12 14:53:04 +0000, Dmitry Gapkalov wrote:

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.

On 2019-04-12 19:34:22 +0000, Sylvain wrote:

Hello,

Thanks for checking! I've modified it:
https://hg.libsdl.org/SDL_ttf/rev/fd1e90b8cfca

On 2019-04-12 19:53:48 +0000, Dmitry Gapkalov wrote:

Thank you so much. Done.

On 2019-04-22 17:42:40 +0000, Dmitry Gapkalov wrote:

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.

On 2019-04-23 12:21:25 +0000, Sylvain wrote:

Fixed https://hg.libsdl.org/SDL_ttf/rev/8be7bd6ac3d0
Thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant