Exemple #1
0
    def process(self, transformer, flip):

        points = []
        for point in self.points:

            point = transformer.transform_point(point, flip)

            if (len(points) < 1 or point.x != points[-1].x
                    or point.y != points[-1].y):
                points.append(point)

        if (points[0].x != points[-1].x or points[0].y != points[-1].y):
            #print( "Warning: Closing polygon. start=({}, {}) end=({}, {})".format(
            #points[ 0 ].x, points[ 0 ].y,
            #points[ -1 ].x, points[ -1 ].y,
            #) )

            points.append(svg.Point(
                points[0].x,
                points[0].y,
            ))

        #else:
        #print( "Polygon closed: start=({}, {}) end=({}, {})".format(
        #points[ 0 ].x, points[ 0 ].y,
        #points[ -1 ].x, points[ -1 ].y,
        #) )

        self.points = points
Exemple #2
0
    def _calculate_translation(self):

        min_point, max_point = self.imported.svg.bbox()

        # Center the drawing:
        adjust_x = min_point.x + (max_point.x - min_point.x) / 2.0
        adjust_y = min_point.y + (max_point.y - min_point.y) / 2.0

        self.translation = svg.Point(
            0.0 - adjust_x,
            0.0 - adjust_y,
        )
Exemple #3
0
    def points_starting_on_index(self, index):

        points = self.points

        if index > 0:

            # Strip off end point, which is a duplicate of the start point:
            points = points[:-1]

            points = points[index:] + points[:index]

            points.append(svg.Point(points[0].x, points[0].y))

        return points
Exemple #4
0
    def inline( self, segments ):

        if len( segments ) < 1:
            return self.points

        print( "    Inlining {} segments...".format( len( segments ) ) )

        all_segments = segments[ : ] + [ self ]
        insertions = []

        # Find the insertion point for each hole:
        for hole in segments:

            insertion = self._find_insertion_point(
                hole, all_segments
            )
            if insertion is not None:
                insertions.append( insertion )

        insertions.sort( key = lambda i: i[ 0 ] )

        inlined = [ self.points[ 0 ] ]
        ip = 1
        points = self.points

        for insertion in insertions:

            while ip <= insertion[ 0 ]:
                inlined.append( points[ ip ] )
                ip += 1

            if (
                inlined[ -1 ].x == insertion[ 1 ][ 0 ].x and
                inlined[ -1 ].y == insertion[ 1 ][ 0 ].y
            ):
                inlined += insertion[ 1 ][ 1 : -1 ]
            else:
                inlined += insertion[ 1 ]

            inlined.append( svg.Point(
                points[ ip - 1 ].x,
                points[ ip - 1 ].y,
            ) )

        while ip < len( points ):
            inlined.append( points[ ip ] )
            ip += 1

        return inlined
Exemple #5
0
    def __addRadialGradient(self, element, fill):
        root = fill.getRoot()
        stops = fill
        while len(stops) == 0 and stops.href:
            stops = root.getElementById(stops.href[1:])
        background = []

        #座標補正
        gradientTransform = fill.gradientTransform.toMatrix()
        center = svg.Point(fill.cx, fill.cy)
        finish = svg.Point(fill.fx, fill.fy)
        center = gradientTransform * center
        finish = gradientTransform * finish

        if fill.gradientUnits == "userSpaceOnUse":
            stroke = svg.Length(element.style.get("stroke-width", 0))
            center = svg.Point(center.x - svg.Length(self["left"]) - stroke,
                               center.y - svg.Length(self["top"]) - stroke)
            finish = svg.Point(finish.x - svg.Length(self["left"]) - stroke,
                               finish.y - svg.Length(self["top"]) - stroke)

        #半径の決定
        zero = svg.Length("0")
        point0 = gradientTransform * svg.Point(zero, zero)
        rx = svg.Length(
            abs(gradientTransform * svg.Point(fill.r, zero) - point0), "px")
        ry = svg.Length(
            abs(gradientTransform * svg.Point(zero, fill.r) - point0), "px")
        r = fill.r

        gradient = ""
        for stop in stops:
            color = svg.Color(stop.style["stop-color"])
            if float(stop.style.get("stop-opacity", "1")) <= 0.999:
                color.a = float(stop.style.get("stop-opacity", "1"))
            gradient += ",%s %.1f%%" % (color, stop.offset * 100)
        background.append("radial-gradient(%s %s,%s %s%s)" %
                          (center.x, center.y, rx, ry, gradient))
        background.append("-o-radial-gradient(%s %s,%s %s%s)" %
                          (center.x, center.y, rx, ry, gradient))
        background.append("-moz-radial-gradient(%s %s,circle%s)" %
                          (center.x, center.y, gradient))
        background.append("-moz-radial-gradient(%s %s,%s %s%s)" %
                          (center.x, center.y, rx, ry, gradient))
        background.append("-ms-radial-gradient(%s %s,%s %s%s)" %
                          (center.x, center.y, rx, ry, gradient))
        background.append("-webkit-radial-gradient(%s %s,%s %s%s)" %
                          (center.x, center.y, rx, ry, gradient))

        self["background"] = background
