<?xl version="0.1" encoding="utf-8" ?>

<Define> ^divide-parse-2 ()
<Arguments>
	^inp-data
</Arguments>
<Sequence> ()

	<Define> ^id-list (cdr (GE inp-data 'id)) </Define>
	<Define> ^prt PROCESSING... </Define>
	<For dec="id-list">
		<set> ^prt (+ prt (car id-list) ",") </set>
	</For>
	(PrintStd prt)

	<Define> ^svg-id (GE (GE inp-data 'id) 1) </Define>
	<Define> ^g-list () </Define>
	<Define> ^result () </Define>
	<Define> ^g-no 0 </Define>

	<Define> ^set-list ()
	<Arguments>
		^cmd
	</Arguments>
	<Sequence> ()
		<set> ^result (cons
			(Reverse (cons cmd g-list))
		result) </set>
	</Sequence>
	</Define>

	<Define> ^svg ()
	<Arguments>
	^g
	</Arguments>
		1
	</Define>

	<Define Order="Normal"> ^g ^sym
	<Sequence> ()
		<Define> ^g-list (cons 
				(SetAttribute (car sym) "_no_" g-no)
				g-list) </Define>
		<set> ^g-no (+ g-no 1) </set>
		(ListEval (CurrentEnvironment) (cdr sym))
	</Sequence>
	</Define>

	<Define> ^image () ^sym
		(set-list sym)
	</Define>

	<Define> ^path () ^sym
		(set-list sym)
	</Define>

	<Define Order="Normal"> ^text ^sym () </Define>


	<Define Order="Normal"> ^clipPath ^sym
	<Sequence> ()
		<Define> ^g-list (cons
				(SetAttribute (car sym) "_no_" g-no)
				g-list) </Define>
		<set> ^g-no (+ g-no 1) </set>
		(ListEval (CurrentEnvironment) (cdr sym))
	</Sequence>
	</Define>


	<Define> ^a () ^sym ()
	</Define>

	<Eval> (CurrentEnvironment) (GE inp-data 'svg)
	</Eval>


	<Define> ^result (Reverse result) </Define>

	<Define Order="Normal"> ^cmp
	<Arguments> ^a ^b </Arguments>
	<Switch>
	<Case> (= (GetSymbolCore (car (GE a (- (ListLength a) 1))))
			'image)
		0
	</Case>
	<Case> (= (GetSymbolCore (car (GE b (- (ListLength b) 1))))
			'image)
		0
	</Case>
	<Case> 1 1 </Case>
	</Switch>
	</Define>

	<Define> ^result (Categorize result cmp) </Define>

<!-- *** -->

	<Define Order="Normal"> ^_h2v
	<Arguments> ^a </Arguments>
		(h2v a)
	</Define>

	<Define Order="Normal"> ^cmp <Arguments> ^a ^b </Arguments>
	<Sequence> ()
		<Define> ^level 0 </Define>
		<For dec="a">
			<Define> ^aa (car a) </Define>
			<Define> ^bb (car b) </Define>
			<If> (= aa bb)
			<Else> <Break> 0 </Break> </Else>
			</If>
			<If> (= (GetType aa) "symbol")
			<Else> <Break> 0 </Break> </Else>
			</If>
			<set> ^b (cdr b) </set>
			<set> ^level (+ level 1) </set>
		</For>
		^level
	</Sequence>
	</Define>

	<Define> ^ret () </Define>
	<For dec="result">
		<Define> ^one-thread (car result) </Define>

		<set> ^svg-data (cons
			'<d>
			<id> ($ svg-id)
			</id>
			<layer> ($ svg-layer-no)
			</layer>
			<svg>
				($ (V2H one-thread cmp _h2v))
			</svg>
			</d>
		svg-data)</set>
		<set> ^svg-layer-no (+ svg-layer-no 1) </set>
	</For>
</Sequence>
</Define>

<Define> ^divide-parse ()
<Arguments>
	^inp-data
</Arguments>
<Sequence> ()
<Define> ^result () </Define>
<For dec="inp-data">
	<set> ^result (cons
		(cons 'DIVIDE (divide-parse-2 (car inp-data)))
		result)
	</set>
	(Reverse result)
</For>
</Sequence>
</Define>

<Define> ^parse-xl-svg ()
<Arguments>
	^main-crd
	^inp-data
</Arguments>
<Sequence> ()
	(PrintStd "**** SVG DIVIDE LAYER FOR PP")
	<Define> ^svg-data () </Define>
	<Define> ^svg-layer-no 0 </Define>
	(divide-parse inp-data)
	<set> ^svg-data (Reverse svg-data) </set>

	^svg-data
</Sequence>
</Define>


<Define> ^layer-type ()
<Arguments> ^out </Arguments>
<Sequence> ()

<Define> ^sym (GetSymbolCore (car out)) </Define>
<Switch>
<Case> (= sym 'g)
	(layer-type (GE out 1))
</Case>
<Case> (= sym 'image)
	luster
</Case>
<Case> (= sym 'path)
	vector
</Case>
<Case> (= sym 'clipPath)
	vector
</Case>
<Case> 1
	(PrintStd "ERROR")
	(PrintStd out)
	<Sleep> 5 </Sleep>
	<Exit/>
</Case>
</Switch>
</Sequence>
</Define>

<Define> ^output-luster ()
<Arguments> ^target ^out-dir ^main-crd</Arguments>
<Export target="exl">

	<Load option="exec"> _graphics.xl </Load>

	<Define> ^target (Local target) </Define>
	<Define> ^out-dir (Local out-dir) </Define>
	<Define> ^main-crd (Local main-crd) </Define>

	<Define> ^layer-id (GE (GE target 'layer) 1) </Define>
	<Define> ^inp-data (cons 'svg
		(GE (GE target 'svg) 1))
	</Define>


	<Define> ^matrix-type ()
	<Arguments> ^type1 ^type2 ^mx </Arguments>
	<Sequence> ()
	<Switch>
	<Case> (And 	(= (GE mx 1) 1)
			(= (GE mx 2) 0)
			(= (GE mx 4) 0)
			(= (GE mx 5) 1))
		<set> ^type2 transform </set>
	</Case>
	<Case> (And	(= (GE mx 1) (GE mx 5))
			(= (GE mx 2) 0)
			(= (GE mx 4) 0))
		<set> ^type2 scale </set>
	</Case>
	</Switch>
	<Switch>
	<Case> (= type1 "transform")
		<Switch>
		<Case> (= type2 "transform")
			transform
		</Case>
		<Case> (= type2 "rotate")
			rotate
		</Case>
		<Case> (= type2 "scale")
			rotate
		</Case>
		<Case> (= type2 "skewX")
			matrix
		</Case>
		<Case> (= type2 "skewY")
			matrix
		</Case>
		<Case> (= type2 "matrix")
			matrix
		</Case>
		<Case> 1
			(PrintStd (+ "error " type2))
			<Sleep> 5 </Sleep>
			<Exit/>
		</Case>
		</Switch>
	</Case>
	<Case> (= type1 "rotate")
		<Switch>
		<Case> (= type2 "transform")
			rotate
		</Case>
		<Case> (= type2 "rotate")
			rotate
		</Case>
		<Case> (= type2 "scale")
			rotate
		</Case>
		<Case> (= type2 "skewX")
			matrix
		</Case>
		<Case> (= type2 "skewY")
			matrix
		</Case>
		<Case> (= type2 "matrix")
			matrix
		</Case>
		<Case> 1
			(PrintStd (+ "error " type2))
			<Sleep> 5 </Sleep>
			<Exit/>
		</Case>
		</Switch>
	</Case>
	<Case> (= type1 "matrix")
		matrix
	</Case>
	<Case> 1
		(PrintStd (+ "error " type1))
		<Sleep> 5 </Sleep>
		<Exit/>
	</Case>
	</Switch>
	</Sequence>
	</Define>

	<Define> ^set-matrix ()
	<Arguments> ^list </Arguments>
	<Sequence> ()
		<Define> ^type (GE list 0) </Define>
		<Define> ^len (ListLength list) </Define>
		<Switch>
		<Case> (= type "matrix")
			^'<matrix v="3" h="3">
			  ($ (GE list 1)) ($ (GE list 3)) ($ (GE list 5))
			  ($ (GE list 2)) ($ (GE list 4)) ($ (GE list 6))
			  0		  0		  1
			</matrix>
		</Case>
		<Case> (And (= type "translate") (= len 2))
			^'<matrix v="3" h="3">
			  1 0 ($ (GE list 1))
			  0 1 ($ (GE list 1))
			  0 0 1
			</matrix>
		</Case>
		<Case> (And (= type "translate") (= len 3))
			^'<matrix v="3" h="3">
			  1 0 ($ (GE list 1))
			  0 1 ($ (GE list 2))
			  0 0 1
			</matrix>
		</Case>
		<Case> (And (= type "scale") (= len 2))
			^'<matrix v="3" h="3">
			  ($ (GE list 1)) 0 		  0
			  0 		  ($ (GE list 1)) 0
			  0 		  0 		  1
			</matrix>
		</Case>
		<Case> (And (= type "scale") (= len 3))
			^'<matrix v="3" h="3">
			  ($ (GE list 1)) 0 		  0
			  0 		  ($ (GE list 2)) 0
			  0 		  0 		  1
			</matrix>
		</Case>
		<Case> (And (= type="rotate") (= len 2))
			<Define> ^deg ([SetUnit unit="degree"]
					(GE list 1)) </Define>
			^'<matrix v="3" h="3">
			  ($ (cos deg)) ($ (- (sin deg))) 0
			  ($ (sin deg)) ($ (cos deg))     0
			  0		0		  1
			</matrix>
		</Case>
		<Case> (And (= type="rotate") (= len 4))
			<Define> ^deg ([SetUnit unit="degree"]
					(GE list 1)) </Define>
			^'<matrix v="3" h="3">
			  ($ (cos deg)) ($ (- (sin deg))) ($ (GE list 2))
			  ($ (sin deg)) ($ (cos deg))     ($ (GE list 3))
			  0		0		  1
			</matrix>
		</Case>
		<Case> (= type "skewX")
			<Define> ^deg ([SetUnit unit="degree"]
					(GE list 1)) </Define>
			^'<matrix v="3" h="3">
			  1 ($ (tan deg)) 0
			  0 1		  0
			  0 0		  1
			</matrix>
		</Case>
		<Case> (= type "skewY")
			<Define> ^deg ([SetUnit unit="degree"]
					(GE list 1)) </Define>
			^'<matrix v="3" h="3">
			  0		0 0
			  ($ (tan deg)) 1 0
			  0		0 1
			</matrix>
		</Case>
		<Case> 1
			(PrintStd (+ "invalid type " type " " len))
			<Sleep> 5 </Sleep>
			<Exit/>
		</Case>
		</Switch>
	</Sequence>
	</Define>

	<Define> ^get-transform ()
	<Arguments> ^data </Arguments>
	<Sequence> ()
		<Define> ^ret () </Define>
		<Define> ^m <Melement size="3"/></Define>
		<Define> ^tr-type transform </Define>
		<RegSwitch> ^data
		<Case rex="[a-zA-Z]*">
			<set> ^ret (cons pattern
			 ret) </set>
		</Case>
		<Case rex="(\+|-)?[0-9]+(\.[0-9]*)?((e|E)(\+|-)?[0-9]+)?">
			<set> ^ret (cons
				([Cast type="floating"] pattern)
			ret) </set>
		</Case>
		<Case rex=" \t\r\n,\(">
			1
		</Case>
		<Case rex="\)">
			<set> ^ret (Reverse ret) </set>
			<Define> ^mm (set-matrix ret) </Define>
			<set> ^m (Mmul m mm) </set>
			<set> ^tr-type (matrix-type tr-type (car ret) mm)
			</set>
			<set> ^ret () </set>
		</Case>
		</RegSwitch>
		<set> ^ret (Reverse ret) </set>
		(List tr-type m)
	</Sequence>
	</Define>

	<Define> ^svg () ^sym  ()
	</Define>

	<Define> ^tr <Melement size="3"/> </Define>
	<Define> ^tr-type transform </Define>

	<Define Order="Normal"> ^g ^sym
	<Sequence> ()
		<If> (= (GetAttribute (car sym) "transform") () )
		<Else>
			<Define> ^m (get-transform transform) </Define>
			<set> ^tr (Mmul tr (GE m 1)) </set>
			<set> ^tr-type (matrix-type tr-type 
				(GE m 0) (GE m 1)) </set>
		</Else>
		</If>
		(ListEval () (cdr sym))
	</Sequence>
	</Define>

	<Define> ^image_tl_x 0 </Define>
	<Define> ^image_tl_y 0 </Define>
	<Define> ^image_br_x 0 </Define>
	<Define> ^image_br_y 0 </Define>
	<Define> ^image_width 0 </Define>
	<Define> ^image_height 0 </Define>
	<Define> ^image_file ^"" </Define>

	<Define> ^image ()
	<Arguments/>
	<Sequence> ()
		<If> (= (IfDef transform 0) 0)
		<Else>
		<Define> ^m (get-transform transform) </Define>
		<set> ^tr (Mmul tr (GE m 1)) </set>
		<set> ^tr-type (matrix-type tr-type
			(GE m 0) (GE m 1)) </set>
		</Else>
		</If>
		<If> (= (IfDef x "a") "a")
		<Else>
		<set> ^image_tl_x ([Cast type="floating"] x) </set>
		</Else>
		</If>
		<If> (= (IfDef y "a") "a")
		<Else>
		<set> ^image_tl_y ([Cast type="floating"] y) </set>
		</Else>
		</If>
		<set> ^image_br_x (+ image_tl_x
				([Cast type="floating"] width))
		</set>
		<set> ^image_br_y (+ image_tl_y
				([Cast type="floating"] height))
		</set>
		<set> ^image_width ([Cast type="floating"] width) </set>
		<set> ^image_height ([Cast type="floating"] height) </set>
		<set> ^image_file ^xlink:href </set>
	</Sequence>
	</Define>

	<Define> ^convert-to-cr ()
	<Arguments>
		^src ^dest
	</Arguments>
	<Sequence> ()
		<netpbm> ^src tmp.ppm </netpbm>
		<Shell pipe="on" cmdout="on">
			(+ "ppm2r64 -pr tmp.ppm tmp.r64 -l - -crsize 128 -cr "
				dest)
		</Shell>
		<Define> ^status <Parse encoding="utf-8">
		<Shell pipe="on" cmdout="on">
			^"r64status tmp.r64"
		</Shell>
		</Parse></Define>
		<Rm>^"tmp.ppm"</Rm>
		<Rm>^"tmp.r64"</Rm>
		^status
	</Sequence>
	</Define>

	<Eval> <CurrentEnvironment/> ^inp-data </Eval>
	<Define> ^src-pt1 ^'([matrix v="3"]
			($ image_tl_x)
			($ image_tl_y)
			1
	) </Define>
	<Define> ^src-pt2 ^'([matrix v="3"]
			($ image_br_x)
			($ image_tl_y)
			1
	) </Define>
	<Define> ^src-pt3 ^'([matrix v="3"]
			($ image_tl_x)
			($ image_br_y)
			1
	) </Define>
	<Define> ^src-pt4 ^'([matrix v="3"]
			($ image_br_x)
			($ image_br_y)
			1
	) </Define>
	<Define> ^dest-pt1 (Mmul tr src-pt1) </Define>
	<Define> ^dest-pt2 (Mmul tr src-pt2) </Define>
	<Define> ^dest-pt3 (Mmul tr src-pt3) </Define>
	<Define> ^dest-pt4 (Mmul tr src-pt4) </Define>

	<Define> ^src-pt1 ^'([matrix v="3"] 0 0 1) </Define>
	<Define> ^src-pt2 ^'([matrix v="3"] 
		($ image_width)
		0
		1
	) </Define>
	<Define> ^src-pt3 ^'([matrix v="3"]
		0 
		($ image_height)
		1
	) </Define>
	<Define> ^src-pt4 ^'([matrix v="3"]
		($ image_width) 
		($ image_height)
		1
	) </Define>
	<Switch>
	<Case> (= tr-type "transform")
		<Define> ^pt-list ^'(
			<point-map> a1
			<point> ($ (GE src-pt1 1))
				($ (GE src-pt1 2))
			</point>
			<point> ($ (GE dest-pt1 1))
				($ (GE dest-pt1 2))
			</point>
			</point-map>
			)
		</Define>
	</Case>
	<Case> (= tr-type "rotate")
		<Define> ^pt-list ^'(
			<point-map> a1
			<point> ($ (GE src-pt1 1))
				($ (GE src-pt1 2))
			</point>
			<point> ($ (GE dest-pt1 1))
				($ (GE dest-pt1 2))
			</point>
			</point-map>
			<point-map> a2
			<point> ($ (GE src-pt4 1))
				($ (GE src-pt4 2))
			</point>
			<point> ($ (GE dest-pt4 1))
				($ (GE dest-pt4 2))
			</point>
			</point-map>
			)
		</Define>
	</Case>
	<Case> (= tr-type "matrix")
		<Define> ^pt-list ^'(
			<point-map> a1
			<point> ($ (GE src-pt1 1))
				($ (GE src-pt1 2))
			</point>
			<point> ($ (GE dest-pt1 1))
				($ (GE dest-pt1 2))
			</point>
			</point-map>
			<point-map> a2
			<point> ($ (GE src-pt2 1))
				($ (GE src-pt2 2))
			</point>
			<point> ($ (GE dest-pt2 1))
				($ (GE dest-pt2 2))
			</point>
			</point-map>
			<point-map> a3
			<point> ($ (GE src-pt3 1))
				($ (GE src-pt3 2))
			</point>
			<point> ($ (GE dest-pt3 1))
				($ (GE dest-pt3 2))
			</point>
			</point-map>
			)
		</Define>
	</Case>
	</Switch>

	<Define> ^status 
	<convert-to-cr> ^image_file (+ out-dir "layer/layer" layer-id ".cr")
	</convert-to-cr></Define>

	<Define> ^dest-cr (+ "layer" layer-id ".cr") </Define>
	<Define> ^dest-lst (+ "layer" layer-id ".lst") </Define>
	<Define> ^dest-map (+ "layer" layer-id ".map") </Define>
	<Save   oflag="creat:trunc" mode="0644"
		option="xl"
		format.mode="xml"
		format.indent="on"
		format.multiroot="on"
		format.text="off"
		encoding="UTF-8">
		(+ out-dir "layer/" dest-lst)
		(quote(
		<?xl version="0.1" encoding="UTF-8"?>
		<luster>
		<meta>
		<file type="cr"> ($ dest-cr) </file>
		<scale> (/ 1 1.0) </scale>
		<scan-resolution> 
			($ (/ ([Cast type="floating"]
				(GE status 1))
				([SetUnit unit="m"] image_width)
				))
		</scan-resolution>
		</meta>
		</luster>
		))
	</Save>
	<Save   oflag="creat:trunc" mode="0644"
		option="xl"
		format.mode="xml"
		format.indent="on"
		format.multiroot="on"
		format.text="off"
		encoding="UTF-8">
		(+ out-dir "layer/" dest-map)
		(quote(
		<?xl version="0.1" encoding="UTF-8"?>
		($ (Append '
		<map>
		<meta>
		<file type="xl"/>
		<src> ($ dest-lst) </src>
		<dest> ($ (+ "../" main-crd)) </dest>
		<dp> ($ layer-id) </dp>
		</meta>
		</map>
		pt-list
		))
		))
	</Save>
</Export>
</Define>

<Define> ^output ()
<Arguments> ^filename ^svg </Arguments>
<Save   oflag="creat:trunc" mode="0644"
	option="xl"
	format.mode="xml"
	format.indent="on"
	format.multiroot="on"
	format.text="off"
	encoding="UTF-8">
	^filename
	(quote(
	<?xl version="0.1" encoding="UTF-8"?>
	<Define> ^clipPath () ^sym () </Define>
	($ svg)
	<OutputPP pp="foo.pp" />
	))
</Save>
</Define>


<Define> ^output-vector ()
<Arguments> ^target ^out-dir ^main-crd</Arguments>
<Sequence> ()
	<Define> ^layer-id (GE (GE target 'layer) 1) </Define>
	<Define> ^svg (cons 'svg
		(GE (GE target 'svg) 1))
	</Define>

	<Define> ^dest-pp (+ "layer" layer-id ".pp") </Define>
	<Define> ^dest-vct (+ "layer" layer-id ".vct") </Define>
	<Define> ^dest-map (+ "layer" layer-id ".map") </Define>

	<Define> ^result
		<Export target="svg2pp" type="normal">
			<Define> ^dest-pp
				(+ (Local out-dir) "layer/"
					(Local dest-pp))
			</Define>
			<Define> ^clipPath () ^sym () </Define>
			($ svg)
<!--			<ConnectPath/> -->
			<OutputPP pp="^dest-pp" />
		</Export>
	</Define>
	<If> (= result ())
	<Else>
	<Save   oflag="creat:trunc" mode="0644"
		option="xl"
		format.mode="lisp"
		format.indent="on"
		format.multiroot="on"
		format.text="off"
		encoding="UTF-8">
		(+ out-dir "layer/" dest-vct)
		(quote(
		<?xl version="0.1" encoding="UTF-8"?>
		<vector>
		<meta>
		<file type="pdb"> ($ dest-pp) </file>
		</meta>
		</vector>
		))
	</Save>
	<Save   oflag="creat:trunc" mode="0644"
		option="xl"
		format.mode="xml"
		format.indent="on"
		format.multiroot="on"
		format.text="off"
		encoding="UTF-8">
		(+ out-dir "layer/" dest-map)
		(quote(
		<?xl version="0.1" encoding="UTF-8"?>
		<map>
		<meta>
		<file type="xl"/>
		<src> ($ dest-vct) </src>
		<dest> ($ (+ "../" main-crd)) </dest>
		<dp> ($ layer-id) </dp>
		</meta>
		<point-map> a1
			<point> 0m 0m </point>
			<point> 0m 0m </point>
		</point-map>
		</map>
		))
	</Save>
	</Else>
	</If>
	^result
</Sequence>
</Define>


<Define> ^output-main-crd ()
<Arguments> 
	^out-dir
	^main-crd
	^header
	^md-encoding
	^meta-data
	^button-list
</Arguments>
<Sequence> ()

<Define> ^get-minrect ()
<Arguments> ^header </Arguments>
<Sequence> ()
	<Define> ^vb (GE (GE header 'viewBox) 1) </Define>
	^'(
	( ($ (GE vb 0)) ($ (GE vb 1)) )
	( ($ (GE vb 2)) ($ (GE vb 3)) )
	)
</Sequence>
</Define>

<Save   oflag="creat:trunc" mode="0644"
	option="xl"
	format.mode="xml"
	format.indent="on"
	format.multiroot="on"
	format.text="off"
	encoding="^md-encoding">
	(+ out-dir main-crd)
	(quote(
	<?xl version="0.1" encoding="$md-encoding"?>
	($ (Append
	'<coordinate>
	<meta>
	($ meta-data)
	<file type="e2d"/>
	<mr> ($ (get-minrect header)) </mr>
	<v>
	<resolution> ($ (GE (GE header 'resolution) 1)) </resolution>
	</v>
	</meta>
	</coordinate>
	button-list
	))
	))
</Save>
</Sequence>
</Define>

<Define> ^output-xl-svg ()
<Arguments>
	^header ^inp-data ^out-dir ^main-crd ^md-encoding ^meta-data
	^button-list
</Arguments>
<Sequence> ()
(PrintStd "**** OUTPUT PP")
<ErrHandler>
<Rm>(+ out-dir "layer/*")</Rm>
</ErrHandler>
<If> (= out-dir "")
<Else><ErrHandler>
<Mkdir>^out-dir</Mkdir>
</ErrHandler></Else></If>
<ErrHandler>
<Mkdir>(+ ^out-dir "layer")</Mkdir>
</ErrHandler>
<Define> ^result () </Define>
<For dec="inp-data">
	<Define> ^target (car inp-data) </Define>
	<Define> ^id-list (cdr (GE target 'id)) </Define>
	<Define> ^svg (GE (GE target 'svg) 1) </Define>
	<Define> ^type (layer-type (car svg)) </Define>
	<Define> ^prt PROCESSING... </Define>
	<For dec="id-list">
		<set> ^prt (+ prt (car id-list) ",") </set>
	</For>
	(PrintStd (+ prt "-->" type ))

	<Switch>
	<Case> (= type "luster")
		(output-luster target out-dir main-crd)
	</Case>
	<Case> (= type "vector")
		<set> ^result
		(output-vector target out-dir main-crd)
		</set>
	</Case>
	<Case> 1
		(PrintStd (+ "ERROR " type))
		<Sleep> 5 </Sleep>
		<Exit/>
	</Case>
	</Switch>
</For>
(output-main-crd out-dir main-crd header md-encoding meta-data button-list)
</Sequence>
</Define>


<Define> ^get-svg-parameter ()
<Arguments>
	^header
	^scale
</Arguments>
<Sequence> ()
	<Define> ^viewBox () </Define>
	<RegSwitch> (GetAttribute header "viewBox")
	<Case rex="\t\r\n ">
		1
	</Case>
	<Case rex="(\+|-)?[0-9]+(\.[0-9]*)?((e|E)(\+|-)?[0-9]+)?">
		<set> ^viewBox (cons 
			(* ([Cast type="floating"] pattern) scale)
			viewBox)
		</set>
	</Case>
	</RegSwitch>
	<Define> ^viewBox (Reverse viewBox) </Define>
	<Define> ^L1 (- (GE viewBox 2) (GE viewBox 0)) </Define>
	<Define> ^L2 (- (GE viewBox 3) (GE viewBox 1)) </Define>
	<If> (> L1 L2) <Then>
		<set> ^L1 ^L2 </set>
	</Then></If>
	^'<svg-info>
		<viewBox> ($ viewBox) </viewBox>
		<resolution> ($ (/ 10dot L1)) </resolution>
	</svg-info>
</Sequence>
</Define>


<Define> ^output-main-mapping ()
<Arguments> ^out-dir ^name ^url-from ^url-to ^point-list </Arguments>
<Sequence> ()
	<Define> ^name
		<RegSwitch> ^name
		<Case rex="\.crd">
			.map
		</Case>
		<Case rex=".">
			^pattern
		</Case>
		<Case rex="\.crd.">
			^pattern
		</Case>
		</RegSwitch>
	</Define>

	<Define> ^pt-list () </Define>
	<Define> ^pt-name 1 </Define>
	<For dec="point-list">
		<Define> ^pt (car point-list) </Define>
		<set> ^pt-list (cons
			'<point-map>
				($ (+ "a" pt-name))
				<point>
				($ (GE pt 0)) ($ (GE pt 1))
				</point>
				<point>
				($ (GE pt 2)) ($ (GE pt 3))
				</point>
			</point-map>
		pt-list) </set>
		<set> ^pt-name (+ pt-name 1) </set>
	</For>

	<Save   oflag="creat:trunc" mode="0644"
		option="xl"
		format.mode="xml"
		format.indent="on"
		format.multiroot="on"
		format.text="off"
		encoding="UTF-8">
		(+ out-dir name)
		(quote(
		<?xl version="0.1" encoding="UTF-8"?>
		($ (Append '
		<map>
		<meta>
		<file type="xl"/>
		<src> ($ url-from) </src>
		<dest> ($ url-to) </dest>
		<dp> ($ 100) </dp>
		</meta>
		</map>
		pt-list
		))
		))
	</Save>
</Sequence>
</Define>