PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ezimage/classes/ezimagefont.php

https://github.com/Yannix/ezpublish
PHP | 230 lines | 107 code | 22 blank | 101 comment | 6 complexity | 0f76c10dc8340e486c930cf56dcc7bdb MD5 | raw file
  1. <?php
  2. /**
  3. * File containing the eZImageFont class.
  4. *
  5. * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
  6. * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
  7. * @version //autogentag//
  8. * @package lib
  9. */
  10. /*!
  11. \class eZImageFont ezimagefont.php
  12. \ingroup eZImageObject
  13. \brief Specifies a font used for drawing text
  14. Font attributes are encapsulated for use with the eZImageInterface::drawText function.
  15. The class stores the family, pointsize and path. Alternatively an x and y adjustment may
  16. be specified incase the font rendering is wrong.
  17. Typical usage:
  18. \code
  19. if ( eZImageFont::exists( 'arial', 'design/standard/fonts' ) )
  20. $font = new eZImageFont( 'arial', 30, 'design/standard/fonts' );
  21. \endcode
  22. All attributes can be modified later on with setFamily, setPath, setPointSize, setXAdjustment and setYAdjustment.
  23. */
  24. class eZImageFont
  25. {
  26. /*!
  27. Initializes the object with a family, point size and path.
  28. X and y adjustment may also be specified.
  29. */
  30. function eZImageFont( $family, $size, $path,
  31. $xAdjustment = 0, $yAdjustment = 0 )
  32. {
  33. $this->FontFamily = $family;
  34. $this->FontPath = $path;
  35. $this->PointSize = $size;
  36. $this->XAdjustment = $xAdjustment;
  37. $this->YAdjustment = $yAdjustment;
  38. $this->initialize();
  39. }
  40. /*!
  41. \return the font family, eg. arial, times
  42. */
  43. function family()
  44. {
  45. return $this->FontFamily;
  46. }
  47. /*!
  48. \return the path to font files, it may be a string or an array of strings.
  49. */
  50. function path()
  51. {
  52. return $this->FontPath;
  53. }
  54. /*!
  55. \return the font file if it has been initialized.
  56. \sa realFile, fontFile, initialize.
  57. */
  58. function file()
  59. {
  60. return $this->FontFile;
  61. }
  62. /*!
  63. Similar to file but returns the absolute path to the font file.
  64. This is required for GD font handling.
  65. */
  66. function realFile()
  67. {
  68. return realpath( "." ) . "/" . $this->FontFile;
  69. }
  70. /*!
  71. \return the point size of the font.
  72. */
  73. function pointSize()
  74. {
  75. return $this->PointSize;
  76. }
  77. /*!
  78. \return the number of pixels in the x direction to adjust the font output.
  79. \sa yAdjustment
  80. */
  81. function xAdjustment()
  82. {
  83. return $this->XAdjustment;
  84. }
  85. /*!
  86. \return the number of pixels in the y direction to adjust the font output.
  87. \sa xAdjustment
  88. */
  89. function yAdjustment()
  90. {
  91. return $this->YAdjustment;
  92. }
  93. /*!
  94. Sets the font family to \a $family.
  95. \note Changing the font family will reinitialize the font.
  96. */
  97. function setFamily( $family )
  98. {
  99. $this->FontFamily = $family;
  100. $this->initialize();
  101. }
  102. /*!
  103. Sets the font path which is used when searching for fonts, the path can either be
  104. a string or an array of strings.
  105. \note Changing the font path will reinitialize the font.
  106. */
  107. function setPath( $path )
  108. {
  109. $this->FontPath = $path;
  110. $this->initialize();
  111. }
  112. /*!
  113. Sets the point size of the font to \a $size.
  114. */
  115. function setPointSize( $size )
  116. {
  117. $this->PointSize = $size;
  118. }
  119. /*!
  120. Sets the number of pixels in the x direction to adjust the font output to \a $adjustment.
  121. \sa setYAdjustment
  122. */
  123. function setXAdjustment( $adjustment )
  124. {
  125. $this->XAdjustment = $adjustment;
  126. }
  127. /*!
  128. Sets the number of pixels in the y direction to adjust the font output to \a $adjustment.
  129. \sa setXAdjustment
  130. */
  131. function setYAdjustment( $adjustment )
  132. {
  133. $this->YAdjustment = $adjustment;
  134. }
  135. /*!
  136. Sets the number of pixels to adjust the font output to \a $xAdjustment and \a $yAdjustment.
  137. \sa setXAdjustment, setYAdjustment
  138. */
  139. function setAdjustment( $xAdjustment, $yAdjustment )
  140. {
  141. $this->XAdjustment = $xAdjustment;
  142. $this->YAdjustment = $yAdjustment;
  143. }
  144. /*!
  145. \static
  146. \return true if the font family \a $fontFamily exists in the path \a $fontPath.
  147. The path can be specified as a string or an array of strings.
  148. */
  149. static function exists( $fontFamily, $fontPath )
  150. {
  151. return eZImageFont::fontFile( $fontFamily, $fontPath ) != false;
  152. }
  153. /*!
  154. \private
  155. Initializes the font file attribute by searching for the font.
  156. */
  157. function initialize()
  158. {
  159. $this->FontFile = eZImageFont::fontFile( $this->FontFamily, $this->FontPath );
  160. }
  161. /*!
  162. \static
  163. \return the file path for the font if it is found or \c false if no font could be used.
  164. The font must be named equal to the \a $fontFamily parameter
  165. with the .ttf suffix, eg. arial.ttf.
  166. \param fontPath The path to the fonts or an array of paths.
  167. */
  168. static function fontFile( $fontFamily, $fontPath )
  169. {
  170. if ( preg_match( '/\.ttf$/i', $fontFamily ) )
  171. $family_file = $fontFamily;
  172. else
  173. $family_file = $fontFamily . '.ttf';
  174. if ( $fontPath != null )
  175. {
  176. if ( !is_array( $fontPath ) )
  177. $fontPath = array( $fontPath );
  178. foreach ( $fontPath as $singleFontPath )
  179. {
  180. $font = $singleFontPath . "/$family_file";
  181. if ( !file_exists( $font ) )
  182. $font = false;
  183. else
  184. return $font;
  185. }
  186. }
  187. else
  188. $font = $fontFamily;
  189. return $font;
  190. }
  191. /// \privatesection
  192. /// The current font family
  193. public $FontFamily;
  194. /// The path or path array to the fonts
  195. public $FontPath;
  196. /// The path to the font file one was found
  197. public $FontFile;
  198. /// The size of the font in points.
  199. public $PointSize;
  200. /// Adjustment in the x direction
  201. public $XAdjustment;
  202. /// Adjustment in the y direction
  203. public $YAdjustment;
  204. }
  205. ?>