PageRenderTime 77ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/branches/b0.1/php_web/report_old/html2ps/box.img.php

https://github.com/tsnoad/Irondata
PHP | 350 lines | 227 code | 81 blank | 42 comment | 20 complexity | 5aa60b0bda42d63abe7d9ec746de5691 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0, LGPL-2.1
  1. <?php
  2. // $Header: /cvsroot/html2ps/box.img.php,v 1.50 2007/05/06 18:49:29 Konstantin Exp $
  3. define('SCALE_NONE',0);
  4. define('SCALE_WIDTH',1);
  5. define('SCALE_HEIGHT',2);
  6. class GenericImgBox extends GenericInlineBox {
  7. function GenericImgBox() {
  8. $this->GenericInlineBox();
  9. }
  10. function get_max_width_natural(&$context) {
  11. return $this->get_full_width($context);
  12. }
  13. function get_min_width(&$context) {
  14. return $this->get_full_width();
  15. }
  16. function get_max_width(&$context) {
  17. return $this->get_full_width();
  18. }
  19. function is_null() {
  20. return false;
  21. }
  22. function pre_reflow_images() {
  23. switch ($this->scale) {
  24. case SCALE_WIDTH:
  25. // Only 'width' attribute given
  26. $size =
  27. $this->src_width/$this->src_height*
  28. $this->get_width();
  29. $this->put_height($size);
  30. // Update baseline according to constrained image height
  31. $this->default_baseline = $this->get_full_height();
  32. break;
  33. case SCALE_HEIGHT:
  34. // Only 'height' attribute given
  35. $size =
  36. $this->src_height/$this->src_width*
  37. $this->get_height();
  38. $this->put_width($size);
  39. $this->setCSSProperty(CSS_WIDTH, new WCConstant($size));
  40. $this->default_baseline = $this->get_full_height();
  41. break;
  42. };
  43. }
  44. function readCSS(&$state) {
  45. parent::readCSS($state);
  46. // '-html2ps-link-target'
  47. global $g_config;
  48. if ($g_config["renderlinks"]) {
  49. $this->_readCSS($state,
  50. array(CSS_HTML2PS_LINK_TARGET));
  51. };
  52. }
  53. function reflow_static(&$parent, &$context) {
  54. $this->pre_reflow_images();
  55. GenericFormattedBox::reflow($parent, $context);
  56. // Check if we need a line break here
  57. $this->maybe_line_break($parent, $context);
  58. // set default baseline
  59. $this->baseline = $this->default_baseline;
  60. // append to parent line box
  61. $parent->append_line($this);
  62. // Move box to the parent current point
  63. $this->guess_corner($parent);
  64. // Move parent's X coordinate
  65. $parent->_current_x += $this->get_full_width();
  66. // Extend parent height
  67. $parent->extend_height($this->get_bottom_margin());
  68. }
  69. function _get_font_name(&$driver, $subword_index) {
  70. if (isset($this->_cache[CACHE_TYPEFACE][$subword_index])) {
  71. return $this->_cache[CACHE_TYPEFACE][$subword_index];
  72. };
  73. $font_resolver =& $driver->get_font_resolver();
  74. $font = $this->getCSSProperty(CSS_FONT);
  75. $typeface = $font_resolver->getTypefaceName($font->family,
  76. $font->weight,
  77. $font->style,
  78. 'iso-8859-1');
  79. $this->_cache[CACHE_TYPEFACE][$subword_index] = $typeface;
  80. return $typeface;
  81. }
  82. function reflow_text(&$driver) {
  83. // In XHTML images are treated as a common inline elements; they are affected by line-height and font-size
  84. global $g_config;
  85. if ($g_config['mode'] == 'xhtml') {
  86. /**
  87. * A simple assumption is made: fonts used for different encodings
  88. * have equal ascender/descender values (while they have the same
  89. * typeface, style and weight).
  90. */
  91. $font_name = $this->_get_font_name($driver, 0);
  92. /**
  93. * Get font vertical metrics
  94. */
  95. $ascender = $driver->font_ascender($font_name, 'iso-8859-1');
  96. if (is_null($ascender)) {
  97. error_log("ImgBox::reflow_text: cannot get font ascender");
  98. return null;
  99. };
  100. $descender = $driver->font_descender($font_name, 'iso-8859-1');
  101. if (is_null($descender)) {
  102. error_log("ImgBox::reflow_text: cannot get font descender");
  103. return null;
  104. };
  105. /**
  106. * Setup box size
  107. */
  108. $font = $this->getCSSProperty(CSS_FONT_SIZE);
  109. $font_size = $font->getPoints();
  110. $this->ascender = $ascender * $font_size;
  111. $this->descender = $descender * $font_size;
  112. } else {
  113. $this->ascender = $this->get_height();
  114. $this->descender = 0;
  115. };
  116. return true;
  117. }
  118. // Image boxes are regular inline boxes; whitespaces after images should be rendered
  119. //
  120. function reflow_whitespace(&$linebox_started, &$previous_whitespace) {
  121. $linebox_started = true;
  122. $previous_whitespace = false;
  123. return;
  124. }
  125. function show_fixed(&$driver) {
  126. return $this->show($driver);
  127. }
  128. }
  129. class BrokenImgBox extends GenericImgBox {
  130. var $alt;
  131. function BrokenImgBox($width, $height, $alt) {
  132. $this->scale = SCALE_NONE;
  133. $this->encoding = DEFAULT_ENCODING;
  134. // Call parent constructor
  135. $this->GenericImgBox();
  136. $this->alt = $alt;
  137. }
  138. function show(&$driver) {
  139. $driver->save();
  140. // draw generic box
  141. GenericFormattedBox::show($driver);
  142. $driver->setlinewidth(0.1);
  143. $driver->moveto($this->get_left(), $this->get_top());
  144. $driver->lineto($this->get_right(), $this->get_top());
  145. $driver->lineto($this->get_right(), $this->get_bottom());
  146. $driver->lineto($this->get_left(), $this->get_bottom());
  147. $driver->closepath();
  148. $driver->stroke();
  149. if (!$GLOBALS['g_config']['debugnoclip']) {
  150. $driver->moveto($this->get_left(), $this->get_top());
  151. $driver->lineto($this->get_right(), $this->get_top());
  152. $driver->lineto($this->get_right(), $this->get_bottom());
  153. $driver->lineto($this->get_left(), $this->get_bottom());
  154. $driver->closepath();
  155. $driver->clip();
  156. };
  157. // Output text with the selected font
  158. $size = pt2pt(BROKEN_IMAGE_ALT_SIZE_PT);
  159. $status = $driver->setfont("Times-Roman", "iso-8859-1", $size);
  160. if (is_null($status)) {
  161. return null;
  162. };
  163. $driver->show_xy($this->alt,
  164. $this->get_left() + $this->width/2 - $driver->stringwidth($this->alt,
  165. "Times-Roman",
  166. "iso-8859-1",
  167. $size)/2,
  168. $this->get_top() - $this->height/2 - $size/2);
  169. $driver->restore();
  170. $strategy =& new StrategyLinkRenderingNormal();
  171. $strategy->apply($this, $driver);
  172. return true;
  173. }
  174. }
  175. class ImgBox extends GenericImgBox {
  176. var $image;
  177. var $type; // unused; should store the preferred image format (JPG / PNG)
  178. function ImgBox($img) {
  179. $this->encoding = DEFAULT_ENCODING;
  180. $this->scale = SCALE_NONE;
  181. // Call parent constructor
  182. $this->GenericImgBox();
  183. // Store image for further processing
  184. $this->image = $img;
  185. }
  186. function &create(&$root, &$pipeline) {
  187. // Open image referenced by HTML tag
  188. // Some crazy HTML writers add leading and trailing spaces to SRC attribute value - we need to remove them
  189. //
  190. $url_autofix = new AutofixUrl();
  191. $src = $url_autofix->apply(trim($root->get_attribute("src")));
  192. $image_url = $pipeline->guess_url($src);
  193. $src_img = ImageFactory::get($image_url, $pipeline);
  194. if (is_null($src_img)) {
  195. // image could not be opened, use ALT attribute
  196. if ($root->has_attribute('width')) {
  197. $width = px2pt($root->get_attribute('width'));
  198. } else {
  199. $width = px2pt(BROKEN_IMAGE_DEFAULT_SIZE_PX);
  200. };
  201. if ($root->has_attribute('height')) {
  202. $height = px2pt($root->get_attribute('height'));
  203. } else {
  204. $height = px2pt(BROKEN_IMAGE_DEFAULT_SIZE_PX);
  205. };
  206. $alt = $root->get_attribute('alt');
  207. $box =& new BrokenImgBox($width, $height, $alt);
  208. $box->readCSS($pipeline->getCurrentCSSState());
  209. $box->put_width($width);
  210. $box->put_height($height);
  211. $box->default_baseline = $box->get_full_height();
  212. $box->src_height = $box->get_height();
  213. $box->src_width = $box->get_width();
  214. return $box;
  215. } else {
  216. $box =& new ImgBox($src_img);
  217. $box->readCSS($pipeline->getCurrentCSSState());
  218. $box->_setupSize();
  219. return $box;
  220. }
  221. }
  222. function _setupSize() {
  223. $this->put_width(px2pt($this->image->sx()));
  224. $this->put_height(px2pt($this->image->sy()));
  225. $this->default_baseline = $this->get_full_height();
  226. $this->src_height = $this->image->sx();
  227. $this->src_width = $this->image->sy();
  228. $wc = $this->getCSSProperty(CSS_WIDTH);
  229. $hc = $this->get_height_constraint();
  230. // Proportional scaling
  231. if ($hc->is_null() && !$wc->isNull()) {
  232. $this->scale = SCALE_WIDTH;
  233. // Only 'width' attribute given
  234. $size =
  235. $this->src_width/$this->src_height*
  236. $this->get_width();
  237. $this->put_height($size);
  238. // Update baseline according to constrained image height
  239. $this->default_baseline = $this->get_full_height();
  240. } elseif (!$hc->is_null() && $wc->isNull()) {
  241. $this->scale = SCALE_HEIGHT;
  242. // Only 'height' attribute given
  243. $size =
  244. $this->src_height/$this->src_width*
  245. $this->get_height();
  246. $this->put_width($size);
  247. $this->setCSSProperty(CSS_WIDTH, new WCConstant($size));
  248. $this->default_baseline = $this->get_full_height();
  249. };
  250. }
  251. function show(&$driver) {
  252. // draw generic box
  253. GenericFormattedBox::show($driver);
  254. // Check if "designer" set the height or width of this image to zero; in this there will be no reason
  255. // in drawing the image at all
  256. //
  257. if ($this->get_width() < EPSILON ||
  258. $this->get_height() < EPSILON) {
  259. return true;
  260. };
  261. $driver->image_scaled($this->image,
  262. $this->get_left(), $this->get_bottom(),
  263. $this->get_width() / $this->image->sx(), $this->get_height() / $this->image->sy());
  264. $strategy =& new StrategyLinkRenderingNormal();
  265. $strategy->apply($this, $driver);
  266. return true;
  267. }
  268. }
  269. ?>