Skip to content

Commit

Permalink
Add classes for indirect objects
Browse files Browse the repository at this point in the history
  • Loading branch information
md2perpe authored and Per Persson committed Jun 12, 2015
1 parent 4edb1ff commit 985d86a
Showing 1 changed file with 119 additions and 45 deletions.
164 changes: 119 additions & 45 deletions index.php
Expand Up @@ -2,105 +2,179 @@
class Writer
{
protected $offset = 0;

public function __construct()
{
header('Content-Type: application/pdf');
}

public function getOffset()
{
return $this->offset;
}

public function write($s)
{
echo $s;
$this->offset += strlen($s);
}

public function writeLn($s)
{
$this->write("$s\r\n");
}

public function writeMap(array $map)
{
foreach ($map as $key => $value) {
$this->writeLn("/{$key} {$value}");
}
}
}


class Document
abstract class IndirectObject
{
protected $index = 1;
protected $objects = [];
protected $offsets = [];
protected $xrefOffset;

public function createIndirectObject($map)
abstract public function getMap();
}

class Catalog extends IndirectObject
{
protected $pages;

public function setPages(Pages $pages)
{
$this->objects[$this->index] = $map;
$this->index++;
$this->pages = $pages;
}


public function output(Writer $writer)

public function getMap()
{
$this->createIndirectObject([
return [
'Type' => '/Catalog',
'Pages' => '2 0 R',
]);

$this->createIndirectObject([
];
}
}

class Pages extends IndirectObject
{
protected $pages = [];

public function addPage(Page $page)
{
$this->pages[] = $page;
}

public function getMap()
{
return [
'Type' => '/Pages',
'Kids' => '[ 3 0 R ]',
'Count' => 1,
]);

$this->createIndirectObject([
];
}
}

class Page extends IndirectObject
{
protected $parent;

public function __construct(Pages $parent)
{
$this->parent = $parent;
}

public function getMap()
{
return [
'Type' => '/Page',
'Parent' => '2 0 R',
'Resources' => '<< >>',
'MediaBox' => '[ 0 0 1000 1000 ]',
]);


];
}
}


class Document
{
protected $index = 1;
protected $objects = [];
protected $offsets = [];
protected $xrefOffset;

protected $catalog;
protected $currentPages;
protected $currentPage;

public function __construct()
{
$catalog = new Catalog();
$this->registerIndirectObject($catalog);

$pages = new Pages();
$this->registerIndirectObject($pages);

$page = new Page($pages);
$this->registerIndirectObject($page);

$catalog->setPages($pages);
$pages->addPage($page);

$this->catalog = $catalog;
$this->currentPages = $pages;
$this->currentPage = $page;
}


public function registerIndirectObject($object)
{
$this->objects[$this->index] = $object;
$this->index++;
}



public function output(Writer $writer)
{
$this->outputHeader($writer);
foreach ($this->objects as $id => $map) {
$this->outputIndirectObject($writer, $id, $map);
foreach ($this->objects as $id => $object) {
$this->outputIndirectObject($writer, $id, $object);
}

$this->outputXref($writer);
$this->outputTrailer($writer);
$this->outputXrefOffset($writer);
$this->outputFooter($writer);
}


public function outputHeader(Writer $writer)
{
$writer->writeLn("%PDF-1.2");
$writer->writeLn("");
}
public function outputIndirectObject(Writer $writer, $id, $map)


public function outputIndirectObject(Writer $writer, $id, $object)
{
$this->offsets[$id] = $writer->getOffset();

$writer->writeLn("{$id} 0 obj");
$writer->writeLn("<<");
foreach ($map as $key => $value) {
$writer->writeLn("/{$key} {$value}");
}
$writer->writeMap($object->getMap());
$writer->writeLn(">>");
$writer->writeLn("endobj");
$writer->writeLn("");
}


public function outputXref(Writer $writer)
{
$this->xrefOffset = $writer->getOffset();

$count = count($this->objects);

$writer->writeLn("xref");
$writer->writeLn(sprintf("0 {$count}"));
$writer->writeLn("0000000000 65535 f");
Expand All @@ -109,24 +183,24 @@ public function outputXref(Writer $writer)
}
$writer->writeLn("");
}

public function outputTrailer(Writer $writer)
{
$count = count($this->objects);

$writer->writeLn("trailer");
$writer->writeLn("<<");
$writer->writeLn("/Size {$count}");
$writer->writeLn("/Root 1 0 R");
$writer->writeLn(">>");
}

public function outputXrefOffset(Writer $writer)
{
$writer->writeLn("startxref");
$writer->writeLn($this->xrefOffset);
}

public function outputFooter(Writer $writer)
{
$writer->writeLn("%%EOF");
Expand Down

0 comments on commit 985d86a

Please sign in to comment.