def layoutXmlLine( xml_data, styles, scale ): """ Performs a layout of a line using xml_data and styles, doing its best to display all of the text of xml_data at the largest size allowed by scale (a list of font sizes). If the text will not fit even at the smallest size of scale, then ellipsifies the text at that size. """ # OPTIMIZATION BEGIN: # Caching of largest working size for given scale and xml_data xml_hash = hash("|".join( ",".join(map(str,scale)) + re.sub("<.*?>", "", xml_data) )) usedSize = _size_scale_map.get(xml_hash, None) if usedSize: try: _updateStyles( styles, scale, usedSize ) document = xmltextlayout.xmlMarkupToDocument( xml_data, styles, XML_ALIASES, ) document.shrinkOffset = scale[-1] - usedSize except MaxLinesExceededError, e: raise else: return document
def layoutMessageXml(xmlMarkup, width, size, height, ellipsify="false", raiseLayoutExceptions=False): """ Lays out the xmlMarkup in a block that is width wide. if raiseLayoutExceptions is False, then this function will suppress any exceptions raised when parsing xmlMarkup and replace it with a message that tells the end-user that the message was broken, providing the end-user with as much of the original message as possible. If raiseLayoutExceptions is True, however, any exceptions raised will be passed through to the caller. """ maxLines = int(height / (size * LINE_SPACING)) _styles.update( "document", width="%fpt" % width, line_height="%spt" % int(size * LINE_SPACING), max_lines=maxLines, font_size="%spt" % size, ellipsify=ellipsify, ) try: document = xmltextlayout.xmlMarkupToDocument(xmlMarkup, _styles, _tagAliases) except MaxLinesExceededError: pass except Exception as e: if raiseLayoutExceptions: raise logging.warn("Could not layout message text %s; got error %s" % (xmlMarkup, e)) document = xmltextlayout.xmlMarkupToDocument( "<document><p>%s</p>%s</document>" % (escape_xml(xmlMarkup.strip()), "<caption>from a broken message</caption>"), _styles, _tagAliases) return document
def layoutMessageXml( xmlMarkup, width, size, height, ellipsify="false", raiseLayoutExceptions=False ): """ Lays out the xmlMarkup in a block that is width wide. if raiseLayoutExceptions is False, then this function will suppress any exceptions raised when parsing xmlMarkup and replace it with a message that tells the end-user that the message was broken, providing the end-user with as much of the original message as possible. If raiseLayoutExceptions is True, however, any exceptions raised will be passed through to the caller. """ maxLines = int( height / (size*LINE_SPACING) ) _styles.update( "document", width = "%fpt" % width, line_height = "%spt" % int(size*LINE_SPACING), max_lines = maxLines, font_size = "%spt" % size, ellipsify = ellipsify, ) try: document = xmltextlayout.xmlMarkupToDocument( xmlMarkup, _styles, _tagAliases ) except MaxLinesExceededError: pass except Exception, e: if raiseLayoutExceptions: raise logging.warn( "Could not layout message text %s; got error %s" % ( xmlMarkup, e ) ) document = xmltextlayout.xmlMarkupToDocument( "<document><p>%s</p>%s</document>" % ( escape_xml( xmlMarkup.strip() ), "<caption>from a broken message</caption>" ), _styles, _tagAliases )
def layoutXmlLine( xml_data, styles, scale ): """ Performs a layout of a line using xml_data and styles, doing its best to display all of the text of xml_data at the largest size allowed by scale (a list of font sizes). If the text will not fit even at the smallest size of scale, then ellipsifies the text at that size. """ document = None for size in reversed( scale ): try: _updateStyles( styles, scale, size ) document = xmltextlayout.xmlMarkupToDocument( xml_data, styles, XML_ALIASES, ) usedSize = size break except Exception: # NOTE: If the error is fundamental (not size-related), # then it will be raised again below # TODO: Figure out what exact types of exceptions are # "non-fundamental" and catch those instead of using # a blanket catch like this. pass if document == None: # no size above worked; use the smallest size _updateStyles( styles, scale, scale[0] ) document = xmltextlayout.xmlMarkupToDocument( xml_data, styles, XML_ALIASES, ) usedSize = scale[0] document.shrinkOffset = scale[-1] - usedSize return document
document.shrinkOffset = scale[-1] - usedSize except MaxLinesExceededError, e: raise else: return document hasFailed = False # OPTIMIZATION END document = None for size in reversed( scale ): try: _updateStyles( styles, scale, size ) document = xmltextlayout.xmlMarkupToDocument( xml_data, styles, XML_ALIASES, ) usedSize = size break except MaxLinesExceededError, e: hasFailed = True # NOTE: If the error is fundamental (not size-related), # then it will be raised again below # TODO: Figure out what exact types of exceptions are # "non-fundamental" and catch those instead of using # a blanket catch like this. logging.error(e) if document == None:
def layoutXmlLine(xml_data, styles, scale): """ Performs a layout of a line using xml_data and styles, doing its best to display all of the text of xml_data at the largest size allowed by scale (a list of font sizes). If the text will not fit even at the smallest size of scale, then ellipsifies the text at that size. """ # OPTIMIZATION BEGIN: # Caching of largest working size for given scale and xml_data xml_hash = hash("|".join( ",".join(map(str, scale)) + strip_html_tags(xml_data) )) usedSize = _size_scale_map.get(xml_hash, None) if usedSize: try: _updateStyles(styles, scale, usedSize) document = xmltextlayout.xmlMarkupToDocument( xml_data, styles, XML_ALIASES, ) document.shrinkOffset = scale[-1] - usedSize except MaxLinesExceededError: raise else: return document hasFailed = False # OPTIMIZATION END document = None for size in reversed(scale): try: _updateStyles(styles, scale, size) document = xmltextlayout.xmlMarkupToDocument( xml_data, styles, XML_ALIASES, ) usedSize = size break except MaxLinesExceededError: hasFailed = True # NOTE: If the error is fundamental (not size-related), # then it will be raised again below # TODO: Figure out what exact types of exceptions are # "non-fundamental" and catch those instead of using # a blanket catch like this. # print xml_data # logging.error(e) if document is None: # no size above worked; use the smallest size _updateStyles(styles, scale, scale[0]) document = xmltextlayout.xmlMarkupToDocument( xml_data, styles, XML_ALIASES, ) usedSize = scale[0] document.shrinkOffset = scale[-1] - usedSize # OPTIMIZATION BEGIN: # Caching of largest working size for given scale and xml_data if hasFailed: _size_scale_map[xml_hash] = usedSize # OPTIMIZATION END return document