PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/demo/scalr_newui/app/src/Lib/Graphics/JPGraph/jpgraph_ttf.inc.php

https://github.com/kennethjiang/Wolke
PHP | 339 lines | 256 code | 34 blank | 49 comment | 49 complexity | 2dc4f43c2f6fb28c5e9b8a859207f272 MD5 | raw file
  1. <?php
  2. //=======================================================================
  3. // File: jpgraph_ttf.inc.php
  4. // Description: Handling of TTF fonts
  5. // Created: 2006-11-19
  6. // Ver: $Id: jpgraph_ttf.inc.php 805 2006-11-28 07:45:54Z ljp $
  7. //
  8. // Copyright (c) Aditus Consulting. All rights reserved.
  9. //========================================================================
  10. // TTF Font families
  11. DEFINE("FF_COURIER",10);
  12. DEFINE("FF_VERDANA",11);
  13. DEFINE("FF_TIMES",12);
  14. DEFINE("FF_COMIC",14);
  15. DEFINE("FF_ARIAL",15);
  16. DEFINE("FF_GEORGIA",16);
  17. DEFINE("FF_TREBUCHE",17);
  18. // Gnome Vera font
  19. // Available from http://www.gnome.org/fonts/
  20. DEFINE("FF_VERA",18);
  21. DEFINE("FF_VERAMONO",19);
  22. DEFINE("FF_VERASERIF",20);
  23. // Chinese font
  24. DEFINE("FF_SIMSUN",30);
  25. DEFINE("FF_CHINESE",31);
  26. DEFINE("FF_BIG5",31);
  27. // Japanese font
  28. DEFINE("FF_MINCHO",40);
  29. DEFINE("FF_PMINCHO",41);
  30. DEFINE("FF_GOTHIC",42);
  31. DEFINE("FF_PGOTHIC",43);
  32. // Hebrew fonts
  33. DEFINE("FF_DAVID",44);
  34. DEFINE("FF_MIRIAM",45);
  35. DEFINE("FF_AHRON",46);
  36. // Extra fonts
  37. // Download fonts from
  38. // http://www.webfontlist.com
  39. // http://www.webpagepublicity.com/free-fonts.html
  40. DEFINE("FF_SPEEDO",50); // This font is also known as Bauer (Used for gauge fascia)
  41. DEFINE("FF_DIGITAL",51); // Digital readout font
  42. DEFINE("FF_COMPUTER",52); // The classic computer font
  43. DEFINE("FF_CALCULATOR",53); // Triad font
  44. // Limits for fonts
  45. DEFINE("_FIRST_FONT",10);
  46. DEFINE("_LAST_FONT",53);
  47. // TTF Font styles
  48. DEFINE("FS_NORMAL",9001);
  49. DEFINE("FS_BOLD",9002);
  50. DEFINE("FS_ITALIC",9003);
  51. DEFINE("FS_BOLDIT",9004);
  52. DEFINE("FS_BOLDITALIC",9004);
  53. //Definitions for internal font
  54. DEFINE("FF_FONT0",1);
  55. DEFINE("FF_FONT1",2);
  56. DEFINE("FF_FONT2",4);
  57. //=================================================================
  58. // CLASS LanguageConv
  59. // Description:
  60. // Converts various character encoding into proper
  61. // UTF-8 depending on how the library have been configured and
  62. // what font family is being used
  63. //=================================================================
  64. class LanguageConv {
  65. private $g2312 = null ;
  66. function Convert($aTxt,$aFF) {
  67. if( LANGUAGE_GREEK ) {
  68. if( GREEK_FROM_WINDOWS ) {
  69. $unistring = LanguageConv::gr_win2uni($aTxt);
  70. } else {
  71. $unistring = LanguageConv::gr_iso2uni($aTxt);
  72. }
  73. return $unistring;
  74. } elseif( LANGUAGE_CYRILLIC ) {
  75. if( CYRILLIC_FROM_WINDOWS && (!defined('LANGUAGE_CHARSET') || stristr(LANGUAGE_CHARSET, 'windows-1251')) ) {
  76. $aTxt = convert_cyr_string($aTxt, "w", "k");
  77. }
  78. if( !defined('LANGUAGE_CHARSET') || stristr(LANGUAGE_CHARSET, 'koi8-r') || stristr(LANGUAGE_CHARSET, 'windows-1251')) {
  79. $isostring = convert_cyr_string($aTxt, "k", "i");
  80. $unistring = LanguageConv::iso2uni($isostring);
  81. }
  82. else {
  83. $unistring = $aTxt;
  84. }
  85. return $unistring;
  86. }
  87. elseif( $aFF === FF_SIMSUN ) {
  88. // Do Chinese conversion
  89. if( $this->g2312 == null ) {
  90. include_once 'jpgraph_gb2312.php' ;
  91. $this->g2312 = new GB2312toUTF8();
  92. }
  93. return $this->g2312->gb2utf8($aTxt);
  94. }
  95. elseif( $aFF === FF_CHINESE ) {
  96. if( !function_exists('iconv') ) {
  97. JpGraphError::RaiseL(25006);
  98. //('Usage of FF_CHINESE (FF_BIG5) font family requires that your PHP setup has the iconv() function. By default this is not compiled into PHP (needs the "--width-iconv" when configured).');
  99. }
  100. return iconv('BIG5','UTF-8',$aTxt);
  101. }
  102. elseif( ASSUME_EUCJP_ENCODING &&
  103. ($aFF == FF_MINCHO || $aFF == FF_GOTHIC || $aFF == FF_PMINCHO || $aFF == FF_PGOTHIC) ) {
  104. if( !function_exists('mb_convert_encoding') ) {
  105. JpGraphError::RaiseL(25127);
  106. }
  107. return mb_convert_encoding($aTxt, 'UTF-8','EUC-JP');
  108. }
  109. elseif( $aFF == FF_DAVID || $aFF == FF_MIRIAM || $aFF == FF_AHRON ) {
  110. return LanguageConv::heb_iso2uni($aTxt);
  111. }
  112. else
  113. return $aTxt;
  114. }
  115. // Translate iso encoding to unicode
  116. public static function iso2uni ($isoline){
  117. $uniline='';
  118. for ($i=0; $i < strlen($isoline); $i++){
  119. $thischar=substr($isoline,$i,1);
  120. $charcode=ord($thischar);
  121. $uniline.=($charcode>175) ? "&#" . (1040+($charcode-176)). ";" : $thischar;
  122. }
  123. return $uniline;
  124. }
  125. // Translate greek iso encoding to unicode
  126. public static function gr_iso2uni ($isoline) {
  127. $uniline='';
  128. for ($i=0; $i < strlen($isoline); $i++) {
  129. $thischar=substr($isoline,$i,1);
  130. $charcode=ord($thischar);
  131. $uniline.=($charcode>179 && $charcode!=183 && $charcode!=187 && $charcode!=189) ? "&#" . (900+($charcode-180)). ";" : $thischar;
  132. }
  133. return $uniline;
  134. }
  135. // Translate greek win encoding to unicode
  136. public static function gr_win2uni ($winline) {
  137. $uniline='';
  138. for ($i=0; $i < strlen($winline); $i++) {
  139. $thischar=substr($winline,$i,1);
  140. $charcode=ord($thischar);
  141. if ($charcode==161 || $charcode==162) {
  142. $uniline.="&#" . (740+$charcode). ";";
  143. }
  144. else {
  145. $uniline.=(($charcode>183 && $charcode!=187 && $charcode!=189) || $charcode==180) ? "&#" . (900+($charcode-180)). ";" : $thischar;
  146. }
  147. }
  148. return $uniline;
  149. }
  150. public static function heb_iso2uni($isoline) {
  151. $isoline = hebrev($isoline);
  152. $o = '';
  153. $n = strlen($isoline);
  154. for($i=0; $i < $n; $i++) {
  155. $c=ord( substr($isoline,$i,1) );
  156. $o .= ($c > 223) && ($c < 251) ? '&#'.(1264+$c).';' : chr($c);
  157. }
  158. return utf8_encode($o);
  159. }
  160. }
  161. //=============================================================
  162. // CLASS TTF
  163. // Description: Handle TTF font names and mapping and loading of
  164. // font files
  165. //=============================================================
  166. class TTF {
  167. private $font_files,$style_names;
  168. //---------------
  169. // CONSTRUCTOR
  170. function TTF() {
  171. // String names for font styles to be used in error messages
  172. $this->style_names=array(FS_NORMAL =>'normal',
  173. FS_BOLD =>'bold',
  174. FS_ITALIC =>'italic',
  175. FS_BOLDITALIC =>'bolditalic');
  176. // File names for available fonts
  177. $this->font_files=array(
  178. FF_COURIER => array(FS_NORMAL =>'cour.ttf',
  179. FS_BOLD =>'courbd.ttf',
  180. FS_ITALIC =>'couri.ttf',
  181. FS_BOLDITALIC =>'courbi.ttf' ),
  182. FF_GEORGIA => array(FS_NORMAL =>'georgia.ttf',
  183. FS_BOLD =>'georgiab.ttf',
  184. FS_ITALIC =>'georgiai.ttf',
  185. FS_BOLDITALIC =>'' ),
  186. FF_TREBUCHE =>array(FS_NORMAL =>'trebuc.ttf',
  187. FS_BOLD =>'trebucbd.ttf',
  188. FS_ITALIC =>'trebucit.ttf',
  189. FS_BOLDITALIC =>'trebucbi.ttf' ),
  190. FF_VERDANA => array(FS_NORMAL =>'verdana.ttf',
  191. FS_BOLD =>'verdanab.ttf',
  192. FS_ITALIC =>'verdanai.ttf',
  193. FS_BOLDITALIC =>'' ),
  194. FF_TIMES => array(FS_NORMAL =>'times.ttf',
  195. FS_BOLD =>'timesbd.ttf',
  196. FS_ITALIC =>'timesi.ttf',
  197. FS_BOLDITALIC =>'timesbi.ttf' ),
  198. FF_COMIC => array(FS_NORMAL =>'comic.ttf',
  199. FS_BOLD =>'comicbd.ttf',
  200. FS_ITALIC =>'',
  201. FS_BOLDITALIC =>'' ),
  202. FF_ARIAL => array(FS_NORMAL =>'arial.ttf',
  203. FS_BOLD =>'arialbd.ttf',
  204. FS_ITALIC =>'ariali.ttf',
  205. FS_BOLDITALIC =>'arialbi.ttf' ) ,
  206. FF_VERA => array(FS_NORMAL =>'Vera.ttf',
  207. FS_BOLD =>'VeraBd.ttf',
  208. FS_ITALIC =>'VeraIt.ttf',
  209. FS_BOLDITALIC =>'VeraBI.ttf' ),
  210. FF_VERAMONO => array(FS_NORMAL =>'VeraMono.ttf',
  211. FS_BOLD =>'VeraMoBd.ttf',
  212. FS_ITALIC =>'VeraMoIt.ttf',
  213. FS_BOLDITALIC =>'VeraMoBI.ttf' ),
  214. FF_VERASERIF=> array(FS_NORMAL =>'VeraSe.ttf',
  215. FS_BOLD =>'VeraSeBd.ttf',
  216. FS_ITALIC =>'',
  217. FS_BOLDITALIC =>'' ) ,
  218. /* Chinese fonts */
  219. FF_SIMSUN => array(FS_NORMAL =>'simsun.ttc',
  220. FS_BOLD =>'simhei.ttf',
  221. FS_ITALIC =>'',
  222. FS_BOLDITALIC =>'' ),
  223. FF_CHINESE => array(FS_NORMAL =>CHINESE_TTF_FONT,
  224. FS_BOLD =>'',
  225. FS_ITALIC =>'',
  226. FS_BOLDITALIC =>'' ),
  227. /* Japanese fonts */
  228. FF_MINCHO => array(FS_NORMAL =>MINCHO_TTF_FONT,
  229. FS_BOLD =>'',
  230. FS_ITALIC =>'',
  231. FS_BOLDITALIC =>'' ),
  232. FF_PMINCHO => array(FS_NORMAL =>PMINCHO_TTF_FONT,
  233. FS_BOLD =>'',
  234. FS_ITALIC =>'',
  235. FS_BOLDITALIC =>'' ),
  236. FF_GOTHIC => array(FS_NORMAL =>GOTHIC_TTF_FONT,
  237. FS_BOLD =>'',
  238. FS_ITALIC =>'',
  239. FS_BOLDITALIC =>'' ),
  240. FF_PGOTHIC => array(FS_NORMAL =>PGOTHIC_TTF_FONT,
  241. FS_BOLD =>'',
  242. FS_ITALIC =>'',
  243. FS_BOLDITALIC =>'' ),
  244. FF_MINCHO => array(FS_NORMAL =>PMINCHO_TTF_FONT,
  245. FS_BOLD =>'',
  246. FS_ITALIC =>'',
  247. FS_BOLDITALIC =>'' ),
  248. /* Hebrew fonts */
  249. FF_DAVID => array(FS_NORMAL =>'DAVIDNEW.TTF',
  250. FS_BOLD =>'',
  251. FS_ITALIC =>'',
  252. FS_BOLDITALIC =>'' ),
  253. FF_MIRIAM => array(FS_NORMAL =>'MRIAMY.TTF',
  254. FS_BOLD =>'',
  255. FS_ITALIC =>'',
  256. FS_BOLDITALIC =>'' ),
  257. FF_AHRON => array(FS_NORMAL =>'ahronbd.ttf',
  258. FS_BOLD =>'',
  259. FS_ITALIC =>'',
  260. FS_BOLDITALIC =>'' ),
  261. /* Misc fonts */
  262. FF_DIGITAL => array(FS_NORMAL =>'DIGIRU__.TTF',
  263. FS_BOLD =>'Digirtu_.ttf',
  264. FS_ITALIC =>'Digir___.ttf',
  265. FS_BOLDITALIC =>'DIGIRT__.TTF' ),
  266. FF_SPEEDO => array(FS_NORMAL =>'Speedo.ttf',
  267. FS_BOLD =>'',
  268. FS_ITALIC =>'',
  269. FS_BOLDITALIC =>'' ),
  270. FF_COMPUTER => array(FS_NORMAL =>'COMPUTER.TTF',
  271. FS_BOLD =>'',
  272. FS_ITALIC =>'',
  273. FS_BOLDITALIC =>'' ),
  274. FF_CALCULATOR => array(FS_NORMAL =>'Triad_xs.ttf',
  275. FS_BOLD =>'',
  276. FS_ITALIC =>'',
  277. FS_BOLDITALIC =>'' ),
  278. );
  279. }
  280. //---------------
  281. // PUBLIC METHODS
  282. // Create the TTF file from the font specification
  283. function File($family,$style=FS_NORMAL) {
  284. $fam = @$this->font_files[$family];
  285. if( !$fam ) {
  286. JpGraphError::RaiseL(25046,$family);//("Specified TTF font family (id=$family) is unknown or does not exist. Please note that TTF fonts are not distributed with JpGraph for copyright reasons. You can find the MS TTF WEB-fonts (arial, courier etc) for download at http://corefonts.sourceforge.net/");
  287. }
  288. $f = @$fam[$style];
  289. if( $f==='' )
  290. JpGraphError::RaiseL(25047,$this->style_names[$style],$this->font_files[$family][FS_NORMAL]);//('Style "'.$this->style_names[$style].'" is not available for font family '.$this->font_files[$family][FS_NORMAL].'.');
  291. if( !$f ) {
  292. JpGraphError::RaiseL(25048,$fam);//("Unknown font style specification [$fam].");
  293. }
  294. if ($family >= FF_MINCHO && $family <= FF_PGOTHIC) {
  295. $f = MBTTF_DIR.$f;
  296. } else {
  297. $f = TTF_DIR.$f;
  298. }
  299. if( file_exists($f) === false || is_readable($f) === false ) {
  300. JpGraphError::RaiseL(25049,$f);//("Font file \"$f\" is not readable or does not exist.");
  301. }
  302. return $f;
  303. }
  304. } // Class
  305. ?>