コード例 #1
0
ファイル: dvi2svg.py プロジェクト: stjordanis/pydvi2svg
    def flush_chars(self):
        new = self.document.createElement
        s2s = self.scale2str
        c2s = self.coord2str

        elements = []

        # (fntnum, dvicode, H, V, glyphscale, color)

        # group chars with same glyphscale
        byglyphscale = group(self.chars, value=lambda x: x[4])
        for (glyphscale, chars2) in byglyphscale:
            g = self.document.createElement('g')
            g.setAttribute(
                'transform',
                'scale(%s,%s)' % (s2s(glyphscale), s2s(-glyphscale)))
            elements.append(g)

            # then group by V
            byV = group(chars2, value=lambda x: x[3])
            for (V, chars3) in byV:

                xo = chars3[0][2] / glyphscale  # get X coords of first
                g1 = new('g')
                g1.setAttribute(
                    'transform',
                    'translate(%s,%s)' % (c2s(xo), c2s(-V / glyphscale)))
                g.appendChild(g1)

                for j, char in enumerate(chars3):
                    c = new('use')
                    g1.appendChild(c)

                    H = char[2]
                    fntnum = char[0]
                    dvicode = char[1]
                    color = char[5]
                    idref = "#%02x%d" % (dvicode, fntnum)

                    c.setAttributeNS('xlink', 'xlink:href', idref)
                    if j > 0:
                        c.setAttribute('x', c2s(H / glyphscale - xo))
                    if color:
                        c.setAttribute('fill', color)

        self.chars = []
        return elements
コード例 #2
0
ファイル: dvi2svg.py プロジェクト: WojciechMula/pydvi2svg
	def flush_chars(self):
		new = self.document.createElement
		s2s = self.scale2str
		c2s = self.coord2str

		elements = []

		# (fntnum, dvicode, H, V, glyphscale, color)

		# group chars with same glyphscale
		byglyphscale = group(self.chars, value=lambda x: x[4])
		for (glyphscale, chars2) in byglyphscale:
			g = self.document.createElement('g')
			g.setAttribute('transform', 'scale(%s,%s)' % (s2s(glyphscale), s2s(-glyphscale) ))
			elements.append(g)

			# then group by V
			byV = group(chars2, value=lambda x: x[3])
			for (V, chars3) in byV:

				xo = chars3[0][2]/glyphscale # get X coords of first
				g1 = new('g')
				g1.setAttribute('transform', 'translate(%s,%s)' %
					(c2s(xo), c2s(-V/glyphscale))
				)
				g.appendChild(g1)

				for j, char in enumerate(chars3):
					c = new('use')
					g1.appendChild(c)
						
					H       = char[2]
					fntnum  = char[0]
					dvicode = char[1]
					color   = char[5]
					idref   = "#%02x%d" % (dvicode, fntnum)

					c.setAttributeNS('xlink', 'xlink:href', idref)
					if j > 0:
						c.setAttribute('x', c2s(H/glyphscale - xo))
					if color:
						c.setAttribute('fill', color)

		self.chars = []
		return elements
