+
+void eDVBTeletextParser::handlePageStart()
+{
+// if (m_C & (1<<4)) /* erase flag set */
+
+ /* we are always erasing the page,
+ even when the erase flag is not set. */
+ m_subtitle_page.clear();
+}
+
+void eDVBTeletextParser::handleLine(unsigned char *data, int len)
+{
+/* // hexdump
+ for (int i=0; i<len; ++i)
+ eDebugNoNewLine("%02x ", decode_odd_parity(data + i));
+ eDebug(""); */
+ if (!m_Y) /* first line is page header, we don't need that. */
+ {
+ m_double_height = -1;
+ return;
+ }
+
+ if (m_double_height == m_Y)
+ {
+ m_double_height = -1;
+ return;
+ }
+
+ int last_was_white = 1, color = 7; /* start with whitespace. start with color=white. (that's unrelated.) */
+
+ std::string text;
+
+// eDebug("handle subtitle line: %d len", len);
+ for (int i=0; i<len; ++i)
+ {
+ unsigned char b = decode_odd_parity(data + i);
+
+ if (b < 0x10) /* spacing attribute */
+ {
+ if (b < 8) /* colors */
+ {
+ if (b != color) /* new color is split into a new string */
+ {
+ addSubtitleString(color, text);
+ text = "";
+ color = b;
+ }
+ } else if (b == 0xd)
+ {
+ m_double_height = m_Y + 1;
+ } else if (b != 0xa && b != 0xb) /* box */
+ eDebug("[ignore %x]", b);
+ /* ignore other attributes */
+ } else
+ {
+ eDebugNoNewLine("%c", b);
+ /* no more than one whitespace, only printable chars */
+ if (((!last_was_white) || (b != ' ')) && (b >= 0x20))
+ {
+ text += b;
+ last_was_white = b == ' ';
+ }
+ }
+ }
+ eDebug("");
+ addSubtitleString(color, text);
+}
+
+void eDVBTeletextParser::handlePageEnd(int have_pts, const pts_t &pts)
+{
+// eDebug("handle page end");
+ addSubtitleString(-2, ""); /* end last line */
+
+ m_subtitle_page.m_have_pts = have_pts;
+ m_subtitle_page.m_pts = pts;
+ m_subtitle_page.m_timeout = 90000 * 20; /* 20s */
+ sendSubtitlePage(); /* send assembled subtitle page to display */
+}
+
+void eDVBTeletextParser::setPage(int page)
+{
+ m_page_M = (page >> 8) & 7; /* magazine to look for */
+ m_page_X = page & 0xFF; /* page number */
+}
+
+void eDVBTeletextParser::connectNewPage(const Slot1<void, const eDVBTeletextSubtitlePage&> &slot, ePtr<eConnection> &connection)
+{
+ connection = new eConnection(this, m_new_subtitle_page.connect(slot));
+}
+
+void eDVBTeletextParser::addSubtitleString(int color, std::string string)
+{
+// eDebug("add subtitle string: %s, col %d", string.c_str(), color);
+
+ int force_cell = 0;
+
+ if (string.substr(0, 2) == "- ")
+ {
+ string = string.substr(2);
+ force_cell = 1;
+ }
+
+// eDebug("color %d, m_subtitle_color %d", color, m_subtitle_color);
+ gRGB rgbcol((color & 1) ? 255 : 128, (color & 2) ? 255 : 128, (color & 4) ? 255 : 128);
+ if ((color != m_subtitle_color || force_cell) && !m_subtitle_text.empty() && ((color == -2) || !string.empty()))
+ {
+// eDebug("add text |%s|: %d != %d || %d", m_subtitle_text.c_str(), color, m_subtitle_color, force_cell);
+ m_subtitle_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(rgbcol, m_subtitle_text));
+ m_subtitle_text = "";
+ } else if (!m_subtitle_text.empty() && m_subtitle_text[m_subtitle_text.size()-1] != ' ')
+ m_subtitle_text += " ";
+
+ if (!string.empty())
+ {
+// eDebug("set %d as new color", color);
+ m_subtitle_color = color;
+ m_subtitle_text += string;
+ }
+}
+
+void eDVBTeletextParser::sendSubtitlePage()
+{
+// eDebug("subtitle page:");
+ for (unsigned int i = 0; i < m_subtitle_page.m_elements.size(); ++i)
+ eDebug("%s", m_subtitle_page.m_elements[i].m_text.c_str());
+ m_new_subtitle_page(m_subtitle_page);
+}