<?xl version="0.1" encoding="EUC-JP"?>


<Define> ^csv-encoding Shift-JIS </Define>

<Load option="exec"> _metadata.xl </Load>
<Load option="exec"> _csv.xl </Load>
<Load option="exec"> _svg2pp.xl </Load>

<Define> ^cmd-file (csv-tune (loading-csv csv-encoding (Arg 1))) </Define>

<Define> ^svg-data () </Define>
<Define> ^out-dir ^"" </Define>
<Define> ^main-crd main.crd </Define>
<Define> ^scale 1m </Define>
<Define> ^meta-data () </Define>
<Define> ^mapping-url "" </Define>
<Define> ^mapping-points () </Define>
<Define> ^button-list () </Define>
<Define> ^marge-list () </Define>
<Define> ^bname 1 </Define>

<!-- Execute csv file commands -->

<Define> ^parse-mapping-point ()
<Arguments> ^ptr </Arguments>
<Sequence> ()
	<Define> ^ret () </Define>
	<RegSwitch> ^ptr
	<Case rex="\(">
		1
	</Case>
	<Case rex="(\+|-)?[0-9]+(\.[0-9]*)?((e|E)(\+|-)?[0-9]+)?">
		<set> ^ret (cons ([Cast type="floating"] pattern) ret)
		</set>
	</Case>
	<Case rex="\)">
		1
	</Case>
	<Case rex=" \t\r\n,">
		1
	</Case>
	</RegSwitch>
	(Reverse ret)
</Sequence>
</Define>

<For dec="cmd-file">
<Define> ^cmd (car cmd-file) </Define>
<Switch>
<Case> (= cmd ())
	1
