Fontproblems with PyGUI and gtk 2.4.10

Marc Christiansen tolot at jupiter.solar-empire.de
Tue Sep 21 17:29:25 EDT 2004


Hi!

After some installation problems related to PyGUI (well, indirectly -- I
had to install new versions of: freetype, fontconfig, xft2 (from
fcpackage.2_1.tar.gz, because the latest xft2 won't compile with X4.2.0)
(which needed a patch to Xft.h because apparently the #include-style of
freetype did change), GLib, Pango, ATK, GTK+ and pygtk) I ran the
scripts from Tests and there were problems that text was cut off or
textfields were not tall enough (e.g. 09-textfield.py). I did some
digging and found the cause. The problem is in the line

  gdk_font = gdk.font_from_description(self._pango_description)

in Fonts.Font._get_gdk_font(). In gtk 2.4.10 gdk_font_from_description
calls gdk_font_from_description_for_display in gdkfont-x11.c, which
contains the line 

  return gdk_font_load_for_display (display, "fixed");

So regardless what font is wanted, the fixed font gets loaded. The big
problem here is that some widgets use a different font but use
Font.width(), get_height() etc. for layout. Of course this give wrong
values (e.g. get_height() always returns 13, width(s) always 6*len(s)).

It looks as if Fonts.py has to be ported to fully use pango. For
get_ascent, get_descent (btw. the gdk_font based version returns ascent)
and get_height there is code which seems to work, it is just commented
and some small changes must made. But rewriting width() and x_to_pos() to
use pango seems to be not easy. At least I see no obvious way to do it.
But this change really should be made because all the GdkFont functions
are declared obsolete in the gtk+ documentation.

I've attached a patch for two small typos in Fonts.py

--- Fonts.py.orig       Tue Sep 21 23:08:28 2004
+++ Fonts.py    Tue Sep 21 23:12:43 2004
@@ -45,7 +45,7 @@
         return self._pango_description.get_family()
     
     def get_size(self):
-        return self.self._pango_description.get_size()
+        return self._pango_description.get_size()
     
     def get_style(self):
         style = []
@@ -60,7 +60,7 @@
         return self._get_gdk_font().ascent
     
     def get_descent(self):
-        return self._get_gdk_font().ascent
+        return self._get_gdk_font().descent
 
     def get_height(self):
         gdk_font = self._get_gdk_font()

and here one to change get_ascent, get_descent and get_height to pango

--- Fonts.py.orig	Tue Sep 21 23:18:54 2004
+++ Fonts.py	Tue Sep 21 23:15:09 2004
@@ -10,6 +10,7 @@
 class Font(GFont):
 
     _gdk_font = None
+    _pango_metrics = None
 
 #	def _from_gdk_font(cls, gdk_font):
 #		font = cls.__new__(cls)
@@ -56,15 +57,15 @@
             style.append('italic')
         return style
     
-    def get_ascent(self):
-        return self._get_gdk_font().ascent
-    
-    def get_descent(self):
-        return self._get_gdk_font().descent
-
-    def get_height(self):
-        gdk_font = self._get_gdk_font()
-        return gdk_font.ascent + gdk_font.descent
+#    def get_ascent(self):
+#        return self._get_gdk_font().ascent
+#    
+#    def get_descent(self):
+#        return self._get_gdk_font().descent
+#
+#    def get_height(self):
+#        gdk_font = self._get_gdk_font()
+#        return gdk_font.ascent + gdk_font.descent
     
     def _get_gdk_font(self):
         gdk_font = self._gdk_font
@@ -73,26 +74,26 @@
             self._gdk_font = gdk_font
         return gdk_font
 
-#	def get_ascent(self):
-#		pfm = self._get_pango_metrics()
-#		return pfm.ascent
-#	
-#	def get_descent(self):
-#		self._get_pango_metrics()
-#		return self._descent
-#	
-#	def get_height(self):
-#		self._get_pango_metrics()
-#		return self._ascent + self._descent
-#	
-#	def _get_pango_metrics(self):
-#		pfm = self._pango_metrics
-#		if not pfm:
-#			pfm = _pango_context.get_metrics(self._pango_description)
-#			self._pango_metrics = pfm
-#			self._ascent = pfm.ascent / pango.SCALE
-#			self._descent = pfm.descent / pango.SCALE
-#		return pfm
+    def get_ascent(self):
+    	self._get_pango_metrics()
+    	return self._ascent
+    
+    def get_descent(self):
+    	self._get_pango_metrics()
+    	return self._descent
+    
+    def get_height(self):
+    	self._get_pango_metrics()
+    	return self._ascent + self._descent
+    
+    def _get_pango_metrics(self):
+    	pfm = self._pango_metrics
+    	if not pfm:
+    		pfm = _pango_context.get_metrics(self._pango_description)
+    		self._pango_metrics = pfm
+    		self._ascent = pfm.get_ascent() / pango.SCALE
+    		self._descent = pfm.get_descent() / pango.SCALE
+    	return pfm
     
     def width(self, s, start = 0, end = sys.maxint):
         gdk_width = self._get_gdk_font().width



More information about the Python-list mailing list