summaryrefslogtreecommitdiff
path: root/pango
diff options
context:
space:
mode:
authorBehdad Esfahbod <behdad@gnome.org>2006-02-26 21:23:14 +0000
committerBehdad Esfahbod <behdad@src.gnome.org>2006-02-26 21:23:14 +0000
commit3eca15ac63e64e48bd9f0881224198c3534043f2 (patch)
tree17f842d896fb887516f1cb843711f44ebdc84463 /pango
parentd952f4afb641fc9f4b42f8347bfa381897ea6830 (diff)
downloadpango-3eca15ac63e64e48bd9f0881224198c3534043f2.tar.gz
Replace each byte in invalid UTF-8 sequences with '?'. (bug #331995)
2006-02-26 Behdad Esfahbod <behdad@gnome.org> * pango/pango-layout.c (pango_layout_set_text): Replace each byte in invalid UTF-8 sequences with '?'. (bug #331995) * examples/renderdemo.c: Don't exit on invalid UTF-8 input.
Diffstat (limited to 'pango')
-rw-r--r--pango/pango-layout.c57
1 files changed, 34 insertions, 23 deletions
diff --git a/pango/pango-layout.c b/pango/pango-layout.c
index 3fec81bb..ab0b9fb3 100644
--- a/pango/pango-layout.c
+++ b/pango/pango-layout.c
@@ -808,9 +808,11 @@ pango_layout_get_ellipsize (PangoLayout *layout)
* pango_layout_set_text:
* @layout: a #PangoLayout
* @text: a valid UTF-8 string
- * @length: the length of @text, in bytes. -1 indicates that
+ * @length: maximum length of @text, in bytes. -1 indicates that
* the string is nul-terminated and the length should be
- * calculated.
+ * calculated. The text will also be truncated on
+ * encountaring a nul-termination even when @length is
+ * positive.
*
* Sets the text of the layout.
**/
@@ -819,37 +821,46 @@ pango_layout_set_text (PangoLayout *layout,
const char *text,
int length)
{
- char *old_text;
+ char *old_text, *start, *end;
g_return_if_fail (layout != NULL);
g_return_if_fail (length == 0 || text != NULL);
old_text = layout->text;
-
+
if (length < 0)
- length = strlen (text);
+ layout->text = g_strdup (text);
+ else if (length > 0)
+ /* This is not exactly what we want. We don't need the padding...
+ */
+ layout->text = g_strndup (text, length);
+ else
+ layout->text = g_malloc0 (1);
-#ifndef G_DISABLE_ASSERT
- if (length != 0)
- {
- const char *end;
- if (!g_utf8_validate (text, length, &end))
- {
- /* TODO: Write out the beginning excerpt of text? */
- g_warning ("Invalid UTF-8 string passed to pango_layout_set_text()");
- }
- }
-#endif
-
- /* NULL-terminate the text for convenience.
+ layout->length = strlen (layout->text);
+
+ /* validate it, and replace invalid bytes with '?'
*/
- layout->text = g_malloc (length + 1);
- if (length > 0)
- memcpy (layout->text, text, length);
- layout->text[length] = '\0';
+ start = layout->text;
+ for (;;) {
+ gboolean valid;
+
+ valid = g_utf8_validate (start, -1, &end);
+
+ if (!*end)
+ break;
+
+ if (!valid)
+ *end++ = '?';
+
+ start = end;
+ }
+
+ if (start != layout->text)
+ /* TODO: Write out the beginning excerpt of text? */
+ g_warning ("Invalid UTF-8 string passed to pango_layout_set_text()");
layout->n_chars = g_utf8_strlen (layout->text, -1);
- layout->length = length;
pango_layout_clear_lines (layout);