コード例 #1
0
 /**
  * Converts any CSS length value into an absolute length in points.
  *
  * length_in_pt() takes a single length (e.g. '1em') or an array of
  * lengths and returns an absolute length.  If an array is passed, then
  * the return value is the sum of all elements.
  *
  * If a reference size is not provided, the default font size is used
  * ({@link Style::$default_font_size}).
  *
  * @param float|array $length   the length or array of lengths to resolve
  * @param float       $ref_size  an absolute reference size to resolve percentage lengths
  * @return float
  */
 function length_in_pt($length, $ref_size = null)
 {
     static $cache = array();
     if (!is_array($length)) {
         $length = array($length);
     }
     if (!isset($ref_size)) {
         $ref_size = self::$default_font_size;
     }
     $key = implode("@", $length) . "/{$ref_size}";
     if (isset($cache[$key])) {
         return $cache[$key];
     }
     $ret = 0;
     foreach ($length as $l) {
         if ($l === "auto") {
             return "auto";
         }
         if ($l === "none") {
             return "none";
         }
         // Assume numeric values are already in points
         if (is_numeric($l)) {
             $ret += $l;
             continue;
         }
         if ($l === "normal") {
             $ret += $ref_size;
             continue;
         }
         // Border lengths
         if ($l === "thin") {
             $ret += 0.5;
             continue;
         }
         if ($l === "medium") {
             $ret += 1.5;
             continue;
         }
         if ($l === "thick") {
             $ret += 2.5;
             continue;
         }
         if (($i = mb_strpos($l, "px")) !== false) {
             $dpi = $this->_stylesheet->get_dompdf()->get_option("dpi");
             $ret += mb_substr($l, 0, $i) * 72 / $dpi;
             continue;
         }
         if (($i = mb_strpos($l, "pt")) !== false) {
             $ret += (double) mb_substr($l, 0, $i);
             continue;
         }
         if (($i = mb_strpos($l, "%")) !== false) {
             $ret += (double) mb_substr($l, 0, $i) / 100 * $ref_size;
             continue;
         }
         if (($i = mb_strpos($l, "rem")) !== false) {
             $ret += (double) mb_substr($l, 0, $i) * $this->_stylesheet->get_dompdf()->get_tree()->get_root()->get_style()->font_size;
             continue;
         }
         if (($i = mb_strpos($l, "em")) !== false) {
             $ret += (double) mb_substr($l, 0, $i) * $this->__get("font_size");
             continue;
         }
         if (($i = mb_strpos($l, "cm")) !== false) {
             $ret += mb_substr($l, 0, $i) * 72 / 2.54;
             continue;
         }
         if (($i = mb_strpos($l, "mm")) !== false) {
             $ret += mb_substr($l, 0, $i) * 72 / 25.4;
             continue;
         }
         // FIXME: em:ex ratio?
         if (($i = mb_strpos($l, "ex")) !== false) {
             $ret += mb_substr($l, 0, $i) * $this->__get("font_size") / 2;
             continue;
         }
         if (($i = mb_strpos($l, "in")) !== false) {
             $ret += (double) mb_substr($l, 0, $i) * 72;
             continue;
         }
         if (($i = mb_strpos($l, "pc")) !== false) {
             $ret += (double) mb_substr($l, 0, $i) * 12;
             continue;
         }
         // Bogus value
         $ret += $ref_size;
     }
     return $cache[$key] = $ret;
 }