<?

class captionConvert {
    
    var $captionArray = array();
    var $startTC;
    var $dropFrame = false;
    
    function captionConvert($startTC = "00:00:00:00") {
        
        $this->startTC = $startTC;
        if(strpos($startTC, ";")) {
            $this->dropFrame = true;
        }
        else {
            $this->dropFrame = false;
        }
    }
    
    
    function addCaption($startTime, $captionData) {


    
        $column = 0; 
        $row = 0;
        
        $bytes = 0;
        $position = 0;
        
        $counter = 0;

        $captionData = str_replace("<br/>", " ", $captionData);

        $captionDataWords = explode(" ", $captionData);
        // Break caption down by words, so that we can insert proper line returns
        foreach($captionDataWords as $word) {
            $word .= " "; // add the space back to the end of the word
            $alreadyDrawn = false;

            // Wrap words over 12 chars, otherwise don't wrap, start a new line
            if(strlen($word) > (30 - $column) && strlen($word) < 17) {
                if($bytes ==1) {
                    $captionLine .= "80 ";
                    $bytes=0;
                    $counter++;
                }
                if($row==0) {
                    $captionLine .= "13d0 13d0 ";
                    $bytes=0;
                    $counter = $counter + 4;
                }
                elseif($row ==1) {
                    $captionLine .= "1370 1370 ";
                    $bytes=0;
                    $counter = $counter + 4;
                }
                elseif($row ==2) {
                    $captionLine .= "94d0 94d0 ";
                    $bytes=0;            
                    $counter = $counter + 4;
                }
                elseif($row ==3) {
                    $captionLine .= "9470 9470 ";
                    $bytes=0;            
                    $counter = $counter + 4;
                }
                elseif($row == 4) {
                    break;
                }
                $column =0;
                $row++;
            }
            elseif(strlen($word) >=17) {

                // big words need to wrap
                for($i=0; $i<strlen($word); $i++) {
                    $character = $word[$i];
                    $column++;
                    $captionLine .= $this->characterLookup($character);
                    $counter++;
                    if($bytes == 1) {
                        $captionLine .= " ";
                        $bytes =0;
                    }
                    else {
                        $bytes++;
                    }
                    if($column == 30) {
                        if($bytes ==1) {
                            $captionLine .= "80 ";
                            $bytes=0;
                            $counter++;
                        }
                        if($row==0) {
                            $captionLine .= "13d0 13d0 ";
                            $bytes=0;
                            $counter = $counter + 4;
                        }
                        elseif($row ==1) {
                            $captionLine .= "1370 1370 ";
                            $bytes=0;
                            $counter = $counter + 4;
                        }
                        elseif($row ==2) {
                            $captionLine .= "94d0 94d0 ";
                            $bytes=0;            
                            $counter = $counter + 4;
                        }
                        elseif($row ==3) {
                            $captionLine .= "9470 9470 ";
                            $bytes=0;            
                            $counter = $counter + 4;
                        }
                        elseif($row==4) {
                            break;
                        }
                        $column =0;

                        $row++;
                    }

                }    
                // Prevent word from being drawn again
                $alreadyDrawn = true;
                
            }
            // Draw the word, since we haven't already
            // if we've got so many words that we'll be off the screen, don't bother
            if($row < 5 && !$alreadyDrawn) {

                for($i=0; $i<strlen($word); $i++) {
                    $character = $word[$i];
                    $column++;
                    $captionLine .= $this->characterLookup($character);
                    $counter++;
                    if($bytes == 1) {
                        $captionLine .= " ";
                        $bytes =0;
                    }
                    else {
                        $bytes++;
                    }
                    
                }
            }

            
            
            
        }

        // finish padding things
        if($bytes == 1) {
            $captionLine .="80 ";
            $counter++;
        }
        else {
            $captionLine .="";
        }


        // Static time offset, because it seemed to work better than the proper way
        // (which is to count the number of characters in the captions, divided by two)
        $timeOffset = 26;

        // Flash uses decimals instead of frames.  Swap it to a colon for parsing
        $startTime = str_replace(".", ":", $startTime);

        $timeArray = explode(":", $startTime);
        
        // All of the following code is ignorant of DF versus NDF.  Which is bad.  But that math is hard.
        
        // This is how much we'll add to every time sample
        $timeOffsetArray = split('[:;]', $this->startTC);
        $timeOffsetFrames = (($timeOffsetArray[0]*3600)+($timeOffsetArray[1]*60)+($timeOffsetArray[2]))*30 + $timeOffsetArray[3];
        
        
        
        if(count($timeArray) == 1) {
            $startTimeFrames = $timeArray[0] * 30;
        }
        else {
        
            $startTimeFrames = (($timeArray[0]*3600)+($timeArray[1]*60)+($timeArray[2]))*30 + (30*(0.01 * $timeArray[3]));
        
            
            
        }

        
        // Don't start within the first 5 frames of a video.  Seems to piss off QT
        $startTimeFrames = $startTimeFrames-$timeOffset;
        if($startTimeFrames < 5) {
            $startTimeFrames+=5;
        }
    
        $startTimeFrames = round($startTimeFrames);
        
        $startTimeFrames = $startTimeFrames + $timeOffsetFrames;
        
        $frames = $startTimeFrames%30;
        $totalSeconds = ($startTimeFrames-$frames)/30;
        
        $tempTimeArray = explode(":", gmdate("H:i:s", $totalSeconds));

    
    
        
        $timeArray[0] = $tempTimeArray[0];
        $timeArray[1] = $tempTimeArray[1];
        $timeArray[2] = $tempTimeArray[2];
        $timeArray[3] = $frames;
        
        
        for($k=0; $k<count($timeArray); $k++) {

            $timeArray[$k] = str_pad($timeArray[$k], 2, "0", STR_PAD_LEFT);

        }

        if($this->dropFrame) {
            $startTime = $timeArray[0] . ":" . $timeArray[1] . ":" . $timeArray[2] . ";" . $timeArray[3];
        }
        else {
            $startTime = implode(":", $timeArray);
        }
        




        if($startTime == "00:00:00:00") {

            $startTime = "00:00:01:00";

        }




        
        $this->captionArray[] = array($startTime, $captionLine);

        
    }
    
