openlp/openlp/core/lib/htmlbuilder.py

563 lines
17 KiB
Python
Raw Normal View History

2010-07-11 10:58:36 +00:00
# -*- coding: utf-8 -*-
# vim: autoindent shiftwidth=4 expandtab textwidth=80 tabstop=4 softtabstop=4
###############################################################################
# OpenLP - Open Source Lyrics Projection #
# --------------------------------------------------------------------------- #
# Copyright (c) 2008-2010 Raoul Snyman #
# Portions copyright (c) 2008-2010 Tim Bentley, Jonathan Corwin, Michael #
2010-07-25 09:03:01 +00:00
# Gorven, Scott Guerrieri, Meinert Jordan, Andreas Preikschat, Christian #
# Richter, Philip Ridout, Maikel Stuivenberg, Martin Thompson, Jon Tibble, #
# Carsten Tinggaard, Frode Woldsund #
2010-07-11 10:58:36 +00:00
# --------------------------------------------------------------------------- #
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 2 of the License. #
# #
# This program is distributed in the hope that it will be useful, but WITHOUT #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
# more details. #
# #
# You should have received a copy of the GNU General Public License along #
# with this program; if not, write to the Free Software Foundation, Inc., 59 #
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
###############################################################################
2010-09-04 07:48:58 +00:00
import logging
from PyQt4 import QtWebKit
2010-07-11 10:58:36 +00:00
from openlp.core.lib import image_to_byte
2010-09-04 07:48:58 +00:00
log = logging.getLogger(__name__)
2010-07-11 10:58:36 +00:00
HTMLSRC = u"""
<html>
<head>
<title>OpenLP Display</title>
<style>
*{
margin: 0;
2010-08-09 21:21:04 +00:00
padding: 0;
border: 0;
2010-09-04 17:55:10 +00:00
overflow: hidden;
2010-07-11 10:58:36 +00:00
}
2010-08-07 06:18:05 +00:00
body {
2010-09-04 07:48:58 +00:00
%s;
2010-08-07 06:18:05 +00:00
}
.size {
position: absolute;
left: 0px;
2010-08-09 21:21:04 +00:00
top: 0px;
width: %spx;
height: %spx;
2010-08-09 21:21:04 +00:00
}
#black {
2010-08-09 21:21:04 +00:00
z-index:8;
background-color: black;
2010-08-09 21:21:04 +00:00
display: none;
}
#image {
z-index:1;
}
#video {
z-index:2;
2010-08-09 21:21:04 +00:00
}
#alert {
position: absolute;
left: 0px;
2010-08-09 21:21:04 +00:00
top: 0px;
z-index:10;
%s
2010-08-09 21:21:04 +00:00
}
#footer {
position: absolute;
z-index:5;
%s
2010-08-09 21:21:04 +00:00
}
/* lyric css */
2010-07-11 10:58:36 +00:00
%s
2010-07-11 10:58:36 +00:00
</style>
<script language="javascript">
2010-08-09 21:21:04 +00:00
var timer = null;
var transition = %s;
2010-08-01 08:28:31 +00:00
2010-08-09 21:21:04 +00:00
function show_video(state, path, volume, loop){
2010-08-07 06:18:05 +00:00
var vid = document.getElementById('video');
2010-08-09 21:21:04 +00:00
if(path != null)
2010-08-07 06:18:05 +00:00
vid.src = path;
2010-08-09 21:21:04 +00:00
if(loop != null){
2010-08-07 06:18:05 +00:00
if(loop)
vid.loop = 'loop';
else
vid.loop = '';
}
2010-08-09 21:21:04 +00:00
if(volume != null){
vid.volume = volume;
}
2010-08-07 06:18:05 +00:00
switch(state){
case 'play':
vid.play();
vid.style.display = 'block';
break;
case 'pause':
vid.pause();
vid.style.display = 'block';
break;
case 'stop':
vid.pause();
vid.style.display = 'none';
break;
case 'close':
vid.pause();
vid.style.display = 'none';
2010-08-09 21:21:04 +00:00
vid.src = '';
2010-08-07 06:18:05 +00:00
break;
}
}
2010-08-09 21:21:04 +00:00
function show_image(src){
2010-08-07 06:18:05 +00:00
var img = document.getElementById('image');
img.src = src;
2010-08-09 21:21:04 +00:00
if(src == '')
2010-08-07 06:18:05 +00:00
img.style.display = 'none';
else
img.style.display = 'block';
}
2010-08-09 21:21:04 +00:00
function show_blank(state){
2010-08-05 05:37:26 +00:00
var black = 'none';
var lyrics = '';
2010-08-07 06:18:05 +00:00
var pause = false;
2010-08-05 05:37:26 +00:00
switch(state){
case 'theme':
2010-08-07 06:18:05 +00:00
lyrics = 'hidden';
pause = true;
2010-08-05 05:37:26 +00:00
break;
case 'black':
2010-08-07 06:18:05 +00:00
black = 'block';
pause = true;
break;
case 'desktop':
pause = true;
2010-08-05 19:01:24 +00:00
break;
2010-08-05 05:37:26 +00:00
}
document.getElementById('black').style.display = black;
document.getElementById('lyricsmain').style.visibility = lyrics;
outline = document.getElementById('lyricsoutline')
if(outline!=null)
outline.style.visibility = lyrics;
shadow = document.getElementById('lyricsshadow')
if(shadow!=null)
shadow.style.visibility = lyrics;
2010-08-07 06:18:05 +00:00
document.getElementById('footer').style.visibility = lyrics;
var vid = document.getElementById('video');
if(vid.src != ''){
if(pause)
vid.pause();
else
vid.play();
}
2010-08-05 05:37:26 +00:00
}
2010-08-09 21:21:04 +00:00
function show_alert(alerttext, position){
2010-08-07 06:18:05 +00:00
var text = document.getElementById('alert');
text.innerHTML = alerttext;
2010-08-09 21:21:04 +00:00
if(alerttext == '') {
2010-08-07 06:18:05 +00:00
text.style.visibility = 'hidden';
return 0;
}
2010-08-09 21:21:04 +00:00
if(position == ''){
position = getComputedStyle(text, '').verticalAlign;
2010-08-07 06:18:05 +00:00
}
2010-08-09 21:21:04 +00:00
switch(position)
2010-08-07 06:18:05 +00:00
{
2010-08-09 21:21:04 +00:00
case 'top':
text.style.top = '0px';
break;
case 'middle':
text.style.top = ((window.innerHeight - text.clientHeight) / 2)
2010-08-09 21:21:04 +00:00
+ 'px';
break;
case 'bottom':
text.style.top = (window.innerHeight - text.clientHeight)
2010-08-09 21:21:04 +00:00
+ 'px';
break;
2010-08-07 06:18:05 +00:00
}
text.style.visibility = 'visible';
return text.clientHeight;
}
2010-08-21 10:07:59 +00:00
function show_footer(footertext){
2010-08-23 22:24:42 +00:00
document.getElementById('footer').innerHTML = footertext;
2010-08-09 21:21:04 +00:00
}
2010-08-09 21:21:04 +00:00
function show_text(newtext){
if(timer != null)
clearTimeout(timer);
2010-09-04 07:48:58 +00:00
text_fade('lyricsmain', newtext);
text_fade('lyricsoutline', newtext);
text_fade('lyricsshadow', newtext);
if(text_opacity()==1) return;
timer = setTimeout(function(){
show_text(newtext);
}, 100);
2010-08-30 18:55:59 +00:00
}
function text_fade(id, newtext){
/*
Using -webkit-transition: opacity 1s linear; would have been preferred
but it isn't currently quick enough when animating multiple layers of
large areas of large text. Therefore do it manually as best we can.
Hopefully in the future we can revisit and do more interesting
transitions using -webkit-transition and -webkit-transform.
However we need to ensure interrupted transitions (quickly change 2
slides) still looks pretty and is zippy.
*/
var text = document.getElementById(id);
if(text==null) return;
if(!transition){
2010-09-01 21:36:02 +00:00
text.innerHTML = newtext;
return;
}
if(newtext==text.innerHTML){
text.style.opacity = parseFloat(text.style.opacity) + 0.3;
2010-09-03 18:30:56 +00:00
if(text.style.opacity>0.7)
text.style.opacity = 1;
2010-08-09 21:21:04 +00:00
} else {
2010-09-03 18:30:56 +00:00
text.style.opacity = parseFloat(text.style.opacity) - 0.3;
2010-09-01 21:36:02 +00:00
if(text.style.opacity<=0.1){
text.innerHTML = newtext;
2010-09-01 21:36:02 +00:00
}
2010-07-30 05:08:49 +00:00
}
}
2010-08-30 20:57:59 +00:00
function text_opacity(){
var text = document.getElementById('lyricsmain');
2010-09-01 21:36:02 +00:00
return getComputedStyle(text, '').opacity;
}
2010-09-04 07:48:58 +00:00
2010-08-09 21:21:04 +00:00
function show_text_complete(){
return (text_opacity()==1);
2010-07-11 10:58:36 +00:00
}
</script>
</head>
<body>
<img id="image" class="size" src="%s" />
<video id="video" class="size"></video>
%s
2010-07-19 18:03:00 +00:00
<div id="footer" class="footer"></div>
<div id="black" class="size"></div>
<div id="alert" style="visibility:hidden;"></div>
2010-07-11 10:58:36 +00:00
</body>
</html>
"""
2010-07-27 19:13:56 +00:00
def build_html(item, screen, alert, islive):
2010-07-25 08:58:08 +00:00
"""
Build the full web paged structure for display
`item`
Service Item to be displayed
`screen`
Current display information
`alert`
Alert display display information
`islive`
Item is going live, rather than preview/theme building
2010-07-25 08:58:08 +00:00
"""
2010-07-11 10:58:36 +00:00
width = screen[u'size'].width()
height = screen[u'size'].height()
2010-07-30 05:08:49 +00:00
theme = item.themedata
webkitvers = webkit_version()
2010-08-09 21:21:04 +00:00
if item.bg_frame:
image = u'data:image/png;base64,%s' % image_to_byte(item.bg_frame)
2010-08-07 06:18:05 +00:00
else:
2010-08-09 21:21:04 +00:00
image = u''
2010-09-04 07:48:58 +00:00
html = HTMLSRC % (build_background_css(item, width, height),
width, height,
build_alert_css(alert, width),
build_footer_css(item),
build_lyrics_css(item, webkitvers),
u'true' if theme and theme.display_slideTransition and islive \
else u'false',
2010-09-04 07:48:58 +00:00
image,
build_lyrics_html(item, webkitvers))
2010-08-09 21:21:04 +00:00
return html
2010-07-11 10:58:36 +00:00
def webkit_version():
2010-09-04 14:23:20 +00:00
"""
Return the Webkit version in use.
Note method added relatively recently, so return 0 if prior to this
"""
try:
webkitvers = float(QtWebKit.qWebKitVersion())
2010-09-04 13:35:15 +00:00
log.debug(u'Webkit version = %s' % webkitvers)
except AttributeError:
webkitvers = 0
return webkitvers
2010-09-04 07:48:58 +00:00
def build_background_css(item, width, height):
"""
Build the background css
`item`
Service Item containing theme and location information
"""
width = int(width) / 2
theme = item.themedata
background = u'background-color: black'
if theme:
if theme.background_type == u'solid':
background = u'background-color: %s' % theme.background_color
else:
if theme.background_direction == u'horizontal':
background = \
2010-09-04 13:35:15 +00:00
u'background: ' \
u'-webkit-gradient(linear, left top, left bottom, ' \
2010-09-04 07:48:58 +00:00
'from(%s), to(%s))' % (theme.background_startColor,
theme.background_endColor)
elif theme.background_direction == u'vertical':
background = \
2010-09-04 13:35:15 +00:00
u'background: -webkit-gradient(linear, left top, ' \
u'right top, from(%s), to(%s))' % \
(theme.background_startColor, theme.background_endColor)
2010-09-04 07:48:58 +00:00
else:
background = \
2010-09-04 13:35:15 +00:00
u'background: -webkit-gradient(radial, %s 50%%, 100, %s ' \
u'50%%, %s, from(%s), to(%s))' % (width, width, width,
theme.background_startColor, theme.background_endColor)
2010-09-04 07:48:58 +00:00
return background
def build_lyrics_css(item, webkitvers):
2010-07-25 08:58:08 +00:00
"""
Build the lyrics display css
2010-07-25 08:58:08 +00:00
`item`
Service Item containing theme and location information
2010-09-04 07:48:58 +00:00
`webkitvers`
The version of qtwebkit we're using
2010-07-25 08:58:08 +00:00
"""
2010-07-28 16:53:54 +00:00
style = """
2010-09-04 07:48:58 +00:00
.lyricstable {
z-index:4;
position: absolute;
display: table;
%s
}
2010-09-04 07:48:58 +00:00
.lyricscell {
display:table-cell;
word-wrap: break-word;
%s
}
2010-09-04 07:48:58 +00:00
.lyricsmain {
%s
}
2010-09-04 07:48:58 +00:00
.lyricsoutline {
%s
}
2010-09-04 07:48:58 +00:00
.lyricsshadow {
%s
}
2010-07-30 05:08:49 +00:00
"""
2010-07-17 08:59:15 +00:00
theme = item.themedata
2010-07-30 05:08:49 +00:00
lyricstable = u''
2010-07-28 16:53:54 +00:00
lyrics = u''
lyricsmain = u''
outline = u''
shadow = u''
2010-07-12 16:49:38 +00:00
if theme:
2010-07-30 05:08:49 +00:00
lyricstable = u'left: %spx; top: %spx;' % \
(item.main.x(), item.main.y())
lyrics = build_lyrics_format_css(theme, item.main.width(),
item.main.height())
# For performance reasons we want to show as few DIV's as possible,
2010-09-04 07:48:58 +00:00
# especially when animating/transitions.
# However some bugs in older versions of qtwebkit mean we need to
# perform workarounds and add extra divs. Only do these when needed.
#
2010-09-04 07:48:58 +00:00
# Before 533.3 the webkit-text-fill colour wasn't displayed, only the
# stroke (outline) color. So put stroke layer underneath the main text.
#
2010-09-04 07:48:58 +00:00
# Before 534.4 the webkit-text-stroke was sometimes out of alignment
# with the fill, or normal text. letter-spacing=1 is workaround
# https://bugs.webkit.org/show_bug.cgi?id=44403
#
2010-09-04 07:48:58 +00:00
# Before 534.4 the text-shadow didn't get displayed when
# webkit-text-stroke was used. So use an offset text layer underneath.
# https://bugs.webkit.org/show_bug.cgi?id=19728
if webkitvers >= 533.3:
lyricsmain += build_lyrics_outline_css(theme)
else:
outline = build_lyrics_outline_css(theme)
if theme.display_shadow:
if theme.display_outline and webkitvers < 534.3:
shadow = u'padding-left: %spx; padding-top: %spx ' % \
(theme.display_shadow_size, theme.display_shadow_size)
shadow += build_lyrics_outline_css(theme, True)
else:
lyricsmain += u' text-shadow: %s %spx %spx;' % \
(theme.display_shadow_color, theme.display_shadow_size,
theme.display_shadow_size)
lyrics_css = style % (lyricstable, lyrics, lyricsmain, outline, shadow)
return lyrics_css
def build_lyrics_outline_css(theme, is_shadow=False):
2010-09-04 14:23:20 +00:00
"""
Build the css which controls the theme outline
Also used by renderer for splitting verses
`theme`
Object containing theme information
`is_shadow`
If true, use the shadow colors instead
"""
if theme.display_outline:
size = float(theme.display_outline_size) / 16
if is_shadow:
fill_color = theme.display_shadow_color
outline_color = theme.display_shadow_color
else:
fill_color = theme.font_main_color
outline_color = theme.display_outline_color
return u' -webkit-text-stroke: %sem %s; ' \
u'-webkit-text-fill-color: %s; ' % (size, outline_color, fill_color)
else:
return u''
def build_lyrics_format_css(theme, width, height):
"""
Build the css which controls the theme format
Also used by renderer for splitting verses
2010-09-04 14:23:20 +00:00
`theme`
Object containing theme information
2010-09-04 14:23:20 +00:00
`width`
Width of the lyrics block
`height`
Height of the lyrics block
"""
if theme.display_horizontalAlign == 2:
align = u'center'
elif theme.display_horizontalAlign == 1:
align = u'right'
else:
align = u'left'
if theme.display_verticalAlign == 2:
valign = u'bottom'
elif theme.display_verticalAlign == 1:
valign = u'middle'
else:
valign = u'top'
lyrics = u'white-space:pre-wrap; word-wrap: break-word; ' \
'text-align: %s; vertical-align: %s; font-family: %s; ' \
'font-size: %spt; color: %s; line-height: %d%%; ' \
'margin:0; padding:0; width: %spx; height: %spx; ' % \
(align, valign, theme.font_main_name, theme.font_main_proportion,
theme.font_main_color, 100 + int(theme.font_main_line_adjustment),
width, height)
if theme.display_outline:
if webkit_version() < 534.3:
lyrics += u' letter-spacing: 1px;'
if theme.font_main_italics:
lyrics += u' font-style:italic; '
if theme.font_main_weight == u'Bold':
lyrics += u' font-weight:bold; '
return lyrics
def build_lyrics_html(item, webkitvers):
"""
Build the HTML required to show the lyrics
2010-07-19 18:03:00 +00:00
`item`
Service Item containing theme and location information
2010-09-04 07:48:58 +00:00
`webkitvers`
The version of qtwebkit we're using
"""
2010-09-04 07:48:58 +00:00
# Bugs in some versions of QtWebKit mean we sometimes need additional
# divs for outline and shadow, since the CSS doesn't work.
# To support vertical alignment middle and bottom, nested div's using
# display:table/display:table-cell are required for each lyric block.
lyrics = u''
theme = item.themedata
if webkitvers < 534.4 and theme and theme.display_outline:
lyrics += u'<div class="lyricstable">' \
2010-09-03 18:30:56 +00:00
u'<div id="lyricsshadow" style="opacity:1" ' \
u'class="lyricscell lyricsshadow"></div></div>'
if webkitvers < 533.3:
lyrics += u'<div class="lyricstable">' \
2010-09-03 18:30:56 +00:00
u'<div id="lyricsoutline" style="opacity:1" ' \
u'class="lyricscell lyricsoutline"></div></div>'
lyrics += u'<div class="lyricstable">' \
2010-09-03 18:30:56 +00:00
u'<div id="lyricsmain" style="opacity:1" ' \
u'class="lyricscell lyricsmain"></div></div>'
return lyrics
2010-09-04 07:48:58 +00:00
def build_footer_css(item):
2010-08-03 04:56:21 +00:00
"""
Build the display of the item footer
`item`
Service Item to be processed.
"""
2010-08-09 21:21:04 +00:00
style = """
2010-08-30 20:57:59 +00:00
left: %spx;
top: %spx;
width: %spx;
height: %spx;
font-family: %s;
font-size: %spt;
color: %s;
text-align: %s;
2010-07-19 18:03:00 +00:00
"""
theme = item.themedata
2010-08-09 21:21:04 +00:00
if not theme:
return u''
if theme.display_horizontalAlign == 2:
align = u'center'
elif theme.display_horizontalAlign == 1:
align = u'right'
else:
align = u'left'
2010-08-25 17:08:24 +00:00
lyrics_html = style % (item.footer.x(), item.footer.y(),
item.footer.width(), item.footer.height(), theme.font_footer_name,
2010-08-09 21:21:04 +00:00
theme.font_footer_proportion, theme.font_footer_color, align)
2010-07-12 16:49:38 +00:00
return lyrics_html
2010-07-11 10:58:36 +00:00
def build_alert_css(alertTab, width):
2010-08-03 04:56:21 +00:00
"""
Build the display of the footer
2010-07-19 18:03:00 +00:00
2010-08-03 04:56:21 +00:00
`alertTab`
Details from the Alert tab for fonts etc
"""
2010-08-01 08:28:31 +00:00
style = """
width: %spx;
2010-08-30 20:57:59 +00:00
vertical-align: %s;
font-family: %s;
font-size: %spt;
color: %s;
background-color: %s;
2010-08-07 06:18:05 +00:00
"""
2010-08-09 21:21:04 +00:00
if not alertTab:
return u''
align = u''
if alertTab.location == 2:
align = u'bottom'
elif alertTab.location == 1:
align = u'middle'
else:
align = u'top'
alert = style % (width, align, alertTab.font_face, alertTab.font_size,
2010-08-09 21:21:04 +00:00
alertTab.font_color, alertTab.bg_color)
return alert