PageRenderTime 39ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/application/helpers/dompdf/include/text_renderer.cls.php

https://bitbucket.org/hlevine/myclientbase-south-african-version
PHP | 162 lines | 64 code | 30 blank | 68 comment | 18 complexity | dfe76d1bd37ffc1145c1ac24bb640df1 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1, GPL-2.0
  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.digitaljunkies.ca/dompdf
  32. *
  33. * @link http://www.digitaljunkies.ca/dompdf
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  37. * @package dompdf
  38. * @version 0.5.1
  39. *
  40. * Changes
  41. * @contributor Helmut Tischer <htischer@weihenstephan.org>
  42. * @version dompdf_trunk_with_helmut_mods.20090528
  43. * - fix text decoration positions according to font metrics
  44. * @version 20090610
  45. * - better accuracy on using different renderer as cpdf, added comments
  46. */
  47. /* $Id: text_renderer.cls.php,v 1.8 2008-03-12 06:35:43 benjcarson Exp $ */
  48. /**
  49. * Renders text frames
  50. *
  51. * @access private
  52. * @package dompdf
  53. */
  54. class Text_Renderer extends Abstract_Renderer {
  55. const DECO_THICKNESS = 0.04; // Thickness of underline. Screen: 0.08, print: better less, e.g. 0.04
  56. //Tweaking if $base and $descent are not accurate.
  57. //Check method_exists( $this->_canvas, "get_cpdf" )
  58. //- For cpdf these can and must stay 0, because font metrics are used directly.
  59. //- For other renderers, if different values are wanted, separate the parameter sets.
  60. // But $size and $size-$height seem to be accurate enough
  61. const UNDERLINE_OFFSET = 0.0; // Relative to bottom of text, as fraction of height.
  62. const OVERLINE_OFFSET = 0.0; // Relative to top of text
  63. const LINETHROUGH_OFFSET = 0.0; // Relative to centre of text.
  64. const DECO_EXTENSION = 0.0; // How far to extend lines past either end, in pt
  65. //........................................................................
  66. function render(Frame $frame) {
  67. $style = $frame->get_style();
  68. list($x, $y) = $frame->get_position();
  69. $cb = $frame->get_containing_block();
  70. if ( ($ml = $style->margin_left) == "auto" || $ml == "none" )
  71. $ml = 0;
  72. if ( ($pl = $style->padding_left) == "auto" || $pl == "none" )
  73. $pl = 0;
  74. if ( ($bl = $style->border_left_width) == "auto" || $bl == "none" )
  75. $bl = 0;
  76. $x += $style->length_in_pt( array($ml, $pl, $bl), $cb["w"] );
  77. $text = $frame->get_text();
  78. $font = $style->font_family;
  79. $size = $style->font_size;
  80. $height = $style->height;
  81. $spacing = $frame->get_text_spacing() + $style->word_spacing;
  82. // if ( preg_replace("/[\s]+/", "", $text) == "" )
  83. // return;
  84. $this->_canvas->text($x, $y, $text,
  85. $font, $size,
  86. $style->color, $spacing);
  87. if ( method_exists( $this->_canvas, "get_cpdf" ) ) {
  88. $base = ($this->_canvas->get_cpdf()->fonts[$this->_canvas->get_cpdf()->currentFont]['FontBBox'][3]*$size)/1000;
  89. $descent = ($this->_canvas->get_cpdf()->fonts[$this->_canvas->get_cpdf()->currentFont]['FontBBox'][1]*$size)/1000;
  90. //print '<pre>Text_Renderer cpdf:'.$base.' '.$descent.' '.$size.'</pre>';
  91. } else {
  92. //Descent is font part below baseline, typically negative. $height is about full height of font box.
  93. //$descent = -$size/6; is less accurate, depends on font family.
  94. $base = $size;
  95. $descent = $size-$height;
  96. //print '<pre>Text_Renderer other than cpdf:'.$base.' '.$descent.' '.$size.'</pre>';
  97. }
  98. // Handle text decoration:
  99. // http://www.w3.org/TR/CSS21/text.html#propdef-text-decoration
  100. // Draw all applicable text-decorations. Start with the root and work
  101. // our way down.
  102. $p = $frame;
  103. $stack = array();
  104. while ( $p = $p->get_parent() )
  105. $stack[] = $p;
  106. while ( count($stack) > 0 ) {
  107. $f = array_pop($stack);
  108. $deco_y = $y;
  109. if ( ($text_deco = $f->get_style()->text_decoration) === "none" )
  110. continue;
  111. $color = $f->get_style()->color;
  112. switch ($text_deco) {
  113. default:
  114. continue;
  115. case "underline":
  116. $deco_y += $base - $descent+ $size * (self::UNDERLINE_OFFSET - self::DECO_THICKNESS/2);
  117. break;
  118. case "overline":
  119. $deco_y += $size * (self::OVERLINE_OFFSET + self::DECO_THICKNESS/2);
  120. break;
  121. case "line-through":
  122. $deco_y += $base * 0.7 + $size * self::LINETHROUGH_OFFSET;
  123. break;
  124. }
  125. $dx = 0;
  126. $x1 = $x - self::DECO_EXTENSION;
  127. $x2 = $x + $style->width + $dx + self::DECO_EXTENSION;
  128. $this->_canvas->line($x1, $deco_y, $x2, $deco_y, $color, $size * self::DECO_THICKNESS);
  129. }
  130. }
  131. }
  132. ?>