diff options
author | James Bursa <james@netsurf-browser.org> | 2005-03-24 00:05:51 +0000 |
---|---|---|
committer | James Bursa <james@netsurf-browser.org> | 2005-03-24 00:05:51 +0000 |
commit | 0d5d898413c1daa3d4b60a7865581d6582f1caef (patch) | |
tree | ec156dd473e167fe70e6a069b6c091eb9acee980 | |
parent | 40c281e5fc81ec404c2c98573bb8ce93c3f49672 (diff) | |
download | librufl-0d5d898413c1daa3d4b60a7865581d6582f1caef.tar.gz librufl-0d5d898413c1daa3d4b60a7865581d6582f1caef.tar.bz2 |
[project @ 2005-03-24 00:05:51 by bursa]
Implement rufl_split().
svn path=/import/rufl/; revision=2459
-rw-r--r-- | rufl.h | 11 | ||||
-rw-r--r-- | rufl_paint.c | 49 | ||||
-rw-r--r-- | rufl_test.c | 6 |
3 files changed, 54 insertions, 12 deletions
@@ -103,6 +103,17 @@ rufl_code rufl_x_to_offset(const char *font_family, rufl_style font_style, /** + * Find the prefix of a string that will fit in a specified width. + */ + +rufl_code rufl_split(const char *font_family, rufl_style font_style, + unsigned int font_size, + const char *string, size_t length, + int width, + size_t *char_offset, int *actual_x); + + +/** * Dump the internal library state to stdout. */ diff --git a/rufl_paint.c b/rufl_paint.c index 9a2da7d..3762859 100644 --- a/rufl_paint.c +++ b/rufl_paint.c @@ -14,7 +14,8 @@ #include "rufl_internal.h" -typedef enum { rufl_PAINT, rufl_WIDTH, rufl_X_TO_OFFSET } rufl_action; +typedef enum { rufl_PAINT, rufl_WIDTH, rufl_X_TO_OFFSET, + rufl_SPLIT } rufl_action; #define rufl_PROCESS_CHUNK 200 bool rufl_can_background_blend = false; @@ -114,6 +115,23 @@ rufl_code rufl_x_to_offset(const char *font_family, rufl_style font_style, /** + * Find the prefix of a string that will fit in a specified width. + */ + +rufl_code rufl_split(const char *font_family, rufl_style font_style, + unsigned int font_size, + const char *string, size_t length, + int width, + size_t *char_offset, int *actual_x) +{ + return rufl_process(rufl_SPLIT, + font_family, font_style, font_size, string, + length, 0, 0, 0, 0, 0, + width, char_offset, actual_x); +} + + +/** * Render, measure, or split Unicode text. */ @@ -140,23 +158,26 @@ rufl_code rufl_process(rufl_action action, assert(action == rufl_PAINT || (action == rufl_WIDTH && width) || (action == rufl_X_TO_OFFSET && char_offset && + actual_x) || + (action == rufl_SPLIT && char_offset && actual_x)); if ((flags & rufl_BLEND_FONT) && !rufl_can_background_blend) { - /* unsuitable FM => clear blending bit */ - flags &= ~rufl_BLEND_FONT; + /* unsuitable FM => clear blending bit */ + flags &= ~rufl_BLEND_FONT; } if (length == 0) { if (action == rufl_WIDTH) *width = 0; - else if (action == rufl_X_TO_OFFSET) { + else if (action == rufl_X_TO_OFFSET || action == rufl_SPLIT) { *char_offset = 0; *actual_x = 0; } return rufl_OK; } - if (action == rufl_X_TO_OFFSET && click_x <= 0) { + if ((action == rufl_X_TO_OFFSET || action == rufl_SPLIT) && + click_x <= 0) { *char_offset = 0; *actual_x = 0; return rufl_OK; @@ -215,7 +236,8 @@ rufl_code rufl_process(rufl_action action, font_size, &x, y, trfm, flags, click_x, &offset); - if (action == rufl_X_TO_OFFSET && (offset < n || click_x < x)) + if ((action == rufl_X_TO_OFFSET || action == rufl_SPLIT) && + (offset < n || click_x < x)) break; if (code != rufl_OK) return code; @@ -224,7 +246,7 @@ rufl_code rufl_process(rufl_action action, if (action == rufl_WIDTH) *width = x; - else if (action == rufl_X_TO_OFFSET) { + else if (action == rufl_X_TO_OFFSET || action == rufl_SPLIT) { *char_offset = offset_map[offset]; *actual_x = x; } @@ -309,12 +331,13 @@ rufl_code rufl_process_span(rufl_action action, } /* increment x by width of span */ - if (action == rufl_X_TO_OFFSET) { + if (action == rufl_X_TO_OFFSET || action == rufl_SPLIT) { rufl_fm_error = xfont_scan_string(f, (const char *) s, (trfm ? font_GIVEN_TRFM : 0) | font_GIVEN_LENGTH | font_GIVEN_FONT | font_KERN | font_GIVEN16_BIT | - font_RETURN_CARET_POS, + ((action == rufl_X_TO_OFFSET) ? + font_RETURN_CARET_POS : 0), (click_x - *x) * 400, 0x7fffffff, 0, trfm, n * 2, (char **) &split_point, &x_out, &y_out, 0); @@ -405,11 +428,13 @@ rufl_code rufl_process_span_old(rufl_action action, } /* increment x by width of span */ - if (action == rufl_X_TO_OFFSET) { + if (action == rufl_X_TO_OFFSET || action == rufl_SPLIT) { rufl_fm_error = xfont_scan_string(f, s2, (trfm ? font_GIVEN_TRFM : 0) | font_GIVEN_LENGTH | font_GIVEN_FONT | - font_KERN | font_RETURN_CARET_POS, + font_KERN | + ((action == rufl_X_TO_OFFSET) ? + font_RETURN_CARET_POS : 0), (click_x - *x) * 400, 0x7fffffff, 0, trfm, n, &split_point, &x_out, &y_out, 0); *offset = split_point - s2; @@ -460,7 +485,7 @@ rufl_code rufl_process_not_available(rufl_action action, if (action == rufl_WIDTH) { *x += n * dx; return rufl_OK; - } else if (action == rufl_X_TO_OFFSET) { + } else if (action == rufl_X_TO_OFFSET || action == rufl_SPLIT) { if (click_x - *x < (int) (n * dx)) *offset = (click_x - *x) / dx; else diff --git a/rufl_test.c b/rufl_test.c index 6068bc5..68148ea 100644 --- a/rufl_test.c +++ b/rufl_test.c @@ -39,6 +39,12 @@ int main(void) "rufl_x_to_offset"); printf("x to offset: %i -> %i %i \"%s\"\n", x, actual_x, char_offset, utf8_test + char_offset); + try(rufl_split("NewHall", rufl_REGULAR, 240, + utf8_test, sizeof utf8_test - 1, + x, &char_offset, &actual_x), + "rufl_split"); + printf("split: %i -> %i %i \"%s\"\n", x, actual_x, + char_offset, utf8_test + char_offset); } rufl_quit(); |