| Summary: | [PATCH] extend API for text measurement without rendering | ||
|---|---|---|---|
| Product: | SDL_ttf | Reporter: | Dmitry Gapkalov <gapkalov> |
| Component: | misc | Assignee: | Sam Lantinga <slouken> |
| Status: | RESOLVED FIXED | QA Contact: | Sam Lantinga <slouken> |
| Severity: | normal | ||
| Priority: | P2 | CC: | sylvain.becker |
| Version: | unspecified | ||
| Hardware: | All | ||
| OS: | All | ||
| Attachments: |
patch
updated patch |
||
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 ? 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. Created attachment 3650 [details]
updated patch
updated version of patch
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 ) 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. 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 ? 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.
Hello, Thanks for checking! I've modified it: https://hg.libsdl.org/SDL_ttf/rev/fd1e90b8cfca Thank you so much. Done. 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.
Fixed https://hg.libsdl.org/SDL_ttf/rev/8be7bd6ac3d0 Thanks ! |
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);