Exemple #6
0
    def transform_point(self, point, flip=False):

        transformed_point = svg.Point(
            (point.x + self.translation.x) * self.scale_factor,
            (point.y + self.translation.y) * self.scale_factor,
        )

        if flip:
            transformed_point.x *= -1

        if self.use_mm:
            transformed_point.x = round(transformed_point.x, 12)
            transformed_point.y = round(transformed_point.y, 12)
        else:
            transformed_point.x = int(round(transformed_point.x))
            transformed_point.y = int(round(transformed_point.y))

        return transformed_point
Exemple #7
0
    def __addLinearGradient(self, element, fill):
        root = fill.getRoot()
        stops = fill
        while len(stops) == 0 and stops.href:
            stops = root.getElementById(stops.href[1:])
        background = []

        #座標補正
        point1 = svg.Point(fill.x1, fill.y1)
        point2 = svg.Point(fill.x2, fill.y2)
        point1 = fill.gradientTransform.toMatrix() * point1
        point2 = fill.gradientTransform.toMatrix() * point2
        if fill.gradientUnits == "userSpaceOnUse":
            stroke = svg.Length(element.style.get("stroke-width", 0))
            point1 = svg.Point(point1.x - svg.Length(self["left"]) - stroke,
                               point1.y - svg.Length(self["top"]) - stroke)
            point2 = svg.Point(point2.x - svg.Length(self["left"]) - stroke,
                               point2.y - svg.Length(self["top"]) - stroke)

        def svgOffsetToPoint(offset):
            return point1 * (1 - offset) + point2 * offset

        #css3のデフォルト
        rad = -math.atan2(point2.y - point1.y, point2.x - point1.x)
        vec = svg.Point(math.cos(rad), -math.sin(rad))
        deg = rad / math.pi * 180
        width = svg.Length(self["width"])
        height = svg.Length(self["height"])
        point0 = svg.Point(0, 0)
        if 0 < deg < 90:
            point0 = svg.Point(0, height)
        elif 90 <= deg:
            point0 = svg.Point(width, height)
        elif deg < -90:
            point0 = svg.Point(width, 0)
        gradientlen = (svg.Point(width, height) - point0 * 2) * vec

        def pointToCSSOffset(point):
            offset = (point - point0) * vec / gradientlen
            return offset

        def svgOffsetToCSSOffset(offset):
            return pointToCSSOffset(svgOffsetToPoint(offset))

        gradient = "(%.1fdeg" % deg
        color_stops = []
        for stop in stops:
            color = svg.Color(stop.style["stop-color"])
            if float(stop.style.get("stop-opacity", "1")) <= 0.999:
                color.a = float(stop.style.get("stop-opacity", "1"))
            gradient += ",%s %.1f%%" % (
                color, svgOffsetToCSSOffset(stop.offset) * 100)

        gradient += ")"
        background.append("linear-gradient" + gradient)
        background.append("-o-linear-gradient" + gradient)
        background.append("-moz-linear-gradient" + gradient)
        background.append("-ms-linear-gradient" + gradient)
        background.append("-webkit-linear-gradient" + gradient)

        #webkit
        webkit = "-webkit-gradient(linear,%f %f,%f %f," % (
            point1.x.px(), point1.y.px(), point2.x.px(), point2.y.px())
        color = svg.Color(stops[0].style["stop-color"])
        if float(stops[0].style.get("stop-opacity", "1")) <= 0.999:
            color.a = float(stops[0].style.get("stop-opacity", "1"))
        webkit += "from(%s)," % color
        if len(stops) > 2:
            for stop in stops[1:-1]:
                color = svg.Color(stop.style["stop-color"])
                if float(stop.style.get("stop-opacity", "1")) <= 0.999:
                    color.a = float(stop.style.get("stop-opacity", "1"))
                webkit += "color-stop(%f,%s)," % (stop.offset, color)
        color = svg.Color(stops[-1].style["stop-color"])
        if float(stops[-1].style.get("stop-opacity", "1")) <= 0.999:
            color.a = float(stops[-1].style.get("stop-opacity", "1"))
        webkit += "to(%s))" % color
        background.append(webkit)

        self["background"] = background