def alignToptoAscenderFunction(paths): font = CurrentFont() mm = minmaxPaths(paths) paths = [ path.applyPlus(TFSPoint(0, font.info.ascender - mm.maxY)) for path in paths ] return paths
def centerBelowAscenderFunction(paths): font = CurrentFont() mm = minmaxPaths(paths) paths = [ path.applyPlus( TFSPoint(0, (font.info.ascender - (mm.maxY - mm.minY)) * 0.5)) for path in paths ] return paths
def processFont(self, filepath): # extension = filepath[filepath.rindex('.'):] ftFont = None print print 'filepath', filepath leftSideBearings = [] rightSideBearings = [] try: ftFont = TFFreetypeFont(filepath) postscript_name = ftFont.postscriptName if postscript_name in self.processedPostscriptNames: return glyphIndexToCharacterMap = ftFont.getGlyphIndexToCharacterMap() # print '\t', 'postscript_name', postscript_name self.processedPostscriptNames.add(postscript_name) # characterToGlyphIndexMap = ftFont.getCharacterToGlyphIndexMap() for glyphIndex in ftFont.getGlyphIndices(): if glyphIndex not in glyphIndexToCharacterMap: continue unicode = glyphIndexToCharacterMap[glyphIndex] if unicode > 0x7f: ''' ignore code points not in first code block. ''' continue contours, xAdvance = ftFont.getGlyphContours(glyphIndex) if len(contours) < 1: ''' Ignore empty glyphs. ''' continue minmax = minmaxPaths(contours) leftSideBearing = minmax.minX rightSideBearing = xAdvance - minmax.maxX leftSideBearings.append(leftSideBearing) rightSideBearings.append(rightSideBearing) except Exception, e: print 'error filepath', filepath print e.message traceback.print_exc() del ftFont return
def renderSvgScene(self, filenamePrefix, pathTuples, hGuidelines=None): from tfs.common.TFSSvg import TFSSvg, TFSSvgPath filename = '%s.svg' % ( filenamePrefix, ) dstFile = os.path.abspath(os.path.join(self.svg_folder, filename)) CANVAS_BACKGROUND_COLOR = 0xffffffff CANVAS_BORDER_COLOR = 0x07fbfbfbf fiSvg = TFSSvg().withBackground(CANVAS_BACKGROUND_COLOR).withBorder(CANVAS_BORDER_COLOR) # if pathTuples: for color, contours in pathTuples: self.subrenderGlyphContours(fiSvg, contours, color) if hGuidelines: for hGuideline in hGuidelines: p0 = TFSPoint(hGuideline, self.fifont.info.descender) p1 = TFSPoint(hGuideline, self.fifont.info.ascender) GUIDELINE_COLOR = 0x7fdfdfdf fiSvg.addItem(TFSSvgPath(openPathWithPoints(p0, p1)).addStroke(GUIDELINE_COLOR, 1)) vGuidelines = ( 0, self.fifont.info.ascender, self.fifont.info.descender, ) if vGuidelines: minmax = None for color, contours in pathTuples: minmax = minmaxMerge(minmax, minmaxPaths(contours)) for vGuideline in vGuidelines: p0 = TFSPoint(0, vGuideline) p1 = TFSPoint(minmax.maxX, vGuideline) GUIDELINE_COLOR = 0x7fdfdfdf fiSvg.addItem(TFSSvgPath(openPathWithPoints(p0, p1)).addStroke(GUIDELINE_COLOR, 1)) SVG_HEIGHT = 400 SVG_MAX_WIDTH = 800 fiSvg.renderToFile(dstFile, margin=10, height=SVG_HEIGHT, maxWidth=SVG_MAX_WIDTH) return filename
def appendDiacriticalContours(self, alignment, baseContours, diacriticalContours, baseCodePoint, diacriticalCodePoint): baseMinmax = minmaxPaths(baseContours) diacriticalMinmax = minmaxPaths(diacriticalContours) def minmaxHCenter(minmax): return (minmax.minX) + ((minmax.maxX - minmax.minX) * 0.5) def minmaxVCenter(minmax): return (minmax.minY) + ((minmax.maxY - minmax.minY) * 0.5) def minmaxTop(minmax): return minmax.maxY def minmaxBottom(minmax): return minmax.minY def getOffsetX(codePoint, joinCentersMap, defaultValue): if codePoint in joinCentersMap: offset = joinCentersMap[codePoint] return offset.x else: return defaultValue def getOffsetY(codePoint, joinCentersMap, defaultValue): if codePoint in joinCentersMap: offset = joinCentersMap[codePoint] if offset.y is None: return defaultValue return offset.y else: return defaultValue if alignment == TFSCompoundsList.DIACRITICAL_ALIGN_TOP_ROTATE_FLOAT: diacriticalContours = [contour.applyScale(-1.0) for contour in diacriticalContours] diacriticalMinmax = minmaxPaths(diacriticalContours) alignment = TFSCompoundsList.DIACRITICAL_ALIGN_TOP elif alignment == TFSCompoundsList.DIACRITICAL_ALIGN_TAIL_H_FLIP: diacriticalContours = [contour.applyScaleXY(-1.0, 1.0) for contour in diacriticalContours] diacriticalMinmax = minmaxPaths(diacriticalContours) alignment = TFSCompoundsList.DIACRITICAL_ALIGN_TAIL if alignment == TFSCompoundsList.DIACRITICAL_ALIGN_TOP: offsetX = (getOffsetX(baseCodePoint, self.topJoinCentersMap, minmaxHCenter(baseMinmax)) - getOffsetX(diacriticalCodePoint, self.topJoinCentersMap, minmaxHCenter(diacriticalMinmax))) offsetY = (self.default_diacritical_distance + getOffsetY(baseCodePoint, self.topJoinCentersMap, minmaxTop(baseMinmax)) - getOffsetY(diacriticalCodePoint, self.topJoinCentersMap, minmaxBottom(diacriticalMinmax))) elif alignment == TFSCompoundsList.DIACRITICAL_ALIGN_TAIL: offsetX = (getOffsetX(baseCodePoint, self.tailJoinCentersMap, minmaxHCenter(baseMinmax)) - getOffsetX(diacriticalCodePoint, self.tailJoinCentersMap, minmaxHCenter(diacriticalMinmax))) offsetY = (getOffsetY(baseCodePoint, self.tailJoinCentersMap, minmaxBottom(baseMinmax)) - getOffsetY(diacriticalCodePoint, self.tailJoinCentersMap, minmaxTop(diacriticalMinmax))) elif alignment == TFSCompoundsList.DIACRITICAL_ALIGN_TAIL_FLOAT: offsetX = (getOffsetX(baseCodePoint, self.tailJoinCentersMap, minmaxHCenter(baseMinmax)) - getOffsetX(diacriticalCodePoint, self.tailJoinCentersMap, minmaxHCenter(diacriticalMinmax))) offsetY = (getOffsetY(baseCodePoint, self.tailJoinCentersMap, minmaxBottom(baseMinmax)) - getOffsetY(diacriticalCodePoint, self.tailJoinCentersMap, minmaxTop(diacriticalMinmax))) - self.default_diacritical_distance elif alignment == TFSCompoundsList.DIACRITICAL_ALIGN_MIDDLE: offsetX = (getOffsetX(baseCodePoint, self.middleJoinCentersMap, minmaxHCenter(baseMinmax)) - getOffsetX(diacriticalCodePoint, self.middleJoinCentersMap, minmaxHCenter(diacriticalMinmax))) offsetY = (getOffsetY(baseCodePoint, self.middleJoinCentersMap, minmaxVCenter(baseMinmax)) - getOffsetY(diacriticalCodePoint, self.middleJoinCentersMap, minmaxVCenter(diacriticalMinmax))) else: raise Exception('Unknown diacritical aligment: ' + alignment) offsetX = round(offsetX) offsetY = round(offsetY) diacriticalContours = [contour.applyPlus(TFSPoint(offsetX, offsetY)) for contour in diacriticalContours] return baseContours + diacriticalContours
def alignToptoAscenderFunction(paths): font = CurrentFont() mm = minmaxPaths(paths) paths = [path.applyPlus(TFSPoint(0, font.info.ascender - mm.maxY)) for path in paths] return paths
def alignBottomToBaselineFunction(paths): font = CurrentFont() mm = minmaxPaths(paths) paths = [path.applyPlus(TFSPoint(0, -mm.minY)) for path in paths] return paths
def centerBelowAscenderFunction(paths): font = CurrentFont() mm = minmaxPaths(paths) paths = [path.applyPlus(TFSPoint(0, (font.info.ascender - (mm.maxY - mm.minY)) * 0.5)) for path in paths] return paths
def alignLeftFunction(paths): mm = minmaxPaths(paths) paths = [path.applyPlus(TFSPoint(-mm.minX, 0)) for path in paths] return paths