How to use the afdko.pdflib.pdfmetrics.stringwidth function in afdko

To help you get started, we’ve selected a few afdko examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github adobe-type-tools / afdko / python / afdko / pdflib / fontpdf.py View on Github external
groupIndex = 0
	fontInfo.setEncoding(fontInfo, params.rt_glyphList[:256])
	rt_canvas.addFont(embeddedFontPSName, fontInfo.encoding, fontInfo, getFontDescriptorItems, getEncodingInfo)

	# figure out layout.
	pageWidth = float(params.pageSize[0] - (params.pageRightMargin + params.pageLeftMargin))
	pageHeight = float(yTop - params.pageBottomMargin)
	maxSize = maxLabelSize = max(waterfallRange)
	# Figure out y-height of waterall, and write pt size headers.
	cur_y = yTop
	pageTitleFont = params.pageTitleFont
	pageTitleSize = params.pageTitleSize
	if maxLabelSize > pageTitleSize:
		maxLabelSize = pageTitleSize
	ptSizeTemplate = "%s pts   "
	ptWidth = pdfmetrics.stringwidth(ptSizeTemplate % ("00"), pageTitleFont)*0.001*maxLabelSize

	waterfallHeight = 0 # page height of one water fall  one row per pt size.
	for gSize in waterfallRange:
		waterfallHeight += gSize*1.2
		cur_y -= gSize*1.2
		labelSize = gSize
		if labelSize > pageTitleSize:
			labelSize = pageTitleSize
		rt_canvas.setFont(pageTitleFont, labelSize)
		rt_canvas.drawString(params.pageLeftMargin, cur_y, ptSizeTemplate % gSize)
	cur_y = yTop


	# Build the list of strings, based on their widths.
	# If the glyph string is too long to font on one line, divide it up into
	# a list of glyphs strings, each of which do fit on one line.
github adobe-type-tools / afdko / python / afdko / pdflib / fontpdf.py View on Github external
def setDefaultHPadding(params, init = None):
	 # Need to allow extra space for the horizontal and vertical hint labels
	if init:
		params.rt_glyphHPadding = init
	else:
		params.rt_glyphHPadding = params.glyphHPadding

	if  params.drawGlyph_HHints:
		params.rt_maxHintLabelWidth = pdfmetrics.stringwidth("<999.0, 999.0> 999.0", params.pointLabelFont) * 0.001 *  params.pointLabelSize
		params.rt_glyphHPadding += params.hhintXOffset + params.rt_maxHintLabelWidth
github adobe-type-tools / afdko / python / afdko / pdflib / fontpdf.py View on Github external
def writeErrorList(self, errorList, params):
		# At this point, I expect ErrorList to be a list of short strings.
		rt_canvas = params.rt_canvas
		ptSize = params.errorLogPointSize
		rt_canvas.setFont( params.errogLogFont,  ptSize)
		posX = 0
		posY = self.cur_y - ptSize

		# calculate column wiidth
		listLen = map(len, errorList)
		maxLen = max(listLen)
		maxString = errorList[listLen.index(maxLen)]
		columnWidth = pdfmetrics.stringwidth(maxString, params.errogLogFont) * 0.001 * ptSize
		columnAdvance = columnWidth +  params.errorLogColumnSpacing
		for textString in errorList:
			if  ((posY - 2*ptSize) < 0) and ((posX + (columnAdvance + columnWidth)) > kGlyphSquare):
				rt_canvas.drawString(posX, posY, textString + "...")
				break # We filled up the available display area.
			else:
				rt_canvas.drawString(posX, posY, textString)
			posY -= ptSize
			if (posY - ptSize) < 0: # I allow a margin of one line at the bottom of the meta area.
				posY = self.cur_y - ptSize
				posX += columnAdvance
github adobe-type-tools / afdko / python / afdko / pdflib / fontpdf.py View on Github external
# Note that I need to invert the matrix. Thi s is because the overall page grid +y is down;
		# when drawing the glyph I invert this, so text will come out upside down; hence I need to invert again to draw text.
		pointLabelColorRGB = params.pointLabelColorRGB
		rt_canvas = params.rt_canvas

		if pointPDF == path[0]:
			if path[-1].pt0 == pointPDF.pt0:
				return # Don't draw label for first point in path if last point s coincident with it.
		text = ""
		if params.pointLabel_doPointType:
			text += pointPDF.type
		if params.pointLabel_doPointIndex :
			text += " %s" % (pointPDF.index)
		if params.pointLabel_doPosition:
			text += " (%s, %s) " % (pointPDF.pt0[0],  pointPDF.pt0[1])
		textWidth = pdfmetrics.stringwidth(text, params.pointLabelFont) * 0.001 * params.pointLabelSize

		p1 = pointPDF.pt0

		# Get the incoming vector. If this poin tis a cureve-to, this is bcp2, else it is the position of the previous point.
		if pointPDF.bcp2:
			p0 = pointPDF.bcp2
		else:
			p0 = pointPDF.last.pt0
			if p1 == p0: # final point was coincdent with current
				if pointPDF.last.bcp2:
					p0 = pointPDF.last.bcp2
				else:
					p0 = pointPDF.last.last.pt0

		if  pointPDF.next.bcp1:
			p2 = pointPDF.next.bcp1
