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

/include/list_bullet_renderer.cls.php

https://github.com/practo/dompdf
PHP | 200 lines | 110 code | 31 blank | 59 comment | 10 complexity | 71df6763ca7ece778e325cc8c6ac7d8f MD5 | raw file
  1. <?php
  2. /**
  3. * DOMPDF - PHP5 HTML to PDF renderer
  4. *
  5. * File: $RCSfile: list_bullet_renderer.cls.php,v $
  6. * Created on: 2004-06-23
  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 20090622
  42. * - bullet size proportional to font size, center position
  43. */
  44. /* $Id$ */
  45. /**
  46. * Renders list bullets
  47. *
  48. * @access private
  49. * @package dompdf
  50. */
  51. class List_Bullet_Renderer extends Abstract_Renderer {
  52. //........................................................................
  53. private function make_counter($n, $type, $pad = null){
  54. $n = intval($n);
  55. $text = "";
  56. $uppercase = false;
  57. switch ($type) {
  58. case "decimal-leading-zero":
  59. case "decimal":
  60. case "1":
  61. if ($pad)
  62. $text = str_pad($n, $pad, "0", STR_PAD_LEFT);
  63. else
  64. $text = $n;
  65. break;
  66. case "upper-alpha":
  67. case "upper-latin":
  68. case "A":
  69. $uppercase = true;
  70. case "lower-alpha":
  71. case "lower-latin":
  72. case "a":
  73. $text = chr( ($n % 26) + ord('a') - 1);
  74. break;
  75. case "upper-roman":
  76. case "I":
  77. $uppercase = true;
  78. case "lower-roman":
  79. case "i":
  80. $text = dec2roman($n);
  81. break;
  82. case "lower-greek":
  83. $text = unichr($n + 944);
  84. break;
  85. }
  86. if ($uppercase)
  87. $text = strtoupper($text);
  88. return $text.".";
  89. }
  90. function render(Frame $frame) {
  91. $style = $frame->get_style();
  92. $font_size = $style->get_font_size();
  93. $line_height = $style->length_in_pt($style->line_height, $frame->get_containing_block("w"));
  94. $this->_set_opacity( $frame->get_opacity( $style->opacity ) );
  95. // Handle list-style-image
  96. // If list style image is requested but missing, fall back to predefined types
  97. if ( $style->list_style_image !== "none" &&
  98. !Image_Cache::is_broken($img = $frame->get_image_url())) {
  99. list($x,$y) = $frame->get_position();
  100. //For expected size and aspect, instead of box size, use image natural size scaled to DPI.
  101. // Resample the bullet image to be consistent with 'auto' sized images
  102. // See also Image_Frame_Reflower::get_min_max_width
  103. // Tested php ver: value measured in px, suffix "px" not in value: rtrim unnecessary.
  104. //$w = $frame->get_width();
  105. //$h = $frame->get_height();
  106. list($width, $height) = dompdf_getimagesize($img);
  107. $w = (((float)rtrim($width, "px")) * 72) / DOMPDF_DPI;
  108. $h = (((float)rtrim($height, "px")) * 72) / DOMPDF_DPI;
  109. $x -= $w;
  110. $y -= ($line_height - $font_size)/2; //Reverse hinting of list_bullet_positioner
  111. $this->_canvas->image( $img, $x, $y, $w, $h);
  112. } else {
  113. $bullet_style = $style->list_style_type;
  114. $fill = false;
  115. switch ($bullet_style) {
  116. default:
  117. case "disc":
  118. $fill = true;
  119. case "circle":
  120. list($x,$y) = $frame->get_position();
  121. $r = ($font_size*(List_Bullet_Frame_Decorator::BULLET_SIZE /*-List_Bullet_Frame_Decorator::BULLET_THICKNESS*/ ))/2;
  122. $x -= $font_size*(List_Bullet_Frame_Decorator::BULLET_SIZE/2);
  123. $y += ($font_size*(1-List_Bullet_Frame_Decorator::BULLET_DESCENT))/2;
  124. $o = $font_size*List_Bullet_Frame_Decorator::BULLET_THICKNESS;
  125. $this->_canvas->circle($x, $y, $r, $style->color, $o, null, $fill);
  126. break;
  127. case "square":
  128. list($x, $y) = $frame->get_position();
  129. $w = $font_size*List_Bullet_Frame_Decorator::BULLET_SIZE;
  130. $x -= $w;
  131. $y += ($font_size*(1-List_Bullet_Frame_Decorator::BULLET_DESCENT-List_Bullet_Frame_Decorator::BULLET_SIZE))/2;
  132. $this->_canvas->filled_rectangle($x, $y, $w, $w, $style->color);
  133. break;
  134. case "decimal-leading-zero":
  135. case "decimal":
  136. case "lower-alpha":
  137. case "lower-latin":
  138. case "lower-roman":
  139. case "lower-greek":
  140. case "upper-alpha":
  141. case "upper-latin":
  142. case "upper-roman":
  143. case "1": // HTML 4.0 compatibility
  144. case "a":
  145. case "i":
  146. case "A":
  147. case "I":
  148. list($x,$y) = $frame->get_position();
  149. $pad = null;
  150. if ( $bullet_style === "decimal-leading-zero" ) {
  151. $pad = strlen($frame->get_parent()->get_parent()->get_node()->getAttribute("dompdf-children-count"));
  152. }
  153. $index = $frame->get_node()->getAttribute("dompdf-counter");
  154. $text = $this->make_counter($index, $bullet_style, $pad);
  155. $font_family = $style->font_family;
  156. $spacing = 0; //$frame->get_text_spacing() + $style->word_spacing;
  157. if ( trim($text) == "" )
  158. return;
  159. $x -= Font_Metrics::get_text_width($text, $font_family, $font_size, $spacing);
  160. $this->_canvas->text($x, $y, $text,
  161. $font_family, $font_size,
  162. $style->color, $spacing);
  163. case "none":
  164. break;
  165. }
  166. }
  167. }
  168. }