class TextLine(object): def __init__(self, ctLine, p, lineIndex): self._ctLine = ctLine self.x, self.y = p # Relative position from top of TextBox self.lineIndex = lineIndex # Vertical line index in TextBox. self.glyphCount = CoreText.CTLineGetGlyphCount(ctLine) self.string = '' self.runs = [] #print ctLine for runIndex, ctRun in enumerate(CoreText.CTLineGetGlyphRuns(ctLine)): textRun = TextRun(ctRun, runIndex) self.runs.append(textRun) self.string += textRun.string def __repr__(self): return '[TextLine #%d Glyphs:%d Runs:%d]' % ( self.lineIndex, self.glyphCount, len(self.runs)) def __len__(self): return self.glyphCount def __getitem__(self, index): return self.runs[index] def getIndexForPosition(self, (x, y)): return CoreText.CTLineGetStringIndexForPosition( self._ctLine, CoreText.CGPoint(x, y))[0]
def getIndexForPosition(self, xy): x, y = xy return CoreText.CTLineGetStringIndexForPosition( self._ctLine, CoreText.CGPoint(x, y))[0]
s.append(part[4:]) self.string = ''.join(s) self.runs = [] for ctRun in CoreText.CTLineGetGlyphRuns(ctLine): self.runs.append(TextRun(ctRun, self.string)) def __repr__(self): print self._ctLine def __len__(self): return self.glyphCount def getIndexForPosition(self, (x, y)): return CoreText.CTLineGetStringIndexForPosition( self._ctLine, CoreText.CGPoint(x, y)) def getOffsetForStringIndex(self, i): return CoreText.CTLineGetOffsetForStringIndex(self._ctLine, i, None) def _get_stringIndex(self): return CoreText.CTLineGetStringRange(self._ctLine).location stringIndex = property(_get_stringIndex) #def _get_alignment(self): # return CoreText.CTTextAlignment(self._ctLine) #alignment = property(_get_alignment) def _get_imageBounds(self): (x, y), (w, h) = CoreText.CTLineGetImageBounds(self._ctLine, None)
def testFunctions(self): v = CoreText.CTLineGetTypeID() self.assertIsInstance(v, int) self.assertResultIsCFRetained( CoreText.CTLineCreateWithAttributedString) astr = CoreText.CFAttributedStringCreate(None, "-", None) self.assertTrue(astr is not None) token = CoreText.CTLineCreateWithAttributedString(astr) self.assertIsInstance(token, CoreText.CTLineRef) astr = CoreText.CFAttributedStringCreate(None, "hello world", None) self.assertTrue(astr is not None) line = CoreText.CTLineCreateWithAttributedString(astr) self.assertIsInstance(line, CoreText.CTLineRef) self.assertResultIsCFRetained(CoreText.CTLineCreateTruncatedLine) v = CoreText.CTLineCreateTruncatedLine(line, 20.0, CoreText.kCTLineTruncationStart, token) self.assertIsInstance(v, CoreText.CTLineRef) self.assertResultIsCFRetained(CoreText.CTLineCreateJustifiedLine) v = CoreText.CTLineCreateJustifiedLine(line, 2.0, 123.0) self.assertIsInstance(v, CoreText.CTLineRef) v = CoreText.CTLineGetGlyphCount(line) self.assertIsInstance(v, int) v = CoreText.CTLineGetGlyphRuns(line) self.assertIsInstance(v, CoreText.CFArrayRef) v = CoreText.CTLineGetStringRange(line) self.assertIsInstance(v, CoreText.CFRange) v = CoreText.CTLineGetPenOffsetForFlush(line, 0.5, 40.0) self.assertIsInstance(v, float) url = CoreText.CFURLCreateWithFileSystemPath( None, "/tmp/pyobjc.test.pdf", CoreText.kCFURLPOSIXPathStyle, False) self.assertIsInstance(url, CoreText.CFURLRef) ctx = CoreText.CGPDFContextCreateWithURL( url, Quartz.CGRect(Quartz.CGPoint(0, 0), Quartz.CGSize(1000, 1000)), None) self.assertIsInstance(ctx, Quartz.CGContextRef) CoreText.CTLineDraw(line, ctx) v = CoreText.CTLineGetImageBounds(line, ctx) self.assertIsInstance(v, CoreText.CGRect) self.assertArgIsOut(CoreText.CTLineGetTypographicBounds, 1) self.assertArgIsOut(CoreText.CTLineGetTypographicBounds, 2) self.assertArgIsOut(CoreText.CTLineGetTypographicBounds, 3) v = CoreText.CTLineGetTypographicBounds(line, None, None, None) self.assertIsInstance(v, tuple) self.assertIsInstance(v[0], float) self.assertIsInstance(v[1], float) self.assertIsInstance(v[2], float) self.assertIsInstance(v[3], float) v = CoreText.CTLineGetTrailingWhitespaceWidth(line) self.assertIsInstance(v, float) v = CoreText.CTLineGetStringIndexForPosition(line, CoreText.CGPoint(10, 10)) self.assertIsInstance(v, int) self.assertArgIsOut(CoreText.CTLineGetOffsetForStringIndex, 2) v = CoreText.CTLineGetOffsetForStringIndex(line, 2, None) self.assertIsInstance(v, tuple) self.assertIsInstance(v[0], float) self.assertIsInstance(v[1], float) if os.path.exists("/tmp/pyobjc.test.pdf"): os.unlink("/tmp/pyobjc.test.pdf")
s.append(unichr(int(part[0:4], 16))) s.append(part[4:]) self.string = ''.join(s) self.runs = [] for ctRun in CoreText.CTLineGetGlyphRuns(ctLine): self.runs.append(TextRun(ctRun, self.string)) def __repr__(self): print(self._ctLine) def __len__(self): return self.glyphCount def getIndexForPosition(self, (x, y)): return CoreText.CTLineGetStringIndexForPosition(self._ctLine, CoreText.CGPoint(x, y)) def getOffsetForStringIndex(self, i): return CoreText.CTLineGetOffsetForStringIndex(self._ctLine, i, None) def _get_stringIndex(self): return CoreText.CTLineGetStringRange(self._ctLine).location stringIndex = property(_get_stringIndex) #def _get_alignment(self): # return CoreText.CTTextAlignment(self._ctLine) #alignment = property(_get_alignment) def _get_imageBounds(self): (x, y), (w, h) = CoreText.CTLineGetImageBounds(self._ctLine,None) return x, y, w, h