def textBoxBaseLines(self, txt, box): x, y, w, h = box attrString = txt.getNSObject() setter = CTFramesetterCreateWithAttributedString(attrString) path = CGPathCreateMutable() CGPathAddRect(path, None, CGRectMake(*box)) box = CTFramesetterCreateFrame(setter, (0, 0), path, None) ctLines = CTFrameGetLines(box) origins = CTFrameGetLineOrigins(box, (0, len(ctLines)), None) return [(x + o.x, y + o.y) for o in origins]
def buttons(self): buttons = [] xPositions = (60, 100, 320, 360) for i in xrange(0, 4): frame = CGRectMake(xPositions[i], 340, 22, 12) b = TingbotButton.alloc().initWithFrame_buttonIndex_(frame, i) buttons.append(b) return buttons
def debooklet(filename): shortName = os.path.splitext(filename)[0] outFilename = shortName + " paged.pdf" # If running python2, uncomment the following line: # filename = filename.decode('utf-8') pdfURL = NSURL.fileURLWithPath_(filename) leftPDF = PDFDocument.alloc().initWithURL_(pdfURL) rightPDF = PDFDocument.alloc().initWithURL_(pdfURL) newPDF = PDFDocument.alloc().init() if leftPDF: if not (doPageOne): leftPage = leftPDF.pageAtIndex_(0) newPDF.insertPage_atIndex_(leftPage, 0) pages = leftPDF.pageCount() startPage = int(not (doPageOne)) for p in range(startPage, pages): outPageCount = newPDF.pageCount() leftPage = leftPDF.pageAtIndex_(p) rightPage = rightPDF.pageAtIndex_(p) mediaBoxSize = leftPage.boundsForBox_(mediabox) rotation = leftPage.rotation() if (rotation == 0) or (rotation == 180): halfway = (mediaBoxSize.size.width / 2) pageHeight = mediaBoxSize.size.height leftHandCrop = CGRectMake(0, 0, halfway, pageHeight) rightHandCrop = CGRectMake(halfway, 0, halfway, pageHeight) leftPage.setBounds_forBox_(leftHandCrop, mediabox) rightPage.setBounds_forBox_(rightHandCrop, mediabox) else: halfway = (mediaBoxSize.size.height / 2) pageWidth = mediaBoxSize.size.width topCrop = CGRectMake(0, 0, pageWidth, halfway) bottomCrop = CGRectMake(0, halfway, pageWidth, halfway) leftPage.setBounds_forBox_(topCrop, mediabox) rightPage.setBounds_forBox_(bottomCrop, mediabox) newPDF.insertPage_atIndex_(leftPage, outPageCount) newPDF.insertPage_atIndex_(rightPage, outPageCount + 1) newPDF.writeToFile_(outFilename)
def trimPDF(filename): # filename = filename.decode('utf-8') shortName = os.path.splitext(filename)[0] outFilename = shortName + " TPS.pdf" pdfURL = NSURL.fileURLWithPath_(filename) pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) if pdfDoc: pages = pdfDoc.pageCount() for p in range(0, pages): page = pdfDoc.pageAtIndex_(p) mediaBoxSize = page.boundsForBox_(mediabox) trimBoxSize = CGRectMake(margins[0], margins[1], (mediaBoxSize.size.width - margins[2] - margins[0]), (mediaBoxSize.size.height - margins[3] - margins[1])) page.setBounds_forBox_(trimBoxSize, mediabox) pdfDoc.writeToFile_(outFilename)
def getTextLines(self, w, h=None, align=LEFT): """Answers the dictionary of TextLine instances. Key is y position of the line. >>> from pagebot.toolbox.units import mm, uRound >>> from drawbotcontext import DrawBotContext >>> context = DrawBotContext() >>> style = dict(font='Verdana', fontSize=pt(12)) >>> bs = context.newString('Example Text ' * 10, style=style) >>> lines = bs.getTextLines(w=200) >>> len(lines) 5 >>> line = lines[0] >>> line.maximumLineHeight 1.4em >>> """ assert w if not h: h = XXXL wpt, hpt = upt(w, h) textLines = [] attrString = self.s.getNSObject() setter = CTFramesetterCreateWithAttributedString(attrString) path = CGPathCreateMutable() CGPathAddRect(path, None, CGRectMake(0, 0, wpt, hpt)) ctBox = CTFramesetterCreateFrame(setter, (0, 0), path, None) ctLines = CTFrameGetLines(ctBox) origins = CTFrameGetLineOrigins(ctBox, (0, len(ctLines)), None) for lIndex, ctLine in enumerate(ctLines): origin = origins[lIndex] textLine = TextLine(ctLine, pt(origin.x), pt(origin.y), lIndex) textLines.append(textLine) return textLines
context.newPage(W, H) x = 0 y = 0 context.text('(%s, %s)' % (x, y), (x, y)) x = W / 2 y = H - hbox context.text('(%s, %s)' % (x, y), (x, y)) b = blurb.getBlurb('stylewars_documentary')[:200] font = findFont('Bungee-Regular') style = dict(font=font, fontSize=pt(18), textFill=color(0.5, 1, 0)) bs = context.newString(b, style=style) lines = bs.getTextLines(w=wbox, h=hbox) attrString = bs.s.getNSObject() setter = CTFramesetterCreateWithAttributedString(attrString) path = CGPathCreateMutable() CGPathAddRect(path, None, CGRectMake(0, 0, wbox, hbox)) ctBox = CTFramesetterCreateFrame(setter, (0, 0), path, None) ctLines = CTFrameGetLines(ctBox) origins = CTFrameGetLineOrigins(ctBox, (0, len(ctLines)), None) context.fill(None) context.stroke(f) context.rect(PADDING, H, wbox, -hbox) context.fill(f) for p in origins: context.circle(PADDING + p.x + 2, (p.y + 2), 4) print(p.y) y = H - LINE for line in lines: