Skip to content

Instantly share code, notes, and snippets.

@nanvel
Last active November 5, 2016 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanvel/ec0dd30753927b4f3a36aa5827c0a1bd to your computer and use it in GitHub Desktop.
Save nanvel/ec0dd30753927b4f3a36aa5827c0a1bd to your computer and use it in GitHub Desktop.
Move SVG image
svg = """
<path d="M 680.40768,58.690197 C 680.17329,58.760524 680.10297,59.018351 680.22018,59.440197 C 680.47016,59.783987 680.47015,61.533977 680.18893,64.690197 C 677.50142,64.783977 674.53266,64.752738 671.31393,64.627697 L 671.12643,64.721447 C 671.22017,65.971477 671.28266,67.346466 671.31393,68.846447 C 674.40767,68.721467 677.28265,68.596477 679.90768,68.440197 C 679.78267,70.752727 679.72015,73.377716 679.75143,76.346447 C 677.90766,77.158957 676.28265,78.190218 674.84518,79.440197 C 673.40767,80.690217 672.22016,82.221457 671.31393,84.033947 C 670.43892,85.815207 670.06391,87.783957 670.15768,89.940197 C 670.25143,92.065207 670.78266,93.752697 671.75143,94.971447 C 672.75142,96.158947 674.06391,96.846447 675.72018,97.065197 C 677.37641,97.315197 678.9389,97.096447 680.37643,96.377697 C 681.81391,95.690197 682.90764,95.065197 683.65768,94.533947 L 684.25143,96.033947 C 686.31391,94.908947 687.81389,94.221447 688.72018,93.940197 L 688.75143,93.408947 C 688.28265,92.565197 687.84514,91.752697 687.43893,90.940197 C 689.1889,88.877707 690.59514,86.783957 691.65768,84.658947 C 692.72015,82.502707 693.68888,80.190217 694.53268,77.721447 C 695.6264,77.877717 696.90763,78.221467 698.37643,78.752697 C 699.8764,79.283967 701.12638,80.283967 702.15768,81.752697 C 703.18889,83.221457 703.65763,84.721456 703.56393,86.283947 C 703.47014,87.846447 702.87638,89.252707 701.78268,90.502697 C 700.68889,91.752707 699.18888,92.752697 697.25143,93.471447 C 695.34515,94.190197 693.37638,94.690197 691.34518,94.971447 L 691.34518,95.471447 C 692.34515,96.846437 693.03264,98.190197 693.43893,99.471447 C 695.72015,98.971447 697.97013,98.190197 700.15768,97.127697 C 702.37639,96.065197 704.28262,94.471457 705.87643,92.377697 C 707.50139,90.252707 708.28262,88.127707 708.22018,86.002697 C 708.15764,83.846467 707.43887,81.815207 706.06393,79.877697 C 704.68889,77.940217 702.90763,76.596467 700.75143,75.846447 C 698.59514,75.065217 696.90763,74.533966 695.65768,74.283947 L 696.25143,72.283947 L 697.00143,71.627697 C 697.37639,71.252727 697.31388,70.971478 696.81393,70.815197 L 692.47018,69.940197 C 691.90765,69.877727 691.65764,70.127727 691.72018,70.690197 C 691.90765,70.846477 691.75138,71.908966 691.25143,73.908947 C 688.25141,74.065217 685.72015,74.440217 683.68893,75.002697 C 683.68891,73.190217 683.78265,70.908977 683.97018,68.158947 C 684.81391,68.065227 686.47015,67.908977 688.93893,67.690197 C 691.4389,67.440227 694.50139,67.002727 698.12643,66.377697 L 696.97018,62.471447 C 693.2514,63.377727 689.00139,64.065227 684.25143,64.502697 C 684.37641,63.252727 684.5014,61.908988 684.62643,60.502697 L 685.25143,59.908947 C 685.59516,59.565237 685.5014,59.315238 685.00143,59.190197 L 680.68893,58.690197 C 680.57174,58.674582 680.48581,58.666755 680.40768,58.690197 z M 690.25143,77.440197 C 689.59515,79.471467 688.90764,81.283957 688.15768,82.908947 C 687.40766,84.502707 686.59515,85.877706 685.75143,87.033947 C 685.40766,86.065207 685.06389,84.940207 684.68893,83.627697 C 684.31391,82.283957 684.03264,80.627717 683.84518,78.658947 C 686.37641,77.940217 688.50139,77.533967 690.25143,77.440197 z M 680.03268,80.252697 C 680.18891,81.877707 680.4389,83.502707 680.75143,85.127697 C 681.09517,86.752707 681.59515,88.596457 682.28268,90.658947 C 681.72016,91.127707 681.0014,91.627707 680.12643,92.190197 C 679.25141,92.721457 678.2514,93.033947 677.12643,93.096447 C 676.00142,93.158947 675.22015,92.877697 674.75143,92.252697 C 674.31392,91.627697 674.09515,90.627707 674.09518,89.252697 C 674.12642,87.846457 674.4389,86.502707 675.03268,85.221447 C 675.65767,83.908957 676.47016,82.846457 677.50143,82.033947 C 678.56391,81.190217 679.40765,80.596467 680.03268,80.252697 z "/>
<path d="M 664.04221,61.926088 L 677.02444,61.631038" class="stroke"/>
<text x="663.65601" y="59.192589" class="number">1</text>
<path d="M 690.46417,50.749672 L 687.33034,61.018412" class="stroke"/>
<text x="691.6955" y="56.927895" class="number">2</text>
<path d="M 700.33346,70.486347 L 694.59173,86.075213" class="stroke"/>
<text x="700.7926" y="73.305542" class="number">3</text>
<path d="M 678.08276,100.03337 C 657.95206,97.23371 669.16942,81.528748 672.42258,76.144219" class="stroke"/>
"""
BORDER = 5
def scan(text):
for p in text.split(" "):
if "," not in p:
continue
x, y = p.split('"')[0].split(",")
yield float(x), float(y)
for l in text.split("\n"):
if "text" not in l:
continue
yield float(l.split('x="')[1].split('"')[0]), float(l.split('y="')[1].split('"')[0])
def rm_zeros(l):
while l[-1] in ("0", "."):
l = l[:-1]
return l
if __name__ == '__main__':
minx = 100000
miny = 100000
maxx = 0
maxy = 0
for x, y in scan(svg):
if x < minx:
minx = x
if y < miny:
miny = y
if x > maxx:
maxx = x
if y > maxy:
maxy = y
result = svg
for x, y in scan(svg):
i = rm_zeros("{0:.6f}".format(x)) + "," + rm_zeros("{0:.6f}".format(y))
o = rm_zeros("{0:.6f}".format(x - minx + BORDER)) + "," + rm_zeros("{0:.6f}".format(y - miny + BORDER))
result = result.replace(i, o)
i = 'x="' + rm_zeros("{0:.6f}".format(x)) + '" y="' + rm_zeros("{0:.6f}".format(y)) + '"'
o = 'x="' + rm_zeros("{0:.6f}".format(x - minx + BORDER)) + '" y="' + rm_zeros("{0:.6f}".format(y - miny + BORDER)) + '"'
result = result.replace(i, o)
print result
print round(maxx - minx + 2 * BORDER), round(maxy - miny + 2 * BORDER)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment