[Python-modules-commits] [cairosvg] 02/05: Import cairosvg_1.0.19.orig.tar.gz

Michael Fladischer fladi at moszumanska.debian.org
Wed Nov 4 12:01:40 UTC 2015


This is an automated email from the git hooks/post-receive script.

fladi pushed a commit to branch master
in repository cairosvg.

commit 18ec9e344d046baf51967e048737468b2703c84d
Author: Michael Fladischer <fladi at debian.org>
Date:   Wed Nov 4 12:51:46 2015 +0100

    Import cairosvg_1.0.19.orig.tar.gz
---
 NEWS.rst                     | 12 +++++++++++
 PKG-INFO                     |  2 +-
 cairosvg/__init__.py         |  2 +-
 cairosvg/parser.py           | 50 ++++++++++++++++++++++----------------------
 cairosvg/surface/__init__.py | 26 ++++++++++++++---------
 cairosvg/surface/defs.py     | 24 ++++++++++++---------
 6 files changed, 69 insertions(+), 47 deletions(-)

diff --git a/NEWS.rst b/NEWS.rst
index f9286a6..738dc0c 100644
--- a/NEWS.rst
+++ b/NEWS.rst
@@ -11,6 +11,18 @@ Version 2.0, not released yet
 * Test CairoSVG with Travis
 
 
+Version 1.0.19, released on 2015-10-30
+======================================
+
+* Drastically improve the performance of ``Node()``
+
+
+Version 1.0.18, released on 2015-10-20
+======================================
+
+* Use cairo groups to apply filters
+
+
 Version 1.0.17, released on 2015-10-09
 ======================================
 
diff --git a/PKG-INFO b/PKG-INFO
index 1535558..8c86898 100644
--- a/PKG-INFO
+++ b/PKG-INFO
@@ -1,6 +1,6 @@
 Metadata-Version: 1.1
 Name: CairoSVG
-Version: 1.0.17
+Version: 1.0.19
 Summary: A Simple SVG Converter for Cairo
 Home-page: http://www.cairosvg.org/
 Author: Kozea
diff --git a/cairosvg/__init__.py b/cairosvg/__init__.py
index 2213036..e15e61b 100644
--- a/cairosvg/__init__.py
+++ b/cairosvg/__init__.py
@@ -27,7 +27,7 @@ import optparse
 from . import surface
 
 
