PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/dompdf/dompdf/include/text_frame_reflower.cls.php

https://gitlab.com/4gdevs/online-class-record-system
PHP | 441 lines | 237 code | 105 blank | 99 comment | 43 complexity | 5b933c195a36edf67eb027c35f8e4be3 MD5 | raw file
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @author Fabien Ménager <fabien.menager@gmail.com>
  7. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  8. */
  9. /**
  10. * Reflows text frames.
  11. *
  12. * @access private
  13. * @package dompdf
  14. */
  15. class Text_Frame_Reflower extends Frame_Reflower {
  16. /**
  17. * @var Block_Frame_Decorator
  18. */
  19. protected $_block_parent; // Nearest block-level ancestor
  20. /**
  21. * @var Text_Frame_Decorator
  22. */
  23. protected $_frame;
  24. public static $_whitespace_pattern = "/[ \t\r\n\f]+/u";
  25. function __construct(Text_Frame_Decorator $frame) { parent::__construct($frame); }
  26. //........................................................................
  27. protected function _collapse_white_space($text) {
  28. //$text = $this->_frame->get_text();
  29. // if ( $this->_block_parent->get_current_line_box->w == 0 )
  30. // $text = ltrim($text, " \n\r\t");
  31. return preg_replace(self::$_whitespace_pattern, " ", $text);
  32. }
  33. //........................................................................
  34. protected function _line_break($text) {
  35. $style = $this->_frame->get_style();
  36. $size = $style->font_size;
  37. $font = $style->font_family;
  38. $current_line = $this->_block_parent->get_current_line_box();
  39. // Determine the available width
  40. $line_width = $this->_frame->get_containing_block("w");
  41. $current_line_width = $current_line->left + $current_line->w + $current_line->right;
  42. $available_width = $line_width - $current_line_width;
  43. // Account for word-spacing
  44. $word_spacing = $style->length_in_pt($style->word_spacing);
  45. $char_spacing = $style->length_in_pt($style->letter_spacing);
  46. // Determine the frame width including margin, padding & border
  47. $text_width = Font_Metrics::get_text_width($text, $font, $size, $word_spacing, $char_spacing);
  48. $mbp_width =
  49. $style->length_in_pt( array( $style->margin_left,
  50. $style->border_left_width,
  51. $style->padding_left,
  52. $style->padding_right,
  53. $style->border_right_width,
  54. $style->margin_right), $line_width );
  55. $frame_width = $text_width + $mbp_width;
  56. // Debugging:
  57. // pre_r("Text: '" . htmlspecialchars($text). "'");
  58. // pre_r("width: " .$frame_width);
  59. // pre_r("textwidth + delta: $text_width + $mbp_width");
  60. // pre_r("font-size: $size");
  61. // pre_r("cb[w]: " .$line_width);
  62. // pre_r("available width: " . $available_width);
  63. // pre_r("current line width: " . $current_line_width);
  64. // pre_r($words);
  65. if ( $frame_width <= $available_width )
  66. return false;
  67. // split the text into words
  68. $words = preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  69. $wc = count($words);
  70. // Determine the split point
  71. $width = 0;
  72. $str = "";
  73. reset($words);
  74. // @todo support <shy>, <wbr>
  75. for ($i = 0; $i < $wc; $i += 2) {
  76. $word = $words[$i] . (isset($words[$i+1]) ? $words[$i+1] : "");
  77. $word_width = Font_Metrics::get_text_width($word, $font, $size, $word_spacing, $char_spacing);
  78. if ( $width + $word_width + $mbp_width > $available_width )
  79. break;
  80. $width += $word_width;
  81. $str .= $word;
  82. }
  83. $break_word = ($style->word_wrap === "break-word");
  84. // The first word has overflowed. Force it onto the line
  85. if ( $current_line_width == 0 && $width == 0 ) {
  86. $s = "";
  87. $last_width = 0;
  88. if ( $break_word ) {
  89. for ( $j = 0; $j < strlen($word); $j++ ) {
  90. $s .= $word[$j];
  91. $_width = Font_Metrics::get_text_width($s, $font, $size, $word_spacing, $char_spacing);
  92. if ($_width > $available_width) {
  93. break;
  94. }
  95. $last_width = $_width;
  96. }
  97. }
  98. if ( $break_word && $last_width > 0 ) {
  99. $width += $last_width;
  100. $str .= substr($s, 0, -1);
  101. }
  102. else {
  103. $width += $word_width;
  104. $str .= $word;
  105. }
  106. }
  107. $offset = mb_strlen($str);
  108. // More debugging:
  109. // pre_var_dump($str);
  110. // pre_r("Width: ". $width);
  111. // pre_r("Offset: " . $offset);
  112. return $offset;
  113. }
  114. //........................................................................
  115. protected function _newline_break($text) {
  116. if ( ($i = mb_strpos($text, "\n")) === false)
  117. return false;
  118. return $i+1;
  119. }
  120. //........................................................................
  121. protected function _layout_line() {
  122. $frame = $this->_frame;
  123. $style = $frame->get_style();
  124. $text = $frame->get_text();
  125. $size = $style->font_size;
  126. $font = $style->font_family;
  127. // Determine the text height
  128. $style->height = Font_Metrics::get_font_height( $font, $size );
  129. $split = false;
  130. $add_line = false;
  131. // Handle text transform:
  132. // http://www.w3.org/TR/CSS21/text.html#propdef-text-transform
  133. switch (strtolower($style->text_transform)) {
  134. default: break;
  135. case "capitalize": $text = mb_convert_case($text, MB_CASE_TITLE); break;
  136. case "uppercase": $text = mb_convert_case($text, MB_CASE_UPPER); break;
  137. case "lowercase": $text = mb_convert_case($text, MB_CASE_LOWER); break;
  138. }
  139. // Handle white-space property:
  140. // http://www.w3.org/TR/CSS21/text.html#propdef-white-space
  141. switch ($style->white_space) {
  142. default:
  143. case "normal":
  144. $frame->set_text( $text = $this->_collapse_white_space($text) );
  145. if ( $text == "" )
  146. break;
  147. $split = $this->_line_break($text);
  148. break;
  149. case "pre":
  150. $split = $this->_newline_break($text);
  151. $add_line = $split !== false;
  152. break;
  153. case "nowrap":
  154. $frame->set_text( $text = $this->_collapse_white_space($text) );
  155. break;
  156. case "pre-wrap":
  157. $split = $this->_newline_break($text);
  158. if ( ($tmp = $this->_line_break($text)) !== false ) {
  159. $add_line = $split < $tmp;
  160. $split = min($tmp, $split);
  161. } else
  162. $add_line = true;
  163. break;
  164. case "pre-line":
  165. // Collapse white-space except for \n
  166. $frame->set_text( $text = preg_replace( "/[ \t]+/u", " ", $text ) );
  167. if ( $text == "" )
  168. break;
  169. $split = $this->_newline_break($text);
  170. if ( ($tmp = $this->_line_break($text)) !== false ) {
  171. $add_line = $split < $tmp;
  172. $split = min($tmp, $split);
  173. } else
  174. $add_line = true;
  175. break;
  176. }
  177. // Handle degenerate case
  178. if ( $text === "" )
  179. return;
  180. if ( $split !== false) {
  181. // Handle edge cases
  182. if ( $split == 0 && $text === " " ) {
  183. $frame->set_text("");
  184. return;
  185. }
  186. if ( $split == 0 ) {
  187. // Trim newlines from the beginning of the line
  188. //$this->_frame->set_text(ltrim($text, "\n\r"));
  189. $this->_block_parent->add_line();
  190. $frame->position();
  191. // Layout the new line
  192. $this->_layout_line();
  193. }
  194. else if ( $split < mb_strlen($frame->get_text()) ) {
  195. // split the line if required
  196. $frame->split_text($split);
  197. $t = $frame->get_text();
  198. // Remove any trailing newlines
  199. if ( $split > 1 && $t[$split-1] === "\n" && !$frame->is_pre() )
  200. $frame->set_text( mb_substr($t, 0, -1) );
  201. // Do we need to trim spaces on wrapped lines? This might be desired, however, we
  202. // can't trim the lines here or the layout will be affected if trimming the line
  203. // leaves enough space to fit the next word in the text stream (because pdf layout
  204. // is performed elsewhere).
  205. /*if (!$this->_frame->get_prev_sibling() && !$this->_frame->get_next_sibling()) {
  206. $t = $this->_frame->get_text();
  207. $this->_frame->set_text( trim($t) );
  208. }*/
  209. }
  210. if ( $add_line ) {
  211. $this->_block_parent->add_line();
  212. $frame->position();
  213. }
  214. } else {
  215. // Remove empty space from start and end of line, but only where there isn't an inline sibling
  216. // and the parent node isn't an inline element with siblings
  217. // FIXME: Include non-breaking spaces?
  218. $t = $frame->get_text();
  219. $parent = $frame->get_parent();
  220. $is_inline_frame = get_class($parent) === 'Inline_Frame_Decorator';
  221. if ((!$is_inline_frame && !$frame->get_next_sibling())/* ||
  222. ( $is_inline_frame && !$parent->get_next_sibling())*/) { // fails <b>BOLD <u>UNDERLINED</u></b> becomes <b>BOLD<u>UNDERLINED</u></b>
  223. $t = rtrim($t);
  224. }
  225. if ((!$is_inline_frame && !$frame->get_prev_sibling())/* ||
  226. ( $is_inline_frame && !$parent->get_prev_sibling())*/) { // <span><span>A<span>B</span> C</span></span> fails (the whitespace is removed)
  227. $t = ltrim($t);
  228. }
  229. $frame->set_text( $t );
  230. }
  231. // Set our new width
  232. $width = $frame->recalculate_width();
  233. }
  234. //........................................................................
  235. function reflow(Block_Frame_Decorator $block = null) {
  236. $frame = $this->_frame;
  237. $page = $frame->get_root();
  238. $page->check_forced_page_break($this->_frame);
  239. if ( $page->is_full() )
  240. return;
  241. $this->_block_parent = /*isset($block) ? $block : */$frame->find_block_parent();
  242. // Left trim the text if this is the first text on the line and we're
  243. // collapsing white space
  244. // if ( $this->_block_parent->get_current_line()->w == 0 &&
  245. // ($frame->get_style()->white_space !== "pre" ||
  246. // $frame->get_style()->white_space !== "pre-wrap") ) {
  247. // $frame->set_text( ltrim( $frame->get_text() ) );
  248. // }
  249. $frame->position();
  250. $this->_layout_line();
  251. if ( $block ) {
  252. $block->add_frame_to_line($frame);
  253. }
  254. }
  255. //........................................................................
  256. // Returns an array(0 => min, 1 => max, "min" => min, "max" => max) of the
  257. // minimum and maximum widths of this frame
  258. function get_min_max_width() {
  259. /*if ( !is_null($this->_min_max_cache) )
  260. return $this->_min_max_cache;*/
  261. $frame = $this->_frame;
  262. $style = $frame->get_style();
  263. $this->_block_parent = $frame->find_block_parent();
  264. $line_width = $frame->get_containing_block("w");
  265. $str = $text = $frame->get_text();
  266. $size = $style->font_size;
  267. $font = $style->font_family;
  268. $word_spacing = $style->length_in_pt($style->word_spacing);
  269. $char_spacing = $style->length_in_pt($style->letter_spacing);
  270. switch($style->white_space) {
  271. default:
  272. case "normal":
  273. $str = preg_replace(self::$_whitespace_pattern," ", $str);
  274. case "pre-wrap":
  275. case "pre-line":
  276. // Find the longest word (i.e. minimum length)
  277. // This technique (using arrays & an anonymous function) is actually
  278. // faster than doing a single-pass character by character scan. Heh,
  279. // yes I took the time to bench it ;)
  280. $words = array_flip(preg_split("/[\s-]+/u",$str, -1, PREG_SPLIT_DELIM_CAPTURE));
  281. /*foreach($words as &$word) {
  282. $word = Font_Metrics::get_text_width($word, $font, $size, $word_spacing, $char_spacing);
  283. }*/
  284. array_walk($words, create_function('&$val,$str',
  285. '$val = Font_Metrics::get_text_width($str, "'.addslashes($font).'", '.$size.', '.$word_spacing.', '.$char_spacing.');'));
  286. arsort($words);
  287. $min = reset($words);
  288. break;
  289. case "pre":
  290. $lines = array_flip(preg_split("/\n/u", $str));
  291. /*foreach($words as &$word) {
  292. $word = Font_Metrics::get_text_width($word, $font, $size, $word_spacing, $char_spacing);
  293. }*/
  294. array_walk($lines, create_function('&$val,$str',
  295. '$val = Font_Metrics::get_text_width($str, "'.addslashes($font).'", '.$size.', '.$word_spacing.', '.$char_spacing.');'));
  296. arsort($lines);
  297. $min = reset($lines);
  298. break;
  299. case "nowrap":
  300. $min = Font_Metrics::get_text_width($this->_collapse_white_space($str), $font, $size, $word_spacing, $char_spacing);
  301. break;
  302. }
  303. switch ($style->white_space) {
  304. default:
  305. case "normal":
  306. case "nowrap":
  307. $str = preg_replace(self::$_whitespace_pattern," ", $text);
  308. break;
  309. case "pre-line":
  310. //XXX: Is this correct?
  311. $str = preg_replace( "/[ \t]+/u", " ", $text);
  312. case "pre-wrap":
  313. // Find the longest word (i.e. minimum length)
  314. $lines = array_flip(preg_split("/\n/", $text));
  315. /*foreach($words as &$word) {
  316. $word = Font_Metrics::get_text_width($word, $font, $size, $word_spacing, $char_spacing);
  317. }*/
  318. array_walk($lines, create_function('&$val,$str',
  319. '$val = Font_Metrics::get_text_width($str, "'.$font.'", '.$size.', '.$word_spacing.', '.$char_spacing.');'));
  320. arsort($lines);
  321. reset($lines);
  322. $str = key($lines);
  323. break;
  324. }
  325. $max = Font_Metrics::get_text_width($str, $font, $size, $word_spacing, $char_spacing);
  326. $delta = $style->length_in_pt(array($style->margin_left,
  327. $style->border_left_width,
  328. $style->padding_left,
  329. $style->padding_right,
  330. $style->border_right_width,
  331. $style->margin_right), $line_width);
  332. $min += $delta;
  333. $max += $delta;
  334. return $this->_min_max_cache = array($min, $max, "min" => $min, "max" => $max);
  335. }
  336. }