1 # docbook.py: extension module
2 # $Id: docbook.py 8353 2009-03-17 16:57:50Z mzjn $
13 unitHash = { 'in': pixelsPerInch,
14 'cm': pixelsPerInch / 2.54,
15 'mm': pixelsPerInch / 25.4,
16 'pc': (pixelsPerInch / 72.0) * 12,
17 'pt': pixelsPerInch / 72.0,
20 # ======================================================================
22 def adjustColumnWidths(ctx, nodeset):
24 # Small check to verify the context is correcly accessed
27 pctxt = libxslt.xpathParserContext(_obj=ctx)
28 ctxt = pctxt.context()
29 tctxt = ctxt.transformContext()
33 # Get the nominal table width
34 varString = lookupVariable(tctxt, "nominal.table.width", None)
36 nominalWidth = 6 * pixelsPerInch;
38 nominalWidth = convertLength(varString);
40 # Get the requested table width
41 tableWidth = lookupVariable(tctxt, "table.width", "100%")
43 foStylesheet = (tctxt.variableLookup("stylesheet.result.type", None) == "fo")
51 colgroup = libxml2.xmlNode(_obj = nodeset[0])
52 # If this is an foStylesheet, we've been passed a list of fo:table-columns.
53 # Otherwise we've been passed a colgroup that contains a list of cols.
55 colChildren = colgroup
57 colChildren = colgroup.children
62 width = col.prop("column-width")
64 width = col.prop("width")
71 starPos = string.find(width, "*")
73 relPart, absPart = string.split(width, "*", 2)
74 relPart = float(relPart)
75 relTotal = relTotal + float(relPart)
79 pixels = convertLength(absPart)
80 absTotal = absTotal + pixels
82 relParts.append(relPart)
83 absParts.append(pixels)
87 # Ok, now we have the relative widths and absolute widths in
88 # two parallel arrays.
90 # - If there are no relative widths, output the absolute widths
91 # - If there are no absolute widths, output the relative widths
92 # - If there are a mixture of relative and absolute widths,
93 # - If the table width is absolute, turn these all into absolute
95 # - If the table width is relative, turn these all into absolute
96 # widths in the nominalWidth and then turn them back into
102 for absPart in absParts:
104 inches = absPart / pixelsPerInch
105 widths.append("%4.2fin" % inches)
107 widths.append("%d" % absPart)
109 for relPart in relParts:
110 rel = relPart / relTotal * 100
112 widths = correctRoundingError(widths)
114 pixelWidth = nominalWidth
115 if string.find(tableWidth, "%") < 0:
116 pixelWidth = convertLength(tableWidth)
118 if pixelWidth <= absTotal:
119 print "Table is wider than table width"
121 pixelWidth = pixelWidth - absTotal
124 for count in range(len(relParts)):
125 rel = relParts[count] / relTotal * pixelWidth
126 relParts[count] = rel + absParts[count]
127 absTotal = absTotal + rel + absParts[count]
129 if string.find(tableWidth, "%") < 0:
130 for count in range(len(relParts)):
132 pixels = relParts[count]
133 inches = pixels / pixelsPerInch
134 widths.append("%4.2fin" % inches)
136 widths.append(relParts[count])
138 for count in range(len(relParts)):
139 rel = relParts[count] / absTotal * 100
141 widths = correctRoundingError(widths)
143 # Danger, Will Robinson! In-place modification of the result tree!
144 # Side-effect free? We don' need no steenkin' side-effect free!
149 col.setProp("column-width", widths[count])
151 col.setProp("width", widths[count])
158 def convertLength(length):
159 # Given "3.4in" return the width in pixels
163 m = re.search('([+-]?[\d\.]+)(\S+)', length)
164 if m != None and m.lastindex > 1:
166 if unitHash.has_key(m.group(2)):
167 unit = unitHash[m.group(2)]
169 print "Unrecognized length: " + m.group(2)
171 pixels = unit * float(m.group(1))
177 def correctRoundingError(floatWidths):
178 # The widths are currently floating point numbers, we have to truncate
179 # them back to integers and then distribute the error so that they sum
184 for width in floatWidths:
185 width = math.floor(width)
187 totalWidth = totalWidth + math.floor(width)
189 totalError = 100 - totalWidth
190 columnError = totalError / len(widths)
192 for count in range(len(widths)):
193 width = widths[count]
194 error = error + columnError
196 adj = math.floor(error)
198 widths[count] = "%d%%" % (width + adj)
200 widths[count] = "%d%%" % width
204 def lookupVariable(tctxt, varName, default):
205 varString = tctxt.variableLookup(varName, None)
206 if varString == None:
209 # If it's a list, get the first element
210 if type(varString) == type([]):
211 varString = varString[0]
213 # If it's not a string, it must be a node, get its content
214 if type(varString) != type(""):
215 varString = varString.content
219 # ======================================================================
222 #once you have a node which is a libxml2 python xmlNode wrapper all common
223 #operations are possible:
224 # .children .last .parent .next .prev .doc for navigation
225 # .content .type for introspection
226 # .prop("attribute_name") to lookup attribute values
228 # # Now make a nodeset to return
229 # # Danger, Will Robinson! This creates a memory leak!
230 # newDoc = libxml2.newDoc("1.0")
231 # newColGroup = newDoc.newDocNode(None, "colgroup", None)
232 # newDoc.addChild(newColGroup)
233 # col = colgroup.children
235 # newCol = newDoc.newDocNode(None, "col", None)
236 # newCol.copyPropList(col);
237 # newCol.setProp("width", "4")
238 # newColGroup.addChild(newCol)