PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/html/app/Vendor/html2ps/box.generic.formatted.php

https://github.com/mehulsbhatt/OPENCDR
PHP | 1042 lines | 682 code | 180 blank | 180 comment | 88 complexity | 6b57884d30c33538d4858476ed9e66d6 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // $Header: /cvsroot/html2ps/box.generic.formatted.php,v 1.21 2007/02/18 09:55:10 Konstantin Exp $
  3. require_once(HTML2PS_DIR.'doc.anchor.class.php');
  4. require_once(HTML2PS_DIR.'layout.vertical.php');
  5. class GenericFormattedBox extends GenericBox {
  6. var $uid;
  7. function _get_collapsable_top_margin_internal() {
  8. $positive_margin = 0;
  9. $negative_margin = 0;
  10. $current_box = $this;
  11. $border = $current_box->get_css_property(CSS_BORDER);
  12. $padding = $current_box->get_css_property(CSS_PADDING);
  13. if ($border->top->get_width() > 0 ||
  14. $padding->top->value > 0) {
  15. return 0;
  16. };
  17. while (!is_null($current_box) &&
  18. $current_box->isBlockLevel()) {
  19. $margin = $current_box->get_css_property(CSS_MARGIN);
  20. $border = $current_box->get_css_property(CSS_BORDER);
  21. $padding = $current_box->get_css_property(CSS_PADDING);
  22. $top_margin = $margin->top->value;
  23. if ($top_margin >= 0) {
  24. $positive_margin = max($positive_margin, $top_margin);
  25. } else {
  26. $negative_margin = min($negative_margin, $top_margin);
  27. };
  28. if ($border->top->get_width() > 0 ||
  29. $padding->top->value > 0) {
  30. $current_box = null;
  31. } else {
  32. $current_box = $current_box->get_first();
  33. };
  34. };
  35. return $positive_margin /*- $negative_margin*/;
  36. }
  37. function _get_collapsable_top_margin_external() {
  38. $positive_margin = 0;
  39. $negative_margin = 0;
  40. $current_box = $this;
  41. while (!is_null($current_box) &&
  42. $current_box->isBlockLevel()) {
  43. $margin = $current_box->get_css_property(CSS_MARGIN);
  44. $border = $current_box->get_css_property(CSS_BORDER);
  45. $padding = $current_box->get_css_property(CSS_PADDING);
  46. $top_margin = $margin->top->value;
  47. if ($top_margin >= 0) {
  48. $positive_margin = max($positive_margin, $top_margin);
  49. } else {
  50. $negative_margin = min($negative_margin, $top_margin);
  51. };
  52. if ($border->top->get_width() > 0 ||
  53. $padding->top->value > 0) {
  54. $current_box = null;
  55. } else {
  56. $current_box = $current_box->get_first();
  57. };
  58. };
  59. return $positive_margin + $negative_margin;
  60. }
  61. function _get_collapsable_bottom_margin_external() {
  62. $positive_margin = 0;
  63. $negative_margin = 0;
  64. $current_box = $this;
  65. while (!is_null($current_box) &&
  66. $current_box->isBlockLevel()) {
  67. $margin = $current_box->get_css_property(CSS_MARGIN);
  68. $border = $current_box->get_css_property(CSS_BORDER);
  69. $padding = $current_box->get_css_property(CSS_PADDING);
  70. $bottom_margin = $margin->bottom->value;
  71. if ($bottom_margin >= 0) {
  72. $positive_margin = max($positive_margin, $bottom_margin);
  73. } else {
  74. $negative_margin = min($negative_margin, $bottom_margin);
  75. };
  76. if ($border->bottom->get_width() > 0 ||
  77. $padding->bottom->value > 0) {
  78. $current_box = null;
  79. } else {
  80. $current_box = $current_box->get_last();
  81. };
  82. };
  83. return $positive_margin + $negative_margin;
  84. }
  85. function collapse_margin_bottom(&$parent, &$context) {
  86. /**
  87. * Now, if there's a parent for this box, we extend its height to fit current box.
  88. * If parent generated new flow context (like table cell or floating box), its content
  89. * area should include the current box bottom margin (bottom margin does not colllapse).
  90. * See CSS 2.1 for more detailed explanations.
  91. *
  92. * @see FlowContext::container_uid()
  93. *
  94. * @link http://www.w3.org/TR/CSS21/visudet.html#Computing_widths_and_margins CSS 2.1 8.3.1 Calculating widths and margins
  95. */
  96. $parent_border = $parent->get_css_property(CSS_BORDER);
  97. $parent_padding = $parent->get_css_property(CSS_PADDING);
  98. /**
  99. * The bottom margin of an in-flow block-level element with a
  100. * 'height' of 'auto' and 'min-height' less than the element's
  101. * used height and 'max-height' greater than the element's used
  102. * height is adjoining to its last in-flow block-level child's
  103. * bottom margin if the element has NO BOTTOM PADDING OR BORDER.
  104. */
  105. $last =& $parent->get_last();
  106. $is_last = !is_null($last) && $this->uid == $last->uid;
  107. if (!is_null($last) &&
  108. $is_last && // This element is a last in-flow block level element AND
  109. $parent->uid != $context->container_uid() && // Parent element did not generate new flow context (like table-cell) AND
  110. $parent_border->bottom->get_width() == 0 && // Parent have NO bottom border AND
  111. $parent_padding->bottom->value == 0) { // Parent have NO bottom padding AND
  112. $parent->extend_height($this->get_bottom_border());
  113. } else {
  114. // Otherwise (in particular, if this box is not last), bottom
  115. // margin of the current box will be contained inside the current box
  116. $parent->extend_height($this->get_bottom_margin());
  117. }
  118. $cm = $context->get_collapsed_margin();
  119. $context->pop_collapsed_margin();
  120. $context->pop_collapsed_margin();
  121. /**
  122. * shift current parent 'watermark' to the current box margin edge;
  123. * all content now will be drawn below this mark (with a small exception
  124. * of elements having negative vertical margins, of course).
  125. */
  126. if ($is_last &&
  127. ($parent_border->bottom->get_width() > 0 ||
  128. $parent_padding->bottom->value > 0)) {
  129. $context->push_collapsed_margin( 0 );
  130. return $this->get_bottom_border() - $cm;
  131. } else {
  132. $collapsable = $this->_get_collapsable_bottom_margin_external();
  133. $context->push_collapsed_margin( $collapsable );
  134. return $this->get_bottom_border();
  135. };
  136. }
  137. function collapse_margin(&$parent, &$context) {
  138. // Do margin collapsing
  139. // Margin collapsing is done as follows:
  140. // 1. If previous sibling was an inline element (so, parent line box was not empty),
  141. // then no collapsing will take part
  142. // 2. If NO previous element exists at all, then collapse current box top margin
  143. // with parent's collapsed top margin.
  144. // 2.1. If parent element was float, no collapsing should be
  145. // 3. If there's previous block element, collapse current box top margin
  146. // with previous elemenent's collapsed bottom margin
  147. // Check if current parent line box contains inline elements only. In this case the only
  148. // margin will be current box margin
  149. if (!$parent->line_box_empty()) {
  150. // Case (1). Previous element was inline element; no collapsing
  151. $parent->close_line($context);
  152. $vmargin = $this->_get_collapsable_top_margin_external();
  153. } else {
  154. $parent_first = $this->parent->get_first();
  155. if (is_null($parent_first) || // Unfortunately, we sometimes get null as a value of $parent_first; this should be checked
  156. $parent_first->uid == $this->uid) {
  157. // Case (2). No previous block element at all; Collapse with parent margins
  158. $collapsable = $this->_get_collapsable_top_margin_external();
  159. $collapsed = $context->get_collapsed_margin();
  160. $vmargin = max(0, $collapsable - $collapsed);
  161. } else {
  162. // Case (3). There's a previous block element
  163. $collapsable = $this->_get_collapsable_top_margin_external();
  164. $collapsed = $context->get_collapsed_margin();
  165. // In this case, base position is a bottom border of the previous element
  166. // $vmargin - offset from a base position - should be at least $collapsed
  167. // (value of collapsed bottom margins from the previous element and its
  168. // children). If current element have $collapsable - collapsed top margin
  169. // (from itself and children too) greater that this value, we should
  170. // offset it further to the bottom
  171. $vmargin = max($collapsable, $collapsed);
  172. };
  173. };
  174. // Determine the base Y coordinate of box margin edge
  175. $y = $parent->_current_y - $vmargin;
  176. $internal_margin = $this->_get_collapsable_top_margin_internal();
  177. $context->push_collapsed_margin($internal_margin);
  178. return $y;
  179. }
  180. function GenericFormattedBox() {
  181. $this->GenericBox();
  182. // Layout data
  183. $this->baseline = 0;
  184. $this->parent = null;
  185. }
  186. function readCSS(&$state) {
  187. parent::readCSS($state);
  188. $this->_readCSS($state,
  189. array(CSS_OVERFLOW,
  190. CSS_PAGE_BREAK_AFTER,
  191. CSS_PAGE_BREAK_BEFORE,
  192. CSS_PAGE_BREAK_INSIDE,
  193. CSS_ORPHANS,
  194. CSS_WIDOWS,
  195. CSS_POSITION,
  196. CSS_TEXT_ALIGN,
  197. CSS_WHITE_SPACE,
  198. CSS_CLEAR,
  199. CSS_CONTENT,
  200. CSS_HTML2PS_PSEUDOELEMENTS,
  201. CSS_FLOAT,
  202. CSS_Z_INDEX,
  203. CSS_HTML2PS_ALIGN,
  204. CSS_HTML2PS_NOWRAP,
  205. CSS_DIRECTION,
  206. CSS_PAGE));
  207. $this->_readCSSLengths($state,
  208. array(CSS_BACKGROUND,
  209. CSS_BORDER,
  210. CSS_BOTTOM,
  211. CSS_TOP,
  212. CSS_LEFT,
  213. CSS_RIGHT,
  214. CSS_MARGIN,
  215. CSS_PADDING,
  216. CSS_TEXT_INDENT,
  217. CSS_HTML2PS_COMPOSITE_WIDTH,
  218. CSS_HEIGHT,
  219. CSS_MIN_HEIGHT,
  220. CSS_MAX_HEIGHT,
  221. CSS_LETTER_SPACING
  222. ));
  223. /**
  224. * CSS 2.1, p 8.5.2:
  225. *
  226. * If an element's border color is not specified with a border
  227. * property, user agents must use the value of the element's
  228. * 'color' property as the computed value for the border color.
  229. */
  230. $border =& $this->get_css_property(CSS_BORDER);
  231. $color =& $this->get_css_property(CSS_COLOR);
  232. if ($border->top->isDefaultColor()) {
  233. $border->top->setColor($color);
  234. };
  235. if ($border->right->isDefaultColor()) {
  236. $border->right->setColor($color);
  237. };
  238. if ($border->bottom->isDefaultColor()) {
  239. $border->bottom->setColor($color);
  240. };
  241. if ($border->left->isDefaultColor()) {
  242. $border->left->setColor($color);
  243. };
  244. $this->setCSSProperty(CSS_BORDER, $border);
  245. $this->_height_constraint =& HCConstraint::create($this);
  246. $this->height = 0;
  247. // 'width'
  248. $wc =& $this->get_css_property(CSS_WIDTH);
  249. $this->width = $wc->apply(0,0);
  250. // 'PSEUDO-CSS' properties
  251. // '-localalign'
  252. switch ($state->get_property(CSS_HTML2PS_LOCALALIGN)) {
  253. case LA_LEFT:
  254. break;
  255. case LA_RIGHT:
  256. $margin =& $this->get_css_property(CSS_MARGIN);
  257. $margin->left->auto = true;
  258. $this->setCSSProperty(CSS_MARGIN, $margin);
  259. break;
  260. case LA_CENTER:
  261. $margin =& $this->get_css_property(CSS_MARGIN);
  262. $margin->left->auto = true;
  263. $margin->right->auto = true;
  264. $this->setCSSProperty(CSS_MARGIN, $margin);
  265. break;
  266. };
  267. }
  268. function _calc_percentage_margins(&$parent) {
  269. $margin = $this->get_css_property(CSS_MARGIN);
  270. $containing_block =& $this->_get_containing_block();
  271. $margin->calcPercentages($containing_block['right'] - $containing_block['left']);
  272. $this->setCSSProperty(CSS_MARGIN, $margin);
  273. }
  274. function _calc_percentage_padding(&$parent) {
  275. $padding = $this->get_css_property(CSS_PADDING);
  276. $containing_block =& $this->_get_containing_block();
  277. $padding->calcPercentages($containing_block['right'] - $containing_block['left']);
  278. $this->setCSSProperty(CSS_PADDING, $padding);
  279. }
  280. function apply_clear($y, &$context) {
  281. return LayoutVertical::apply_clear($this, $y, $context);
  282. }
  283. /**
  284. * CSS 2.1:
  285. * 10.2 Content width: the 'width' property
  286. * Values have the following meanings:
  287. * <percentage> Specifies a percentage width. The percentage is calculated with respect to the width of the generated box's containing block.
  288. *
  289. * If the containing block's width depends on this element's width,
  290. * then the resulting layout is undefined in CSS 2.1.
  291. */
  292. function _calc_percentage_width(&$parent, &$context) {
  293. $wc = $this->get_css_property(CSS_WIDTH);
  294. if ($wc->isFraction()) {
  295. $containing_block =& $this->_get_containing_block();
  296. // Calculate actual width
  297. $width = $wc->apply($this->width, $containing_block['right'] - $containing_block['left']);
  298. // Assign calculated width
  299. $this->put_width($width);
  300. // Remove any width constraint
  301. $this->setCSSProperty(CSS_WIDTH, new WCConstant($width));
  302. }
  303. }
  304. function _calc_auto_width_margins(&$parent) {
  305. $float = $this->get_css_property(CSS_FLOAT);
  306. if ($float !== FLOAT_NONE) {
  307. $this->_calc_auto_width_margins_float($parent);
  308. } else {
  309. $this->_calc_auto_width_margins_normal($parent);
  310. }
  311. }
  312. // 'auto' margin value became 0, 'auto' width is 'shrink-to-fit'
  313. function _calc_auto_width_margins_float(&$parent) {
  314. // If 'width' is set to 'auto' the used value is the "shrink-to-fit" width
  315. // TODO
  316. if (false) {
  317. // Calculation of the shrink-to-fit width is similar to calculating the
  318. // width of a table cell using the automatic table layout
  319. // algorithm. Roughly: calculate the preferred width by formatting the
  320. // content without breaking lines other than where explicit line breaks
  321. // occur, and also calculate the preferred minimum width, e.g., by trying
  322. // all possible line breaks. CSS 2.1 does not define the exact
  323. // algorithm. Thirdly, find the available width: in this case, this is
  324. // the width of the containing block minus minus the used values of
  325. // 'margin-left', 'border-left-width', 'padding-left', 'padding-right',
  326. // 'border-right-width', 'margin-right', and the widths of any relevant
  327. // scroll bars.
  328. // Then the shrink-to-fit width is: min(max(preferred minimum width, available width), preferred width).
  329. // Store used value
  330. };
  331. // If 'margin-left', or 'margin-right' are computed as 'auto', their used value is '0'.
  332. $margin = $this->get_css_property(CSS_MARGIN);
  333. if ($margin->left->auto) { $margin->left->value = 0; }
  334. if ($margin->right->auto) { $margin->right->value = 0; }
  335. $this->setCSSProperty(CSS_MARGIN, $margin);
  336. $this->width = $this->get_width();
  337. }
  338. // 'margin-left' + 'border-left-width' + 'padding-left' + 'width' + 'padding-right' + 'border-right-width' + 'margin-right' = width of containing block
  339. function _calc_auto_width_margins_normal(&$parent) {
  340. // get the containing block width
  341. $containing_block =& $this->_get_containing_block();
  342. $parent_width = $containing_block['right'] - $containing_block['left'];
  343. // If 'width' is set to 'auto', any other 'auto' values become '0' and 'width' follows from the resulting equality.
  344. // If both 'margin-left' and 'margin-right' are 'auto', their used values are equal.
  345. // This horizontally centers the element with respect to the edges of the containing block.
  346. $margin = $this->get_css_property(CSS_MARGIN);
  347. if ($margin->left->auto && $margin->right->auto) {
  348. $margin_value = ($parent_width - $this->get_full_width()) / 2;
  349. $margin->left->value = $margin_value;
  350. $margin->right->value = $margin_value;
  351. } else {
  352. // If there is exactly one value specified as 'auto', its used value follows from the equality.
  353. if ($margin->left->auto) {
  354. $margin->left->value = $parent_width - $this->get_full_width();
  355. } elseif ($margin->right->auto) {
  356. $margin->right->value = $parent_width - $this->get_full_width();
  357. };
  358. };
  359. $this->setCSSProperty(CSS_MARGIN, $margin);
  360. $this->width = $this->get_width();
  361. }
  362. function get_descender() {
  363. return 0;
  364. }
  365. function get_ascender() {
  366. return 0;
  367. }
  368. function _get_vert_extra() {
  369. return
  370. $this->get_extra_top() +
  371. $this->get_extra_bottom();
  372. }
  373. function _get_hor_extra() {
  374. return
  375. $this->get_extra_left() +
  376. $this->get_extra_right();
  377. }
  378. // Width:
  379. // 'get-min-width' stub
  380. function get_min_width(&$context) {
  381. die("OOPS! Unoverridden get_min_width called in class ".get_class($this)." inside ".get_class($this->parent));
  382. }
  383. function get_preferred_width(&$context) {
  384. return $this->get_max_width($context) - $this->_get_hor_extra();
  385. }
  386. function get_preferred_minimum_width(&$context) {
  387. return $this->get_min_width($context);
  388. }
  389. // 'get-max-width' stub
  390. function get_max_width(&$context) {
  391. die("OOPS! Unoverridden get_max_width called in class ".get_class($this)." inside ".get_class($this->parent));
  392. }
  393. function get_max_width_natural(&$context) {
  394. return $this->get_max_width($context);
  395. }
  396. function get_full_width() {
  397. return $this->get_width() + $this->_get_hor_extra();
  398. }
  399. function put_full_width($value) {
  400. // Calculate value of additional horizontal space consumed by margins and padding
  401. $this->width = $value - $this->_get_hor_extra();
  402. }
  403. function &_get_containing_block() {
  404. $position = $this->get_css_property(CSS_POSITION);
  405. switch ($position) {
  406. case POSITION_ABSOLUTE:
  407. $containing_block =& $this->_get_containing_block_absolute();
  408. return $containing_block;
  409. case POSITION_FIXED:
  410. $containing_block =& $this->_get_containing_block_fixed();
  411. return $containing_block;
  412. case POSITION_STATIC:
  413. case POSITION_RELATIVE:
  414. $containing_block =& $this->_get_containing_block_static();
  415. return $containing_block;
  416. default:
  417. die(sprintf('Unexpected position enum value: %d', $position));
  418. };
  419. }
  420. function &_get_containing_block_fixed() {
  421. $media = $GLOBALS['g_media'];
  422. $containing_block = array();
  423. $containing_block['left'] = mm2pt($media->margins['left']);
  424. $containing_block['right'] = mm2pt($media->margins['left'] + $media->real_width());
  425. $containing_block['top'] = mm2pt($media->margins['bottom'] + $media->real_height());
  426. $containing_block['bottom'] = mm2pt($media->margins['bottom']);
  427. return $containing_block;
  428. }
  429. // Get the position and size of containing block for current
  430. // ABSOLUTE POSITIONED element. It is assumed that this function
  431. // is called for ABSOLUTE positioned boxes ONLY
  432. //
  433. // @return associative array with 'top', 'bottom', 'right' and 'left'
  434. // indices in data space describing the position of containing block
  435. //
  436. function &_get_containing_block_absolute() {
  437. $parent =& $this->parent;
  438. // No containing block at all...
  439. // How could we get here?
  440. if (is_null($parent)) {
  441. trigger_error("No containing block found for absolute-positioned element",
  442. E_USER_ERROR);
  443. };
  444. // CSS 2.1:
  445. // If the element has 'position: absolute', the containing block is established by the
  446. // nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed', in the following way:
  447. // - In the case that the ancestor is inline-level, the containing block depends on
  448. // the 'direction' property of the ancestor:
  449. // 1. If the 'direction' is 'ltr', the top and left of the containing block are the top and left
  450. // content edges of the first box generated by the ancestor, and the bottom and right are the
  451. // bottom and right content edges of the last box of the ancestor.
  452. // 2. If the 'direction' is 'rtl', the top and right are the top and right edges of the first
  453. // box generated by the ancestor, and the bottom and left are the bottom and left content
  454. // edges of the last box of the ancestor.
  455. // - Otherwise, the containing block is formed by the padding edge of the ancestor.
  456. // TODO: inline-level ancestors
  457. while ((!is_null($parent->parent)) &&
  458. ($parent->get_css_property(CSS_POSITION) === POSITION_STATIC)) {
  459. $parent =& $parent->parent;
  460. }
  461. // Note that initial containg block (containig BODY element) will be formed by BODY margin edge,
  462. // unlike other blocks which are formed by padding edges
  463. if ($parent->parent) {
  464. // Normal containing block
  465. $containing_block = array();
  466. $containing_block['left'] = $parent->get_left_padding();
  467. $containing_block['right'] = $parent->get_right_padding();
  468. $containing_block['top'] = $parent->get_top_padding();
  469. $containing_block['bottom'] = $parent->get_bottom_padding();
  470. } else {
  471. // Initial containing block
  472. $containing_block = array();
  473. $containing_block['left'] = $parent->get_left_margin();
  474. $containing_block['right'] = $parent->get_right_margin();
  475. $containing_block['top'] = $parent->get_top_margin();
  476. $containing_block['bottom'] = $parent->get_bottom_margin();
  477. };
  478. return $containing_block;
  479. }
  480. function &_get_containing_block_static() {
  481. $parent =& $this->parent;
  482. // No containing block at all...
  483. // How could we get here?
  484. if (is_null($parent)) {
  485. die("No containing block found for static-positioned element");
  486. };
  487. while (!is_null($parent->parent) &&
  488. !$parent->isBlockLevel() &&
  489. !$parent->isCell()) {
  490. $parent =& $parent->parent;
  491. };
  492. // Note that initial containg block (containing BODY element)
  493. // will be formed by BODY margin edge,
  494. // unlike other blocks which are formed by content edges
  495. $containing_block = array();
  496. $containing_block['left'] = $parent->get_left();
  497. $containing_block['right'] = $parent->get_right();
  498. $containing_block['top'] = $parent->get_top();
  499. $containing_block['bottom'] = $parent->get_bottom();
  500. return $containing_block;
  501. }
  502. // Height constraint
  503. function get_height_constraint() {
  504. return $this->_height_constraint;
  505. }
  506. function put_height_constraint(&$wc) {
  507. $this->_height_constraint = $wc;
  508. }
  509. // Extends the box height to cover the given Y coordinate
  510. // If box height is already big enough, no changes will be made
  511. //
  512. // @param $y_coord Y coordinate should be covered by the box
  513. //
  514. function extend_height($y_coord) {
  515. $this->put_height(max($this->get_height(), $this->get_top() - $y_coord));
  516. }
  517. function extend_width($x_coord) {
  518. $this->put_width(max($this->get_width(), $x_coord - $this->get_left()));
  519. }
  520. function get_extra_bottom() {
  521. $border = $this->get_css_property(CSS_BORDER);
  522. return
  523. $this->get_margin_bottom() +
  524. $border->bottom->get_width() +
  525. $this->get_padding_bottom();
  526. }
  527. function get_extra_left() {
  528. $border = $this->get_css_property(CSS_BORDER);
  529. $left_border = $border->left;
  530. return
  531. $this->get_margin_left() +
  532. $left_border->get_width() +
  533. $this->get_padding_left();
  534. }
  535. function get_extra_right() {
  536. $border = $this->get_css_property(CSS_BORDER);
  537. $right_border = $border->right;
  538. return
  539. $this->get_margin_right() +
  540. $right_border->get_width() +
  541. $this->get_padding_right();
  542. }
  543. function get_extra_top() {
  544. $border = $this->get_css_property(CSS_BORDER);
  545. return
  546. $this->get_margin_top() +
  547. $border->top->get_width() +
  548. $this->get_padding_top();
  549. }
  550. function get_extra_line_left() { return 0; }
  551. function get_extra_line_right() { return 0; }
  552. function get_margin_bottom() {
  553. $margin = $this->get_css_property(CSS_MARGIN);
  554. return $margin->bottom->value;
  555. }
  556. function get_margin_left() {
  557. $margin = $this->get_css_property(CSS_MARGIN);
  558. return $margin->left->value;
  559. }
  560. function get_margin_right() {
  561. $margin = $this->get_css_property(CSS_MARGIN);
  562. return $margin->right->value;
  563. }
  564. function get_margin_top() {
  565. $margin = $this->get_css_property(CSS_MARGIN);
  566. return $margin->top->value;
  567. }
  568. function get_padding_right() {
  569. $padding = $this->get_css_property(CSS_PADDING);
  570. return $padding->right->value;
  571. }
  572. function get_padding_left() {
  573. $padding = $this->get_css_property(CSS_PADDING);
  574. return $padding->left->value;
  575. }
  576. function get_padding_top() {
  577. $padding = $this->get_css_property(CSS_PADDING);
  578. return $padding->top->value;
  579. }
  580. function get_border_top_width() {
  581. return $this->border->top->width;
  582. }
  583. function get_padding_bottom() {
  584. $padding = $this->get_css_property(CSS_PADDING);
  585. return $padding->bottom->value;
  586. }
  587. function get_left_border() {
  588. $padding = $this->get_css_property(CSS_PADDING);
  589. $border = $this->get_css_property(CSS_BORDER);
  590. return
  591. $this->get_left() -
  592. $padding->left->value -
  593. $border->left->get_width();
  594. }
  595. function get_right_border() {
  596. $padding = $this->get_css_property(CSS_PADDING);
  597. $border = $this->get_css_property(CSS_BORDER);
  598. return
  599. $this->get_left() +
  600. $this->get_width() +
  601. $padding->right->value +
  602. $border->right->get_width();
  603. }
  604. function get_top_border() {
  605. $border = $this->get_css_property(CSS_BORDER);
  606. return
  607. $this->get_top_padding() +
  608. $border->top->get_width();
  609. }
  610. function get_bottom_border() {
  611. $border = $this->get_css_property(CSS_BORDER);
  612. return
  613. $this->get_bottom_padding() -
  614. $border->bottom->get_width();
  615. }
  616. function get_left_padding() {
  617. $padding = $this->get_css_property(CSS_PADDING);
  618. return $this->get_left() - $padding->left->value;
  619. }
  620. function get_right_padding() {
  621. $padding = $this->get_css_property(CSS_PADDING);
  622. return $this->get_left() + $this->get_width() + $padding->right->value;
  623. }
  624. function get_top_padding() {
  625. $padding = $this->get_css_property(CSS_PADDING);
  626. return
  627. $this->get_top() +
  628. $padding->top->value;
  629. }
  630. function get_bottom_padding() {
  631. $padding = $this->get_css_property(CSS_PADDING);
  632. return $this->get_bottom() - $padding->bottom->value;
  633. }
  634. function get_left_margin() {
  635. return
  636. $this->get_left() -
  637. $this->get_extra_left();
  638. }
  639. function get_right_margin() {
  640. return
  641. $this->get_right() +
  642. $this->get_extra_right();
  643. }
  644. function get_bottom_margin() {
  645. return
  646. $this->get_bottom() -
  647. $this->get_extra_bottom();
  648. }
  649. function get_top_margin() {
  650. $margin = $this->get_css_property(CSS_MARGIN);
  651. return
  652. $this->get_top_border() +
  653. $margin->top->value;
  654. }
  655. // Geometry
  656. function contains_point_margin($x, $y) {
  657. // Actually, we treat a small area around the float as "inside" float;
  658. // it will help us to prevent incorrectly positioning float due the rounding errors
  659. $eps = 0.1;
  660. return
  661. $this->get_left_margin()-$eps <= $x &&
  662. $this->get_right_margin()+$eps >= $x &&
  663. $this->get_top_margin()+$eps >= $y &&
  664. $this->get_bottom_margin() < $y;
  665. }
  666. function get_width() {
  667. $wc = $this->get_css_property(CSS_WIDTH);
  668. if ($this->parent) {
  669. return $wc->apply($this->width, $this->parent->width);
  670. } else {
  671. return $wc->apply($this->width, $this->width);
  672. }
  673. }
  674. // Unlike real/constrained width, or min/max width,
  675. // expandable width shows the size current box CAN be expanded;
  676. // it is pretty obvious that width-constrained boxes will never be expanded;
  677. // any other box can be expanded up to its parent _expandable_ width -
  678. // as parent can be expanded too.
  679. //
  680. function get_expandable_width() {
  681. $wc = $this->get_css_property(CSS_WIDTH);
  682. if ($wc->isNull() && $this->parent) {
  683. return $this->parent->get_expandable_width();
  684. } else {
  685. return $this->get_width();
  686. };
  687. }
  688. function put_width($value) {
  689. // TODO: constraints
  690. $this->width = $value;
  691. }
  692. function get_height() {
  693. if ($this->_height_constraint->applicable($this)) {
  694. return $this->_height_constraint->apply($this->height, $this);
  695. } else {
  696. return $this->height;
  697. };
  698. }
  699. function get_height_padded() {
  700. return $this->get_height() + $this->get_padding_top() + $this->get_padding_bottom();
  701. }
  702. function put_height($value) {
  703. if ($this->_height_constraint->applicable($this)) {
  704. $this->height = $this->_height_constraint->apply($value, $this);
  705. } else {
  706. $this->height = $value;
  707. };
  708. }
  709. function put_full_height($value) {
  710. $this->put_height($value - $this->_get_vert_extra());
  711. }
  712. // Returns total height of current element:
  713. // top padding + top margin + content + bottom padding + bottom margin + top border + bottom border
  714. function get_full_height() {
  715. return $this->get_height() +
  716. $this->get_extra_top() +
  717. $this->get_extra_bottom();
  718. }
  719. function get_real_full_height() {
  720. return $this->get_full_height();
  721. }
  722. function out_of_flow() {
  723. $position = $this->get_css_property(CSS_POSITION);
  724. $display = $this->get_css_property(CSS_DISPLAY);
  725. return
  726. $position == POSITION_ABSOLUTE ||
  727. $position == POSITION_FIXED ||
  728. $display == 'none';
  729. }
  730. function moveto($x, $y) { $this->offset($x - $this->get_left(), $y - $this->get_top()); }
  731. function show(&$viewport) {
  732. $border = $this->get_css_property(CSS_BORDER);
  733. $background = $this->get_css_property(CSS_BACKGROUND);
  734. // Draw border of the box
  735. $border->show($viewport, $this);
  736. // Render background of the box
  737. $background->show($viewport, $this);
  738. parent::show($viewport);
  739. return true;
  740. }
  741. function show_fixed(&$viewport) {
  742. return $this->show($viewport);
  743. }
  744. function is_null() {
  745. return false;
  746. }
  747. function line_break_allowed() {
  748. $white_space = $this->get_css_property(CSS_WHITE_SPACE);
  749. $nowrap = $this->get_css_property(CSS_HTML2PS_NOWRAP);
  750. return
  751. ($white_space === WHITESPACE_NORMAL ||
  752. $white_space === WHITESPACE_PRE_WRAP ||
  753. $white_space === WHITESPACE_PRE_LINE) &&
  754. $nowrap === NOWRAP_NORMAL;
  755. }
  756. function get_left_background() { return $this->get_left_padding(); }
  757. function get_right_background() { return $this->get_right_padding(); }
  758. function get_top_background() { return $this->get_top_padding(); }
  759. function get_bottom_background() { return $this->get_bottom_padding(); }
  760. function isVisibleInFlow() {
  761. $visibility = $this->get_css_property(CSS_VISIBILITY);
  762. $position = $this->get_css_property(CSS_POSITION);
  763. return
  764. $visibility === VISIBILITY_VISIBLE &&
  765. $position !== POSITION_FIXED;
  766. }
  767. function reflow_footnote(&$parent, &$context) {
  768. $this->reflow_static($parent, $context);
  769. }
  770. /**
  771. * The 'top' and 'bottom' properties move relatively positioned
  772. * element(s) up or down without changing their size. 'top' moves
  773. * the boxes down, and 'bottom' moves them up. Since boxes are not
  774. * split or stretched as a result of 'top' or 'bottom', the computed
  775. * values are always: top = -bottom. If both are 'auto', their
  776. * computed values are both '0'. If one of them is 'auto', it
  777. * becomes the negative of the other. If neither is 'auto', 'bottom'
  778. * is ignored (i.e., the computed value of 'bottom' will be minus
  779. * the value of 'top').
  780. */
  781. function offsetRelative() {
  782. /**
  783. * Note that percentage positioning values are ignored for
  784. * relative positioning
  785. */
  786. /**
  787. * Check if 'top' value is percentage
  788. */
  789. $top = $this->get_css_property(CSS_TOP);
  790. if ($top->isNormal()) {
  791. $top_value = $top->getPoints();
  792. } elseif ($top->isPercentage()) {
  793. $containing_block = $this->_get_containing_block();
  794. $containing_block_height = $containing_block['top'] - $containing_block['bottom'];
  795. $top_value = $containing_block_height * $top->getPercentage() / 100;
  796. } elseif ($top->isAuto()) {
  797. $top_value = null;
  798. }
  799. /**
  800. * Check if 'bottom' value is percentage
  801. */
  802. $bottom = $this->get_css_property(CSS_BOTTOM);
  803. if ($bottom->isNormal()) {
  804. $bottom_value = $bottom->getPoints();
  805. } elseif ($bottom->isPercentage()) {
  806. $containing_block = $this->_get_containing_block();
  807. $containing_block_height = $containing_block['top'] - $containing_block['bottom'];
  808. $bottom_value = $containing_block_height * $bottom->getPercentage() / 100;
  809. } elseif ($bottom->isAuto()) {
  810. $bottom_value = null;
  811. }
  812. /**
  813. * Calculate vertical offset for relative positioned box
  814. */
  815. if (!is_null($top_value)) {
  816. $vertical_offset = -$top_value;
  817. } elseif (!is_null($bottom_value)) {
  818. $vertical_offset = $bottom_value;
  819. } else {
  820. $vertical_offset = 0;
  821. };
  822. /**
  823. * Check if 'left' value is percentage
  824. */
  825. $left = $this->get_css_property(CSS_LEFT);
  826. if ($left->isNormal()) {
  827. $left_value = $left->getPoints();
  828. } elseif ($left->isPercentage()) {
  829. $containing_block = $this->_get_containing_block();
  830. $containing_block_width = $containing_block['right'] - $containing_block['left'];
  831. $left_value = $containing_block_width * $left->getPercentage() / 100;
  832. } elseif ($left->isAuto()) {
  833. $left_value = null;
  834. }
  835. /**
  836. * Check if 'right' value is percentage
  837. */
  838. $right = $this->get_css_property(CSS_RIGHT);
  839. if ($right->isNormal()) {
  840. $right_value = $right->getPoints();
  841. } elseif ($right->isPercentage()) {
  842. $containing_block = $this->_get_containing_block();
  843. $containing_block_width = $containing_block['right'] - $containing_block['left'];
  844. $right_value = $containing_block_width * $right->getPercentage() / 100;
  845. } elseif ($right->isAuto()) {
  846. $right_value = null;
  847. }
  848. /**
  849. * Calculate vertical offset for relative positioned box
  850. */
  851. if (!is_null($left_value)) {
  852. $horizontal_offset = $left_value;
  853. } elseif (!is_null($right_value)) {
  854. $horizontal_offset = -$right_value;
  855. } else {
  856. $horizontal_offset = 0;
  857. };
  858. $this->offset($horizontal_offset,
  859. $vertical_offset);
  860. }
  861. }
  862. ?>