def normalize_font(name, cssvalue): # See https://developer.mozilla.org/en-US/docs/Web/CSS/font composition = font_composition val = cssvalue.cssText if val == 'inherit': return {k:'inherit' for k in composition} if val in {'caption', 'icon', 'menu', 'message-box', 'small-caption', 'status-bar'}: return {k:DEFAULTS[k] for k in composition} try: primitives = list(v.cssText for v in cssvalue) except TypeError: primitives = [cssvalue.cssText] if len(primitives) < 2: return {} # Mandatory to define both font size and font family style = {k:DEFAULTS[k] for k in composition} style['font-family'] = primitives.pop() if len(primitives) > 1 and cssprofiles.validate('line-height', primitives[-1]) and cssprofiles.validate('font-size', primitives[-2]): style['line-height'], style['font-size'] = primitives.pop(), primitives.pop() else: val = primitives.pop() if not cssprofiles.validate('font-size', val): return {} style['font-size'] = val composition = composition[:3] while primitives: value = primitives.pop() for key in composition: if cssprofiles.validate(key, value): style[key] = value break return style
def normalize_font(cssvalue, font_family_as_list=False): # See https://developer.mozilla.org/en-US/docs/Web/CSS/font composition = font_composition val = cssvalue.cssText if val == 'inherit': return {k: 'inherit' for k in composition} if val in { 'caption', 'icon', 'menu', 'message-box', 'small-caption', 'status-bar' }: return {k: DEFAULTS[k] for k in composition} if getattr(cssvalue, 'length', 1) < 2: return {} # Mandatory to define both font size and font family style = {k: DEFAULTS[k] for k in composition} families = [] vals = [x.cssText for x in cssvalue] found_font_size = False while vals: text = vals.pop() if not families and text == 'inherit': families.append(text) continue if cssprofiles.validate('line-height', text): if not vals or not cssprofiles.validate('font-size', vals[-1]): if cssprofiles.validate('font-size', text): style['font-size'] = text found_font_size = True break return {} # must have font-size here style['line-height'] = text style['font-size'] = vals.pop() found_font_size = True break if cssprofiles.validate('font-size', text): style['font-size'] = text found_font_size = True break if families == ['inherit']: return { } # Cannot have multiple font-families if the last one if inherit families.insert(0, text) if not families or not found_font_size: return {} # font-family required style['font-family'] = families if font_family_as_list else ', '.join( families) props = ['font-style', 'font-variant', 'font-weight'] while vals: for i, prop in enumerate(tuple(props)): if cssprofiles.validate(prop, vals[0]): props.pop(i) style[prop] = vals.pop(0) break else: return {} # unrecognized value return style
def normalize_font(cssvalue, font_family_as_list=False): # See https://developer.mozilla.org/en-US/docs/Web/CSS/font composition = font_composition val = cssvalue.cssText if val == "inherit": return {k: "inherit" for k in composition} if val in {"caption", "icon", "menu", "message-box", "small-caption", "status-bar"}: return {k: DEFAULTS[k] for k in composition} if getattr(cssvalue, "length", 1) < 2: return {} # Mandatory to define both font size and font family style = {k: DEFAULTS[k] for k in composition} families = [] vals = [x.cssText for x in cssvalue] found_font_size = False while vals: text = vals.pop() if not families and text == "inherit": families.append(text) continue if cssprofiles.validate("line-height", text): if not vals or not cssprofiles.validate("font-size", vals[-1]): if cssprofiles.validate("font-size", text): style["font-size"] = text found_font_size = True break return {} # must have font-size here style["line-height"] = text style["font-size"] = vals.pop() found_font_size = True break if cssprofiles.validate("font-size", text): style["font-size"] = text found_font_size = True break if families == ["inherit"]: return {} # Cannot have multiple font-families if the last one if inherit families.insert(0, text) if not families or not found_font_size: return {} # font-family required style["font-family"] = families if font_family_as_list else ", ".join(families) props = ["font-style", "font-variant", "font-weight"] while vals: for i, prop in enumerate(tuple(props)): if cssprofiles.validate(prop, vals[0]): props.pop(i) style[prop] = vals.pop(0) break else: return {} # unrecognized value return style
def _normalize_font(self, cssvalue): composition = ('font-style', 'font-variant', 'font-weight', 'font-size', 'line-height', 'font-family') style = {} if cssvalue.cssText == 'inherit': for key in composition: style[key] = 'inherit' else: try: primitives = [v.cssText for v in cssvalue] except TypeError: primitives = [cssvalue.cssText] primitives.reverse() value = primitives.pop() for key in composition: if cssprofiles.validate(key, value): style[key] = value if not primitives: break value = primitives.pop() for key in composition: if key not in style: val = ('inherit' if key in {'font-family', 'font-size'} else 'normal') style[key] = val return style
def _normalize_list_style(self, cssvalue): composition = ('list-style-type', 'list-style-position', 'list-style-image') style = {} if cssvalue.cssText == 'inherit': for key in composition: style[key] = 'inherit' else: try: primitives = [v.cssText for v in cssvalue] except TypeError: primitives = [cssvalue.cssText] primitives.reverse() value = primitives.pop() for key in composition: if cssprofiles.validate(key, value): style[key] = value if not primitives: break value = primitives.pop() for key in composition: if key not in style: style[key] = DEFAULTS[key] return style
def normalize_font(name, cssvalue): # See https://developer.mozilla.org/en-US/docs/Web/CSS/font composition = font_composition val = cssvalue.cssText if val == 'inherit': return {k: 'inherit' for k in composition} if val in { 'caption', 'icon', 'menu', 'message-box', 'small-caption', 'status-bar' }: return {k: DEFAULTS[k] for k in composition} try: primitives = list(v.cssText for v in cssvalue) except TypeError: primitives = [cssvalue.cssText] if len(primitives) < 2: return {} # Mandatory to define both font size and font family style = {k: DEFAULTS[k] for k in composition} style['font-family'] = primitives.pop() if len(primitives) > 1 and cssprofiles.validate( 'line-height', primitives[-1]) and cssprofiles.validate( 'font-size', primitives[-2]): style['line-height'], style['font-size'] = primitives.pop( ), primitives.pop() else: val = primitives.pop() if not cssprofiles.validate('font-size', val): return {} style['font-size'] = val composition = composition[:3] while primitives: value = primitives.pop() for key in composition: if cssprofiles.validate(key, value): style[key] = value break return style
def normalize_simple_composition(name, cssvalue, composition, check_inherit=True): if check_inherit and cssvalue.cssText == 'inherit': style = {k:'inherit' for k in composition} else: style = {k:DEFAULTS[k] for k in composition} try: primitives = [v.cssText for v in cssvalue] except TypeError: primitives = [cssvalue.cssText] while primitives: value = primitives.pop() for key in composition: if cssprofiles.validate(key, value): style[key] = value break return style