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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/* HBSubtitlesTrack.m
This file is part of the HandBrake source code.
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License. */
#import "HBSubtitlesTrack.h"
#import "HBCodingUtilities.h"
#include "common.h"
#include "lang.h"
#define CHAR_CODE_DEFAULT_INDEX 11
static NSArray *charEncodingArray = nil;
static NSArray *_languagesArray = nil;
NSString *keySubTrackName = @"keySubTrackName";
NSString *keySubTrackLanguageIsoCode = @"keySubTrackLanguageIsoCode";
NSString *keySubTrackType = @"keySubTrackType";
NSString *keySubTrackSrtFileURL = @"keySubTrackSrtFileURL";
@interface HBSubtitlesTrack ()
@property (nonatomic, readwrite) BOOL validating;
@end
@implementation HBSubtitlesTrack
+ (void)initialize
{
if ([HBSubtitlesTrack class] == self)
{
// populate the charCodeArray.
charEncodingArray = @[@"ANSI_X3.4-1968", @"ANSI_X3.4-1986", @"ANSI_X3.4", @"ANSI_X3.110-1983", @"ANSI_X3.110",
@"ASCII", @"ECMA-114", @"ECMA-118", @"ECMA-128", @"ECMA-CYRILLIC", @"IEC_P27-1", @"ISO-8859-1",
@"ISO-8859-2", @"ISO-8859-3", @"ISO-8859-4", @"ISO-8859-5", @"ISO-8859-6", @"ISO-8859-7",
@"ISO-8859-8", @"ISO-8859-9", @"ISO-8859-9E", @"ISO-8859-10", @"ISO-8859-11", @"ISO-8859-13",
@"ISO-8859-14", @"ISO-8859-15", @"ISO-8859-16", @"UTF-7", @"UTF-8", @"UTF-16", @"UTF-16LE",
@"UTF-16BE", @"UTF-32", @"UTF-32LE", @"UTF-32BE"];
// populate the languages array
NSMutableArray *languages = [[NSMutableArray alloc] init];
for (const iso639_lang_t *lang = lang_get_next(NULL); lang != NULL; lang = lang_get_next(lang))
{
[languages addObject:@(lang->eng_name)];
}
_languagesArray = [languages copy];
}
}
- (instancetype)initWithTrackIdx:(NSUInteger)index
container:(int)container
dataSource:(id<HBTrackDataSource>)dataSource
delegate:(id<HBTrackDelegate>)delegate
{
self = [super init];
if (self)
{
_dataSource = dataSource;
_sourceTrackIdx = index;
_container = container;
[self validateSettings];
_delegate = delegate;
}
return self;
}
- (void)validateSettings
{
NSDictionary *sourceTrack = [_dataSource sourceTrackAtIndex:_sourceTrackIdx];
self.type = [sourceTrack[keySubTrackType] intValue];
if (!hb_subtitle_can_burn(_type))
{
// the source track cannot be burned in, so uncheck the widget
self.burnedIn = NO;
}
if (!hb_subtitle_can_force(_type))
{
// the source track does not support forced flags, so uncheck the widget
self.forcedOnly = NO;
}
if (!hb_subtitle_can_pass(self.type, self.container))
{
self.burnedIn = YES;
}
if (_sourceTrackIdx == 1)
{
self.forcedOnly = YES;
self.burnedIn = YES;
}
// check to see if we are an srt, in which case set our file path and source track type kvp's
if (_type == SRTSUB)
{
self.fileURL = [sourceTrack[keySubTrackSrtFileURL] copy];
self.isoLanguage = @"eng";
self.charCode = charEncodingArray[CHAR_CODE_DEFAULT_INDEX];
}
else
{
self.fileURL = nil;
self.isoLanguage = nil;
self.charCode = nil;
}
}
#pragma mark - Track properties
- (void)setSourceTrackIdx:(NSUInteger)sourceTrackIdx
{
if (sourceTrackIdx != _sourceTrackIdx)
{
[[self.undo prepareWithInvocationTarget:self] setSourceTrackIdx:_sourceTrackIdx];
}
NSUInteger oldIdx = _sourceTrackIdx;
_sourceTrackIdx = sourceTrackIdx;
if (!(self.undo.isUndoing || self.undo.isRedoing))
{
[self validateSettings];
if (oldIdx != sourceTrackIdx)
{
[self.delegate track:self didChangeSourceFrom:oldIdx];
}
}
}
- (void)setType:(int)type
{
if (type != _type)
{
[[self.undo prepareWithInvocationTarget:self] setType:_type];
}
_type = type;
}
- (void)setForcedOnly:(BOOL)forcedOnly
{
if (forcedOnly != _forcedOnly)
{
[[self.undo prepareWithInvocationTarget:self] setForcedOnly:_forcedOnly];
}
_forcedOnly = forcedOnly;
}
- (void)setBurnedIn:(BOOL)burnedIn
{
if (burnedIn != _burnedIn)
{
[[self.undo prepareWithInvocationTarget:self] setBurnedIn:_burnedIn];
}
_burnedIn = burnedIn;
if (!(self.undo.isUndoing || self.undo.isRedoing) || !self.validating)
{
self.validating = YES;
if (_burnedIn)
{
[self.delegate didSetBurnedInOption:self];
self.def = NO;
};
self.validating = NO;
}
}
- (BOOL)validateBurnedIn:(id *)ioValue error:(NSError * __autoreleasing *)outError
{
BOOL retval = YES;
if (nil != *ioValue)
{
BOOL value = [*ioValue boolValue];
if (value && [self.delegate canSetBurnedInOption:self] == NO)
{
*ioValue = @NO;
}
}
return retval;
}
- (void)setDef:(BOOL)def
{
if (def != _def)
{
[[self.undo prepareWithInvocationTarget:self] setDef:_def];
}
_def = def;
if (!(self.undo.isUndoing || self.undo.isRedoing) || !self.validating)
{
self.validating = YES;
if (_def)
{
[self.delegate didSetDefaultOption:self];
self.burnedIn = NO;
}
self.validating = NO;
}
}
#pragma mark - Srt only properties
- (void)setFileURL:(NSURL *)fileURL
{
if (fileURL != _fileURL)
{
[[self.undo prepareWithInvocationTarget:self] setFileURL:_fileURL];
}
_fileURL = [fileURL copy];
}
- (void)setIsoLanguage:(NSString *)isoLanguage
{
if (![isoLanguage isEqualToString:_isoLanguage])
{
[[self.undo prepareWithInvocationTarget:self] setIsoLanguage:_isoLanguage];
}
_isoLanguage = [isoLanguage copy];
}
- (void)setCharCode:(NSString *)charCode
{
if (![charCode isEqualToString:_charCode])
{
[[self.undo prepareWithInvocationTarget:self] setCharCode:_charCode];
}
_charCode = [charCode copy];
}
- (void)setOffset:(int)offset
{
if (offset != _offset)
{
[[self.undo prepareWithInvocationTarget:self] setOffset:_def];
}
_offset = offset;
}
#pragma mark -
- (NSArray *)sourceTracksArray
{
return [self.dataSource sourceTracksArray];
}
- (BOOL)isSrt
{
return self.type == SRTSUB;
}
- (BOOL)isEnabled
{
return self.sourceTrackIdx != 0;
}
- (BOOL)canPassthru
{
return (BOOL)hb_subtitle_can_pass(self.type, self.container) && self.isEnabled;
}
- (NSArray<NSString *> *)languages
{
return _languagesArray;
}
- (NSArray<NSString *> *)encodings
{
return charEncodingArray;
}
#pragma mark - KVO
+ (NSSet *)keyPathsForValuesAffectingValueForKey:(NSString *)key
{
NSSet *retval = nil;
if ([key isEqualToString: @"isSrt"])
{
retval = [NSSet setWithObjects: @"type", nil];
}
else if ([key isEqualToString: @"isEnabled"])
{
retval = [NSSet setWithObjects: @"sourceTrackIdx", nil];
}
else if ([key isEqualToString: @"canPassthru"])
{
retval = [NSSet setWithObjects: @"isEnabled", @"sourceTrackIdx", nil];
}
else
{
retval = [NSSet set];
}
return retval;
}
- (void)setNilValueForKey:(NSString *)key
{
if ([key isEqualToString:@"offset"])
{
[self setValue:@0 forKey:key];
}
}
#pragma mark - NSCopying
- (instancetype)copyWithZone:(NSZone *)zone
{
HBSubtitlesTrack *copy = [[[self class] alloc] init];
if (copy)
{
copy->_sourceTrackIdx = _sourceTrackIdx;
copy->_type = _type;
copy->_container = _container;
copy->_forcedOnly = _forcedOnly;
copy->_burnedIn = _burnedIn;
copy->_def = _def;
copy->_fileURL = [_fileURL copy];
copy->_isoLanguage = [_isoLanguage copy];
copy->_charCode = [_charCode copy];
copy->_offset = _offset;
}
return copy;
}
#pragma mark - NSCoding
+ (BOOL)supportsSecureCoding
{
return YES;
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeInt:1 forKey:@"HBSubtitlesTrackVersion"];
encodeInteger(_sourceTrackIdx);
encodeInt(_type);
encodeInt(_container);
encodeBool(_forcedOnly);
encodeBool(_burnedIn);
encodeBool(_def);
encodeObject(_fileURL);
encodeObject(_isoLanguage);
encodeObject(_charCode);
encodeInt(_offset);
}
- (instancetype)initWithCoder:(NSCoder *)decoder
{
self = [super init];
decodeInteger(_sourceTrackIdx);
decodeInt(_type);
decodeInt(_container);
decodeBool(_forcedOnly);
decodeBool(_burnedIn);
decodeBool(_def);
decodeObject(_fileURL, NSURL);
decodeObject(_isoLanguage, NSString);
decodeObject(_charCode, NSString);
decodeInt(_offset);
return self;
}
@end
#pragma mark - Value Trasformers
@implementation HBIsoLanguageTrasformer
+ (Class)transformedValueClass
{
return [NSString class];
}
- (id)transformedValue:(id)value
{
if ([value length])
{
iso639_lang_t *lang = lang_for_code2([value UTF8String]);
if (lang)
{
return @(lang->eng_name);
}
}
return nil;
}
+ (BOOL)allowsReverseTransformation
{
return YES;
}
- (id)reverseTransformedValue:(id)value
{
if ([value length])
{
iso639_lang_t *lang = lang_for_english([value UTF8String]);
if (lang)
{
return @(lang->iso639_2);
}
}
return nil;
}
@end
|