diff options
author | Adrian Johnson <ajohnson@redneon.com> | 2010-10-02 12:34:42 +0930 |
---|---|---|
committer | Adrian Johnson <ajohnson@redneon.com> | 2010-10-02 18:13:49 +0930 |
commit | cd74f5edabf653d1c1c6daacea3626ba2548d5e0 (patch) | |
tree | 1c555027f84945aaaf05138b200f07e4cd9d2301 /src/cairo-pdf-operators.c | |
parent | 165a14b5646d582781d119874f549ec9a02d7f53 (diff) |
pdf-operators: word wrap latin text strings
Diffstat (limited to 'src/cairo-pdf-operators.c')
-rw-r--r-- | src/cairo-pdf-operators.c | 73 |
1 files changed, 65 insertions, 8 deletions
diff --git a/src/cairo-pdf-operators.c b/src/cairo-pdf-operators.c index 21592b54..7613b41a 100644 --- a/src/cairo-pdf-operators.c +++ b/src/cairo-pdf-operators.c @@ -169,8 +169,10 @@ typedef struct _word_wrap_stream { int max_column; int column; cairo_bool_t last_write_was_space; + cairo_bool_t in_string; cairo_bool_t in_hexstring; - cairo_bool_t empty_hexstring; + cairo_bool_t empty_string; + cairo_bool_t in_escape; } word_wrap_stream_t; static int @@ -179,7 +181,7 @@ _count_word_up_to (const unsigned char *s, int length) int word = 0; while (length--) { - if (! (_cairo_isspace (*s) || *s == '<')) { + if (! (_cairo_isspace (*s) || *s == '<' || *s == '(')) { s++; word++; } else { @@ -213,6 +215,33 @@ _count_hexstring_up_to (const unsigned char *s, int length, int columns) return word; } +/* Count up to either the end of the string or the number of columns + * remaining. + */ +static int +_count_string_up_to (const unsigned char *s, int length, int columns, cairo_bool_t *in_escape) +{ + int word = 0; + + while (length--) { + if (*in_escape || *s != ')') { + word++; + if (!*in_escape && *s++ == '\\') + *in_escape = TRUE; + else + *in_escape = FALSE; + } else { + return word; + } + + columns--; + if (columns < 0 && word > 0) + return word; + } + + return word; +} + static cairo_status_t _word_wrap_stream_write (cairo_output_stream_t *base, const unsigned char *data, @@ -225,7 +254,7 @@ _word_wrap_stream_write (cairo_output_stream_t *base, while (length) { if (*data == '<') { stream->in_hexstring = TRUE; - stream->empty_hexstring = TRUE; + stream->empty_string = TRUE; stream->last_write_was_space = FALSE; data++; length--; @@ -238,7 +267,27 @@ _word_wrap_stream_write (cairo_output_stream_t *base, length--; _cairo_output_stream_printf (stream->output, ">"); stream->column++; - } else if (_cairo_isspace (*data)) { + } else if (*data == '(') { + stream->in_string = TRUE; + stream->in_escape = FALSE; + stream->empty_string = TRUE; + stream->last_write_was_space = FALSE; + data++; + length--; + _cairo_output_stream_printf (stream->output, "("); + stream->column++; + } else if (*data == ')') { + stream->in_string = FALSE; + stream->last_write_was_space = FALSE; + data++; + length--; + _cairo_output_stream_printf (stream->output, ")"); + stream->column++; + if (stream->column > stream->max_column) { + _cairo_output_stream_printf (stream->output, "\n"); + stream->column = 0; + } + } else if (_cairo_isspace (*data) && !stream->in_string) { newline = (*data == '\n' || *data == '\r'); if (! newline && stream->column >= stream->max_column) { _cairo_output_stream_printf (stream->output, "\n"); @@ -257,6 +306,9 @@ _word_wrap_stream_write (cairo_output_stream_t *base, if (stream->in_hexstring) { word = _count_hexstring_up_to (data, length, MAX (stream->max_column - stream->column, 0)); + } else if (stream->in_string) { + word = _count_string_up_to (data, length, + MAX (stream->max_column - stream->column, 0), &stream->in_escape); } else { word = _count_word_up_to (data, length); } @@ -264,8 +316,11 @@ _word_wrap_stream_write (cairo_output_stream_t *base, * string word from a previous call to write. */ if (stream->column + word >= stream->max_column) { if (stream->last_write_was_space || - (stream->in_hexstring && !stream->empty_hexstring)) + (stream->in_hexstring && !stream->empty_string) || + (stream->in_string && !stream->empty_string && !stream->in_escape)) { + if (stream->in_string) + _cairo_output_stream_printf (stream->output, "\\"); _cairo_output_stream_printf (stream->output, "\n"); stream->column = 0; } @@ -275,8 +330,8 @@ _word_wrap_stream_write (cairo_output_stream_t *base, length -= word; stream->column += word; stream->last_write_was_space = FALSE; - if (stream->in_hexstring) - stream->empty_hexstring = FALSE; + if (stream->in_hexstring || stream->in_string) + stream->empty_string = FALSE; } } @@ -314,7 +369,9 @@ _word_wrap_stream_create (cairo_output_stream_t *output, int max_column) stream->column = 0; stream->last_write_was_space = FALSE; stream->in_hexstring = FALSE; - stream->empty_hexstring = TRUE; + stream->in_string = FALSE; + stream->in_escape = FALSE; + stream->empty_string = TRUE; return &stream->base; } |