【发布时间】:2013-02-05 23:56:37
【问题描述】:
我有以下代码尝试调整reportlab Platypus flowable 文本的字体大小,直到它适合我给它的可用高度。但是我发现 Paragraph flowable 没有在递归的每个循环中保存其 style.fontSize 属性,并且 python baulks 具有无限递归。
def fits_space(w,h,aW,aH,p):
"""
Determines if inputted text fits in the space given for a paragraph
and if it doesn't, reduces the font-size until it does.
"""
if w<=aW and h<=aH:
# return font size to apply it to the para again
print "final font_size: %d" % p.style.fontSize
return p # now render the paragraph in the doctemplate
else:
p.style.fontSize -= 1
w,h = p.wrap(aW, aH)
fits_space(w,h,aW,aH,p)
def renderPage(name, text):
doc = SimpleDocTemplate("%s.pdf" % name)
parts = []
style = ParagraphStyle(name='fancy')
style.fontSize = 150
p = Paragraph(text, style)
aW = PAGE_WIDTH-4*inch # available width and height
aH = PAGE_HEIGHT-4*inch
w,h = p.wrap(aW, aH) # find required space
p = fits_space(w,h,aW,aH,p) # recursively fit the font size to the text
parts.append(p)
doc.build(parts)
谁能告诉我为什么 - 在 fit_space() 函数中,在 else 子句中,当我调用 p.wrap(aW, aH) 时,输出的值与我将段落的 fontSize 减 1 之前的值相同吗?如果我减小字体大小,包裹的高度肯定会更小吗?
任何想法我哪里出错了?
更新 Nitzie 下面的代码几乎可以工作,在我的例子中,只需要在 style.leading 中添加一个调整大小:
def shrink_font_size(aW, aH, text, style):
"""Shrinks font size by using pdfmetrics to calculate the height
of a paragraph, given the font name, size, and available width."""
def break_lines(text, aW):
# simpleSplit calculates how reportlab will break up the lines for
# display in a paragraph, by using width/fontsize.
return simpleSplit(text, style.fontName, style.fontSize, aW)
def line_wrap(lines, style):
# Get overall width of text by getting stringWidth of longest line
width = stringWidth(max(lines), style.fontName, style.fontSize)
# Paragraph height can be calculated via line spacing and number of lines.
height = style.leading * len(lines)
return width, height
lines = break_lines(text, aW)
width, height = line_wrap(lines, style)
while height > aH or width > aW:
style.fontSize -= 1
style.leading -= 1 # do this if you're leading is based on the fontSize
lines = break_lines(text, aW)
width, height = line_wrap(lines, style)
TIA
【问题讨论】:
-
PAGE_WIDTH和PAGE_HEIGHT变量从何而来? -
我修复了
KeepInFrame的类似问题。我想我会留下答案的链接,以防有人像我一样偶然发现这个问题。 stackoverflow.com/questions/12014573/…