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

Warn on unavailable fonts, fall back onto GTK default font #118

Merged
merged 2 commits into from
Aug 1, 2023
Merged
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
18 changes: 17 additions & 1 deletion xdot/ui/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#
import math
import operator
import warnings

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('PangoCairo', '1.0')

from gi.repository import GObject
from gi.repository import Gdk
from gi.repository import Gtk
from gi.repository import GdkPixbuf
from gi.repository import Pango
from gi.repository import PangoCairo
Expand Down Expand Up @@ -99,6 +101,7 @@ def get_text(self):
class TextShape(Shape):

LEFT, CENTER, RIGHT = -1, 0, 1
DEFAULT_FONTNAME = Gtk.Settings.get_default().get_property("gtk-font-name")

def __init__(self, pen, x, y, j, w, t):
Shape.__init__(self)
Expand All @@ -109,6 +112,10 @@ def __init__(self, pen, x, y, j, w, t):
self.w = w # width
self.t = t # text

def _font_available(self, fontname, pango_context):
available_fonts = [family.get_name() for family in pango_context.list_families()]
return fontname in available_fonts

def _draw(self, cr, highlight, bounding):

try:
Expand Down Expand Up @@ -157,7 +164,16 @@ def _draw(self, cr, highlight, bounding):
assert success
layout.set_attributes(attrs)

font.set_family(self.pen.fontname)
if self._font_available(self.pen.fontname, pango_context=context):
font.set_family(self.pen.fontname)
else:
msg = "Font family {fontname!r} is not available, using {default!r}".format(
fontname=self.pen.fontname,
default=self.DEFAULT_FONTNAME
)
warnings.warn(msg)
font.set_family(self.DEFAULT_FONTNAME)

font.set_absolute_size(self.pen.fontsize*Pango.SCALE)
layout.set_font_description(font)

Expand Down