混合CTRunDraw和drawAtPoint

时间:2016-07-28 15:40:19

标签: python objective-c pdf core-text pyobjc

我编写了一个模块,它提供了几种渲染文本和创建PDF文件的方法。现在我发现使用Core Text和更高级别的文本呈现会混淆Core Text布局的大小。后者主要显示得太大了。

方案:

  • 创建PDF上下文
  • 开始页面
  • 通过CTRunDraw(B)*
  • 呈现文字
  • 结束页面
  • 关闭PDF背景
  • 保存PDF

*)按预期方式正确呈现。 但只要我之前没有使用NSAttributedStringdrawAtPoint_(A)绘制任何文字。

这意味着,这两种方法都可以完美地运行,但核心文本绘图在它们聚集在一起时会变得太大。

保存和恢复图形上下文时是否需要注意什么?我几乎尝试了什么。我可以在这里添加一些代码,但是它很复杂,并且在复杂的设置中,所以如果您可以指定您想要查看的内容,请告诉我。

(A)高级文字图纸:

class Text(object):

    def __init__(self, text="", font="Monaco", fontSize=12, position=None ):
        self.text = text
        self.font = font
        self.fontSize = fontSize
        self.position = position
        self.fontRGBColor = 0, 1, 1, 1

    def drawText(self):
        textAttributes = {}
        textAttributes[NSFontAttributeName] = NSFont.fontWithName_size_( self.font, self.fontSize )
        textAttributes[NSForegroundColorAttributeName] = NSColor.colorWithCalibratedRed_green_blue_alpha_( *self.fontRGBColor )
        textAttributedString = NSAttributedString.alloc().initWithString_attributes_( self.text, textAttributes )
        textAttributedString.drawAtPoint_((self.position[0], self.position[1]))

(B)核心文字方法:

class TextBox(object):
    def __init__(self):
        self.font = "Monaco"
        self.fontSize = 12
        self.text = "This is my Text"
        self.fontColor = 0, 1, 1, 0, 0.75

        self.pdfContext = NSGraphicsContext.currentContext().graphicsPort()


    def drawTextBox(self):
        CGContextSaveGState(self.pdfContext)

        myBounds = (0, 0), (100, 100)
        posX, posY = myBounds[0]
        width, height = myBounds[1]

        self.fontObject = NSFont.fontWithName_size_(self.font, self.fontSize)
        self.fontAttributes = {}
        self.fontAttributes[NSFontAttributeName] = self.fontObject
        self.fontAttributes[NSForegroundColorAttributeName] = NSColor.colorWithDeviceCyan_magenta_yellow_black_alpha_(*self.fontColor)

        TextBoxWithFeaturesAttributedString = NSMutableAttributedString.alloc().initWithString_attributes_(self.text, self.fontAttributes)
        setter = CTFramesetterCreateWithAttributedString(TextBoxWithFeaturesAttributedString)
        path = CGPathCreateMutable()

        CGPathAddRect(path, None, CGRectMake(0, 0, width, height))
        box = CTFramesetterCreateFrame(setter, (0, 0), path, None)
        ctLines = CTFrameGetLines(box)
        origins = CTFrameGetLineOrigins(box, (0, len(ctLines)), None)

        for i, (originX, originY) in enumerate(origins):
            ctLine = ctLines[i]
            bounds = CTLineGetImageBounds(ctLine, self.pdfContext)
            if bounds.size.width == 0:
                continue
            ctRuns = CTLineGetGlyphRuns(ctLine)
            for ctRun in ctRuns:
                CGContextSetTextPosition(self.pdfContext, posX + originX, posY + originY)
                CTRunDraw(ctRun, self.pdfContext, (0, 0))

        CGContextRestoreGState(self.pdfContext)

提前多多感谢和赞赏。

0 个答案:

没有答案
相关问题