You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

294 lines
6.5 KiB

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