Refactor css font size parsing
- Get rid of deprecated Gdk.Screen methods - remove support for 'ex' unit - not easily correct implemented and not commonly used Fixes #8491
This commit is contained in:
parent
bd3d195b29
commit
d3890585f6
|
@ -67,10 +67,6 @@ __all__ = ['HtmlTextView']
|
||||||
whitespace_rx = re.compile('\\s+')
|
whitespace_rx = re.compile('\\s+')
|
||||||
allwhitespace_rx = re.compile('^\\s*$')
|
allwhitespace_rx = re.compile('^\\s*$')
|
||||||
|
|
||||||
# pixels = points * display_resolution
|
|
||||||
display_resolution = 0.3514598*(Gdk.Screen.height() /
|
|
||||||
float(Gdk.Screen.height_mm()))
|
|
||||||
|
|
||||||
# embryo of CSS classes
|
# embryo of CSS classes
|
||||||
classes = {
|
classes = {
|
||||||
#'system-message':';display: none',
|
#'system-message':';display: none',
|
||||||
|
@ -227,6 +223,11 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
|
||||||
self.list_counters = [] # stack (top at head) of list
|
self.list_counters = [] # stack (top at head) of list
|
||||||
# counters, or None for unordered list
|
# counters, or None for unordered list
|
||||||
|
|
||||||
|
def _get_points_from_pixels(self, pixels):
|
||||||
|
resolution = self.textview.get_screen().get_resolution()
|
||||||
|
# points = pixels * 72 / resolution
|
||||||
|
return pixels * 72 / resolution
|
||||||
|
|
||||||
def _parse_style_color(self, tag, value):
|
def _parse_style_color(self, tag, value):
|
||||||
color = _parse_css_color(value)
|
color = _parse_css_color(value)
|
||||||
tag.set_property('foreground-gdk', color)
|
tag.set_property('foreground-gdk', color)
|
||||||
|
@ -236,7 +237,6 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
|
||||||
tag.set_property('background-gdk', color)
|
tag.set_property('background-gdk', color)
|
||||||
tag.set_property('paragraph-background-gdk', color)
|
tag.set_property('paragraph-background-gdk', color)
|
||||||
|
|
||||||
|
|
||||||
def _get_current_attributes(self):
|
def _get_current_attributes(self):
|
||||||
attrs = self.textview.get_default_attributes()
|
attrs = self.textview.get_default_attributes()
|
||||||
self.iter.backward_char()
|
self.iter.backward_char()
|
||||||
|
@ -266,10 +266,7 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
|
||||||
val = sign*max(1, min(abs(val), 500))
|
val = sign*max(1, min(abs(val), 500))
|
||||||
frac = val/100
|
frac = val/100
|
||||||
if font_relative:
|
if font_relative:
|
||||||
attrs = self._get_current_attributes()
|
callback(frac, '%', *args)
|
||||||
if not attrs.font:
|
|
||||||
font_size = self.get_font_size()
|
|
||||||
callback(frac*display_resolution*font_size, *args)
|
|
||||||
elif block_relative:
|
elif block_relative:
|
||||||
# CSS says 'Percentage values: refer to width of the closest
|
# CSS says 'Percentage values: refer to width of the closest
|
||||||
# block-level ancestor'
|
# block-level ancestor'
|
||||||
|
@ -285,8 +282,12 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
|
||||||
callback(frac, *args)
|
callback(frac, *args)
|
||||||
return
|
return
|
||||||
|
|
||||||
def get_val():
|
def get_val(min_val=minl, max_val=maxl):
|
||||||
val = float(value[:-2])
|
try:
|
||||||
|
val = float(value[:-2])
|
||||||
|
except:
|
||||||
|
log.warning('Unable to parse length value "%s"' % value)
|
||||||
|
return None
|
||||||
if val > 0:
|
if val > 0:
|
||||||
sign = 1
|
sign = 1
|
||||||
elif val < 0:
|
elif val < 0:
|
||||||
|
@ -294,27 +295,24 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
|
||||||
else:
|
else:
|
||||||
sign = 0
|
sign = 0
|
||||||
# validate length
|
# validate length
|
||||||
return sign*max(minl, min(abs(val*display_resolution), maxl))
|
return sign*max(min_val, min(abs(val), max_val))
|
||||||
if value.endswith('pt'): # points
|
if value.endswith('pt'): # points
|
||||||
callback(get_val()*display_resolution, *args)
|
size = get_val(5, 50)
|
||||||
|
if size is None:
|
||||||
|
return
|
||||||
|
callback(size, 'pt', *args)
|
||||||
|
|
||||||
elif value.endswith('em'): # ems, the width of the element's font
|
elif value.endswith('em'):
|
||||||
attrs = self._get_current_attributes()
|
size = get_val(0.3, 4)
|
||||||
if not attrs.font:
|
if size is None:
|
||||||
font_size = self.get_font_size()
|
return
|
||||||
callback(get_val()*display_resolution*font_size, *args)
|
callback(size, 'em', *args)
|
||||||
|
|
||||||
elif value.endswith('ex'): # x-height, ~ the height of the letter 'x'
|
elif value.endswith('px'): # pixels
|
||||||
# FIXME: figure out how to calculate this correctly
|
size = get_val(5, 50)
|
||||||
# for now 'em' size is used as approximation
|
if size is None:
|
||||||
|
return
|
||||||
attrs = self._get_current_attributes()
|
callback(size, 'px', *args)
|
||||||
if not attrs.font:
|
|
||||||
font_size = self.get_font_size()
|
|
||||||
callback(get_val()*display_resolution*font_size, *args)
|
|
||||||
|
|
||||||
elif value.endswith('px'): # pixels
|
|
||||||
callback(get_val(), *args)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
|
@ -327,13 +325,18 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
|
||||||
else:
|
else:
|
||||||
sign = 0
|
sign = 0
|
||||||
# validate length
|
# validate length
|
||||||
val = sign*max(minl, min(abs(val), maxl))
|
val = sign*max(5, min(abs(val), 70))
|
||||||
callback(val, *args)
|
callback(val, 'px', *args)
|
||||||
except Exception:
|
except Exception:
|
||||||
log.warning('Unable to parse length value "%s"' % value)
|
log.warning('Unable to parse length value "%s"' % value)
|
||||||
|
|
||||||
def __parse_font_size_cb(length, tag):
|
def __parse_font_size_cb(size, type_, tag):
|
||||||
tag.set_property('size-points', length/display_resolution)
|
if type_ in ('em', '%'):
|
||||||
|
tag.set_property('scale', size)
|
||||||
|
elif type_ == 'pt':
|
||||||
|
tag.set_property('size-points', size)
|
||||||
|
elif type_ == 'px':
|
||||||
|
tag.set_property('size-points', self._get_points_from_pixels(size))
|
||||||
__parse_font_size_cb = staticmethod(__parse_font_size_cb)
|
__parse_font_size_cb = staticmethod(__parse_font_size_cb)
|
||||||
|
|
||||||
def _parse_style_display(self, tag, value):
|
def _parse_style_display(self, tag, value):
|
||||||
|
@ -830,10 +833,6 @@ class HtmlHandler(xml.sax.handler.ContentHandler):
|
||||||
self._jump_line()
|
self._jump_line()
|
||||||
self._end_span()
|
self._end_span()
|
||||||
|
|
||||||
def get_font_size(self):
|
|
||||||
context = self.conv_textview.tv.get_style_context()
|
|
||||||
font = context.get_font(Gtk.StateType.NORMAL)
|
|
||||||
return font.get_size() / Pango.SCALE
|
|
||||||
|
|
||||||
class HtmlTextView(Gtk.TextView):
|
class HtmlTextView(Gtk.TextView):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue