1 /***************************************************************************
2  *                                                                         *
3  *   This program is free software; you can redistribute it and/or modify  *
4  *   it under the terms of the GNU General Public License as published by  *
5  *   the Free Software Foundation; either version 2 of the License, or     *
6  *   (at your option) any later version.                                   *
7  *                                                                         *
8  ***************************************************************************/
9 #include "ADM_default.h"
10 #include "ADM_coreSubtitles.h"
11 #include "ADM_vidMisc.h"
12 
13 namespace ADM_sub
14 {
15 extern bool loadSrt(const char *file,ListOfSubtitleLines &lines);
16 extern bool srt2ssa(subtitleTextEntry &in,subtitleTextEntry &out);
17 };
18 
19 /**
20  *
21  * @param subtitleFile
22  * @return
23  */
ADM_subtitle()24 ADM_subtitle:: ADM_subtitle()
25 {
26     _type=SUB_TYPE_NONE;
27 }
28 /**
29  *
30  * @param subtitleFile
31  * @return
32  */
~ADM_subtitle()33 ADM_subtitle::~ADM_subtitle()
34 {
35 
36 }
37 /**
38  *
39  * @param subtitleFile
40  * @return
41  */
load(const char * subtitleFile)42 bool      ADM_subtitle::load(const char *subtitleFile)
43 {
44     int l=strlen(subtitleFile);
45     if(l<4)
46     {
47         ADM_warning("Subtitle file is too short <%s>\n",subtitleFile);
48         return false;
49     }
50     const char *ext=subtitleFile+l-3;
51     if(!strcasecmp(ext,"srt"))
52     {
53         bool r=ADM_sub::loadSrt(subtitleFile,_list);
54         if(!r)
55         {
56             return false;
57         }
58         _type=SUB_TYPE_SRT;
59         return true;
60     }
61     ADM_warning("Unknown extension <%s>, or not supported\n",ext);
62     return false;
63 }
64 /**
65  * \fn dump
66  * @return
67  */
dump(void)68 bool      ADM_subtitle::dump(void)
69 {
70     int n=_list.size();
71     for(int i=0;i<n;i++)
72     {
73         subtitleTextEntry &e=_list[i];
74         printf(" %s ->",ADM_us2plain(e.start));
75         printf(" %s :",ADM_us2plain(e.stop));
76         int m=e.texts.size();
77         for(int j=0;j<m;j++)
78                 printf(" --><%s> \n",e.texts[j].c_str());
79     }
80     return true;
81 }
82 
83 /**
84  */
srt2ssa()85 bool       ADM_subtitle::srt2ssa()
86 {
87     ListOfSubtitleLines converted;
88     if(_type!=SUB_TYPE_SRT)
89     {
90         ADM_warning("srt2ssa: Input file is not SRT\n");
91         return false;
92     }
93     int n=_list.size();
94     for(int i=0;i<n;i++)
95     {
96         subtitleTextEntry in,out;
97         in=_list[i];
98         ADM_sub::srt2ssa(in,out);
99         converted.push_back(out);
100     }
101     _list.clear();
102     _list=converted;
103     _type=SUB_TYPE_SSA;
104     ADM_info("Converted %d entries\n",_list.size());
105     return true;
106 }
writeSSAHeader(FILE * f,int w,int h)107 static void writeSSAHeader(FILE *f, int w, int h)
108 {
109 #define W(x) fprintf(f,x"\n");
110 #define WINT(x,y) fprintf(f,x"\n",y);
111 W("[Script Info]");
112 W("Title:");
113 W("Original Script:");
114 W("Original Translation:");
115 W("Original Editing:");
116 W("Original Timing:");
117 W("Synch Point:");
118 W("Script Updated By:");
119 W("Update Details:");
120 W("ScriptType: v4.00+");
121 W("Collisions: Normal");
122 
123 #define LIBASS_DEFAULT_WIDTH 384
124 #define LIBASS_DEFAULT_HEIGHT 288
125 
126     // sanity check
127     if(w < LIBASS_DEFAULT_WIDTH)
128         W("PlayResX:")
129     else
130         WINT("PlayResX: %d",w)
131 
132     if(h < LIBASS_DEFAULT_HEIGHT)
133         W("PlayResY:")
134     else
135         WINT("PlayResY: %d",h)
136 
137 W("PlayDepth:");
138 W("Timer: 100.0000");
139 W("WrapStyle:");
140 W("");
141 W("[V4+ Styles]");
142 W("Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding");
143 W("Style: Default,Arial,30,&H00ffffff,&H0000ffff,&H00000000,&H00000000,0,0,0,0,100,100,0,0.00,1,2,2,2,30,30,10,0");
144 W("");
145 W("[Events]");
146 W("Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text")
147 
148 }
149 /**
150  *      \fn saveAsSSA
151  */
saveAsSSA(const char * out,int width,int height)152 bool       ADM_subtitle::saveAsSSA(const char *out, int width, int height)
153 {
154     ListOfSubtitleLines converted;
155     if(_type!=SUB_TYPE_SSA)
156     {
157         ADM_warning("saveAsSSA: Input file is not SSA\n");
158         return false;
159     }
160     int n=_list.size();
161     FILE *file=ADM_fopen(out,"wt");
162     if(!file)
163     {
164         ADM_warning("Cannot create <%s>\n",out);
165         return false;
166     }
167     writeSSAHeader(file,width,height);
168     for(int i=0;i<n;i++)
169     {
170         subtitleTextEntry &in=_list[i];
171         int m=in.texts.size();
172         if(!m) continue;
173         fprintf(file,"%s",in.texts[0].c_str());
174         for(int j=1;j<m;j++)
175         {
176                 fprintf(file,"\\n%s",in.texts[j].c_str());
177         }
178         fprintf(file,"\n");
179     }
180     ADM_info("%s written\n",out);
181     fclose(file);
182     return true;
183 }
184