<?php

$v
=new note2svg($argv);
$v->load();
$v->parse();

class 
note2svg {

    var 
$filename='.penNotes.strokes_data';
    var 
$outfilename='penNote';
    var 
$filedata=null;
    
    var 
$colors=array("#0000FF","#FF0000","#00FF00","#FFFF00","#FFFFFF","#000000");
    
    var 
$bgColor=5;
    
    function 
__construct($argv) {
        if (isset(
$argv[1]) && $argv[1]!='') {
            
$this->filename=$argv[1];
        }
        
        if (isset(
$argv[2]) && $argv[2]!='') {
            
$this->outfilename=$argv[2];
        }
    }
    
    function 
load() {
        if (!
file_exists($this->filename)) {
            echo 
"No such file: ".$this->filename.".\n";
            exit(
1);
        }
        
$this->filedata=file($this->filename);
        if (!
is_array($this->filedata) || count($this->filedata)<1) {
            echo 
"Error reading file, or file is empty.\n";
            exit(
1);
        }
    }
    
    function 
parse() {
        
$count=0;
        
$note=array();
        foreach(
$this->filedata as $line) {
            
$fields=explode(',',$line);
            if (
count($fields)>2) {
                
$fgColor=trim($fields[0]);
                
$this->bgColor=trim($fields[1]);
                
$size=explode(';',$fields[2]);
                
$size=trim($size[0]);
                
$fields=explode(';',$line);
                if (
count($fields)>1) {
                    
$coords=trim($fields[1]);
                    if (
$coords!='') {
                        
$coords=explode(' ',$coords);
                        
$note[]=array(
                            
'size'=>$size,
                            
'fgColor'=>$fgColor,
                            
'bgColor'=>$this->bgColor,
                            
'coords'=>$coords,
                        );
                    }
                }
            } else {
                if (
count($note)>0) {
                    
$this->save_as_svg($note,$count);
                }
                
$note=array();
                
$count++;
            }
        }
    }
    
    function 
save_as_svg($note,$notenum) {
        
$doc=new DOMDocument('1.0','UTF-8');
        
$svg=$doc->createElement('svg');
        
$doc->appendChild($svg);
        
$svg->setAttribute('xmlns','http://www.w3.org/2000/svg');
        
$ns='http://www.inkscape.org/namespaces/inkscape';
        
$svg->setAttributeNS('http://www.w3.org/2000/svg','xmlns:inkscape',$ns);
        
        
$notenumstr=str_pad($notenum,4,'0',STR_PAD_LEFT);
        
        
$mx=10;$my=10;
        foreach(
$note as $stroke) {
            foreach(
$stroke['coords'] as $coord) {
                
$c=explode(',',$coord);
                
$mx=max($mx,$c[0]);
                
$my=max($my,$c[1]);
            }
        }
        if (
$mx<=480 && $my<=640) {
            
$mx=480;
            
$my=640;
        } elseif (
$mx<=640 && $my<=480) {
            
$mx=640;
            
$my=480;
        } else {
            
$mx+=10;
            
$my+=10;
        }
        
        
$svg->setAttribute('width',$mx);
        
$svg->setAttribute('height',$my);
        
        
$svg->appendChild($layer=$doc->createElement('g'));
        
$layer->setAttributeNS($ns,'inkscape:label','Note '.$notenumstr);
        
$layer->setAttributeNS($ns,'inkscape:groupmode','layer');
        
$layer->setAttribute('id','layer1');
        
        
$layer->appendChild($group=$doc->createElement('g'));
        
$group->setAttribute('id','note'.$notenumstr);
        
$group->setAttribute('style','fill:none');
        
        
$group->appendChild($bg=$doc->createElement('rect'));
        
$bg->setAttribute('style','fill:'.$this->colors[$this->bgColor]);
        
$bg->setAttribute('id','background-color');
        
$bg->setAttribute('x','0');
        
$bg->setAttribute('y','0');
        
$bg->setAttribute('width',$mx);
        
$bg->setAttribute('height',$my);
        
        foreach(
$note as $stroke) {
            
$group->appendChild($path=$doc->createElement('path'));
            
$path->setAttribute('d','M '.implode(' L ',$stroke['coords']));
            
$path->setAttribute('style',
                
'stroke:'.$this->colors[$stroke['fgColor']]
                .
';stroke-width:'.$stroke['size'].'px'
            
);
        }
        
        
$xml=$doc->saveXML();
        
file_put_contents($this->outfilename.$notenumstr.'.svg',$xml);
        echo 
"Saved note ".$notenum."\n";
    }
    
}

?>