Skip to content

Commit

Permalink
Add adjustment for text baseline in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
sde1000 committed May 9, 2024
1 parent 7b127ae commit cd63c87
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
7 changes: 6 additions & 1 deletion quicktill/till.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ def add_arguments(parser):
"--pitch-adjust", dest="pitch_adjust", default=0,
action="store", type=int, metavar="PIXELS",
help="Adjust the row height for text")
gtkp.add_argument(
"--baseline-adjust", dest="baseline_adjust", default=0,
action="store", type=int, metavar="PIXELS",
help="Adjust the baseline for text")
parser.set_defaults(command=runtill, nolisten=False)
gtkp.add_argument(
"--geometry", dest="geometry", default=None,
Expand Down Expand Up @@ -298,7 +302,8 @@ def run(args):
monospace_font=args.monospace,
keyboard=args.keyboard,
geometry=args.geometry,
pitch_adjust=args.pitch_adjust)
pitch_adjust=args.pitch_adjust,
baseline_adjust=args.baseline_adjust)
else:
from . import ui_ncurses
ui_ncurses.run()
Expand Down
24 changes: 16 additions & 8 deletions quicktill/ui_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@ class gtk_root(Gtk.DrawingArea):

def __init__(self, monospace_font, font,
preferred_height=24, preferred_width=80,
minimum_height=14, minimum_width=80, pitch_adjust=0):
minimum_height=14, minimum_width=80, pitch_adjust=0,
baseline_adjust=0):
super().__init__()
self.monospace = monospace_font
self.font = font if font else monospace_font
self._pitch_adjust = pitch_adjust
self._baseline_adjust = baseline_adjust
fontmap = PangoCairo.font_map_get_default()
pangoctx = fontmap.create_context()
metrics = pangoctx.get_metrics(self.monospace)
Expand Down Expand Up @@ -197,7 +199,8 @@ def new(self, height, width, y, x, colour=ui.colour_default,
new = text_window(
self, height, width, y, x, colour,
monospace_font=self.monospace, font=self.font,
pitch_adjust=self._pitch_adjust)
pitch_adjust=self._pitch_adjust,
baseline_adjust=self._baseline_adjust)
if always_on_top:
self._ontop.append(new)
else:
Expand Down Expand Up @@ -334,8 +337,10 @@ class text_window(window):
"""
def __init__(self, drawable, height, width, y, x,
colour=ui.colour_default,
monospace_font=None, font=None, pitch_adjust=0):
monospace_font=None, font=None, pitch_adjust=0,
baseline_adjust=0):
# y and x are in pixels, height and width are in characters
self._baseline_adjust = baseline_adjust
self.monospace = monospace_font
self.font = font if font else monospace_font
fontmap = PangoCairo.font_map_get_default()
Expand Down Expand Up @@ -428,7 +433,8 @@ def addstr(self, y, x, text, colour=None):
layout.set_text(text, -1)
layout.set_font_description(self.monospace)
ctx.set_source_rgb(*colours[colour.foreground])
ctx.move_to(x * self.fontwidth, y * self.fontheight)
ctx.move_to(x * self.fontwidth,
y * self.fontheight - self._baseline_adjust)
PangoCairo.show_layout(ctx, layout)

def wrapstr(self, y, x, width, s, colour=None, display=True):
Expand All @@ -449,7 +455,8 @@ def wrapstr(self, y, x, width, s, colour=None, display=True):
width, height = layout.get_pixel_size()
lines = height // self.fontheight
if display:
ctx.move_to(x * self.fontwidth, y * self.fontheight)
ctx.move_to(x * self.fontwidth,
y * self.fontheight - self._baseline_adjust)
PangoCairo.show_layout(ctx, layout)
self.damage(y * self.fontheight, x * self.fontwidth,
height, width)
Expand Down Expand Up @@ -485,7 +492,7 @@ def drawstr(self, y, x, width, s, colour=None, align="<", display=True):
left = (x + width / 2) * self.fontwidth - (lwidth / 2)
else:
left = (x + width) * self.fontwidth - lwidth
ctx.move_to(left, y * self.fontheight)
ctx.move_to(left, y * self.fontheight - self._baseline_adjust)
PangoCairo.show_layout(ctx, layout)
self.damage(y * self.fontheight, left,
lheight, lwidth)
Expand Down Expand Up @@ -582,7 +589,7 @@ def _x_unblank_screen():


def run(fullscreen=False, font="sans 20", monospace_font="monospace 20",
keyboard=False, geometry=None, pitch_adjust=0):
keyboard=False, geometry=None, pitch_adjust=0, baseline_adjust=0):
"""Start running with the GTK display system
"""
if os.getenv('DISPLAY'):
Expand All @@ -593,7 +600,8 @@ def run(fullscreen=False, font="sans 20", monospace_font="monospace 20",
font = Pango.FontDescription(font)
ui.rootwin = gtk_root(monospace_font, font,
preferred_height=20 if keyboard else 24,
minimum_width=60, pitch_adjust=pitch_adjust)
minimum_width=60, pitch_adjust=pitch_adjust,
baseline_adjust=baseline_adjust)
ui.beep = Gdk.beep
if keyboard and tillconfig.keyboard:
keyboard_gtk.init_css()
Expand Down

0 comments on commit cd63c87

Please sign in to comment.