00bf12d585ab27d9697d72adc09d1e929a78827a
[vuplus_dvbapp] / lib / gui / esubtitle.cpp
1 #include <lib/gui/esubtitle.h>
2 #include <lib/gui/ewidgetdesktop.h>
3 #include <lib/gdi/grc.h>
4 #include <lib/gdi/font.h>
5 #include <lib/base/estring.h>
6 #include <lib/base/nconfig.h>
7
8         /*
9                 ok, here's much room for improvements.
10         
11                 first, the placing of the individual elements is sub-optimal.
12                 then maybe a colored background would be an option.
13                 ....
14         */      
15
16 eSubtitleWidget::eSubtitleStyle eSubtitleWidget::subtitleStyles[Subtitle_MAX];
17
18 eSubtitleWidget::eSubtitleWidget(eWidget *parent)
19         : eWidget(parent), m_hide_subtitles_timer(eTimer::create(eApp))
20 {
21         setBackgroundColor(gRGB(0,0,0,255));
22         m_page_ok = 0;
23         m_dvb_page_ok = 0;
24         m_pango_page_ok = 0;
25         CONNECT(m_hide_subtitles_timer->timeout, eSubtitleWidget::clearPage);
26 }
27
28 #define startX 50
29 void eSubtitleWidget::setPage(const eDVBTeletextSubtitlePage &p)
30 {
31         eDVBTeletextSubtitlePage newpage = p;
32         m_page = p;
33         m_page.clear();
34         m_page_ok = 1;
35         invalidate(m_visible_region);  // invalidate old visible regions
36         m_visible_region.rects.clear();
37
38         unsigned int elements = newpage.m_elements.size();
39         if (elements)
40         {
41                 int startY = elements > 1
42                         ? size().height() / 2
43                         : size().height() / 3 * 2;
44                 int width = size().width() - startX * 2;
45                 int height = size().height() - startY;
46                 int size_per_element = height / (elements ? elements : 1);
47                 bool original_position = ePythonConfigQuery::getConfigBoolValue("config.subtitles.subtitle_original_position");
48                 gRGB color;
49                 bool original_colors = false;
50                 switch (ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_fontcolor", 0))
51                 {
52                         default:
53                         case 0: /* use original teletext colors */
54                                 color = newpage.m_elements[0].m_color;
55                                 original_colors = true;
56                                 break;
57                         case FONTCOLOR_WHITE:
58                                 color = gRGB(255, 255, 255);
59                                 break;
60                         case FONTCOLOR_YELLOW:
61                                 color = gRGB(255, 255, 0);
62                                 break;
63                         case FONTCOLOR_GREEN:
64                                 color = gRGB(0, 255, 0);
65                                 break;
66                         case FONTCOLOR_CYAN:
67                                 color = gRGB(0, 255, 255);
68                                 break;
69                         case FONTCOLOR_BLUE:
70                                 color = gRGB(0, 0, 255);
71                                 break;
72                         case FONTCOLOR_MAGNETA:
73                                 color = gRGB(255, 0, 255);
74                                 break;
75                         case FONTCOLOR_RED:
76                                 color = gRGB(255, 0, 0);
77                                 break;
78                         case FONTCOLOR_BLACK:
79                                 color = gRGB(0, 0, 0);
80                                 break;
81                 }
82                 color.a = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_opacity");
83
84                 int line = newpage.m_elements[0].m_source_line;
85                 int currentelement = 0;
86                 m_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(color, "", line));
87                 for (int i=0; i<elements; ++i)
88                 {
89                         if (!m_page.m_elements[currentelement].m_text.empty())
90                                 m_page.m_elements[currentelement].m_text += " ";
91                         if (original_colors && color != newpage.m_elements[i].m_color)
92                         {
93                                 color = newpage.m_elements[i].m_color;
94                                 m_page.m_elements[currentelement].m_text += (std::string)color;
95                         }
96                         if (line != newpage.m_elements[i].m_source_line)
97                         {
98                                 line = newpage.m_elements[i].m_source_line;
99                                 m_page.m_elements.push_back(eDVBTeletextSubtitlePageElement(color, "", line));
100                                 currentelement++;
101                         }
102                         m_page.m_elements[currentelement].m_text += newpage.m_elements[i].m_text;
103                 }
104                 for (int i=0; i<m_page.m_elements.size(); i++)
105                 {
106                         eRect &area = m_page.m_elements[i].m_area;
107                         area.setLeft(startX);
108                         if (!original_position)
109                         {
110                                 int lowerborder = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_position", 50);
111                                 if (lowerborder == 0)
112                                         lowerborder -= 100 * getDesktop(0)->size().height()/720;
113                                 else if (lowerborder == 50)
114                                         lowerborder -= 50 * getDesktop(0)->size().height()/720;
115                                 area.setTop(size_per_element * i + startY - lowerborder);
116                         }
117                         else
118                                 area.setTop(size_per_element * i + startY);
119                         area.setWidth(width);
120                         area.setHeight(size_per_element);
121                         m_visible_region.rects.push_back(area);
122                 }
123         }
124         m_hide_subtitles_timer->start(7500, true);
125         invalidate(m_visible_region);  // invalidate new regions
126 }
127
128 void eSubtitleWidget::setPage(const eDVBSubtitlePage &p)
129 {
130         eDebug("setPage");
131         m_dvb_page = p;
132         invalidate(m_visible_region);  // invalidate old visible regions
133         m_visible_region.rects.clear();
134         int line = 0;
135         bool original_position = ePythonConfigQuery::getConfigBoolValue("config.subtitles.subtitle_original_position");
136         for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
137         {
138                 if (!original_position)
139                 {
140                         int lines = m_dvb_page.m_regions.size();
141                         int lowerborder = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_position", -1);
142                         if (lowerborder >= 0)
143                         {
144                                 it->m_position = ePoint(it->m_position.x(), p.m_display_size.height() - (lines - line) * it->m_pixmap->size().height() - lowerborder);
145                         }
146                         line++;
147                 }
148                 eDebug("add %d %d %d %d", it->m_position.x(), it->m_position.y(), it->m_pixmap->size().width(), it->m_pixmap->size().height());
149                 eDebug("disp width %d, disp height %d", p.m_display_size.width(), p.m_display_size.height());
150                 eRect r = eRect(it->m_position, it->m_pixmap->size());
151                 r.scale(size().width(), p.m_display_size.width(), size().height(), p.m_display_size.height());
152                 m_visible_region.rects.push_back(r);
153         }
154         m_dvb_page_ok = 1;
155         m_hide_subtitles_timer->start(7500, true);
156         invalidate(m_visible_region);  // invalidate new regions
157 }
158
159 void eSubtitleWidget::setPage(const ePangoSubtitlePage &p)
160 {
161         m_pango_page = p;
162         m_pango_page_ok = 1;
163         invalidate(m_visible_region);  // invalidate old visible regions
164         m_visible_region.rects.clear();
165
166         bool rewrap_enabled = ePythonConfigQuery::getConfigBoolValue("config.subtitles.subtitle_rewrap");
167         bool colourise_dialogs_enabled = ePythonConfigQuery::getConfigBoolValue("config.subtitles.colourise_dialogs");
168         bool original_position = ePythonConfigQuery::getConfigBoolValue("config.subtitles.subtitle_original_position");
169
170         int elements = m_pango_page.m_elements.size();
171
172         if(rewrap_enabled | colourise_dialogs_enabled)
173         {
174                 size_t ix, colourise_dialogs_current = 0;
175                 std::vector<std::string> colourise_dialogs_colours;
176                 std::string replacement;
177                 std::string alignmentValue;
178                 ePythonConfigQuery::getConfigValue("config.subtitles.subtitle_alignment", alignmentValue);
179                 bool alignment_center = (alignmentValue == "center");
180
181                 if(colourise_dialogs_enabled)
182                 {
183                         colourise_dialogs_colours.push_back((std::string)gRGB(0xff, 0xff, 0x00));       // yellow
184                         colourise_dialogs_colours.push_back((std::string)gRGB(0x00, 0xff, 0xff));       // cyan
185                         colourise_dialogs_colours.push_back((std::string)gRGB(0xff, 0x00, 0xff));       // magenta
186                         colourise_dialogs_colours.push_back((std::string)gRGB(0x00, 0xff, 0x00));       // green
187                         colourise_dialogs_colours.push_back((std::string)gRGB(0xff, 0xaa, 0xaa));       // light red
188                         colourise_dialogs_colours.push_back((std::string)gRGB(0xaa, 0xaa, 0xff));       // light blue
189                 }
190
191                 for (int i=0; i<elements; ++i)
192                 {
193                         std::string& line = m_pango_page.m_elements[i].m_pango_line;
194
195                         for (ix = 0; ix < line.length(); ix++)
196                         {
197                                 if(rewrap_enabled && !line.compare(ix, 1, "\n"))
198                                         line.replace(ix, 1, " ");
199
200                                 if(colourise_dialogs_enabled && !line.compare(ix, 2, "- "))
201                                 {
202                                         /* workaround for rendering fault when colouring is enabled, rewrap is off and alignment is center */
203                                         replacement = std::string((!rewrap_enabled && alignment_center) ? "  " : "") + colourise_dialogs_colours.at(colourise_dialogs_current);
204
205                                         line.replace(ix, 2, replacement);
206                                         colourise_dialogs_current++;
207
208                                         if(colourise_dialogs_current >= colourise_dialogs_colours.size())
209                                                 colourise_dialogs_current = 0;
210                                 }
211                         }
212                 }
213         }
214
215         if (elements)
216         {
217                 int startY = elements > 1
218                         ? size().height() / 2
219                         : size().height() / 3 * 2;
220                 int width = size().width() - startX * 2;
221                 int height = size().height() - startY;
222                 int size_per_element = height / (elements ? elements : 1);
223                 for (int i=0; i<elements; ++i)
224                 {
225                         eRect &area = m_pango_page.m_elements[i].m_area;
226                         area.setLeft(startX);
227                         if (!original_position)
228                         {
229                                 int lowerborder = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_position", 50);
230                                 if (lowerborder == 0)
231                                         lowerborder -= 100 * getDesktop(0)->size().height()/720;
232                                 else if (lowerborder == 50)
233                                         lowerborder -= 50 * getDesktop(0)->size().height()/720;
234                                 area.setTop(size_per_element * i + startY - lowerborder);
235                         }
236                         else
237                                 area.setTop(size_per_element * i + startY);
238                         area.setWidth(width);
239                         area.setHeight(size_per_element);
240                         m_visible_region.rects.push_back(area);
241                 }
242         }
243         int timeout_ms = m_pango_page.m_timeout;
244         m_hide_subtitles_timer->start(timeout_ms, true);
245         invalidate(m_visible_region);  // invalidate new regions
246 }
247
248 void eSubtitleWidget::clearPage()
249 {
250         eDebug("subtitle timeout... hide");
251         m_page_ok = 0;
252         m_dvb_page_ok = 0;
253         m_pango_page_ok = 0;
254         invalidate(m_visible_region);
255         m_visible_region.rects.clear();
256 }
257
258 void eSubtitleWidget::setPixmap(ePtr<gPixmap> &pixmap, gRegion changed, eRect pixmap_dest)
259 {
260         m_pixmap = pixmap;
261         m_pixmap_dest = pixmap_dest; /* this is in a virtual 720x576 cage */
262         
263                 /* 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 */
264         changed.scale(m_pixmap_dest.width(), 720, m_pixmap_dest.height(), 576);
265         changed.moveBy(ePoint(m_pixmap_dest.x(), m_pixmap_dest.y()));
266
267         if (pixmap->size().width() && pixmap->size().height())
268                 changed.scale(size().width(), pixmap->size().width(), size().height(), pixmap->size().height());
269         
270         invalidate(changed);
271 }
272
273 int eSubtitleWidget::event(int event, void *data, void *data2)
274 {
275         switch (event)
276         {
277         case evtPaint:
278         {
279                 ePtr<eWindowStyle> style;
280                 gPainter &painter = *(gPainter*)data2;
281
282                 getStyle(style);
283                 eWidget::event(event, data, data2);
284
285                 std::string alignmentValue;
286                 int rt_halignment_flag;
287                 ePythonConfigQuery::getConfigValue("config.subtitles.subtitle_alignment", alignmentValue);
288                 if (alignmentValue == "right")
289                         rt_halignment_flag = gPainter::RT_HALIGN_RIGHT;
290                 else if (alignmentValue == "left")
291                         rt_halignment_flag = gPainter::RT_HALIGN_LEFT;
292                 else
293                         rt_halignment_flag = gPainter::RT_HALIGN_CENTER;
294
295                 int fontsize = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_fontsize", 34) * getDesktop(0)->size().width()/1280;
296                 int edgestyle = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_edgestyle");
297                 int borderwidth = (edgestyle == FONTSTYLE_UNIFORM) ? ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_edgestyle_level") : 0;
298                 gRGB bordercolor = gRGB();
299                 bool original_colors = false;
300
301                 if (m_pixmap)
302                 {
303                         eRect r = m_pixmap_dest;
304                         r.scale(size().width(), 720, size().height(), 576);
305                         painter.blitScale(m_pixmap, r);
306                 } else if (m_page_ok)
307                 {
308                         int elements = m_page.m_elements.size();
309
310                         subtitleStyles[Subtitle_TTX].font->pointSize = fontsize;
311
312                         painter.setFont(subtitleStyles[Subtitle_TTX].font);
313                         for (int i=0; i<elements; ++i)
314                         {
315                                 eDVBTeletextSubtitlePageElement &element = m_page.m_elements[i];
316                                 eRect &area = element.m_area;
317                                 gRGB fontcolor = (!subtitleStyles[Subtitle_TTX].have_foreground_color) ? element.m_color : subtitleStyles[Subtitle_TTX].foreground_color;
318                                 int bg_r, bg_g, bg_b, bg_a;
319                                 if (ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_bgopacity") < 0xFF)
320                                 {
321                                         unsigned int padding = 10;
322                                         eTextPara *para = new eTextPara(area);
323                                         para->setFont(subtitleStyles[Subtitle_TTX].font);
324                                         para->renderString(element.m_text.c_str(), RS_WRAP);
325
326                                         eRect bgbox = para->getBoundBox();
327                                         int bgboxWidth = bgbox.width();
328                                         int bgboxHeight = bgbox.height();
329                                         if (alignmentValue == "left")
330                                                 bgbox.setLeft(area.left() - padding - borderwidth);
331                                         else if (alignmentValue == "right")
332                                                 bgbox.setLeft(area.left() + area.width() - bgboxWidth - padding - borderwidth);
333                                         else
334                                                 bgbox.setLeft(area.left() + area.width() / 2 - bgboxWidth / 2 - padding - borderwidth);
335                                         bgbox.setTop(area.top() + area.height() / 2 - bgboxHeight / 2 - padding * 2 - borderwidth);
336                                         bgbox.setWidth(bgboxWidth + padding * 2 + borderwidth * 2);
337                                         bgbox.setHeight(bgboxHeight + padding * 3 + borderwidth * 2);
338
339                                         switch (ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_bgcolor", 0))
340                                         {
341                                                 case BGCOLOR_WHITE:
342                                                         bg_r = 255;
343                                                         bg_g = 255;
344                                                         bg_b = 255;
345                                                         break;
346                                                 case BGCOLOR_YELLOW:
347                                                         bg_r = 255;
348                                                         bg_g = 255;
349                                                         bg_b = 0;
350                                                         break;
351                                                 case BGCOLOR_GREEN:
352                                                         bg_r = 0;
353                                                         bg_g = 255;
354                                                         bg_b = 0;
355                                                         break;
356                                                 case BGCOLOR_CYAN:
357                                                         bg_r = 0;
358                                                         bg_g = 255;
359                                                         bg_b = 255;
360                                                         break;
361                                                 case BGCOLOR_BLUE:
362                                                         bg_r = 0;
363                                                         bg_g = 0;
364                                                         bg_b = 255;
365                                                         break;
366                                                 case BGCOLOR_MAGNETA:
367                                                         bg_r = 255;
368                                                         bg_g = 0;
369                                                         bg_b = 255;
370                                                         break;
371                                                 case BGCOLOR_RED:
372                                                         bg_r = 255;
373                                                         bg_g = 0;
374                                                         bg_b = 0;
375                                                         break;
376                                                 case BGCOLOR_BLACK:
377                                                 default:
378                                                         bg_r = 0;
379                                                         bg_g = 0;
380                                                         bg_b = 0;
381                                                         break;
382                                         }
383                                         bg_a = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_bgopacity", 0);
384
385                                         painter.setForegroundColor(gRGB(bg_r, bg_g, bg_b, bg_a));
386                                         painter.fill(bgbox);
387                                 }
388
389                                 int offset = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_edgestyle_level", 3);
390                                 switch(edgestyle)
391                                 {
392                                         default:
393                                         case FONTSTYLE_NONE:
394                                                 offset = 0;
395                                                 borderwidth = 0;
396                                                 break;
397                                         case FONTSTYLE_RAISED:
398                                         {
399                                                 eRect shadow = area;
400                                                 ePoint shadow_offset = ePoint(-offset, -offset);
401                                                 shadow.moveBy(shadow_offset);
402                                                 painter.setForegroundColor(subtitleStyles[Subtitle_TTX].shadow_color);
403                                                 painter.renderText(shadow, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|rt_halignment_flag);
404                                         }
405                                                 break;
406                                         case FONTSTYLE_DEPRESSED:
407                                         {
408                                                 eRect shadow = area;
409                                                 ePoint shadow_offset = ePoint(offset, offset);
410                                                 shadow.moveBy(shadow_offset);
411                                                 painter.setForegroundColor(subtitleStyles[Subtitle_TTX].shadow_color);
412                                                 painter.renderText(shadow, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|rt_halignment_flag);
413                                         }
414                                                 break;
415                                         case FONTSTYLE_UNIFORM:
416                                         {
417                                                 if (borderwidth > 0)
418                                                 {
419                                                         if (fontcolor.r == 0 && fontcolor.g == 0 && fontcolor.b == 0)
420                                                         {
421                                                                 gRGB tmp_border_white = gRGB(255,255,255);
422                                                                 bordercolor = tmp_border_white;
423                                                         }
424                                                         else if (bg_r == 0 && bg_g == 0 && bg_b == 0)
425                                                         {
426                                                                 borderwidth = 0;
427                                                         }
428                                                 }
429                                         }
430                                                 break;
431                                 }
432
433                                 if ( !subtitleStyles[Subtitle_TTX].have_foreground_color )
434                                         painter.setForegroundColor(element.m_color);
435                                 else
436                                         painter.setForegroundColor(subtitleStyles[Subtitle_TTX].foreground_color);
437                                 painter.renderText(area, element.m_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|rt_halignment_flag, bordercolor, borderwidth);
438                         }
439                 }
440                 else if (m_pango_page_ok)
441                 {
442                         int elements = m_pango_page.m_elements.size();
443                         subfont_t face;
444
445                         for (int i=0; i<elements; ++i)
446                         {
447                                 face = Subtitle_Regular;
448                                 ePangoSubtitlePageElement &element = m_pango_page.m_elements[i];
449                                 std::string text = element.m_pango_line;
450                                 text = replace_all(text, "&apos;", "'");
451                                 text = replace_all(text, "&quot;", "\"");
452                                 text = replace_all(text, "&amp;", "&");
453                                 text = replace_all(text, "&lt;", "<");
454                                 text = replace_all(text, "&gt;", ">");
455
456                                 std::string shadow_text = text;
457                                 if (edgestyle == FONTSTYLE_RAISED || edgestyle == FONTSTYLE_DEPRESSED)
458                                 {
459                                         shadow_text = replace_all(shadow_text, "</u>", "");
460                                         shadow_text = replace_all(shadow_text, "</i>", "");
461                                         shadow_text = replace_all(shadow_text, "</b>", "");
462                                         shadow_text = replace_all(shadow_text, "<u>", "");
463                                         shadow_text = replace_all(shadow_text, "<i>", "");
464                                         shadow_text = replace_all(shadow_text, "<b>", "");
465                                 }
466
467                                 if (ePythonConfigQuery::getConfigBoolValue("config.subtitles.pango_subtitle_fontswitch"))
468                                 {
469                                         if (text.find("<i>") != std::string::npos || text.find("</i>") != std::string::npos)
470                                                 face = Subtitle_Italic;
471                                         else if (text.find("<b>") != std::string::npos || text.find("</b>") != std::string::npos)
472                                                 face = Subtitle_Bold;
473                                 }
474                                 int subtitleColors = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_fontcolor", 1);
475                                 if (!subtitleColors)
476                                         {
477                                                 text = replace_all(text, "<i>", gRGB(255,255,0));
478                                                 text = replace_all(text, "<b>", gRGB(0,255,255));
479                                                 text = replace_all(text, "<u>", (std::string) gRGB(0,255,0));
480                                                 text = replace_all(text, "</i>", (std::string) gRGB(255,255,255));
481                                                 text = replace_all(text, "</b>", (std::string) gRGB(255,255,255));
482                                                 text = replace_all(text, "</u>", (std::string) gRGB(255,255,255));
483                                         }
484                                 else
485                                 {
486                                         text = replace_all(text, "</u>", "");
487                                         text = replace_all(text, "</i>", "");
488                                         text = replace_all(text, "</b>", "");
489                                         text = replace_all(text, "<u>", "");
490                                         text = replace_all(text, "<i>", "");
491                                         text = replace_all(text, "<b>", "");
492                                 }
493
494                                 gRGB fontcolor = (!subtitleStyles[face].have_foreground_color) ? element.m_color : subtitleStyles[face].foreground_color;
495                                 switch (subtitleColors)
496                                 {
497                                         default:
498                                         case 0: /* use original colors */
499                                                 original_colors = true;
500                                                 break;
501                                         case FONTCOLOR_WHITE:
502                                                 fontcolor = gRGB(255, 255, 255);
503                                                 break;
504                                         case FONTCOLOR_YELLOW:
505                                                 fontcolor = gRGB(255, 255, 0);
506                                                 break;
507                                         case FONTCOLOR_GREEN:
508                                                 fontcolor = gRGB(0, 255, 0);
509                                                 break;
510                                         case FONTCOLOR_CYAN:
511                                                 fontcolor = gRGB(0, 255, 255);
512                                                 break;
513                                         case FONTCOLOR_BLUE:
514                                                 fontcolor = gRGB(0, 0, 255);
515                                                 break;
516                                         case FONTCOLOR_MAGNETA:
517                                                 fontcolor = gRGB(255, 0, 255);
518                                                 break;
519                                         case FONTCOLOR_RED:
520                                                 fontcolor = gRGB(255, 0, 0);
521                                                 break;
522                                         case FONTCOLOR_BLACK:
523                                                 fontcolor = gRGB(0, 0, 0);
524                                                 break;
525                                 }
526                                 fontcolor.a = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_opacity");
527                                 if (!original_colors)
528                                         text = (std::string)fontcolor + text;
529
530                                 subtitleStyles[face].font->pointSize = fontsize;
531                                 painter.setFont(subtitleStyles[face].font);
532                                 eRect &area = element.m_area;
533                                 int bg_r, bg_g, bg_b, bg_a;
534                                 if (ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_bgopacity") < 0xFF)
535                                 {
536                                         unsigned int padding = 10;
537                                         eTextPara *para = new eTextPara(area);
538                                         para->setFont(subtitleStyles[face].font);
539                                         para->renderString(text.c_str(), RS_WRAP);
540
541                                         eRect bgbox = para->getBoundBox();
542                                         int bgboxWidth = bgbox.width();
543                                         int bgboxHeight = bgbox.height();
544                                         if (alignmentValue == "left")
545                                                 bgbox.setLeft(area.left() - padding - borderwidth);
546                                         else if (alignmentValue == "right")
547                                                 bgbox.setLeft(area.left() + area.width() - bgboxWidth - padding - borderwidth);
548                                         else
549                                                 bgbox.setLeft(area.left() + area.width() / 2 - bgboxWidth / 2 - padding - borderwidth);
550                                         bgbox.setTop(area.top() + area.height() / 2 - bgboxHeight / 2 - padding * 2 - borderwidth);
551                                         bgbox.setWidth(bgboxWidth + padding * 2 + borderwidth * 2);
552                                         bgbox.setHeight(bgboxHeight + padding * 3 + borderwidth * 2);
553
554                                         switch (ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_bgcolor", 0))
555                                         {
556                                                 case BGCOLOR_WHITE:
557                                                         bg_r = 255;
558                                                         bg_g = 255;
559                                                         bg_b = 255;
560                                                         break;
561                                                 case BGCOLOR_YELLOW:
562                                                         bg_r = 255;
563                                                         bg_g = 255;
564                                                         bg_b = 0;
565                                                         break;
566                                                 case BGCOLOR_GREEN:
567                                                         bg_r = 0;
568                                                         bg_g = 255;
569                                                         bg_b = 0;
570                                                         break;
571                                                 case BGCOLOR_CYAN:
572                                                         bg_r = 0;
573                                                         bg_g = 255;
574                                                         bg_b = 255;
575                                                         break;
576                                                 case BGCOLOR_BLUE:
577                                                         bg_r = 0;
578                                                         bg_g = 0;
579                                                         bg_b = 255;
580                                                         break;
581                                                 case BGCOLOR_MAGNETA:
582                                                         bg_r = 255;
583                                                         bg_g = 0;
584                                                         bg_b = 255;
585                                                         break;
586                                                 case BGCOLOR_RED:
587                                                         bg_r = 255;
588                                                         bg_g = 0;
589                                                         bg_b = 0;
590                                                         break;
591                                                 case BGCOLOR_BLACK:
592                                                 default:
593                                                         bg_r = 0;
594                                                         bg_g = 0;
595                                                         bg_b = 0;
596                                                         break;
597                                         }
598                                         bg_a = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_bgopacity", 0);
599
600                                         painter.setForegroundColor(gRGB(bg_r, bg_g, bg_b, bg_a));
601                                         painter.fill(bgbox);
602                                 }
603
604                                 int offset = ePythonConfigQuery::getConfigIntValue("config.subtitles.subtitle_edgestyle_level", 3);
605                                 switch(edgestyle)
606                                 {
607                                         default:
608                                         case FONTSTYLE_NONE:
609                                                 offset = 0;
610                                                 borderwidth = 0;
611                                                 break;
612                                         case FONTSTYLE_RAISED:
613                                         {
614                                                 eRect shadow = area;
615                                                 ePoint shadow_offset = ePoint(-offset, -offset);
616                                                 shadow.moveBy(shadow_offset);
617                                                 painter.setForegroundColor(subtitleStyles[face].shadow_color);
618                                                 painter.renderText(shadow, shadow_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|rt_halignment_flag);
619                                         }
620                                                 break;
621                                         case FONTSTYLE_DEPRESSED:
622                                         {
623                                                 eRect shadow = area;
624                                                 ePoint shadow_offset = ePoint(offset, offset);
625                                                 shadow.moveBy(shadow_offset);
626                                                 painter.setForegroundColor(subtitleStyles[face].shadow_color);
627                                                 painter.renderText(shadow, shadow_text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|rt_halignment_flag);
628                                         }
629                                                 break;
630                                         case FONTSTYLE_UNIFORM:
631                                         {
632                                                 if (borderwidth > 0)
633                                                 {
634                                                         if (fontcolor.r == 0 && fontcolor.g == 0 && fontcolor.b == 0)
635                                                         {
636                                                                 gRGB tmp_border_white = gRGB(255,255,255);
637                                                                 bordercolor = tmp_border_white;
638                                                         }
639                                                         else if (bg_r == 0 && bg_g == 0 && bg_b == 0)
640                                                         {
641                                                                 borderwidth = 0;
642                                                         }
643                                                 }
644                                         }
645                                                 break;
646                                 }
647
648                                 if ( !subtitleStyles[face].have_foreground_color && element.m_have_color )
649                                         painter.setForegroundColor(element.m_color);
650                                 else
651                                         painter.setForegroundColor(subtitleStyles[face].foreground_color);
652                                 painter.renderText(area, text, gPainter::RT_WRAP|gPainter::RT_VALIGN_CENTER|rt_halignment_flag, bordercolor, borderwidth);
653                         }
654                 }
655                 else if (m_dvb_page_ok)
656                 {
657                         for (std::list<eDVBSubtitleRegion>::iterator it(m_dvb_page.m_regions.begin()); it != m_dvb_page.m_regions.end(); ++it)
658                         {
659                                 eRect r = eRect(it->m_position, it->m_pixmap->size());
660                                 r.scale(size().width(), m_dvb_page.m_display_size.width(), size().height(),  m_dvb_page.m_display_size.height());
661                                 painter.blitScale(it->m_pixmap, r);
662                         }
663                 }
664                 return 0;
665         }
666         default:
667                 return eWidget::event(event, data, data2);
668         }
669 }
670
671 void eSubtitleWidget::setFontStyle(subfont_t face, gFont *font, int haveColor, const gRGB &col, const gRGB &shadowCol, const ePoint &shadowOffset)
672 {
673         subtitleStyles[face].font = font;
674         subtitleStyles[face].have_foreground_color = haveColor;
675         subtitleStyles[face].foreground_color = col;
676         subtitleStyles[face].shadow_color = shadowCol;
677         subtitleStyles[face].shadow_offset = shadowOffset;
678 }
679