From a508cbadc5ecd914965468086b9aee5d120fdb05 Mon Sep 17 00:00:00 2001 From: nyamatongwe Date: Fri, 15 Jun 2007 11:24:38 +0000 Subject: Correctly measure widths of Latin1 characters that take 2 bytes in UTF-8. --- gtk/PlatGTK.cxx | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/gtk/PlatGTK.cxx b/gtk/PlatGTK.cxx index 3cdfeb195..72e6eff83 100644 --- a/gtk/PlatGTK.cxx +++ b/gtk/PlatGTK.cxx @@ -1386,6 +1386,10 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, int *positi int places = curIndex - i; int distance = position - positions[i-1]; while (i < curIndex) { + // Evenly distribute space among bytes of this cluster. + // Would be better to find number of characters and then + // divide evenly between characters with each byte of a character + // being at the same position. positions[i] = position - (curIndex - 1 - i) * distance / places; i++; } @@ -1442,16 +1446,22 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, int *positi PangoLayoutIter *iter = pango_layout_get_iter(layout); pango_layout_iter_get_cluster_extents(iter, NULL, &pos); int i = 0; + int positionStart = 0; + int clusterStart = 0; + // Each Latin1 input character may take 1 or 2 bytes in UTF-8 + // and groups of up to 3 may be represented as ligatures. while (pango_layout_iter_next_cluster(iter)) { pango_layout_iter_get_cluster_extents(iter, NULL, &pos); int position = PANGO_PIXELS(pos.x); - int curIndex = pango_layout_iter_get_index(iter); - int places = curIndex - i; - int distance = position - positions[i-1]; - while (i < curIndex) { - positions[i] = position - (curIndex - 1 - i) * distance / places; - i++; + int distance = position - positionStart; + int clusterEnd = pango_layout_iter_get_index(iter); + int ligatureLength = g_utf8_strlen(utfForm + clusterStart, clusterEnd - clusterStart); + PLATFORM_ASSERT(ligatureLength > 0 && ligatureLength <= 3); + for (int charInLig=0; charInLig