コード例 #1
0
ファイル: text_split.py プロジェクト: wachin/inkscape
    def split_letters(self, node):
        """Returns a list of letters"""

        letters = []

        words = self.split_words(node)
        if not words:
            return letters

        for word in words:

            x = float(word.get("x"))
            y = word.get("y")

            # gets the font size. If element doesn't have a style attribute, it assumes font-size = 12px
            fontsize = word.style.get("font-size", "12px")
            fs = self.svg.unittouu(fontsize)

            # for each letter in element string
            for letter in word[0].text:
                tspan = Tspan()
                tspan.text = letter

                text = TextElement(**node.attrib)
                text.set("x", str(x))
                text.set("y", str(y))
                x += fs

                text.append(tspan)
                letters.append(text)
        return letters
コード例 #2
0
def textAt( x, y, text, style, rotate=0 ):
  "Places a test-element at global-coordinates."
  
  elem = TextElement(x=str(x), y=str(y))
  elem.text = text
  elem.style = style
  if rotate != 0:
    elem.set('transform','rotate({},{},{})'.format(rotate,x,y))
  return elem
コード例 #3
0
ファイル: parchis_extension.py プロジェクト: afijog/plugins
    def generateText(self, text, x, y, size):
        tspan = Tspan()
        tspan.text = text

        textElement = TextElement()
        textElement.set("x", str(x))
        textElement.set("y", str(y))
        # textElement.style = "font-size:50px; font-family:Metal Lord"
        textElement.style = "font-size:{}px;".format(size)

        textElement.append(tspan)

        return textElement
コード例 #4
0
ファイル: text_split.py プロジェクト: wachin/inkscape
    def split_words(self, node):
        """Returns a list of words"""
        words = []

        # Function to recursively extract text
        def plain_str(elem):
            words = []
            if elem.text:
                words.append(elem.text)
            for n in elem:
                words.extend(plain_str(n))
                if n.tail:
                    words.append(n.tail)
            return words

        # if text has more than one line, iterates through elements
        lines = self.split_lines(node)
        if not lines:
            return words

        for line in lines:
            # gets the position of text node
            x = float(line.get("x"))
            y = line.get("y")

            # gets the font size. if element doesn't have a style attribute, it assumes font-size = 12px
            fontsize = line.style.get("font-size", "12px")
            fs = self.svg.unittouu(fontsize)

            # extract and returns a list of words
            words_list = "".join(plain_str(line)).split()
            prev_len = 0

            # creates new text nodes for each string in words_list
            for word in words_list:
                tspan = Tspan()
                tspan.text = word

                text = TextElement(**line.attrib)
                tspan.set('sodipodi:role', "line")

                # positioning new text elements
                x = x + prev_len * fs
                prev_len = len(word)
                text.set("x", str(x))
                text.set("y", str(y))

                text.append(tspan)
                words.append(text)

        return words
コード例 #5
0
ファイル: text_split.py プロジェクト: wachin/inkscape
    def split_lines(self, node):
        """Returns a list of lines"""
        lines = []
        count = 1

        for elem in node:
            if isinstance(elem, TextPath):
                inkex.errormsg(
                    "Text on path isn't supported. First remove text from path."
                )
                break
            elif not isinstance(elem, (FlowPara, Tspan)):
                continue

            text = TextElement(**node.attrib)

            # handling flowed text nodes
            if isinstance(node, FlowRoot):
                fontsize = node.style.get("font-size", "12px")
                fs = self.svg.unittouu(fontsize)

                # selects the flowRegion's child (svg:rect) to get @X and @Y
                flowref = node.findone('svg:flowRegion')[0]

                if isinstance(flowref, Rectangle):
                    text.set("x", flowref.get("x"))
                    text.set("y", str(float(flowref.get("y")) + fs * count))
                    count += 1
                else:
                    inkex.debug(
                        "This type of text element isn't supported. First unflow text."
                    )
                    break

                # now let's convert flowPara into tspan
                tspan = Tspan()
                tspan.set("sodipodi:role", "line")
                tspan.text = elem.text
                text.append(tspan)

            else:
                from copy import copy
                x = elem.get("x") or node.get("x")
                y = elem.get("y") or node.get("y")

                text.set("x", x)
                text.set("y", y)
                text.append(copy(elem))

            lines.append(text)

        return lines
