1<?php
2
3class CSSSize extends CSSPropertyHandler {
4  function CSSSize() {
5    $this->CSSPropertyHandler(false, false);
6  }
7
8  function default_value() {
9    $null = null;
10    return $null;
11  }
12
13  // <length>{1,2} | auto | [ <page-size> || [ portrait | landscape] ]
14  function parse($value) {
15    if ($value == '') {
16      return null;
17    };
18
19    // First attempt to create media with predefined name
20    if (preg_match('/^(\w+)(?:\s+(portrait|landscape))?$/', $value, $matches)) {
21      $name = $matches[1];
22      $landscape = isset($matches[2]) && $matches[2] == 'landscape';
23
24      $media =& Media::predefined($name);
25
26      if (is_null($media)) {
27        return null;
28      };
29
30      return array('size' => array('width' => $media->get_width(),
31                                   'height' => $media->get_height()),
32                   'landscape' => $landscape);
33    };
34
35    // Second, attempt to create media with predefined size
36    $parts = preg_split('/\s+/', $value);
37    $width_str = $parts[0];
38    $height_str = isset($parts[1]) ? $parts[1] : $parts[0];
39
40    $width = units2pt($width_str);
41    $height = units2pt($height_str);
42
43    if ($width == 0 ||
44        $height == 0) {
45      return null;
46    };
47
48    return array('size' => array('width' => $width / mm2pt(1) / pt2pt(1),
49                                 'height' => $height / mm2pt(1) / pt2pt(1)),
50                 'landscape' => false);
51  }
52
53  function get_property_code() {
54    return CSS_SIZE;
55  }
56
57  function get_property_name() {
58    return 'size';
59  }
60}
61
62CSS::register_css_property(new CSSSize());
63
64?>