-
Notifications
You must be signed in to change notification settings - Fork 54
/
MSubtitle.m
339 lines (310 loc) · 11.3 KB
/
MSubtitle.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//
// Movist
//
// Copyright 2006 ~ 2008 Yong-Hoe Kim. All rights reserved.
// Yong-Hoe Kim <[email protected]>
//
// This file is part of Movist.
//
// Movist is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Movist is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#import "MSubtitle.h"
#import "MSubtitleItem.h"
@implementation MSubtitle
+ (NSArray*)fileExtensions
{
static NSArray* exts = nil;
if (exts == nil) {
exts = [[NSApp supportedFileExtensionsWithPrefix:@"Subtitle-"] retain];
}
return exts; // don't send autorelease. it should be alive forever.
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
- (id)initWithURL:(NSURL*)url
{
//TRACE(@"%s %@", __PRETTY_FUNCTION__, type);
if ((self = [super init])) {
_url = [url retain];
_name = [NSLocalizedString(@"Unnamed", nil) retain];
_language = [NSLocalizedString(@"Unknown Language", nil) retain];
_embedded = FALSE;
_enabled = FALSE;
_items = [[NSMutableArray alloc] init];
_indexCache = -1; // for initial comparison
_renderConditionLock = [[NSConditionLock alloc] initWithCondition:0];
_forwardRenderInterval = 30.0;
_backwardRenderInterval = 30.0;
_seekIndex = 0;
_playIndex = 0;
[self initRenderInfo];
}
return self;
}
- (void)dealloc
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[self quitRenderThread];
[_renderConditionLock release];
[_items release];
[_trackName release];
[_extraInfo release];
[_language release];
[_name release];
[_type release];
[super dealloc];
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
- (NSURL*)url { return _url; }
- (NSString*)type { return _type; }
- (NSString*)name { return _name; }
- (NSString*)language { return _language; }
- (NSString*)extraInfo { return _extraInfo; }
- (NSString*)trackName { return _trackName; }
- (BOOL)isEmbedded { return _embedded; }
- (void)setType:(NSString*)s { [s retain], [_type release], _type = s; }
- (void)setName:(NSString*)s { [s retain], [_name release], _name = s; }
- (void)setLanguage:(NSString*)s { [s retain], [_language release], _language = s; }
- (void)setExtraInfo:(NSString*)s { [s retain], [_extraInfo release], _extraInfo = s; }
- (void)setTrackName:(NSString*)s { [s retain], [_trackName release], _trackName = s; }
- (void)setEmbedded:(BOOL)embedded { _embedded = embedded; }
- (NSString*)summary
{
NSString* s = [NSString stringWithFormat:@"%@, %@ (%@)", [self type], _name, _language];
return (_extraInfo) ? [s stringByAppendingFormat:@", %@", _extraInfo] : s;
}
- (NSString*)UIName
{
if (_trackName && _name && _language)
return [NSString stringWithFormat:@"%@ - %@ (%@)", _trackName, _name, _language];
else if (_trackName && _language)
return [NSString stringWithFormat:@"%@ (%@)", _trackName, _language];
else if (_trackName && _name)
return [NSString stringWithFormat:@"%@ - %@", _trackName, _name];
else if (_name && _language)
return [NSString stringWithFormat:@"%@ (%@)", _name, _language];
else if (_trackName)
return _trackName;
else if (_name)
return _name;
else if (_language)
return _language;
else
return NSLocalizedString(@"Unknown Subtitle", nil);
}
- (BOOL)checkDefaultLanguage:(NSArray*)defaultLangIDs
{
#define CHECK_SUBSTRING(field, identifier) \
(field && [field rangeOfString:identifier \
options:NSCaseInsensitiveSearch].location != NSNotFound)
NSString* identifier;
NSEnumerator* e = [defaultLangIDs objectEnumerator];
while ((identifier = [e nextObject])) {
if (CHECK_SUBSTRING(_name, identifier) ||
CHECK_SUBSTRING(_language, identifier) ||
CHECK_SUBSTRING(_extraInfo, identifier)) {
return TRUE;
}
}
return FALSE;
}
- (BOOL)isEmpty { return ([_items count] == 0); }
- (float)beginTime { return [self isEmpty] ? 0 : [(MSubtitleItem*)[_items objectAtIndex:0] beginTime]; }
- (float)endTime { return [self isEmpty] ? 0 : [[_items lastObject] endTime]; }
- (BOOL)isEnabled { return _enabled; }
- (void)setEnabled:(BOOL)enabled
{
if (_enabled != enabled) {
_enabled = enabled;
_indexCache = -1;
[[NSNotificationCenter defaultCenter]
postNotificationName:MSubtitleEnableChangeNotification object:self];
}
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark loading
- (void)addString:(NSMutableAttributedString*)string time:(float)time
{
int index;
MSubtitleItem* item;
for (index = [_items count] - 1; 0 <= index; index--) {
item = [_items objectAtIndex:index];
if ([item beginTime] < time) {
if ([item endTime] < 0.0 || time < [item endTime]) {
[item setEndTime:time];
}
break;
}
else if ([item beginTime] == time) {
[item appendString:string];
return;
}
}
if (0 < [string length]) {
item = [MSubtitleItem itemWithString:string beginTime:time endTime:-1.0];
[_items insertObject:item atIndex:index + 1];
}
}
- (void)addString:(NSMutableAttributedString*)string
beginTime:(float)beginTime endTime:(float)endTime
{
//TRACE(@"subtitle[\"%@\"]: [%.1f~%.1f]:\"%@\"",
// [self name], beginTime, endTime, [string string]);
#define NEW_LINE [[[NSAttributedString alloc] initWithString:@"\n"] autorelease]
int index;
MSubtitleItem* item;
for (index = [_items count] - 1; 0 <= index; index--) {
item = [_items objectAtIndex:index];
if ([item beginTime] <= beginTime) {
break;
}
}
if (index < 0) {
if (0 < [_items count]) {
item = [_items objectAtIndex:0];
if ([item beginTime] < endTime) {
[item appendString:NEW_LINE];
[item appendString:string];
endTime = [item beginTime];
}
}
item = [MSubtitleItem itemWithString:string beginTime:beginTime endTime:endTime];
[_items insertObject:item atIndex:0];
}
else if ([item endTime] <= beginTime) {
item = [MSubtitleItem itemWithString:string beginTime:beginTime endTime:endTime];
[_items insertObject:item atIndex:index + 1];
}
else if ([item beginTime] == beginTime) {
if (endTime == [item endTime]) {
[item appendString:NEW_LINE];
[item appendString:string];
}
else if (endTime < [item endTime]) {
[item setBeginTime:endTime];
NSAttributedString* s = [[item string] copy];
item = [MSubtitleItem itemWithString:[s autorelease]
beginTime:beginTime endTime:endTime];
[item appendString:NEW_LINE];
[item appendString:string];
[_items insertObject:item atIndex:index];
}
else {
[item appendString:NEW_LINE];
[item appendString:string];
[self addString:string beginTime:[item endTime] endTime:endTime];
}
}
else if ([item beginTime] < beginTime) {
float bt = [item beginTime];
[item setBeginTime:beginTime];
NSAttributedString* s = [[item string] copy];
item = [MSubtitleItem itemWithString:[s autorelease]
beginTime:bt endTime:beginTime];
[_items insertObject:item atIndex:index];
[self addString:string beginTime:beginTime endTime:endTime];
}
}
- (void)addImage:(NSImage*)image
beginTime:(float)beginTime endTime:(float)endTime
{
[_items addObject:
[MSubtitleItem itemWithImage:image beginTime:beginTime endTime:endTime]];
}
- (void)checkEndTimes
{
MSubtitleItem* item;
int i, count = [_items count];
for (i = 0; i < count; i++) {
item = [_items objectAtIndex:i];
if ([item endTime] < 0.0) {
if (i < count - 1) {
[item setEndTime:[(MSubtitleItem*)[_items objectAtIndex:i + 1] beginTime]];
}
else {
[item setEndTime:[item beginTime] + 5.0];
}
}
}
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark seeking
- (MSubtitleItem*)itemAtTime:(float)time direction:(int)direction
{
int index = [self indexAtTime:time direction:direction];
return (0 <= index) ? [_items objectAtIndex:index] : nil;
}
- (int)indexAtTime:(float)time direction:(int)direction
{
int index = MAX(0, _indexCache);
if ([_items count] <= index) {
//TRACE(@"%s(\"%@\")[%.03f]: <none>", __PRETTY_FUNCTION__, _name, time);
return _indexCache = (direction < 0) ? [_items count] - 1 : -1;
}
MSubtitleItem* si = (MSubtitleItem*)[_items objectAtIndex:index];
if (time < [si beginTime]) {
while (0 <= --index) { // find in previous items
si = [_items objectAtIndex:index];
if ([si beginTime] <= time) {
break;
}
}
if (index < 0) {
//TRACE(@"%s(\"%@\")[%.03f]: <none>", __PRETTY_FUNCTION__, _name, time);
return _indexCache = (0 < direction) ? 0 : -1;
}
else if ([si endTime] <= time) {
//TRACE(@"%s(\"%@\")[%.03f]: <none>", __PRETTY_FUNCTION__, _name, time);
return _indexCache = (0 < direction) ? (index + 1) : (direction < 0) ? index : -1;
}
}
else if ([si endTime] < time) {
int maxIndex = [_items count] - 1;
while (++index <= maxIndex) { // find in next items
si = [_items objectAtIndex:index];
if (time < [si endTime]) {
break;
}
}
if (maxIndex < index) {
//TRACE(@"%s(\"%@\")[%.03f]: <none>", __PRETTY_FUNCTION__, _name, time);
return _indexCache = (direction < 0) ? maxIndex : -1;
}
else if (time < [si beginTime]) {
//TRACE(@"%s(\"%@\")[%.03f]: <none>", __PRETTY_FUNCTION__, _name, time);
return _indexCache = (direction < 0) ? (index - 1) : (0 < direction) ? index : -1;
}
}
//TRACE(@"%s(\"%@\")[%.03f:%d=>%d]:\"%@\"", __PRETTY_FUNCTION__, _name,
// time, _indexCache, index, [[si string] string]);
return _indexCache = index;
}
- (float)prevSubtitleTime:(float)time
{
int index = [self indexAtTime:time direction:-1]; // backward
if (index < 0) {
index = 0;
}
return [(MSubtitleItem*)[_items objectAtIndex:index] beginTime] + 0.1;
}
- (float)nextSubtitleTime:(float)time
{
int index = [self indexAtTime:time direction:+1]; // forward
if (index < 0) {
index = [_items count] - 1;
}
return [(MSubtitleItem*)[_items objectAtIndex:index] beginTime] + 0.1;
}
@end