DriverSDL: system font lookup.
authorRadek Brich <radek.brich@devl.cz>
Sun, 06 Jan 2013 23:03:24 +0100
changeset 54 86b84535726e
parent 53 c4263588b716
child 55 1ab0edd5d784
DriverSDL: system font lookup.
sdlterm/cython/sdlterm.pyx
sdlterm/src/sdlterm.cc
sdlterm/src/sdlterm.h
tuikit/driver_sdl.py
--- a/sdlterm/cython/sdlterm.pyx	Sun Jan 06 13:55:02 2013 +0100
+++ b/sdlterm/cython/sdlterm.pyx	Sun Jan 06 23:03:24 2013 +0100
@@ -63,9 +63,7 @@
     def __dealloc__(self):
         del self.thisptr
 
-    def select_font(self, fname_regular, fname_bold, ptsize):
-        fname_regular = fname_regular.encode('utf8')
-        fname_bold = fname_bold.encode('utf8')
+    def select_font(self, bytes fname_regular, bytes fname_bold, int ptsize):
         self.thisptr.select_font(fname_regular, fname_bold, ptsize)
     def resize(self, width, height):
         self.thisptr.resize(width, height)
--- a/sdlterm/src/sdlterm.cc	Sun Jan 06 13:55:02 2013 +0100
+++ b/sdlterm/src/sdlterm.cc	Sun Jan 06 23:03:24 2013 +0100
@@ -4,7 +4,7 @@
 #include <algorithm>
 
 
-void ColorMap::index_to_rgb(int index, SDL_Color &color)
+void ColorMap::index_to_rgb(int index, SDL_Color &color) const
 {
     color.r = _map[index][0];
     color.g = _map[index][1];
--- a/sdlterm/src/sdlterm.h	Sun Jan 06 13:55:02 2013 +0100
+++ b/sdlterm/src/sdlterm.h	Sun Jan 06 23:03:24 2013 +0100
@@ -7,10 +7,12 @@
 
 namespace Style
 {
-    const int BOLD      = 1 << 0;  // bold font
-    const int UNDERLINE = 1 << 1;  // underline text
-    const int STANDOUT  = 1 << 2;  // inverse bg/fg
-    const int BLINK     = 1 << 3;  // blinking
+    enum {
+        BOLD      = 1 << 0,  // bold font
+        UNDERLINE = 1 << 1,  // underline text
+        STANDOUT  = 1 << 2,  // inverse bg/fg
+        BLINK     = 1 << 3,  // blinking
+    };
 };
 
 
@@ -54,6 +56,14 @@
 };
 
 
+/* TTF font glyph renderer
+ *
+ * Renders uniformly sized cells with single character.
+ * Wraps SDL_ttf functions, see its documentation here:
+ *
+ * http://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html
+ *
+ */
 class GlyphRenderer
 {
 public:
--- a/tuikit/driver_sdl.py	Sun Jan 06 13:55:02 2013 +0100
+++ b/tuikit/driver_sdl.py	Sun Jan 06 23:03:24 2013 +0100
@@ -9,6 +9,7 @@
 from tuikit.driver import Driver
 
 import logging
+import subprocess
 
 import sys
 sys.path.insert(0, 'sdlterm/build/lib.linux-x86_64-3.2')
@@ -56,8 +57,8 @@
 
     def start(self, mainfunc):
         '''Start driver and run mainfunc.'''
+        self.select_system_font()
         self.sdlterm.resize(800, 600)
-        self.sdlterm.select_font("sdlterm/font/DejaVuSansMono.ttf", "sdlterm/font/DejaVuSansMono-Bold.ttf", 12)
         self.size.w, self.size.h = self.sdlterm.width, self.sdlterm.height
         mainfunc()
 
@@ -149,6 +150,36 @@
         '''Hide cursor.'''
         self.sdlterm.hide_cursor()
 
+    ## private utility ##
+
+    def _lookup_system_font(self, family, style):
+        """Find system font by family name and style.
+
+        style -- font style: 'normal', 'bold'
+
+        Returns font file name.
+
+        Uses fontconfig, it must be installed in system.
+
+        """
+        return subprocess.check_output([
+            'fc-match',
+            '%s:style=%s:fontformat=truetype:spacing=mono' % (family, style),
+            '-f%{file}'])
+
+    def select_system_font(self, family='monospace'):
+        """Search for system fonts by family name and use them.
+
+        Example family names:
+            'monospace' - default monospace system font
+            'DejaVu Sans Mono'
+            'Liberation Mono'
+        """
+        fname_regular = self._lookup_system_font(family, 'normal')
+        fname_bold = self._lookup_system_font(family, 'bold')
+        self.log.info('DriverSDL: using fonts:\n%s\n%s', fname_regular.decode(), fname_bold.decode())
+        self.sdlterm.select_font(fname_regular, fname_bold, 12)
+
 
 driverclass = DriverSDL