migrate eDVBTsTools to new iDataSource seek function
[enigma2.git] / lib / gui / esubtitle.cpp
1 #include <lib/gui/esubtitle.h>
2 #include <lib/gdi/grc.h>
3 #include <lib/base/estring.h>
4
5         /*
6                 ok, here's much room for improvements.
7         
8                 first, the placing of the individual elements is sub-optimal.
9                 then maybe a colored background would be an option.
10                 ....
11         */      
12
13 eSubtitleWidget::eSubtitleStyle eSubtitleWidget::subtitleStyles[Subtitle_MAX];
14
15 eSubtitleWidget::eSubtitleWidget(eWidget *parent)
16         : eWidget(parent), m_hide_subtitles_timer(eTimer::create(eApp))
17 {
18         setBackgroundColor(gRGB(0,0,0,255));
19         m_page_ok = 0;
20         m_dvb_page_ok = 0;
21         m_pango_page_ok = 0;
22         CONNECT(m_hide_subtitles_timer->timeout, eSubtitleWidget::clearPage);
23 }
24
25 #define startX 50
26 void eSubtitleWidget::setPage(const eDVBTeletextSubtitlePage &p)
27 {
28         m_page = p;
29         m_page_ok = 1;
30         invalidate(m_visible_region);  // invalidate old visible regions
31         m_visible_region.rects.clear();
32
33         int elements = m_page.m_elements.size();
34         if (elements)
35         {
36                 int startY = elements > 1
37                         ? size().height() / 2
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)
43                 {
44                         eRect &area = m_page.m_elements[i].m_area;
45                         area.setLeft(startX);
46                         area.setTop(size_per_element * i + startY);
47                         area.setWidth(width);
48                         area.setHeight(size_per_element);
49                         m_visible_region.rects.push_back(area);
50                 }
51         }
52         m_hide_subtitles_timer->start(7500, true);
53         invalidate(m_visible_region);  // invalidate new regions
54 }
55
56 void eSubtitleWidget::setPage(const eDVBSubtitlePage &p)
57 {
58         eDebug("setPage");
59         m_dvb_page = 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)
63         {
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);
69         }
70         m_dvb_page_ok = 1;
71         m_hide_subtitles_timer->start(7500, true);
72         invalidate(m_visible_region);  // invalidate new regions
73 }
74
75 void eSubtitleWidget::setPage(const ePangoSubtitlePage &p)
76 {
77         m_pango_page = p;
78         m_pango_page_ok = 1;
79         invalidate(m_visible_region);  // invalidate old visible regions
80         m_visible_region.rects.clear();
81
82         int elements = m_pango_page.m_elements.size();
83         if (elements)
84         {
85                 int startY = elements > 1
86                         ? size().height() / 2
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)
92                 {
93                         eRect &area = m_pango_page.m_elements[i].m_area;
94                         area.setLeft(startX);
95                         area.setTop(size_per_element * i + startY);
96                         area.setWidth(width);
97                         area.setHeight(size_per_element);
98                         m_visible_region.rects.push_back(area);
99                 }
100         }
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
104 }
105
106 void eSubtitleWidget::clearPage()
107 {
108         eDebug("subtitle timeout... hide");
109         m_page_ok = 0;
110         m_dvb_page_ok = 0;
111         m_pango_page_ok = 0;
112         invalidate(m_visible_region);
113         m_visible_region.rects.clear();
114 }
115
116 void eSubtitleWidget::setPixmap(ePtr<gPixmap> &pixmap, gRegion changed, eRect pixmap_dest)
117 {
118         m_pixmap = pixmap;
119         m_pixmap_dest = pixmap_dest; /* this is in a virtual 720x576 cage */
120         
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()));
124
125         if (pixmap->size().width() && pixmap->size().height())
126                 changed.scale(size().width(), pixmap->size().width(), size().height(), pixmap->size().height());
127         
128         invalidate(changed);
129 }
130
131 int eSubtitleWidget::event(int event, void *data, void *data2)
132 {
133         switch (event)
134         {
135         case evtPaint:
136         {
137                 ePtr<eWindowStyle> style;
138                 gPainter &painter = *(gPainter*)data2;
139
140                 getStyle(style);
141                 eWidget::event(event, data, data2);
142
143                 if (m_pixmap)
144                 {
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)
149                 {
150                         int elements = m_page.m_elements.size();
151                         painter.setFont(subtitleStyles[Subtitle_TTX].font);
152                         for (int i=0; i<elements; ++i)
153                         {
154                                 eDVBTeletextSubtitlePageElement &element = m_page.m_elements[i];
155                                 eRect &area = element.m_area;
156                                 eRect shadow = 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);
162                                 else
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);
165                         }
166                 }
167                 else if (m_pango_page_ok)
168                 {
169                         int elements = m_pango_page.m_elements.size();
170                         subfont_t face;
171
172                         for (int i=0; i<elements; ++i)
173                         {
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 )
179                                 {
180                                         switch (char(text.at(1)))
181                                         {
182                                         case 'i':
183                                                 face = Subtitle_Italic;
184                                                 break;
185                                         case 'b':
186                                                 face = Subtitle_Bold;
187                                                 break;
188                                         }
189                                         text = text.substr(3, text.length()-7);
190                                 }
191                                 text = replace_all(text, "&apos;", "'");
192                                 text = replace_all(text, "&quot;", "\"");
193                                 text = replace_all(text, "&amp;", "&");
194                                 painter.setFont(subtitleStyles[face].font);
195                                 eRect &area = element.m_area;
196                                 eRect shadow = 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);
202                                 else
203                                         painter.setForegroundColor(subtitleStyles[face].foreground_color);
204                                 painter.renderText(area, text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
205                         }
206                 }
207                 else if (m_dvb_page_ok)
208                 {
209                         for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
210                         {
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);
214                         }
215                 }
216                 return 0;
217         }
218         default:
219                 return eWidget::event(event, data, data2);
220         }
221 }
222
223 void eSubtitleWidget::setFontStyle(subfont_t face, gFont *font, int haveColor, const gRGB &col, const gRGB &shadowCol, const ePoint &shadowOffset)
224 {
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;
230 }
231