github adobe-type-tools / afdko / python / afdko / pdflib / fontpdf.py View on Github external
continue
			if prevHint and (prevHint[1] >= h1):
				hintColorOverlapRGB = params.hintColorOverlapRGB
				rt_canvas.setStrokeColorRGB(hintColorOverlapRGB[0], hintColorOverlapRGB[1], hintColorOverlapRGB[2])
				rt_canvas.setFillColorRGB(hintColorOverlapRGB[0] *0.6, hintColorOverlapRGB[1] *0.6, hintColorOverlapRGB[2] *0.6)
				xOffset += 2 # so the rects of different colors don't exactly overlap
			else:
				hintColorRGB = params.hintColorRGB
				rt_canvas.setStrokeColorRGB(hintColorRGB[0], hintColorRGB[1], hintColorRGB[2])
				rt_canvas.setFillColorRGB(hintColorRGB[0] *0.6, hintColorRGB[1] *0.6, hintColorRGB[2] *0.6)
			x0 = -xOffset
			x1 = kGlyphSquare+xOffset
			if (i%2) == 1:
				posX = x1
			else:
				posX = x0  - pdfmetrics.stringwidth(hintString1, params.pointLabelFont) * 0.001 * ptSize
			rt_canvas.setLineWidth(params.markLineWidth)
			rt_canvas.rect(x0, h1, x1-x0, width)
			posY = h1 + 0.2* ptSize
			rt_canvas.drawString(posX, posY , hintString1)
			if hintString2:
				posY = posY + 1.2*ptSize
				rt_canvas.drawString(posX, posY, hintString2)
			prevHint = (h1, h2)
		rt_canvas.restoreState()
github adobe-type-tools / afdko / python / afdko / pdflib / fontpdf.py View on Github external
# figure out how much space to leave at start of line for PS names and fond index fields.
	psNameSize = params.fontsetGroupPtSize
	pageTitleFont = params.pageTitleFont
	maxLen = 0
	maxPSName = None
	for entry in pdfFontList:
		psName = entry[1].getPSName()
		psNameLen = len(psName)
		if psNameLen > maxLen:
			maxLen = psNameLen
			maxPSName = psName
	indexString = "%s" % (len(pdfFontList))
	pageTitleFont
	psNameAndIndex = maxPSName + " " + indexString
	psNameWidth = pdfmetrics.stringwidth(psNameAndIndex, pageTitleFont)  * 0.001 * psNameSize
	indexWidth = pdfmetrics.stringwidth(indexString, pageTitleFont)  * 0.001 * psNameSize
	# glyph width equivalents of ps name.
	kPSNameFieldWidth = int( 1 + (psNameWidth/params.userPtSize)) # The space to leave at the begining of a line for the PS name, in em-spaces.
	kFontIndexWidth = int( 1 + (indexWidth/params.userPtSize))  # The space to leave at the begining of every line for the font index, in em-spaces.

	# get layout parameters
	scaledxAdvance = params.userPtSize
	scaledYAdvance = int(params.userPtSize*1.2)
	scaledYAdvanceGroupTitle = int(psNameSize*1.2)
	yTop = getTitleHeight(params)
	pageWidth = float(params.pageSize[0] - (params.pageRightMargin + params.pageLeftMargin))
	pageHeight = float(yTop - params.pageBottomMargin)

	numAcross = int(pageWidth/params.userPtSize) - kFontIndexWidth # -kFontIndexWidth em-spaces is to allow for an initial font index in the line.
	linesPerPage = int(pageHeight/scaledYAdvance)
	# collect max number of glyph names in a font. Allow for different charsets in the fonts.
	maxNames = 0
github adobe-type-tools / afdko / python / afdko / pdflib / pdfgen.py View on Github external
def stringWidth(self, text, fontname, fontsize):
        "gets width of a string in the given font and size"
        return pdfmetrics.stringwidth(text, fontname) * 0.001 * fontsize
github adobe-type-tools / afdko / python / afdko / pdflib / fontpdf.py View on Github external
if p1 == p0: # final point was coincdent with current
					if pointPDF.last.bcp2:
						p0 = pointPDF.last.bcp2
					else:
						p0 = pointPDF.last.last.pt0

			if  pointPDF.next.bcp1:
				p2 = pointPDF.next.bcp1
			else:
				p2 = pointPDF.next.pt0
				startPt = path[0]
			tickSize = params.pointLabel_LineLength
			rt_canvas.setLineWidth(params.markLineWidth)
			tickPos = getTickPos(p2, p1, p0, tickSize, self.isTT)
			text = "P %s" % (pathIndex,)
			textWidth = pdfmetrics.stringwidth(text, params.pointLabelFont) * 0.001 * params.pointLabelSize
			textPos = [tickPos[0], tickPos[1]]
			if textPos[0] <  p1[0]:
				textPos[0]  = textPos[0] - textWidth
			if textPos[1] <  p1[1]:
				textPos[1]   = textPos[1] - tickSize
			rt_canvas.line(p1[0], p1[1], tickPos[0], tickPos[1])
			rt_canvas.saveState()
			rt_canvas.setFillColorRGB(contourLabelColorRGB[0], contourLabelColorRGB[1], contourLabelColorRGB[2])
			rt_canvas.setStrokeColorRGB(contourLabelColorRGB[0], contourLabelColorRGB[1], contourLabelColorRGB[2])
			rt_canvas.setFont( params.pointLabelFont, tickSize)
			rt_canvas.drawString( textPos[0], textPos[1], text)
			rt_canvas.restoreState()
			pathIndex += 1
		rt_canvas.restoreState()