| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 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 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "subtitleformat.h" |
| 16 | #include <QMap> |
| 17 | |
| 18 | QVector<SubToken> SubtitleFormat::decodeTokenStream(QString tokenStream) const { |
| 19 | QVector<SubToken> tokens; |
| 20 | SubToken tok; |
| 21 | |
| 22 | QMap<QString, SubTokenType> tokenTypes = { |
| 23 | {"{y:b}", STT_BOLD}, {"{b}", STT_BOLD}, |
| 24 | {"<b>", STT_BOLD}, {"{y:i}", STT_ITALIC}, |
| 25 | {"{i}", STT_ITALIC}, {"<i>", STT_ITALIC}, |
| 26 | {"{y:u}", STT_UNDERLINE}, {"{u}", STT_UNDERLINE}, |
| 27 | {"<u>", STT_UNDERLINE}, {"{/y:b}", STT_BOLD_END}, |
| 28 | {"{/b}", STT_BOLD_END}, {"</b>", STT_BOLD_END}, |
| 29 | {"{/y:i}", STT_ITALIC_END}, {"{/i}", STT_ITALIC_END}, |
| 30 | {"</i>", STT_ITALIC_END}, {"{/y:u}", STT_UNDERLINE_END}, |
| 31 | {"{/u}", STT_UNDERLINE_END}, {"</u>", STT_UNDERLINE_END}, |
| 32 | {"{/c}", STT_FONTCOLOR_END}, {"</font>", STT_FONTCOLOR_END}, |
| 33 | {"|", STT_NEWLINE}, {"\r\n", STT_NEWLINE}, |
| 34 | {"\n", STT_NEWLINE}}; |
| 35 | |
| 36 | QString wordBuff; |
| 37 | |
| 38 | while (!tokenStream.isEmpty()) { |
| 39 | tok.payload.clear(); |
| 40 | |
| 41 | bool matched = false; |
| 42 | |
| 43 | foreach (QString token, tokenTypes.keys()) { |
| 44 | if (tokenStream.startsWith(token, Qt::CaseInsensitive)) { |
| 45 | tok.type = tokenTypes[token]; |
| 46 | tokenStream.remove(0, token.size()); |
| 47 | matched = true; |
| 48 | break; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | if (!matched) { |
| 53 | QRegExp colorR1("^\\{c:(.*)\\}"); |
| 54 | colorR1.setPatternSyntax(QRegExp::RegExp2); |
| 55 | colorR1.setCaseSensitivity(Qt::CaseInsensitive); |
| 56 | |
| 57 | QRegExp colorR2("^<font color=(.*)>"); |
| 58 | colorR1.setPatternSyntax(QRegExp::RegExp2); |
| 59 | colorR1.setCaseSensitivity(Qt::CaseInsensitive); |
| 60 | |
| 61 | if (colorR1.indexIn(tokenStream) == 0) { |
| 62 | tok.type = STT_FONTCOLOR; |
| 63 | tok.payload = parseColor(colorR1.cap(1)); |
| 64 | tokenStream.remove(0, colorR1.cap(0).size()); |
| 65 | matched = true; |
| 66 | } else if (colorR2.indexIn(tokenStream) == 0) { |
| 67 | tok.type = STT_FONTCOLOR; |
| 68 | tok.payload = parseColor(colorR2.cap(1)); |
| 69 | tokenStream.remove(0, colorR2.cap(0).size()); |
| 70 | matched = true; |
| 71 | } else if (tokenStream[0] == '/' && wordBuff.isEmpty()) { |
| 72 | tok.type = STT_ITALIC; |
| 73 | tokenStream.remove(0, 1); |
| 74 | matched = true; |
| 75 | } else if (tokenStream[0].isSpace()) { |
| 76 | while (!tokenStream.isEmpty() && tokenStream[0].isSpace()) |
| 77 | tokenStream.remove(0, 1); |
| 78 | tok.type = STT_WS; |
| 79 | matched = true; |
| 80 | } else { |
| 81 | wordBuff += tokenStream[0]; |
| 82 | tokenStream.remove(0, 1); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if (matched) { |
| 87 | if (!wordBuff.isEmpty()) { |
| 88 | SubToken wordTok; |
| 89 | wordTok.type = STT_WORD; |
| 90 | wordTok.payload = wordBuff; |
| 91 | tokens.push_back(wordTok); |
| 92 | wordBuff.clear(); |
| 93 | } |
| 94 | tokens.push_back(tok); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if (!wordBuff.isEmpty()) { |
| 99 | SubToken wordTok; |
| 100 | wordTok.type = STT_WORD; |
| 101 | wordTok.payload = wordBuff; |
| 102 | tokens.push_back(wordTok); |
| 103 | } |
| 104 | |
| 105 | if (!tokens.isEmpty() && tokens[0].type == STT_WS) { |
| 106 | while (tokens[0].type == STT_WS) tokens.pop_front(); |
| 107 | } |
| 108 | |
| 109 | return tokens; |
| 110 | } |
| 111 | |
| 112 | QString SubtitleFormat::parseColor(QString colorString) const { |
| 113 | return colorString.replace('"', "").replace('#', "").replace('$', ""); |
| 114 | } |