1 #include <lib/gui/esubtitle.h>
2 #include <lib/gdi/grc.h>
3 #include <lib/base/estring.h>
6 ok, here's much room for improvements.
8 first, the placing of the individual elements is sub-optimal.
9 then maybe a colored background would be an option.
13 eSubtitleWidget::eSubtitleStyle eSubtitleWidget::subtitleStyles[Subtitle_MAX];
15 eSubtitleWidget::eSubtitleWidget(eWidget *parent)
16 : eWidget(parent), m_hide_subtitles_timer(eTimer::create(eApp))
18 setBackgroundColor(gRGB(0,0,0,255));
22 CONNECT(m_hide_subtitles_timer->timeout, eSubtitleWidget::clearPage);
26 void eSubtitleWidget::setPage(const eDVBTeletextSubtitlePage &p)
30 invalidate(m_visible_region); // invalidate old visible regions
31 m_visible_region.rects.clear();
33 int elements = m_page.m_elements.size();
36 int startY = elements > 1
38 : size().height() / 3 * 2;
39 int width = size().width() - startX * 2;
40 int height = size().height() - startY;
41 int size_per_element = height / (elements ? elements : 1);
42 for (int i=0; i<elements; ++i)
44 eRect &area = m_page.m_elements[i].m_area;
46 area.setTop(size_per_element * i + startY);
48 area.setHeight(size_per_element);
49 m_visible_region.rects.push_back(area);
52 m_hide_subtitles_timer->start(7500, true);
53 invalidate(m_visible_region); // invalidate new regions
56 void eSubtitleWidget::setPage(const eDVBSubtitlePage &p)
60 invalidate(m_visible_region); // invalidate old visible regions
61 m_visible_region.rects.clear();
62 for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
64 eDebug("add %d %d %d %d", it->m_position.x(), it->m_position.y(), it->m_pixmap->size().width(), it->m_pixmap->size().height());
65 eDebug("disp width %d, disp height %d", p.m_display_size.width(), p.m_display_size.height());
66 eRect r = eRect(it->m_position, it->m_pixmap->size());
67 r.scale(size().width(), p.m_display_size.width(), size().height(), p.m_display_size.height());
68 m_visible_region.rects.push_back(r);
71 m_hide_subtitles_timer->start(7500, true);
72 invalidate(m_visible_region); // invalidate new regions
75 void eSubtitleWidget::setPage(const ePangoSubtitlePage &p)
79 invalidate(m_visible_region); // invalidate old visible regions
80 m_visible_region.rects.clear();
82 int elements = m_pango_page.m_elements.size();
85 int startY = elements > 1
87 : size().height() / 3 * 2;
88 int width = size().width() - startX * 2;
89 int height = size().height() - startY;
90 int size_per_element = height / (elements ? elements : 1);
91 for (int i=0; i<elements; ++i)
93 eRect &area = m_pango_page.m_elements[i].m_area;
95 area.setTop(size_per_element * i + startY);
97 area.setHeight(size_per_element);
98 m_visible_region.rects.push_back(area);
101 int timeout_ms = m_pango_page.m_timeout;
102 m_hide_subtitles_timer->start(timeout_ms, true);
103 invalidate(m_visible_region); // invalidate new regions
106 void eSubtitleWidget::clearPage()
108 eDebug("subtitle timeout... hide");
112 invalidate(m_visible_region);
113 m_visible_region.rects.clear();
116 void eSubtitleWidget::setPixmap(ePtr<gPixmap> &pixmap, gRegion changed, eRect pixmap_dest)
119 m_pixmap_dest = pixmap_dest; /* this is in a virtual 720x576 cage */
121 /* incoming "changed" regions are relative to the physical pixmap area, so they have to be scaled to the virtual pixmap area, then to the screen */
122 changed.scale(m_pixmap_dest.width(), 720, m_pixmap_dest.height(), 576);
123 changed.moveBy(ePoint(m_pixmap_dest.x(), m_pixmap_dest.y()));
125 if (pixmap->size().width() && pixmap->size().height())
126 changed.scale(size().width(), pixmap->size().width(), size().height(), pixmap->size().height());
131 int eSubtitleWidget::event(int event, void *data, void *data2)
137 ePtr<eWindowStyle> style;
138 gPainter &painter = *(gPainter*)data2;
141 eWidget::event(event, data, data2);
145 eRect r = m_pixmap_dest;
146 r.scale(size().width(), 720, size().height(), 576);
147 painter.blitScale(m_pixmap, r);
148 } else if (m_page_ok)
150 int elements = m_page.m_elements.size();
151 painter.setFont(subtitleStyles[Subtitle_TTX].font);
152 for (int i=0; i<elements; ++i)
154 eDVBTeletextSubtitlePageElement &element = m_page.m_elements[i];
155 eRect &area = element.m_area;
157 shadow.moveBy(subtitleStyles[Subtitle_TTX].shadow_offset);
158 painter.setForegroundColor(subtitleStyles[Subtitle_TTX].shadow_color);
159 painter.renderText(shadow, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
160 if ( !subtitleStyles[Subtitle_TTX].have_foreground_color )
161 painter.setForegroundColor(element.m_color);
163 painter.setForegroundColor(subtitleStyles[Subtitle_TTX].foreground_color);
164 painter.renderText(area, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
167 else if (m_pango_page_ok)
169 int elements = m_pango_page.m_elements.size();
172 for (int i=0; i<elements; ++i)
174 face = Subtitle_Regular;
175 ePangoSubtitlePageElement &element = m_pango_page.m_elements[i];
176 std::string text = element.m_pango_line;
177 std::string::size_type loc = text.find("<", 0 );
178 if ( loc != std::string::npos )
180 switch (char(text.at(1)))
183 face = Subtitle_Italic;
186 face = Subtitle_Bold;
189 text = text.substr(3, text.length()-7);
191 text = replace_all(text, "'", "'");
192 text = replace_all(text, """, "\"");
193 text = replace_all(text, "&", "&");
194 painter.setFont(subtitleStyles[face].font);
195 eRect &area = element.m_area;
197 shadow.moveBy(subtitleStyles[face].shadow_offset);
198 painter.setForegroundColor(subtitleStyles[face].shadow_color);
199 painter.renderText(shadow, text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
200 if ( !subtitleStyles[face].have_foreground_color && element.m_have_color )
201 painter.setForegroundColor(element.m_color);
203 painter.setForegroundColor(subtitleStyles[face].foreground_color);
204 painter.renderText(area, text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
207 else if (m_dvb_page_ok)
209 for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
211 eRect r = eRect(it->m_position, it->m_pixmap->size());
212 r.scale(size().width(), m_dvb_page.m_display_size.width(), size().height(), m_dvb_page.m_display_size.height());
213 painter.blitScale(it->m_pixmap, r);
219 return eWidget::event(event, data, data2);
223 void eSubtitleWidget::setFontStyle(subfont_t face, gFont *font, int haveColor, const gRGB &col, const gRGB &shadowCol, const ePoint &shadowOffset)
225 subtitleStyles[face].font = font;
226 subtitleStyles[face].have_foreground_color = haveColor;
227 subtitleStyles[face].foreground_color = col;
228 subtitleStyles[face].shadow_color = shadowCol;
229 subtitleStyles[face].shadow_offset = shadowOffset;