2
0

Style.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Pdf
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** Zend_Pdf_Color */
  22. require_once 'Zend/Pdf/Color.php';
  23. /** Zend_Pdf_Element_Numeric */
  24. require_once 'Zend/Pdf/Element/Numeric.php';
  25. /** Zend_Pdf_Element_Array */
  26. require_once 'Zend/Pdf/Element/Array.php';
  27. /** Zend_Pdf_Resource_Font */
  28. require_once 'Zend/Pdf/Resource/Font.php';
  29. /**
  30. * Style object.
  31. * Style object doesn't directly correspond to any PDF file object.
  32. * It's utility class, used as a container for style information.
  33. * It's used by Zend_Pdf_Page class in draw operations.
  34. *
  35. * @package Zend_Pdf
  36. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Pdf_Style
  40. {
  41. /**
  42. * Fill color.
  43. * Used to fill geometric shapes or text.
  44. *
  45. * @var Zend_Pdf_Color|null
  46. */
  47. private $_fillColor = null;
  48. /**
  49. * Line color.
  50. * Current color, used for lines and font outlines.
  51. *
  52. * @var Zend_Pdf_Color|null
  53. */
  54. private $_color;
  55. /**
  56. * Line width.
  57. *
  58. * @var Zend_Pdf_Element_Numeric
  59. */
  60. private $_lineWidth;
  61. /**
  62. * Array which describes line dashing pattern.
  63. * It's array of numeric:
  64. * array($on_length, $off_length, $on_length, $off_length, ...)
  65. *
  66. * @var array
  67. */
  68. private $_lineDashingPattern;
  69. /**
  70. * Line dashing phase
  71. *
  72. * @var float
  73. */
  74. private $_lineDashingPhase;
  75. /**
  76. * Current font
  77. *
  78. * @var Zend_Pdf_Resource_Font
  79. */
  80. private $_font;
  81. /**
  82. * Font size
  83. *
  84. * @var float
  85. */
  86. private $_fontSize;
  87. /**
  88. * Create style.
  89. *
  90. * @param Zend_Pdf_Style $anotherStyle
  91. */
  92. public function __construct($anotherStyle = null)
  93. {
  94. if ($anotherStyle !== null) {
  95. $this->_fillColor = $anotherStyle->_fillColor;
  96. $this->_color = $anotherStyle->_color;
  97. $this->_lineWidth = $anotherStyle->_lineWidth;
  98. $this->_lineDashingPattern = $anotherStyle->_lineDashingPattern;
  99. $this->_lineDashingPhase = $anotherStyle->_lineDashingPhase;
  100. $this->_font = $anotherStyle->_font;
  101. $this->_fontSize = $anotherStyle->_fontSize;
  102. }
  103. }
  104. /**
  105. * Set fill color.
  106. *
  107. * @param Zend_Pdf_Color $color
  108. */
  109. public function setFillColor(Zend_Pdf_Color $color)
  110. {
  111. $this->_fillColor = $color;
  112. }
  113. /**
  114. * Set line color.
  115. *
  116. * @param Zend_Pdf_Color $color
  117. */
  118. public function setLineColor(Zend_Pdf_Color $color)
  119. {
  120. $this->_color = $color;
  121. }
  122. /**
  123. * Set line width.
  124. *
  125. * @param float $width
  126. */
  127. public function setLineWidth($width)
  128. {
  129. $this->_lineWidth = new Zend_Pdf_Element_Numeric($width);
  130. }
  131. /**
  132. * Set line dashing pattern
  133. *
  134. * @param array $pattern
  135. * @param float $phase
  136. */
  137. public function setLineDashingPattern($pattern, $phase = 0)
  138. {
  139. if ($pattern === Zend_Pdf_Page::LINE_DASHING_SOLID) {
  140. $pattern = array();
  141. $phase = 0;
  142. }
  143. $this->_lineDashingPattern = $pattern;
  144. $this->_lineDashingPhase = new Zend_Pdf_Element_Numeric($phase);
  145. }
  146. /**
  147. * Set current font.
  148. *
  149. * @param Zend_Pdf_Resource_Font $font
  150. * @param float $fontSize
  151. */
  152. public function setFont(Zend_Pdf_Resource_Font $font, $fontSize)
  153. {
  154. $this->_font = $font;
  155. $this->_fontSize = $fontSize;
  156. }
  157. /**
  158. * Modify current font size
  159. *
  160. * @param float $fontSize
  161. */
  162. public function setFontSize($fontSize)
  163. {
  164. $this->_fontSize = $fontSize;
  165. }
  166. /**
  167. * Get fill color.
  168. *
  169. * @return Zend_Pdf_Color|null
  170. */
  171. public function getFillColor()
  172. {
  173. return $this->_fillColor;
  174. }
  175. /**
  176. * Get line color.
  177. *
  178. * @return Zend_Pdf_Color|null
  179. */
  180. public function getLineColor()
  181. {
  182. return $this->_color;
  183. }
  184. /**
  185. * Get line width.
  186. *
  187. * @return float
  188. */
  189. public function getLineWidth()
  190. {
  191. return $this->_lineWidth->value;
  192. }
  193. /**
  194. * Get line dashing pattern
  195. *
  196. * @return array
  197. */
  198. public function getLineDashingPattern()
  199. {
  200. return $this->_lineDashingPattern;
  201. }
  202. /**
  203. * Get current font.
  204. *
  205. * @return Zend_Pdf_Resource_Font $font
  206. */
  207. public function getFont()
  208. {
  209. return $this->_font;
  210. }
  211. /**
  212. * Get current font size
  213. *
  214. * @return float $fontSize
  215. */
  216. public function getFontSize()
  217. {
  218. return $this->_fontSize;
  219. }
  220. /**
  221. * Get line dashing phase
  222. *
  223. * @return float
  224. */
  225. public function getLineDashingPhase()
  226. {
  227. return $this->_lineDashingPhase->value;
  228. }
  229. /**
  230. * Dump style to a string, which can be directly inserted into content stream
  231. *
  232. * @return string
  233. */
  234. public function instructions()
  235. {
  236. $instructions = '';
  237. if ($this->_fillColor !== null) {
  238. $instructions .= $this->_fillColor->instructions(false);
  239. }
  240. if ($this->_color !== null) {
  241. $instructions .= $this->_color->instructions(true);
  242. }
  243. if ($this->_lineWidth !== null) {
  244. $instructions .= $this->_lineWidth->toString() . " w\n";
  245. }
  246. if ($this->_lineDashingPattern !== null) {
  247. $dashPattern = new Zend_Pdf_Element_Array();
  248. foreach ($this->_lineDashingPattern as $dashItem) {
  249. $dashElement = new Zend_Pdf_Element_Numeric($dashItem);
  250. $dashPattern->items[] = $dashElement;
  251. }
  252. $instructions .= $dashPattern->toString() . ' '
  253. . $this->_lineDashingPhase->toString() . " d\n";
  254. }
  255. return $instructions;
  256. }
  257. }