From c23d8f3c10611593f5712c74c3e7325e88f6ff5b Mon Sep 17 00:00:00 2001 From: Christian Franke Date: Wed, 20 Nov 2013 14:53:18 +0100 Subject: Render directly, without intermedia surfaces --- read_layers.py | 117 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 63 insertions(+), 54 deletions(-) diff --git a/read_layers.py b/read_layers.py index 15675b3..de1bd47 100644 --- a/read_layers.py +++ b/read_layers.py @@ -24,90 +24,98 @@ def makedirs(dirs): pass os.makedirs(dirs) + class Layer(object): - def __init__(self, name, surface): - self.name = name - self._surface = surface + def __init__(self, info, path): + self.name = info.get('name', None) + if self.name is None: + self.name = os.path.splitext(os.path.basename(path))[0] + + self._scale = info.get('scale', 1.0) + self._x_offset = info.get('x-offset', 0.0) + self._y_offset = info.get('y-offset', 0.0) + + self._load_file(path) @property def width(self): - return self._surface.get_width() + return self._x_offset + (self._orig_width * self._scale) @property def height(self): - return self._surface.get_height() + return self._y_offset + (self._orig_height * self._scale) def draw(self, context): - context.set_source_surface(self._surface) - context.paint() + context.transform(cairo.Matrix(x0=self._x_offset, y0=self._y_offset)) + context.transform(cairo.Matrix(xx=self._scale, yy=self._scale)) -class LayerReader(object): - def __init__(self, layer_type=None): - self.layer_type = Layer if layer_type is None else layer_type + def _load_file(self, path): + raise NotImplementedError - def read(self, path): - info_file = '{0}.txt'.format(path) - if os.path.exists(info_file): - with open(info_file, 'r') as f: - info = yaml.safe_load(f) - else: - info = {} - if path.endswith('.pdf'): - from gi.repository import Poppler - document = Poppler.Document.new_from_file('file://{0}'.format(path), None) - page = document.get_page(0) - orig_width, orig_height = page.get_size() +class PdfLayer(Layer): + def _load_file(self, path): + from gi.repository import Poppler + document = Poppler.Document.new_from_file('file://{0}'.format(path), None) + self._page = document.get_page(0) - def draw_layer(context): - page.render(context) - elif path.endswith('.png'): - image = cairo.ImageSurface.create_from_png(path) - orig_width, orig_height = image.get_width(), image.get_height() + @property + def _orig_width(self): + return self._page.get_size()[0] - def draw_layer(context): - context.set_source_surface(image) - context.paint() - else: - raise RuntimeError("Unsupported Format for '{0}'".format(path)) + @property + def _orig_height(self): + return self._page.get_size()[1] - layer_name = info.get('name', None) - if layer_name is None: - layer_name = os.path.splitext(os.path.basename(path))[0] + def draw(self, context): + super(PdfLayer, self).draw(context) + self._page.render(context) - scale = info.get('scale', 1.0) - x_offset = info.get('x-offset', 0.0) - y_offset = info.get('y-offset', 0.0) - width = int(orig_width * scale + x_offset) - height = int(orig_height * scale + y_offset) +class PngLayer(Layer): + def _load_file(self, path): + self._image = cairo.ImageSurface.create_from_png(path) - layer_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height) - layer_context = cairo.Context(layer_surface) + @property + def _orig_width(self): + return self._image.get_width() - # Init surface to white - layer_context.set_source_rgba(1.0, 1.0, 1.0, 1.0) - layer_context.paint() + @property + def _orig_height(self): + return self._image.get_height() - layer_context.translate(x_offset, y_offset) - layer_context.scale(scale, scale) + def draw(self, context): + super(PngLayer, self).draw(context) + context.set_source_surface(self._image) + context.paint() - draw_layer(layer_context) - return self.layer_type(layer_name, layer_surface) class LayerLoader(object): - def __init__(self, reader=None): - self.reader = LayerReader() if reader is None else reader - def load(self, layer_path): layers = [] for layer_file in sorted(os.listdir(layer_path)): if layer_file.endswith('.txt'): continue layer_file = os.path.join(layer_path, layer_file) - layers.append(self.reader.read(layer_file)) + layers.append(self.read(layer_file)) return layers + def read(self, path): + info_file = '{0}.txt'.format(path) + if os.path.exists(info_file): + with open(info_file, 'r') as f: + info = yaml.safe_load(f) + else: + info = {} + + if path.endswith('.pdf'): + return PdfLayer(info, path) + elif path.endswith('.png'): + return PngLayer(info, path) + else: + raise RuntimeError("Unsupported Format for '{0}'".format(path)) + + class TileGenerator(object): def __init__(self, layer, width=None, height=None, tile_size=None, scale=0): self.layer = layer @@ -156,14 +164,15 @@ class TileGenerator(object): tile_path = os.path.join(path, str(max_zoom_level - zoom_level), str(x), '{0}.png'.format(y)) tile_transform = column_transform * cairo.Matrix(y0=-self.tile_size * y) tile_context.set_matrix(tile_transform) - print "Matrix for %s is %r" % (tile_path, tile_context.get_matrix()) tile_context.set_source_rgba(1.0, 1.0, 1.0, 1.0) tile_context.paint() layer.draw(tile_context) + tile_surface.write_to_png(tile_path) + if __name__ == '__main__': layer_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'layers') tiles_path = os.path.join(os.path.abspath(os.path.dirname(__file__)), 'tiles') -- cgit v1.2.1