"Unknown Title", "toc" => true); private $parts = array(); private $images = array(); /** * Get the text data (the "html" code) */ public function getTextData(){ $prefix = ""; $title = "

".$this->settings["title"]."

"; list($text, $entries) = $this->generateText(); if($this->settings["toc"]) { $toc = $this->generateTOC($entries); //Generate TOC to get the right length $toc = $this->generateTOC($entries, strlen($prefix)+strlen($toc)+strlen($title)); //Generate the real TOC } $suffix = ""; return $prefix.$toc.$title.$text.$suffix; } /** * Generate the body's text and the chapter entries * @return array($string, $entries) $string is the html data, $entries * contains the level, the title and the position of the titles. */ public function generateText(){ $str = ""; $entries = array(); for($i = 0; $i < sizeof($this->parts); $i++){ list($type, $data) = $this->parts[$i]; $id = "title_".$i; switch($type){ case self::PARAGRAPH: $str .= "

".$data."

"; break; case self::PAGEBREAK: $str .= ''; break; case self::H2: $entries[] = array("level" => 2, "position" => strlen($str), "title" => $data, "id" => $id); $str .= "

".$data."

"; break; case self::H3: $entries[] = array("level" => 3, "position" => strlen($str), "title" => $data, "id" => $id); $str .= "

".$data."

"; break; case self::IMAGE: $str .= ""; break; } } return array($str, $entries); } /** * Generate a TOC * @param $entries The entries array generated by generateText * @param $base The zero position */ public function generateTOC($entries, $base = 0){ $toc = "

Contents

"; $toc .= "
"; for($i = 0, $len = sizeof($entries); $i < $len; $i++){ $entry = $entries[$i]; $pos = str_pad($entry["position"]+$base, 10, "0", STR_PAD_LEFT); $toc .= ""; } return $toc."
".$entry["title"]."
"; } /** * Get the file records of the images */ public function getImages(){ return $this->images; } /** * Get the metadata */ public function getMetaData(){ return $this->settings; } /** * Change the file's settings. For example set("author", "John Doe") or set("title", "The adventures of John Doe"). * @param $key Key of the setting to insert. */ public function set($key, $value){ $this->settings[$key] = $value; } /** * Get the file's settings. */ public function get($key){ return $this->settings[$key]; } /** * Append a paragraph of text to the file. * @param string $text The text to insert. */ public function appendParagraph($text){ $this->parts[] = array(self::PARAGRAPH, $text); } /** * Append a chapter title (H2) * @param string $title The title to insert. */ public function appendChapterTitle($title){ $this->parts[] = array(self::H2, $title); } /** * Append a section title (H3) * @param string $title The title to insert. */ public function appendSectionTitle($title){ $this->parts[] = array(self::H3, $title); } public function appendPageBreak() { $this->parts[] = array(self::PAGEBREAK, null); } /** * Append an image. * @param resource $img An image file (for example, created by `imagecreate`) */ public function appendImage($img){ $imgIndex = sizeof($this->images); $this->images[] = new FileRecord(new Record(ImageHandler::CreateImage($img))); $this->parts[] = array(self::IMAGE, $imgIndex); } }