Fixed some regressions in the ssa converter.
This commit is contained in:
2 files changed
+68
-39
No files matched your search
+43
-19
@@ -68,8 +68,25 @@ typedef NS_ENUM(NSUInteger, MP42SSATokenType) {
|
||||
{
|
||||
NSMutableString *result = [NSMutableString string];
|
||||
NSArray<MP42SSAToken *> *tokens = tokenizer(line.text);
|
||||
NSUInteger textLength = 0;
|
||||
|
||||
BOOL drawingMode = NO;
|
||||
BOOL bold = NO;
|
||||
BOOL italic = NO;
|
||||
BOOL underlined = NO;
|
||||
|
||||
if (line.style.bold) {
|
||||
[result insertString:@"<b>" atIndex:0];
|
||||
bold = YES;
|
||||
}
|
||||
if (line.style.underline) {
|
||||
[result insertString:@"<u>" atIndex:0];
|
||||
underlined = YES;
|
||||
}
|
||||
if (line.style.italic) {
|
||||
[result insertString:@"<i>" atIndex:0];
|
||||
italic = YES;
|
||||
}
|
||||
|
||||
for (MP42SSAToken *token in tokens) {
|
||||
NSString *textToAppend = nil;
|
||||
@@ -77,23 +94,29 @@ typedef NS_ENUM(NSUInteger, MP42SSATokenType) {
|
||||
if (token->_type == MP42SSATokenTypeText) {
|
||||
textToAppend = token->_text;
|
||||
}
|
||||
else if (token->_type == MP42SSATokenTypeBoldOpen) {
|
||||
else if (token->_type == MP42SSATokenTypeBoldOpen && bold == NO) {
|
||||
textToAppend = @"<b>";
|
||||
bold = YES;
|
||||
}
|
||||
else if (token->_type == MP42SSATokenTypeBoldClose) {
|
||||
else if (token->_type == MP42SSATokenTypeBoldClose && bold == YES) {
|
||||
textToAppend = @"</b>";
|
||||
bold = NO;
|
||||
}
|
||||
else if (token->_type == MP42SSATokenTypeItalicOpen) {
|
||||
else if (token->_type == MP42SSATokenTypeItalicOpen && italic == NO) {
|
||||
textToAppend = @"<i>";
|
||||
italic = YES;
|
||||
}
|
||||
else if (token->_type == MP42SSATokenTypeItalicClose) {
|
||||
else if (token->_type == MP42SSATokenTypeItalicClose && italic == YES) {
|
||||
textToAppend = @"</i>";
|
||||
italic = NO;
|
||||
}
|
||||
else if (token->_type == MP42SSATokenTypeUnderlinedOpen) {
|
||||
else if (token->_type == MP42SSATokenTypeUnderlinedOpen && underlined == NO) {
|
||||
textToAppend = @"<u>";
|
||||
underlined = YES;
|
||||
}
|
||||
else if (token->_type == MP42SSATokenTypeUnderlinedClose) {
|
||||
else if (token->_type == MP42SSATokenTypeUnderlinedClose && underlined == YES) {
|
||||
textToAppend = @"</u>";
|
||||
underlined = NO;
|
||||
}
|
||||
else if (token->_type == MP42SSATokenTypeDrawingOpen) {
|
||||
drawingMode = YES;
|
||||
@@ -104,25 +127,26 @@ typedef NS_ENUM(NSUInteger, MP42SSATokenType) {
|
||||
|
||||
if (textToAppend && drawingMode == NO) {
|
||||
[result appendString:textToAppend];
|
||||
if (token->_type == MP42SSATokenTypeText) {
|
||||
textLength += textToAppend.length;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bold) {
|
||||
[result appendString:@"</b>"];
|
||||
}
|
||||
if (underlined) {
|
||||
[result appendString:@"</u>"];
|
||||
}
|
||||
if (italic) {
|
||||
[result appendString:@"</i>"];
|
||||
}
|
||||
|
||||
[result replaceOccurrencesOfString:@"\\N" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, result.length)];
|
||||
[result replaceOccurrencesOfString:@"\\n" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, result.length)];
|
||||
|
||||
if (result.length) {
|
||||
if (line.style.bold) {
|
||||
[result insertString:@"<b>" atIndex:0];
|
||||
}
|
||||
if (line.style.underline) {
|
||||
[result insertString:@"<u>" atIndex:0];
|
||||
}
|
||||
if (line.style.italic) {
|
||||
[result insertString:@"<i>" atIndex:0];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return textLength ? result : @"";
|
||||
}
|
||||
|
||||
static inline NSArray<MP42SSAToken *> *tokenizer(NSString *line)
|
||||
|
||||
+25
-20
@@ -56,27 +56,29 @@
|
||||
break;
|
||||
}
|
||||
|
||||
NSString *text = [[NSString alloc] initWithBytes:sampleBuffer->data
|
||||
length:sampleBuffer->size
|
||||
encoding:NSUTF8StringEncoding];
|
||||
if (sampleBuffer->size > 1) {
|
||||
NSString *text = [[NSString alloc] initWithBytes:sampleBuffer->data
|
||||
length:sampleBuffer->size - 1
|
||||
encoding:NSUTF8StringEncoding];
|
||||
|
||||
if (_converter && text.length) {
|
||||
if (_converter) {
|
||||
MP42SSALine *SSAline = [_parser addLine:text];
|
||||
if (SSAline) {
|
||||
text = [_converter convertLine:SSAline];
|
||||
}
|
||||
else {
|
||||
text = nil;
|
||||
if (_converter && text.length) {
|
||||
if (_converter) {
|
||||
MP42SSALine *SSAline = [_parser addLine:text];
|
||||
if (SSAline) {
|
||||
text = [_converter convertLine:SSAline];
|
||||
}
|
||||
else {
|
||||
text = nil;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (text.length) {
|
||||
MP42SubLine *sl = [[MP42SubLine alloc] initWithLine:text
|
||||
start:sampleBuffer->decodeTimestamp
|
||||
end:sampleBuffer->decodeTimestamp + sampleBuffer->duration];
|
||||
[_ss addLine:sl];
|
||||
if (text.length) {
|
||||
MP42SubLine *sl = [[MP42SubLine alloc] initWithLine:text
|
||||
start:sampleBuffer->decodeTimestamp
|
||||
end:sampleBuffer->decodeTimestamp + sampleBuffer->duration];
|
||||
[_ss addLine:sl];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,16 +91,19 @@
|
||||
if ((self = [super init])) {
|
||||
MP42SubtitleCodecType format = track.format;
|
||||
|
||||
_ss = [[MP42SubSerializer alloc] init];
|
||||
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
||||
_done = dispatch_semaphore_create(0);
|
||||
|
||||
if (format == kMP42SubtitleCodecType_SSA) {
|
||||
NSData *cookie = [track.importer magicCookieForTrack:track];
|
||||
NSString *cookieString = [[NSString alloc] initWithData:cookie encoding:NSUTF8StringEncoding];
|
||||
_parser = [[MP42SSAParser alloc] initWithMKVHeader:cookieString];
|
||||
_converter = [[MP42SSAConverter alloc] initWithParser:_parser];
|
||||
|
||||
[_ss setSSA:YES];
|
||||
}
|
||||
|
||||
_ss = [[MP42SubSerializer alloc] init];
|
||||
_inputSamplesBuffer = [[MP42Fifo alloc] initWithCapacity:100];
|
||||
_done = dispatch_semaphore_create(0);
|
||||
_trackID = track.sourceId;
|
||||
|
||||
_decoderThread = [[NSThread alloc] initWithTarget:self selector:@selector(TextConverterThreadMainRoutine) object:nil];
|
||||
|
||||
Reference in new issue
Block a user