1<?php 2// $Header: /cvsroot/html2ps/media.layout.inc.php,v 1.16 2007/05/07 12:15:53 Konstantin Exp $ 3 4$GLOBALS['g_predefined_media'] = array(); 5$GLOBALS['g_media'] = null; 6 7// TODO: check for validity 8function add_predefined_media($name, $height, $width) { 9 global $g_predefined_media; 10 $g_predefined_media[$name] = array('height' => $height, 'width' => $width); 11} 12 13class Media { 14 var $margins; 15 var $size; 16 var $pixels; 17 var $is_landscape; 18 19 /** 20 * @param Array $size associative array with 'height' and 'width' keys (mm) 21 * @param Array $margins associative array with 'top', 'bottom', 'left' and 'right' keys (mm) 22 */ 23 function Media($size, $margins) { 24 $this->size = $size; 25 $this->margins = $margins; 26 $this->pixels = 800; 27 } 28 29 function ©() { 30 $new_item =& new Media($this->size, $this->margins); 31 $new_item->pixels = $this->pixels; 32 return $new_item; 33 } 34 35 function doInherit() { 36 } 37 38 function get_width() { 39 return $this->is_landscape ? $this->size['height'] : $this->size['width'] ; 40 } 41 42 function width() { 43 return $this->get_width(); 44 } 45 46 function get_height() { 47 return $this->height(); 48 } 49 50 function height() { 51 return $this->is_landscape ? $this->size['width'] : $this->size['height']; 52 } 53 54 function real_width() { 55 return $this->width() - $this->margins['left'] - $this->margins['right']; 56 } 57 58 function real_height() { 59 return $this->height() - $this->margins['bottom'] - $this->margins['top']; 60 } 61 62 function set_height($height) { 63 $this->size['height'] = $height; 64 } 65 66 function set_landscape($state) { 67 $this->is_landscape = (bool)$state; 68 } 69 70 // TODO: validity checking 71 function set_margins($margins) { 72 $this->margins = $margins; 73 } 74 75 function set_pixels($pixels) { 76 $this->pixels = $pixels; 77 } 78 79 function set_width($width) { 80 $this->size['width'] = $width; 81 } 82 83 // TODO: validity checking 84 function &predefined($name) { 85 global $g_predefined_media; 86 87 // Let's check if the chosen media defined 88 if (isset($g_predefined_media[$name])) { 89 $media =& new Media($g_predefined_media[$name], array('top'=>0, 'bottom'=>0, 'left'=>0, 'right'=>0)); 90 } else { 91 $media = null; 92 }; 93 94 return $media; 95 } 96 97 /** 98 * Pixels per millimeter 99 */ 100 function PPM() { 101 return $this->pixels / ($this->size['width'] - $this->margins['left'] - $this->margins['right']); 102 } 103 104 function to_bbox() { 105 return '0 0 '.ceil(mm2pt($this->size['width'])).' '.ceil(mm2pt($this->size['height'])); 106 } 107 108 function to_ps_landscape() { 109 if (!$this->is_landscape) { return "/initpage {} def"; }; 110 return "/initpage {90 rotate 0 pageheight neg translate} def"; 111 } 112 113 function to_ps() { 114 return 115 // Note that /pagewidth and /pageheight should contain page size on the "client" 116 // coordinate system for correct rendering, so the will swap place in landscape mode, 117 // while /width and height set in PageSize should have the real media values, because 118 // actual coordinate system rotation/offset is done by the /initpage command without 119 // actually ratating the media. 120 "/pagewidth {".$this->width()." mm} def\n". 121 "/pageheight {".$this->height()." mm} def\n". 122 "/lmargin {{$this->margins['left']} mm} def\n". 123 "/rmargin {{$this->margins['right']} mm} def\n". 124 "/tmargin {{$this->margins['top']} mm} def\n". 125 "/bmargin {{$this->margins['bottom']} mm} def\n". 126 "/px {pagewidth lmargin sub rmargin sub {$this->pixels} div mul} def\n". 127 "<< /PageSize [".$this->size['width']." mm ".$this->size['height']." mm] >> setpagedevice\n". 128 $this->to_ps_landscape(); 129 } 130} 131 132?>