forked from samiamwork/Movist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSubtitle.m
executable file
·374 lines (333 loc) · 11.7 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// 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"
@interface MSubtitleString : NSObject
{
NSMutableAttributedString* _string; // mutable for changing font-size
float _beginTime;
float _endTime;
}
- (id)initWithString:(NSMutableAttributedString*)s
beginTime:(float)beginTime endTime:(float)endTime;
#pragma mark -
- (NSMutableAttributedString*)string;
- (float)beginTime;
- (float)endTime;
- (void)setString:(NSMutableAttributedString*)string;
- (void)setBeginTime:(float)time;
- (void)setEndTime:(float)time;
@end
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
@implementation MSubtitleString
- (id)initWithString:(NSMutableAttributedString*)s
beginTime:(float)beginTime endTime:(float)endTime
{
//TRACE(@"%s \"%@\" (%g ~ %g)", __PRETTY_FUNCTION__, [s string], beginTime, endTime);
if (self = [super init]) {
_string = [s retain];
_beginTime = beginTime;
_endTime = endTime;
}
return self;
}
- (void)dealloc
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[_string release];
[super dealloc];
}
- (void)setString:(NSMutableAttributedString*)string
{
//TRACE(@"%s \"%@\"", __PRETTY_FUNCTION__, [string string]);
[string retain], [_string release], _string = string;
}
- (void)setBeginTime:(float)time
{
//TRACE(@"%s %g", __PRETTY_FUNCTION__, time);
_beginTime = time;
}
- (void)setEndTime:(float)time
{
//TRACE(@"%s %g", __PRETTY_FUNCTION__, time);
_endTime = time;
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
- (NSMutableAttributedString*)string { return _string; }
- (float)beginTime { return _beginTime; }
- (float)endTime { return _endTime; }
@end
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
@implementation MSubtitle
+ (NSArray*)fileExtensions
{
static NSArray* exts = nil;
if (!exts) {
exts = [[NSApp supportedFileExtensionsWithPrefix:@"Subtitle-"] retain];
}
//TRACE(@"exts=%@", [exts retainCount], exts);
return exts; // don't send autorelease. it should be alive forever.
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
- (id)initWithURL:(NSURL*)url type:(NSString*)type
{
//TRACE(@"%s %@", __PRETTY_FUNCTION__, type);
if (self = [super init]) {
_url = [url retain];
_type = [type retain];
_name = [NSLocalizedString(@"Unnamed Subtitle", nil) retain];
_enabled = TRUE;
_strings = [[NSMutableArray alloc] init];
_lastSearchedIndex = -1; // for initial comparison
_emptyString = [[NSMutableAttributedString alloc] initWithString:@"" attributes:nil];
}
return self;
}
- (void)dealloc
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
[_strings removeAllObjects];
[_strings release];
[_name release];
[_type release];
[_emptyString release];
[super dealloc];
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
- (NSURL*)url { return _url; }
- (NSString*)type { return _type; }
- (NSString*)name { return _name; }
- (void)setName:(NSString*)name
{
//TRACE(@"%s \"%@\"", __PRETTY_FUNCTION__, name);
[name retain], [_name release], _name = name;
}
- (BOOL)isEmpty { return ([_strings count] == 0); }
- (float)beginTime { return [self isEmpty] ? 0 : [[_strings objectAtIndex:0] beginTime]; }
- (float)endTime { return [self isEmpty] ? 0 : [[_strings lastObject] endTime]; }
- (BOOL)isEnabled { return _enabled; }
- (void)setEnabled:(BOOL)enabled { _enabled = enabled; }
- (void)addString:(NSMutableAttributedString*)string time:(float)time
{
//TRACE(@"%s \"%@\" %g", __PRETTY_FUNCTION__, [string string], time);
int index;
MSubtitleString* ss;
for (index = [_strings count] - 1; 0 <= index; index--) {
ss = [_strings objectAtIndex:index];
if ([ss beginTime] < time) {
if ([ss endTime] < 0.0 || time < [ss endTime]) {
[ss setEndTime:time];
//TRACE(@"add subtitle: [%g~%g] \"%@\"",
// [ss beginTime], [ss endTime], [[ss string] string]);
}
break;
}
else if ([ss beginTime] == time) {
[[ss string] appendAttributedString:string];
return;
}
}
if (0 < [string length]) {
ss = [[MSubtitleString alloc] initWithString:string
beginTime:time endTime:-1.0];
[_strings insertObject:ss atIndex:index + 1];
}
}
- (void)addString:(NSMutableAttributedString*)string
beginTime:(float)beginTime endTime:(float)endTime
{
//TRACE(@"%s \"%@\" %g", __PRETTY_FUNCTION__, [string string], time);
int index;
MSubtitleString* ss;
for (index = [_strings count] - 1; 0 <= index; index--) {
ss = [_strings objectAtIndex:index];
if ([ss beginTime] <= beginTime) {
break;
}
}
if (index < 0) {
if (0 < [_strings count]) {
ss = [_strings objectAtIndex:0];
if ([ss beginTime] < endTime) {
NSAttributedString* as = [[NSAttributedString alloc] initWithString:@"\n"];
[[ss string] appendAttributedString:[as autorelease]];
[[ss string] appendAttributedString:string];
endTime = [ss beginTime];
}
}
ss = [[MSubtitleString alloc] initWithString:string
beginTime:beginTime endTime:endTime];
[_strings insertObject:ss atIndex:0];
}
else if ([ss endTime] <= beginTime) {
ss = [[MSubtitleString alloc] initWithString:string
beginTime:beginTime endTime:endTime];
[_strings insertObject:ss atIndex:index + 1];
}
else if ([ss beginTime] == beginTime) {
if (endTime == [ss endTime]) {
NSAttributedString* as = [[NSAttributedString alloc] initWithString:@"\n"];
[[ss string] appendAttributedString:[as autorelease]];
[[ss string] appendAttributedString:string];
}
else if (endTime < [ss endTime]) {
[ss setBeginTime:endTime];
NSMutableAttributedString* mas = [[[ss string] mutableCopy] autorelease];
NSAttributedString* as = [[NSAttributedString alloc] initWithString:@"\n"];
[mas appendAttributedString:[as autorelease]];
[mas appendAttributedString:string];
ss = [[MSubtitleString alloc] initWithString:mas
beginTime:beginTime endTime:endTime];
[_strings insertObject:ss atIndex:index];
}
else {
NSAttributedString* as = [[NSAttributedString alloc] initWithString:@"\n"];
[[ss string] appendAttributedString:[as autorelease]];
[[ss string] appendAttributedString:string];
[self addString:string beginTime:[ss endTime] endTime:endTime];
}
}
else if ([ss beginTime] < beginTime) {
float bt = [ss beginTime];
[ss setBeginTime:beginTime];
NSMutableAttributedString* mas = [[ss string] copy];
ss = [[MSubtitleString alloc] initWithString:mas
beginTime:bt endTime:beginTime];
[_strings insertObject:ss atIndex:index];
[self addString:string beginTime:beginTime endTime:endTime];
}
}
- (void)checkEndTimes
{
MSubtitleString* s;
int i, count = [_strings count];
for (i = 0; i < count; i++) {
s = [_strings objectAtIndex:i];
if ([s endTime] < 0.0) {
if (i < count - 1) {
[s setEndTime:[[_strings objectAtIndex:i + 1] beginTime]];
}
else {
[s setEndTime:[s beginTime] + 5.0];
}
}
}
}
- (NSMutableAttributedString*)stringAtTime:(float)time
{
//TRACE(@"%s %g", __PRETTY_FUNCTION__, time);
int index = (_lastSearchedIndex < 0) ? 0 : _lastSearchedIndex;
if (index < 0 || [_strings count] <= index) {
//TRACE(@"%s(\"%@\")[%.03f]: <none>", __PRETTY_FUNCTION__, _name, time);
_lastSearchedIndex = -1;
return nil;
}
MSubtitleString* ss = (MSubtitleString*)[_strings objectAtIndex:index];
if (time < [ss beginTime]) {
while (0 < index--) { // find in previous strings
ss = (MSubtitleString*)[_strings objectAtIndex:index];
if ([ss beginTime] <= time) {
break;
}
}
if (0 <= index && time < [ss endTime]) {
//TRACE(@"%s(\"%@\")[%.03f:%d=>%d]:\"%@\"", __PRETTY_FUNCTION__, _name,
// time, _lastSearchedIndex, index, [[ss string] string]);
_lastSearchedIndex = index;
return [ss string];
}
}
else if ([ss endTime] < time) {
int maxIndex = [_strings count] - 1;
while (index++ < maxIndex) { // find in next strings
ss = (MSubtitleString*)[_strings objectAtIndex:index];
if (time < [ss endTime]) {
break;
}
}
if (index <= maxIndex && [ss beginTime] <= time) {
//TRACE(@"%s(\"%@\")[%.03f:%d=>%d]:\"%@\"", __PRETTY_FUNCTION__, _name,
// time, _lastSearchedIndex, index, [[ss string] string]);
_lastSearchedIndex = index;
return [ss string];
}
}
else {
//TRACE(@"%s(\"%@\")[%.03f:%d=>%d]:\"%@\"", __PRETTY_FUNCTION__, _name,
// time, _lastSearchedIndex, index, [[ss string] string]);
_lastSearchedIndex = index;
return [ss string];
}
// string not found
//TRACE(@"%s(\"%@\")[%.03f]: <none>", __PRETTY_FUNCTION__, _name, time);
_lastSearchedIndex = -1;
return _emptyString;
}
- (void)clearCache
{
//TRACE(@"%s", __PRETTY_FUNCTION__);
_lastSearchedIndex = -1;
}
////////////////////////////////////////////////////////////////////////////////
- (float)nearestSubtitleTime:(float)time nextNeighbor:(BOOL)nextNeighbor
{
MSubtitleString* s;
int i, count = [_strings count];
for (i = 0; i < count; i++) {
s = [_strings objectAtIndex:i];
if (time < [s beginTime]) {
if (nextNeighbor) {
// i
}
else if (0 < i) {
i--;
}
break;
}
else if (time < [s endTime]) {
if (nextNeighbor) {
if (i < count - 1) {
i++;
}
}
else if (0 < i) {
i--;
}
break;
}
}
s = (i < count) ? [_strings objectAtIndex:i] : [_strings lastObject];
return [s beginTime] + 0.1; // add margin to ensure to display subtitle
}
- (float)prevSubtitleTime:(float)time
{
return [self nearestSubtitleTime:time nextNeighbor:FALSE];
}
- (float)nextSubtitleTime:(float)time
{
return [self nearestSubtitleTime:time nextNeighbor:TRUE];
}
@end