<?php

require_once(__DIR__ . "/File.php");

define("PDF_SCREEN", 0);
define("PDF_EBOOK", 1);
define("PDF_PRINT", 2);
define("PDF_PREPRESS", 3);

class PDF extends File {
	
	private $_force_download = false;
	private $_fake_filename = null;

	public function __construct($f) {
		parent::__construct($f);
	}

	public function extract_page($page, $file, $dpi = 300) {

		$m = Config::get("MAGICK");

		$p = new Process($m);
		$p->arg("-density"); $p->arg($dpi);
		$p->arg(sprintf("%s[%d]", $this->path(), $page));
		$p->arg("-alpha"); $p->arg("remove");
		$p->arg($file);

		$rv = $p->execute();

		if ($rv != 0) {
			$img = new Image(640, 640);

			$y = 40;
			$x = 10;

			$b = $img->color(255,255,255);
			$f = $img->color(0, 0, 0);
			$img->clear($b);

			$e = implode("\n", $p->stderr());
			$e = wordwrap($e, 80);
			$e = explode("\n", $e);
			
			foreach ($e as $l) {
				$img->text($x, $y, $l, $f, 3);
				$y += 20;
			}

			$img->save($file, "image/jpeg");
			return $img;

		}

		return new Image($file);
	}

	public function force_download() {
		$this->_force_download = true;
	}

	public function fake_filename($f) {
		$this->_fake_filename = $f;
	}

	public function emit() {

		$filename = $this->_fake_filename;
		if ($filename == null) {
			$filename = $this->basename();
		}

		if ($this->_force_download) {
			$this->set_header("Content-Type", "application/octet-stream");
        	$this->set_header("Cache-Control", "public, max-age=31560000, immutable");
        	$this->set_header("Content-Disposition", "attachment; filename=\"$filename\"");
		} else {
			$this->set_header("Content-Type", "application/pdf");
        	$this->set_header("Cache-Control", "public, max-age=31560000, immutable");
        	$this->set_header("Content-Disposition", "inline; filename=\"$filename\"");
		}

		parent::emit();
	}


	public function info() {
		$infolines = array();
		$info = array();
		$proc = new Process("pdfinfo");
		$proc->arg($this->path());
		$proc->execute();

		$infolines = $proc->stdout();
		foreach ($infolines as $line) {
			if (preg_match('/^([^:]+):\s+(.*)$/', $line, $m)) {
				$info[$m[1]] = $m[2];
			}
		}
		return $info;
	}


	public function geminiOverview($verbose=false) {
		$g = new Gemini();
		$g->verbose = $verbose;
		try {
			return $g->geminiOverview($this);
		} catch (Exception $e) {
			throw $e;
		}
	}

	public function recompress($dest, $size=PDF_SCREEN) {
		$proc = new Process("gs");

		$proc->arg("-sDEVICE=pdfwrite");
		$proc->arg("-dCompatibilityLevel=1.6");
		switch ($size) {	
			case PDF_SCREEN:
				$proc->arg("-dPDFSETTINGS=/screen");
				break;
			case PDF_EBOOK:
				$proc->arg("-dPDFSETTINGS=/ebook");
				break;
			case PDF_PRINT:
				$proc->arg("-dPDFSETTINGS=/printer");
				break;
			case PDF_PREPRESS:
				$proc->arg("-dPDFSETTINGS=/prepress");
				break;
		}
		$proc->arg("-dNOPAUSE");
		$proc->arg("-dQUIET");
		$proc->arg("-dBATCH");
		$proc->arg("-sOutputFile=" . $dest);
		$proc->arg($this->path());
		$rv = $proc->execute();

		if ($rv == 0) {
			return new PDF($dest);
		}
		return false;
	}
}
