Commit 8e7bfdb0 authored by David Boddie's avatar David Boddie 💬
Browse files

Use correct PDF file, handle compressed PDFs, use space efficiently

parent 4c3db8cf
......@@ -11,32 +11,36 @@
import cairo
import gettext
import gi
import glob
import gzip
import os
import sys
gi.require_version('Gtk', '3.0')
gi.require_version('Poppler', '0.18')
from gi.repository import GLib, Gtk, Poppler
from gi.repository import GLib, Gio, Gtk, Poppler
# Placeholder to be replaced by the build system
localedir = '@localedir@'
prefix = '@prefix@'
document_pattern = os.path.join(prefix, 'share', 'doc',
'librem5-quick-start-guide',
'Librem5-Quick-Start.pdf*')
class Application(Gtk.Application):
def __init__(self):
def __init__(self, doc):
super().__init__(application_id='sm.puri.QuickStartViewer')
GLib.set_application_name(_('Quick Start Guide'))
GLib.set_prgname('sm.puri.QuickStartViewer')
self.doc = doc
self.index = 0
def do_startup(self):
Gtk.Application.do_startup(self)
self.doc = Poppler.Document.new_from_file('file:///home/david/Repos/quickstart/Librem5-Quick-Start.pdf')
#os.path.join(prefix, 'doc', 'librem5-quick-start',
# 'Librem5-Quick-Start.pdf'))
self.font_options = cairo.FontOptions()
self.font_options.set_antialias(cairo.ANTIALIAS_BEST)
......@@ -51,9 +55,6 @@ class Application(Gtk.Application):
self.previous_button = Gtk.Button.new_from_icon_name('go-previous-symbolic', Gtk.IconSize.MENU)
self.next_button = Gtk.Button.new_from_icon_name('go-next-symbolic', Gtk.IconSize.MENU)
self.previous_button.connect('clicked', self.go_previous)
self.next_button.connect('clicked', self.go_next)
header.add(self.previous_button)
header.add(self.next_button)
......@@ -61,27 +62,31 @@ class Application(Gtk.Application):
# Main content area
scroller = Gtk.ScrolledWindow(
self.scroller = Gtk.ScrolledWindow(
hexpand=True,
kinetic_scrolling=True,
vexpand=True
)
self.page_widget = Gtk.Image()
scroller.add(self.page_widget)
self.scroller.add(self.page_widget)
window.add(scroller)
self.previous_button.connect('clicked', self.go_previous)
self.next_button.connect('clicked', self.go_next)
window.connect('size-allocate', self.draw_page)
window.add(self.scroller)
window.set_default_size(360, 720)
window.show_all()
self.draw_page()
self.update_buttons()
def draw_page(self):
def draw_page(self, *args):
page = self.doc.get_page(self.index)
sw, sh = 360, 640
rect = self.scroller.get_allocation()
sw, sh = rect.width, rect.height
surface = cairo.ImageSurface(cairo.FORMAT_RGB24, sw, sh)
context = cairo.Context(surface)
......@@ -125,8 +130,23 @@ class Application(Gtk.Application):
if __name__ == '__main__':
for path in glob.glob(document_pattern):
if path.endswith('.gz'):
data = gzip.GzipFile(path).read()
break
elif path.endswith('.pdf'):
data = open(path, 'rb').read()
break
else:
sys.stderr.write('Failed to find the Quick Start Guide.\n')
sys.exit(1)
# https://stackoverflow.com/questions/42735374/has-anyone-been-able-to-use-poppler-new-from-data-in-python
input_stream = Gio.MemoryInputStream.new_from_data(data)
doc = Poppler.Document.new_from_stream(input_stream, -1)
# Install a translator for Python code to use throughout the application.
gettext.install('librem5-quick-start', localedir, names=['ngettext'])
app = Application()
app = Application(doc)
sys.exit(app.run(sys.argv))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment