Index: trunk/extensions/LilyPond/LilyPond.php |
— | — | @@ -68,296 +68,13 @@ |
69 | 69 | $wgLilypondBorderY = 0; |
70 | 70 | |
71 | 71 | $wgHooks['ParserFirstCallInit'][] = 'wfLilyPondExtension'; |
| 72 | +$wgAutoloadClasses['LilyPond'] = dirname( __FILE__ ) . '/LilyPond.class.php'; |
72 | 73 | |
73 | 74 | /** |
74 | 75 | * @param $parser Parser |
75 | 76 | */ |
76 | 77 | function wfLilyPondExtension( &$parser ) { |
77 | | - $parser->setHook( "lilypond", "renderLilyPondFragment" ); |
78 | | - $parser->setHook( "lilymidi", "renderLilyPondMidiFragment" ); |
79 | | - $parser->setHook( "lilybook", "renderLilyPond" ); |
| 78 | + $parser->setHook( "lilypond", "LilyPond::renderFragment" ); |
| 79 | + $parser->setHook( "lilymidi", "LilyPond::renderMidiFragment" ); |
| 80 | + $parser->setHook( "lilybook", "LilyPond::render" ); |
80 | 81 | } |
81 | | - |
82 | | -/** |
83 | | - * @param $lilypond_code string |
84 | | - * @return string |
85 | | - */ |
86 | | -function renderLilyPondMidiFragment( $lilypond_code ) { |
87 | | - return renderLilyPondFragment( $lilypond_code, true ); |
88 | | -} |
89 | | - |
90 | | -/** |
91 | | - * @param $lilypond_code string |
92 | | - * @param $midi bool |
93 | | - * @return string |
94 | | - */ |
95 | | -function renderLilyPondFragment( $lilypond_code, $midi = false ) { |
96 | | - return renderLilyPond( "\\header {\n" |
97 | | - . "\ttagline = ##f\n" |
98 | | - . "}\n" |
99 | | - . "\\paper {\n" |
100 | | - . "\traggedright = ##t\n" |
101 | | - . "\traggedbottom = ##t\n" |
102 | | - . "\tindent = 0\mm\n" |
103 | | - . "}\n" |
104 | | - . "\\score {\n" |
105 | | - . $lilypond_code |
106 | | - . "\t\\layout { }\n" |
107 | | - . ( $midi ? "\t\\midi { }\n":"" ) |
108 | | - . "}\n", $lilypond_code ); |
109 | | -} |
110 | | - |
111 | | -/** |
112 | | - * @param $lilypond_code |
113 | | - * @param $short_code bool |
114 | | - * @return string |
115 | | - */ |
116 | | -function renderLilyPond( $lilypond_code, $short_code = false ) { |
117 | | - global $wgMathPath, $wgMathDirectory, $wgTmpDirectory, $wgLilypond, $wgLilypondPreMidi, |
118 | | - $wgLilypondPostMidi, $wgLilypondTrim, $wgLilypondBorderX, $wgLilypondBorderY; |
119 | | - |
120 | | - $mf = wfMsg( "math_failure" ); |
121 | | - |
122 | | - $md5 = md5( $lilypond_code ); |
123 | | - |
124 | | - if ( file_exists( $wgMathDirectory . "/" . $md5 . ".midi" ) ) { |
125 | | - $pre = "<a href=\"" . $wgMathPath . "/" . $md5 . ".midi\"> " . $wgLilypondPreMidi; |
126 | | - $post = $wgLilypondPostMidi . " </a>"; |
127 | | - } else { |
128 | | - $pre = ""; |
129 | | - $post = ""; |
130 | | - } |
131 | | - |
132 | | - $link = ''; |
133 | | - # if short_code is supplied, this is a fragment |
134 | | - if ( $short_code ) { |
135 | | - $link = "<img src=\"" . $wgMathPath . "/" . $md5 . ".png\" alt=\"" |
136 | | - . htmlspecialchars( $short_code ) . "\">"; |
137 | | - |
138 | | - if ( file_exists( "$wgMathDirectory/$md5.png" ) ) { |
139 | | - return $pre . $link . $post; |
140 | | - } |
141 | | - } else { |
142 | | - if ( file_exists( "$wgMathDirectory/$md5-1.png" ) ) { |
143 | | - for ( $i = 1; file_exists( $wgMathDirectory . "/" . |
144 | | - $md5 . "-" . $i . ".png" ); |
145 | | - $i++ ) { |
146 | | - |
147 | | - $link .= "<img src=\"" . $wgMathPath . "/" . |
148 | | - $md5 . "-" . $i . ".png\" alt=\"" . |
149 | | - htmlspecialchars( "page " . $i ) . "\">"; |
150 | | - } |
151 | | - return $pre . $link . $post; |
152 | | - } |
153 | | - } |
154 | | - |
155 | | - # Ensure that the temp and output dirs are available before continuing. |
156 | | - if ( !file_exists( $wgMathDirectory ) ) { |
157 | | - wfSuppressWarnings(); |
158 | | - $res = mkdir( $wgMathDirectory ); |
159 | | - wfRestoreWarnings(); |
160 | | - if ( !$res ) { |
161 | | - return "<b>$mf (" . wfMsg( "math_bad_output" ) . |
162 | | - $wgMathDirectory . ")</b>"; |
163 | | - } |
164 | | - } elseif ( !is_dir( $wgMathDirectory ) || |
165 | | - !is_writable( $wgMathDirectory ) ) { |
166 | | - return "<b>$mf (" . wfMsg( "math_bad_output" ) . ")</b>"; |
167 | | - } |
168 | | - if ( !file_exists( $wgTmpDirectory ) ) { |
169 | | - wfSuppressWarnings(); |
170 | | - $res = mkdir( $wgTmpDirectory ); |
171 | | - wfRestoreWarnings(); |
172 | | - if ( !$res ) { |
173 | | - return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) |
174 | | - . ")</b>"; |
175 | | - } |
176 | | - } elseif ( !is_dir( $wgTmpDirectory ) || |
177 | | - !is_writable( $wgTmpDirectory ) ) { |
178 | | - return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>"; |
179 | | - } |
180 | | - |
181 | | - $lyFile = $md5 . ".ly"; |
182 | | - $out = fopen( $wgTmpDirectory . "/" . $lyFile, "w" ); |
183 | | - if ( $out === false ) { |
184 | | - return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>"; |
185 | | - } |
186 | | - fwrite( $out, $lilypond_code ); |
187 | | - fclose( $out ); |
188 | | - |
189 | | - $cmd = $wgLilypond . |
190 | | - " -dsafe='#t' -dbackend=eps --png --header=texidoc " . |
191 | | - escapeshellarg( $lyFile ) . " 2>&1"; |
192 | | - |
193 | | - wfDebug( "Lilypond: $cmd\n" ); |
194 | | - $oldcwd = getcwd(); |
195 | | - chdir( $wgTmpDirectory ); |
196 | | - exec( $cmd, $output, $ret ); |
197 | | - chdir( $oldcwd ); |
198 | | - |
199 | | - if ( $ret != 0 ) { |
200 | | - return "<br><b>LilyPond error:</b><br><i>" |
201 | | - . str_replace( array( $md5, " " ), |
202 | | - array( "<b>your code</b>", " " ), |
203 | | - nl2br( htmlentities( join( "\n", $output ) ) ) ) |
204 | | - . "</i><br>"; |
205 | | - } |
206 | | - |
207 | | - if ( $short_code ) { |
208 | | - $outputFile = $wgTmpDirectory . "/" . $md5 . ".png"; |
209 | | - |
210 | | - if ( !file_exists( $outputFile ) ) { |
211 | | - return "<b>$mf (" . wfMsg( "math_image_error" ) |
212 | | - . ")</b>"; |
213 | | - } |
214 | | - |
215 | | - rename( $outputFile, $wgMathDirectory . "/" . $md5 . ".png" ); |
216 | | - } |
217 | | - |
218 | | - # remove all temporary files |
219 | | - $files = opendir( $wgTmpDirectory ); |
220 | | - $last_page = 0; |
221 | | - |
222 | | - while ( false !== ( $file = readdir( $files ) ) ) { |
223 | | - if ( substr( $file, 0, 32 ) != $md5 ) { |
224 | | - continue; |
225 | | - } |
226 | | - |
227 | | - $file_absolute = $wgTmpDirectory . "/" . $file; |
228 | | - if ( !$short_code && preg_match( '/-page(\d+)\.png$/', |
229 | | - $file, $matches ) ) { |
230 | | - if ( $matches[1] > $last_page ) { |
231 | | - $last_page = $matches[1]; |
232 | | - } |
233 | | - rename( $file_absolute, $wgMathDirectory . "/" . |
234 | | - $md5 . "-" . $matches[1] . ".png" ); |
235 | | - continue; |
236 | | - } |
237 | | - |
238 | | - if ( preg_match( '/.png$/', $file ) ) { |
239 | | - rename( $file_absolute, $wgMathDirectory . "/" . $md5 . ".png" ); |
240 | | - continue; |
241 | | - } |
242 | | - |
243 | | - if ( preg_match( '/.midi$/', $file ) ) { |
244 | | - rename( $file_absolute, $wgMathDirectory . "/" . |
245 | | - $md5 . ".midi" ); |
246 | | - $pre = "<a href=\"" . $wgMathPath . "/" . $md5 . ".midi\"> " . $wgLilypondPreMidi; |
247 | | - $post = $wgLilypondPostMidi . " </a>"; |
248 | | - continue; |
249 | | - } |
250 | | - |
251 | | - if ( !is_file( $file_absolute ) ) { |
252 | | - continue; |
253 | | - } |
254 | | - unlink( $file_absolute ); |
255 | | - } |
256 | | - closedir( $files ); |
257 | | - |
258 | | - if ( $short_code ) { |
259 | | - if ( !file_exists( $wgMathDirectory . "/" . $md5 . ".png" ) ) { |
260 | | - $errmsg = wfMsg( "math_image_error" ); |
261 | | - return "<h3>$mf ($errmsg): " . |
262 | | - htmlspecialchars( $lilypond_code ) . "</h3>"; |
263 | | - } |
264 | | - } else { |
265 | | - $link .= "<img src=\"" . $wgMathPath . "/" . $md5 . ".png\" alt=\"" |
266 | | - . htmlspecialchars( "page " ) . "\">"; |
267 | | - } |
268 | | - |
269 | | - if ( $wgLilypondTrim ) { |
270 | | - $imgFile = $wgMathDirectory . "/" . $md5 . ".png"; |
271 | | - trimImage( $imgFile, $imgFile, 0xFFFFFF ); |
272 | | - } |
273 | | - |
274 | | - if ( $wgLilypondBorderX > 0 || $wgLilypondBorderY > 0 ) { |
275 | | - $imgFile = $wgMathDirectory . "/" . $md5 . ".png"; |
276 | | - frameImage( $imgFile, $imgFile, 0xFFFFFF, $wgLilypondBorderX, $wgLilypondBorderY ); |
277 | | - } |
278 | | - |
279 | | - return $pre . $link . $post; |
280 | | -} |
281 | | - |
282 | | -/** |
283 | | - * @param $source |
284 | | - * @param $dest |
285 | | - * @param $bgColour |
286 | | - */ |
287 | | -function trimImage( $source, $dest, $bgColour ) { |
288 | | - $srcImage = imagecreatefrompng( $source ); |
289 | | - $width = imagesx( $srcImage ); |
290 | | - $height = imagesy( $srcImage ); |
291 | | - |
292 | | - $xmin = 0; |
293 | | - $found = false; |
294 | | - for ( $x = 0; $x < $width && !$found; $x++ ) { |
295 | | - for ( $y = 0; $y < $height && !$found; $y++ ) { |
296 | | - $rgb = imagecolorat( $srcImage, $x, $y ); |
297 | | - if ( $rgb != $bgColour ) { |
298 | | - $xmin = $x; |
299 | | - $found = true; |
300 | | - } |
301 | | - } |
302 | | - } |
303 | | - |
304 | | - $xmax = $xmin; |
305 | | - $found = false; |
306 | | - for ( $x = $width -1; $x > $xmin && !$found; $x-- ) { |
307 | | - for ( $y = 0; $y < $height && !$found; $y++ ) { |
308 | | - $rgb = imagecolorat( $srcImage, $x, $y ); |
309 | | - if ( $rgb != $bgColour ) { |
310 | | - $xmax = $x; |
311 | | - $found = true; |
312 | | - } |
313 | | - } |
314 | | - } |
315 | | - |
316 | | - $ymin = 0; |
317 | | - $found = false; |
318 | | - for ( $y = 0; $y < $height && !$found; $y++ ) { |
319 | | - for ( $x = 0; $x < $width && !$found; $x++ ) { |
320 | | - $rgb = imagecolorat( $srcImage, $x, $y ); |
321 | | - if ( $rgb != $bgColour ) { |
322 | | - $ymin = $y; |
323 | | - $found = true; |
324 | | - } |
325 | | - } |
326 | | - } |
327 | | - |
328 | | - $ymax = $ymin; |
329 | | - $found = false; |
330 | | - for ( $y = $height -1; $y > $ymin && !$found; $y-- ) { |
331 | | - for ( $x = 0; $x < $width && !$found; $x++ ) { |
332 | | - $rgb = imagecolorat( $srcImage, $x, $y ); |
333 | | - if ( $rgb != $bgColour ) { |
334 | | - $ymax = $y; |
335 | | - $found = true; |
336 | | - } |
337 | | - } |
338 | | - } |
339 | | - |
340 | | - $newWidth = $xmax - $xmin + 1; |
341 | | - $newHeight = $ymax - $ymin + 1; |
342 | | - |
343 | | - $dstImage = imagecreatetruecolor( $newWidth, $newHeight ); |
344 | | - imagecopy( $dstImage, $srcImage, 0, 0, $xmin, $ymin, $newWidth, $newHeight ); |
345 | | - imagepng( $dstImage, $dest ); |
346 | | -} |
347 | | - |
348 | | -/** |
349 | | - * @param $source |
350 | | - * @param $dest |
351 | | - * @param $bgColour |
352 | | - * @param $borderWidth |
353 | | - * @param $borderHeight |
354 | | - */ |
355 | | -function frameImage( $source, $dest, $bgColour, $borderWidth, $borderHeight ) { |
356 | | - $srcImage = imagecreatefrompng( $source ); |
357 | | - $width = imagesx( $srcImage ); |
358 | | - $height = imagesy( $srcImage ); |
359 | | - $dstImage = imagecreatetruecolor( $width + 2 * $borderWidth, $height + 2 * $borderHeight ); |
360 | | - $allocatedBgColour = imagecolorallocate( $dstImage, ( $bgColour >> 16 ) & 0xFF, ( $bgColour >> 8 ) & 0xFF, $bgColour & 0xFF ); |
361 | | - imagefill( $dstImage, 0, 0, $allocatedBgColour ); |
362 | | - imagecopy( $dstImage, $srcImage, $borderWidth, $borderHeight, 0, 0, $width, $height ); |
363 | | - imagepng( $dstImage, $dest ); |
364 | | -} |
Index: trunk/extensions/LilyPond/LilyPond.class.php |
— | — | @@ -0,0 +1,287 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +class LilyPond { |
| 5 | + /** |
| 6 | + * @param $lilypond_code string |
| 7 | + * @return string |
| 8 | + */ |
| 9 | + public static function renderMidiFragment( $lilypond_code ) { |
| 10 | + return renderLilyPondFragment( $lilypond_code, true ); |
| 11 | + } |
| 12 | + |
| 13 | + /** |
| 14 | + * @param $lilypond_code string |
| 15 | + * @param $midi bool |
| 16 | + * @return string |
| 17 | + */ |
| 18 | + public static function renderFragment( $lilypond_code, $midi = false ) { |
| 19 | + return renderLilyPond( "\\header {\n" |
| 20 | + . "\ttagline = ##f\n" |
| 21 | + . "}\n" |
| 22 | + . "\\paper {\n" |
| 23 | + . "\traggedright = ##t\n" |
| 24 | + . "\traggedbottom = ##t\n" |
| 25 | + . "\tindent = 0\mm\n" |
| 26 | + . "}\n" |
| 27 | + . "\\score {\n" |
| 28 | + . $lilypond_code |
| 29 | + . "\t\\layout { }\n" |
| 30 | + . ( $midi ? "\t\\midi { }\n":"" ) |
| 31 | + . "}\n", $lilypond_code ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param $lilypond_code |
| 36 | + * @param $short_code bool |
| 37 | + * @return string |
| 38 | + */ |
| 39 | + public static function render( $lilypond_code, $short_code = false ) { |
| 40 | + global $wgMathPath, $wgMathDirectory, $wgTmpDirectory, $wgLilypond, $wgLilypondPreMidi, |
| 41 | + $wgLilypondPostMidi, $wgLilypondTrim, $wgLilypondBorderX, $wgLilypondBorderY; |
| 42 | + |
| 43 | + $mf = wfMsg( "math_failure" ); |
| 44 | + |
| 45 | + $md5 = md5( $lilypond_code ); |
| 46 | + |
| 47 | + if ( file_exists( $wgMathDirectory . "/" . $md5 . ".midi" ) ) { |
| 48 | + $pre = "<a href=\"" . $wgMathPath . "/" . $md5 . ".midi\"> " . $wgLilypondPreMidi; |
| 49 | + $post = $wgLilypondPostMidi . " </a>"; |
| 50 | + } else { |
| 51 | + $pre = ""; |
| 52 | + $post = ""; |
| 53 | + } |
| 54 | + |
| 55 | + $link = ''; |
| 56 | + # if short_code is supplied, this is a fragment |
| 57 | + if ( $short_code ) { |
| 58 | + $link = "<img src=\"" . $wgMathPath . "/" . $md5 . ".png\" alt=\"" |
| 59 | + . htmlspecialchars( $short_code ) . "\">"; |
| 60 | + |
| 61 | + if ( file_exists( "$wgMathDirectory/$md5.png" ) ) { |
| 62 | + return $pre . $link . $post; |
| 63 | + } |
| 64 | + } else { |
| 65 | + if ( file_exists( "$wgMathDirectory/$md5-1.png" ) ) { |
| 66 | + for ( $i = 1; file_exists( $wgMathDirectory . "/" . |
| 67 | + $md5 . "-" . $i . ".png" ); |
| 68 | + $i++ ) { |
| 69 | + |
| 70 | + $link .= "<img src=\"" . $wgMathPath . "/" . |
| 71 | + $md5 . "-" . $i . ".png\" alt=\"" . |
| 72 | + htmlspecialchars( "page " . $i ) . "\">"; |
| 73 | + } |
| 74 | + return $pre . $link . $post; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + # Ensure that the temp and output dirs are available before continuing. |
| 79 | + if ( !file_exists( $wgMathDirectory ) ) { |
| 80 | + wfSuppressWarnings(); |
| 81 | + $res = mkdir( $wgMathDirectory ); |
| 82 | + wfRestoreWarnings(); |
| 83 | + if ( !$res ) { |
| 84 | + return "<b>$mf (" . wfMsg( "math_bad_output" ) . |
| 85 | + $wgMathDirectory . ")</b>"; |
| 86 | + } |
| 87 | + } elseif ( !is_dir( $wgMathDirectory ) || |
| 88 | + !is_writable( $wgMathDirectory ) ) { |
| 89 | + return "<b>$mf (" . wfMsg( "math_bad_output" ) . ")</b>"; |
| 90 | + } |
| 91 | + if ( !file_exists( $wgTmpDirectory ) ) { |
| 92 | + wfSuppressWarnings(); |
| 93 | + $res = mkdir( $wgTmpDirectory ); |
| 94 | + wfRestoreWarnings(); |
| 95 | + if ( !$res ) { |
| 96 | + return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) |
| 97 | + . ")</b>"; |
| 98 | + } |
| 99 | + } elseif ( !is_dir( $wgTmpDirectory ) || |
| 100 | + !is_writable( $wgTmpDirectory ) ) { |
| 101 | + return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>"; |
| 102 | + } |
| 103 | + |
| 104 | + $lyFile = $md5 . ".ly"; |
| 105 | + $out = fopen( $wgTmpDirectory . "/" . $lyFile, "w" ); |
| 106 | + if ( $out === false ) { |
| 107 | + return "<b>$mf (" . wfMsg( "math_bad_tmpdir" ) . ")</b>"; |
| 108 | + } |
| 109 | + fwrite( $out, $lilypond_code ); |
| 110 | + fclose( $out ); |
| 111 | + |
| 112 | + $cmd = $wgLilypond . |
| 113 | + " -dsafe='#t' -dbackend=eps --png --header=texidoc " . |
| 114 | + escapeshellarg( $lyFile ) . " 2>&1"; |
| 115 | + |
| 116 | + wfDebug( "Lilypond: $cmd\n" ); |
| 117 | + $oldcwd = getcwd(); |
| 118 | + chdir( $wgTmpDirectory ); |
| 119 | + exec( $cmd, $output, $ret ); |
| 120 | + chdir( $oldcwd ); |
| 121 | + |
| 122 | + if ( $ret != 0 ) { |
| 123 | + return "<br><b>LilyPond error:</b><br><i>" |
| 124 | + . str_replace( array( $md5, " " ), |
| 125 | + array( "<b>your code</b>", " " ), |
| 126 | + nl2br( htmlentities( join( "\n", $output ) ) ) ) |
| 127 | + . "</i><br>"; |
| 128 | + } |
| 129 | + |
| 130 | + if ( $short_code ) { |
| 131 | + $outputFile = $wgTmpDirectory . "/" . $md5 . ".png"; |
| 132 | + |
| 133 | + if ( !file_exists( $outputFile ) ) { |
| 134 | + return "<b>$mf (" . wfMsg( "math_image_error" ) |
| 135 | + . ")</b>"; |
| 136 | + } |
| 137 | + |
| 138 | + rename( $outputFile, $wgMathDirectory . "/" . $md5 . ".png" ); |
| 139 | + } |
| 140 | + |
| 141 | + # remove all temporary files |
| 142 | + $files = opendir( $wgTmpDirectory ); |
| 143 | + $last_page = 0; |
| 144 | + |
| 145 | + while ( false !== ( $file = readdir( $files ) ) ) { |
| 146 | + if ( substr( $file, 0, 32 ) != $md5 ) { |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + $file_absolute = $wgTmpDirectory . "/" . $file; |
| 151 | + if ( !$short_code && preg_match( '/-page(\d+)\.png$/', |
| 152 | + $file, $matches ) ) { |
| 153 | + if ( $matches[1] > $last_page ) { |
| 154 | + $last_page = $matches[1]; |
| 155 | + } |
| 156 | + rename( $file_absolute, $wgMathDirectory . "/" . |
| 157 | + $md5 . "-" . $matches[1] . ".png" ); |
| 158 | + continue; |
| 159 | + } |
| 160 | + |
| 161 | + if ( preg_match( '/.png$/', $file ) ) { |
| 162 | + rename( $file_absolute, $wgMathDirectory . "/" . $md5 . ".png" ); |
| 163 | + continue; |
| 164 | + } |
| 165 | + |
| 166 | + if ( preg_match( '/.midi$/', $file ) ) { |
| 167 | + rename( $file_absolute, $wgMathDirectory . "/" . |
| 168 | + $md5 . ".midi" ); |
| 169 | + $pre = "<a href=\"" . $wgMathPath . "/" . $md5 . ".midi\"> " . $wgLilypondPreMidi; |
| 170 | + $post = $wgLilypondPostMidi . " </a>"; |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + if ( !is_file( $file_absolute ) ) { |
| 175 | + continue; |
| 176 | + } |
| 177 | + unlink( $file_absolute ); |
| 178 | + } |
| 179 | + closedir( $files ); |
| 180 | + |
| 181 | + if ( $short_code ) { |
| 182 | + if ( !file_exists( $wgMathDirectory . "/" . $md5 . ".png" ) ) { |
| 183 | + $errmsg = wfMsg( "math_image_error" ); |
| 184 | + return "<h3>$mf ($errmsg): " . |
| 185 | + htmlspecialchars( $lilypond_code ) . "</h3>"; |
| 186 | + } |
| 187 | + } else { |
| 188 | + $link .= "<img src=\"" . $wgMathPath . "/" . $md5 . ".png\" alt=\"" |
| 189 | + . htmlspecialchars( "page " ) . "\">"; |
| 190 | + } |
| 191 | + |
| 192 | + if ( $wgLilypondTrim ) { |
| 193 | + $imgFile = $wgMathDirectory . "/" . $md5 . ".png"; |
| 194 | + self::trimImage( $imgFile, $imgFile, 0xFFFFFF ); |
| 195 | + } |
| 196 | + |
| 197 | + if ( $wgLilypondBorderX > 0 || $wgLilypondBorderY > 0 ) { |
| 198 | + $imgFile = $wgMathDirectory . "/" . $md5 . ".png"; |
| 199 | + self::frameImage( $imgFile, $imgFile, 0xFFFFFF, $wgLilypondBorderX, $wgLilypondBorderY ); |
| 200 | + } |
| 201 | + |
| 202 | + return $pre . $link . $post; |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * @param $source |
| 207 | + * @param $dest |
| 208 | + * @param $bgColour |
| 209 | + */ |
| 210 | + private static function trimImage( $source, $dest, $bgColour ) { |
| 211 | + $srcImage = imagecreatefrompng( $source ); |
| 212 | + $width = imagesx( $srcImage ); |
| 213 | + $height = imagesy( $srcImage ); |
| 214 | + |
| 215 | + $xmin = 0; |
| 216 | + $found = false; |
| 217 | + for ( $x = 0; $x < $width && !$found; $x++ ) { |
| 218 | + for ( $y = 0; $y < $height && !$found; $y++ ) { |
| 219 | + $rgb = imagecolorat( $srcImage, $x, $y ); |
| 220 | + if ( $rgb != $bgColour ) { |
| 221 | + $xmin = $x; |
| 222 | + $found = true; |
| 223 | + } |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + $xmax = $xmin; |
| 228 | + $found = false; |
| 229 | + for ( $x = $width -1; $x > $xmin && !$found; $x-- ) { |
| 230 | + for ( $y = 0; $y < $height && !$found; $y++ ) { |
| 231 | + $rgb = imagecolorat( $srcImage, $x, $y ); |
| 232 | + if ( $rgb != $bgColour ) { |
| 233 | + $xmax = $x; |
| 234 | + $found = true; |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + $ymin = 0; |
| 240 | + $found = false; |
| 241 | + for ( $y = 0; $y < $height && !$found; $y++ ) { |
| 242 | + for ( $x = 0; $x < $width && !$found; $x++ ) { |
| 243 | + $rgb = imagecolorat( $srcImage, $x, $y ); |
| 244 | + if ( $rgb != $bgColour ) { |
| 245 | + $ymin = $y; |
| 246 | + $found = true; |
| 247 | + } |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + $ymax = $ymin; |
| 252 | + $found = false; |
| 253 | + for ( $y = $height -1; $y > $ymin && !$found; $y-- ) { |
| 254 | + for ( $x = 0; $x < $width && !$found; $x++ ) { |
| 255 | + $rgb = imagecolorat( $srcImage, $x, $y ); |
| 256 | + if ( $rgb != $bgColour ) { |
| 257 | + $ymax = $y; |
| 258 | + $found = true; |
| 259 | + } |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + $newWidth = $xmax - $xmin + 1; |
| 264 | + $newHeight = $ymax - $ymin + 1; |
| 265 | + |
| 266 | + $dstImage = imagecreatetruecolor( $newWidth, $newHeight ); |
| 267 | + imagecopy( $dstImage, $srcImage, 0, 0, $xmin, $ymin, $newWidth, $newHeight ); |
| 268 | + imagepng( $dstImage, $dest ); |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * @param $source |
| 273 | + * @param $dest |
| 274 | + * @param $bgColour |
| 275 | + * @param $borderWidth |
| 276 | + * @param $borderHeight |
| 277 | + */ |
| 278 | + private static function frameImage( $source, $dest, $bgColour, $borderWidth, $borderHeight ) { |
| 279 | + $srcImage = imagecreatefrompng( $source ); |
| 280 | + $width = imagesx( $srcImage ); |
| 281 | + $height = imagesy( $srcImage ); |
| 282 | + $dstImage = imagecreatetruecolor( $width + 2 * $borderWidth, $height + 2 * $borderHeight ); |
| 283 | + $allocatedBgColour = imagecolorallocate( $dstImage, ( $bgColour >> 16 ) & 0xFF, ( $bgColour >> 8 ) & 0xFF, $bgColour & 0xFF ); |
| 284 | + imagefill( $dstImage, 0, 0, $allocatedBgColour ); |
| 285 | + imagecopy( $dstImage, $srcImage, $borderWidth, $borderHeight, 0, 0, $width, $height ); |
| 286 | + imagepng( $dstImage, $dest ); |
| 287 | + } |
| 288 | + } |
\ No newline at end of file |
Property changes on: trunk/extensions/LilyPond/LilyPond.class.php |
___________________________________________________________________ |
Added: svn:eol-syle |
1 | 289 | + native |