Skip to content

ASS Subtitle Fixes #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Source/SubtitleProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ class SubtitleProvider :

int parseHexOrDecimalInt(std::wstring const& str, size_t offset)
{
if (offset > 2 && str[offset - 2] == L'H' && str[offset - 1] == L'&')
{
// Fix for cases where colors are specified like: 3cH&H2A4F5D&
offset++;
}
if (str.length() > offset + 1 && str[offset] == L'H')
{
return parseHexInt(str.substr(offset + 1));
Expand Down
41 changes: 33 additions & 8 deletions Source/SubtitleProviderSsaAss.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class SubtitleProviderSsaAss : public SubtitleProvider

if (resy != str.npos && sscanf_s((char*)m_pAvCodecCtx->subtitle_header + resy, "\nPlayResY: %i\n", &h) == 1)
{
hasPlayResY = true;
height = h;
}

Expand Down Expand Up @@ -293,6 +294,14 @@ class SubtitleProviderSsaAss : public SubtitleProvider
subStyle.FontFamily(GetFontFamily(fnName));
}
}
else if (checkTag(tag, L"fsc"))
{
// TODO: \fsc<x or y><percent> <x or y> x scales horizontally, y scales vertically <percent>
}
else if (checkTag(tag, L"fsp"))
{
// TODO: \fsp<pixels> <pixels> changes the distance between letters. (default: 0)
}
else if (checkTag(tag, L"fs"))
{
auto size = parseDouble(tag.substr(2));
Expand All @@ -301,6 +310,11 @@ class SubtitleProviderSsaAss : public SubtitleProvider
subStyle.FontSize(GetFontSize(size));
}
}
else if (checkTag(tag, L"clip"))
{
// TODO: \clip(<x1>, <y1>, <x2>, <y2>) Clips any drawing outside the rectangle defined by the parameters.
// \clip([<scale>,] <drawing commands>) Clipping against drawn shapes. <scale> has the same meaning as in the case of \p<scale>
}
else if (checkTag(tag, L"c", 2))
{
int color = parseHexOrDecimalInt(tag, 2);
Expand Down Expand Up @@ -425,7 +439,8 @@ class SubtitleProviderSsaAss : public SubtitleProvider
}
catch (...)
{
OutputDebugString(L"Failed to parse tag: ");
std::wstring output = L"Failed to parse tag: " + tag + L"\r\n";
OutputDebugString(output.c_str());
}
}

Expand Down Expand Up @@ -574,7 +589,7 @@ class SubtitleProviderSsaAss : public SubtitleProvider
}
else if (result <= 0)
{
OutputDebugString(L"Failed to decode subtitle.");
OutputDebugString(L"Failed to decode subtitle.\r\n");
}

return nullptr;
Expand All @@ -601,14 +616,14 @@ class SubtitleProviderSsaAss : public SubtitleProvider
int bold, italic, underline, strikeout;
float scaleX, scaleY, spacing, angle;
int borderStyle;
float outline;
int shadow, alignment;
float outline, shadow;
int alignment;
float marginL, marginR, marginV;
int encoding;

// try with hex colors
auto count = sscanf_s((char*)m_pAvCodecCtx->subtitle_header + stylesV4plus,
"%[^,],%[^,],%f,&H%x,&H%x,&H%x,&H%x,%i,%i,%i,%i,%f,%f,%f,%f,%i,%f,%i,%i,%f,%f,%f,%i",
"%[^,],%[^,],%f,&H%x,&H%x,&H%x,&H%x,%i,%i,%i,%i,%f,%f,%f,%f,%i,%f,%f,%i,%f,%f,%f,%i",
name, MAX_STYLE_NAME_CHARS, font, MAX_STYLE_NAME_CHARS,
&size, &color, &secondaryColor, &outlineColor, &backColor,
&bold, &italic, &underline, &strikeout,
Expand All @@ -620,7 +635,7 @@ class SubtitleProviderSsaAss : public SubtitleProvider
{
// try with decimal colors
count = sscanf_s((char*)m_pAvCodecCtx->subtitle_header + stylesV4plus,
"%[^,],%[^,],%f,%i,%i,%i,%i,%i,%i,%i,%i,%f,%f,%f,%f,%i,%f,%i,%i,%f,%f,%f,%i",
"%[^,],%[^,],%f,%i,%i,%i,%i,%i,%i,%i,%i,%f,%f,%f,%f,%i,%f,%f,%i,%f,%f,%f,%i",
name, MAX_STYLE_NAME_CHARS, font, MAX_STYLE_NAME_CHARS,
&size, &color, &secondaryColor, &outlineColor, &backColor,
&bold, &italic, &underline, &strikeout,
Expand Down Expand Up @@ -976,8 +991,17 @@ class SubtitleProviderSsaAss : public SubtitleProvider
TimedTextDouble GetFontSize(double fontSize)
{
TimedTextDouble size;
size.Unit = TimedTextUnit::Percentage;
size.Value = fontSize * 2.7;

if (hasPlayResY && videoHeight > 0 && height > 0)
{
size.Unit = TimedTextUnit::Pixels;
size.Value = static_cast<double>(videoHeight) / height * fontSize * 0.85;
}
else
{
size.Unit = TimedTextUnit::Percentage;
size.Value = fontSize * 2.7;
}

return size;
}
Expand Down Expand Up @@ -1050,6 +1074,7 @@ class SubtitleProviderSsaAss : public SubtitleProvider
int textIndex = 0;
int width = 0;
int height = 0;
bool hasPlayResY = false;
double videoAspectRatio = 0.0;
int videoWidth = 0;
int videoHeight = 0;
Expand Down