f3c466c621140a6130122ed323ad82c9c97f8c98
[atutor.git] / mods / atutor_opencaps / opencaps / conversion_service / include / classes / ccformats / cc_QTtext_format.php
1 <?php\r
2 /**\r
3  * QTtext Class\r
4  */\r
5 class QTtext extends CaptionFormat\r
6 {\r
7         private $textStyles = array();  \r
8         \r
9         /**\r
10          * Imports a caption string into a CaptionCollection \r
11          *\r
12          * @param String $theCCString the caption file as string\r
13          * @return CaptionCollection $myCcCollection A CaptionCollection Object\r
14          */\r
15         public function importCC($theCCString) \r
16         {\r
17                 //echo 'show the replaced TQtext:<form><textarea name="textarea" cols="120" rows="5">'. $theCCString. '</textarea></form>';\r
18                 \r
19                 // clean malformed patterns creted when files were saved on win notepad\r
20         //$toSearch = array(chr(13).chr(10));\r
21         //$toReplace = array(chr(10));\r
22                 \r
23                 $toSearch = array(chr(13).chr(10),chr(13),chr(10).chr(32).chr(10),chr(10).chr(10) ); // this default break line in Capscribe Desktop - Mac OS\r
24         $toReplace = array(chr(10),chr(10),chr(10),chr(10));\r
25                 \r
26         //$toSearch = chr(13); // this default break line in Capscribe Desktop - Mac OS\r
27         //$toReplace = chr(10);\r
28                 \r
29                 $theCCString = str_replace($toSearch,$toReplace,$theCCString);\r
30         \r
31                 // get all lines in array\r
32                 $allLines = split(chr(10),$theCCString);\r
33                 $controlTime = 0; // control time marks IN and OUT\r
34                 $controlCaptionLines = 0; // control the number of lines of one caption \r
35                 $addCaptionControl = 0; //Start Adding caption when = 1\r
36                 $isTime=0;\r
37                 $captionCounter = 0; // Total number of captions found\r
38                 \r
39                 $timesIn = array();\r
40                 $timesOut = array();\r
41                 $captions = array();\r
42                 \r
43         // Defining QText known pattenrs;\r
44                 $qtPatternRegEx = "/({(.*?)})/";  \r
45                 $pattern_QT_time_format_000 = "\[([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{3})\]";\r
46         $pattern_QT_time_format_00 = "\[([0-9]{2}:[0-9]{2}:[0-9]{2}.[0-9]{2})\]";\r
47         $pattern_QT_time_format_selected = '';\r
48             \r
49         // determine if miliseconds are in 2 of 3 digit format \r
50         if (preg_match("/$pattern_QT_time_format_000/", $theCCString))\r
51         {\r
52                 $pattern_QT_time_format_selected = $pattern_QT_time_format_000;\r
53         } \r
54         else if (preg_match("/$pattern_QT_time_format_00/", $theCCString)) \r
55         {\r
56                 $pattern_QT_time_format_selected = $pattern_QT_time_format_00;\r
57         }\r
58                 \r
59         //  loop through caption lines\r
60         for ($i=0; $i<count($allLines);$i++)\r
61         {\r
62                 //echo '<br/>';\r
63                 //echo '<br/>Line ('.$i.') *** '.($allLines[$i]).'';\r
64                 \r
65                 // is this a time mark ?\r
66                 $isTime = preg_match("/$pattern_QT_time_format_selected/", $allLines[$i]);\r
67                 \r
68                 // if it is a time mark, start converting else is a caption n lines\r
69                 if ($isTime==1)\r
70                 {\r
71                         // clean up time mark '[]' characters\r
72                         $allLines[$i] = str_replace('[','',$allLines[$i]);\r
73                         $allLines[$i] = str_replace(']','',$allLines[$i]);\r
74                         \r
75                         // Is this the First time mark?\r
76                         if ($controlTime == 0)\r
77                         {\r
78                                 // ok get the first time mark and wait for the time out mark \r
79                                 $timesIn[] = $allLines[$i]; // mm this is not java style... FIX ME!!\r
80                                 $controlTime = 2;\r
81                                 // start caption text and wait for n lines\r
82                                 $captions[$captionCounter]= '';\r
83                                         $addCaptionControl = 1;\r
84                                 $currentIndex = $captionCounter;\r
85                                 $captionCounter++;\r
86                                         //echo '------ This The first time TIME MARK';\r
87                         }\r
88                         \r
89                     // Is this the Time IN of the caption?\r
90                         else if ($controlTime == 1)\r
91                         {\r
92                                 // add time in\r
93                                 $timesIn[] = $allLines[$i]; // mm this is not java style... FIX ME!!\r
94                                 $controlTime = 2;\r
95                                 // start caption text and wait for n lines\r
96                                 $captions[$captionCounter]= '';\r
97                                 $addCaptionControl = 1;\r
98                                 $currentIndex = $captionCounter;\r
99                                 $captionCounter++;\r
100                                         //echo '------ This is IN TIME MARK';\r
101                         }\r
102 \r
103                     // Is this the Time Out of the caption?\r
104                         else if ($controlTime == 2)\r
105                         {\r
106                                 // add time in\r
107                                 $timesOut[] = $allLines[$i]; // mm this is not java style... FIX ME!!\r
108                                 $controlTime = 1;\r
109                                 $addCaptionControl = 0;\r
110                                         //echo '------ This is OUT TIME MARK';\r
111                         }\r
112                         \r
113                 } else {\r
114                         \r
115                         // start adding captions\r
116                         if ($addCaptionControl==1)\r
117                         {\r
118                                 // add new line to the current caption\r
119                                 $captions[$currentIndex] .= $allLines[$i].chr(10);\r
120                                 //echo '------ The caption'.$captions[$currentIndex];\r
121                         }\r
122                 } // end if\r
123                 \r
124         } // end for \r
125         \r
126         // create a collection object\r
127         $myQTextCollection = new CaptionCollection();\r
128         \r
129         /*\r
130         echo '<br/><br/>Total Captions Found:  '.$captionCounter;\r
131         echo '<br/><br/>Total TIME IN:  '.count($timesIn);\r
132         echo '<br/><br/>Total TIME OUT:  '.count($timesOut);\r
133         echo '<br/><br/>Total CAPTIONS:  '.count($captions);\r
134         */\r
135         \r
136         // test in a text area\r
137                 //echo '<form><textarea cols="100" rows="10">';\r
138             ///*\r
139         for ($j=0;$j<$captionCounter;$j++)\r
140             {\r
141             \r
142             // get all QT styles found in caption text\r
143             preg_match_all($qtPatternRegEx,$captions[$j],$captionStylesFound);\r
144 \r
145             // clean all QT styles from the caption  \r
146             $captions[$j] = str_replace($captionStylesFound[0], "", $captions[$j]);\r
147             \r
148             // show the plain text Caption\r
149             //echo '<br/><br/>'.$captions[$j];\r
150             \r
151             /*\r
152             // show all QT found in caption line\r
153             foreach ($captionStylesFound[0] as $allStyleAttrib => $allStyleValue)\r
154             {\r
155                 echo '<br/>'.$allStyleAttrib.' = '.$allStyleValue;\r
156             }\r
157             */        \r
158 \r
159                         $textStyles = Array();\r
160                         \r
161             // add text styles features to caption text styles array\r
162             if (isset($captionStylesFound[0]))\r
163             {\r
164                 foreach ($captionStylesFound[0] as $txtStyle)\r
165                 {\r
166                         //$textStyles[] = $txtStyle;\r
167                         //echo '<br/>'.$txtStyle;\r
168                         \r
169                         //adding only known text styles\r
170                         if ($txtStyle == '{justify:center}')\r
171                         {\r
172                                 $textStyles['text-align'] = 'center';\r
173                         } else if ($txtStyle == '{justify:left}')\r
174                         {\r
175                                 $textStyles['text-align'] = 'left';\r
176                         }                                       \r
177                         else if ($txtStyle == '{justify:right}')\r
178                         {\r
179                                 $textStyles['text-align'] = 'right';\r
180                         } else {\r
181                                 // adding QT formats\r
182                                 //$textStyles['QT'.$txtStyle] = $txtStyle.'-- NOT used yet';\r
183                         } // end if else\r
184                 } // end for each\r
185             } // end if\r
186 \r
187             // add a capton only if all values are set\r
188                 \r
189                         if (isset($timesIn[$j]) && isset($timesOut[$j]) && isset($captions[$j]))\r
190                         {\r
191                                 // Create a Caption Object\r
192                                 $newCaption = new Caption($timesIn[$j],$timesOut[$j],$captions[$j],$textStyles);\r
193                 \r
194                 // print added caption\r
195                 //echo $newCaption->toString(); \r
196 \r
197                 // add caption object to the collection\r
198                 $myQTextCollection->addCaption($newCaption);\r
199                 }\r
200             \r
201             /*\r
202             echo chr(10).'IN:  '.$timesIn[$j];\r
203                 echo chr(10).'OUT:  '.$timesOut[$j];\r
204                 echo chr(10).'Caption:  '.$captions[$j];\r
205                 */\r
206                 //echo chr(10).'Caption:  '.$captions[$j];\r
207                 \r
208             }// end for    \r
209             //*/\r
210 \r
211                         //echo '</textarea></form>';         \r
212         \r
213                         \r
214         // print for test\r
215         //echo $myQTextCollection->toString();\r
216                         \r
217         return $myQTextCollection;\r
218                  \r
219         } // end importCC()\r
220 \r
221         /**\r
222          * Exports a CaptionCollection object into a string\r
223          *\r
224          * @param CaptionCollection $theCollection A CaptionCollection Object\r
225          * @return String $captionString The caption as a String\r
226          */\r
227         public function exportCC($theCollection)\r
228         {\r
229                 \r
230                 $ccExport = '';\r
231         \r
232                 $myCollection = $theCollection->getCollection();\r
233                 \r
234                 // add QT header\r
235                 $ccExport .= $this->getHeader()."\r\n";\r
236                 \r
237                 foreach ($myCollection as $captionObj)\r
238                 {\r
239                         // add time in\r
240                         $ccExport .= '['.$captionObj->getInTime().']'."\r\n";\r
241                         \r
242                         // Convert <br/> into \n character\r
243                         $fixCaption = str_replace('<BR />',"\r\n",$captionObj->getCaption());\r
244                         //echo '<br/>'.$fixCaption;\r
245                         \r
246                         // get getTextStyles array from caption object\r
247                         $ccTxtStyles = $captionObj->getTextStyles();\r
248                         \r
249                                 //$ccTxtStyles = $this->getDefaultStyles($captionObj->getTextAtribute('text-align'));\r
250                         \r
251                         //  check if the caption has text styles\r
252                         if (count($ccTxtStyles)!=0)\r
253                         {\r
254                                 // loop all txt styles\r
255                                 foreach ($ccTxtStyles as $txtAttrib => $txtValue)\r
256                                 {\r
257                                         // HERE parse any known txt style and apply it to the caption\r
258                                         \r
259                                         // only for text alignment in QT\r
260                                         if ($txtValue=='center' || $txtValue=='left' || $txtValue=='right')\r
261                                         {\r
262                                                 // get the txt align default values\r
263                                                 $thisTxtAlign = $this->getDefaultStyles($txtValue,strlen($fixCaption));\r
264                                                         //echo '<br/>'.$fixCaption;\r
265                                                         //echo '<br/> Lenght: '.strlen($fixCaption);\r
266                                                 \r
267                                                 // add default text styles to the caption \r
268                                                 $fixCaption = $thisTxtAlign.$fixCaption;\r
269                                         } // end if\r
270                                         \r
271                                 } // end for\r
272                                         \r
273                         } // end if txt styles\r
274                         \r
275                         // add caption(s) to export string \r
276                         //$ccExport .= $fixCaption;\r
277                         $ccExport .= $fixCaption."\r\n";\r
278                         \r
279                         // add time out\r
280                         $ccExport .= '['.$captionObj->getOutTime().']'."\r\n";\r
281                         \r
282                 } // end foreach\r
283                 \r
284                 \r
285                 // Remove any doble break line (already done in import string.. but just in case captions lines bring this issue;) \r
286                 //-- $ccExport = str_replace(chr(10).chr(10),chr(10),$ccExport);\r
287                 \r
288                 return $ccExport;\r
289                 \r
290         } // end  exportCC()\r
291         \r
292         /**\r
293          * Verify if the caption file is a QText caption file\r
294          *\r
295          * @param String $theCCString A caption file as text = string\r
296          * @return Boolean\r
297          */\r
298         public function checkFormat($theCCString)\r
299         {\r
300                 \r
301                 $isValid = false;\r
302                 $patternCheck = "/({(QTtext)})/"; // RegExp to look for QText \r
303                 preg_match_all($patternCheck,$theCCString,$patternFound);\r
304                 \r
305                 if(count($patternFound)>0)\r
306                 {\r
307                         $isValid = true;\r
308                 }\r
309 \r
310                 return $isValid;\r
311                 \r
312         } // end  checkFormat() \r
313 \r
314         /**\r
315          * Get the name of the Caption Format Class\r
316          *\r
317          * @return String $captionFormatname The name of the Caption Format\r
318          */\r
319         public function getName()\r
320         {\r
321                 $captionFormatname = 'Quick Time Text';\r
322                 \r
323                 return $captionFormatname;\r
324         }\r
325         \r
326         /**\r
327          * Get the Description of the Caption Format\r
328          *\r
329          * @return String $captionAbout Description of the Caption Format\r
330          */\r
331         public function getAbout()\r
332         {\r
333                 $captionAbout = 'This format can be used by Quick Time Player.\r
334                 This is the native format of CAPSCRIBE !!! \r
335                 Additional information can be found at <a href="http://www.apple.com/quicktime/tutorials/texttracks.html">Apple Website</a>';\r
336                 \r
337                 return $captionAbout;\r
338                 \r
339                 \r
340         }\r
341         \r
342         /**\r
343          * Get the version of the caption format class\r
344          *\r
345          * @return String $theVersion The Version of the Caption Format as String\r
346          */\r
347         public function getVersion()\r
348         {\r
349                 $theVersion = '1.0';\r
350                  \r
351                 return $theVersion;\r
352         }\r
353                 \r
354         public function getFileExtension()\r
355         {\r
356                 $theExtension = 'txt';\r
357                  \r
358                 return $theExtension;\r
359         }\r
360         \r
361         public function getIdPattern()\r
362         {\r
363                 $idPattern = '/({QTtext})/';\r
364                 //$idPattern = "/({(.*?)})/";\r
365                 //$qtPatternRegEx = "/({(.*?)})/";\r
366                 \r
367                 return $idPattern;\r
368         }\r
369         \r
370         public function allowsTextStyles()\r
371         {\r
372                 return '1';\r
373         }       \r
374 \r
375         public function template()\r
376         {\r
377                 $ccTemplate = '\r
378 {QTtext}{font: Comic Sans MS}{italic}{size:18}{textColor: 65280,65280,65280}{backColor: 0,0,0}{justify:center}{timeScale: 1000}{timeStamps:absolute}{keyedtext:on}{language:0}{height:156}{width:383}{textEncoding:0}\r
379 [00:00:00.002]\r
380 caption 1 line1\r
381 caption 1 line2\r
382 [00:00:02.849]\r
383 [00:00:02.851]\r
384 caption 2 line1\r
385 caption 2 line2\r
386 [00:00:04.950]\r
387 [00:00:04.952]\r
388 caption 3\r
389 [00:00:06.082]\r
390 \r
391 ... or ...\r
392 \r
393 {QTtext} {font:Tahoma}\r
394 {plain} {size:20}\r
395 {timeScale:30}\r
396 {width:160} {height:32}\r
397 {timeStamps:absolute} {language:0}\r
398 [00:00:00.00]\r
399 caption 1 - using Bold style?\r
400 [00:00:02.84]\r
401  \r
402 [00:00:02.85]\r
403 caption 2\r
404 [00:00:04.95]\r
405  \r
406 [00:00:04.95]\r
407 caption 3\r
408 [00:00:06.08]\r
409 \r
410 ';\r
411                 \r
412                 return $ccTemplate;\r
413         }\r
414 \r
415 /*////////////////////////////////////////////////////////\r
416         Functions for QText conversion\r
417 //////////////////////////////////////////////////////*/\r
418         \r
419         /**\r
420          * Creates the QText default header.. soon this method will receive all the needed parameters\r
421          *\r
422          * @return String $myHeader The QText default header\r
423          */\r
424         private function getHeader()\r
425         {\r
426                 /*\r
427                 {font: Tahoma}\r
428                 {plain}\r
429                 {size:18}\r
430                 {textColor: 45824,45824,45824}\r
431                 {backColor: 0,0,0}\r
432                 {justify:center}\r
433                 {timeScale: 1000}\r
434                 {timeStamps:absolute}\r
435                 {keyedtext:on}\r
436                 {language:0}\r
437                 {height:114}\r
438                 {width:334}\r
439                 {textEncoding:0}\r
440                  */\r
441                 \r
442                 // common vars\r
443                 $textFont = "Comic Sans MS";\r
444                 $textSize = "18";\r
445                 $textJustify = "center";\r
446 \r
447                 // Unique QText header Variables                \r
448                 $myHeader = '{QTtext}';\r
449                 $myHeader .= '{font: '.$textFont.'}';\r
450                 //$myHeader .= '{plain}';\r
451                 //$myHeader .= '{size: '.$textSize.'}';\r
452                 $myHeader .= '{textColor: 65280,65280,0}';\r
453                 $myHeader .= '{backColor: 0,0,0}';\r
454                 $myHeader .= '{justify:'.$textJustify.'}';\r
455                 $myHeader .= '{timeScale: 1000}';\r
456                 $myHeader .= '{timeStamps:absolute}';\r
457                 $myHeader .= '{keyedtext:on}'; ///  WHAT IS THIS????? FIX ME\r
458                 $myHeader .= '{language:0}'; // working on ...\r
459                 $myHeader .= '{width:114}'; // working on ...\r
460                 $myHeader .= '{height:334}'; // working on ...\r
461                 $myHeader .= '{textEncoding:0}';\r
462                 // ... and much more can be set here acording to QT specification ...\r
463                 \r
464                 /*\r
465                 {anti-alias: onOrOff }\r
466                 {doNotDisplay: onOrOff }\r
467                 {doNotAutoScale: onOrOff }\r
468                 */\r
469                 \r
470                 $myHeader = '{QTtext}{font: Tahoma}{plain}{size:18}{textColor: 45824,45824,45824}{backColor: 0,0,0}{justify:center}{timeScale: 1000}{timeStamps:absolute}{keyedtext:on}{language:0}{height:114}{width:334}{textEncoding:0}';\r
471                 \r
472                 return $myHeader;\r
473         }       // end getHeader\r
474         \r
475         /**\r
476          * Sets default QT text styles at the caption level. No text style implemented yet at the word level \r
477          * @param String $txtAlign The text alignment, (e.g. 'left', 'right') if null assumed center\r
478          * @return String $myStyles the QText styles to be added before the caption text.\r
479          */\r
480         private function getDefaultStyles($txtAlign,$txtLength )\r
481         {\r
482                 // {hilitecolor:3000,0,0}{hilite:0,103}{font:Tahoma}{size:18}{textcolor:45824,45824,45824}{backcolor:0,0,0}{justify:center}{plain}( \r
483                 $myStyles = '';\r
484                 //$myStyles .= '{hilitecolor:3000,0,0}{hilite:0,139}{font:Tahoma}';\r
485                 $myStyles .= '{hilitecolor:3000,0,0}{hilite:0,'.$txtLength.'}{font:Tahoma}{size:18}{backcolor:0,0,0}';\r
486                 \r
487                 if ($txtAlign == 'left')\r
488                 {\r
489                         $myStyles .= '{textcolor:65280,55040,18432}{justify:'.$txtAlign.'}';\r
490                 } \r
491                 else if ($txtAlign == 'right')\r
492                 {\r
493                         $myStyles .= '{textcolor:26112,65280,65280}{justify:'.$txtAlign.'}';\r
494                 }\r
495                 else \r
496                 {\r
497                         $myStyles .= '{textcolor:45824,45824,45824}{justify:center}';\r
498                 }\r
499                 // and much more to be added here... !! \r
500                 \r
501                 /**\r
502                 {plain} Plain text.\r
503                 {bold} Bold text.\r
504                 {italic} Italic text.\r
505                 {underline} Underlined text.\r
506                 {outline} Outlined text.\r
507                 {shadow} Text with a drop shadow.\r
508                 {condense} Text with spacing between characters decreased.\r
509                 {extend} Text with spacing between characters increased. \r
510                  */\r
511                 //$myStyles .= '{plain}';\r
512                 \r
513                 return $myStyles;\r
514         \r
515         }\r
516 }  // end classQText \r
517 ?>