def start(self, c): # save the type of tag; it's used in PmlBaseDoc.afterFlowable() # to check if we need to add an outline-entry # c.frag.tag = self.tag if self.attr.align is not None: #print self.attr.align, getAlign(self.attr.align) c.frag.alignment = getAlign(self.attr.align)
def CSS2Frag(c, kw, isBlock): # COLORS if "color" in c.cssAttr: c.frag.textColor = getColor(c.cssAttr["color"]) if "background-color" in c.cssAttr: c.frag.backColor = getColor(c.cssAttr["background-color"]) # FONT SIZE, STYLE, WEIGHT if "font-family" in c.cssAttr: c.frag.fontName = c.getFontName(c.cssAttr["font-family"]) if "font-size" in c.cssAttr: # XXX inherit c.frag.fontSize = max( getSize("".join(c.cssAttr["font-size"]), c.frag.fontSize, c.baseFontSize), 1.0) if "line-height" in c.cssAttr: leading = "".join(c.cssAttr["line-height"]) c.frag.leading = getSize(leading, c.frag.fontSize) c.frag.leadingSource = leading else: c.frag.leading = getSize(c.frag.leadingSource, c.frag.fontSize) if "letter-spacing" in c.cssAttr: c.frag.letterSpacing = c.cssAttr["letter-spacing"] if "-pdf-line-spacing" in c.cssAttr: c.frag.leadingSpace = getSize("".join(c.cssAttr["-pdf-line-spacing"])) # print "line-spacing", c.cssAttr["-pdf-line-spacing"], c.frag.leading if "font-weight" in c.cssAttr: value = lower(c.cssAttr["font-weight"]) if value in ("bold", "bolder", "500", "600", "700", "800", "900"): c.frag.bold = 1 else: c.frag.bold = 0 for value in toList(c.cssAttr.get("text-decoration", "")): if "underline" in value: c.frag.underline = 1 if "line-through" in value: c.frag.strike = 1 if "none" in value: c.frag.underline = 0 c.frag.strike = 0 if "font-style" in c.cssAttr: value = lower(c.cssAttr["font-style"]) if value in ("italic", "oblique"): c.frag.italic = 1 else: c.frag.italic = 0 if "white-space" in c.cssAttr: # normal | pre | nowrap c.frag.whiteSpace = str(c.cssAttr["white-space"]).lower() # ALIGN & VALIGN if "text-align" in c.cssAttr: c.frag.alignment = getAlign(c.cssAttr["text-align"]) if "vertical-align" in c.cssAttr: c.frag.vAlign = c.cssAttr["vertical-align"] # HEIGHT & WIDTH if "height" in c.cssAttr: try: # XXX Relative is not correct! c.frag.height = "".join(toList(c.cssAttr["height"])) except TypeError: # sequence item 0: expected string, tuple found c.frag.height = "".join(toList(c.cssAttr["height"][0])) if c.frag.height in ("auto",): c.frag.height = None if "width" in c.cssAttr: try: # XXX Relative is not correct! c.frag.width = "".join(toList(c.cssAttr["width"])) except TypeError: c.frag.width = "".join(toList(c.cssAttr["width"][0])) if c.frag.width in ("auto",): c.frag.width = None # ZOOM if "zoom" in c.cssAttr: # XXX Relative is not correct! zoom = "".join(toList(c.cssAttr["zoom"])) if zoom.endswith("%"): zoom = float(zoom[: - 1]) / 100.0 c.frag.zoom = float(zoom) # MARGINS & LIST INDENT, STYLE if isBlock: transform_attrs(c.frag, (("spaceBefore", "margin-top"), ("spaceAfter", "margin-bottom"), ("firstLineIndent", "text-indent"), ), c.cssAttr, getSize, extras=c.frag.fontSize ) if "margin-left" in c.cssAttr: c.frag.bulletIndent = kw["margin-left"] # For lists kw["margin-left"] += getSize(c.cssAttr["margin-left"], c.frag.fontSize) c.frag.leftIndent = kw["margin-left"] if "margin-right" in c.cssAttr: kw["margin-right"] += getSize( c.cssAttr["margin-right"], c.frag.fontSize) c.frag.rightIndent = kw["margin-right"] if "list-style-type" in c.cssAttr: c.frag.listStyleType = str(c.cssAttr["list-style-type"]).lower() if "list-style-image" in c.cssAttr: c.frag.listStyleImage = c.getFile(c.cssAttr["list-style-image"]) # PADDINGS if isBlock: transform_attrs(c.frag, (("paddingTop", "padding-top"), ("paddingBottom", "padding-bottom"), ("paddingLeft", "padding-left"), ("paddingRight", "padding-right"), ), c.cssAttr, getSize, extras=c.frag.fontSize ) # BORDERS if isBlock: transform_attrs(c.frag, (("borderTopWidth", "border-top-width"), ("borderBottomWidth", "border-bottom-width"), ("borderLeftWidth", "border-left-width"), ("borderRightWidth", "border-right-width"), ), c.cssAttr, getSize, extras=c.frag.fontSize ) transform_attrs(c.frag, ( ("borderTopStyle", "border-top-style"), ("borderBottomStyle", "border-bottom-style"), ("borderLeftStyle", "border-left-style"), ("borderRightStyle", "border-right-style") ), c.cssAttr, lambda x: x ) transform_attrs(c.frag, ( ("borderTopColor", "border-top-color"), ("borderBottomColor", "border-bottom-color"), ("borderLeftColor", "border-left-color"), ("borderRightColor", "border-right-color") ), c.cssAttr, getColor )
def start(self, c): if self.attr.align is not None: c.frag.alignment = getAlign(self.attr.align) c.clearFrag() self.story = c.swapStory() attrs = self.attr tdata = c.tableData cspan = attrs.colspan rspan = attrs.rowspan row = tdata.row col = tdata.col while 1: for x, y in tdata.span: if x == col and y == row: col += 1 tdata.col += 1 break begin = (col, row) end = (col, row) if cspan: end = (end[0] + cspan - 1, end[1]) if rspan: end = (end[0], end[1] + rspan - 1) if begin != end: tdata.add_style(('SPAN', begin, end)) for x in six.moves.range(begin[0], end[0] + 1): for y in six.moves.range(begin[1], end[1] + 1): if x != begin[0] or y != begin[1]: tdata.add_empty(x, y) # Set Border and padding styles tdata.add_cell_styles(c, begin, end, "td") # Calculate widths # Add empty placeholders for new columns if (col + 1) > len(tdata.colw): tdata.colw = tdata.colw + \ ((col + 1 - len(tdata.colw)) * [_width()]) # Get value of with, if no spanning if not cspan: width = c.frag.width or self.attr.width # If is value, the set it in the right place in the arry if width is not None: tdata.colw[col] = _width(width) log.debug("Col {} has width {}".format(col, width)) else: # If there are no child nodes, nothing within the column can change the # width. Set the column width to the sum of the right and left padding # rather than letting it default. log.debug(width) if len(self.node.childNodes) == 0: width = c.frag.paddingLeft + c.frag.paddingRight log.debug("Col {} has width {}".format(col, width)) if width: tdata.colw[col] = _width(width) else: # Child nodes are present, we cannot do anything about the # width except set it externally. pass # Calculate heights if row + 1 > len(tdata.rowh): tdata.rowh = tdata.rowh + \ ((row + 1 - len(tdata.rowh)) * [_width()]) if not rspan: height = c.frag.height or self.attr.get('height', None) if height is not None: tdata.rowh[row] = _height(height) tdata.add_style(('FONTSIZE', begin, end, 1.0)) tdata.add_style(('LEADING', begin, end, 1.0)) # Vertical align valign = self.attr.valign or c.frag.vAlign if valign is not None: tdata.add_style(('VALIGN', begin, end, valign.upper())) # Reset border, otherwise the paragraph block will have borders too frag = c.frag set_value(frag, ('borderLeftWidth', 'borderRightWidth', 'borderTopWidth', 'borderBottomWidth'), 0) set_value(frag, ('borderLeftColor', 'borderLeftStyle', 'borderRightColor', 'borderRightStyle', 'borderTopColor', 'borderTopStyle', 'borderBottomColor', 'borderBottomStyle'), None)
def start(self, c): if self.attr.align is not None: c.frag.alignment = getAlign(self.attr.align) c.clearFrag() self.story = c.swapStory() attrs = self.attr tdata = c.tableData cspan = attrs.colspan rspan = attrs.rowspan row = tdata.row col = tdata.col while 1: for x, y in tdata.span: if x == col and y == row: col += 1 tdata.col += 1 break begin = (col, row) end = (col, row) if cspan: end = (end[0] + cspan - 1, end[1]) if rspan: end = (end[0], end[1] + rspan - 1) if begin != end: tdata.add_style(('SPAN', begin, end)) for x in six.moves.range(begin[0], end[0] + 1): for y in six.moves.range(begin[1], end[1] + 1): if x != begin[0] or y != begin[1]: tdata.add_empty(x, y) # Set Border and padding styles tdata.add_cell_styles(c, begin, end, "td") # Calculate widths # Add empty placeholders for new columns if (col + 1) > len(tdata.colw): tdata.colw = tdata.colw + \ ((col + 1 - len(tdata.colw)) * [_width()]) # Get value of with, if no spanning if not cspan: width = c.frag.width or self.attr.width # If is value, the set it in the right place in the arry if width is not None: tdata.colw[col] = _width(width) log.debug("Col {} has width {}".format(col, width)) else: # If there are no child nodes, nothing within the column can change the # width. Set the column width to the sum of the right and left padding # rather than letting it default. log.debug(width) if len(self.node.childNodes) == 0: width = c.frag.paddingLeft + c.frag.paddingRight log.debug("Col {} has width {}".format(col, width)) if width: tdata.colw[col] = _width(width) else: # Child nodes are present, we cannot do anything about the # width except set it externally. pass # Calculate heights if row + 1 > len(tdata.rowh): tdata.rowh = tdata.rowh + \ ((row + 1 - len(tdata.rowh)) * [_width()]) if not rspan: height = c.frag.height or self.attr.get('height', None) if height is not None: tdata.rowh[row] = _height(height) tdata.add_style(('FONTSIZE', begin, end, 1.0)) tdata.add_style(('LEADING', begin, end, 1.0)) # Vertical align valign = self.attr.valign or c.frag.vAlign if valign is not None: tdata.add_style(('VALIGN', begin, end, valign.upper())) # Reset border, otherwise the paragraph block will have borders too frag = c.frag set_value(frag, ('borderLeftWidth', 'borderRightWidth', 'borderTopWidth', 'borderBottomWidth'), 0) set_value(frag, ('borderLeftColor', 'borderLeftStyle', 'borderRightColor', 'borderRightStyle', 'borderTopColor', 'borderTopStyle', 'borderBottomColor', 'borderBottomStyle'), None )
def start(self, c): if self.attr.align is not None: c.frag.alignment = getAlign(self.attr.align) c.clearFrag() self.story = c.swapStory() attrs = self.attr tdata = c.tableData cspan = attrs.colspan rspan = attrs.rowspan row = tdata.row col = tdata.col while 1: for x, y in tdata.span: if x == col and y == row: col += 1 tdata.col += 1 break begin = (col, row) end = (col, row) if cspan: end = (end[0] + cspan - 1, end[1]) if rspan: end = (end[0], end[1] + rspan - 1) if begin != end: # ~ print begin, end tdata.add_style(("SPAN", begin, end)) for x in xrange(begin[0], end[0] + 1): for y in xrange(begin[1], end[1] + 1): if x != begin[0] or y != begin[1]: tdata.add_empty(x, y) # Set Border and padding styles tdata.add_cell_styles(c, begin, end, "td") # Calculate widths # Add empty placeholders for new columns if (col + 1) > len(tdata.colw): tdata.colw = tdata.colw + ((col + 1 - len(tdata.colw)) * [_width()]) # Get value of with, if no spanning if not cspan: width = c.frag.width or self.attr.width # If is value, the set it in the right place in the arry if width is not None: tdata.colw[col] = _width(width) # Calculate heights if row + 1 > len(tdata.rowh): tdata.rowh = tdata.rowh + ((row + 1 - len(tdata.rowh)) * [_width()]) if not rspan: height = c.frag.height or self.attr.get("height", None) if height is not None: tdata.rowh[row] = _height(height) tdata.add_style(("FONTSIZE", begin, end, 1.0)) tdata.add_style(("LEADING", begin, end, 1.0)) # Vertical align valign = self.attr.valign or c.frag.vAlign if valign is not None: tdata.add_style(("VALIGN", begin, end, valign.upper())) # Reset border, otherwise the paragraph block will have borders too frag = c.frag frag.borderLeftWidth = 0 frag.borderLeftColor = None frag.borderLeftStyle = None frag.borderRightWidth = 0 frag.borderRightColor = None frag.borderRightStyle = None frag.borderTopWidth = 0 frag.borderTopColor = None frag.borderTopStyle = None frag.borderBottomWidth = 0 frag.borderBottomColor = None frag.borderBottomStyle = None
def CSS2Frag(c, kw, isBlock): # COLORS if "color" in c.cssAttr: c.frag.textColor = getColor(c.cssAttr["color"]) if "background-color" in c.cssAttr: c.frag.backColor = getColor(c.cssAttr["background-color"]) # FONT SIZE, STYLE, WEIGHT if "font-family" in c.cssAttr: c.frag.fontName = c.getFontName(c.cssAttr["font-family"]) if "font-size" in c.cssAttr: # XXX inherit try: c.frag.fontSize = max(getSize("".join(c.cssAttr["font-size"]), c.frag.fontSize, c.baseFontSize), 1.0) except TypeError: # sequence item 0: expected string, tuple found c.frag.fontSize = max(getSize("".join(c.cssAttr["font-size"][0]), c.frag.fontSize, c.baseFontSize), 1.0) if "line-height" in c.cssAttr: leading = "".join(c.cssAttr["line-height"]) c.frag.leading = getSize(leading, c.frag.fontSize) c.frag.leadingSource = leading else: c.frag.leading = getSize(c.frag.leadingSource, c.frag.fontSize) if "letter-spacing" in c.cssAttr: c.frag.letterSpacing = c.cssAttr["letter-spacing"] if "-pdf-line-spacing" in c.cssAttr: c.frag.leadingSpace = getSize("".join(c.cssAttr["-pdf-line-spacing"])) # print "line-spacing", c.cssAttr["-pdf-line-spacing"], c.frag.leading if "font-weight" in c.cssAttr: value = lower(c.cssAttr["font-weight"]) if value in ("bold", "bolder", "500", "600", "700", "800", "900"): c.frag.bold = 1 else: c.frag.bold = 0 for value in toList(c.cssAttr.get("text-decoration", "")): if "underline" in value: c.frag.underline = 1 if "line-through" in value: c.frag.strike = 1 if "none" in value: c.frag.underline = 0 c.frag.strike = 0 if "font-style" in c.cssAttr: value = lower(c.cssAttr["font-style"]) if value in ("italic", "oblique"): c.frag.italic = 1 else: c.frag.italic = 0 if "white-space" in c.cssAttr: # normal | pre | nowrap c.frag.whiteSpace = str(c.cssAttr["white-space"]).lower() # ALIGN & VALIGN if "text-align" in c.cssAttr: c.frag.alignment = getAlign(c.cssAttr["text-align"]) if "vertical-align" in c.cssAttr: c.frag.vAlign = c.cssAttr["vertical-align"] # HEIGHT & WIDTH if "height" in c.cssAttr: try: c.frag.height = "".join(toList(c.cssAttr["height"])) # XXX Relative is not correct! except TypeError: # sequence item 0: expected string, tuple found c.frag.height = "".join(toList(c.cssAttr["height"][0])) if c.frag.height in ("auto",): c.frag.height = None if "width" in c.cssAttr: try: c.frag.width = "".join(toList(c.cssAttr["width"])) # XXX Relative is not correct! except TypeError: c.frag.width = "".join(toList(c.cssAttr["width"][0])) if c.frag.width in ("auto",): c.frag.width = None # ZOOM if "zoom" in c.cssAttr: zoom = "".join(toList(c.cssAttr["zoom"])) # XXX Relative is not correct! if zoom.endswith("%"): zoom = float(zoom[: - 1]) / 100.0 c.frag.zoom = float(zoom) # MARGINS & LIST INDENT, STYLE if isBlock: if "margin-top" in c.cssAttr: c.frag.spaceBefore = getSize(c.cssAttr["margin-top"], c.frag.fontSize) if "margin-bottom" in c.cssAttr: c.frag.spaceAfter = getSize(c.cssAttr["margin-bottom"], c.frag.fontSize) if "margin-left" in c.cssAttr: c.frag.bulletIndent = kw["margin-left"] # For lists kw["margin-left"] += getSize(c.cssAttr["margin-left"], c.frag.fontSize) c.frag.leftIndent = kw["margin-left"] if "margin-right" in c.cssAttr: kw["margin-right"] += getSize(c.cssAttr["margin-right"], c.frag.fontSize) c.frag.rightIndent = kw["margin-right"] if "text-indent" in c.cssAttr: c.frag.firstLineIndent = getSize(c.cssAttr["text-indent"], c.frag.fontSize) if "list-style-type" in c.cssAttr: c.frag.listStyleType = str(c.cssAttr["list-style-type"]).lower() if "list-style-image" in c.cssAttr: c.frag.listStyleImage = c.getFile(c.cssAttr["list-style-image"]) # PADDINGS if isBlock: if "padding-top" in c.cssAttr: c.frag.paddingTop = getSize(c.cssAttr["padding-top"], c.frag.fontSize) if "padding-bottom" in c.cssAttr: c.frag.paddingBottom = getSize(c.cssAttr["padding-bottom"], c.frag.fontSize) if "padding-left" in c.cssAttr: c.frag.paddingLeft = getSize(c.cssAttr["padding-left"], c.frag.fontSize) if "padding-right" in c.cssAttr: c.frag.paddingRight = getSize(c.cssAttr["padding-right"], c.frag.fontSize) # BORDERS if isBlock: if "border-top-width" in c.cssAttr: c.frag.borderTopWidth = getSize(c.cssAttr["border-top-width"], c.frag.fontSize) if "border-bottom-width" in c.cssAttr: c.frag.borderBottomWidth = getSize(c.cssAttr["border-bottom-width"], c.frag.fontSize) if "border-left-width" in c.cssAttr: c.frag.borderLeftWidth = getSize(c.cssAttr["border-left-width"], c.frag.fontSize) if "border-right-width" in c.cssAttr: c.frag.borderRightWidth = getSize(c.cssAttr["border-right-width"], c.frag.fontSize) if "border-top-style" in c.cssAttr: c.frag.borderTopStyle = c.cssAttr["border-top-style"] if "border-bottom-style" in c.cssAttr: c.frag.borderBottomStyle = c.cssAttr["border-bottom-style"] if "border-left-style" in c.cssAttr: c.frag.borderLeftStyle = c.cssAttr["border-left-style"] if "border-right-style" in c.cssAttr: c.frag.borderRightStyle = c.cssAttr["border-right-style"] if "border-top-color" in c.cssAttr: c.frag.borderTopColor = getColor(c.cssAttr["border-top-color"]) if "border-bottom-color" in c.cssAttr: c.frag.borderBottomColor = getColor(c.cssAttr["border-bottom-color"]) if "border-left-color" in c.cssAttr: c.frag.borderLeftColor = getColor(c.cssAttr["border-left-color"]) if "border-right-color" in c.cssAttr: c.frag.borderRightColor = getColor(c.cssAttr["border-right-color"])
def start(self, c): if self.attr.align is not None: c.frag.alignment = getAlign(self.attr.align) c.clearFrag() self.story = c.swapStory() attrs = self.attr tdata = c.tableData cspan = attrs.colspan rspan = attrs.rowspan row = tdata.row col = tdata.col while 1: for x, y in tdata.span: if x == col and y == row: col += 1 tdata.col += 1 break begin = (col, row) end = (col, row) if cspan: end = (end[0] + cspan - 1, end[1]) if rspan: end = (end[0], end[1] + rspan - 1) if begin != end: #~ print begin, end tdata.add_style(('SPAN', begin, end)) for x in xrange(begin[0], end[0] + 1): for y in xrange(begin[1], end[1] + 1): if x != begin[0] or y != begin[1]: tdata.add_empty(x, y) # Set Border and padding styles tdata.add_cell_styles(c, begin, end, "td") # Calculate widths # Add empty placeholders for new columns if (col + 1) > len(tdata.colw): tdata.colw = tdata.colw + ( (col + 1 - len(tdata.colw)) * [_width()]) # Get value of with, if no spanning if not cspan: width = c.frag.width or self.attr.width # If is value, the set it in the right place in the arry if width is not None: tdata.colw[col] = _width(width) # Calculate heights if row + 1 > len(tdata.rowh): tdata.rowh = tdata.rowh + ( (row + 1 - len(tdata.rowh)) * [_width()]) if not rspan: height = None if height is not None: tdata.rowh[row] = _height(height) tdata.add_style(('FONTSIZE', begin, end, 1.0)) tdata.add_style(('LEADING', begin, end, 1.0)) # Vertical align valign = self.attr.valign or c.frag.vAlign if valign is not None: tdata.add_style(('VALIGN', begin, end, valign.upper())) # Reset border, otherwise the paragraph block will have borders too frag = c.frag frag.borderLeftWidth = 0 frag.borderLeftColor = None frag.borderLeftStyle = None frag.borderRightWidth = 0 frag.borderRightColor = None frag.borderRightStyle = None frag.borderTopWidth = 0 frag.borderTopColor = None frag.borderTopStyle = None frag.borderBottomWidth = 0 frag.borderBottomColor = None frag.borderBottomStyle = None
def CSS2Frag(c, kw, isBlock): def safeGetColor(color, default = None): try: return getColor(color) except ValueError: # the css parser is responsible for calculating of inherited values so inherit do not work here return getColor(default) # COLORS if c.cssAttr.has_key("color"): c.frag.textColor = safeGetColor(c.cssAttr["color"], default="#000000") if c.cssAttr.has_key("background-color"): c.frag.backColor = safeGetColor(c.cssAttr["background-color"], default="#ffffff") # FONT SIZE, STYLE, WEIGHT if c.cssAttr.has_key("font-family"): c.frag.fontName = c.getFontName(c.cssAttr["font-family"]) if c.cssAttr.has_key("font-size"): # XXX inherit c.frag.fontSize = max(getSize("".join(c.cssAttr["font-size"]), c.frag.fontSize, c.baseFontSize), 1.0) if c.cssAttr.has_key("line-height"): leading = "".join(c.cssAttr["line-height"]) c.frag.leading = getSize(leading, c.frag.fontSize) c.frag.leadingSource = leading else: c.frag.leading = getSize(c.frag.leadingSource, c.frag.fontSize) if c.cssAttr.has_key("-pdf-line-spacing"): c.frag.leadingSpace = getSize("".join(c.cssAttr["-pdf-line-spacing"])) # print "line-spacing", c.cssAttr["-pdf-line-spacing"], c.frag.leading if c.cssAttr.has_key("font-weight"): value = c.cssAttr["font-weight"].lower() if value in ("bold", "bolder", "500", "600", "700", "800", "900"): c.frag.bold = 1 else: c.frag.bold = 0 for value in toList(c.cssAttr.get("text-decoration", "")): if "underline" in value: c.frag.underline = 1 if "line-through" in value: c.frag.strike = 1 if "none" in value: c.frag.underline = 0 c.frag.strike = 0 if c.cssAttr.has_key("font-style"): value = c.cssAttr["font-style"].lower() if value in ("italic", "oblique"): c.frag.italic = 1 else: c.frag.italic = 0 if c.cssAttr.has_key("white-space"): # normal | pre | nowrap c.frag.whiteSpace = str(c.cssAttr["white-space"]).lower() # ALIGN & VALIGN if c.cssAttr.has_key("text-align"): c.frag.alignment = getAlign(c.cssAttr["text-align"]) if c.cssAttr.has_key("vertical-align"): c.frag.vAlign = c.cssAttr["vertical-align"] # HEIGHT & WIDTH if c.cssAttr.has_key("height"): c.frag.height = "".join(toList(c.cssAttr["height"])) # XXX Relative is not correct! if c.frag.height in ("auto",): c.frag.height = None if c.cssAttr.has_key("width"): # print c.cssAttr["width"] c.frag.width = "".join(toList(c.cssAttr["width"])) # XXX Relative is not correct! if c.frag.width in ("auto",): c.frag.width = None # ZOOM if c.cssAttr.has_key("zoom"): # print c.cssAttr["width"] zoom = "".join(toList(c.cssAttr["zoom"])) # XXX Relative is not correct! if zoom.endswith("%"): zoom = float(zoom[: - 1]) / 100.0 c.frag.zoom = float(zoom) # MARGINS & LIST INDENT, STYLE if isBlock: if c.cssAttr.has_key("margin-top"): c.frag.spaceBefore = getSize(c.cssAttr["margin-top"], c.frag.fontSize) if c.cssAttr.has_key("margin-bottom"): c.frag.spaceAfter = getSize(c.cssAttr["margin-bottom"], c.frag.fontSize) if c.cssAttr.has_key("margin-left"): c.frag.bulletIndent = kw["margin-left"] # For lists kw["margin-left"] += getSize(c.cssAttr["margin-left"], c.frag.fontSize) c.frag.leftIndent = kw["margin-left"] # print "MARGIN LEFT", kw["margin-left"], c.frag.bulletIndent if c.cssAttr.has_key("margin-right"): kw["margin-right"] += getSize(c.cssAttr["margin-right"], c.frag.fontSize) c.frag.rightIndent = kw["margin-right"] # print c.frag.rightIndent if c.cssAttr.has_key("text-indent"): c.frag.firstLineIndent = getSize(c.cssAttr["text-indent"], c.frag.fontSize) if c.cssAttr.has_key("list-style-type"): c.frag.listStyleType = str(c.cssAttr["list-style-type"]).lower() if c.cssAttr.has_key("list-style-image"): c.frag.listStyleImage = c.getFile(c.cssAttr["list-style-image"]) # PADDINGS if isBlock: if c.cssAttr.has_key("padding-top"): c.frag.paddingTop = getSize(c.cssAttr["padding-top"], c.frag.fontSize) if c.cssAttr.has_key("padding-bottom"): c.frag.paddingBottom = getSize(c.cssAttr["padding-bottom"], c.frag.fontSize) if c.cssAttr.has_key("padding-left"): c.frag.paddingLeft = getSize(c.cssAttr["padding-left"], c.frag.fontSize) if c.cssAttr.has_key("padding-right"): c.frag.paddingRight = getSize(c.cssAttr["padding-right"], c.frag.fontSize) # BORDERS if isBlock: if c.cssAttr.has_key("border-top-width"): c.frag.borderTopWidth = getSize(c.cssAttr["border-top-width"], c.frag.fontSize) if c.cssAttr.has_key("border-bottom-width"): c.frag.borderBottomWidth = getSize(c.cssAttr["border-bottom-width"], c.frag.fontSize) if c.cssAttr.has_key("border-left-width"): c.frag.borderLeftWidth = getSize(c.cssAttr["border-left-width"], c.frag.fontSize) if c.cssAttr.has_key("border-right-width"): c.frag.borderRightWidth = getSize(c.cssAttr["border-right-width"], c.frag.fontSize) if c.cssAttr.has_key("border-top-style"): c.frag.borderTopStyle = c.cssAttr["border-top-style"] if c.cssAttr.has_key("border-bottom-style"): c.frag.borderBottomStyle = c.cssAttr["border-bottom-style"] if c.cssAttr.has_key("border-left-style"): c.frag.borderLeftStyle = c.cssAttr["border-left-style"] if c.cssAttr.has_key("border-right-style"): c.frag.borderRightStyle = c.cssAttr["border-right-style"] if c.cssAttr.has_key("border-top-color"): c.frag.borderTopColor = safeGetColor(c.cssAttr["border-top-color"], default="#ffffff") if c.cssAttr.has_key("border-bottom-color"): c.frag.borderBottomColor = safeGetColor(c.cssAttr["border-bottom-color"], default="#ffffff") if c.cssAttr.has_key("border-left-color"): c.frag.borderLeftColor = safeGetColor(c.cssAttr["border-left-color"], default="#ffffff") if c.cssAttr.has_key("border-right-color"): c.frag.borderRightColor = safeGetColor(c.cssAttr["border-right-color"], default="#ffffff")
def CSS2Frag(c, kw, isBlock): def safeGetColor(color, default=None): try: return getColor(color) except ValueError: # the css parser is responsible for calculating of inherited values so inherit do not work here return getColor(default) # COLORS if c.cssAttr.has_key("color"): c.frag.textColor = safeGetColor(c.cssAttr["color"], default="#000000") if c.cssAttr.has_key("background-color"): c.frag.backColor = safeGetColor(c.cssAttr["background-color"], default="#ffffff") # FONT SIZE, STYLE, WEIGHT if c.cssAttr.has_key("font-family"): c.frag.fontName = c.getFontName(c.cssAttr["font-family"]) if c.cssAttr.has_key("font-size"): # XXX inherit c.frag.fontSize = max( getSize("".join(c.cssAttr["font-size"]), c.frag.fontSize, c.baseFontSize), 1.0) if c.cssAttr.has_key("line-height"): leading = "".join(c.cssAttr["line-height"]) c.frag.leading = getSize(leading, c.frag.fontSize) c.frag.leadingSource = leading else: c.frag.leading = getSize(c.frag.leadingSource, c.frag.fontSize) if c.cssAttr.has_key("-pdf-line-spacing"): c.frag.leadingSpace = getSize("".join(c.cssAttr["-pdf-line-spacing"])) # print "line-spacing", c.cssAttr["-pdf-line-spacing"], c.frag.leading if c.cssAttr.has_key("font-weight"): value = c.cssAttr["font-weight"].lower() if value in ("bold", "bolder", "500", "600", "700", "800", "900"): c.frag.bold = 1 else: c.frag.bold = 0 for value in toList(c.cssAttr.get("text-decoration", "")): if "underline" in value: c.frag.underline = 1 if "line-through" in value: c.frag.strike = 1 if "none" in value: c.frag.underline = 0 c.frag.strike = 0 if c.cssAttr.has_key("font-style"): value = c.cssAttr["font-style"].lower() if value in ("italic", "oblique"): c.frag.italic = 1 else: c.frag.italic = 0 if c.cssAttr.has_key("white-space"): # normal | pre | nowrap c.frag.whiteSpace = str(c.cssAttr["white-space"]).lower() # ALIGN & VALIGN if c.cssAttr.has_key("text-align"): c.frag.alignment = getAlign(c.cssAttr["text-align"]) if c.cssAttr.has_key("vertical-align"): c.frag.vAlign = c.cssAttr["vertical-align"] # HEIGHT & WIDTH if c.cssAttr.has_key("height"): c.frag.height = "".join(toList( c.cssAttr["height"])) # XXX Relative is not correct! if c.frag.height in ("auto", ): c.frag.height = None if c.cssAttr.has_key("width"): # print c.cssAttr["width"] c.frag.width = "".join(toList( c.cssAttr["width"])) # XXX Relative is not correct! if c.frag.width in ("auto", ): c.frag.width = None # ZOOM if c.cssAttr.has_key("zoom"): # print c.cssAttr["width"] zoom = "".join(toList( c.cssAttr["zoom"])) # XXX Relative is not correct! if zoom.endswith("%"): zoom = float(zoom[:-1]) / 100.0 c.frag.zoom = float(zoom) # MARGINS & LIST INDENT, STYLE if isBlock: if c.cssAttr.has_key("margin-top"): c.frag.spaceBefore = getSize(c.cssAttr["margin-top"], c.frag.fontSize) if c.cssAttr.has_key("margin-bottom"): c.frag.spaceAfter = getSize(c.cssAttr["margin-bottom"], c.frag.fontSize) if c.cssAttr.has_key("margin-left"): c.frag.bulletIndent = kw["margin-left"] # For lists kw["margin-left"] += getSize(c.cssAttr["margin-left"], c.frag.fontSize) c.frag.leftIndent = kw["margin-left"] # print "MARGIN LEFT", kw["margin-left"], c.frag.bulletIndent if c.cssAttr.has_key("margin-right"): kw["margin-right"] += getSize(c.cssAttr["margin-right"], c.frag.fontSize) c.frag.rightIndent = kw["margin-right"] # print c.frag.rightIndent if c.cssAttr.has_key("text-indent"): c.frag.firstLineIndent = getSize(c.cssAttr["text-indent"], c.frag.fontSize) if c.cssAttr.has_key("list-style-type"): c.frag.listStyleType = str(c.cssAttr["list-style-type"]).lower() if c.cssAttr.has_key("list-style-image"): c.frag.listStyleImage = c.getFile(c.cssAttr["list-style-image"]) # PADDINGS if isBlock: if c.cssAttr.has_key("padding-top"): c.frag.paddingTop = getSize(c.cssAttr["padding-top"], c.frag.fontSize) if c.cssAttr.has_key("padding-bottom"): c.frag.paddingBottom = getSize(c.cssAttr["padding-bottom"], c.frag.fontSize) if c.cssAttr.has_key("padding-left"): c.frag.paddingLeft = getSize(c.cssAttr["padding-left"], c.frag.fontSize) if c.cssAttr.has_key("padding-right"): c.frag.paddingRight = getSize(c.cssAttr["padding-right"], c.frag.fontSize) # BORDERS if isBlock: if c.cssAttr.has_key("border-top-width"): c.frag.borderTopWidth = getSize(c.cssAttr["border-top-width"], c.frag.fontSize) if c.cssAttr.has_key("border-bottom-width"): c.frag.borderBottomWidth = getSize( c.cssAttr["border-bottom-width"], c.frag.fontSize) if c.cssAttr.has_key("border-left-width"): c.frag.borderLeftWidth = getSize(c.cssAttr["border-left-width"], c.frag.fontSize) if c.cssAttr.has_key("border-right-width"): c.frag.borderRightWidth = getSize(c.cssAttr["border-right-width"], c.frag.fontSize) if c.cssAttr.has_key("border-top-style"): c.frag.borderTopStyle = c.cssAttr["border-top-style"] if c.cssAttr.has_key("border-bottom-style"): c.frag.borderBottomStyle = c.cssAttr["border-bottom-style"] if c.cssAttr.has_key("border-left-style"): c.frag.borderLeftStyle = c.cssAttr["border-left-style"] if c.cssAttr.has_key("border-right-style"): c.frag.borderRightStyle = c.cssAttr["border-right-style"] if c.cssAttr.has_key("border-top-color"): c.frag.borderTopColor = safeGetColor(c.cssAttr["border-top-color"], default="#ffffff") if c.cssAttr.has_key("border-bottom-color"): c.frag.borderBottomColor = safeGetColor( c.cssAttr["border-bottom-color"], default="#ffffff") if c.cssAttr.has_key("border-left-color"): c.frag.borderLeftColor = safeGetColor( c.cssAttr["border-left-color"], default="#ffffff") if c.cssAttr.has_key("border-right-color"): c.frag.borderRightColor = safeGetColor( c.cssAttr["border-right-color"], default="#ffffff")
c.frag.strike = 1 if "none" in value: c.frag.underline = 0 c.frag.strike = 0 if "font-style" in c.cssAttr: value = c.cssAttr["font-style"].lower() if value in ("italic", "oblique"): c.frag.italic = 1 else: c.frag.italic = 0 if "white-space" in c.cssAttr: # normal | pre | nowrap c.frag.whiteSpace = str(c.cssAttr["white-space"]).lower() # ALIGN & VALIGN if "text-align" in c.cssAttr: c.frag.alignment = getAlign(c.cssAttr["text-align"]) if "vertical-align" in c.cssAttr: c.frag.vAlign = c.cssAttr["vertical-align"] # HEIGHT & WIDTH if "height" in c.cssAttr: c.frag.height = "".join(toList(c.cssAttr["height"])) # XXX Relative is not correct! if c.frag.height in ("auto",): c.frag.height = None if "width" in c.cssAttr: c.frag.width = "".join(toList(c.cssAttr["width"])) # XXX Relative is not correct! if c.frag.width in ("auto",): c.frag.width = None # ZOOM if "zoom" in c.cssAttr: zoom = "".join(toList(c.cssAttr["zoom"])) # XXX Relative is not correct! if zoom.endswith("%"):