#! /usr/bin/perl

# 13/03/2005 Laurent FACQ - REAUMUR - Universite de Bordeaux - facq@u-bordeaux.fr

### This is a first quick&dirty attempt to generate a .sxi openoffice file from a .dot file
### with positions


# neato graph2.neato > gr2.dot
##  ./dot2sxi.pl gr2.dot 

# ../dot2sxi.pl ../gr2.dot  > content.xml ;   rm ../test2.sxi ;zip -r ../test2.sxi * 

unlink("/tmp/res.sxi");
unlink("/tmp/res");

while ($l=<>)
{
    if ($l =~ m/bb="\d/)
    { # bounding box
	($bbxmin,$bbymin,$bbxmax,$bbymax)= ($l =~ m/bb="(\d+),(\d+),(\d+),(\d+)"/);
	#print "BB : $l - $bbxmin $bbxmax $bbymin $bbymax\n";
	
    }

#    if ($l =~ m/label=""/) 
#    {# pseudo nodes
#	($name)= ($l =~ m/\s+(\S+)\s+/);
#        $pnode{$name} 
#	next; 
#    } ### TMP Skip pseudo nodes for links/edges

    if ($l =~ m/pos="\d/)
    { # node
	($name)= ($l =~ m/\s+(\S+)\s+/);
	
	if ($l =~ m/label=""/) { $node{$name}{edge}= 1; }
	
	($x,$y)= ($l =~ m/pos="(\d+),(\d+)"/);
	$node{$name}{x}= $x;
	$node{$name}{y}= $y;
    }
    if ($l =~ m/pos="e,\d/)
    { # edge
	($from,$to)= ($l =~ m/\s+(\S+)\s+->\s+(\S+)\s+/);
        #print STDERR "$from $to\n";
	# todo
	$edge{$from."--".$to}{from}=$from;
	$edge{$from."--".$to}{to}=$to;
    }
}

$xsize= 30;
$ysize= 30;

$drawid= 10;

sub nodeposx
{
    my ($node)=@_;
    
    return (($node{$node}{x}-$bbxmin)/($bbxmax-$bbxmin))*$xsize;
}
sub nodeposy
{
    my ($node)=@_;
    
    return (($node{$node}{y}-$bbymin)/($bbymax-$bbymin))*$ysize;
}

sub textbox
{
    my ($id,$text,$x,$y)= @_;
    
    return "<draw:text-box draw:style-name=\"gr1\" draw:text-style-name=\"P1\" 
draw:id=\"".$id."\" draw:layer=\"layout\" 
svg:width=\"9.501cm\" svg:height=\"1.026cm\" svg:x=\"".$x."cm\" svg:y=\"".$y."cm\">
<text:p text:style-name=\"P1\">".$text."</text:p>
</draw:text-box>";
}

sub textcircle
{
    my ($id,$text,$x,$y)= @_;
    
    return "<draw:circle draw:style-name=\"gr1\" draw:text-style-name=\"P2\" 
draw:id=\"".$id."\" draw:layer=\"layout\" 
svg:width=\"3.000cm\" svg:height=\"3.000cm\" svg:x=\"".$x."cm\" svg:y=\"".$y."cm\">
<text:p text:style-name=\"P1\">".$text."</text:p>
<draw:glue-point draw:id=\"4\" svg:x=\"+0.000cm\" svg:y=\"-0.000cm\"/>
</draw:circle>";
}

sub smalltextcircle
{
    my ($id,$text,$x,$y)= @_;
    
    return "<draw:circle draw:style-name=\"mgr1\" draw:text-style-name=\"P2\" 
draw:id=\"".$id."\" draw:layer=\"layout\" 
svg:width=\"1.000cm\" svg:height=\"1.000cm\" svg:x=\"".$x."cm\" svg:y=\"".$y."cm\">
<text:p text:style-name=\"P1\"><text:span text:style-name=\"T1\">".$text."</text:span></text:p>
<draw:glue-point draw:id=\"4\" svg:x=\"+0.000cm\" svg:y=\"-0.000cm\"/>
</draw:circle>";
}

sub verysmalltextcircle
{
    my ($id,$text,$x,$y)= @_;
    
    return "<draw:circle draw:style-name=\"mgr1\" draw:text-style-name=\"P2\" 
draw:id=\"".$id."\" draw:layer=\"layout\" 
svg:width=\"0.100cm\" svg:height=\"0.100cm\" svg:x=\"".$x."cm\" svg:y=\"".$y."cm\">
<text:p text:style-name=\"P1\">
<text:span text:style-name=\"T1\">".$text."</text:span>
</text:p>
<draw:glue-point draw:id=\"4\" svg:x=\"+0.000cm\" svg:y=\"-0.000cm\"/>
</draw:circle>";
}

#<draw:circle draw:style-name="gr1" draw:text-style-name="P2" draw:layer="layout" svg:width="3.501cm" svg:height="3.501cm" svg:x="8cm" svg:y="4cm">
#<text:p text:style-name="P1">taga</text:p>
#</draw:circle>

sub connector
{
    my ($from,$to)= @_;

    return "
<draw:connector draw:style-name=\"gr3\" 
 draw:text-style-name=\"P2\" draw:layer=\"layout\" 
  draw:typeTTTTT=\"curve\" 
  draw:type=\"line\" 

  draw:line-skew=\"0.000cm\"

 draw:start-shape=\"".$node{$from}{id}."\" draw:start-glue-point=\"4\"  
 draw:end-shape=\"".$node{$to}{id}."\" draw:end-glue-point=\"4\"  />
";
    
}



$head= <<'EOF';
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE office:document-content PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "office.dtd"><office:document-content xmlns:office="http://openoffice.org/2000/office" xmlns:style="http://openoffice.org/2000/style" xmlns:text="http://openoffice.org/2000/text" xmlns:table="http://openoffice.org/2000/table" xmlns:draw="http://openoffice.org/2000/drawing" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:number="http://openoffice.org/2000/datastyle" xmlns:presentation="http://openoffice.org/2000/presentation" xmlns:svg="http://www.w3.org/2000/svg" xmlns:chart="http://openoffice.org/2000/chart" xmlns:dr3d="http://openoffice.org/2000/dr3d" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="http://openoffice.org/2000/form" xmlns:script="http://openoffice.org/2000/script" office:class="presentation" office:version="1.0"><office:script/>
<office:automatic-styles>

<style:style style:name="dp1" style:family="drawing-page">
<style:properties presentation:background-visible="true" presentation:background-objects-visible="true"/>
</style:style>

<style:style style:name="gr1" style:family="graphics" style:parent-style-name="standard">
  <style:properties draw:stroke="none" 
   svg:stroke-color="#000000" draw:fill="none" draw:fill-color="#ffffff" 
   draw:auto-grow-width="true" draw:auto-grow-height="false" fo:max-height="0cm" fo:min-height="0cm"/>
</style:style>

<style:style style:name="gr2" style:family="graphics" style:parent-style-name="standard"><style:properties draw:textarea-horizontal-align="center" draw:textarea-vertical-align="middle" draw:color-mode="standard" draw:luminance="0%" draw:contrast="0%" draw:gamma="1" draw:red="0%" draw:green="0%" draw:blue="0%" fo:clip="rect(0cm 0cm 0cm 0cm)" draw:transparency="0%" draw:mirror="false"/>
</style:style>


<style:style style:name="mgr1" style:family="graphics" style:parent-style-name="standard">
 <style:properties draw:textarea-horizontal-align="center" draw:textarea-vertical-align="middle"/>
</style:style>

<style:style style:name="T1" style:family="text"><style:properties fo:font-size="8pt"/></style:style>

<style:style style:name="gr3" style:family="graphics" style:parent-style-name="standard">
 <style:properties draw:textarea-horizontal-align="center" draw:textarea-vertical-align="middle"/>
</style:style>

<style:style style:name="gr4" style:family="graphics">
 <style:properties draw:size-protect="true"/>
</style:style>

<style:style style:name="pr1" style:family="presentation" style:parent-style-name="Default-notes">
 <style:properties draw:fill-color="#ffffff" draw:auto-grow-width="true" fo:min-height="10.629cm"/>
</style:style>

<style:style style:name="P1" style:family="paragraph">
 <style:properties fo:margin-left="0cm" fo:margin-right="0cm" fo:text-indent="0cm"/>
</style:style>

<style:style style:name="P2" style:family="paragraph">
 <style:properties fo:text-align="center"/>
</style:style>

<style:style style:name="P3" style:family="paragraph">
 <style:properties fo:margin-left="0.6cm" fo:margin-right="0cm" fo:text-indent="-0.6cm"/>
</style:style>

<text:list-style style:name="L1"><text:list-level-style-bullet text:level="1" text:bullet-char="●"><style:properties fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet><text:list-level-style-bullet text:level="2" text:bullet-char="●"><style:properties text:space-before="0.6cm" text:min-label-width="0.6cm" fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet><text:list-level-style-bullet text:level="3" text:bullet-char="●"><style:properties text:space-before="1.2cm" text:min-label-width="0.6cm" fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet><text:list-level-style-bullet text:level="4" text:bullet-char="●"><style:properties text:space-before="1.8cm" text:min-label-width="0.6cm" fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet><text:list-level-style-bullet text:level="5" text:bullet-char="●"><style:properties text:space-before="2.4cm" text:min-label-width="0.6cm" fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet><text:list-level-style-bullet text:level="6" text:bullet-char="●"><style:properties text:space-before="3cm" text:min-label-width="0.6cm" fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet><text:list-level-style-bullet text:level="7" text:bullet-char="●"><style:properties text:space-before="3.6cm" text:min-label-width="0.6cm" fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet><text:list-level-style-bullet text:level="8" text:bullet-char="●"><style:properties text:space-before="4.2cm" text:min-label-width="0.6cm" fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet><text:list-level-style-bullet text:level="9" text:bullet-char="●"><style:properties text:space-before="4.8cm" text:min-label-width="0.6cm" fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet><text:list-level-style-bullet text:level="10" text:bullet-char="●"><style:properties text:space-before="5.4cm" text:min-label-width="0.6cm" fo:font-family="StarSymbol" style:use-window-font-color="true" fo:font-size="45%"/></text:list-level-style-bullet></text:list-style></office:automatic-styles>

<office:body>
<draw:page draw:name="page1" draw:style-name="dp1" draw:id="1" draw:master-page-name="Default">
EOF

$middle= <<'EOF';

<draw:text-box draw:style-name="gr1" draw:text-style-name="P1" draw:id="2" draw:layer="layout" svg:width="9.501cm" svg:height="1.026cm" svg:x="4.5cm" svg:y="8cm">
<text:p text:style-name="P1">Hello Bibi World</text:p>
</draw:text-box>

<draw:image draw:style-name="gr2" draw:text-style-name="P2" draw:id="1" draw:layer="layout" svg:width="5.625cm" svg:height="5.678cm" svg:x="9.376cm" svg:y="10.823cm" xlink:href="#Pictures/100002010000005000000050A611AD08.png" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad">
<draw:glue-point draw:id="5" svg:x="-1.223cm" svg:y="-2.045cm"/>
<draw:glue-point draw:id="6" svg:x="-0.334cm" svg:y="-0.283cm"/>
<draw:glue-point draw:id="7" svg:x="+4.000cm" svg:y="+2.000cm"/>
<draw:glue-point draw:id="8" svg:x="+4.000cm" svg:y="-2.000cm"/>
</draw:image>

<draw:connector draw:style-name="gr3" 
 draw:text-style-name="P2" draw:layer="layout" 

 draw:start-shape="1" draw:start-glue-point="6" 
 draw:end-shape="2" draw:end-glue-point="3"/>

<draw:text-box draw:style-name="gr1" draw:text-style-name="P1" draw:id="3" draw:layer="layout" svg:width="9.501cm" svg:height="1.026cm" svg:x="8cm" svg:y="4.5cm">
<text:p text:style-name="P1">Nouveau TExte1</text:p>
</draw:text-box>

<draw:connector draw:style-name="gr3" 
 draw:text-style-name="P2" draw:layer="layout" 
 svg:x1="20cm" svg:y1="23.661cm" svg:x2="18.5cm" svg:y2="10.012cm" 
 draw:start-shape="1" draw:start-glue-point="8" 
 draw:end-shape="3" draw:end-glue-point="0"   />

<draw:connector draw:style-name="gr3" 
 draw:text-style-name="P2" draw:layer="layout" 
 svg:x1="20cm" svg:y1="23.661cm" svg:x2="18.5cm" svg:y2="10.012cm" 
 draw:start-shape="1" draw:start-glue-point="8" 
 draw:end-shape="1" draw:end-glue-point="7" 
/>

EOF

    $tail= <<'EOF';
<presentation:notes>
<draw:page-thumbnail draw:style-name="gr4" draw:layer="layout" svg:width="12.768cm" svg:height="9.576cm" svg:x="4.411cm" svg:y="2.794cm" draw:page-number="1" presentation:class="page"/>
<draw:text-box presentation:style-name="pr1" draw:text-style-name="P3" draw:layer="layout" svg:width="15.021cm" svg:height="10.63cm" svg:x="3.292cm" svg:y="13.299cm" presentation:class="notes" presentation:placeholder="true"/>
</presentation:notes>
</draw:page>
</office:body>
</office:document-content>
EOF


print $head;


$nodes= "";

foreach $node ( keys %node )
{
    $drawid++;
    #print " $node ".nodeposx($node).",".nodeposy($node)."\n";
    
    #print textbox($drawid,$node,nodeposx($node),nodeposy($node))."\n";
    #print textcircle($drawid,$node,nodeposx($node),nodeposy($node))."\n";
    if ($node{$node}{edge})
    {
	$nodes.= verysmalltextcircle($drawid,"",nodeposx($node),nodeposy($node))."\n";
    }
    else
    {
	$nodes.= smalltextcircle($drawid,$node,nodeposx($node),nodeposy($node))."\n";
    }
    $node{$node}{id}= $drawid;
}

foreach $edge ( keys %edge )
{
    $from= $edge{$edge}{from};
    $to=   $edge{$edge}{to};
    #print STDERR "$from $node{$from}{id} -> $to $node{$to}{id} \n";
    print connector($from,$to);
}

print $nodes; ### put nodes after connector to have the connector under the nodes :)

print $tail;
