Пример #1
0
    def draw_glyph(self, svg_paths_dir, margins, angle):
        """Define SVG elements to draw glyph based on this dot's value, position and size.

        Parameters
        ----------
        svg_paths_dir : str
            directory containing glyphs paths as SVG
        margins : [float, float, float, float]
            top, right, bottom, left
        angle : float
            degrees

        Returns
        -------
        string
        """

        # read glyph path svg file as multiline
        glyph_file = os.path.join(svg_paths_dir,
                                  "{:02d}.svg".format(self.value))
        doc = minidom.parse(glyph_file)
        width = float(doc.getElementsByTagName("svg")[0].getAttribute("width"))
        height = float(
            doc.getElementsByTagName("svg")[0].getAttribute("height"))
        lines = [[(float(point.split(",")[0]), float(point.split(",")[1]))
                  for point in path.getAttribute("points").split(" ")]
                 for path in doc.getElementsByTagName("polyline")]
        doc.unlink()
        multiline = MultiLineString(lines)

        # compute cell bounds
        target_xmin = self.ulx + margins[3]
        target_xmax = self.ulx + self.size[0] - margins[1]
        target_ymin = self.uly + margins[0]
        target_ymax = self.uly + self.size[1] - margins[2]

        # compute glyph bounds
        xmin, ymin, xmax, ymax = multiline.bounds
        target_xmin += xmin * (self.size[0] - margins[1] - margins[3]) / width
        target_xmax -= (width - xmax) * (self.size[0] - margins[1] -
                                         margins[3]) / width
        target_ymin += ymin * (self.size[1] - margins[0] - margins[2]) / height
        target_ymax -= (height - ymax) * (self.size[1] - margins[0] -
                                          margins[2]) / height

        # transform multiline based on cell position and size and angle
        multiline = transform_multiline(multiline, target_xmin, target_xmax,
                                        target_ymin, target_ymax, angle)

        return multiline.svg()