    // This isn't a totally complete lookup array, but it's a start
    
    var $characterArray = array(" " => "20", "!" => "a1", "\"" => "a2", "#" => "23", "\$" => "a4", 
                            "%" => "25", "&" => "26", "'" => "a7", "(" => "a8", ")" => "29",
                            "." => "ae", "+" => "ab", "," => "2c", "-" => "ad",
                            "/" => "2f", "0" => "b0", "1" => "31", "2" => "32", "3" => "b3",
                            "4" => "34", "5" => "b5", "6" => "b6", "7" => "37", "8" => "38",
                            "9" => "b9", ":" => "ba", ";" => "3b", "<" => "bc", "=" => "3d",
                            ">" => "3e", "?" => "bf", "@" => "40", "A" => "c1", "B" => "c2",
                            "C" => "43", "D" => "c4", "E" => "45", "F" => "46", "G" => "c7",
                            "H" => "c8", "I" => "49", "J" => "4a", "K" => "cb", "L" => "4c",
                            "M" => "cd", "N" => "ce", "O" => "4f", "P" => "d0", "Q" => "51",
                            "R" => "52", "S" => "d3", "T" => "54", "U" => "d5", "V" => "d6",
                            "W" => "57", "X" => "58", "Y" => "d9", "Z" => "da", "[" => "5b",
                            "]" => "5d", "a" => "61", "b" => "62", "c" => "e3", "d" => "64",
                            "e" => "e5", "f" => "e6", "g" => "67", "h" => "68", "i" => "e9",
                            "j" => "ea", "k" => "6b", "l" => "ec", "m" => "6d", "n" => "6e",
                            "o" => "ef", "p" => "70", "q" => "f1", "r" => "f2", "s" => "73",
                            "t" => "f4", "u" => "75", "v" => "76", "w" => "f7", "x" => "f8",
                            "y" => "79", "z" => "7a");

                            
    
    
    // We return an opaque space (0x20 as opposed to 0x80) for characters we can't find
    function characterLookup($character) {
        $character = $this->characterArray[$character];
        if($character == "") {
            return "20";
        }
        else {
            return $character;
        }
        
    }
    

    
    function outputCaptions() {
        $finishedCaptions = "Scenarist_SCC V1.0\n\n";
        
        $startTime = $this->startTC;
        
        $finishedCaptions .= $startTime . " 942c 942c\n\n";
        
        foreach($this->captionArray as $line) {
            $finishedCaptions .= $line[0] . " " . "94ae 94ae 9420 9420 10d0 10d0 " . $line[1] . "942f 942f" . "\n\n";
        }
        
        return $finishedCaptions
    }
    
    
}