</Case>
<Case> (= (car cmd) ".svgfile")
	<!-- illustrator's SVG is not UTF-8. -->
	<set> ^svg-data (GE ([Load option="xl" encoding="ascii"]
				(GE cmd 1)) 
				'svg) </set>
</Case>
<Case> (= (car cmd) ".outputdir")
	<set> ^out-dir (GE cmd 1) </set>
	<If> (= (CutOut out-dir (- (StringLength out-dir) 1) 1)
		"/")
	<Else>
	<set> ^out-dir (+ out-dir "/") </set>
	</Else>
	</If>
</Case>
<Case> (= (car cmd) ".maincrd")
	<set> ^main-crd (GE cmd 1) </set>
</Case>
<Case> (= (car cmd) ".scale")
	<set> ^scale ([Cast type="floating"] (GE cmd 1)) </set>
</Case>
<Case> (= (car cmd) ".meta")
	<set> ^meta-data (cons (List (GE cmd 1) (GE cmd 2) (GE cmd 3))
		meta-data) </set>
</Case>
<Case> (= (car cmd) ".mapping.url")
	<set> ^mapping-url (GE cmd 1) </set>
</Case>
<Case> (= (car cmd) ".mapping.point")
	<set> ^mapping-points (cons
		(Append (parse-mapping-point (GE cmd 1))
			(parse-mapping-point (GE cmd 2)))
		mapping-points) </set>
</Case>
<Case> (= (car cmd) ".button")
	<Define> ^c (cdr (cdr cmd)) </Define>
	<Define> ^button-layer () </Define>
	<For dec="c">
		<Define> ^d (car c) </Define>
		<If> (= (CutString d 0 1) ":")
			<Then> <Break/> </Then>	</If>
		<set> ^button-layer (cons d button-layer) </set>
	</For>
	<set> ^button-list (cons
	'<bl>
		<type> ($ (GE cmd 1)) </type>
		<mode> ($ (CutString (car c) 1 
			(StringLength (car c)))) </mode>
		<name> ($ bname) </name>
		<button-layer> ($ button-layer) </button-layer>
		<active-layer> ($ (cdr c)) </active-layer>
	</bl>
	button-list) </set>
	<set> ^bname (+ bname 1) </set>
</Case>
<Case> (= (car cmd) ".marge")
	<set> ^marge-list (cons (cdr cmd) marge-list) </set>
</Case>
<Case> (= (CutString (car cmd) 0 1) "#")
	1
</Case>
<Case> (= (car cmd) "")
	1
</Case>
<Case> 1
	(PrintStd (+ "invalid command :: " (car cmd)))
	(PrintStd cmd)
	<Exit/>
</Case>
</Switch>
</For>

<!-- output main mapping -->

<output-main-mapping>
	^out-dir
	^main-crd
	^main-crd
	^mapping-url
	^mapping-points
</output-main-mapping>


<!-- preparing SVG data :: changing illustrator SVG -->

<Define> ^id-redefine ()
<Arguments>
	^id
</Arguments>
<Sequence> ()
<Define> ^ret () </Define>
	<RegSwitch> ^id
	<Case 
	  rex="_x0000__x[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]_">
		<set> ^ret (cons
			(CutString pattern 7 7)
			ret)
		</set>
	</Case>
	<Case rex="_x[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]_">
		<set> ^ret (cons 
			([Cast type="integer"]
				(+ "0x" (CutString pattern 4 2)))
			ret)
		</set>
	</Case>
	<Case rex=".">
		<set> ^ret (cons pattern ret) </set>
	</Case>
	</RegSwitch>
	([Word2String encoding="Shift-JIS" word="1"] (Reverse ret))
</Sequence>
</Define>

<Define> ^svg-header (car svg-data) </Define>
<Define> ^sd (cdr svg-data) </Define>
<Define> ^svg-data () </Define>

<For dec="sd">
<Define> ^g (car sd) </Define>
<Define> ^gg (car g) </Define>
<Switch>
<Case> (= (GetSymbolCore gg) ([Cast type="symbol"] "g"))
	<Define> ^id (id-redefine (GetAttribute gg "id")) </Define>
	(PrintStd (+ "LAYER " id))
	<set> ^svg-data (cons 
		'<d>
			<id> ($ id) </id>
			<svg> ($ g) </svg>
		</d>
		svg-data)
	</set>
</Case>
</Switch>
</For>

<Define> ^svg-data (Reverse svg-data) </Define>


<!-- some functions for scaling -->

<Define> ^delete-layer ()
<Arguments>
	^layer-name
</Arguments>
<Sequence> ()
	<Define> ^ret () </Define>
	<For dec="svg-data">
		<Define> ^e (car svg-data) </Define>
		<If> (= (GE (GE e 'id) 1) layer-name)
		<Else>
			<set> ^ret (cons e ret) </set>
		</Else></If>
	</For>
	<set> ^svg-data (Reverse ret) </set>
</Sequence>
</Define>

<Define> ^find-layer ()
<Arguments>
	^layer-name
</Arguments>
<Sequence>()
	<Define> ^ptr ^svg-data </Define>
	<For dec="ptr">
		<Define> ^e (car ptr) </Define>
		<If> (= (GE (GE e 'id) 1) layer-name)
		<Then><Break> ^e </Break></Then></If>
	</For>
</Sequence>
</Define>

<Define> ^replace-layer ()
<Arguments>
	^new-layer
</Arguments>
<Sequence> ()
	<Define> ^id (GE (GE new-layer 'id) 1) </Define>
	<Define> ^ptr () </Define>
	<For dec="svg-data">
		<If> (= id (GE (GE (car svg-data) 'id) 1))
		<Then>
			<set> ^ptr (cons new-layer ptr) </set>
			<set> ^svg-data (cdr svg-data) </set>
			<Break/>
		</Then>
		<Else>
			<set> ^ptr (cons (car svg-data) ptr) </set>
		</Else>
		</If>
	</For>
	<For dec="svg-data">
		<set> ^ptr (cons (car svg-data) ptr) </set>
	</For>
	<set> ^svg-data (Reverse ptr) </set>
</Sequence>
</Define>

<Define> ^get-scale-line ()
<Arguments>
</Arguments>
<Sequence> ()
<Define> ^scale-g (find-layer "scale") </Define>

<Define Order="Normal"> ^g ^sym
<Sequence> ()
	<Eval><CurrentEnvironment/> (GE sym 1) </Eval>
</Sequence>
</Define>

<Define> ^path ()
<Arguments/>
<Sequence>  ()
<Export target="exl">
	<Define> ^d (Local d) </Define>
(PrintStd (+ "path :: " d))

	<Define> ^pt () </Define>
	<Define> ^dd () </Define>
	<Define> ^last-char "" </Define>

	<Define> ^set-ptr ()
	<Arguments/>
	<Sequence> ()
	<set> ^dd (Reverse dd) </set>
	<Switch>
	<Case> (Or (= last-char "M") (= last-char "L"))
		<set> ^pt (cons '<matrix h="2">
			($ (GE dd 0)) ($ (GE dd 1))
			</matrix> pt) </set>
	</Case>
	<Case> (= pt ())
		1
	</Case>
	<Case> (Or (= last-char "m") (= last-char "l"))
		<set> ^pt (cons (Madd (car pt)
			'<matrix h="2">
			  ($ (GE dd 0)) ($ (GE dd 1))
			</matrix>) pt)
		</set>
	</Case>
	<Case> (= last-char "V")
		<set> ^pt (cons
			'<matrix h="2">
			  0 ($ (GE dd 0))
			</matrix> pt)
		</set>
	</Case>
	<Case> (= last-char "v")
		<set> ^pt (cons (Madd (car pt)
			'<matrix h="2">
			  0 ($ (GE dd 0))
			</matrix>) pt)
		</set>
	</Case>
	<Case> (= last-char "H")
		<set> ^pt (cons
			'<matrix h="2">
			  ($ (GE dd 0)) 0
			</matrix> pt)
		</set>
	</Case>
	<Case> (= last-char "h")
		<set> ^pt (cons (Madd (car pt)
			'<matrix h="2">
			  ($ (GE dd 0)) 0
			</matrix>) pt)
		</set>
	</Case>
	</Switch>
	<set> ^dd () </set>
	</Sequence>
	</Define>

	<RegSwitch> ^d	
	<Case rex="M">
		<set> ^last-char M </set>
	</Case>
	<Case rex="m">
		<set> ^last-char m </set>
	</Case>
	<Case rex="L">
		<set-ptr/>
		<set> ^last-char L </set>
	</Case>
	<Case rex="l">
		<set-ptr/>
		<set> ^last-char l </set>
	</Case>
	<Case rex="V">
		<set-ptr/>
		<set> ^last-char V </set>
	</Case>
	<Case rex="v">
		<set-ptr/>
		<set> ^last-char v </set>
	</Case>
	<Case rex="H">
		<set-ptr/>
		<set> ^last-char H </set>
	</Case>
	<Case rex="h">
		<set-ptr/>
		<set> ^last-char h </set>
	</Case>
	<Case rex="(\+|-)?[0-9]+(\.[0-9]*)?((e|E)(\+|-)?[0-9]+)?">
		<set> ^dd (cons ([Cast type="floating"] pattern) dd) </set>
	</Case>
	<Case rex=" \t\n\r,">
		1
	</Case>
	</RegSwitch>
	<set-ptr/>
	<set> ^pt (Reverse pt) </set>
	<If> (> 2 (ListLength pt))
	<Then>
		(PrintStd "invalid scale layer")
		(PrintStd pt)
	</Then>
	<Else>
		<Define> ^v (Msub (GE pt 0) (GE pt 1)) </Define>
		(sqrt (Minner v v))
	</Else>
	</If>
</Export>
</Sequence>
</Define>

<Eval> <CurrentEnvironment/> (GE (GE scale-g 'svg) 1) </Eval>

</Sequence>
</Define>

<If> (Not (= (find-layer "scale") ()))
<Then>

<set> ^scale (/ scale (get-scale-line)) </set>
(PrintStd (+ "SCALE = " scale "/illustrator pixel"))

(delete-layer "scale")


<Sequence> (NewEnvironment(CurrentEnvironment))
<Define> ^d ^svg-data </Define>
<Define> ^ret () </Define>

(PrintStd "delete scale")

<For dec="d">
	<Define> ^svg (GE (GE (car d) 'svg) 1) </Define>
	<Define> ^id (GE (car d) 'id) </Define>
	<Define> ^transform (+ "scale(" (SetUnit scale) ")") </Define>
	<If> (= (GetType (car svg)) "symbol")
	<Then>
	<set> ^ret (cons
	'<d>
		($ id) 
		<svg>
			<g transform="$transform"> ($ svg)
			</g>
		</svg>
	</d>
	ret) </set>
	</Then>
	<Else>
	<set> ^ret (cons
	'<d>
		($ id)
		<svg>
			($ (cons [g transform="$transform"] svg))
		</svg>
	</d>
	ret) </set>
	</Else>
	</If>
</For>
<set> ^svg-data (Reverse ret) </set>

</Sequence>

</Then>
</If>

<!-- layer button manage -->


<!-- naming the path -->
<Sequence> (NewEnvironment (CurrentEnvironment))

<Define> ^g () ^sym ^sym </Define>
<Define> ^clipPath () ^sym ^sym </Define>
<Define> ^path () ^sym
	(SetAttribute sym "id" bname)
</Define>

<Define> ^b ^button-list </Define>
<For dec="b">
	<Define> ^bll (GE (GE (car b) 'button-layer) 1) </Define>
	<For dec="bll">
		<Define> ^bl (car bll) </Define>
		<Define> ^d (find-layer bl) </Define>
		<Define> ^svg (GE (GE d 'svg) 1) </Define>
		<Define> ^bname (+ "b" (GE (GE (car b) 'name) 1))</Define>

		<Define> ^svg (Eval (CurrentEnvironment) svg) </Define>

		(replace-layer
			'<d>
				<id> ($ (GE (GE d 'id) 1)) </id>
				<svg> ($ svg) </svg>
			</d>)
	</For>

</For>

</Sequence>

<!-- Checking Metadata -->

<Define> ^meta-data (setup-metadata meta-data) </Define>


<!-- Marge SVG -->

(PrintStd marge-list)

<Sequence> (NewEnvironment(CurrentEnvironment))

<set> ^marge-list (Reverse marge-list) </set>

<Define> ^m ^marge-list </Define>

<For dec="m">
	<Define> ^mm (car m) </Define>
	<Define> ^svg () </Define>
	<Define> ^id () </Define>
	<Define> ^up-down (car mm) </Define>
	<Define> ^mm (cdr mm) </Define>
	<For dec="mm">
		<Define> ^layer (car mm) </Define>
		<Define> ^dd (find-layer layer) </Define>
		(delete-layer layer)
		<Define> ^s (GE (GE dd 'svg) 1) </Define>
		<Define> ^i (cdr (GE dd 'id)) </Define>
		<set> ^id (Append id i) </set>
		<Switch>
		<Case> (= (GetType (car s)) "symbol")
			<set> ^svg (Append svg (cons s ())) </set>
		</Case>
		<Case> (= (GetType (car s)) "pair")
			<set> ^svg (Append svg s) </set>
		</Case>
		<Case> 1
			(PrintStd (+ "invalid svg type "
				(GetType (car s))))
			<Exit/>
		</Case>
		</Switch>
	</For>
	<set> ^id (cons 'id id) </set>
	<If> (= (cdr svg) ())
	<Then> <set> ^svg (cons 'svg svg) </set></Then>
	<Else> <set> ^svg (List 'svg (cons 'g svg)) </set></Else>
	</If>
	<If> (= up-down "up") <Then>
		<set> ^svg-data (Append svg-data (cons
			'<d>
				($ id)
				($ svg)
			</d>
		() )) </set>
	</Then>
	<Else>
		<set> ^svg-data (cons
			'<d>
				($ id)
				($ svg)
			</d>
		svg-data) </set>
	</Else>
	</If>
</For>

</Sequence>

<!-- SVG processing  DIVIDE -->

<Define> ^svg-data (parse-xl-svg main-crd svg-data)
</Define>

<!-- BUTTON processing -->

<Sequence> (NewEnvironment (CurrentEnvironment))

<Define> ^b ^button-list </Define>
<Define> ^bbb () </Define>

<Define> ^id-exist? ()
<Arguments> ^id ^a </Arguments>
<Sequence> ()
	<If> (= 1
	<For dec="a">
		<If> (= id (car a)) <Then>
			<Break> 1 </Break>
		</Then></If>
	</For>)
	<Then> 1 </Then>
	<Else> 0 </Else>
	</If>
</Sequence>
</Define>

<Define> ^get-layers-name-and-type ()
<Arguments> ^a </Arguments>
<Sequence> ()
	<Define> ^on () </Define>
	<Define> ^off () </Define>
	<Define> ^d ^svg-data </Define>
	<For dec="d">
		<Define> ^dd (car d) </Define>
		<Define> ^id (cdr (GE dd 'id)) </Define>
		<Define> ^exit-flag 0 </Define>
		<Define> ^l-type (layer-type (car (GE (GE dd 'svg) 1)))
		</Define>
		<Switch>
		<Case> (= l-type "luster")
			<set> ^l-type lst </set>
		</Case>
		<Case> (= l-type "vector")
			<set> ^l-type vct </set>
		</Case>
		<Case> 1
			(PrintStd (+ "l-type error " l-type))
			<Exit/>
		</Case>
		</Switch>
		<If> (= 1
			<For dec="id">
				<If> (id-exist? (car id) a)
				<Then> <Break> 1 </Break> </Then>
				</If>
			</For>)
		<Then>
			<set> ^on (cons
				(List
					(GE (GE dd 'layer) 1)
					l-type)
				on) </set>
		</Then>
		<Else>
			<set> ^off (cons
				(List
					(GE (GE dd 'layer) 1)
					l-type)
				off) </set>
		</Else>
		</If>
	</For>
	(List on off)
</Sequence>
</Define>

<For dec="b">
	<Define> ^bb (car b) </Define>
	<Define> ^active-layer (GE (GE bb 'active-layer) 1) </Define>
	<Define> ^ret (get-layers-name-and-type active-layer) </Define>
	<Define> ^e (cons (car bb) (cons '(on-off-list ($ ret)) (cdr bb)))
	</Define>
	<set> ^bbb (cons e bbb) </set>
</For>

<set> ^button-list (Reverse bbb) </set>

<Define> ^setup-action ()
<Arguments> ^b </Arguments>
<Sequence> ()
<Define> ^lst (GE (GE b 'on-off-list) 1) </Define>
<Define> ^on (GE lst 0) </Define>
<Define> ^off (GE lst 1) </Define>

<Define> ^off-list () </Define>
<For dec="off">
	<Define> ^file (car off) </Define>
	<set> ^off-list (cons 
		(+ "layer/layer" (GE file 0) "." (GE file 1))
	off-list) </set>
</For>
<Define> ^on-list () </Define>
<For dec="on">
	<Define> ^file (car on) </Define>
	<set> ^on-list (cons 
		(+ "layer/layer" (GE file 0) "." (GE file 1))
	on-list) </set>
</For>
<Switch>
<Case> (And (= on-list ()) (= off-list ()))
	()
</Case>
<Case> (= on-list ())
	<List>
	(cons '[gv-set-status indicate="off"] off-list)
	</List>
</Case>
<Case> (= off-list ())
	<List>
	(cons '[gv-set-status indicate="on"] on-list)
	</List>
</Case>
<Case> 1
	<List>
	(cons '[gv-set-status indicate="on"] on-list)
	(cons '[gv-set-status indicate="off"] off-list)
	</List>
</Case>
</Switch>
</Sequence>
</Define>

<Define> ^b ^button-list </Define>
<Define> ^initial (setup-action (car b)) </Define>

<Define> ^case-list () </Define>


<For dec="b">
	<Define> ^bb (car b) </Define>
	<Define> ^action (setup-action bb) </Define>

	<Define> ^target (+ "b" (GE (GE bb 'name) 1)) </Define>
	<Define> ^type (GE (GE bb 'type) 1) </Define>
	<Define> ^mode (GE (GE bb 'mode) 1) </Define>

	<set> ^case-list (cons
		(cons '[case action="$type" mode="$mode" target="$target"]
			action)
	case-list)
	</set>

	<If> (= (GE (GE bb 'mode) 1) "initial")
	<Then> <set> ^initial ^action </set> </Then>
	</If>
</For>

<set> ^button-list ^'(
	($ (Append
	'<initial>
		<gv-set-status indicate="off" target="object"/>
		<gv-set-status indicate="auto" mode="initial" target="self"/>
	</initial>
		initial))
	($ (cons 'button-action	case-list))
) </set>

</Sequence>

<!-- SVG processing  OUTPUT -->

<output-xl-svg>
	(get-svg-parameter svg-header scale)
	^svg-data
	^out-dir
	^main-crd
	^csv-encoding
	^meta-data
	^button-list
</output-xl-svg>

