1<?php
2/**
3 * Library functions for SketchCanvas server-side renderer
4 */
5
6function parsePointList($str){
7	$pl = explode(":", $str);
8	$ret = array();
9	foreach ($pl as $key => $value) {
10		$point = explode(",", $value);
11		foreach ($point as $idx => $ivalue) {
12			$point[$idx] = intval($ivalue);
13		}
14		array_push($ret, $point);
15	}
16	return $ret;
17}
18
19/// See PathShape.prototype.deserialize in SketchCanvas.js
20function parsePathCommands($str){
21	$cmds = preg_split("/[MCLS]/", $str);
22
23	$ret = array();
24	foreach($cmds as $i => $sub){
25		if($sub === "")
26			continue;
27
28		$pt2 = preg_split("/[\s,]/", $sub);
29
30		$pt = array();
31		if(6 <= count($pt2)){
32			$pt["cx"] = floatval($pt2[0]);
33			$pt["cy"] = floatval($pt2[1]);
34		}
35		if(4 <= count($pt2)){
36			$pt["dx"] = floatval($pt2[count($pt2)-4]);
37			$pt["dy"] = floatval($pt2[count($pt2)-3]);
38		}
39		$pt["x"] = floatval($pt2[count($pt2)-2]);
40		$pt["y"] = floatval($pt2[count($pt2)-1]);
41		array_push($ret, $pt);
42	}
43	return $ret;
44}
45
46/// @brief Converts a sequence (array) to a set (object)
47///
48/// Due to a limitation in js-yaml JavaScript library, a set is represented
49/// as a sequence in the YAML document.  PHP neither supports set type,
50/// so let's port the function from JavaScript to PHP.
51///
52/// @sa set2seq
53function seq2set($seq){
54	$ret = array();
55	for($i = 0; $i < count($seq); $i++){
56		if($seq[$i] === "")
57			continue;
58		$ret[$seq[$i]] = TRUE;
59	}
60	return $ret;
61}
62
63/// @brief Converts a set (object) to a sequence (array)
64/// @sa seq2set
65function set2seq($set){
66	$ret = array();
67	foreach ($set as $i) {
68		if(i === "")
69			continue;
70		$ret[] = $i;
71	}
72	return $ret;
73}
74