Absolute.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. */
  8. namespace Dompdf\Positioner;
  9. use Dompdf\FrameDecorator\AbstractFrameDecorator;
  10. /**
  11. * Positions absolutely positioned frames
  12. */
  13. class Absolute extends AbstractPositioner
  14. {
  15. /**
  16. * @param AbstractFrameDecorator $frame
  17. */
  18. function position(AbstractFrameDecorator $frame)
  19. {
  20. $style = $frame->get_style();
  21. $p = $frame->find_positionned_parent();
  22. list($x, $y, $w, $h) = $frame->get_containing_block();
  23. $top = $style->length_in_pt($style->top, $h);
  24. $right = $style->length_in_pt($style->right, $w);
  25. $bottom = $style->length_in_pt($style->bottom, $h);
  26. $left = $style->length_in_pt($style->left, $w);
  27. if ($p && !($left === "auto" && $right === "auto")) {
  28. // Get the parent's padding box (see http://www.w3.org/TR/CSS21/visuren.html#propdef-top)
  29. list($x, $y, $w, $h) = $p->get_padding_box();
  30. }
  31. list($width, $height) = [$frame->get_margin_width(), $frame->get_margin_height()];
  32. $orig_style = $frame->get_original_style();
  33. $orig_width = $orig_style->width;
  34. $orig_height = $orig_style->height;
  35. /****************************
  36. *
  37. * Width auto:
  38. * ____________| left=auto | left=fixed |
  39. * right=auto | A | B |
  40. * right=fixed | C | D |
  41. *
  42. * Width fixed:
  43. * ____________| left=auto | left=fixed |
  44. * right=auto | E | F |
  45. * right=fixed | G | H |
  46. *****************************/
  47. if ($left === "auto") {
  48. if ($right === "auto") {
  49. // A or E - Keep the frame at the same position
  50. $x = $x + $frame->find_block_parent()->get_current_line_box()->w;
  51. } else {
  52. if ($orig_width === "auto") {
  53. // C
  54. $x += $w - $width - $right;
  55. } else {
  56. // G
  57. $x += $w - $width - $right;
  58. }
  59. }
  60. } else {
  61. if ($right === "auto") {
  62. // B or F
  63. $x += (float)$left;
  64. } else {
  65. if ($orig_width === "auto") {
  66. // D - TODO change width
  67. $x += (float)$left;
  68. } else {
  69. // H - Everything is fixed: left + width win
  70. $x += (float)$left;
  71. }
  72. }
  73. }
  74. // The same vertically
  75. if ($top === "auto") {
  76. if ($bottom === "auto") {
  77. // A or E - Keep the frame at the same position
  78. $y = $frame->find_block_parent()->get_current_line_box()->y;
  79. } else {
  80. if ($orig_height === "auto") {
  81. // C
  82. $y += (float)$h - $height - (float)$bottom;
  83. } else {
  84. // G
  85. $y += (float)$h - $height - (float)$bottom;
  86. }
  87. }
  88. } else {
  89. if ($bottom === "auto") {
  90. // B or F
  91. $y += (float)$top;
  92. } else {
  93. if ($orig_height === "auto") {
  94. // D - TODO change height
  95. $y += (float)$top;
  96. } else {
  97. // H - Everything is fixed: top + height win
  98. $y += (float)$top;
  99. }
  100. }
  101. }
  102. $frame->set_position($x, $y);
  103. }
  104. }