Use object manager to create object instances
[TYPO3CMS/Extensions/sr_freecap.git] / Classes / Utility / FontMakingUtility.php
1 <?php
2 namespace SJBR\SrFreecap\Utility;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2012-2013 Stanislas Rolland <typo3(arobas)sjbr.ca>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 * A copy is found in the textfile GPL.txt and important notices to the license
18 * from the author is found in LICENSE.txt distributed with these scripts.
19 *
20 *
21 * This script is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * This copyright notice MUST APPEAR in all copies of the script!
27 ***************************************************************/
28 /**
29 * Utility for making GD fonts
30 *
31 * @author Stanislas Rolland <typo3(arobas)sjbr.ca>
32 */
33 class FontMakingUtility {
34
35 /**
36 * Returns an image displaying a list of characters with specified font file and character size
37 *
38 * @param string $characters: text to display on the image
39 * @param string $font: specified True Type font file name
40 * @param integer $width: width of each character
41 * @param string $height: height of the image
42 * @return array image file info array
43 */
44 public static function makeFontImage ($characters, $font, $width = 34, $height = 50) {
45
46 $size = intval($height * .8);
47 $vertOffset = intval($height * .7);
48 $color = '#000000';
49 $bgColor = 'white';
50 $align = 'left';
51
52 $charactersArray = explode(',', $characters);
53 $charactersCount = count($charactersArray);
54
55 $gifObjArray = array();
56 $gifObjArray['backColor'] = $bgColor;
57 $gifObjArray['transparentBackground'] = 0;
58 $gifObjArray['reduceColors'] = '';
59 $gifObjArray['maxWidth'] = ($charactersCount * $width) + 1;
60 $gifObjArray['XY'] = ($charactersCount * $width) . ',' . $height;
61
62 for ($ic = 1; $ic < $charactersCount+1; $ic++) {
63 $gifObjArray[$ic . '0'] = 'TEXT';
64 $gifObjArray[$ic . '0.']['text'] = $charactersArray[$ic-1];
65
66 $bbox = imagettfbbox($size, 0, $font, $charactersArray[$ic-1]);
67 $hOffset = intval(($width - ($bbox[4] - $bbox[6]))/2);
68 $vOffset = intval(($width - ($bbox[7] - $bbox[1]))/2);
69
70 $gifObjArray[$ic . '0.']['niceText'] = 0;
71 $gifObjArray[$ic . '0.']['antiAlias'] = 1;
72 $gifObjArray[$ic . '0.']['align'] = $align;
73 $gifObjArray[$ic . '0.']['fontSize'] = $size;
74 $gifObjArray[$ic . '0.']['fontFile'] = $font;
75 $gifObjArray[$ic . '0.']['fontColor'] = $color;
76 $gifObjArray[$ic . '0.']['maxWidth'] = $width;
77 $gifObjArray[$ic . '0.']['offset'] = (($ic-1) * $width + $hOffset) . ',' . $vertOffset;
78 }
79 $objectManager = new \TYPO3\CMS\Extbase\Object\ObjectManager();
80 $gifCreator = $objectManager->create('SJBR\SrFreecap\Utility\GifBuilderUtility');
81 $gifCreator->init();
82 if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) {
83 $gifCreator->start($gifObjArray, array());
84 return $gifCreator->gifBuild();
85 } else {
86 return FALSE;
87 }
88 }
89
90 /************************************************************\
91 *
92 * GD Fontmaker Copyright 2005 Howard Yeend
93 * www.puremango.co.uk
94 *
95 * This file is part of GD Fontmaker.
96 *
97 * GD Fontmaker is free software; you can redistribute it and/or modify
98 * it under the terms of the GNU General Public License as published by
99 * the Free Software Foundation; either version 2 of the License, or
100 * (at your option) any later version.
101 *
102 * GD Fontmaker is distributed in the hope that it will be useful,
103 * but WITHOUT ANY WARRANTY; without even the implied warranty of
104 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
105 * GNU General Public License for more details.
106 *
107 * You should have received a copy of the GNU General Public License
108 * along with GD Fontmaker; if not, write to the Free Software
109 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
110 *
111 *
112 \************************************************************/
113 public static function makeFont ($image, $numchars, $startchar, $pixelwidth, $pixelheight, $endianness = 0) {
114 $startchar = ord($startchar);
115 // encode this at start of font
116 if ($endianness) {
117 // big-endian
118 $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);
119 } else {
120 // little-endian
121 $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);
122 }
123 // loop through each pixel of each character of the PNG
124 // (we know the dimensions of the characters because the user told us what they were)
125 $y = 0;
126 $x = 0;
127 $start_x = 0;
128 for ($c = 0; $c < $numchars*$pixelwidth; $c += $pixelwidth) {
129 for ($y = 0; $y < $pixelheight; $y++) {
130 for ($x = $c; $x < $c+$pixelwidth; $x++) {
131 // get colour of this pixel
132 $rgb = ImageColorAt($image, $x, $y);
133 if ($rgb == 0) {
134 // it's black; font data
135 $fontdata .= chr(255);
136 } else {
137 // it's not black; background
138 $fontdata .= chr(0);
139 }
140 $i++;
141 }
142 }
143 }
144 return $fontdata;
145 }
146 }
147 ?>