text_renderer.cls.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: text_renderer.cls.php,v $
  6. * Created on: 2004-06-01
  7. *
  8. * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this library in the file LICENSE.LGPL; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  23. * 02111-1307 USA
  24. *
  25. * Alternatively, you may distribute this software under the terms of the
  26. * PHP License, version 3.0 or later. A copy of this license should have
  27. * been distributed with this file in the file LICENSE.PHP . If this is not
  28. * the case, you can obtain a copy at http://www.php.net/license/3_0.txt.
  29. *
  30. * The latest version of DOMPDF might be available at:
  31. * http://www.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  37. * @package dompdf
  38. *
  39. * Changes
  40. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  41. * @version dompdf_trunk_with_helmut_mods.20090528
  42. * - fix text decoration positions according to font metrics
  43. * @version 20090610
  44. * - better accuracy on using different renderer as cpdf, added comments
  45. */
  46. /* $Id: text_renderer.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  47. /**
  48. * Renders text frames
  49. *
  50. * @access private
  51. * @package dompdf
  52. */
  53. class Text_Renderer extends Abstract_Renderer {
  54. const DECO_THICKNESS = 0.04; // Thickness of underline. Screen: 0.08, print: better less, e.g. 0.04
  55. //Tweaking if $base and $descent are not accurate.
  56. //Check method_exists( $this->_canvas, "get_cpdf" )
  57. //- For cpdf these can and must stay 0, because font metrics are used directly.
  58. //- For other renderers, if different values are wanted, separate the parameter sets.
  59. // But $size and $size-$height seem to be accurate enough
  60. const UNDERLINE_OFFSET = 0.0; // Relative to bottom of text, as fraction of height.
  61. const OVERLINE_OFFSET = 0.0; // Relative to top of text
  62. const LINETHROUGH_OFFSET = 0.0; // Relative to centre of text.
  63. const DECO_EXTENSION = 0.0; // How far to extend lines past either end, in pt
  64. //........................................................................
  65. function render(Frame $frame) {
  66. $style = $frame->get_style();
  67. list($x, $y) = $frame->get_position();
  68. $cb = $frame->get_containing_block();
  69. if ( ($ml = $style->margin_left) === "auto" || $ml === "none" )
  70. $ml = 0;
  71. if ( ($pl = $style->padding_left) === "auto" || $pl === "none" )
  72. $pl = 0;
  73. if ( ($bl = $style->border_left_width) === "auto" || $bl === "none" )
  74. $bl = 0;
  75. $x += $style->length_in_pt( array($ml, $pl, $bl), $cb["w"] );
  76. $text = $frame->get_text();
  77. $font = $style->font_family;
  78. $size = $style->font_size;
  79. $height = $style->height;
  80. $spacing = $frame->get_text_spacing() + $style->word_spacing;
  81. // if ( preg_replace("/[\s]+/", "", $text) == "" )
  82. // return;
  83. $this->_canvas->text($x, $y, $text,
  84. $font, $size,
  85. $style->color, $spacing);
  86. if ( method_exists( $this->_canvas, "get_cpdf" ) ) {
  87. $base = ($this->_canvas->get_cpdf()->fonts[$this->_canvas->get_cpdf()->currentFont]['FontBBox'][3]*$size)/1000;
  88. $descent = ($this->_canvas->get_cpdf()->fonts[$this->_canvas->get_cpdf()->currentFont]['FontBBox'][1]*$size)/1000;
  89. //print '<pre>Text_Renderer cpdf:'.$base.' '.$descent.' '.$size.'</pre>';
  90. } else {
  91. //Descent is font part below baseline, typically negative. $height is about full height of font box.
  92. //$descent = -$size/6; is less accurate, depends on font family.
  93. $base = $size;
  94. $descent = $size-$height;
  95. //print '<pre>Text_Renderer other than cpdf:'.$base.' '.$descent.' '.$size.'</pre>';
  96. }
  97. // Handle text decoration:
  98. // http://www.w3.org/TR/CSS21/text.html#propdef-text-decoration
  99. // Draw all applicable text-decorations. Start with the root and work
  100. // our way down.
  101. $p = $frame;
  102. $stack = array();
  103. while ( $p = $p->get_parent() )
  104. $stack[] = $p;
  105. while ( count($stack) > 0 ) {
  106. $f = array_pop($stack);
  107. $deco_y = $y;
  108. if ( ($text_deco = $f->get_style()->text_decoration) === "none" )
  109. continue;
  110. $color = $f->get_style()->color;
  111. switch ($text_deco) {
  112. default:
  113. continue;
  114. case "underline":
  115. $deco_y += $base - $descent+ $size * (self::UNDERLINE_OFFSET - self::DECO_THICKNESS/2);
  116. break;
  117. case "overline":
  118. $deco_y += $size * (self::OVERLINE_OFFSET + self::DECO_THICKNESS/2);
  119. break;
  120. case "line-through":
  121. $deco_y += $base * 0.7 + $size * self::LINETHROUGH_OFFSET;
  122. break;
  123. }
  124. $dx = 0;
  125. $x1 = $x - self::DECO_EXTENSION;
  126. $x2 = $x + $style->width + $dx + self::DECO_EXTENSION;
  127. $this->_canvas->line($x1, $deco_y, $x2, $deco_y, $color, $size * self::DECO_THICKNESS);
  128. }
  129. }
  130. }