summaryrefslogtreecommitdiff
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
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.
-rw-r--r--ChangeLog13
-rw-r--r--examples/renderdemo.c2
-rw-r--r--pango/pango-layout.c57
3 files changed, 44 insertions, 28 deletions
diff --git a/ChangeLog b/ChangeLog
index 6a7678df..7171c4a4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,11 +1,18 @@
+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.
+
2006-02-25 Hans Breuer <hans@breuer.org>
- * pango/pangocairo-win32font.c(pango_cairo_win32_font_install) :
+ * pango/pangocairo-win32font.c(pango_cairo_win32_font_install):
return TRUE to glyphs rendered at all (instead of box). This may
be compiler specific (what to return w/o return) but fixes bug #332538
- * pango/makefile.msc : updated
- * pango/pango.def : just some sorting
+ * pango/makefile.msc: updated
+ * pango/pango.def: just some sorting
2006-02-25 Behdad Esfahbod <behdad@gnome.org>
diff --git a/examples/renderdemo.c b/examples/renderdemo.c
index 52e911bf..0dda7eb2 100644
--- a/examples/renderdemo.c
+++ b/examples/renderdemo.c
@@ -638,8 +638,6 @@ parse_options (int argc, char *argv[])
{
if (!g_file_get_contents (argv[1], &text, &len, &error))
fail ("%s\n", error->message);
- if (!g_utf8_validate (text, len, NULL))
- fail ("Input text is not valid UTF-8");
}
/* Strip trailing whitespace
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);