Ticket #2225: ext_subtitle_file_fixes.patch

File ext_subtitle_file_fixes.patch, 3.4 KB (added by Pekka Jääskeläinen <pekka.jaaskelainen@…>, 19 years ago)

fix

  • libs/libmythtv/tv_play.cpp

    Improvements and fixes to the external (text) subtitle file support:
    
    * Fix printing the loaded subtitle file to the log.
    * Fix loading of UTF8 subs: detect if the strings cannot be
      presented as valid UTF8 chars and fall back to Latin1 in
      that case (TODO: add user option to select the fall back
      encoding, as it's not possible to reliably detect the
      8-bit encoding)
    
    old new  
    70537053        baseName + "*.srt; " + baseName + "*.sub; " + baseName + "*.txt;");
    70547054
    70557055    bool found = false;
     7056    QString candidate = "";
    70567057    QStringList::const_iterator it = candidates.begin();
    70577058    for (; (it != candidates.end()) && !found; ++it)
    70587059    {
    7059         if (nvp->LoadExternalSubtitles(dirName + "/" + *it))
     7060        candidate = dirName + "/" + *it;
     7061        if (nvp->LoadExternalSubtitles(candidate));
    70607062            found = true;
    70617063    }
    70627064
    70637065    if (found)
    70647066    {
    70657067        VERBOSE(VB_PLAYBACK, LOC +
    7066                 QString("Loaded text subtitles from '%1'.").arg(*it));
     7068                QString("Loaded text subtitles from '%1'.").arg(candidate));
    70677069    }
    70687070
    70697071    return found;
  • libs/libmythtv/textsubtitleparser.cpp

    old new  
    55 */
    66
    77#include <stdio.h>
    8 #include <qtextcodec.h>
     8#include <cstring>
    99#include <algorithm>
     10#include <qtextcodec.h>
    1011
    1112using std::lower_bound;
    1213
     
    115116
    116117    target.SetFrameBasedTiming(!sub_data.uses_time);
    117118
     119
     120    // assume the text to be in UTF8 compatible format until encountering
     121    // a string of which characters cannot be encoded by UTF8
     122    QTextCodec* utf8Codec = QTextCodec::codecForName("utf8");
     123    assert(utf8Codec);
     124
     125    bool utf8 = true;
     126
    118127    // convert the subtitles to our own format and free the original structures
    119128    for (int sub_i = 0; sub_i < sub_data.num; ++sub_i)
    120129    {
     
    129138
    130139        for (int line = 0; line < sub->lines; ++line)
    131140        {
    132             newsub.textLines.push_back(QString(sub->text[line]));
     141            const char *subLine = sub->text[line];
     142            int lineLength = std::strlen(subLine);
     143
     144            // check if the string contains unknown chars to UTF8,
     145            // if the encoding has not been detected to be non-UTF8
     146            // before (the heuristics method can sometimes return
     147            // larger than lineLength for unknown reason, thus the >= )
     148            utf8 = utf8 &&
     149                (utf8Codec->heuristicContentMatch(subLine, lineLength) >= lineLength);
     150
     151            // use Latin1 as the fall back encoding in case there was
     152            // non-UTF8 strings detected
     153            // TODO: user option for setting the fall back encoding could
     154            // be nice (it's impossible to detect the correct 8-bit encoding
     155            // automatically)
     156            if (utf8)
     157                newsub.textLines.push_back(QString::fromUtf8(subLine));
     158            else
     159                newsub.textLines.push_back(QString::fromLatin1(subLine));
     160
    133161            free(sub->text[line]);
    134162        }
    135 
    136163        target.AddSubtitle(newsub);
    137164    }
    138165