PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/library/ezc/Graph/src/options/font.php

https://github.com/fusenigk/mantisbt-1
PHP | 312 lines | 158 code | 20 blank | 134 comment | 18 complexity | 46053b5d4bba8748893604e1440bbbd8 MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the ezcGraphFontOption class
  4. *
  5. * @package Graph
  6. * @version 1.5
  7. * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. */
  10. /**
  11. * Class containing the options for font configuration.
  12. *
  13. * We try to fulfill two goals regarding font configuration. First, there
  14. * should be a single point to configure the fonts used for the text areas
  15. * in the chart. On the other hand, it should be possible to configure
  16. * the fonts independently for each chart element.
  17. *
  18. * The solution is that you can modify the global font configuration by
  19. * accessing $graph->options->font. This takes effect on all chart
  20. * elements unless you intentionally access the font configuration of an
  21. * individual chart element. The following example shows, how this works.
  22. *
  23. * <code>
  24. * $graph = new ezcGraphPieChart();
  25. * $graph->title = 'Access statistics';
  26. *
  27. * // Set the maximum font size to 8 for all chart elements
  28. * $graph->options->font->maxFontSize = 8;
  29. *
  30. * // Set the font size for the title independently to 14
  31. * $graph->title->font->maxFontSize = 14;
  32. *
  33. * // The following only affects all elements except the // title element,
  34. * // which now has its own font configuration.
  35. * //
  36. * // Keep in mind that the specified font is driver specific. A pure name
  37. * // works for the SVG driver, used here. The GD driver for example
  38. * // requires a path to a TTF file.
  39. * $graph->options->font->name = 'serif';
  40. *
  41. * $graph->data['Access statistics'] = new ezcGraphArrayDataSet( array(
  42. * 'Mozilla' => 19113,
  43. * 'Explorer' => 10917,
  44. * 'Opera' => 1464,
  45. * 'Safari' => 652,
  46. * 'Konqueror' => 474,
  47. * ) );
  48. * </code>
  49. *
  50. * @property string $name
  51. * Name of font.
  52. * @property string $path
  53. * Path to font file.
  54. * @property int $type
  55. * Type of used font. May be one of the following:
  56. * - TTF_FONT Native TTF fonts
  57. * - PS_FONT PostScript Type1 fonts
  58. * - FT2_FONT FreeType 2 fonts
  59. * The type is normally automatically detected when you set the path
  60. * to the font file.
  61. * @property float $minFontSize
  62. * Minimum font size for displayed texts.
  63. * @property float $maxFontSize
  64. * Maximum font size for displayed texts.
  65. * @property float $minimalUsedFont
  66. * The minimal used font size for the current element group. This
  67. * property is set by the driver to maintain this information and
  68. * should not be used to configure the apperance of the chart. See
  69. * $minFontSize instead.
  70. * @property ezcGraphColor $color
  71. * Font color.
  72. * @property ezcGraphColor $background
  73. * Background color. The actual area filled with the background color
  74. * is influenced by the settings $padding and $minimizeBorder.
  75. * @property ezcGraphColor $border
  76. * Border color for the text. The distance between the text and
  77. * border is defined by the properties $padding and $minimizeBorder.
  78. * @property int $borderWidth
  79. * With of the border. To enable the border you need to set the
  80. * $border property to some color.
  81. * @property int $padding
  82. * Padding between text and border.
  83. * @property bool $minimizeBorder
  84. * Fit the border exactly around the text, or use the complete
  85. * possible space. This setting is only relevant, when a border
  86. * color has been set for the font.
  87. * @property bool $textShadow
  88. * Draw shadow for texts. The color of the shadow is defined in
  89. * the property $textShadowColor.
  90. * @property int $textShadowOffset
  91. * Offset for text shadow. This defines the distance the shadow
  92. * is moved to the bottom left relative from the text position.
  93. * @property ezcGraphColor $textShadowColor
  94. * Color of text shadow. If left at the default value "false""
  95. * the inverse color of the text color will be used.
  96. *
  97. * @version 1.5
  98. * @package Graph
  99. */
  100. class ezcGraphFontOptions extends ezcBaseOptions
  101. {
  102. /**
  103. * Indicates if path already has been checked for correct font
  104. *
  105. * @var bool
  106. */
  107. protected $pathChecked = false;
  108. /**
  109. * Constructor
  110. *
  111. * @param array $options Default option array
  112. * @return void
  113. * @ignore
  114. */
  115. public function __construct( array $options = array() )
  116. {
  117. $this->properties['name'] = 'sans-serif';
  118. // $this->properties['path'] = 'Graph/tests/data/font.ttf';
  119. $this->properties['path'] = '';
  120. $this->properties['type'] = ezcGraph::TTF_FONT;
  121. $this->properties['minFontSize'] = 6;
  122. $this->properties['maxFontSize'] = 96;
  123. $this->properties['minimalUsedFont'] = 96;
  124. $this->properties['color'] = ezcGraphColor::fromHex( '#000000' );
  125. $this->properties['background'] = false;
  126. $this->properties['border'] = false;
  127. $this->properties['borderWidth'] = 1;
  128. $this->properties['padding'] = 0;
  129. $this->properties['minimizeBorder'] = true;
  130. $this->properties['textShadow'] = false;
  131. $this->properties['textShadowOffset'] = 1;
  132. $this->properties['textShadowColor'] = false;
  133. parent::__construct( $options );
  134. }
  135. /**
  136. * Set an option value
  137. *
  138. * @param string $propertyName
  139. * @param mixed $propertyValue
  140. * @throws ezcBasePropertyNotFoundException
  141. * If a property is not defined in this class
  142. * @return void
  143. */
  144. public function __set( $propertyName, $propertyValue )
  145. {
  146. switch ( $propertyName )
  147. {
  148. case 'minFontSize':
  149. if ( !is_numeric( $propertyValue ) ||
  150. ( $propertyValue < 1 ) )
  151. {
  152. throw new ezcBaseValueException( $propertyName, $propertyValue, 'float > 1' );
  153. }
  154. // Ensure min font size is smaller or equal max font size.
  155. if ( $propertyValue > $this->properties['maxFontSize'] )
  156. {
  157. throw new ezcBaseValueException( $propertyName, $propertyValue, 'float <= ' . $this->properties['maxFontSize'] );
  158. }
  159. $this->properties[$propertyName] = (float) $propertyValue;
  160. break;
  161. case 'maxFontSize':
  162. if ( !is_numeric( $propertyValue ) ||
  163. ( $propertyValue < 1 ) )
  164. {
  165. throw new ezcBaseValueException( $propertyName, $propertyValue, 'float > 1' );
  166. }
  167. // Ensure max font size is greater or equal min font size.
  168. if ( $propertyValue < $this->properties['minFontSize'] )
  169. {
  170. throw new ezcBaseValueException( $propertyName, $propertyValue, 'float >= ' . $this->properties['minFontSize'] );
  171. }
  172. $this->properties[$propertyName] = (float) $propertyValue;
  173. break;
  174. case 'minimalUsedFont':
  175. $propertyValue = (float) $propertyValue;
  176. if ( $propertyValue < $this->minimalUsedFont )
  177. {
  178. $this->properties['minimalUsedFont'] = $propertyValue;
  179. }
  180. break;
  181. case 'color':
  182. case 'background':
  183. case 'border':
  184. case 'textShadowColor':
  185. $this->properties[$propertyName] = ezcGraphColor::create( $propertyValue );
  186. break;
  187. case 'borderWidth':
  188. case 'padding':
  189. case 'textShadowOffset':
  190. if ( !is_numeric( $propertyValue ) ||
  191. ( $propertyValue < 0 ) )
  192. {
  193. throw new ezcBaseValueException( $propertyName, $propertyValue, 'int >= 0' );
  194. }
  195. $this->properties[$propertyName] = (int) $propertyValue;
  196. break;
  197. case 'minimizeBorder':
  198. case 'textShadow':
  199. if ( !is_bool( $propertyValue ) )
  200. {
  201. throw new ezcBaseValueException( $propertyName, $propertyValue, 'bool' );
  202. }
  203. $this->properties[$propertyName] = (bool) $propertyValue;
  204. break;
  205. case 'name':
  206. if ( is_string( $propertyValue ) )
  207. {
  208. $this->properties['name'] = $propertyValue;
  209. }
  210. else
  211. {
  212. throw new ezcBaseValueException( $propertyName, $propertyValue, 'string' );
  213. }
  214. break;
  215. case 'path':
  216. if ( is_file( $propertyValue ) && is_readable( $propertyValue ) )
  217. {
  218. $this->properties['path'] = $propertyValue;
  219. $parts = pathinfo( $this->properties['path'] );
  220. switch ( strtolower( $parts['extension'] ) )
  221. {
  222. case 'fdb':
  223. $this->properties['type'] = ezcGraph::PALM_FONT;
  224. break;
  225. case 'pfb':
  226. $this->properties['type'] = ezcGraph::PS_FONT;
  227. break;
  228. case 'ttf':
  229. $this->properties['type'] = ezcGraph::TTF_FONT;
  230. break;
  231. case 'svg':
  232. $this->properties['type'] = ezcGraph::SVG_FONT;
  233. $this->properties['name'] = ezcGraphSvgFont::getFontName( $propertyValue );
  234. break;
  235. default:
  236. throw new ezcGraphUnknownFontTypeException( $propertyValue, $parts['extension'] );
  237. }
  238. $this->pathChecked = true;
  239. }
  240. else
  241. {
  242. throw new ezcBaseFileNotFoundException( $propertyValue, 'font' );
  243. }
  244. break;
  245. case 'type':
  246. if ( is_int( $propertyValue ) )
  247. {
  248. $this->properties['type'] = $propertyValue;
  249. }
  250. else
  251. {
  252. throw new ezcBaseValueException( $propertyName, $propertyValue, 'int' );
  253. }
  254. break;
  255. default:
  256. throw new ezcBasePropertyNotFoundException( $propertyName );
  257. break;
  258. }
  259. }
  260. /**
  261. * __get
  262. *
  263. * @param mixed $propertyName
  264. * @throws ezcBasePropertyNotFoundException
  265. * If a the value for the property options is not an instance of
  266. * @return mixed
  267. * @ignore
  268. */
  269. public function __get( $propertyName )
  270. {
  271. switch ( $propertyName )
  272. {
  273. case 'textShadowColor':
  274. // Use inverted font color if false
  275. if ( $this->properties['textShadowColor'] === false )
  276. {
  277. $this->properties['textShadowColor'] = $this->properties['color']->invert();
  278. }
  279. return $this->properties['textShadowColor'];
  280. case 'path':
  281. if ( $this->pathChecked === false )
  282. {
  283. // Enforce call of path check
  284. $this->__set( 'path', $this->properties['path'] );
  285. }
  286. // No break to use parent return
  287. default:
  288. return parent::__get( $propertyName );
  289. }
  290. }
  291. }
  292. ?>