Re: [Nautilus-list] Two patches for Eel library 1.0



Hello,
Done. Please check them. And I made a zh_CN.GB2312.po file for eel library.

Regards,

James Su

Darin Adler wrote:

On Wednesday, June 13, 2001, at 06:47  AM, <suzhe gnuchina org> wrote:

   I made two patches for Eel library 1.0 to deal with multibyte string
ellipsize and smooth text layout bugs.
   I have test these patch under zh_CN.GB2312 and zh_CN.GBK locales.


These patches look pretty good. One small problem with them is that they don't use the proper coding style. For example, you write:

    g_free( xxx );

and in Eel, the style is:

    g_free (xxx);

and you write:

    if( wc_text_len <= 0 ){

but in Eel, the style is:

    if (wc_text_len <= 0) {

If you would fix these minor problems and resubmit the patch it would be much appreciated.

    -- Darin

_______________________________________________
Nautilus-list mailing list
Nautilus-list lists eazel com
http://lists.eazel.com/mailman/listinfo/nautilus-list


diff -urN eel-1.0/eel/eel-gdk-font-extensions.c eel-1.0.new/eel/eel-gdk-font-extensions.c
--- eel-1.0/eel/eel-gdk-font-extensions.c	Wed Apr 18 04:05:12 2001
+++ eel-1.0.new/eel/eel-gdk-font-extensions.c	Thu Jun 14 17:49:43 2001
@@ -643,21 +643,37 @@
 static char *
 eel_string_ellipsize_start (const char *string, GdkFont *font, int width)
 {
+	GdkWChar *wc_string;
+	gchar *mb_string;
+	char *result;
+
+	int wc_len;
 	int truncate_offset;
 	int resulting_width;
 
-	resulting_width = gdk_string_width (font, string);
+	wc_len = strlen (string) + 1;
+	wc_string = g_new0 (GdkWChar, wc_len);
+	wc_len = gdk_mbstowcs (wc_string, string, wc_len);
+
+	if (wc_len <= 0) {
+		g_free (wc_string);
+		return g_strdup("");
+	}
+
+	resulting_width = gdk_text_width_wc (font, wc_string, wc_len);
 	if (resulting_width <= width) {
 		/* String is already short enough. */
-		return g_strdup (string);
+		mb_string = gdk_wcstombs (wc_string);
+		g_free (wc_string);
+		return mb_string;
 	}
 	
 	/* Account for the width of the ellipsis. */
 	width -= gdk_string_width (font, ELLIPSIS);
-	
 
 	if (width < 0) {
 		/* No room even for a an ellipsis. */
+		g_free (wc_string);
 		return g_strdup ("");
 	}
 	
@@ -668,31 +684,53 @@
 	 * gdk_string_width ("foobar")
 	 */
         for (truncate_offset = 1; ; truncate_offset++) {
-        	if (string[truncate_offset] == '\0') {
+        	if (wc_string[truncate_offset] == L'\0') {
 			break;
         	}
 
-        	resulting_width -= gdk_char_width (font, string[truncate_offset - 1]);
+        	resulting_width -= gdk_char_width_wc (font, wc_string[truncate_offset - 1]);
 
         	if (resulting_width <= width) {
 			break;
         	}
         }
 
-	return g_strconcat (ELLIPSIS, string + truncate_offset, NULL);
+	mb_string = gdk_wcstombs (wc_string + truncate_offset);
+	result = g_strconcat (ELLIPSIS, mb_string, NULL);
+
+	g_free (mb_string);
+	g_free (wc_string);
+
+	return result;
 }
 
 static char *
 eel_string_ellipsize_end (const char *string, GdkFont *font, int width)
 {
-	int truncated_length;
+	GdkWChar *wc_string;
+	gchar *mb_string;
 	char *result;
+
+	int wc_len;
+
+	int truncated_length;
 	int resulting_width;
 
-	resulting_width = gdk_string_width (font, string);
+	wc_len = strlen (string) + 1;
+	wc_string = g_new0 (GdkWChar, wc_len);
+	wc_len = gdk_mbstowcs (wc_string, string, wc_len);
+
+	if (wc_len <= 0) {
+		g_free (wc_string);
+		return g_strdup("");
+	}
+
+	resulting_width = gdk_text_width_wc (font, wc_string, wc_len);
 	if (resulting_width <= width) {
 		/* String is already short enough. */
-		return g_strdup (string);
+		mb_string = gdk_wcstombs(wc_string);
+		g_free (wc_string);
+		return mb_string;
 	}
 	
 	/* Account for the width of the ellipsis. */
@@ -701,19 +739,24 @@
 
 	if (width < 0) {
 		/* No room even for a an ellipsis. */
+		g_free (wc_string);
 		return g_strdup ("");
 	}
 	
-        for (truncated_length = strlen (string) - 1; truncated_length > 0; truncated_length--) {
-        	resulting_width -= gdk_char_width (font, string[truncated_length]);
+        for (truncated_length = wc_len - 1; truncated_length > 0; truncated_length--) {
+        	resulting_width -= gdk_char_width_wc (font, wc_string[truncated_length]);
         	if (resulting_width <= width) {
 			break;
         	}
         }
-	
-	result = g_malloc (truncated_length + strlen (ELLIPSIS) + 1);
-	memcpy (result, string, truncated_length);
-	strcpy (result + truncated_length, ELLIPSIS);
+
+	wc_string [truncated_length] = L'\0';
+
+	mb_string = gdk_wcstombs (wc_string);
+	result = g_strconcat (mb_string, ELLIPSIS, NULL);
+
+	g_free (mb_string);
+	g_free (wc_string);
 
 	return result;
 }
@@ -721,29 +764,46 @@
 static char *
 eel_string_ellipsize_middle (const char *string, GdkFont *font, int width)
 {
+	GdkWChar *wc_string;
+	gchar *mb_string;
+	char *left, *right, *result;
+
+	int wc_len;
+
 	int original_length;
 	int starting_fragment_length;
 	int ending_fragment_offset;
 	int resulting_width;
-	char *result;
 
-	resulting_width = gdk_string_width (font, string);
+	wc_len = strlen (string) + 1;
+	wc_string = g_new0 (GdkWChar, wc_len);
+	wc_len = gdk_mbstowcs (wc_string, string, wc_len);
+
+	if( wc_len <= 0 ) {
+		g_free (wc_string);
+		return g_strdup("");
+	}
+
+	resulting_width = gdk_text_width_wc (font, wc_string, wc_len);
+
 	if (resulting_width <= width) {
 		/* String is already short enough. */
-		return g_strdup (string);
+		mb_string = gdk_wcstombs(wc_string);
+		g_free (wc_string);
+		return mb_string;
 	}
 	
 	/* Account for the width of the ellipsis. */
 	width -= gdk_string_width (font, ELLIPSIS);
-	
 
 	if (width < 0) {
 		/* No room even for a an ellipsis. */
+		g_free (wc_string);
 		return g_strdup ("");
 	}
 
 	/* Split the original string into two halves */
-	original_length = strlen (string);
+	original_length = wc_len;
 	
 	g_assert (original_length > 0);
 	
@@ -753,7 +813,7 @@
 	/* Shave off a character at a time from the first and the second half
 	 * until we can fit
 	 */
-	resulting_width -= gdk_char_width (font, string[ending_fragment_offset - 1]);
+	resulting_width -= gdk_char_width_wc (font, wc_string[ending_fragment_offset - 1]);
 	
 	/* depending on whether the original string length is odd or even, start by
 	 * shaving off the characters from the starting or ending fragment
@@ -768,7 +828,7 @@
 			if (starting_fragment_length > 0) {
 				starting_fragment_length--;
 			}
-			resulting_width -= gdk_char_width (font, string[starting_fragment_length]);
+			resulting_width -= gdk_char_width_wc (font, wc_string[starting_fragment_length]);
 	case 1:
 			if (resulting_width <= width) {
 				break;
@@ -777,16 +837,21 @@
 			if (ending_fragment_offset < original_length) {
 				ending_fragment_offset++;
 			}
-			resulting_width -= gdk_char_width (font, string[ending_fragment_offset - 1]);
+			resulting_width -= gdk_char_width_wc (font, wc_string[ending_fragment_offset - 1]);
 	        }
 	}
-	
+
 	/* patch the two fragments together with an ellipsis */
-	result = g_malloc (starting_fragment_length + (original_length - ending_fragment_offset)
-		+ strlen (ELLIPSIS) + 1);
-	memcpy (result, string, starting_fragment_length);
-	strcpy (result + starting_fragment_length, ELLIPSIS);
-	strcpy (result + starting_fragment_length + strlen (ELLIPSIS), string + ending_fragment_offset);
+
+	right = gdk_wcstombs (wc_string+ending_fragment_offset);
+	wc_string [starting_fragment_length] = L'\0';
+	left = gdk_wcstombs (wc_string);
+
+	result = g_strconcat (left, ELLIPSIS, right, NULL);
+
+	g_free (wc_string);
+	g_free (left);
+	g_free (right);
 
 	return result;
 }
diff -urN eel-1.0/eel/eel-smooth-text-layout.c eel-1.0.new/eel/eel-smooth-text-layout.c
--- eel-1.0/eel/eel-smooth-text-layout.c	Tue May  1 21:20:38 2001
+++ eel-1.0.new/eel/eel-smooth-text-layout.c	Thu Jun 14 17:37:59 2001
@@ -1078,6 +1078,22 @@
  *
  * Return value: A newly-created &EelTextLayout structure.
  */
+static int
+wcs_scalable_font_text_width (const EelScalableFont *font,
+			      int font_size,
+			      GdkWChar *wc_text)
+{
+	gchar *mb_text;
+	int mb_text_len, width;
+
+	mb_text = gdk_wcstombs (wc_text);
+	mb_text_len = strlen (mb_text);
+
+	width = eel_scalable_font_text_width (font, font_size, mb_text, mb_text_len);
+	g_free (mb_text);
+	return width;
+}
+
 EelTextLayout *
 eel_text_layout_new (const EelScalableFont *font,
 		     int font_size,
@@ -1088,25 +1104,46 @@
 {
 	EelTextLayout *text_layout;
 	EelTextLayoutRow *row;
-	const char *row_end;
-	const char *s, *word_start, *word_end, *old_word_end;
+
 	char *sub_text;
-	int i, w_len;
-	int w;
-	const char *text_iter;
-	int text_len, separators_len;
+	int i;
+
+	GdkWChar *wc_text, *wc_separators;
+	GdkWChar *wc_text_iter; 
+	GdkWChar *wc_row_end, *wc_word_start;
+	GdkWChar *wc_word_end, *wc_old_word_end;
+	GdkWChar *wc_s;
+	GdkWChar wc_char;
+
+	int wc_text_len, wc_separators_len, wc_word_len;
+	int word_width;
 
 	g_return_val_if_fail (font != NULL, NULL);
 	g_return_val_if_fail (font_size > 0, NULL);
 	g_return_val_if_fail (text != NULL, NULL);
 	g_return_val_if_fail (eel_strlen (text) > 0, NULL);
 
+	wc_text_len = strlen (text) + 1;
+	wc_text = g_new0 (GdkWChar, wc_text_len);
+	wc_text_len = gdk_mbstowcs (wc_text, text, wc_text_len);
+
+	if (wc_text_len <= 0) {
+		g_free (wc_text);
+		return NULL;
+	}
+
 	if (!separators)
 		separators = " ";
 
-	text_len = strlen (text);
-
-	separators_len = strlen (separators);
+	wc_separators_len = strlen (separators) + 1;
+	wc_separators = g_new0 (GdkWChar, wc_separators_len);
+	wc_separators_len = gdk_mbstowcs (wc_separators, separators, wc_separators_len);
+
+	if (wc_separators_len <= 0) {
+		g_free (wc_text);
+		g_free (wc_separators);
+		return NULL;
+	}
 
 	text_layout = g_new0 (EelTextLayout, 1);
 
@@ -1117,60 +1154,72 @@
 	text_layout->height = 0;
 	text_layout->baseline_skip = font_size;
 
-	word_end = NULL;
+	wc_word_end = NULL;
 
-	text_iter = text;
-	while (*text_iter) {
-		for (row_end = text_iter; *row_end != 0 && *row_end != '\n'; row_end++);
+	wc_text_iter = wc_text;
+	while (*wc_text_iter != L'\0') {
+		for (wc_row_end = wc_text_iter; *wc_row_end != L'\0' && *wc_row_end != L'\n'; wc_row_end++);
 
 		/* Accumulate words from this row until they don't fit in the max_width */
 
-		s = text_iter;
+		wc_s = wc_text_iter;
 
-		while (s < row_end) {
-			word_start = s;
-			old_word_end = word_end;
-			for (word_end = word_start; *word_end; word_end++) {
-				const char *p;
-				for (p = separators; *p; p++) {
-					if (*word_end == *p)
+		while (wc_s < wc_row_end) {
+			wc_word_start = wc_s;
+			wc_old_word_end = wc_word_end;
+			for (wc_word_end = wc_word_start; *wc_word_end != L'\0'; wc_word_end++) {
+				const GdkWChar *wc_p;
+				for (wc_p = wc_separators; *wc_p != L'\0'; wc_p++) {
+					if (*wc_word_end == *wc_p)
 						goto found;
 				}
 			}
 		found:
-			if (word_end < row_end)
-				word_end++;
+			if (wc_word_end < wc_row_end)
+				wc_word_end++;
+
+			wc_char = *wc_word_end;
+			*wc_word_end = L'\0';
+			word_width = wcs_scalable_font_text_width (font, font_size, wc_text_iter);
+			*wc_word_end = wc_char;
 
-			if (eel_scalable_font_text_width (font, font_size, text_iter, word_end - text_iter) > max_width) {
-				if (word_start == text_iter) {
+			if ( word_width > max_width) {
+				if (wc_word_start == wc_text_iter) {
 					if (confine) {
 						/* We must force-split the word.  Look for a proper
                                                  * place to do it.
 						 */
 
-						w_len = word_end - word_start;
+						wc_word_len = wc_word_end - wc_word_start;
 						
-						for (i = 1; i < w_len; i++) {
-							w = eel_scalable_font_text_width (font, font_size, word_start, i);
-							if (w > max_width) {
+						for (i = 1; i < wc_word_len; i++) {
+
+							wc_char = *(wc_word_start+i);
+							*(wc_word_start+i) = L'\0';
+							word_width = wcs_scalable_font_text_width (font, font_size, wc_word_start);
+							*(wc_word_start+i) = wc_char;
+
+							if (word_width > max_width) {
 								if (i == 1)
 									/* Shit, not even a single character fits */
-									max_width = w;
+									max_width = word_width;
 								else
 									break;
 							}
 						}
 
 						/* Create sub-row with the chars that fit */
-
-						sub_text = g_strndup (word_start, i - 1);
+						wc_char = *(wc_word_start+i-1);
+						*(wc_word_start+i-1) = L'\0';
+						sub_text = gdk_wcstombs (wc_word_start);
+						*(wc_word_start+i-1) = wc_char;
 						
 						row = g_new0 (EelTextLayoutRow, 1);
 						row->text = sub_text;
-						row->text_length = i - 1;
+						row->text_length = strlen (sub_text);
 						row->width = eel_scalable_font_text_width (font, font_size, 
 											   sub_text, 
-											   strlen (sub_text));
+											   row->text_length);
 
 						text_layout->rows = g_list_append (text_layout->rows, row);
 
@@ -1181,26 +1230,29 @@
 
 						/* Bump the text pointer */
 
-						text_iter += i - 1;
-						s = text_iter;
+						wc_text_iter += i - 1;
+						wc_s = wc_text_iter;
 
 						continue;
-					} else
-						max_width = eel_scalable_font_text_width (font, font_size, word_start, word_end - word_start);
-
+					} else {
+						wc_char = *wc_word_end;
+						*wc_word_end = L'\0';
+						max_width = wcs_scalable_font_text_width (font, font_size, wc_text_iter);
+						*wc_word_end = wc_char;
+					}
 					continue; /* Retry split */
 				} else {
-					word_end = old_word_end; /* Restore to region that does fit */
+					wc_word_end = wc_old_word_end; /* Restore to region that does fit */
 					break; /* Stop the loop because we found something that doesn't fit */
 				}
 			}
 
-			s = word_end;
+			wc_s = wc_word_end;
 		}
 
 		/* Append row */
 
-		if (text_iter == row_end) {
+		if (wc_text_iter == wc_row_end) {
 			/* We are on a newline, so append an empty row */
 
 			text_layout->rows = g_list_append (text_layout->rows, NULL);
@@ -1208,19 +1260,18 @@
 
 			/* Next! */
 
-			text_iter = row_end + 1;
+			wc_text_iter = wc_row_end + 1;
 		} else {
 			/* Create subrow and append it to the list */
-
-			int sub_len;
-			sub_len = word_end - text_iter;
-
-			sub_text = g_strndup (text_iter, sub_len);
+			wc_char = *wc_word_end;
+			*wc_word_end = L'\0';
+			sub_text = gdk_wcstombs (wc_text_iter);
+			*wc_word_end = wc_char;
 
 			row = g_new0 (EelTextLayoutRow, 1);
 			row->text = sub_text;
-			row->text_length = sub_len;
-			row->width = eel_scalable_font_text_width (font, font_size, sub_text, sub_len);
+			row->text_length = strlen (sub_text);
+			row->width = eel_scalable_font_text_width (font, font_size, sub_text, row->text_length);
 
 			text_layout->rows = g_list_append (text_layout->rows, row);
 
@@ -1231,10 +1282,10 @@
 
 			/* Next! */
 
-			text_iter = word_end;
+			wc_text_iter = wc_word_end;
 		}
 	}
-
+	g_free (wc_text);
 	return text_layout;
 }
 
# nautilus zh_CN.GB2312.po
# Copyright (C) 2001 Free Software Foundation, Inc.
# James Su <suzhe gnuchina org>, 2001.
#
msgid ""
msgstr ""
"Project-Id-Version: nautilus 1.0.3\n"
"POT-Creation-Date: 2001-05-07 16:13-0700\n"
"PO-Revision-Date: 2001-06-13 17:00+0800\n"
"Last-Translator: James Su <suzhe gnuchina org>\n"
"Language-Team: suzhe <suzhe gnuchina org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=gb2312\n"
"Content-Transfer-Encoding: 8bit\n"

#: eel/eel-dnd.c:494
msgid "_Move here"
msgstr "移动到这里"

#: eel/eel-dnd.c:495
msgid "_Copy here"
msgstr "复制到这里"

#: eel/eel-dnd.c:496
msgid "_Link here"
msgstr "连接到这里"

#: eel/eel-dnd.c:498
msgid "Cancel"
msgstr "放弃"

#: eel/eel-font-picker.c:80
msgid "Normal"
msgstr "正常"

#: eel/eel-font-picker.c:81
msgid "Unknown"
msgstr "未知"

#: eel/eel-font-picker.c:87
msgid "Bold"
msgstr "粗体"

#: eel/eel-font-picker.c:88
msgid "Book"
msgstr "书体"

#: eel/eel-font-picker.c:89
msgid "Black"
msgstr "黑体"

#: eel/eel-font-picker.c:90
msgid "Demibold"
msgstr "半粗体"

#: eel/eel-font-picker.c:91
msgid "Light"
msgstr "细体"

#: eel/eel-font-picker.c:97
msgid "Italic"
msgstr "斜体"

#: eel/eel-font-picker.c:98
msgid "Oblique"
msgstr "倾斜"

#: eel/eel-font-picker.c:99
msgid "Reverse Italic"
msgstr "反斜体"

#: eel/eel-font-picker.c:100
msgid "Reverse Oblique"
msgstr "反倾斜"

#: eel/eel-font-picker.c:101
msgid "Other"
msgstr "其它"

#: eel/eel-font-picker.c:107
msgid "Condensed"
msgstr "缩小的"

#: eel/eel-font-picker.c:108
msgid "Semicondensed"
msgstr "半缩小的"

#: eel/eel-font-picker.c:428
msgid "More..."
msgstr "更多..."

#: eel/eel-gconf-extensions.c:82
#, c-format
msgid ""
"GConf error:\n"
"  %s"
msgstr ""

#: eel/eel-gconf-extensions.c:86
#, c-format
msgid ""
"GConf error:\n"
"  %s\n"
"All further errors shown only on terminal"
msgstr ""

#: eel/eel-gconf-extensions.c:90
msgid "GConf Error"
msgstr ""

#. localizers: These strings are part of the strftime
#. * self-check code and must be changed to match what strtfime
#. * yields -- usually just omitting the AM part is all that's
#. * needed.
#.
#: eel/eel-glib-extensions.c:1475
msgid "01/01/00, 01:00 AM"
msgstr "01/01/00, 01:00 上午"

#: eel/eel-glib-extensions.c:1476
msgid "1/1/00, 1:00 AM"
msgstr "1/1/00, 1:00 上午"

#: eel/eel-glib-extensions.c:1477
msgid " 1/ 1/00,  1:00 AM"
msgstr " 1/ 1/00,  1:00 上午"

#: eel/eel-gnome-extensions.c:711
msgid "No image was selected.  You must click on an image to select it."
msgstr "没有选中图像, 请点击图像以选中它."

#: eel/eel-gnome-extensions.c:712
msgid "No selection made"
msgstr "没有选择"

#: eel/eel-password-dialog.c:267
msgid "Username:"
msgstr "用户明:"

#: eel/eel-password-dialog.c:274
msgid "Password:"
msgstr "口令:"

#: eel/eel-password-dialog.c:304
msgid "Remember this password"
msgstr "记住这个口令"

#: eel/eel-preferences.c:91
msgid "Beginner"
msgstr "初学者"

#: eel/eel-preferences.c:92
msgid "Intermediate"
msgstr "中极用户"

#: eel/eel-preferences.c:93
msgid "Advanced"
msgstr "高级用户"

#: eel/eel-stock-dialogs.c:514 eel/eel-stock-dialogs.c:552
msgid "Info"
msgstr "信息"

#: eel/eel-stock-dialogs.c:554 eel/eel-stock-dialogs.c:607
msgid "Details"
msgstr "详情"

#: eel/eel-stock-dialogs.c:572
msgid "Warning"
msgstr "警告"

#: eel/eel-stock-dialogs.c:583 eel/eel-stock-dialogs.c:605
msgid "Error"
msgstr "错误"

#: eel/eel-stock-dialogs.c:638 eel/eel-stock-dialogs.c:666
msgid "Question"
msgstr "问题"


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]