[PATCH][RFC] Improve icon view line breaks



The attached patch is meant to improve icon view line breaks by looking
for '.', '_' and CamelCase characters, and inserting pango hints that it
may preferably break at a particular position (i.e. treat it as end of
word). It is meant to fix bug 300790 [1], while the more interesting
discussion was going on in bug 323711 [2] between Behdad and myself.
caveats:
 - Some developers might not like breaking after CamelCase. It looks
more aesthetically, but makes FooFooBar indistinguishable from FooFoo
Bar if we break between the two words. Comments appreciated
 - The code tries to be smart by only breaking after '.' and '_' if no
digit follows to not break version information. It seems to work quiet
well, but needs some testing
 - The layout is changed when editing a filename that had a manually
inserted line break. This won't be fixed until [2] is properly resolved

[1] http://bugzilla.gnome.org/show_bug.cgi?id=300790
[2] http://bugzilla.gnome.org/show_bug.cgi?id=323711

-- 
Christian Neumair <chris gnome-de org>
Index: libnautilus-private/nautilus-icon-canvas-item.c
===================================================================
RCS file: /cvs/gnome/nautilus/libnautilus-private/nautilus-icon-canvas-item.c,v
retrieving revision 1.195
diff -u -p -r1.195 nautilus-icon-canvas-item.c
--- libnautilus-private/nautilus-icon-canvas-item.c	14 Nov 2005 15:49:58 -0000	1.195
+++ libnautilus-private/nautilus-icon-canvas-item.c	11 Dec 2005 21:01:42 -0000
@@ -1588,6 +1588,8 @@ nautilus_icon_canvas_item_draw (EelCanva
 	draw_label_text (icon_item, drawable, FALSE, icon_rect);
 }
 
+#define ZERO_WIDTH_SPACE "\xE2\x80\x8B"
+
 static PangoLayout *
 create_label_layout (NautilusIconCanvasItem *item,
 		     const char *text)
@@ -1597,6 +1599,9 @@ create_label_layout (NautilusIconCanvasI
 	PangoFontDescription *desc;
 	NautilusIconContainer *container;
 	EelCanvasItem *canvas_item;
+	char *zeroified_text;
+	const char *p;
+	char *q;
 
 	canvas_item = EEL_CANVAS_ITEM (item);
 
@@ -1604,7 +1609,29 @@ create_label_layout (NautilusIconCanvasI
 	context = gtk_widget_get_pango_context (GTK_WIDGET (canvas_item->canvas));
 	layout = pango_layout_new (context);
 
-	pango_layout_set_text (layout, text, -1);
+	if (text == NULL) {
+		zeroified_text = NULL;
+	} else {
+		zeroified_text = g_malloc ((1 + strlen (ZERO_WIDTH_SPACE)) * (strlen (text) + 1));
+		q = zeroified_text;
+
+		for (p = text; *p != '\0'; p++) {
+			*(q++) = *p;
+
+			if (((*p == '_' || *p == '.') && !g_ascii_isdigit(*(p+1))) ||
+			    (g_ascii_islower (*p) && g_ascii_isupper (*(p+1)))) {
+				/* Ensure that we allow to break after '_' or '.' characters,
+				 * if they are not likely to be part of a version information, to
+				 * not break wrapping of foobar-0.0.1.
+				 * Also allow wrapping for CamelCase filenames. */
+				strcpy (q, ZERO_WIDTH_SPACE);
+				q += strlen (ZERO_WIDTH_SPACE);
+			}
+		}
+		*q = '\0';
+	}
+
+	pango_layout_set_text (layout, zeroified_text, -1);
 	pango_layout_set_width (layout, floor (nautilus_icon_canvas_item_get_max_text_width (item)) * PANGO_SCALE);
 			
 	if (container->details->label_position == NAUTILUS_ICON_LABEL_POSITION_BESIDE) {
@@ -1627,6 +1654,7 @@ create_label_layout (NautilusIconCanvasI
 	}
 	pango_layout_set_font_description (layout, desc);
 	pango_font_description_free (desc);
+	g_free (zeroified_text);
 	
 	return layout;
 }

Attachment: signature.asc
Description: This is a digitally signed message part



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