From 3e40a0ef9a32c75e9007774fd630a9afec7bb7b3 Mon Sep 17 00:00:00 2001 From: Marco Herrn Date: Wed, 5 Dec 2018 23:20:28 +0100 Subject: [PATCH 1/3] Correctly read files that include a 'replace' directive - Closes #23 - This commit only fixes the Exception occurring when reading a file that contains a 'replace' directive. However it does nothing to actually process it! Therefore another commit is necessary to actually handle 'replace' (in addition to 'override'). --- XKBGrammar.g | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/XKBGrammar.g b/XKBGrammar.g index a7e3e78..ebb1f5e 100644 --- a/XKBGrammar.g +++ b/XKBGrammar.g @@ -38,6 +38,7 @@ tokens ELEM_VIRTUALMODS; KEYELEMENTS; OVERRIDE; + REPLACE; OVERLAY; } @@ -97,7 +98,7 @@ line_keytype ; line_key - : OVERRIDE? 'key' '<' NAME '>' '{' keyelements (',' keyelements)* '}' + : (OVERRIDE|REPLACE)? 'key' '<' NAME '>' '{' keyelements (',' keyelements)* '}' -> ^(TOKEN_KEY OVERRIDE? ^(KEYCODEX NAME) keyelements+) ; @@ -120,6 +121,10 @@ override : 'override' ; +replace + : 'replace' + ; + keyelements : elem_keysyms | elem_keysymgroup @@ -174,6 +179,10 @@ OVERRIDE : 'override' ; +REPLACE + : 'replace' + ; + NAME : ( 'a'..'z' | 'A'..'Z' | '_' | '0'..'9' | '+' | '-' )* ; From 4c30e7ae4a33d409a4ade81d0e154c36f4e63bc0 Mon Sep 17 00:00:00 2001 From: Marco Herrn Date: Sat, 8 Dec 2018 22:12:17 +0100 Subject: [PATCH 2/3] Use Pango for font handling - Closes #25 - Respects font style of selected font - Better placement of texts by taking pango font metrics into account - Use a bold font as default --- Common.py | 6 ++---- DumbKey.py | 39 +++++++++++++++++++++++++-------------- KeyboardLayoutEditor | 20 +++----------------- 3 files changed, 30 insertions(+), 35 deletions(-) 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 + '.') From ce333a36108a3127536990388106af5e5724bc50 Mon Sep 17 00:00:00 2001 From: Marco Herrn Date: Sat, 8 Dec 2018 22:12:52 +0100 Subject: [PATCH 3/3] Change default size of window - Larget default window size to have all texts fit by default --- KeyboardLayoutEditor | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/KeyboardLayoutEditor b/KeyboardLayoutEditor index 482f1c1..933016f 100755 --- a/KeyboardLayoutEditor +++ b/KeyboardLayoutEditor @@ -100,7 +100,7 @@ class Controller_KeyboardLayoutEditor: # Set initial size (width, height) = self.window.get_size() - self.window.resize(800, 550) + self.window.resize(1350, 700) self.window.connect("check_resize", self.check_resize)