在这个例子中,我在沿 X 坐标移动的同时,在 Y 坐标中距离原点 30px 处绘制一些线元素。这是在记录表面上绘制的,因为在我的真实代码中,在开始之前我不知道扩展。绘制完成后,我在 PDFSurface 上重复绘制,但由于我希望我的 PDF 和 PNG 与我绘制的元素齐平,我将 PDFSurface 的边界设置为 RecordingSurface 的 ink_extents 的高度和宽度。接下来因为我在下方绘制了 30 像素,上面只有空白空间,所以我将我的绘图转换为 set_source_surface -30 像素。现在,在编写 PNG 时一切正常,但 PDF 为空。如果我手动将高度扩展到 100px,则 PDF 会以正确的原点正确绘制。如果我从 Y 0px 而不是 Y 30px 开始绘图,它也可以工作。我错过了什么还是一个错误?
谢谢你的帮助。这是一个测试代码:
import cairocffi as cairo
rs = cairo.RecordingSurface(cairo.CONTENT_COLOR_ALPHA, None)
ctxRS = cairo.Context(rs)
items = ([0], [28, 29], [39], [42], [64], [67], [70, 71, 72], [76, 77], [79, 80], [86], [90], [104, 105],
[131], [134, 135, 136, 137, 138, 139], [145, 146, 147], [150, 151], [156], [174], [188], [191],
[199, 200], [203], [212], [216], [229, 230], [240, 241, 242, 243], [262])
for item in items:
initialPosition = item[0]
numberOfItems = len(item)
offset = initialPosition * 10
lineLength = 10 * numberOfItems
ctxRS.set_source_rgba(1.0, 0.0, 0.0, 1.0)
ctxRS.set_line_width(0)
ctxRS.move_to(offset, 30)
ctxRS.line_to(offset, 40)
ctxRS.line_to(offset + lineLength, 40)
ctxRS.line_to(offset + lineLength, 30)
ctxRS.close_path()
ctxRS.stroke_preserve()
ctxRS.set_source_rgba(1.0, 0.0, 0.0, 1.0)
ctxRS.fill()
x, y, width, height = rs.ink_extents()
ps = cairo.PDFSurface("cairoTest.pdf", width, height)
ctxPS = cairo.Context(ps)
ctxPS.set_source_surface(rs, -x, -y)
ctxPS.paint()
ps.write_to_png("cairoTest.png")
ps.finish()