From 1fca6ec81dc9263609cefce500980023d32d6c60 Mon Sep 17 00:00:00 2001 From: Jonathan Corwin Date: Fri, 3 Sep 2010 23:03:54 +0100 Subject: [PATCH 1/4] Italics/Bold theme to CSS. Share css for both QTextDocument and QWebView --- openlp/core/lib/__init__.py | 2 +- openlp/core/lib/htmlbuilder.py | 76 ++++++++++++++++++++++------------ openlp/core/lib/renderer.py | 41 +++++------------- openlp/core/lib/serviceitem.py | 4 +- 4 files changed, 64 insertions(+), 59 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index 721b6ae23..da1778d65 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -324,7 +324,7 @@ from settingstab import SettingsTab from serviceitem import ServiceItem from serviceitem import ServiceItemType from serviceitem import ItemCapabilities -from htmlbuilder import build_html +from htmlbuilder import build_html, build_lyrics_format_css from toolbar import OpenLPToolbar from dockwidget import OpenLPDockWidget from theme import ThemeLevel, ThemeXML diff --git a/openlp/core/lib/htmlbuilder.py b/openlp/core/lib/htmlbuilder.py index d03b7f7cc..f65bfb1e3 100644 --- a/openlp/core/lib/htmlbuilder.py +++ b/openlp/core/lib/htmlbuilder.py @@ -263,13 +263,10 @@ def build_html(item, screen, alert, islive): `islive` Item is going live, rather than preview/theme building """ - try: - webkitvers = float(QtWebKit.qWebKitVersion()) - except AttributeError: - webkitvers = 0 width = screen[u'size'].width() height = screen[u'size'].height() theme = item.themedata + webkitvers = webkit_version() if item.bg_frame: image = u'data:image/png;base64,%s' % image_to_byte(item.bg_frame) else: @@ -284,9 +281,16 @@ def build_html(item, screen, alert, islive): build_lyrics_html(item, webkitvers)) return html +def webkit_version(): + try: + webkitvers = float(QtWebKit.qWebKitVersion()) + except AttributeError: + webkitvers = 0 + return webkitvers + def build_lyrics_css(item, webkitvers): """ - Build the video display css + Build the lyrics display css `item` Service Item containing theme and location information @@ -304,7 +308,6 @@ def build_lyrics_css(item, webkitvers): } .lyricscell { display:table-cell; - word-wrap: break-word; %s } .lyricsmain { @@ -326,24 +329,9 @@ def build_lyrics_css(item, webkitvers): if theme: lyricstable = u'left: %spx; top: %spx;' % \ (item.main.x(), item.main.y()) - 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'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)) + lyrics = build_lyrics_format_css(theme) + lyrics += u'width: %spx; height: %spx; ' % \ + (item.main.width(), item.main.height()) # For performance reasons we want to show as few DIV's as possible, # especially when animating/transitions. # 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. # https://bugs.webkit.org/show_bug.cgi?id=19728 if theme.display_outline: - if webkitvers < 534.3: - lyrics += u' letter-spacing: 1px;' outline = u' -webkit-text-stroke: %sem %s; ' \ '-webkit-text-fill-color: %s; ' % \ (float(theme.display_outline_size) / 16, @@ -383,6 +369,44 @@ def build_lyrics_css(item, webkitvers): lyrics_css = style % (lyricstable, lyrics, lyricsmain, outline, shadow) 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): """ Build the HTML required to show the lyrics diff --git a/openlp/core/lib/renderer.py b/openlp/core/lib/renderer.py index 41e547800..dae7c820a 100644 --- a/openlp/core/lib/renderer.py +++ b/openlp/core/lib/renderer.py @@ -31,7 +31,7 @@ import logging 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__) @@ -145,39 +145,20 @@ class Renderer(object): text.append(line) doc = QtGui.QTextDocument() 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() formatted = [] - if self._theme.font_main_weight == u'Bold' and \ - self._theme.font_main_italics: - shell = u'{p}{st}{it}%s{/it}{/st}{/p}' - 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'' + shell = u'
' % build_lyrics_format_css(self._theme) + html_text = u'' + styled_text = shell for line in text: - # mark line ends - temp_text = temp_text + line + line_end - html_text = shell % expand_tags(temp_text) - doc.setHtml(html_text) - # Text too long so gone to next mage + styled_text += expand_tags(line) + line_end + doc.setHtml(styled_text + u'
') + # Text too long so go to next page if layout.pageCount() != 1: - formatted.append(shell % old_html_text) - temp_text = line + line_end - old_html_text = temp_text - formatted.append(shell % old_html_text) + formatted.append(html_text) + styled_text = shell + html_text += line + line_end + formatted.append(html_text) log.debug(u'format_slide - End') return formatted diff --git a/openlp/core/lib/serviceitem.py b/openlp/core/lib/serviceitem.py index 134df0c42..0e8625ce7 100644 --- a/openlp/core/lib/serviceitem.py +++ b/openlp/core/lib/serviceitem.py @@ -160,9 +160,9 @@ class ServiceItem(object): self.themedata = self.render_manager.renderer._theme for slide in self._raw_frames: before = time.time() - formated = self.render_manager \ + formatted = self.render_manager \ .format_slide(slide[u'raw_slide'], line_break) - for page in formated: + for page in formatted: self._display_frames.append( {u'title': clean_tags(page), u'text': clean_tags(page.rstrip()), From 1899fc341566448c51a43ae4510c48a3fb9818fe Mon Sep 17 00:00:00 2001 From: Jonathan Corwin Date: Sat, 4 Sep 2010 14:06:06 +0100 Subject: [PATCH 2/4] css tidies, attempt to get paging correct, replacing qtextdoc with qwebview --- openlp/core/lib/__init__.py | 3 +- openlp/core/lib/htmlbuilder.py | 67 ++++++++++++++++++++-------------- openlp/core/lib/renderer.py | 44 ++++++++++++++++------ 3 files changed, 74 insertions(+), 40 deletions(-) diff --git a/openlp/core/lib/__init__.py b/openlp/core/lib/__init__.py index da1778d65..25f7630f9 100644 --- a/openlp/core/lib/__init__.py +++ b/openlp/core/lib/__init__.py @@ -324,7 +324,8 @@ from settingstab import SettingsTab from serviceitem import ServiceItem from serviceitem import ServiceItemType from serviceitem import ItemCapabilities -from htmlbuilder import build_html, build_lyrics_format_css +from htmlbuilder import build_html, build_lyrics_format_css, \ + build_lyrics_outline_css from toolbar import OpenLPToolbar from dockwidget import OpenLPDockWidget from theme import ThemeLevel, ThemeXML diff --git a/openlp/core/lib/htmlbuilder.py b/openlp/core/lib/htmlbuilder.py index f65bfb1e3..fff159415 100644 --- a/openlp/core/lib/htmlbuilder.py +++ b/openlp/core/lib/htmlbuilder.py @@ -329,9 +329,10 @@ def build_lyrics_css(item, webkitvers): if theme: lyricstable = u'left: %spx; top: %spx;' % \ (item.main.x(), item.main.y()) - lyrics = build_lyrics_format_css(theme) - lyrics += u'width: %spx; height: %spx; ' % \ - (item.main.width(), item.main.height()) + lyrics = build_lyrics_format_css(theme, item.main.width(), + item.main.height()) + #lyrics += u'width: %spx; height: %spx; ' % \ + # (item.main.width(), item.main.height()) # For performance reasons we want to show as few DIV's as possible, # especially when animating/transitions. # However some bugs in older versions of qtwebkit mean we need to @@ -347,29 +348,37 @@ def build_lyrics_css(item, webkitvers): # 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 theme.display_outline: - outline = u' -webkit-text-stroke: %sem %s; ' \ - '-webkit-text-fill-color: %s; ' % \ - (float(theme.display_outline_size) / 16, - theme.display_outline_color, theme.font_main_color) - if webkitvers >= 533.3: - lyricsmain += outline - if theme.display_shadow and webkitvers < 534.3: - shadow = u'-webkit-text-stroke: %sem %s; ' \ - u'-webkit-text-fill-color: %s; ' \ - u' padding-left: %spx; padding-top: %spx' % \ - (float(theme.display_outline_size) / 16, - theme.display_shadow_color, theme.display_shadow_color, - theme.display_shadow_size, theme.display_shadow_size) - if theme.display_shadow and \ - (not theme.display_outline or webkitvers >= 534.3): - lyricsmain += u' text-shadow: %s %spx %spx;' % \ - (theme.display_shadow_color, theme.display_shadow_size, - theme.display_shadow_size) + 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_format_css(theme): +def build_lyrics_outline_css(theme, is_shadow=False): + 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 @@ -393,18 +402,20 @@ def build_lyrics_format_css(theme): valign = u'middle' else: valign = u'top' - lyrics = u'word-wrap: break-word; ' \ + 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%%;' % \ + '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)) + 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 += ' font-style:italic; ' + lyrics += u' font-style:italic; ' if theme.font_main_weight == u'Bold': - lyrics += ' font-weight:bold; ' + lyrics += u' font-weight:bold; ' return lyrics def build_lyrics_html(item, webkitvers): diff --git a/openlp/core/lib/renderer.py b/openlp/core/lib/renderer.py index dae7c820a..260ce5e16 100644 --- a/openlp/core/lib/renderer.py +++ b/openlp/core/lib/renderer.py @@ -29,9 +29,10 @@ format it for the output display. """ import logging -from PyQt4 import QtGui, QtCore +from PyQt4 import QtGui, QtCore, QtWebKit -from openlp.core.lib import resize_image, expand_tags, build_lyrics_format_css +from openlp.core.lib import resize_image, expand_tags, \ + build_lyrics_format_css, build_lyrics_outline_css log = logging.getLogger(__name__) @@ -143,20 +144,41 @@ class Renderer(object): lines = verse.split(u'\n') for line in lines: text.append(line) - doc = QtGui.QTextDocument() - doc.setPageSize(QtCore.QSizeF(self._rect.width(), self._rect.height())) - layout = doc.documentLayout() + + web = QtWebKit.QWebView() + web.resize(self._rect.width(), self._rect.height()) + web.setVisible(False) + frame = web.page().mainFrame() + # Adjust width and height to account for shadow. outline done in css + width = self._rect.width() + int(self._theme.display_shadow_size) + height = self._rect.height() + int(self._theme.display_shadow_size) + css = u'' \ + u'
' % \ + (build_lyrics_format_css(self._theme, width, height), + build_lyrics_outline_css(self._theme)) +# doc = QtGui.QTextDocument() +# doc.setPageSize(QtCore.QSizeF(self._rect.width(), self._rect.height())) +# doc.setDocumentMargin(0) +# css = u'* {%s}' % build_lyrics_format_css(self._theme) +# doc.setDefaultStyleSheet(css) + #layout = doc.documentLayout() formatted = [] - shell = u'
' % build_lyrics_format_css(self._theme) html_text = u'' - styled_text = shell + styled_text = u'' + divheight = 'document.getElementById("main").scrollHeight' for line in text: - styled_text += expand_tags(line) + line_end - doc.setHtml(styled_text + u'
') + styled_line = expand_tags(line) + line_end + styled_text += styled_line + html = css + styled_text + u'
' + web.setHtml(html) +# doc.setHtml(styled_text) # Text too long so go to next page - if layout.pageCount() != 1: +# if doc.pageCount() != 1: + text_height = int(frame.evaluateJavaScript(divheight).toString()) + if text_height > height: formatted.append(html_text) - styled_text = shell + html_text = u'' + styled_text = styled_line html_text += line + line_end formatted.append(html_text) log.debug(u'format_slide - End') From a74af25265059a8ceeaa526a768e1d872972eeba Mon Sep 17 00:00:00 2001 From: Jonathan Corwin Date: Sat, 4 Sep 2010 14:09:47 +0100 Subject: [PATCH 3/4] tidy --- openlp/core/lib/htmlbuilder.py | 2 -- openlp/core/lib/renderer.py | 16 ++++------------ 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/openlp/core/lib/htmlbuilder.py b/openlp/core/lib/htmlbuilder.py index fff159415..e6a7857ff 100644 --- a/openlp/core/lib/htmlbuilder.py +++ b/openlp/core/lib/htmlbuilder.py @@ -331,8 +331,6 @@ def build_lyrics_css(item, webkitvers): (item.main.x(), item.main.y()) lyrics = build_lyrics_format_css(theme, item.main.width(), item.main.height()) - #lyrics += u'width: %spx; height: %spx; ' % \ - # (item.main.width(), item.main.height()) # For performance reasons we want to show as few DIV's as possible, # especially when animating/transitions. # However some bugs in older versions of qtwebkit mean we need to diff --git a/openlp/core/lib/renderer.py b/openlp/core/lib/renderer.py index 260ce5e16..46e0452ac 100644 --- a/openlp/core/lib/renderer.py +++ b/openlp/core/lib/renderer.py @@ -152,29 +152,21 @@ class Renderer(object): # Adjust width and height to account for shadow. outline done in css width = self._rect.width() + int(self._theme.display_shadow_size) height = self._rect.height() + int(self._theme.display_shadow_size) - css = u'' \ + shell = u'' \ u'
' % \ (build_lyrics_format_css(self._theme, width, height), build_lyrics_outline_css(self._theme)) -# doc = QtGui.QTextDocument() -# doc.setPageSize(QtCore.QSizeF(self._rect.width(), self._rect.height())) -# doc.setDocumentMargin(0) -# css = u'* {%s}' % build_lyrics_format_css(self._theme) -# doc.setDefaultStyleSheet(css) - #layout = doc.documentLayout() formatted = [] html_text = u'' styled_text = u'' - divheight = 'document.getElementById("main").scrollHeight' + js_height = 'document.getElementById("main").scrollHeight' for line in text: styled_line = expand_tags(line) + line_end styled_text += styled_line - html = css + styled_text + u'
' + html = shell + styled_text + u'' web.setHtml(html) -# doc.setHtml(styled_text) # Text too long so go to next page -# if doc.pageCount() != 1: - text_height = int(frame.evaluateJavaScript(divheight).toString()) + text_height = int(frame.evaluateJavaScript(js_height).toString()) if text_height > height: formatted.append(html_text) html_text = u'' From 5dd5e1506053caa663022427f16fb671cd10b9ec Mon Sep 17 00:00:00 2001 From: Jonathan Corwin Date: Sat, 4 Sep 2010 15:23:20 +0100 Subject: [PATCH 4/4] Subtract shadow from width, not add it --- openlp/core/lib/htmlbuilder.py | 25 +++++++++++++++++++++---- openlp/core/lib/renderer.py | 7 +++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/openlp/core/lib/htmlbuilder.py b/openlp/core/lib/htmlbuilder.py index 2d9323a8b..a39c3dac5 100644 --- a/openlp/core/lib/htmlbuilder.py +++ b/openlp/core/lib/htmlbuilder.py @@ -286,6 +286,10 @@ def build_html(item, screen, alert, islive): return html def webkit_version(): + """ + Return the Webkit version in use. + Note method added relatively recently, so return 0 if prior to this + """ try: webkitvers = float(QtWebKit.qWebKitVersion()) log.debug(u'Webkit version = %s' % webkitvers) @@ -402,6 +406,16 @@ def build_lyrics_css(item, webkitvers): return lyrics_css def build_lyrics_outline_css(theme, is_shadow=False): + """ + 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: @@ -420,11 +434,14 @@ def build_lyrics_format_css(theme, width, height): Build the css which controls the theme format Also used by renderer for splitting verses - `item` - Service Item containing theme and location information + `theme` + Object containing theme information - `webkitvers` - The version of qtwebkit we're using + `width` + Width of the lyrics block + + `height` + Height of the lyrics block """ if theme.display_horizontalAlign == 2: diff --git a/openlp/core/lib/renderer.py b/openlp/core/lib/renderer.py index 1b1605e00..330c434c5 100644 --- a/openlp/core/lib/renderer.py +++ b/openlp/core/lib/renderer.py @@ -143,15 +143,14 @@ class Renderer(object): for verse in verses_text: lines = verse.split(u'\n') for line in lines: - text.append(line) - + text.append(line) web = QtWebKit.QWebView() web.resize(self._rect.width(), self._rect.height()) web.setVisible(False) frame = web.page().mainFrame() # Adjust width and height to account for shadow. outline done in css - width = self._rect.width() + int(self._theme.display_shadow_size) - height = self._rect.height() + int(self._theme.display_shadow_size) + width = self._rect.width() - int(self._theme.display_shadow_size) + height = self._rect.height() - int(self._theme.display_shadow_size) shell = u'' \ u'
' % \ (build_lyrics_format_css(self._theme, width, height),