2 * Copyright (C) 2003-2006 Gabest
3 * http://www.gabest.org
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)
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.
15 * You should have received a copy of the GNU General Public License
16 * along with GNU Make; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
29 typedef enum {TIME
, FRAME
} tmode
; // the meaning of STSEntry::start/end
35 double fontSize
; // height
41 bool operator == (const STSStyleBase
& s
)const;
44 LOGFONTA
& operator <<= (LOGFONTA
& lfa
, const STSStyleBase
& s
);
45 LOGFONTW
& operator <<= (LOGFONTW
& lfw
, const STSStyleBase
& s
);
47 struct STSStyle
: public STSStyleBase
50 FwRect marginRect
; // measured from the sides
51 int scrAlignment
; // 1 - 9: as on the numpad, 0: default
52 int borderStyle
; // 0: outline, 1: opaque box
53 double outlineWidthX
, outlineWidthY
;
54 double shadowDepthX
, shadowDepthY
;
55 COLORREF colors
[4]; // usually: {primary, secondary, outline/background, shadow}
58 double fontScaleX
, fontScaleY
; // percent
59 double fontSpacing
; // +/- pixels
63 double fontAngleZ
, fontAngleX
, fontAngleY
;
64 double fontShiftX
, fontShiftY
;
65 int relativeTo
; // 0: window, 1: video, 2: undefined (~window)
71 bool operator == (const STSStyle
& s
)const;
72 bool IsFontStyleEqual(const STSStyle
& s
) const;
74 void operator = (const LOGFONT
& lf
);
76 friend CString
& operator <<= (CString
& style
, const STSStyle
& s
);
77 friend STSStyle
& operator <<= (STSStyle
& s
, const CString
& style
);
80 typedef ::boost::flyweights::flyweight
<STSStyle
, ::boost::flyweights::no_locking
> FwSTSStyle
;
83 static inline std::size_t hash_value(const STSStyleBase
& s
)
85 std::size_t hash
= CStringElementTraits
<CString
>::Hash(s
.fontName
);
86 hash
= (hash
<<5) + (hash
) + s
.charSet
;
87 hash
= (hash
<<5) + (hash
) + hash_value(s
.fontSize
);
88 hash
= (hash
<<5) + (hash
) + s
.fontWeight
;
89 hash
= (hash
<<5) + (hash
) + s
.fItalic
; //Todo: fix me
90 hash
= (hash
<<5) + (hash
) + s
.fUnderline
;
91 hash
= (hash
<<5) + (hash
) + s
.fStrikeOut
;
95 static inline std::size_t hash_value(const STSStyle
& s
)
98 std::size_t hash
= hash_value(static_cast<const STSStyleBase
&>(s
));
99 hash
= (hash
<<5) + (hash
) + s
.colors
[0];
100 hash
= (hash
<<5) + (hash
) + s
.colors
[2];
101 hash
= (hash
<<5) + (hash
) + hash_value(s
.fontScaleX
);
102 hash
= (hash
<<5) + (hash
) + hash_value(s
.fontScaleY
);
103 hash
= (hash
<<5) + (hash
) + hash_value(s
.fontAngleX
);
104 hash
= (hash
<<5) + (hash
) + hash_value(s
.fontAngleY
);
105 hash
= (hash
<<5) + (hash
) + hash_value(s
.fontAngleZ
);
106 hash
= (hash
<<5) + (hash
) + hash_value(s
.fontShiftX
);
107 hash
= (hash
<<5) + (hash
) + hash_value(s
.fontShiftY
);
112 class CSTSStyleMap
: public CAtlMap
<CString
, STSStyle
*, CStringElementTraits
<CString
> >
116 virtual ~CSTSStyleMap() {Free();}
124 CString style
, actor
, effect
;
135 bool animated
; //if this segment has animate effect
138 STSSegment() { animated
=false; }
139 STSSegment(int s
, int e
) {start
= s
; end
= e
; animated
= false;}
140 STSSegment(const STSSegment
& stss
) {*this = stss
;}
141 void operator = (const STSSegment
& stss
) {start
= stss
.start
; end
= stss
.end
; animated
= stss
.animated
; subs
.Copy(stss
.subs
);}
144 class CSimpleTextSubtitle
146 friend class CSubtitleEditorDlg
;
149 CAtlArray
<STSEntry
> m_entries
;
150 CAtlArray
<STSSegment
> m_segments
;
151 virtual void OnChanged() {}
157 CTextFile::enc m_encoding
;
160 CSize m_dstScreenSize
;
161 int m_defaultWrapStyle
;
165 bool m_fUsingAutoGeneratedDefaultStyle
;
167 CSTSStyleMap m_styles
;
169 enum EPARCompensationType
177 EPARCompensationType m_ePARCompensationType
;
178 double m_dPARCompensation
;
181 CSimpleTextSubtitle();
182 virtual ~CSimpleTextSubtitle();
184 virtual void Copy(CSimpleTextSubtitle
& sts
);
185 virtual void Empty();
189 void Sort(bool fRestoreReadorder
= false);
190 void RemoveAllEntries();
191 void CreateSegments();
193 void Append(CSimpleTextSubtitle
& sts
, int timeoff
= -1);
195 bool Open(CString fn
, int CharSet
, CString name
= _T(""));
196 bool Open(CTextFile
* f
, int CharSet
, CString name
);
197 bool Open(BYTE
* data
, int len
, int CharSet
, CString name
);
198 bool SaveAs(CString fn
, exttype et
, double fps
= -1, CTextFile::enc
= CTextFile::ASCII
);
200 void Add(CStringW str
, bool fUnicode
, int start
, int end
, CString style
= CString(_T("Default")),
201 const CString
& actor
= CString(_T("")),
202 const CString
& effect
= CString(_T("")),
203 const CRect
& marginRect
= CRect(0,0,0,0), int layer
= 0, int readorder
= -1);
204 //void Add(CStringW str, bool fUnicode, int start, int end, const CString& style, const CString& actor, const CString& effect, const CRect& marginRect, int layer, int readorder);
205 //void Add(CStringW str, bool fUnicode, int start, int end);
207 //add an STSEntry obj to the array
208 //NO addition segments added
209 //remember to call sort when all STSEntrys are ready
210 void AddSTSEntryOnly(CStringW str
, bool fUnicode
, int start
, int end
, CString style
= _T("Default"), const CString
& actor
= _T(""), const CString
& effect
= _T(""), const CRect
& marginRect
= CRect(0,0,0,0), int layer
= 0, int readorder
= -1);
212 STSStyle
* CreateDefaultStyle(int CharSet
);
213 void ChangeUnknownStylesToDefault();
214 void AddStyle(CString name
, STSStyle
* style
); // style will be stored and freed in Empty() later
215 bool CopyStyles(const CSTSStyleMap
& styles
, bool fAppend
= false);
217 bool SetDefaultStyle(STSStyle
& s
);
218 bool GetDefaultStyle(STSStyle
& s
);
220 void ConvertToTimeBased(double fps
);
221 void ConvertToFrameBased(double fps
);
223 int TranslateStart(int i
, double fps
);
224 int TranslateEnd(int i
, double fps
);
225 int SearchSub(int t
, double fps
);
227 int TranslateSegmentStart(int i
, double fps
);
228 int TranslateSegmentEnd(int i
, double fps
);
229 void TranslateSegmentStartEnd(int i
, double fps
, /*out*/int& start
, /*out*/int& end
);
231 //find the first STSSegment with stop time > @t
232 //@iSegment: return the index, 0 based, of the STSSegment found, return STSSegments count if all STSSegments' stop time are NOT bigger than @t
233 //@nSegment: return the STSSegments count
234 //@return: the ptr to the STSSegment found, return NULL if such STSSegment not exist
235 const STSSegment
* SearchSubs(int t
, double fps
, /*[out]*/ int* iSegment
= NULL
, int* nSegments
= NULL
);
237 //find STSSegment with a duration containing @t, i.e. with a start time <= @t and a stop time > @t
238 //@iSegment: return the index, 0 based, of the STSSegment found. Return -1 if such STSSegment not exist
239 //@nSegment: return the STSSegments count
240 //@return: the ptr to the STSSegment found, return NULL if such STSSegment not exist
241 STSSegment
* SearchSubs2(int t
, double fps
, /*[out]*/ int* iSegment
=NULL
, int* nSegments
=NULL
);
243 const STSSegment
* GetSegment(int iSegment
) {return iSegment
>= 0 && iSegment
< (int)m_segments
.GetCount() ? &m_segments
[iSegment
] : NULL
;}
245 STSStyle
* GetStyle(int i
);
246 bool GetStyle(int i
, STSStyle
* const stss
);
247 int GetCharSet(int i
);
248 bool IsEntryUnicode(int i
);
249 void ConvertUnicode(int i
, bool fUnicode
);
251 CStringA
GetStrA(int i
, bool fSSA
= false);
252 CStringW
GetStrW(int i
, bool fSSA
= false);
253 CStringW
GetStrWA(int i
, bool fSSA
= false);
256 #define GetStr GetStrW
258 #define GetStr GetStrA
261 void SetStr(int i
, CStringA str
, bool fUnicode
/* ignored */);
262 void SetStr(int i
, CStringW str
, bool fUnicode
);
264 friend bool OpenMicroDVD(CTextFile
* file
, CSimpleTextSubtitle
& ret
, int CharSet
);
267 extern BYTE CharSetList
[];
268 extern TCHAR
* CharSetNames
[];
269 extern int CharSetLen
;
271 class CHtmlColorMap
: public CAtlMap
<CString
, DWORD
, CStringElementTraits
<CString
> > {public: CHtmlColorMap();};
272 extern CHtmlColorMap g_colors
;