PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/zeta/Graph/src/options/font.php

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