本文整理汇总了Python中reportlab.graphics.shapes.Group.translate方法的典型用法代码示例。如果您正苦于以下问题:Python Group.translate方法的具体用法?Python Group.translate怎么用?Python Group.translate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reportlab.graphics.shapes.Group
的用法示例。
在下文中一共展示了Group.translate方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ttf_ocr
# 需要导入模块: from reportlab.graphics.shapes import Group [as 别名]
# 或者: from reportlab.graphics.shapes.Group import translate [as 别名]
def ttf_ocr(font, key=None):
gs = font.getGlyphSet()
keys = [key for key in gs.keys() if key.startswith("uni")] if key is None else [key]
c = []
for i, key in enumerate(keys):
if key not in gs:
logger.info("No this key: %s" % key)
c.append("")
continue
pen = ReportLabPen(gs, Path(fillColor=colors.black, strokeWidth=0.01))
g = gs[key]
g.draw(pen)
w, h = 50, 50
g = Group(pen.path)
g.translate(10, 10)
g.scale(0.02, 0.02)
d = Drawing(w, h)
d.add(g)
renderPM.drawToFile(d, png_file.name, fmt="PNG")
result = os.popen("tesseract %s stdout -l chi_sim -psm 5" % png_file.name).read().strip().decode("utf-8",
"ignore")
if len(result) != 1:
result = os.popen("tesseract %s stdout -l chi_sim -psm 8" % png_file.name).read().strip().decode("utf-8",
"ignore")
logger.info("key: %s, result: %s" % (key, result))
c.append(result)
if key is not None:
return c[0]
return c
示例2: makeCircularString
# 需要导入模块: from reportlab.graphics.shapes import Group [as 别名]
# 或者: from reportlab.graphics.shapes.Group import translate [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
示例3: convertText
# 需要导入模块: from reportlab.graphics.shapes import Group [as 别名]
# 或者: from reportlab.graphics.shapes.Group import translate [as 别名]
def convertText(self, node):
attrConv = self.attrConverter
x, y = map(node.getAttribute, ('x', 'y'))
x, y = map(attrConv.convertLength, (x, y))
xml_space = node.getAttribute("{%s}space" % XML_NS)
if xml_space:
preserve_space = xml_space == 'preserve'
else:
preserve_space = self.preserve_space
gr = Group()
frag_lengths = []
dx0, dy0 = 0, 0
x1, y1 = 0, 0
ff = attrConv.findAttr(node, "font-family") or "Helvetica"
ff = attrConv.convertFontFamily(ff)
fs = attrConv.findAttr(node, "font-size") or "12"
fs = attrConv.convertLength(fs)
for c in itertools.chain([node], node.getchildren()):
has_x = False
dx, dy = 0, 0
baseLineShift = 0
if node_name(c) == 'text':
text = self.clean_text(c.text, preserve_space)
if not text:
continue
elif node_name(c) == 'tspan':
text = self.clean_text(c.text, preserve_space)
if not text:
continue
x1, y1, dx, dy = [c.attrib.get(name, '') for name in ("x", "y", "dx", "dy")]
has_x = x1 != ''
x1, y1, dx, dy = map(attrConv.convertLength, (x1, y1, dx, dy))
dx0 = dx0 + dx
dy0 = dy0 + dy
baseLineShift = c.attrib.get("baseline-shift", '0')
if baseLineShift in ("sub", "super", "baseline"):
baseLineShift = {"sub":-fs/2, "super":fs/2, "baseline":0}[baseLineShift]
else:
baseLineShift = attrConv.convertLength(baseLineShift, fs)
else:
continue
frag_lengths.append(stringWidth(text, ff, fs))
new_x = x1 if has_x else sum(frag_lengths[:-1])
shape = String(x + new_x, y - y1 - dy0 + baseLineShift, text)
self.applyStyleOnShape(shape, node)
if node_name(c) == 'tspan':
self.applyStyleOnShape(shape, c)
gr.add(shape)
gr.scale(1, -1)
gr.translate(0, -2*y)
return gr
示例4: _rawDraw
# 需要导入模块: from reportlab.graphics.shapes import Group [as 别名]
# 或者: from reportlab.graphics.shapes.Group import translate [as 别名]
def _rawDraw(self):
_text = self._text
self._text = _text or ""
self.computeSize()
self._text = _text
g = Group()
g.translate(self.x + self.dx, self.y + self.dy)
g.rotate(self.angle)
y = self._top - self._leading * self._baselineRatio
textAnchor = self._getTextAnchor()
if textAnchor == "start":
x = self._left
elif textAnchor == "middle":
x = self._left + self._ewidth * 0.5
else:
x = self._right
# paint box behind text just in case they
# fill it
if self.boxFillColor or (self.boxStrokeColor and self.boxStrokeWidth):
g.add(
Rect(
self._left - self.leftPadding,
self._bottom - self.bottomPadding,
self._width,
self._height,
strokeColor=self.boxStrokeColor,
strokeWidth=self.boxStrokeWidth,
fillColor=self.boxFillColor,
)
)
fillColor, fontName, fontSize = self.fillColor, self.fontName, self.fontSize
strokeColor, strokeWidth, leading = self.strokeColor, self.strokeWidth, self._leading
svgAttrs = getattr(self, "_svgAttrs", {})
if strokeColor:
for line in self._lines:
s = _text2Path(line, x, y, fontName, fontSize, textAnchor)
s.fillColor = fillColor
s.strokeColor = strokeColor
s.strokeWidth = strokeWidth
g.add(s)
y -= leading
else:
for line in self._lines:
s = String(x, y, line, _svgAttrs=svgAttrs)
s.textAnchor = textAnchor
s.fontName = fontName
s.fontSize = fontSize
s.fillColor = fillColor
g.add(s)
y -= leading
return g
示例5: draw
# 需要导入模块: from reportlab.graphics.shapes import Group [as 别名]
# 或者: from reportlab.graphics.shapes.Group import translate [as 别名]
def draw(self):
_text = self._text
self._text = _text or ''
self.computeSize()
self._text = _text
g = Group()
g.translate(self.x + self.dx, self.y + self.dy)
g.rotate(self.angle)
y = self._top - self.fontSize
textAnchor = self._getTextAnchor()
if textAnchor == 'start':
x = self._left
elif textAnchor == 'middle':
x = self._left + self._ewidth * 0.5
else:
x = self._right
# paint box behind text just in case they
# fill it
if self.boxFillColor or (self.boxStrokeColor and self.boxStrokeWidth):
g.add(
Rect(
self._left - self.leftPadding,
self._bottom - self.bottomPadding,
self._width,
self._height,
strokeColor=self.boxStrokeColor,
strokeWidth=self.boxStrokeWidth,
fillColor=self.boxFillColor))
fillColor, fontName, fontSize = self.fillColor, self.fontName, self.fontSize
strokeColor, strokeWidth, leading = self.strokeColor, self.strokeWidth, (
self.leading or 1.2 * fontSize)
if strokeColor:
for line in self._lines:
s = _text2Path(line, x, y, fontName, fontSize, textAnchor)
s.fillColor = fillColor
s.strokeColor = strokeColor
s.strokeWidth = strokeWidth
g.add(s)
y = y - leading
else:
for line in self._lines:
s = String(x, y, line)
s.textAnchor = textAnchor
s.fontName = fontName
s.fontSize = fontSize
s.fillColor = fillColor
g.add(s)
y = y - leading
return g
示例6: process
# 需要导入模块: from reportlab.graphics.shapes import Group [as 别名]
# 或者: from reportlab.graphics.shapes.Group import translate [as 别名]
def process(O, grid_ij, xy_max, label, data, label_font_size=12):
if (reportlab is None): return
from reportlab.graphics.shapes import Group, String
lp = O.line_plot(xy_max, data=data, label_font_size=label_font_size)
gr = Group(lp)
i,j = grid_ij
assert 0 <= i < O.grid[0]
assert 0 <= j < O.grid[1]
i = O.grid[0] - 1 - i
tx, ty = O.margin + j * (O.page_size[0] - 2 * O.margin) / O.grid[1] \
- j * O.more_narrow_shift, \
O.margin + i * (O.page_size[1] - 2 * O.margin
- O.top_label_space) / O.grid[0]
gr.translate(tx, ty)
O.top_group.add(gr)
O.top_group.add(String(
tx+lp.x+lp.width*0.5,
ty+lp.y+lp.height*1.05,
label,
fontSize=label_font_size,
textAnchor="middle"))
if (i == 0 and j == 0):
O.top_group.add(String(
tx+lp.x+lp.width*0.5,
ty+lp.y-lp.height*0.3,
u"RMSD start (\u00C5)",
fontSize=label_font_size,
textAnchor="middle"))
gr = Group(String(
0,
0,
u"RMSD final (\u00C5)",
fontSize=label_font_size,
textAnchor="middle"))
gr.rotate(90)
gr.translate(
ty+lp.y+lp.height*0.5,
-(tx+lp.x-lp.width*0.15))
O.top_group.add(gr)
示例7: frozenset
# 需要导入模块: from reportlab.graphics.shapes import Group [as 别名]
# 或者: from reportlab.graphics.shapes.Group import translate [as 别名]
class Renderer:
LINK = '{http://www.w3.org/1999/xlink}href'
SVG_NS = '{http://www.w3.org/2000/svg}'
SVG_ROOT = SVG_NS + 'svg'
SVG_A = SVG_NS + 'a'
SVG_G = SVG_NS + 'g'
SVG_TITLE = SVG_NS + 'title'
SVG_DESC = SVG_NS + 'desc'
SVG_DEFS = SVG_NS + 'defs'
SVG_SYMBOL = SVG_NS + 'symbol'
SVG_USE = SVG_NS + 'use'
SVG_RECT = SVG_NS + 'rect'
SVG_CIRCLE = SVG_NS + 'circle'
SVG_ELLIPSE = SVG_NS + 'ellipse'
SVG_LINE = SVG_NS + 'line'
SVG_POLYLINE = SVG_NS + 'polyline'
SVG_POLYGON = SVG_NS + 'polygon'
SVG_PATH = SVG_NS + 'path'
SVG_TEXT = SVG_NS + 'text'
SVG_TSPAN = SVG_NS + 'tspan'
SVG_IMAGE = SVG_NS + 'image'
SVG_NODES = frozenset((
SVG_ROOT, SVG_A, SVG_G, SVG_TITLE, SVG_DESC, SVG_DEFS, SVG_SYMBOL,
SVG_USE, SVG_RECT, SVG_CIRCLE, SVG_ELLIPSE, SVG_LINE, SVG_POLYLINE,
SVG_POLYGON, SVG_PATH, SVG_TEXT, SVG_TSPAN, SVG_IMAGE
))
SKIP_NODES = frozenset((SVG_TITLE, SVG_DESC, SVG_DEFS, SVG_SYMBOL))
PATH_NODES = frozenset((SVG_RECT, SVG_CIRCLE, SVG_ELLIPSE, SVG_LINE,
SVG_POLYLINE, SVG_POLYGON, SVG_PATH, SVG_TEXT))
def __init__(self, filename):
self.filename = filename
self.level = 0
self.styles = {}
self.mainGroup = Group()
self.drawing = None
self.root = None
def render(self, node, parent=None):
if parent is None:
parent = self.mainGroup
# ignore if display = none
display = node.get('display')
if display == "none":
return
if node.tag == self.SVG_ROOT:
self.level += 1
if not self.drawing is None:
raise SVGError('drawing already created!')
self.root = node
# default styles
style = {
'color':'none',
'fill':'none',
'stroke':'none',
'font-family':'Helvetica',
'font-size':'12'
}
self.styles[self.level] = style
# iterate children
for child in node:
self.render(child, self.mainGroup)
# create drawing
width = node.get('width', '100%')
height = node.get('height', '100%')
if node.get("viewBox"):
try:
minx, miny, width, height = node.get("viewBox").split()
except ValueError:
raise SVGError("viewBox values not valid")
if width.endswith('%') and height.endswith('%'):
# handle relative size
wscale = parseLength(width) / 100.
hscale = parseLength(height) / 100.
xL,yL,xH,yH = self.mainGroup.getBounds()
self.drawing = Drawing(xH*wscale + xL, yH*hscale + yL)
else:
self.drawing = Drawing(parseLength(width), parseLength(height))
height = self.drawing.height
self.mainGroup.scale(1, -1)
self.mainGroup.translate(0, -height)
self.drawing.add(self.mainGroup)
self.level -= 1
#.........这里部分代码省略.........
示例8: TTFont
# 需要导入模块: from reportlab.graphics.shapes import Group [as 别名]
# 或者: from reportlab.graphics.shapes.Group import translate [as 别名]
from fontTools.ttLib import TTFont
from reportlab.lib import colors
path = sys.argv[1]
glyphName = sys.argv[2]
if (len(sys.argv) > 3):
imageFile = sys.argv[3]
else:
imageFile = "%s.png" % glyphName
font = TTFont(path) # it would work just as well with fontTools.t1Lib.T1Font
gs = font.getGlyphSet()
pen = ReportLabPen(gs, Path(fillColor=colors.red, strokeWidth=5))
g = gs[glyphName]
g.draw(pen)
w, h = g.width, 1000
from reportlab.graphics import renderPM
from reportlab.graphics.shapes import Group, Drawing, scale
# Everything is wrapped in a group to allow transformations.
g = Group(pen.path)
g.translate(0, 200)
g.scale(0.3, 0.3)
d = Drawing(w, h)
d.add(g)
renderPM.drawToFile(d, imageFile, fmt="PNG")