コード例 #3
0
ファイル: dvi2svg.py プロジェクト: WojciechMula/pydvi2svg
	def flush_chars(self):
		new = self.document.createElement
		s2s = self.scale2str
		c2s = self.coord2str
		
		# (fntnum, dvicode, H, V, glyphscale, color, putset)
		elements = []

		# group chars typeseted with the same font
		byfntnum = group(self.chars, value=lambda x: x[0])
		for (fntnum, char_list) in byfntnum:
			g = new('g')
			elements.append(g)

			fnt   = font.get_font(fntnum)
			style = "font-family:%s; font-size:%0.1fpt" % (fnt.fontfamily, fnt.designsize)
			s,d   = fnt.sd
			if s != d:	# scaled font
				style += "; font-scale: %0.1f%%" % ((100.0*s)/d)

			g.setAttribute('style', style)

			def isglyphknown(fntnum, dvicode):
				try:
					return bool(name_lookup[font.get_char_name(fntnum, dvicode)])
				except KeyError:
					return False

			# (fntnum, dvicode, H, V, glyphscale, color, putset)
			def output_char_string(list):
				H     = list[0][2]
				V     = list[0][3]
				color = list[0][5]
				text  = ''.join([name_lookup[font.get_char_name(item[0], item[1])] for item in list])

				node = new('text')
				if color:
					node.setAttribute('fill', color)

				node.setAttribute('x', c2s(H))
				node.setAttribute('y', c2s(V))
				node.appendChild(self.document.createTextNode(text))
				return node

			# (fntnum, dvicode, H, V, glyphscale, color, putset)
			def output_char(char):
				H     = char[2]
				V     = char[3]
				color = char[5]
				text  = name_lookup[font.get_char_name(char[0], char[1])]

				node = new('text')
				if color:
					node.setAttribute('fill', color)

				node.setAttribute('x', c2s(H))
				node.setAttribute('y', c2s(V))
				node.appendChild(self.document.createTextNode(text))
				return node

			# find unknown chars
			for (known, char_list2) in group(char_list, lambda x: isglyphknown(x[0], x[1])):
				if not known:
					for char in char_list2:
						H = char[2]
						V = char[3]

						node = new('text')
						node.setAttribute('x', c2s(H))
						node.setAttribute('y', c2s(V))
						node.setAttribute('fill', 'red')
						node.appendChild(self.document.createTextNode('?'))
						g.appendChild(node)
				else:
					# group set_char commands
					for (set_char, char_list3) in group(char_list2, lambda x: x[6]):
						if set_char == 'set':
							for (color, char_list4) in group(char_list3, lambda x: x[5]):
								g.appendChild(output_char_string(char_list4))
						else:
							for char in char_list3:
								g.appendChild(output_char(char))
					#rof

		self.chars = []
		return elements
コード例 #4
0
ファイル: dvi2svg.py プロジェクト: stjordanis/pydvi2svg
    def flush_chars(self):
        new = self.document.createElement
        s2s = self.scale2str
        c2s = self.coord2str

        # (fntnum, dvicode, H, V, glyphscale, color, putset)
        elements = []

        # group chars typeseted with the same font
        byfntnum = group(self.chars, value=lambda x: x[0])
        for (fntnum, char_list) in byfntnum:
            g = new('g')
            elements.append(g)

            fnt = font.get_font(fntnum)
            style = "font-family:%s; font-size:%0.1fpt" % (fnt.fontfamily,
                                                           fnt.designsize)
            s, d = fnt.sd
            if s != d:  # scaled font
                style += "; font-scale: %0.1f%%" % ((100.0 * s) / d)

            g.setAttribute('style', style)

            def isglyphknown(fntnum, dvicode):
                try:
                    return bool(name_lookup[font.get_char_name(
                        fntnum, dvicode)])
                except KeyError:
                    return False

            # (fntnum, dvicode, H, V, glyphscale, color, putset)
            def output_char_string(list):
                H = list[0][2]
                V = list[0][3]
                color = list[0][5]
                text = ''.join([
                    name_lookup[font.get_char_name(item[0], item[1])]
                    for item in list
                ])

                node = new('text')
                if color:
                    node.setAttribute('fill', color)

                node.setAttribute('x', c2s(H))
                node.setAttribute('y', c2s(V))
                node.appendChild(self.document.createTextNode(text))
                return node

            # (fntnum, dvicode, H, V, glyphscale, color, putset)
            def output_char(char):
                H = char[2]
                V = char[3]
                color = char[5]
                text = name_lookup[font.get_char_name(char[0], char[1])]

                node = new('text')
                if color:
                    node.setAttribute('fill', color)

                node.setAttribute('x', c2s(H))
                node.setAttribute('y', c2s(V))
                node.appendChild(self.document.createTextNode(text))
                return node

            # find unknown chars
            for (known,
                 char_list2) in group(char_list,
                                      lambda x: isglyphknown(x[0], x[1])):
                if not known:
                    for char in char_list2:
                        H = char[2]
                        V = char[3]

                        node = new('text')
                        node.setAttribute('x', c2s(H))
                        node.setAttribute('y', c2s(V))
                        node.setAttribute('fill', 'red')
                        node.appendChild(self.document.createTextNode('?'))
                        g.appendChild(node)
                else:
                    # group set_char commands
                    for (set_char,
                         char_list3) in group(char_list2, lambda x: x[6]):
                        if set_char == 'set':
                            for (color,
                                 char_list4) in group(char_list3,
                                                      lambda x: x[5]):
                                g.appendChild(output_char_string(char_list4))
                        else:
                            for char in char_list3:
                                g.appendChild(output_char(char))
                    #rof

        self.chars = []
        return elements