Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly read files that include a 'replace' directive #24

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>

import cairo
import pango
import Enum

keytypes = Enum.Enum("SIMPLE SPECIAL").vals(illegal=255)
Expand All @@ -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,
Expand Down
39 changes: 25 additions & 14 deletions DumbKey.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import gobject
import cairo
import copy
import pango

import Common
import KeyValue
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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])

12 changes: 6 additions & 6 deletions KeyDict.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ def set(self):
"AE10": Key.Key(1, "AE10", False, Common.keytypes.SIMPLE),
"AE11": Key.Key(1, "AE11", False, Common.keytypes.SIMPLE),
"AE12": Key.Key(1, "AE12", False, Common.keytypes.SIMPLE),
"BKSP": Key.Key(2, "BKSP", False, Common.keytypes.SPECIAL,'Backspace'),
"BKSP": Key.Key(2, "BKSP", False, Common.keytypes.SPECIAL,'Backspace'),

"TAB" : Key.Key(1.8, "TAB", False, Common.keytypes.SPECIAL, 'Tab', 'ISO_Left'),
"TAB" : Key.Key(1.8, "TAB", False, Common.keytypes.SPECIAL, 'Tab', 'ISO_Left'),
"AD01": Key.Key(1, "AD01", False, Common.keytypes.SIMPLE),
"AD02": Key.Key(1, "AD02", False, Common.keytypes.SIMPLE),
"AD03": Key.Key(1, "AD03", False, Common.keytypes.SIMPLE),
Expand All @@ -72,7 +72,7 @@ def set(self):
"AD12": Key.Key(1, "AD12", False, Common.keytypes.SIMPLE),
"BKSL": Key.Key(1, "BKSL", False, Common.keytypes.SIMPLE),

"CAPS": Key.Key(2, "CAPS", False, Common.keytypes.SPECIAL, 'Caps Lock'),
"CAPS": Key.Key(2, "CAPS", False, Common.keytypes.SPECIAL, 'Caps Lock'),
"AC01": Key.Key(1, "AC01", False, Common.keytypes.SIMPLE),
"AC02": Key.Key(1, "AC02", False, Common.keytypes.SIMPLE),
"AC03": Key.Key(1, "AC03", False, Common.keytypes.SIMPLE),
Expand All @@ -84,9 +84,9 @@ def set(self):
"AC09": Key.Key(1, "AC09", False, Common.keytypes.SIMPLE),
"AC10": Key.Key(1, "AC10", False, Common.keytypes.SIMPLE),
"AC11": Key.Key(1, "AC11", False,Common.keytypes.SIMPLE),
"RTRN": Key.Key(2, "RTRN", False, Common.keytypes.SPECIAL, 'Return'),
"RTRN": Key.Key(2, "RTRN", False, Common.keytypes.SPECIAL, 'Return'),

"LFSH": Key.Key(1, "LFSH", False, Common.keytypes.SPECIAL,'Shift L', 'ISO Prev'),
"LFSH": Key.Key(1, "LFSH", False, Common.keytypes.SPECIAL,'Shift L', 'ISO Prev'),
"LSGT": Key.Key(1, "LSGT", False, Common.keytypes.SIMPLE),
"AB01": Key.Key(1, "AB01", False, Common.keytypes.SIMPLE),
"AB02": Key.Key(1, "AB02", False, Common.keytypes.SIMPLE),
Expand All @@ -98,7 +98,7 @@ def set(self):
"AB08": Key.Key(1, "AB08", False, Common.keytypes.SIMPLE),
"AB09": Key.Key(1, "AB09", False, Common.keytypes.SIMPLE),
"AB10": Key.Key(1, "AB10", False, Common.keytypes.SIMPLE),
"RTSH": Key.Key(2.5, "RTSH", False, Common.keytypes.SPECIAL, 'Shift R', 'ISO Next'),
"RTSH": Key.Key(2.5, "RTSH", False, Common.keytypes.SPECIAL, 'Shift R', 'ISO Next'),

"LCTL": Key.Key(1.8, "LCTL", False, Common.keytypes.SPECIAL, 'Control L'),
"LWIN": Key.Key(1.5, "LWIN", False, Common.keytypes.SPECIAL, 'Super L'),
Expand Down
22 changes: 4 additions & 18 deletions KeyboardLayoutEditor
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 + '.')
Expand Down
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@

PyGTK program that helps create or edit XKB keyboard layouts. Created by [@simos](https://github.com/simos) in Google Summer of Code 2008. [More information](http://simos.info/blog/archives/747/)

![Screenshot](http://simos.info/blog/wp-content/uploads/2008/10/kle-intro.png)
![Screenshot](screenshot.png)

## Changes to upstream

This is not the upstream source. It's a fork to fix some bugs that are not
incorporated into upstream yet. If you are searching for the non-patched
upstream see http://github.com/simos/keyboardlayouteditor.

The following bugs are fixed in this fork:

- [Don't throw an exception on a `replace` directive](http://github.com/simos/keyboardlayouteditor/issues/24)
- [Respect selected font style](http://github.com/simos/keyboardlayouteditor/issues/25)

Additional changes:

- Print symbol next to special keys names (for Backspace ⌫, Caps Lock ⇩, Return ↵, Shift ⇧, Tab ↹)


## Requirements

Expand Down
11 changes: 10 additions & 1 deletion XKBGrammar.g
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ tokens
ELEM_VIRTUALMODS;
KEYELEMENTS;
OVERRIDE;
REPLACE;
OVERLAY;
}

Expand Down Expand Up @@ -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+)
;

Expand All @@ -120,6 +121,10 @@ override
: 'override'
;

replace
: 'replace'
;

keyelements
: elem_keysyms
| elem_keysymgroup
Expand Down Expand Up @@ -174,6 +179,10 @@ OVERRIDE
: 'override'
;

REPLACE
: 'replace'
;

NAME
: ( 'a'..'z' | 'A'..'Z' | '_' | '0'..'9' | '+' | '-' )*
;
Expand Down
Binary file added screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.