Skip to content

Commit

Permalink
Added RTL script support to subpicture import.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrd2 committed Jun 17, 2019
1 parent 29109c3 commit b1d7a69
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/formats/vobsub/vobsubinputformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class VobSubInputFormat : public InputFormat
VobSubInputProcessDialog dlgProc(&subtitle, app()->mainWindow());

dlgProc.processFrames(&proc);
dlgProc.setRTL(dlgInit.isRTL());

QByteArray symFile(filebase + ".sym");

Expand Down
6 changes: 6 additions & 0 deletions src/formats/vobsub/vobsubinputinitdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ VobSubInputInitDialog::streamIndex() const
return ui->comboStream->currentIndex();
}

bool
VobSubInputInitDialog::isRTL() const
{
return ui->isRTL->isChecked();
}

quint32
VobSubInputInitDialog::postProcessingFlags() const
{
Expand Down
1 change: 1 addition & 0 deletions src/formats/vobsub/vobsubinputinitdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class VobSubInputInitDialog : public QDialog

void streamListSet(const QStringList streams);
int streamIndex() const;
bool isRTL() const;

quint32 postProcessingFlags() const;

Expand Down
11 changes: 9 additions & 2 deletions src/formats/vobsub/vobsubinputinitdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="isRTL">
<property name="text">
<string>Subtitle is using RTL script</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand All @@ -117,8 +124,8 @@
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
<x>254</x>
<y>293</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
Expand Down
32 changes: 25 additions & 7 deletions src/formats/vobsub/vobsubinputprocessdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class VobSubInputProcessDialog::Frame : public QSharedData
Time subShowTime;
Time subHideTime;
QList<PiecePtr> pieces;
bool rtl;
};

class VobSubInputProcessDialog::Piece : public QSharedData
Expand Down Expand Up @@ -88,6 +89,7 @@ class VobSubInputProcessDialog::Piece : public QSharedData
}

inline bool operator<(const Piece &other) const;
inline bool operator>(const Piece &other) const;
inline bool operator==(const Piece &other) const;
inline Piece & operator+=(const Piece &other);

Expand Down Expand Up @@ -235,14 +237,16 @@ VobSubInputProcessDialog::Frame::processPieces()
}

// sort pieces, line by line, left to right, comparison is done in Piece::operator<()
std::sort(pieces.begin(), pieces.end(), [](const PiecePtr &a, const PiecePtr &b)->bool{
return *a < *b;
});
auto sortCb = rtl
? [](const PiecePtr &a, const PiecePtr &b)->bool{ return *a > *b; }
: [](const PiecePtr &a, const PiecePtr &b)->bool{ return *a < *b; };
std::sort(pieces.begin(), pieces.end(), sortCb);

PiecePtr prevPiece;
foreach(piece, pieces) {
if(prevPiece && prevPiece->line == piece->line) {
spaceStats[piece->left - prevPiece->right]++;
const int spaceWidth = rtl ? prevPiece->left - piece->right : piece->left - prevPiece->right;
spaceStats[spaceWidth]++;
spaceCount++;
}
prevPiece = piece;
Expand All @@ -267,6 +271,16 @@ VobSubInputProcessDialog::Piece::operator<(const Piece &other) const
return false;
}

inline bool
VobSubInputProcessDialog::Piece::operator>(const Piece &other) const
{
if(line->top < other.line->top)
return true;
if(line->intersects(other.line) && left > other.left)
return true;
return false;
}

inline bool
VobSubInputProcessDialog::Piece::operator==(const Piece &other) const
{
Expand Down Expand Up @@ -576,6 +590,7 @@ VobSubInputProcessDialog::onStreamData(const QPixmap &pixmap, quint64 msecStart,
frame->subShowTime.setMillisTime(double(msecStart));
frame->subHideTime.setMillisTime(double(msecStart + msecDuration));
frame->subPixmap = pixmap;
frame->rtl = m_isRTL;

ui->subtitleView->setPixmap(frame->subPixmap);
QCoreApplication::processEvents();
Expand Down Expand Up @@ -678,10 +693,13 @@ VobSubInputProcessDialog::processNextPiece()
PiecePtr piecePrev;
foreach(PiecePtr piece, m_pieces) {
if(piecePrev) {
if(!piecePrev->line->intersects(piece->line))
if(!piecePrev->line->intersects(piece->line)) {
subText.append(QChar(QChar::LineFeed));
else if(piece->left - piecePrev->right > m_spaceWidth)
subText.append(QChar(QChar::Space));
} else {
const int spaceWidth = m_isRTL ? piecePrev->left - piece->right : piece->left - piecePrev->right;
if(spaceWidth > m_spaceWidth)
subText.append(QChar(QChar::Space));
}
}

subText += piece->text;
Expand Down
3 changes: 3 additions & 0 deletions src/formats/vobsub/vobsubinputprocessdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class VobSubInputProcessDialog : public QDialog
bool symFileOpen(const QString &filename);
bool symFileSave(const QString &filename);

inline void setRTL(bool rtl) { m_isRTL = rtl; }

bool eventFilter(QObject *obj, QEvent *event) override;

void processFrames(StreamProcessor *streamProcessor);
Expand Down Expand Up @@ -88,6 +90,7 @@ private slots:
Subtitle *m_subtitle;

qint32 m_spaceWidth;
bool m_isRTL;

QList<PiecePtr> m_pieces;
QList<PiecePtr>::iterator m_pieceCurrent;
Expand Down

0 comments on commit b1d7a69

Please sign in to comment.