/**
   * Class constructor
   *
   * @param mixed  $paper       The size of paper to use either a string (see {@link CPDF_Adapter::$PAPER_SIZES}) or
   *                            an array(xmin,ymin,xmax,ymax)
   * @param string $orientation The orientation of the document (either 'landscape' or 'portrait')
   * @param DOMPDF $dompdf
   */
  function __construct($paper = "letter", $orientation = "portrait", DOMPDF $dompdf) {
    if ( is_array($paper) ) {
      $size = $paper;
    }
    else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) ) {
      $size = self::$PAPER_SIZES[mb_strtolower($paper)];
    }
    else {
      $size = self::$PAPER_SIZES["letter"];
    }

    if ( mb_strtolower($orientation) === "landscape" ) {
      list($size[2], $size[3]) = array($size[3], $size[2]);
    }

    $this->_width = $size[2] - $size[0];
    $this->_height= $size[3] - $size[1];

    $this->_dompdf = $dompdf;

    $this->_pdf = new PDFLib();

    if ( defined("DOMPDF_PDFLIB_LICENSE") )
      $this->_pdf->set_parameter( "license", DOMPDF_PDFLIB_LICENSE);

    $this->_pdf->set_parameter("textformat", "utf8");
    $this->_pdf->set_parameter("fontwarning", "false");

    $this->_pdf->set_info("Creator", "DOMPDF");

    // Silence pedantic warnings about missing TZ settings
    $tz = @date_default_timezone_get();
    date_default_timezone_set("UTC");
    $this->_pdf->set_info("Date", date("Y-m-d"));
    date_default_timezone_set($tz);

    if ( self::$IN_MEMORY )
      $this->_pdf->begin_document("","");
    else {
      $tmp_dir = $this->_dompdf->get_options("temp_dir");
      $tmp_name = tempnam($tmp_dir, "libdompdf_pdf_");
      @unlink($tmp_name);
      $this->_file = "$tmp_name.pdf";
      $this->_pdf->begin_document($this->_file,"");
    }

    $this->_pdf->begin_page_ext($this->_width, $this->_height, "");

    $this->_page_number = $this->_page_count = 1;
    $this->_page_text = array();

    $this->_imgs = array();
    $this->_fonts = array();
    $this->_objs = array();

    // Set up font paths
    $families = Font_Metrics::get_font_families();
    foreach ($families as $files) {
      foreach ($files as $file) {
        $face = basename($file);
        $afm = null;

        // Prefer ttfs to afms
        if ( file_exists("$file.ttf") ) {
          $outline = "$file.ttf";

        } else if ( file_exists("$file.TTF") ) {
          $outline = "$file.TTF";

        } else if ( file_exists("$file.pfb") ) {
          $outline = "$file.pfb";

          if ( file_exists("$file.afm") ) {
            $afm = "$file.afm";
          }

        } else if ( file_exists("$file.PFB") ) {
          $outline = "$file.PFB";
          if ( file_exists("$file.AFM") ) {
            $afm = "$file.AFM";
          }
        } else {
          continue;
        }

        $this->_pdf->set_parameter("FontOutline", "\{$face\}=\{$outline\}");

        if ( !is_null($afm) ) {
          $this->_pdf->set_parameter("FontAFM", "\{$face\}=\{$afm\}");
        }
      }
    }
  }