4

多くのテキストノードを含むSVGファイルがいくつかあります。

SVGドキュメント内の各テキストノードの絶対位置を取得したいと思います(SVGドキュメントのwidth="743.75"とheight="1052.5")。

テキストノードの例は次のようになります。

 <g>
  <text transform="matrix(1,0,0,-1,106.5,732.5)">
   <tspan x="0 7.8979998 14.003 17.698999" y="0">Date</tspan>
  </text>
 </g>

各テキストボックスの正の絶対X値とY値に到達するために、すべてのmatrix()変換を計算するにはどうすればよいですか?使用して各行列に順番に渡すことができる単純な再帰関数はありますか?

ありがとう!

4

2 に答える 2

1

また、画像を除いて、この問題を解決しようとしています。再帰の問題は、変換行列をドット乗算する必要があることです。私はNumPyを使用しています。しかし、私の計算では、表向きは間違った答えが得られています。たぶん、あなたはもっと幸運になるでしょう。

http://www.scipy.org/Tentative_NumPy_Tutorial

http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined

from decimal import Decimal
import xml.dom.minidom as dom
from numpy import *
doc = dom.parse("labels.svg")

def walk(node):
    if node.nodeType != 1:
        return
    if node.tagName == 'image':
        href = node.getAttribute('xlink:href')
        if not href.startswith("labels/"):
            return
        name = (
            href.
            replace('labels/', '').
            replace('.png', '').
            replace('-resized', '').
            replace('-normalized', '')
        )
        left = float(node.getAttribute('x'))
        top = float(node.getAttribute('y'))
        position = matrix([left, top, float(1)])
        width = float(node.getAttribute('width'))
        height = float(node.getAttribute('height'))
        size = matrix([left, top, float(1)])
        transform = node.getAttribute('transform')
        if transform:
            a, b, c, d, e, f = map(float, transform
                .replace('matrix(', '')
                .replace(')', '')
                .split(',')
            )
            transform = matrix([
                [a, c, e],
                [b, d, f],
                [0, 0, 1]
            ])
            left, top, _ = (transform.I * position.T).A1
        print name, (left, top)
    child = node.firstChild
    while child:
        walk(child)
        child = child.nextSibling

walk(doc.documentElement)
于 2010-07-23T02:07:05.020 に答える