Skip to content

Instantly share code, notes, and snippets.

@digarok
Created April 16, 2018 13:10
Show Gist options
  • Select an option

  • Save digarok/2eb58538a0846f020eb21d263f0b4e24 to your computer and use it in GitHub Desktop.

Select an option

Save digarok/2eb58538a0846f020eb21d263f0b4e24 to your computer and use it in GitHub Desktop.
PHP script that can parse just enough of an SVG to get line coordinates and scale them to integer values for use on 320x200 Apple IIgs screen.
<?php
// hi plamen ;)
class Line
{
public $internal_x1 = 0;
public $internal_y1 = 0;
public $internal_x2 = 0;
public $internal_y2 = 0;
public function svgStrParse($s) {
// m = moveto l = lineto
preg_match('/m(\d+\.*\d*)\ (\d+\.*\d*)/', $s, $matches);
$x1 = $matches[1];
$y1 = $matches[2];
preg_match('/l(\-*\d+\.*\d*)\ (\-*\d+\.*\d*)/', $s, $matches);
$x2 = $matches[1];
$y2 = $matches[2];
$this->internal_x1 = $x1;
$this->internal_y1 = $y1;
$this->internal_x2 = $x1+$x2;
$this->internal_y2 = $y1+$y2;
}
public function printScaleDW($scale) {
//var_dump($this);
$x1 = (int) ($this->internal_x1 * $scale);
$y1 = (int) ($this->internal_y1 * $scale);
$x2 = (int) ($this->internal_x2 * $scale);
$y2 = (int) ($this->internal_y2 * $scale);
print " DW $x1,$y1,$x2,$y2\n";
}
}
$x = file_get_contents('assets/fastchip.svg');
$svg = new SimpleXMLElement($x);
$clipbox = $svg->clipPath->path['d'];
# string(53) "m0 0l927.2572 0l0 596.94226l-927.2572 0l0 -596.94226z"
preg_match('/m0\ 0l(\d+\.*\d*)\ 0l0\ (\d+\.*\d*)l.*/', $clipbox, $matches);
$boxsize = array($matches[1], $matches[2]);
$scale_x = 320/$matches[1];
foreach($svg->g->path as $p) {
$line = new Line();
$line->svgStrParse($p['d']);
$line->printScaleDW($scale_x);
}
## OUTPUT LOOKS SOMETHING LIKE THIS
#Tree dw 70,5,100,36
# dw 100,35,90,35
# dw 90,35,120,65
# dw 120,65,110,65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment