properly scale change regions
[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                 eRect r = eRect(it->m_position, it->m_pixmap->size());
66                 r.scale(size().width(), 720, size().height(), 576);
67                 m_visible_region.rects.push_back(r);
68         }
69         m_dvb_page_ok = 1;
70         m_hide_subtitles_timer->start(7500, true);
71         invalidate(m_visible_region);  // invalidate new regions
72 }
73
74 void eSubtitleWidget::setPage(const ePangoSubtitlePage &p)
75 {
76         m_pango_page = p;
77         m_pango_page_ok = 1;
78         invalidate(m_visible_region);  // invalidate old visible regions
79         m_visible_region.rects.clear();
80
81         int elements = m_pango_page.m_elements.size();
82         if (elements)
83         {
84                 int startY = elements > 1
85                         ? size().height() / 2
86                         : size().height() / 3 * 2;
87                 int width = size().width() - startX * 2;
88                 int height = size().height() - startY;
89                 int size_per_element = height / (elements ? elements : 1);
90                 for (int i=0; i<elements; ++i)
91                 {
92                         eRect &area = m_pango_page.m_elements[i].m_area;
93                         area.setLeft(startX);
94                         area.setTop(size_per_element * i + startY);
95                         area.setWidth(width);
96                         area.setHeight(size_per_element);
97                         m_visible_region.rects.push_back(area);
98                 }
99         }
100         int timeout_ms = m_pango_page.m_timeout;
101         m_hide_subtitles_timer->start(timeout_ms, true);
102         invalidate(m_visible_region);  // invalidate new regions
103 }
104
105 void eSubtitleWidget::clearPage()
106 {
107         eDebug("subtitle timeout... hide");
108         m_page_ok = 0;
109         m_dvb_page_ok = 0;
110         m_pango_page_ok = 0;
111         invalidate(m_visible_region);
112         m_visible_region.rects.clear();
113 }
114
115 void eSubtitleWidget::setPixmap(ePtr<gPixmap> &pixmap, gRegion changed, eRect pixmap_dest)
116 {
117         m_pixmap = pixmap;
118         m_pixmap_dest = pixmap_dest; /* this is in a virtual 720x576 cage */
119         
120                 /* 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 */
121         changed.scale(m_pixmap_dest.width(), 720, m_pixmap_dest.height(), 576);
122         changed.moveBy(ePoint(m_pixmap_dest.x(), m_pixmap_dest.y()));
123
124         changed.scale(size().width(), pixmap->size().width(), size().height(), pixmap->size().height());
125         
126         invalidate(changed);
127 }
128
129 int eSubtitleWidget::event(int event, void *data, void *data2)
130 {
131         switch (event)
132         {
133         case evtPaint:
134         {
135                 ePtr<eWindowStyle> style;
136                 gPainter &painter = *(gPainter*)data2;
137
138                 getStyle(style);
139                 eWidget::event(event, data, data2);
140
141                 if (m_pixmap)
142                 {
143                         eRect r = m_pixmap_dest;
144                         r.scale(size().width(), 720, size().height(), 576);
145                         painter.blitScale(m_pixmap, r);
146                 } else if (m_page_ok)
147                 {
148                         int elements = m_page.m_elements.size();
149                         painter.setFont(subtitleStyles[Subtitle_TTX].font);
150                         for (int i=0; i<elements; ++i)
151                         {
152                                 eDVBTeletextSubtitlePageElement &element = m_page.m_elements[i];
153                                 eRect &area = element.m_area;
154                                 eRect shadow = area;
155                                 shadow.moveBy(subtitleStyles[Subtitle_TTX].shadow_offset);
156                                 painter.setForegroundColor(subtitleStyles[Subtitle_TTX].shadow_color);
157                                 painter.renderText(shadow, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
158                                 if ( !subtitleStyles[Subtitle_TTX].have_foreground_color )
159                                         painter.setForegroundColor(element.m_color);
160                                 else
161                                         painter.setForegroundColor(subtitleStyles[Subtitle_TTX].foreground_color);
162                                 painter.renderText(area, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
163                         }
164                 }
165                 else if (m_pango_page_ok)
166                 {
167                         int elements = m_pango_page.m_elements.size();
168                         subfont_t face;
169
170                         for (int i=0; i<elements; ++i)
171                         {
172                                 face = Subtitle_Regular;
173                                 ePangoSubtitlePageElement &element = m_pango_page.m_elements[i];
174                                 std::string text = element.m_pango_line;
175                                 std::string::size_type loc = text.find("<", 0 );
176                                 if ( loc != std::string::npos )
177                                 {
178                                         switch (char(text.at(1)))
179                                         {
180                                         case 'i':
181                                                 face = Subtitle_Italic;
182                                                 break;
183                                         case 'b':
184                                                 face = Subtitle_Bold;
185                                                 break;
186                                         }
187                                         text = text.substr(3, text.length()-7);
188                                 }
189                                 text = replace_all(text, "&apos;", "'");
190                                 text = replace_all(text, "&quot;", "\"");
191                                 text = replace_all(text, "&amp;", "&");
192                                 painter.setFont(subtitleStyles[face].font);
193                                 eRect &area = element.m_area;
194                                 eRect shadow = area;
195                                 shadow.moveBy(subtitleStyles[face].shadow_offset);
196                                 painter.setForegroundColor(subtitleStyles[face].shadow_color);
197                                 painter.renderText(shadow, text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
198                                 if ( !subtitleStyles[face].have_foreground_color && element.m_have_color )
199                                         painter.setForegroundColor(element.m_color);
200                                 else
201                                         painter.setForegroundColor(subtitleStyles[face].foreground_color);
202                                 painter.renderText(area, text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|gPainter::RT_HALIGN_CENTER);
203                         }
204                 }
205                 else if (m_dvb_page_ok)
206                 {
207                         for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
208                         {
209                                         /* dvb subtitles are living in their 720x576 cage... i think. check this for HD. */
210                                 eRect r = eRect(it->m_position, it->m_pixmap->size());
211                                 r.scale(size().width(), 720, size().height(), 576);
212                                 painter.blitScale(it->m_pixmap, r);
213                         }
214                 }
215                 return 0;
216         }
217         default:
218                 return eWidget::event(event, data, data2);
219         }
220 }
221
222 void eSubtitleWidget::setFontStyle(subfont_t face, gFont *font, int haveColor, const gRGB &col, const gRGB &shadowCol, const ePoint &shadowOffset)
223 {
224         subtitleStyles[face].font = font;
225         subtitleStyles[face].have_foreground_color = haveColor;
226         subtitleStyles[face].foreground_color = col;
227         subtitleStyles[face].shadow_color = shadowCol;
228         subtitleStyles[face].shadow_offset = shadowOffset;
229 }
230