Italics/Bold theme to CSS. Share css for both QTextDocument and QWebView

This commit is contained in:
Jonathan Corwin 2010-09-03 23:03:54 +01:00
parent b2563f0ce0
commit 1fca6ec81d
4 changed files with 64 additions and 59 deletions

View File

@ -324,7 +324,7 @@ from settingstab import SettingsTab
from serviceitem import ServiceItem from serviceitem import ServiceItem
from serviceitem import ServiceItemType from serviceitem import ServiceItemType
from serviceitem import ItemCapabilities from serviceitem import ItemCapabilities
from htmlbuilder import build_html from htmlbuilder import build_html, build_lyrics_format_css
from toolbar import OpenLPToolbar from toolbar import OpenLPToolbar
from dockwidget import OpenLPDockWidget from dockwidget import OpenLPDockWidget
from theme import ThemeLevel, ThemeXML from theme import ThemeLevel, ThemeXML

View File

@ -263,13 +263,10 @@ def build_html(item, screen, alert, islive):
`islive` `islive`
Item is going live, rather than preview/theme building Item is going live, rather than preview/theme building
""" """
try:
webkitvers = float(QtWebKit.qWebKitVersion())
except AttributeError:
webkitvers = 0
width = screen[u'size'].width() width = screen[u'size'].width()
height = screen[u'size'].height() height = screen[u'size'].height()
theme = item.themedata theme = item.themedata
webkitvers = webkit_version()
if item.bg_frame: if item.bg_frame:
image = u'data:image/png;base64,%s' % image_to_byte(item.bg_frame) image = u'data:image/png;base64,%s' % image_to_byte(item.bg_frame)
else: else:
@ -284,9 +281,16 @@ def build_html(item, screen, alert, islive):
build_lyrics_html(item, webkitvers)) build_lyrics_html(item, webkitvers))
return html return html
def webkit_version():
try:
webkitvers = float(QtWebKit.qWebKitVersion())
except AttributeError:
webkitvers = 0
return webkitvers
def build_lyrics_css(item, webkitvers): def build_lyrics_css(item, webkitvers):
""" """
Build the video display css Build the lyrics display css
`item` `item`
Service Item containing theme and location information Service Item containing theme and location information
@ -304,7 +308,6 @@ def build_lyrics_css(item, webkitvers):
} }
.lyricscell { .lyricscell {
display:table-cell; display:table-cell;
word-wrap: break-word;
%s %s
} }
.lyricsmain { .lyricsmain {
@ -326,24 +329,9 @@ def build_lyrics_css(item, webkitvers):
if theme: if theme:
lyricstable = u'left: %spx; top: %spx;' % \ lyricstable = u'left: %spx; top: %spx;' % \
(item.main.x(), item.main.y()) (item.main.x(), item.main.y())
if theme.display_horizontalAlign == 2: lyrics = build_lyrics_format_css(theme)
align = u'center' lyrics += u'width: %spx; height: %spx; ' % \
elif theme.display_horizontalAlign == 1: (item.main.width(), item.main.height())
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'width: %spx; height: %spx; text-align: %s; ' \
'vertical-align: %s; font-family: %s; font-size: %spt; ' \
'color: %s; line-height: %d%%;' % \
(item.main.width(), item.main.height(), align, valign,
theme.font_main_name, theme.font_main_proportion,
theme.font_main_color, 100 + int(theme.font_main_line_adjustment))
# For performance reasons we want to show as few DIV's as possible, # For performance reasons we want to show as few DIV's as possible,
# especially when animating/transitions. # especially when animating/transitions.
# However some bugs in older versions of qtwebkit mean we need to # However some bugs in older versions of qtwebkit mean we need to
@ -360,8 +348,6 @@ def build_lyrics_css(item, webkitvers):
# webkit-text-stroke was used. So use an offset text layer underneath. # webkit-text-stroke was used. So use an offset text layer underneath.
# https://bugs.webkit.org/show_bug.cgi?id=19728 # https://bugs.webkit.org/show_bug.cgi?id=19728
if theme.display_outline: if theme.display_outline:
if webkitvers < 534.3:
lyrics += u' letter-spacing: 1px;'
outline = u' -webkit-text-stroke: %sem %s; ' \ outline = u' -webkit-text-stroke: %sem %s; ' \
'-webkit-text-fill-color: %s; ' % \ '-webkit-text-fill-color: %s; ' % \
(float(theme.display_outline_size) / 16, (float(theme.display_outline_size) / 16,
@ -383,6 +369,44 @@ def build_lyrics_css(item, webkitvers):
lyrics_css = style % (lyricstable, lyrics, lyricsmain, outline, shadow) lyrics_css = style % (lyricstable, lyrics, lyricsmain, outline, shadow)
return lyrics_css return lyrics_css
def build_lyrics_format_css(theme):
"""
Build the css which controls the theme format
Also used by renderer for splitting verses
`item`
Service Item containing theme and location information
`webkitvers`
The version of qtwebkit we're using
"""
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'word-wrap: break-word; ' \
'text-align: %s; vertical-align: %s; font-family: %s; ' \
'font-size: %spt; color: %s; line-height: %d%%;' % \
(align, valign, theme.font_main_name, theme.font_main_proportion,
theme.font_main_color, 100 + int(theme.font_main_line_adjustment))
if theme.display_outline:
if webkit_version() < 534.3:
lyrics += u' letter-spacing: 1px;'
if theme.font_main_italics:
lyrics += ' font-style:italic; '
if theme.font_main_weight == u'Bold':
lyrics += ' font-weight:bold; '
return lyrics
def build_lyrics_html(item, webkitvers): def build_lyrics_html(item, webkitvers):
""" """
Build the HTML required to show the lyrics Build the HTML required to show the lyrics

View File

@ -31,7 +31,7 @@ import logging
from PyQt4 import QtGui, QtCore from PyQt4 import QtGui, QtCore
from openlp.core.lib import resize_image, expand_tags from openlp.core.lib import resize_image, expand_tags, build_lyrics_format_css
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -145,39 +145,20 @@ class Renderer(object):
text.append(line) text.append(line)
doc = QtGui.QTextDocument() doc = QtGui.QTextDocument()
doc.setPageSize(QtCore.QSizeF(self._rect.width(), self._rect.height())) doc.setPageSize(QtCore.QSizeF(self._rect.width(), self._rect.height()))
df = doc.defaultFont()
df.setPointSize(self._theme.font_main_proportion)
df.setFamily(self._theme.font_main_name)
main_weight = 50
if self._theme.font_main_weight == u'Bold':
main_weight = 75
df.setWeight(main_weight)
doc.setDefaultFont(df)
layout = doc.documentLayout() layout = doc.documentLayout()
formatted = [] formatted = []
if self._theme.font_main_weight == u'Bold' and \ shell = u'<div style="%s">' % build_lyrics_format_css(self._theme)
self._theme.font_main_italics: html_text = u''
shell = u'{p}{st}{it}%s{/it}{/st}{/p}' styled_text = shell
elif self._theme.font_main_weight == u'Bold' and \
not self._theme.font_main_italics:
shell = u'{p}{st}%s{/st}{/p}'
elif self._theme.font_main_italics:
shell = u'{p}{it}%s{/it}{/p}'
else:
shell = u'{p}%s{/p}'
temp_text = u''
old_html_text = u''
for line in text: for line in text:
# mark line ends styled_text += expand_tags(line) + line_end
temp_text = temp_text + line + line_end doc.setHtml(styled_text + u'</div>')
html_text = shell % expand_tags(temp_text) # Text too long so go to next page
doc.setHtml(html_text)
# Text too long so gone to next mage
if layout.pageCount() != 1: if layout.pageCount() != 1:
formatted.append(shell % old_html_text) formatted.append(html_text)
temp_text = line + line_end styled_text = shell
old_html_text = temp_text html_text += line + line_end
formatted.append(shell % old_html_text) formatted.append(html_text)
log.debug(u'format_slide - End') log.debug(u'format_slide - End')
return formatted return formatted

View File

@ -160,9 +160,9 @@ class ServiceItem(object):
self.themedata = self.render_manager.renderer._theme self.themedata = self.render_manager.renderer._theme
for slide in self._raw_frames: for slide in self._raw_frames:
before = time.time() before = time.time()
formated = self.render_manager \ formatted = self.render_manager \
.format_slide(slide[u'raw_slide'], line_break) .format_slide(slide[u'raw_slide'], line_break)
for page in formated: for page in formatted:
self._display_frames.append( self._display_frames.append(
{u'title': clean_tags(page), {u'title': clean_tags(page),
u'text': clean_tags(page.rstrip()), u'text': clean_tags(page.rstrip()),