9962b7970e1dbfd77e2aab90c848f0fe62dce66d
[atutor.git] / mods / atutor_opencaps / opencaps / conversion_service / include / classes / ccformats / cc_DFXP_format.php
1 <?php\r
2 /**\r
3  * DFXP Class\r
4  */\r
5 class DFXP extends CaptionFormat\r
6 {\r
7         private $textStyles = array();  \r
8         \r
9 \r
10         /**\r
11          * Imports a caption string into a CaptionCollection \r
12          *\r
13          * @param String $theCCString the caption file as string\r
14          * @return CaptionCollection $myCcCollection A CaptionCollection Object\r
15          */\r
16         public function importCC($theCCString) {\r
17                 \r
18                 // include classes for XML parser\r
19                 include_once('include/classes/core_XmlTag.php');\r
20                 include_once('include/classes/core_XmlTagCollection.php');\r
21                 include_once('include/classes/core_XmlTagTrace.php');\r
22                 \r
23                 // create a xml parser \r
24                 $myXmlParse = new XmlTagTrace($theCCString,'P',4);\r
25                 \r
26                 // print xml collection\r
27                 //$myXmlParse->toString();\r
28                 \r
29                 // get xml collection\r
30                 $myXmlTagCollection = $myXmlParse->getCollection();\r
31                 \r
32         //include_once('CaptionCollection.php');\r
33         $myCcCollection = new CaptionCollection();\r
34 \r
35         foreach ($myXmlTagCollection as $xmlTagObj) \r
36         {               \r
37                 //echo '<br/>Begin Time: '.$xmlTagObj->getTagAttribute('BEGIN');\r
38                 //echo '<br/>End Time: '.$xmlTagObj->getTagAttribute('END'); \r
39                 //echo '<br>Caption:'.$xmlTagObj->getTagValue();\r
40                 \r
41                 $txtStylesArr = Array();\r
42                 $newCaption = new Caption($xmlTagObj->getTagAttribute('BEGIN').'0',$xmlTagObj->getTagAttribute('END').'0',$xmlTagObj->getTagValue(),$txtStylesArr);\r
43                 \r
44                 // add captions to the collection\r
45                         $myCcCollection->addCaption($newCaption);\r
46                         \r
47         } // end for \r
48         \r
49             //$myCcCollection->toString();\r
50                 \r
51         return $myCcCollection;\r
52                  \r
53         } // end importCC()\r
54         \r
55         /**\r
56          * Exports a CaptionCollection object into a string\r
57          *\r
58          * @param CaptionCollection $theCollection A CaptionCollection Object\r
59          * @return String $captionString The caption as a String\r
60          */\r
61         public function exportCC($theCollection)\r
62         {\r
63                 $ttCaption = '';\r
64         \r
65                 $myCollection = $theCollection->getCollection();\r
66                 \r
67                 // add header\r
68                 $ttCaption .= $this->getTTHeader();\r
69                 \r
70                 // Caption counter\r
71                 $capCount = 0;\r
72 \r
73                 foreach ($myCollection as $captionObj)\r
74                 {\r
75                         $capCount++;\r
76                         \r
77                         // adding caption number - for debug purpuses \r
78                         //$captionObj->getCaption() = "[CAP no. $capCount]" . " ". $captionObj->getCaption();\r
79                         \r
80                         // adapt \n character to <br/>\r
81                         //$fixCap = "[CAP no. $capCount] : ". CcUtil::ccNewLineToBr($captionObj->getCaption(),' <br/>'); \r
82                         $fixCap = "". TxtFileTools::ccNewLineToBr($captionObj->getCaption(),' <br />');\r
83 \r
84                         // convert qt to TT time format\r
85                         $ttTimeIn = $this->timeQtToTT($captionObj->getInTime());\r
86                         $ttTimeOut = $this->timeQtToTT($captionObj->getOutTime());\r
87         \r
88                         // ading TTcaptions\r
89                         $ttCaption .= "".$this->getTTCaption($ttTimeIn,$ttTimeOut,$fixCap,$captionObj->getTextStyles());\r
90 \r
91                         // show caption object\r
92                         //$captionObj->toString();\r
93                         \r
94                 } // end foreach\r
95                 \r
96                 //  close TT file\r
97                 $ttCaption .= $this->getTTClose();\r
98                 \r
99                 return $ttCaption;\r
100                 \r
101         } // end  exportCC()\r
102         \r
103         /**\r
104          * Verify if the caption file is a QText caption file \r
105         */\r
106         public function checkFormat($theCCString)\r
107         {\r
108                 $isValid = false;\r
109                 $patternCheck = "/({(QTtext)})/"; // RegExp to look for QText \r
110                 preg_match_all($patternCheck,$theCCString,$patternFound);\r
111                 \r
112                 if(count($patternFound)>0)\r
113                 {\r
114                         $isValid = true;\r
115                 }\r
116 \r
117                 return $isValid;\r
118                 \r
119         } // end  checkFormat() \r
120 \r
121         /*\r
122          * Here functions to re-define\r
123          */\r
124         public function getName()\r
125         {\r
126                 return 'Timed Text (TT) Authoring Format 1.0 \96 Distribution Format Exchange Profile (DFXP)';\r
127         }\r
128         \r
129         public function getAbout()\r
130         {\r
131                 return 'This is the most comprehensive caption format ever !!\r
132                 XML based.\r
133                 Additional information at <a href="http://www.w3.org/TR/2006/CR-ttaf1-dfxp-20061116/">W3-dfxp</a>';\r
134         }\r
135         \r
136         public function getVersion()\r
137         {\r
138                 return '1.0';\r
139         }\r
140                 \r
141         public function getFileExtension()\r
142         {\r
143                 return 'dfxp.xml';\r
144         }\r
145         \r
146         public function getIdPattern()\r
147         {\r
148                 $idPattern = '/(<tt xmlns="http:\/\/www.w3.org\/2006\/04\/ttaf1)/';\r
149                 \r
150                 return $idPattern;\r
151         }\r
152         \r
153         public function allowsTextStyles()\r
154         {\r
155                 return '1';\r
156         }\r
157         \r
158         public function template()\r
159         {\r
160                 \r
161         }\r
162 \r
163 \r
164 /*////////////////////////////////////////////////////////\r
165         Functions for TT - Timed Text Conversion\r
166 //////////////////////////////////////////////////////*/\r
167 \r
168 \r
169 /**\r
170  * creates a TT Caption \r
171  * @return String $ttCaption A TT formatted caption \r
172  * @param string $capInTime Caption start time (e.g 0:00:00.80)\r
173  * @param string $capOutTime Caption end time (e.g 0:00:00.80)\r
174  * @param String $caption caption, with all styles\r
175  * @param Array $txtStyles Array with text styles in the caption\r
176  */\r
177 private function getTTCaption($capInTime,$capOutTime,$caption,$txtStyles)\r
178 {\r
179         \r
180 // <p begin="0:00:00.00" end="0:00:00.80">1 begining</p>\r
181 \r
182         $ttCaption = "";\r
183         \r
184                 // Find if text alignment in $txtStyles array\r
185                 if (isset($txtStyles['text-align']))\r
186                 {\r
187                         if ($txtStyles['text-align']=='right')\r
188                         {\r
189         // Add right alignment style\r
190         $ttCaption = '\r
191         <p begin="'.$capInTime.'" end="'.$capOutTime.'" style="txtRight">'.$caption.'</p>';                             \r
192                         } \r
193                         else if ($txtStyles['text-align']=='left')\r
194                         {\r
195         // Add right alignment style\r
196         $ttCaption = '\r
197         <p begin="'.$capInTime.'" end="'.$capOutTime.'" style="txtLeft">'.$caption.'</p>';\r
198                         }\r
199                         else if ($txtStyles['text-align']=='center')\r
200                         {\r
201         $ttCaption = '\r
202         <p begin="'.$capInTime.'" end="'.$capOutTime.'">'.$caption.'</p>';\r
203 \r
204                         }\r
205                         \r
206                 } else {\r
207                         // no text style, creates a plain caption   \r
208         $ttCaption = '\r
209         <p begin="'.$capInTime.'" end="'.$capOutTime.'">'.$caption.'</p>';\r
210                 \r
211                 } // end if\r
212         \r
213 \r
214 \r
215         return $ttCaption;\r
216         \r
217 } // end getTTCaption()\r
218 \r
219 /**\r
220  * Converts QT time to TT Time; drops last right number on the time format\r
221  * @return String $ttTime TT time format 00:01:10.28"\r
222  * @param String $qtTime QT time Format; "00:01:10.280"\r
223  */\r
224 private function timeQtToTT($qtTime)\r
225 {\r
226         $ttTime = substr($qtTime, 0, 11); // returns "d"   ;\r
227         return $ttTime;\r
228 } // end timeQtToTT\r
229 \r
230 /// TT Header \r
231 private function getTTHeader()\r
232 {\r
233 // common vars\r
234 $textFont = "Arial";\r
235 $textFontWeight = "normal";\r
236 $textFontStyle = "normal";\r
237 $textSize = "12";\r
238 $textJustify = "center";\r
239 \r
240 // unique vars \r
241 $capTitle = 'This is a sample SAMI 1.0 caption';\r
242 $textHtmlColor = "white";\r
243 $bgHtmlColor = "black";\r
244 $capLangName = "English";\r
245 $capLangCode= "EN-US-CC";\r
246 \r
247 $capTT_header = '<?xml version="1.0" encoding="UTF-8"?>\r
248 <tt xmlns="http://www.w3.org/2006/04/ttaf1"\r
249       xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling" xml:lang="en">\r
250   <head>\r
251     <styling>\r
252       <style id="txtRight" tts:textAlign="right" tts:color="cyan"/>\r
253       <style id="txtLeft" tts:textAlign="left" tts:color="#FCCA03"/>\r
254       <style id="defaultSpeaker" tts:fontSize="'.$textSize.'px" tts:fontFamily="'.$textFont.'" tts:fontWeight="'.$textFontWeight.'" tts:fontStyle="'.$textFontStyle.'" tts:textDecoration="none" tts:color="'.$textHtmlColor.'" tts:backgroundColor="'.$bgHtmlColor.'" tts:textAlign="'.$textJustify.'" />\r
255       <style id="defaultCaption" tts:fontSize="14px" tts:fontFamily="SansSerif" tts:fontWeight="normal" tts:fontStyle="normal" tts:textDecoration="none" tts:color="white" tts:backgroundColor="black" tts:textAlign="left" />\r
256     </styling>\r
257   </head>\r
258   <body id="ccbody" style="defaultSpeaker">\r
259     <div xml:lang="en">';\r
260     \r
261         return $capTT_header;\r
262 \r
263 } // end getTTHeader()\r
264 \r
265 \r
266 // Time Text Close\r
267 private function getTTClose()\r
268 {\r
269 // alternative close    "<div xml:lang="en"/>"\r
270 $capTT_close = '\r
271     </div>\r
272   </body>\r
273 </tt>'\r
274 ;\r
275     return $capTT_close;\r
276 }\r
277         \r
278 }  // end classQText \r
279 ?>\r
280 \r
281 \r
282 \r