Style.php 6.5 KB

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