Initial upload to TYPO3 Forge
[TYPO3CMS/Extensions/sr_freecap.git] / mod1 / class.tx_srfreecap_fontmaker.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2007-2008 Stanislas Rolland <typo3(arobas)sjbr.ca>
6 * All rights reserved
7 *
8 * This script is part of the Typo3 project. The Typo3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24 /**
25 * Module 'GD Font Makers' for the 'sr_freecap' extension.
26 *
27 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
28 */
29 if (!defined('PATH_tslib')) {
30 if (@is_dir(PATH_site.'typo3/sysext/cms/tslib/')) {
31 define('PATH_tslib', PATH_site.'typo3/sysext/cms/tslib/');
32 } elseif (@is_dir(PATH_site.'tslib/')) {
33 define('PATH_tslib', PATH_site.'tslib/');
34 }
35 }
36 require_once(PATH_t3lib.'class.t3lib_scbase.php');
37 require_once(PATH_t3lib.'class.t3lib_tstemplate.php');
38 require_once(PATH_t3lib.'class.t3lib_stdgraphic.php');
39 require_once(PATH_t3lib.'class.t3lib_div.php');
40 require_once(PATH_tslib.'class.tslib_gifbuilder.php');
41 require_once(PATH_tslib.'class.tslib_content.php');
42
43 class tx_srfreecap_fontmaker extends t3lib_SCbase {
44 var $extKey = 'sr_freecap';
45 var $extPrefix = 'tx_srfreecap';
46 var $pageinfo;
47
48 /**
49 * Initialize module
50 *
51 * @return void
52 */
53 function init() {
54 parent::init();
55 }
56
57 /**
58 * Adds items to the->MOD_MENU array. Used for the function menu selector.
59 *
60 * @return void
61 */
62 function menuConfig() {
63 global $LANG;
64 $this->MOD_MENU = Array (
65 'function' => Array (
66 '1' => $LANG->getLL('function1'),
67 '2' => $LANG->getLL('function2'),
68 '3' => $LANG->getLL('function3'),
69 )
70 );
71 parent::menuConfig();
72 }
73
74 /**
75 * Main function of the module. Write the content to $this->content
76 *
77 * @return void
78 */
79 function main() {
80 global $BE_USER, $LANG, $BACK_PATH, $TYPO3_CONF_VARS;
81
82 // Access check!
83 // The page will show only if there is a valid page and if this page may be viewed by the user
84 $this->pageinfo = t3lib_BEfunc::readPageAccess($this->id, $this->perms_clause);
85 $access = is_array($this->pageinfo) ? 1 : 0;
86
87 if (($this->id && $access) || ($BE_USER->user['admin'] && !$this->id)) {
88
89 // Draw the header.
90 $this->doc = t3lib_div::makeInstance('mediumDoc');
91 $this->doc->backPath = $BACK_PATH;
92 $this->doc->form = '<form action="" method="post" enctype="multipart/form-data">';
93
94 // JavaScript
95 $this->doc->JScode = '
96 <script type="text/javascript">
97 /*<![CDATA[*/
98 <!--
99 script_ended = 0;
100 function jumpToUrl(URL) {
101 document.location = URL;
102 }
103 // -->
104 /*]]>*/
105 </script>
106 ';
107
108 //$headerSection = $this->doc->getHeader('pages', $this->pageinfo, $this->pageinfo['_thePath']).'<br>'.$LANG->php3Lang['labels']['path'].': '.t3lib_div::fixed_lgd_pre($this->pageinfo['_thePath'], 50);
109
110 $this->content .= $this->doc->startPage($LANG->getLL('title'));
111 $this->content .= $this->doc->header($LANG->getLL('title'));
112 $this->content .= $this->doc->spacer(5);
113 //$this->content .= $this->doc->section('', $this->doc->funcMenu($headerSection, t3lib_BEfunc::getFuncMenu($this->id, 'SET[function]', $this->MOD_SETTINGS['function'], $this->MOD_MENU['function'])));
114 $this->content .= $this->doc->divider(5);
115
116 // Render content:
117 $this->moduleContent();
118
119 // ShortCut
120 if ($BE_USER->mayMakeShortcut()) {
121 $this->content .= $this->doc->spacer(20).$this->doc->section('', $this->doc->makeShortcutIcon('id', implode(',', array_keys($this->MOD_MENU)), $this->MCONF['name']));
122 }
123
124 $this->content .= $this->doc->spacer(10);
125 } else {
126 // If no access or if ID == zero
127 $this->doc = t3lib_div::makeInstance('mediumDoc');
128 $this->doc->backPath = $BACK_PATH;
129
130 $this->content .= $this->doc->startPage($LANG->getLL('title'));
131 $this->content .= $this->doc->header($LANG->getLL('title'));
132 $this->content .= $this->doc->spacer(5);
133 $this->content .= $this->doc->spacer(10);
134 }
135 }
136
137 /**
138 * Prints out the module HTML
139 *
140 * @return void
141 */
142 function printContent() {
143 global $SOBE;
144
145 $this->content .= $this->doc->middle();
146 $this->content .= $this->doc->endPage();
147 echo $this->content;
148 }
149
150 /**
151 * Generates the module content
152 *
153 * @return void
154 */
155 function moduleContent() {
156 global $LANG, $TYPO3_DB, $BACK_PATH, $TYPO3_CONF_VARS;
157
158 // get user supplied data
159 $charactersToIncludeInFont = intval(t3lib_div::_GP('charactersToIncludeInFont'));
160
161 $pixelwidth = intval(t3lib_div::_GP('pixelwidth'));
162 $pixelheight = intval(t3lib_div::_GP('pixelheight'));
163 if (!$pixelheight) $pixelheight = 50;
164
165 $endianness = intval(t3lib_div::_GP('endianness'));
166
167 $gdFontFileName = t3lib_div::_GP('gdfontfilename');
168 if (!trim($gdFontFileName)) {
169 $gdFontFileName = 'font';
170 }
171
172 $fontFileName = t3lib_div::_GP('fontfilename');
173 if (!trim($fontFileName)) {
174 $fontFileName = 't3lib/fonts/nimbus.ttf';
175 }
176 $ttfFontFileName = t3lib_div::getFileAbsFileName($fontFileName);
177
178 if (!is_file($ttfFontFileName)) {
179 $this->content .= $LANG->getLL('ttfFontFileNotFound') . ' '. $fontFileName;
180 } elseif (!empty($pixelwidth)) {
181 if ($charactersToIncludeInFont == 1) {
182 $characters = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z';
183 $numchars = 26;
184 $startCharacter = 'a';
185 } else if ($charactersToIncludeInFont == 2) {
186 $characters = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o'
187 .',p,q,r,s,t,u,v,w,x,y,z,-,-,-,-,-'
188 .',-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-'
189 .',-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-'
190 .',-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-'
191 .',-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-'
192 .',-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,-'
193 .',-,-,-,-,-,-,-,-,-,-,-,-,-,-,-,ß'
194 .',à,á,â,ã,ä,å,æ,ç,è,é,ê,ë,ì,í,î,ï'
195 .',ð,ñ,ò,ó,ô,õ,ö,-,ø,ù,ú,û,ü,ý,þ,ÿ';
196 $numchars = 159;
197 $startCharacter = 'a';
198 } else {
199 $characters = '0,1,2,3,4,5,6,7,8,9';
200 $numchars = 10;
201 $startCharacter = '0';
202 }
203
204 $PNGImageFile = $this->makeFontImage($characters, $ttfFontFileName, $pixelwidth, $pixelheight);
205 $this->content .= $LANG->getLL('usingFontFile') . ' ' . $fontFileName . $this->doc->spacer(5);
206 $this->content .= $LANG->getLL('pngImageCreated') . ' ' . $PNGImageFile . $this->doc->spacer(5) . '<img src="' . $BACK_PATH . '../' . $PNGImageFile . '" />' . $this->doc->spacer(20);
207
208 if ($TYPO3_CONF_VARS['GFX']['gdlib_png']) {
209 $image = @ImageCreateFromPNG(PATH_site.$PNGImageFile);
210 } else {
211 $image = @ImageCreateFromGIF(PATH_site.$PNGImageFile);
212 }
213 $fontdata = $this->makeFont($image, $numchars, $startCharacter, $pixelwidth, $pixelheight, $endianness);
214 $gdfFontFileName = $this->buildFontFile($gdFontFileName,$fontdata);
215 ImageDestroy($image);
216
217 if ($gdfFontFileName) {
218 $this->content .= $LANG->getLL('gdFontFileCreated') . ' ' . $gdfFontFileName;
219 } else {
220 $this->content .= $LANG->getLL('gdFontFileNotCreated') . ' ' . $gdfFontFileName;
221 }
222 }
223 $this->content .= $this->doc->spacer(20);
224 $this->content .= '
225 <table cellspacing="5">
226 <tr><td>' . $LANG->getLL('charactersToIncludeInFont') . '</td><td>
227 <input id="numbers-only" type="radio" name="charactersToIncludeInFont" value="0" checked="checked" style="margin-right: 3px;" /><label for="numbers-only">' . $LANG->getLL('numbers-only') . '</label>
228 <br /><input id="ASCII-lowercase-letters" type="radio" name="charactersToIncludeInFont" value="1" style="margin-right: 3px;" /><label for="ASCII-lowercase-letters">' . $LANG->getLL('ASCII-lowercase-letters') . '</label>
229 <br /><input id="ANSI-extended-ASCII-lowercase-letters" type="radio" name="charactersToIncludeInFont" value="2" style="margin-right: 3px;" /><label for="ANSI-extended-ASCII-lowercase-letters">' . $LANG->getLL('ANSI-extended-ASCII-lowercase-letters') . '</label>
230 </td></tr>
231 <tr><td><label for="pixelwidth">' . $LANG->getLL('characterWidth') . '</label></td><td><input id="pixelwidth" type="text" name="pixelwidth" size="5" /></td></tr>
232 <tr><td><label for="pixelheight">' . $LANG->getLL('characterHeight') . '</label></td><td><input id="pixelheight" type="text" name="pixelheight" size="5" /></td></tr>
233 <tr><td>' . $LANG->getLL('endianness') . '</td><td>
234 <input id="little-endian" type="radio" name="endianness" value="0" checked="checked" style="margin-right: 3px;" /><label for="little-endian">' . $LANG->getLL('littleEndian') . '</label>
235 <br /><input id="big-endian" type="radio" name="endianness" value="1" style="margin-right: 3px;" /><label for="big-endian">' . $LANG->getLL('bigEndian') . '</label>
236 </td></tr>
237 <tr><td><label for="fontfilename">' . $LANG->getLL('pathToTTFFile') . '</td><td><input id="fontfilename" type="text" name="fontfilename" size="50" /></td></tr>
238 <tr><td><label for="gdfontfilename">' . $LANG->getLL('gdFontFilePrefix') . '</td><td><input id="gdfontfilename" type="text" name="gdfontfilename" size="25"></td></tr>
239 <tr><td colspan="2"><input type="submit" value="' . htmlspecialchars($LANG->getLL('makeFont')) . '" /></td></tr>
240 </table>
241 ';
242 $this->content .= $this->doc->section('', $content, 0, 1);
243 }
244
245 /**
246 * Return a file name built with the label and containing the specified contents
247 *
248 * @return string filename
249 */
250
251 function buildFontFile($label,$contents,$ext='gdf') {
252 $relFilename = 'uploads/' . $this->extPrefix . '/' . $label . '_' . t3lib_div::shortMD5($contents) . '.' . $ext;
253 $absFilename = PATH_site . $relFilename;
254 if (t3lib_div::writeFile($absFilename,$contents)) {
255 return $relFilename;
256 } else {
257 return FALSE;
258 }
259 }
260
261 /**
262 * Outputs an image of a specified background color displaying a list of characters with specified font size, file and color
263 *
264 * @param string $text: text to display on he image
265 * @param integer $size: specified font size
266 * @param string $font: specified font file name
267 * @param string $color: specified font color
268 * @param string $bgColor: specified background color
269 * @param string $align: left or right alignment of text
270 * @param string $width: width of each character
271 * @param string $height: height of the image
272 * @return array image file info array
273 */
274 function makeFontImage($characters, $font, $width = 35, $height = 50) {
275
276 $size = intval($height*.8);
277 $vertOffset = intval($height*.7);
278 $color = '#000000';
279 $bgColor = 'white';
280 $align = 'left';
281
282 $charactersArray = explode(',', $characters);
283 $charactersCount = count($charactersArray);
284
285 $gifObjArray = array();
286 $gifObjArray['backColor'] = $bgColor;
287 $gifObjArray['transparentBackground'] = 0;
288 $gifObjArray['reduceColors'] = '';
289 $gifObjArray['maxWidth'] = ($charactersCount*$width)+1;
290 $gifObjArray['XY'] = ($charactersCount*$width) .','.$height;
291
292 for ($ic = 1; $ic < $charactersCount+1; $ic++) {
293 $gifObjArray[$ic.'0'] = 'TEXT';
294 $gifObjArray[$ic.'0.']['text'] = $charactersArray[$ic-1];
295
296 $bbox = imagettfbbox($size, 0, $font, $charactersArray[$ic-1]);
297 $hOffset = intval(($width - ($bbox[4] - $bbox[6]))/2);
298 $vOffset = intval(($width - ($bbox[7] - $bbox[1]))/2);
299
300 $gifObjArray[$ic.'0.']['niceText'] = 0;
301 $gifObjArray[$ic.'0.']['antiAlias'] = 1;
302 $gifObjArray[$ic.'0.']['align'] = $align;
303 $gifObjArray[$ic.'0.']['fontSize'] = $size;
304 $gifObjArray[$ic.'0.']['fontFile'] = $font;
305 $gifObjArray[$ic.'0.']['fontColor'] = $color;
306 $gifObjArray[$ic.'0.']['maxWidth'] = $width;
307 $gifObjArray[$ic.'0.']['offset'] = (($ic-1)*$width+$hOffset).','.$vertOffset;
308 }
309
310 $gifCreator = t3lib_div::makeInstance('tx_srfreecap_gifbuilder');
311 $theImage='';
312 $gifCreator->init();
313 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
314 $gifCreator->start($gifObjArray,array());
315 return $gifCreator->gifBuild();
316 } else {
317 return FALSE;
318 }
319 }
320
321 /************************************************************\
322 *
323 * GD Fontmaker Copyright 2005 Howard Yeend
324 * www.puremango.co.uk
325 *
326 * This file is part of GD Fontmaker.
327 *
328 * GD Fontmaker is free software; you can redistribute it and/or modify
329 * it under the terms of the GNU General Public License as published by
330 * the Free Software Foundation; either version 2 of the License, or
331 * (at your option) any later version.
332 *
333 * GD Fontmaker is distributed in the hope that it will be useful,
334 * but WITHOUT ANY WARRANTY; without even the implied warranty of
335 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
336 * GNU General Public License for more details.
337 *
338 * You should have received a copy of the GNU General Public License
339 * along with GD Fontmaker; if not, write to the Free Software
340 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
341 *
342 *
343 \************************************************************/
344
345 function makeFont($image,$numchars,$startchar,$pixelwidth,$pixelheight,$endianness=0) {
346
347 $startchar = ord($startchar);
348
349 // encode this at start of font
350 if ($endianness) {
351 // big-endian
352 $fontdata = chr(0).chr(0).chr(0).chr($numchars).chr(0).chr(0).chr(0).chr($startchar).chr(0).chr(0).chr(0).chr($pixelwidth).chr(0).chr(0).chr(0).chr($pixelheight);
353 } else {
354 // little-endian
355 $fontdata = chr($numchars).chr(0).chr(0).chr(0).chr($startchar).chr(0).chr(0).chr(0).chr($pixelwidth).chr(0).chr(0).chr(0).chr($pixelheight).chr(0).chr(0).chr(0);
356 }
357 // loop through each pixel of each character of the PNG
358 // (we know the dimensions of the characters because the user told us what they were)
359 $y = 0;
360 $x = 0;
361 $start_x = 0;
362 for ($c=0; $c<$numchars*$pixelwidth ; $c+=$pixelwidth) {
363 for ($y=0 ; $y<$pixelheight ; $y++) {
364 for($x=$c ; $x<$c+$pixelwidth ; $x++) {
365 // get colour of this pixel
366 $rgb = ImageColorAt($image, $x, $y);
367
368 if ($rgb==0) {
369 // it's black; font data
370 $fontdata .= chr(255);
371 } else {
372 // it's not black; background
373 $fontdata .= chr(0);
374 }
375 $i++;
376 }
377 }
378 }
379 return $fontdata;
380 }
381 }
382
383 class tx_srfreecap_gifbuilder extends tslib_gifbuilder {
384
385 /**
386 * Returns the reference to a "resource" in TypoScript.
387 *
388 * @param string The resource value.
389 * @return string Returns the relative filepath
390 * @access private
391 * @see t3lib_TStemplate::getFileName()
392 */
393 function checkFile($file) {
394 return $file;
395 }
396
397 /**
398 * Writes the input GDlib image pointer to file
399 *
400 * @param pointer The GDlib image resource pointer
401 * @param string The filename to write to
402 * @return mixed The output of either imageGif, imagePng or imageJpeg based on the filename to write
403 * @see maskImageOntoImage(), scale(), output()
404 */
405 function ImageWrite($destImg, $theImage) {
406 return parent::ImageWrite($destImg, PATH_site.$theImage);
407 }
408 }
409
410 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/sr_freecap/mod1/class.tx_srfreecap_fontmaker.php']) {
411 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/sr_freecap/mod1/class.tx_srfreecap_fontmaker.php']);
412 }
413
414 ?>