-VERSION = '1.0.17'
+VERSION = '1.0.19'
 SURFACES = {
     'SVG': surface.SVGSurface,  # Tell us if you actually use this one!
     'PNG': surface.PNGSurface,
diff --git a/cairosvg/parser.py b/cairosvg/parser.py
index c5e3630..70f5a3d 100644
--- a/cairosvg/parser.py
+++ b/cairosvg/parser.py
@@ -46,7 +46,7 @@ except ImportError:
 
 import re
 import gzip
-import uuid
+import random
 import os.path
 
 from .css import apply_stylesheets
@@ -94,12 +94,19 @@ def handle_white_spaces(string, preserve):
         string = re.sub(" +", " ", string)
     return string
 
+NOT_INHERITED_ATTRS = frozenset([
+    "transform", "opacity", "style", "viewBox", "stop-color",
+    "stop-opacity", "width", "height", "filter", "mask", "rotate",
+    "{http://www.w3.org/1999/xlink}href", "id", "x", "y",
+    "overflow", "clip", "clip-path",
+])
 
 class Node(dict):
     """SVG node with dict-like properties and children."""
-    def __init__(self, node, parent=None, parent_children=False, url=None):
+
+    def __init__(self, node, parent=None, parent_children=False, url=None,
+                __not_inherited_attrs=NOT_INHERITED_ATTRS):
         """Create the Node from ElementTree ``node``, with ``parent`` Node."""
-        super(Node, self).__init__()
         self.children = ()
 
         self.root = False
@@ -109,35 +116,29 @@ class Node(dict):
 
         # Inherits from parent properties
         if parent is not None:
-            items = parent.copy()
-            not_inherited = (
-                "transform", "opacity", "style", "viewBox", "stop-color",
-                "stop-opacity", "width", "height", "filter", "mask", "rotate",
-                "{http://www.w3.org/1999/xlink}href", "id", "x", "y",
-                "overflow", "clip", "clip-path")
-            for attribute in not_inherited:
-                if attribute in items:
-                    del items[attribute]
-
-            self.update(items)
+            self.update([
+                (a, parent[a]) for a in parent
+                if a not in __not_inherited_attrs
+            ])
             self.url = url or parent.url
             self.parent = parent
         else:
             self.url = getattr(self, "url", None)
             self.parent = getattr(self, "parent", None)
 
-        self.update(dict(self.node.attrib.items()))
+        self.update(self.node.attrib)
 
         # Give an id for nodes that don't have one
         if "id" not in self:
-            self["id"] = uuid.uuid4().hex
+            self["id"] = "node_%s_%x" %(id(self), random.getrandbits(32))
 
         # Handle the CSS
         style = self.pop("_style", "") + ";" + self.pop("style", "").lower()
         for declaration in style.split(";"):
-            if ":" in declaration:
-                name, value = declaration.split(":", 1)
-                self[name.strip()] = value.strip()
+            name, colon, value = declaration.partition(":")
+            if not colon:
+                continue
+            self[name.strip()] = value.strip()
 
         # Replace currentColor by a real color value
         color_attributes = (
@@ -148,12 +149,11 @@ class Node(dict):
                 self[attribute] = self.get("color", "black")
 
         # Replace inherit by the parent value
-        for attribute, value in dict(self).items():
-            if value == "inherit":
-                if parent is not None and attribute in parent:
-                    self[attribute] = parent.get(attribute)
-                else:
-                    del self[attribute]
+        for attribute in [k for k in self if self[k] == "inherit"]:
+            if parent is not None and attribute in parent:
+                self[attribute] = parent.get(attribute)
+            else:
+                del self[attribute]
 
         # Manage text by creating children
         if self.tag in ("text", "textPath", "a"):
diff --git a/cairosvg/surface/__init__.py b/cairosvg/surface/__init__.py
index 642ee55..93e349c 100644
--- a/cairosvg/surface/__init__.py
+++ b/cairosvg/surface/__init__.py
@@ -32,8 +32,8 @@ except (ImportError, OSError):
 from ..parser import Tree
 from .colors import color
 from .defs import (
-    apply_filter_after, apply_filter_before, gradient_or_pattern, parse_def,
-    paint_mask)
+    apply_filter_after_painting, apply_filter_before_painting,
+    gradient_or_pattern, parse_def, paint_mask, prepare_filter)
 from .helpers import (
     node_format, transform, normalize, paint, urls, apply_matrix_transform,
     PointError, rect)
@@ -239,8 +239,16 @@ class Surface(object):
 
         masks = urls(node.get("mask"))
         mask = masks[0][1:] if masks else None
+
+        filters = urls(node.get("filter"))
+        filter_ = filters[0][1:] if filters else None
+
         opacity = float(node.get("opacity", 1))
-        if mask or (opacity < 1 and node.children):
+
+        if filter_:
+            prepare_filter(self, node, filter_)
+
+        if filter_ or mask or (opacity < 1 and node.children):
             self.context.push_group()
 
         self.context.move_to(
@@ -314,9 +322,6 @@ class Surface(object):
                 self.context.clip()
                 self.context.set_fill_rule(cairo.FILL_RULE_WINDING)
 
-        # Filter
-        apply_filter_before(self, node)
-
         if node.tag in TAGS:
             try:
                 TAGS[node.tag](self, node)
@@ -324,9 +329,6 @@ class Surface(object):
                 # Error in point parsing, do nothing
                 pass
 
-        # Filter
-        apply_filter_after(self, node)
-
         # Get stroke and fill opacity
         stroke_opacity = float(node.get("stroke-opacity", 1))
         fill_opacity = float(node.get("fill-opacity", 1))
@@ -381,12 +383,16 @@ class Surface(object):
             for child in node.children:
                 self.draw(child)
 
-        if mask or (opacity < 1 and node.children):
+        if filter_ or mask or (opacity < 1 and node.children):
             self.context.pop_group_to_source()
+            if filter_:
+                apply_filter_before_painting(self, node, filter_)
             if mask and mask in self.masks:
                 paint_mask(self, node, mask, opacity)
             else:
                 self.context.paint_with_alpha(opacity)
+            if filter_:
+                apply_filter_after_painting(self, node, filter_)
 
         # Clean cursor's position after 'text' tags
         if node.tag == "text":
diff --git a/cairosvg/surface/defs.py b/cairosvg/surface/defs.py
index cfa9d6a..957683d 100644
--- a/cairosvg/surface/defs.py
+++ b/cairosvg/surface/defs.py
@@ -337,12 +337,10 @@ def draw_marker(surface, node, position="mid"):
         node.pending_markers.append(pending_marker)
 
 
-def apply_filter_before(surface, node):
+def prepare_filter(surface, node, name):
     if node["id"] in surface.masks:
         return
 
-    names = urls(node.get("filter"))
-    name = names[0][1:] if names else None
     if name in surface.filters:
         filter_node = surface.filters[name]
         for child in filter_node.children:
@@ -359,14 +357,10 @@ def apply_filter_before(surface, node):
                 surface.context.translate(dx, dy)
 
 
-def apply_filter_after(surface, node):
-    surface.context.set_operator(BLEND_OPERATORS["normal"])
-
+def apply_filter_before_painting(surface, node, name):
     if node["id"] in surface.masks:
         return
 
-    names = urls(node.get("filter"))
-    name = names[0][1:] if names else None
     if name in surface.filters:
         filter_node = surface.filters[name]
         for child in filter_node.children:
@@ -374,8 +368,18 @@ def apply_filter_after(surface, node):
             if child.tag == "feBlend":
                 surface.context.set_operator(BLEND_OPERATORS.get(
                     child.get("mode", "normal"), BLEND_OPERATORS["normal"]))
+
+
+def apply_filter_after_painting(surface, node, name):
+    if node["id"] in surface.masks:
+        return
+
+    if name in surface.filters:
+        filter_node = surface.filters[name]
+        for child in filter_node.children:
             # Flood
-            elif child.tag == "feFlood":
+            if child.tag == "feFlood":
+                surface.context.save()
                 surface.context.new_path()
                 if filter_node.get("primitiveUnits") == "objectBoundingBox":
                     x = size(surface, node.get("x"), "x")
@@ -393,7 +397,7 @@ def apply_filter_after(surface, node):
                     paint(child.get("flood-color"))[1],
                     float(child.get("flood-opacity", 1))))
                 surface.context.fill()
-                surface.context.new_path()
+                surface.context.restore()
 
 
 def use(surface, node):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/cairosvg.git



More information about the Python-modules-commits mailing list