~eda-qa/dhlib/main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/ruby
#
# Converts Shapes in an SWF into curve point arrays
#

require 'rexml/document'
include REXML
require 'fileutils'

if ARGV.length != 1
	puts "Syntax: swf_to_pts.rb swfmill_svg2xml.xml"
	exit -1
end

file = File.new( ARGV[0] )
doc = Document.new( file )

alignX = -0.5
alignY = -0.5

doc.elements.each( "//DefineShape3" ) { |el|
	puts
	puts "DefineSprite #{el.attributes['objectID']}"
	pts = []
	
	# x,y entries in the XML are relative to the previous point (the base)
	basex = 0
	basey = 0
	XPath.each( el, ".//edges/*" ) { |op|
		case op.name
			when "ShapeSetup"
				if( op.attributes['x'] )
					basex = op.attributes['x'].to_f
					basey = op.attributes['y'].to_f
					if pts.length != 0
						pts.push( [] )	# a second shape/segment
						pts.push( [] )	# let two nulls become a split
					end
					pts.push( [basex,basey] )
				end
				
			when "CurveTo"
				x1 = op.attributes['x1'].to_f
				y1 = op.attributes['y1'].to_f
				x2 = op.attributes['x2'].to_f
				y2 = op.attributes['y2'].to_f
				pts.push( [x1+basex,y1+basey] )
				basex += x1
				basey += y1
				pts.push( [x2+basex,y2+basey] )
				basex += x2
				basey += y2
				
			when "LineTo"
				x = op.attributes['x'].to_f
				y = op.attributes['y'].to_f
				pts.push( [] )
				pts.push( [x+basex,y+basey] )
				basex += x
				basey += y
							
			else
				puts "Unsupported #{op.name} "
		end
	}
	if pts.length == 0
		next
	end
	
	# Claculate extents to transform to 0,1 range
	minx = maxx = pts[0][0]
	miny = maxy = pts[0][1]
	pts.each { |pt|
		if pt.length == 0
			next
		end
		
		minx = [minx,pt[0]].min
		maxx = [maxx,pt[0]].max
		miny = [miny,pt[1]].min
		maxy = [maxy,pt[1]].max
	}
	offx = 0-minx
	offy = 0-miny
	rangex = maxx - minx
	rangey = maxy - miny
	#puts "#{offx}, #{offy} == #{rangex},#{rangey}"
	
	print "["
	pts.each { |pt|
		if pt.length == 0
			print "[],"
		else
			x = (pt[0]+offx)/rangex + alignX;
			y = (pt[1]+offy)/rangey + alignY;
			print "[#{x},#{y}],"
		end
	}
	puts "]"
}