Skip to content

Commit

Permalink
Inherit cairo.Surface properties from target in cache
Browse files Browse the repository at this point in the history
Lookup the target window when creating a caching surface
and inherit its properties (font, scaling which fixes #43, fallback
resolution, etc.)
  • Loading branch information
Cimbali committed Nov 28, 2017
1 parent 6231239 commit af8acfe
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pympress/scribble.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def __init__(self, config, builder, notes_mode):
self.scribble_color = Gdk.RGBA()
self.scribble_color.parse(config.get('scribble', 'color'))
self.scribble_width = config.getint('scribble', 'width')
self.cache.add_widget("scribble_p_da", PDF_CONTENT_PAGE if notes_mode else PDF_REGULAR, False)
self.cache.add_widget(self.scribble_p_da, PDF_CONTENT_PAGE if notes_mode else PDF_REGULAR, False)

self.config = config

Expand Down
12 changes: 9 additions & 3 deletions pympress/surfacecache.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ class SurfaceCache(object):
#: popped from the start of the cache.
surface_cache = {}

#: `dict` containing functions that return a :class:`~cairo.Surface` given a :class:`~cairo.Context`, width `int` and height `int`
#: see :func:`~cairo.Surface.create_similar`
surface_factory = {}

#: Size of the different managed widgets, as a `dict` of tuples
surface_size = {}

Expand Down Expand Up @@ -104,21 +108,23 @@ def __init__(self, doc, max_pages):
self.doc_lock = threading.Lock()


def add_widget(self, widget_name, wtype, start_enabled = True):
def add_widget(self, widget, wtype, start_enabled = True):
""" Add a widget to the list of widgets that have to be managed (for caching and prerendering).
This creates new entries for ``widget_name`` in the needed internal data
structures, and creates a new thread for prerendering pages for this widget.
Args:
widget_name (`str`): string used to identify a widget
widget (:class:`~Gtk.Widget`): The widget for which we need to cache
wtype (`int`): type of document handled by the widget (see :attr:`surface_type`)
start_enabled (`bool`): whether this widget is initially in the list of widgets to prerender
"""
widget_name = widget.get_name()
self.surface_cache[widget_name] = OrderedDict()
self.surface_size[widget_name] = (-1, -1)
self.surface_type[widget_name] = wtype
self.locks[widget_name] = threading.Lock()
self.surface_factory[widget_name] = lambda c, w, h: widget.get_window().create_similar_surface(c, w, h)
if start_enabled:
self.enable_prerender(widget_name)

Expand Down Expand Up @@ -279,7 +285,7 @@ def renderer(self, widget_name, page_nb):
# Render to a ImageSurface
# 32 to support alpha (needed with premultiplied values?)
# Anyway 24 uses 32-bit values with 8 unused
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, ww, wh)
surface = self.surface_factory[widget_name](cairo.CONTENT_COLOR, ww, wh)
context = cairo.Context(surface)
page.render_cairo(context, ww, wh, wtype)
del context
Expand Down
14 changes: 7 additions & 7 deletions pympress/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ def make_cwin(self):
else:
page_type = document.PDF_REGULAR

self.cache.add_widget("c_da", page_type)
self.cache.add_widget(self.c_da, page_type)
self.c_frame.set_property("ratio", self.doc.current_page().get_aspect_ratio(page_type))


Expand Down Expand Up @@ -278,13 +278,13 @@ def make_pwin(self):
self.get_object(n).set_active(init_checkstates[n])

if self.notes_mode:
self.cache.add_widget("p_da_cur", PDF_CONTENT_PAGE)
self.cache.add_widget("p_da_next", PDF_CONTENT_PAGE)
self.cache.add_widget("p_da_notes", PDF_NOTES_PAGE)
self.cache.add_widget(self.p_da_cur, PDF_CONTENT_PAGE)
self.cache.add_widget(self.p_da_next, PDF_CONTENT_PAGE)
self.cache.add_widget(self.p_da_notes, PDF_NOTES_PAGE)
else:
self.cache.add_widget("p_da_cur", PDF_REGULAR)
self.cache.add_widget("p_da_next", PDF_REGULAR)
self.cache.add_widget("p_da_notes", PDF_REGULAR, False)
self.cache.add_widget(self.p_da_cur, PDF_REGULAR)
self.cache.add_widget(self.p_da_next, PDF_REGULAR)
self.cache.add_widget(self.p_da_notes, PDF_REGULAR, False)


# set default value
Expand Down

0 comments on commit af8acfe

Please sign in to comment.