List_Bullet_Renderer

Renders list bullets.

Defined (1)

The class is defined in the following location(s).

/lib/dompdf/include/list_bullet_renderer.cls.php  
  1. class List_Bullet_Renderer extends Abstract_Renderer { 
  2. static function get_counter_chars($type) { 
  3. static $cache = array(); 
  4.  
  5. if ( isset($cache[$type]) ) { 
  6. return $cache[$type]; 
  7.  
  8. $uppercase = false; 
  9. $text = ""; 
  10.  
  11. switch ($type) { 
  12. case "decimal-leading-zero": 
  13. case "decimal": 
  14. case "1": 
  15. return "0123456789"; 
  16.  
  17. case "upper-alpha": 
  18. case "upper-latin": 
  19. case "A": 
  20. $uppercase = true; 
  21. case "lower-alpha": 
  22. case "lower-latin": 
  23. case "a": 
  24. $text = "abcdefghijklmnopqrstuvwxyz"; 
  25. break; 
  26.  
  27. case "upper-roman": 
  28. case "I": 
  29. $uppercase = true; 
  30. case "lower-roman": 
  31. case "i": 
  32. $text = "ivxlcdm"; 
  33. break; 
  34.  
  35. case "lower-greek": 
  36. for($i = 0; $i < 24; $i++) { 
  37. $text .= unichr($i+944); 
  38. break; 
  39.  
  40. if ( $uppercase ) { 
  41. $text = strtoupper($text); 
  42.  
  43. return $cache[$type] = "$text."; 
  44.  
  45. /** 
  46. * @param integer $n 
  47. * @param string $type 
  48. * @param integer $pad 
  49. * @return string 
  50. */ 
  51. private function make_counter($n, $type, $pad = null) { 
  52. $n = intval($n); 
  53. $text = ""; 
  54. $uppercase = false; 
  55.  
  56. switch ($type) { 
  57. case "decimal-leading-zero": 
  58. case "decimal": 
  59. case "1": 
  60. if ($pad)  
  61. $text = str_pad($n, $pad, "0", STR_PAD_LEFT); 
  62. else  
  63. $text = $n; 
  64. break; 
  65.  
  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.  
  76. case "upper-roman": 
  77. case "I": 
  78. $uppercase = true; 
  79. case "lower-roman": 
  80. case "i": 
  81. $text = dec2roman($n); 
  82. break; 
  83.  
  84. case "lower-greek": 
  85. $text = unichr($n + 944); 
  86. break; 
  87.  
  88. if ( $uppercase ) { 
  89. $text = strtoupper($text); 
  90.  
  91. return "$text."; 
  92.  
  93. function render(Frame $frame) { 
  94. $style = $frame->get_style(); 
  95. $font_size = $style->get_font_size(); 
  96. $line_height = $style->length_in_pt($style->line_height, $frame->get_containing_block("w")); 
  97.  
  98. $this->_set_opacity( $frame->get_opacity( $style->opacity ) ); 
  99.  
  100. $li = $frame->get_parent(); 
  101.  
  102. // Don't render bullets twice if if was split 
  103. if ($li->_splitted) { 
  104. return; 
  105.  
  106. // Handle list-style-image 
  107. // If list style image is requested but missing, fall back to predefined types 
  108. if ( $style->list_style_image !== "none" && 
  109. !Image_Cache::is_broken($img = $frame->get_image_url())) { 
  110.  
  111. list($x, $y) = $frame->get_position(); 
  112.  
  113. //For expected size and aspect, instead of box size, use image natural size scaled to DPI. 
  114. // Resample the bullet image to be consistent with 'auto' sized images 
  115. // See also Image_Frame_Reflower::get_min_max_width 
  116. // Tested php ver: value measured in px, suffix "px" not in value: rtrim unnecessary. 
  117. //$w = $frame->get_width(); 
  118. //$h = $frame->get_height(); 
  119. list($width, $height) = dompdf_getimagesize($img); 
  120. $dpi = $this->_dompdf->get_option("dpi"); 
  121. $w = ((float)rtrim($width, "px") * 72) / $dpi; 
  122. $h = ((float)rtrim($height, "px") * 72) / $dpi; 
  123.  
  124. $x -= $w; 
  125. $y -= ($line_height - $font_size)/2; //Reverse hinting of list_bullet_positioner 
  126.  
  127. $this->_canvas->image( $img, $x, $y, $w, $h); 
  128.  
  129. } else { 
  130.  
  131. $bullet_style = $style->list_style_type; 
  132.  
  133. $fill = false; 
  134.  
  135. switch ($bullet_style) { 
  136.  
  137. default: 
  138. case "disc": 
  139. $fill = true; 
  140.  
  141. case "circle": 
  142. list($x, $y) = $frame->get_position(); 
  143. $r = ($font_size*(List_Bullet_Frame_Decorator::BULLET_SIZE /**-List_Bullet_Frame_Decorator::BULLET_THICKNESS*/ ))/2; 
  144. $x -= $font_size*(List_Bullet_Frame_Decorator::BULLET_SIZE/2); 
  145. $y += ($font_size*(1-List_Bullet_Frame_Decorator::BULLET_DESCENT))/2; 
  146. $o = $font_size*List_Bullet_Frame_Decorator::BULLET_THICKNESS; 
  147. $this->_canvas->circle($x, $y, $r, $style->color, $o, null, $fill); 
  148. break; 
  149.  
  150. case "square": 
  151. list($x, $y) = $frame->get_position(); 
  152. $w = $font_size*List_Bullet_Frame_Decorator::BULLET_SIZE; 
  153. $x -= $w; 
  154. $y += ($font_size*(1-List_Bullet_Frame_Decorator::BULLET_DESCENT-List_Bullet_Frame_Decorator::BULLET_SIZE))/2; 
  155. $this->_canvas->filled_rectangle($x, $y, $w, $w, $style->color); 
  156. break; 
  157.  
  158. case "decimal-leading-zero": 
  159. case "decimal": 
  160. case "lower-alpha": 
  161. case "lower-latin": 
  162. case "lower-roman": 
  163. case "lower-greek": 
  164. case "upper-alpha": 
  165. case "upper-latin": 
  166. case "upper-roman": 
  167. case "1": // HTML 4.0 compatibility 
  168. case "a": 
  169. case "i": 
  170. case "A": 
  171. case "I": 
  172. $pad = null; 
  173. if ( $bullet_style === "decimal-leading-zero" ) { 
  174. $pad = strlen($li->get_parent()->get_node()->getAttribute("dompdf-children-count")); 
  175.  
  176. $node = $frame->get_node(); 
  177.  
  178. if ( !$node->hasAttribute("dompdf-counter") ) { 
  179. return; 
  180.  
  181. $index = $node->getAttribute("dompdf-counter"); 
  182. $text = $this->make_counter($index, $bullet_style, $pad); 
  183.  
  184. if ( trim($text) == "" ) { 
  185. return; 
  186.  
  187. $spacing = 0; 
  188. $font_family = $style->font_family; 
  189.  
  190. $line = $li->get_containing_line(); 
  191. list($x, $y) = array($frame->get_position("x"), $line->y); 
  192.  
  193. $x -= Font_Metrics::get_text_width($text, $font_family, $font_size, $spacing); 
  194.  
  195. // Take line-height into account 
  196. $line_height = $style->line_height; 
  197. $y += ($line_height - $font_size) / 4; // FIXME I thought it should be 2, but 4 gives better results 
  198.  
  199. $this->_canvas->text($x, $y, $text,  
  200. $font_family, $font_size,  
  201. $style->color, $spacing); 
  202.  
  203. case "none": 
  204. break;