本文整理匯總了Python中reportlab.pdfbase.pdfmetrics.stringWidth方法的典型用法代碼示例。如果您正苦於以下問題:Python pdfmetrics.stringWidth方法的具體用法?Python pdfmetrics.stringWidth怎麽用?Python pdfmetrics.stringWidth使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類reportlab.pdfbase.pdfmetrics
的用法示例。
在下文中一共展示了pdfmetrics.stringWidth方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _getWidths
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def _getWidths(i,s, fontName, fontSize, subCols):
S = []
aS = S.append
if isSeq(s):
for j,t in enumerate(s):
sc = subCols[j,i]
fN = getattr(sc,'fontName',fontName)
fS = getattr(sc,'fontSize',fontSize)
m = [stringWidth(x, fN, fS) for x in t.split('\n')]
m = max(sc.minWidth,m and max(m) or 0)
aS(m)
aS(sc.rpad)
del S[-1]
else:
sc = subCols[0,i]
fN = getattr(sc,'fontName',fontName)
fS = getattr(sc,'fontSize',fontSize)
m = [stringWidth(x, fN, fS) for x in s.split('\n')]
aS(max(sc.minWidth,m and max(m) or 0))
return S
示例2: drawHumanReadable
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def drawHumanReadable(self):
if self.humanReadable:
#we have text
from reportlab.pdfbase.pdfmetrics import getAscent, stringWidth
s = str(self._humanText())
fontSize = self.fontSize
fontName = self.fontName
w = stringWidth(s,fontName,fontSize)
width = self._width
if self.quiet:
width -= self.lquiet+self.rquiet
x = self.lquiet
else:
x = 0
if w>width: fontSize *= width/float(w)
y = 1.07*getAscent(fontName)*fontSize/1000.
self.annotate(x+width/2.,-y,s,fontName,fontSize)
示例3: drawString
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def drawString(self, stringObj):
if self._fill:
S = self._tracker.getState()
text_anchor, x, y, text, enc = S['textAnchor'], stringObj.x,stringObj.y,stringObj.text, stringObj.encoding
if not text_anchor in ['start','inherited']:
font, font_size = S['fontName'], S['fontSize']
textLen = stringWidth(text, font, font_size, enc)
if text_anchor=='end':
x -= textLen
elif text_anchor=='middle':
x -= textLen*0.5
elif text_anchor=='numeric':
x -= numericXShift(text_anchor,text,textLen,font,font_size,enc)
else:
raise ValueError('bad value for textAnchor '+str(text_anchor))
t = self._canvas.beginText(x,y)
t.textLine(text)
self._canvas.drawText(t)
示例4: _do_dots_frag
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def _do_dots_frag(cur_x, cur_x_s, maxWidth, xs, tx):
text,fontName,fontSize,textColor,backColor,dy = _getDotsInfo(xs.style)
txtlen = tx._canvas.stringWidth(text, fontName, fontSize)
if cur_x_s+txtlen<=maxWidth:
if tx._fontname!=fontName or tx._fontsize!=fontSize:
tx.setFont(fontName,fontSize)
maxWidth += getattr(tx,'_dotsOffsetX',tx._x0)
tx.setTextOrigin(0,xs.cur_y+dy)
setXPos(tx,cur_x_s-cur_x)
n = int((maxWidth-cur_x_s)/txtlen)
setXPos(tx,maxWidth - txtlen*n)
if xs.textColor!=textColor:
tx.setFillColor(textColor)
if backColor: xs.backColors.append((cur_x,maxWidth,backColor))
tx._textOut(n*text,1)
if dy: tx.setTextOrigin(tx._x0,xs.cur_y-dy)
示例5: _handleBulletWidth
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def _handleBulletWidth(bulletText,style,maxWidths):
'''work out bullet width and adjust maxWidths[0] if neccessary
'''
if bulletText:
if isinstance(bulletText,strTypes):
bulletWidth = stringWidth( bulletText, style.bulletFontName, style.bulletFontSize)
else:
#it's a list of fragments
bulletWidth = 0
for f in bulletText:
bulletWidth += stringWidth(f.text, f.fontName, f.fontSize)
bulletLen = style.bulletIndent + bulletWidth + 0.6 * style.bulletFontSize
if style.wordWrap=='RTL':
indent = style.rightIndent+style.firstLineIndent
else:
indent = style.leftIndent+style.firstLineIndent
if bulletLen > indent:
#..then it overruns, and we have less space available on line 1
maxWidths[0] -= (bulletLen - indent)
示例6: hBoxText
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def hBoxText(msg, canvas, x, y, fontName):
"""Helper for stringwidth tests on Asian fonts.
Registers font if needed. Then draws the string,
and a box around it derived from the stringWidth function"""
canvas.saveState()
try:
font = pdfmetrics.getFont(fontName)
except KeyError:
font = cidfonts.UnicodeCIDFont(fontName)
pdfmetrics.registerFont(font)
canvas.setFillGray(0.8)
canvas.rect(x,y,pdfmetrics.stringWidth(msg, fontName, 16),16,stroke=0,fill=1)
canvas.setFillGray(0)
canvas.setFont(fontName, 16,16)
canvas.drawString(x,y,msg)
canvas.restoreState()
示例7: _getWidths
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def _getWidths(i,s, fontName, fontSize, subCols):
S = []
aS = S.append
if isSeqType(s):
for j,t in enumerate(s):
sc = subCols[j,i]
fN = getattr(sc,'fontName',fontName)
fS = getattr(sc,'fontSize',fontSize)
m = [stringWidth(x, fN, fS) for x in t.split('\n')]
m = max(sc.minWidth,m and max(m) or 0)
aS(m)
aS(sc.rpad)
del S[-1]
else:
sc = subCols[0,i]
fN = getattr(sc,'fontName',fontName)
fS = getattr(sc,'fontSize',fontSize)
m = [stringWidth(x, fN, fS) for x in s.split('\n')]
aS(max(sc.minWidth,m and max(m) or 0))
return S
示例8: _text2PathDescription
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def _text2PathDescription(text, x=0, y=0, fontName=_baseGFontName, fontSize=1000,
anchor='start', truncate=1, pathReverse=0):
global _gs
if not _gs:
import _renderPM
_gs = _renderPM.gstate(1,1)
from reportlab.graphics import renderPM
renderPM._setFont(_gs,fontName,fontSize)
P = []
if not anchor=='start':
textLen = stringWidth(text, fontName,fontSize)
if anchor=='end':
x = x-textLen
elif anchor=='middle':
x = x - textLen/2.
for g in _gs._stringPath(text,x,y):
P.extend(_processGlyph(g,truncate=truncate,pathReverse=pathReverse))
return P
示例9: drawString
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def drawString(self, stringObj):
if self._fill:
S = self._tracker.getState()
text_anchor, x, y, text, enc = S['textAnchor'], stringObj.x,stringObj.y,stringObj.text, stringObj.encoding
if not text_anchor in ['start','inherited']:
font, font_size = S['fontName'], S['fontSize']
textLen = stringWidth(text, font, font_size, enc)
if text_anchor=='end':
x -= textLen
elif text_anchor=='middle':
x -= textLen*0.5
elif text_anchor=='numeric':
x -= numericXShift(text_anchor,text,textLen,font,font_size,enc)
else:
raise ValueError, 'bad value for textAnchor '+str(text_anchor)
t = self._canvas.beginText(x,y)
t.textLine(text)
self._canvas.drawText(t)
示例10: _handleBulletWidth
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def _handleBulletWidth(bulletText,style,maxWidths):
'''work out bullet width and adjust maxWidths[0] if neccessary
'''
if bulletText:
if isinstance(bulletText,basestring):
bulletWidth = stringWidth( bulletText, style.bulletFontName, style.bulletFontSize)
else:
#it's a list of fragments
bulletWidth = 0
for f in bulletText:
bulletWidth = bulletWidth + stringWidth(f.text, f.fontName, f.fontSize)
bulletRight = style.bulletIndent + bulletWidth + 0.6 * style.bulletFontSize
indent = style.leftIndent+style.firstLineIndent
if bulletRight > indent:
#..then it overruns, and we have less space available on line 1
maxWidths[0] -= (bulletRight - indent)
示例11: _header_footer
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def _header_footer(self, canvas, doc):
# Save the state of our canvas so we can draw on it
canvas.saveState()
style_right = ParagraphStyle(name='right', parent=self.bodystyle, fontName='arialuni',
fontSize=10, alignment=TA_RIGHT)
fieldsight_logo = Image('http://' + self.base_url +'/static/images/fs1.jpg')
fieldsight_logo._restrictSize(1.5 * inch, 1.5 * inch)
# headerleft = Paragraph("FieldSight", self.bodystyle)
headerright = Paragraph(self.project_name, style_right)
# w1, h1 = headerleft.wrap(doc.width, doc.topMargin)
w2, h2 = headerright.wrap(doc.width, doc.topMargin)
textWidth = stringWidth(self.project_name, fontName='arialuni',
fontSize=10)
fieldsight_logo.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin + 12)
headerright.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin + 20)
try:
project_logo = Image(self.project_logo)
project_logo._restrictSize(0.4 * inch, 0.4 * inch)
project_logo.drawOn(canvas, headerright.width + doc.leftMargin -0.5 * inch - textWidth, doc.height + doc.topMargin + 10)
except:
pass
# header.drawOn(canvas, doc.leftMargin + doc.width, doc.height + doc.topMargin +20)
# Footer
footer = Paragraph('Page no. '+str(canvas._pageNumber), style_right)
w, h = footer.wrap(doc.width, doc.bottomMargin)
footer.drawOn(canvas, doc.leftMargin, h + 40)
# Release the canvas
canvas.restoreState()
示例12: split_line
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def split_line(line, max_width, get_width_func=None):
"""
Split `line` into multi-lines if width exceeds `max_width`.
:param line: Line to be split.
:param max_width: Maximum length of each line (unit: px).
:param get_width_func: A function which computes width of string
according to font and font size.
:return: list of lines
"""
result = []
total_width = 0
tmp_str = ""
get_text_width = (
get_width_func
if get_width_func
else lambda text: stringWidth(text, "Helvetica", 9)
)
for ch in line:
char_width = get_text_width(ch)
if total_width + char_width <= max_width or not tmp_str:
tmp_str += ch
total_width += char_width
else:
result.append(tmp_str)
tmp_str = ch
total_width = char_width
if tmp_str:
result.append(tmp_str)
return result
示例13: split_text
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def split_text(
text, font_name, font_size, max_width, keep_leading_whitespace=False
):
"""
Wraps `text` within given `max_width` limit (measured in px), keeping
initial indentation of each line (and generated lines) if
`keep_leading_whitespace` is True.
:param text: Text to be split.
:param font_name: Font name.
:param font_size: Font size.
:param max_width: Maximum length of each line (unit: px).
:param keep_leading_whitespace: each split line keeps the leading
whitespace.
:return: list of lines
"""
def get_text_width(text, name=font_name, size=font_size):
return stringWidth(text, name, size)
result = []
lines = [line for line in re.split(r"[\r\n]+", text) if line]
for line in lines:
line_list = split_line(line, max_width, get_text_width)
if keep_leading_whitespace and len(line_list) > 1:
first, rest = line_list[0], line_list[1:]
indent_match = _DESCRIPTION_CUTOFF_REGEX.match(first)
if indent_match:
prefix = first[indent_match.start() : indent_match.end()]
line_list = [first] + ["{}{}".format(prefix, s) for s in rest]
result.extend(line_list)
return os.linesep.join(result)
示例14: makeCircularString
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def makeCircularString(x, y, radius, angle, text, fontName, fontSize, inside=0, G=None,textAnchor='start'):
'''make a group with circular text in it'''
if not G: G = Group()
angle %= 360
pi180 = pi/180
phi = angle*pi180
width = stringWidth(text, fontName, fontSize)
sig = inside and -1 or 1
hsig = sig*0.5
sig90 = sig*90
if textAnchor!='start':
if textAnchor=='middle':
phi += sig*(0.5*width)/radius
elif textAnchor=='end':
phi += sig*float(width)/radius
elif textAnchor=='numeric':
phi += sig*float(numericXShift(textAnchor,text,width,fontName,fontSize,None))/radius
for letter in text:
width = stringWidth(letter, fontName, fontSize)
beta = float(width)/radius
h = Group()
h.add(String(0, 0, letter, fontName=fontName,fontSize=fontSize,textAnchor="start"))
h.translate(x+cos(phi)*radius,y+sin(phi)*radius) #translate to radius and angle
h.rotate((phi-hsig*beta)/pi180-sig90) # rotate as needed
G.add(h) #add to main group
phi -= sig*beta #increment
return G
示例15: _text2PathDescription
# 需要導入模塊: from reportlab.pdfbase import pdfmetrics [as 別名]
# 或者: from reportlab.pdfbase.pdfmetrics import stringWidth [as 別名]
def _text2PathDescription(text, x=0, y=0, fontName=_baseGFontName, fontSize=1000,
anchor='start', truncate=1, pathReverse=0):
from reportlab.graphics import renderPM, _renderPM
_gs = _renderPM.gstate(1,1)
renderPM._setFont(_gs,fontName,fontSize)
P = []
if not anchor=='start':
textLen = stringWidth(text, fontName,fontSize)
if anchor=='end':
x = x-textLen
elif anchor=='middle':
x = x - textLen/2.
for g in _gs._stringPath(text,x,y):
P.extend(_processGlyph(g,truncate=truncate,pathReverse=pathReverse))
return P