private static Paragraph GetPacParagraph(ref int index, byte[] buffer) { while (index < 15) { index++; } bool con = true; while (con) { index++; if (index + 20 >= buffer.Length) { return(null); } if (buffer[index] == 0xFE && buffer[index - 1] == 0x80) { con = false; } } int feIndex = index; byte alignment = buffer[feIndex + 1]; byte verticalAlignment = buffer[feIndex - 1]; var p = new Paragraph(); int timeStartIndex = feIndex - 15; p.StartTime = Pac.GetTimeCode(timeStartIndex + 1, buffer); p.EndTime = Pac.GetTimeCode(timeStartIndex + 5, buffer); int textLength = buffer[timeStartIndex + 9] + buffer[timeStartIndex + 10] * 256; if (textLength > 500) { return(null); // probably not correct index } int maxIndex = timeStartIndex + 10 + textLength; var sb = new StringBuilder(); index = feIndex + 3; int textIndex = index; int textBegin = index; while (textIndex < buffer.Length && textIndex <= maxIndex) { if (buffer[textIndex] == 0xFE) { if (textIndex > textBegin) { sb.AppendLine(Encoding.UTF8.GetString(buffer, textBegin, textIndex - textBegin - 1)); textBegin = textIndex + 7; textIndex += 6; } } else if (buffer[textIndex] == 0xFF) { sb.Append(' '); } textIndex++; } if (textIndex > textBegin) { sb.Append(Encoding.UTF8.GetString(buffer, textBegin, textIndex - textBegin - 1)); } p.Text = sb.ToString().Trim(); for (int k = 0; k < p.Text.Length; k++) { if (p.Text[k] == 65533) { p.Text = p.Text.Remove(k, 1).Insert(k, "."); } } index += textLength; if (index + 20 >= buffer.Length) { return(null); } p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); p.Text = p.Text.Replace(Environment.NewLine + ">", Environment.NewLine); p.Text = p.Text.Replace("\0", string.Empty); if (verticalAlignment < 5) { if (alignment == 1) // left { p.Text = "{\\an7}" + p.Text; } else if (alignment == 0) // right { p.Text = "{\\an9}" + p.Text; } else { p.Text = "{\\an8}" + p.Text; } } else if (verticalAlignment < 9) { if (alignment == 1) // left { p.Text = "{\\an4}" + p.Text; } else if (alignment == 0) // right { p.Text = "{\\an6}" + p.Text; } else { p.Text = "{\\an5}" + p.Text; } } else { if (alignment == 1) // left { p.Text = "{\\an1}" + p.Text; } else if (alignment == 0) // right { p.Text = "{\\an3}" + p.Text; } } p.Text = p.Text.Replace(Convert.ToChar(0).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(1).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(2).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(3).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(4).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(5).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(6).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(7).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(8).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(11).ToString(CultureInfo.InvariantCulture), string.Empty); p.Text = p.Text.Replace(Convert.ToChar(12).ToString(CultureInfo.InvariantCulture), string.Empty); return(p); }
private static Paragraph GetPacParagraph(ref int index, byte[] buffer) { while (index < 15) { index++; } bool con = true; while (con) { index++; if (index + 20 >= buffer.Length) { return(null); } if (buffer[index] == 0xFE && buffer[index - 1] == 0x80) { con = false; } } int feIndex = index; byte alignment = buffer[feIndex + 1]; byte verticalAlignment = buffer[feIndex - 1]; var p = new Paragraph(); int timeStartIndex = feIndex - 15; p.StartTime = Pac.GetTimeCode(timeStartIndex + 1, buffer); p.EndTime = Pac.GetTimeCode(timeStartIndex + 5, buffer); int textLength = buffer[timeStartIndex + 9] + buffer[timeStartIndex + 10] * 256; if (textLength > 500) { return(null); // probably not correct index } int maxIndex = timeStartIndex + 10 + textLength; var sb = new StringBuilder(); index = feIndex + 3; int textIndex = index; int textBegin = index; while (textIndex < buffer.Length && textIndex <= maxIndex) { if (buffer[textIndex] == 0xFE) { if (textIndex > textBegin) { for (int j = textBegin; j <= textIndex - textBegin - 1; j++) { if (buffer[j] == 0xff) { buffer[j] = 0x2e; // replace end of line marker } } sb.AppendLine(Encoding.UTF8.GetString(buffer, textBegin, textIndex - textBegin)); textBegin = textIndex + 7; textIndex += 6; } } else if (buffer[textIndex] == 0xFF) { sb.Append(' '); } textIndex++; } if (textIndex > textBegin) { sb.Append(Encoding.UTF8.GetString(buffer, textBegin, textIndex - textBegin - 1)); } p.Text = sb.ToString().Trim(); if (p.Text.Length > 1 && (p.Text[0] == 31 || p.Text[1] == 65279)) { p.Text = p.Text.Remove(0, 2); } for (int k = 0; k < p.Text.Length; k++) { if (p.Text[k] == 65533) { p.Text = p.Text.Remove(k, 1).Insert(k, "."); } } index += textLength; if (index + 20 >= buffer.Length) { return(null); } p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); p.Text = p.Text.Replace(Environment.NewLine + ">", Environment.NewLine); p.Text = p.Text.Replace("\0", string.Empty); if (verticalAlignment < 5) { if (alignment == 1) // left { p.Text = "{\\an7}" + p.Text; } else if (alignment == 0) // right { p.Text = "{\\an9}" + p.Text; } else { p.Text = "{\\an8}" + p.Text; } } else if (verticalAlignment < 9) { if (alignment == 1) // left { p.Text = "{\\an4}" + p.Text; } else if (alignment == 0) // right { p.Text = "{\\an6}" + p.Text; } else { p.Text = "{\\an5}" + p.Text; } } else { if (alignment == 1) // left { p.Text = "{\\an1}" + p.Text; } else if (alignment == 0) // right { p.Text = "{\\an3}" + p.Text; } } // Remove all control-characters if any in p.Text. p.Text = p.Text.RemoveControlCharactersButWhiteSpace(); p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); p.Text = p.Text.Replace(Environment.NewLine + " ", Environment.NewLine); // Fix italics (basic) if (p.Text.StartsWith('<') && !p.Text.StartsWith("<i>", StringComparison.OrdinalIgnoreCase) && !p.Text.StartsWith("<b>", StringComparison.OrdinalIgnoreCase) && !p.Text.StartsWith("<u>", StringComparison.OrdinalIgnoreCase) && !p.Text.StartsWith("<font ", StringComparison.OrdinalIgnoreCase)) { p.Text = "<i>" + p.Text.TrimStart('<').Replace(Environment.NewLine + "<", Environment.NewLine) + "</i>"; } else if (p.Text.Contains(Environment.NewLine + "<")) { p.Text = p.Text.Replace(Environment.NewLine + "<", Environment.NewLine + "<i>") + "</i>"; } return(p); }