[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / cores / dvdplayer / DVDSubtitles / DVDSubtitleTagMicroDVD.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "DVDSubtitleTagMicroDVD.h"
22 #include "DVDCodecs/Overlay/DVDOverlayText.h"
23
24 void CDVDSubtitleTagMicroDVD::ConvertLine(CDVDOverlayText* pOverlay, const char* line, int len)
25 {
26   CStdString strUTF8;
27   strUTF8.assign(line, len);
28
29   m_flag[FLAG_BOLD] = 0;
30   m_flag[FLAG_ITALIC] = 0;
31   m_flag[FLAG_COLOR] = 0;
32
33   int machine_status = 1;
34   size_t pos = 0;
35
36   while (machine_status > 0)
37   {
38     if (machine_status == 1)
39     {
40       if (strUTF8[pos] == '{')
41       {
42         size_t pos2, pos3;
43         if (((pos2 = strUTF8.find(':', pos)) != CStdString::npos) && \
44            ((pos3 = strUTF8.find('}', pos2)) != CStdString::npos))
45         {
46           CStdString tagName = strUTF8.substr(pos + 1, pos2 - pos - 1);
47           CStdString tagValue = strUTF8.substr(pos2 + 1, pos3 - pos2 - 1);
48           tagValue.ToLower();
49           strUTF8.erase(pos, pos3 - pos + 1);
50           if ((tagName == "Y") || (tagName == "y"))
51           {
52             if ((tagValue == "b") && (m_flag[FLAG_BOLD] == 0))
53             {
54               m_flag[FLAG_BOLD] = (tagName == "Y")?TAG_ALL_LINE:TAG_ONE_LINE;
55               strUTF8.insert(pos, "[B]");
56               pos += 3;
57             }
58             else if ((tagValue == "i") && (m_flag[FLAG_ITALIC] == 0))
59             {
60               m_flag[FLAG_ITALIC] = (tagName == "Y")?TAG_ALL_LINE:TAG_ONE_LINE;
61               strUTF8.insert(pos, "[I]");
62               pos += 3;
63             }
64           }
65           else if ((tagName == "C") || (tagName == "c"))
66           {
67             if ((tagValue[0] == '$') && (tagValue.size() == 7))
68             {
69               bool bHex = true;
70               for( int i=1 ; i<7 ; i++ )
71               {
72                 char temp = tagValue[i];
73                 if( !(('0' <= temp && temp <= '9') ||
74                   ('a' <= temp && temp <= 'f') ||
75                   ('A' <= temp && temp <= 'F') ))
76                 {
77                   bHex = false;
78                   break;
79                 }
80               }
81               if( bHex && (m_flag[FLAG_COLOR] == 0))
82               {
83                 CStdString tempColorTag = "[COLOR ";
84                 tempColorTag += "FF";
85                 tempColorTag += tagValue.substr(1, 6);
86                 tempColorTag += "]";
87                 m_flag[FLAG_COLOR] = (tagName == "C")?TAG_ALL_LINE:TAG_ONE_LINE;
88                 strUTF8.insert(pos, tempColorTag);
89                 pos += tempColorTag.length();
90               }
91             }
92           }
93         }
94         else
95           machine_status = 2;
96       }
97       else if (strUTF8[pos] == '/')
98       {
99         if (m_flag[FLAG_ITALIC] == 0)
100         {
101           m_flag[FLAG_ITALIC] = TAG_ONE_LINE;
102           strUTF8.replace(pos, 1, "[I]");
103           pos += 3;
104         }
105         else
106           strUTF8.erase(pos, 1);
107       }
108       else
109         machine_status = 2;
110     }
111     else if (machine_status == 2)
112     {
113       size_t pos4;
114       if ((pos4= strUTF8.find('|', pos)) != CStdString::npos)
115       {
116         pos = pos4;
117         if (m_flag[FLAG_BOLD] == TAG_ONE_LINE)
118         {
119           m_flag[FLAG_BOLD] = 0;
120           strUTF8.insert(pos, "[/B]");
121           pos += 4;
122         }
123         if (m_flag[FLAG_ITALIC] == TAG_ONE_LINE)
124         {
125           m_flag[FLAG_ITALIC] = 0;
126           strUTF8.insert(pos, "[/I]");
127           pos += 4;
128         }
129         if (m_flag[FLAG_COLOR] == TAG_ONE_LINE)
130         {
131           m_flag[FLAG_COLOR] = 0;
132           strUTF8.insert(pos, "[/COLOR]");
133           pos += 8;
134         }
135         strUTF8.replace(pos, 1, "[CR]");
136         pos += 4;
137         machine_status = 1;
138       }
139       else
140       {
141         if (m_flag[FLAG_BOLD] != 0)
142           strUTF8.append("[/B]");
143         if (m_flag[FLAG_ITALIC] != 0)
144           strUTF8.append("[/I]");
145         if (m_flag[FLAG_COLOR] != 0)
146           strUTF8.append("[/COLOR]");
147         machine_status = 0;
148       }
149     }
150   }
151     
152   if (strUTF8.IsEmpty())
153     return;
154
155   if( strUTF8[strUTF8.size()-1] == '\n' )
156     strUTF8.Delete(strUTF8.size()-1);
157
158   // add a new text element to our container
159   pOverlay->AddElement(new CDVDOverlayText::CElementText(strUTF8.c_str()));
160 }
161