コード例 #6
0
    def generate(self):
        """Generate the actual svg from the coding"""
        string = self.encode(self.text)

        if string == 'ERROR':
            return

        name = self.get_id('barcode')

        # use an svg group element to contain the barcode
        barcode = Group()
        barcode.set('id', name)
        barcode.set('style', 'fill: black;')

        barcode.transform.add_translate(self.pos_x, self.pos_y)
        if self.scale:
            barcode.transform.add_scale(self.scale)

        bar_id = 1
        bar_offset = 0
        tops = set()

        for datum in self.graphical_array(string):
            # Datum 0 tells us what style of bar is to come next
            style = self.get_style(int(datum[0]))
            # Datum 1 tells us what width in units,
            # style tells us how wide a unit is
            width = int(datum[1]) * int(style['width'])

            if style['write']:
                tops.add(style['top'])
                rect = Rectangle()
                rect.set('x', str(bar_offset))
                rect.set('y', str(style['top']))
                if self.pos_text == TEXT_POS_TOP:
                    rect.set('y', str(style['top'] + self.font_size))
                rect.set('id', "{}_bar{:d}".format(name, bar_id))
                rect.set('width', str(width))
                rect.set('height', str(style['height']))
                barcode.append(rect)
            bar_offset += width
            bar_id += 1

        for extra in self._extra:
            if extra is not None:
                barcode.append(extra)

        bar_width = bar_offset
        # Add text at the bottom of the barcode
        text = TextElement()
        text.set('x', str(int(bar_width / 2)))
        text.set('y', str(min(tops) + self.font_size - 1))
        if self.pos_text == TEXT_POS_BOTTOM:
            text.set('y', str(self.height + max(tops) + self.font_size))
        text.set('style', TEXT_TEMPLATE % self.font_size)
        text.set('xml:space', 'preserve')
        text.set('id', '{}_text'.format(name))
        text.text = str(self.text)
        barcode.append(text)
        return barcode
コード例 #7
0
    def effect(self):

        if len(self.svg.selection) != 1:
            raise AbortExtension(_("Debe seleccionar un objeto"))

        scale = self.svg.unittouu('1mm')  # convert to document units

        patternSize = self.options.patternSize
        fromSize = self.options.fromSize
        toSize = self.options.toSize

        if not (fromSize <= patternSize <= toSize):
            raise AbortExtension(
                _("La talla del patrón debe estar dentro de desde y hasta"))

        downerSizesCount = patternSize - fromSize
        upperSizesCount = toSize - patternSize

        pattern = self.svg.selection.first()
        parent = pattern.getparent()

        bbox = pattern.shape_box()
        scaleX = 10 * scale  # scale width 10mm
        scaleY = 23.21 * scale  # scale height 23.21mm
        width = bbox.width * scale
        height = bbox.height * scale

        for i, size in enumerate(
                range(patternSize + upperSizesCount, patternSize, -1)):
            copy = pattern.duplicate()
            size_text = TextElement()

            proportionX = 1 + (1 - ((width - (scaleX *
                                              (upperSizesCount - i))) / width))
            proportionY = 1 + (1 - ((height -
                                     (scaleY *
                                      (upperSizesCount - i))) / height))

            transform = Transform()
            transform.add_scale(proportionX, proportionY)
            copy.transform = transform

            size_text.text = str(size)
            size_text.set(
                'style',
                "font-size:8px;shape-inside:url(#{});".format(copy.get('id')))

            group = Group()
            group.append(copy)
            group.append(size_text)
            parent.append(group)

            group.set(
                'transform',
                "translate(-{},-{})".format(copy.shape_box().left,
                                            copy.shape_box().top))

        for i, size in enumerate(
                range(patternSize - 1, patternSize - downerSizesCount - 1, -1),
                1):
            copy = pattern.duplicate()
            size_text = TextElement()

            proportionX = (width - (scaleX * i)) / width
            proportionY = (height - (scaleY * i)) / height

            transform = Transform()
            transform.add_scale(proportionX, proportionY)
            copy.transform = transform

            size_text.text = str(size)
            size_text.set(
                'style',
                "font-size:8px;shape-inside:url(#{});".format(copy.get('id')))

            group = Group()
            group.append(copy)
            group.append(size_text)
            parent.append(group)

            group.set(
                'transform',
                "translate(-{},-{})".format(copy.shape_box().left,
                                            copy.shape_box().top))

        patternGroup = Group()
        pattern_size_text = TextElement()
        pattern_size_text.text = str(patternSize)
        pattern_size_text.set(
            'style',
            "font-size:8px;shape-inside:url(#{});".format(pattern.get('id')))
        patternGroup.append(pattern)
        patternGroup.append(pattern_size_text)
        parent.append(patternGroup)