PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/extensions/pdf/dompdf/include/text_renderer.cls.php

https://bitbucket.org/sanbrar/zurmo_invoice
PHP | 150 lines | 80 code | 31 blank | 39 comment | 17 complexity | 8ebf13a5cb7342edfca5130403735914 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, LGPL-3.0, BSD-2-Clause, GPL-3.0
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://www.dompdf.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @author Helmut Tischer <htischer@weihenstephan.org>
  7. * @author Fabien Ménager <fabien.menager@gmail.com>
  8. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  9. * @version $Id: text_renderer.cls.php 471 2012-02-06 21:59:10Z fabien.menager $
  10. */
  11. /**
  12. * Renders text frames
  13. *
  14. * @access private
  15. * @package dompdf
  16. */
  17. class Text_Renderer extends Abstract_Renderer {
  18. const DECO_THICKNESS = 0.02; // Thickness of underline. Screen: 0.08, print: better less, e.g. 0.04
  19. //Tweaking if $base and $descent are not accurate.
  20. //Check method_exists( $this->_canvas, "get_cpdf" )
  21. //- For cpdf these can and must stay 0, because font metrics are used directly.
  22. //- For other renderers, if different values are wanted, separate the parameter sets.
  23. // But $size and $size-$height seem to be accurate enough
  24. const UNDERLINE_OFFSET = 0.0; // Relative to bottom of text, as fraction of height.
  25. const OVERLINE_OFFSET = 0.0; // Relative to top of text
  26. const LINETHROUGH_OFFSET = 0.0; // Relative to centre of text.
  27. const DECO_EXTENSION = 0.0; // How far to extend lines past either end, in pt
  28. //........................................................................
  29. function render(Frame $frame) {
  30. $text = $frame->get_text();
  31. if ( trim($text) === "" )
  32. return;
  33. $style = $frame->get_style();
  34. list($x, $y) = $frame->get_position();
  35. $cb = $frame->get_containing_block();
  36. if ( ($ml = $style->margin_left) === "auto" || $ml === "none" )
  37. $ml = 0;
  38. if ( ($pl = $style->padding_left) === "auto" || $pl === "none" )
  39. $pl = 0;
  40. if ( ($bl = $style->border_left_width) === "auto" || $bl === "none" )
  41. $bl = 0;
  42. $x += $style->length_in_pt( array($ml, $pl, $bl), $cb["w"] );
  43. $font = $style->font_family;
  44. $size = $frame_font_size = $style->font_size;
  45. $height = $style->height;
  46. $word_spacing = $frame->get_text_spacing() + $style->length_in_pt($style->word_spacing);
  47. $char_spacing = $style->length_in_pt($style->letter_spacing);
  48. $width = $style->width;
  49. /*$text = str_replace(
  50. array("{PAGE_NUM}"),
  51. array($this->_canvas->get_page_number()),
  52. $text
  53. );*/
  54. $this->_canvas->text($x, $y, $text,
  55. $font, $size,
  56. $style->color, $word_spacing, $char_spacing);
  57. $line = $frame->get_containing_line();
  58. // FIXME Instead of using the tallest frame to position,
  59. // the decoration, the text should be well placed
  60. if ( false && $line->tallest_frame ) {
  61. $base_frame = $line->tallest_frame;
  62. $style = $base_frame->get_style();
  63. $size = $style->font_size;
  64. $height = $line->h * ($size / $style->line_height);
  65. }
  66. if ( method_exists( $this->_canvas, "get_cpdf" ) ) {
  67. $cpdf = $this->_canvas->get_cpdf();
  68. //$cpdf_font = $cpdf->fonts[$style->font_family];
  69. //$base = ($cpdf_font["UnderlinePosition"]*$size)/1000;
  70. //$descent = (($cpdf_font["Ascender"]-$cpdf_font["Descender"])*$size)/1000;
  71. $fontBBox = $cpdf->fonts[$style->font_family]['FontBBox'];
  72. $base = (($fontBBox[3]*$size)/1000) * 0.90;
  73. $descent = ($fontBBox[1]*$size)/1000;
  74. //print '<pre>Text_Renderer cpdf:'.$base.' '.$descent.' '.$size.'</pre>';
  75. } else {
  76. //Descent is font part below baseline, typically negative. $height is about full height of font box.
  77. //$descent = -$size/6; is less accurate, depends on font family.
  78. // @todo Could we get font info for PDFlib adapter and others ?
  79. $base = $size*1.08;
  80. $descent = $size-$height;
  81. //print '<pre>Text_Renderer other than cpdf:'.$base.' '.$descent.' '.$size.'</pre>';
  82. }
  83. // Handle text decoration:
  84. // http://www.w3.org/TR/CSS21/text.html#propdef-text-decoration
  85. // Draw all applicable text-decorations. Start with the root and work our way down.
  86. $p = $frame;
  87. $stack = array();
  88. while ( $p = $p->get_parent() )
  89. $stack[] = $p;
  90. while ( isset($stack[0]) ) {
  91. $f = array_pop($stack);
  92. if ( ($text_deco = $f->get_style()->text_decoration) === "none" )
  93. continue;
  94. $deco_y = $y; //$line->y;
  95. $color = $f->get_style()->color;
  96. switch ($text_deco) {
  97. default:
  98. continue;
  99. case "underline":
  100. $deco_y += $base - $descent + $size * (self::UNDERLINE_OFFSET - self::DECO_THICKNESS/2);
  101. break;
  102. case "overline":
  103. $deco_y += $size * (self::OVERLINE_OFFSET + self::DECO_THICKNESS/2);
  104. break;
  105. case "line-through":
  106. $deco_y += $base * 0.7 + $size * self::LINETHROUGH_OFFSET;
  107. break;
  108. }
  109. $dx = 0;
  110. $x1 = $x - self::DECO_EXTENSION;
  111. $x2 = $x + $width + $dx + self::DECO_EXTENSION;
  112. $this->_canvas->line($x1, $deco_y, $x2, $deco_y, $color, $size * self::DECO_THICKNESS);
  113. }
  114. if (DEBUG_LAYOUT && DEBUG_LAYOUT_LINES) {
  115. $text_width = Font_Metrics::get_text_width($text, $font, $frame_font_size);
  116. $this->_debug_layout(array($x, $y, $text_width+($line->wc-1)*$word_spacing, $frame_font_size), "orange", array(0.5, 0.5));
  117. }
  118. }
  119. }