diff --git a/Common.py b/Common.py index 04ce776..4acc59c 100644 --- a/Common.py +++ b/Common.py @@ -15,6 +15,7 @@ # along with this program. If not, see import cairo +import pango import Enum keytypes = Enum.Enum("SIMPLE SPECIAL").vals(illegal=255) @@ -27,10 +28,7 @@ keysegmentslistreverse = list(keysegmentslist) keysegmentslistreverse.reverse() -fontname = "Sans" -fontstyle = cairo.FONT_SLANT_NORMAL -fontweight = cairo.FONT_WEIGHT_NORMAL -fontsize = 12 +font_desc = pango.FontDescription("Sans Bold 12") # You need to have gucharmap installed. # It might be possible to perform drag n drop from the KDE equivalent, diff --git a/DumbKey.py b/DumbKey.py index a146248..837fc00 100644 --- a/DumbKey.py +++ b/DumbKey.py @@ -18,6 +18,7 @@ import gobject import cairo import copy +import pango import Common import KeyValue @@ -75,6 +76,7 @@ def setvalues(self, size = 1, keycode = None, vertical = False, def expose(self, widget, event): self.context = widget.window.cairo_create() + self.layout= self.context.create_layout() # set a clip region for the expose event self.context.rectangle(event.area.x, event.area.y, @@ -177,28 +179,37 @@ def draw_linewh(self, context, x, y, w, h): def draw_character(self, context, char, align, cx, cy, cwidth, cheight): if char == '': return - self.context.select_font_face(Common.fontname, Common.fontstyle, - Common.fontweight) - self.context.set_font_size(Common.fontsize * 1.0) - fascent, fdescent, fheight, fxadvance, fyadvance = self.context.font_extents() - xbearing, ybearing, width, height, xadvance, yadvance = \ - self.context.text_extents(char) + + # set the font and text + self.layout.set_font_description(Common.font_desc) + self.layout.set_text(char) + + # now calculate the position for the text based on the fonts metrics + text_bounds = self.layout.get_pixel_size() + twidth= text_bounds[0] + theight= text_bounds[1] + spacing = theight / 3 + + # TODO: The font-size should be adjusted according to the available + # space. If the rendered text is larger than the available space + # its font-size should be reduced until it fits if align == Common.alignments.CENTRE: - self.context.move_to(cx + cwidth/2 - width/2 - xbearing, - cy + cheight/2 - height/2 - ybearing) + self.context.move_to(cx + cwidth/2 - twidth/2, + cy + cheight/2 - theight/2) elif align == Common.alignments.LEFT: - self.context.move_to(cx + cwidth/16 - xbearing, - cy + cheight - cheight/16 + ybearing) + self.context.move_to(cx + spacing, + cy + cheight - theight - spacing) elif align == Common.alignments.RIGHT: - self.context.move_to(cx + cwidth/2 - width/2 - xbearing, - cy + cheight/2 - height/2 - ybearing) + self.context.move_to(cx + cwidth - twidth - spacing, + cy + cheight - theight - spacing) else: print "Error; unknown alignment" sys.exit(-1) self.context.set_source_rgb(.30, .30, .30) - self.context.show_text(char) + self.context.update_layout(self.layout) + self.context.show_layout(self.layout) def redraw(self): (x,y,width,height) = self.get_allocation() @@ -294,4 +305,4 @@ def extract_display_keyvalues(self): if self.dvalues[counter].getType() == Common.keyvaluetype.NOSYMBOL: self.dvalues_inherited[counter] = False self.dvalues[counter] = copy.copy(self.keyvalues[counter]) - \ No newline at end of file + diff --git a/KeyboardLayoutEditor b/KeyboardLayoutEditor index c82a974..482f1c1 100755 --- a/KeyboardLayoutEditor +++ b/KeyboardLayoutEditor @@ -277,7 +277,7 @@ Please do not put punctuation marks." fontbutton_vbox = gtk.VBox() fontbutton_label = gtk.Label("Select a font") fontbutton_vbox.pack_start(fontbutton_label, expand=False, fill=False) - fontbutton = gtk.FontButton(fontname=Common.fontname + " " + str(Common.fontsize)) + fontbutton = gtk.FontButton(fontname=Common.font_desc.to_string()) fontbutton.set_title('Select a font') fontbutton.connect('font-set', self.font_set_callback) fontbutton_vbox.pack_start(fontbutton, expand=False, fill=False) @@ -900,23 +900,9 @@ Please do not put punctuation marks." def font_set_callback(self, fontbutton): newfont = fontbutton.get_font_name() + font_desc = pango.FontDescription(newfont) + Common.font_desc= font_desc context = self.window.create_pango_context() - for family in context.list_families(): - if newfont.find(family.get_name()) == 0: - face = family.list_faces()[0] - Common.fontname = family.get_name() - Common.fontsize = string.atoi(newfont.rpartition(' ')[-1], 10) - Common.fontstyle = cairo.FONT_SLANT_NORMAL - Common.fontweight = cairo.FONT_WEIGHT_NORMAL - if face.get_face_name() == "Regular": - Common.fontstyle = cairo.FONT_SLANT_NORMAL - if face.get_face_name() == "Bold": - Common.fontweight = cairo.FONT_SLANT_BOLD - if face.get_face_name() == "Italic": - Common.fontstyle = cairo.FONT_SLANT_ITALIC - if face.get_face_name() == "Oblique": - Common.fontstyle = cairo.FONT_SLANT_OBLIQUE - break for keycode in KeyDict.Keys.keys(): KeyDict.Keys[keycode].key.redraw() Common.addtostatusbar('Font set to ' + newfont + '.')