Skip to content

Commit

Permalink
Use Pango for font handling
Browse files Browse the repository at this point in the history
- Closes simos#25
- Respects font style of selected font
- Better placement of texts by taking pango font metrics into account
- Use a bold font as default
  • Loading branch information
hupfdule committed Dec 8, 2018
1 parent 3e40a0e commit 4c30e7a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
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])

20 changes: 3 additions & 17 deletions KeyboardLayoutEditor
Original file line number Diff line number Diff line change
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

0 comments on commit 4c30e7a

Please sign in to comment.