def extractComposites(glyph): """Return a new glyph with outline copies of each composite from the source glyph.""" decomposedComposites = RGlyph() if len(glyph.components): font = glyph.getParent() for comp in reversed(glyph.components): # obtain source data baseGlyphName = comp.baseGlyph baseGlyph = font[baseGlyphName] t = transform.Transform(*comp.transformation) # create a temporary glyph on which to draw the decomposed composite single_decomposedComposite = RGlyph() decompPen = single_decomposedComposite.getPen() baseGlyph.draw(decompPen) single_decomposedComposite.transform(t) # add single composite to the returned glyph decomposedComposites.appendGlyph(single_decomposedComposite) return decomposedComposites
def _decompose(glyph): components = glyph.components font = glyph.getParent() decomposedGlyph = RGlyph() if font is not None: for component in components: decomponent = RGlyph() decomponent.appendGlyph(font[component.baseGlyph]) decomponent.scale((component.scale[0], component.scale[1])) decomponent.move((component.offset[0], component.offset[1])) decomposedGlyph.appendGlyph(decomponent) for contour in glyph.contours: decomposedGlyph.appendContour(contour) decomposedGlyph.width = glyph.width return decomposedGlyph
def __call__(self, glyph, font, **arguments): try: filteredGlyph = RGlyph() filteredGlyph.width = glyph.width drawingPen = filteredGlyph.getPen() filterPen = self.filterObject(drawingPen, **arguments) glyph.draw(filterPen) except: try: sourceGlyph = RGlyph() sourceGlyph.appendGlyph(glyph) sourceGlyph.width = glyph.width filteredGlyph = self.filterObject(sourceGlyph, **arguments) except: filteredGlyph = ErrorGlyph() return filteredGlyph
def set(self, glyphInput, keepWords=False): thisFont = self.thisFont kerning = self.kerning fontKeys = thisFont.keys() if isinstance(glyphInput, str): glyphRecord = self.stringToGlyphs(glyphInput) elif isinstance(glyphInput, list): glyphRecord = glyphInput suffixes = self.settings['suffix'] if len(suffixes) > 0: for i, glyphName in enumerate(glyphRecord): for suffix in suffixes: if glyphName + suffix in fontKeys: glyphRecord[i] = glyphName + suffix kernGroups = self.getKernGroups(glyphRecord) nbrOfGlyphs = len(glyphRecord) UPM = thisFont.info.unitsPerEm descender = thisFont.info.descender ascender = thisFont.info.ascender capHeight = thisFont.info.capHeight xHeight = thisFont.info.xHeight pointSize = self.type['size'] _line_Height = self.type['_line_Height'] tracking = self.type['tracking'] color = self.type['color'][0:] alpha = self.type['alpha'] x, y, width, height = self.canvas['posSize'] sc = pointSize/UPM xAdvance = yAdvance = 0 kerningValue = 0 # exceed allowance: to which extent can a word go beyond bounds eA = 0.97 wordLetterOffset = 0 spaceWidth = thisFont['space'].width glyphGroups = [] previousGlyph = None previousGlyphGroups = [] wordGlyph = RGlyph() wordGlyph.name = '' glyphDrawingTime = 0 wordKerning = [] useKerning = self.settings['useKerning'] showKerning = self.settings['showKerning'] showGlyphBox = self.settings['showGlyphBox'] showFrame = self.settings['showFrame'] pageStamp = self.settings['pageStamp'] newPage(*self.formats[self.preset]) if showFrame: self.showFrame() if pageStamp: self.pageStamp() # compensate for upward y coordinates xPos = x yPos = y + height - ((UPM+descender)*sc) translate(xPos, yPos) for i, glyphName in enumerate(glyphRecord): # filtering missing glyphs if (glyphName not in fontKeys) and (glyphName != '\n'): if '.notdef' in fontKeys: glyphName = '.notdef' else: continue # skip spaces at the start of a new line if (glyphName == 'space') and (xAdvance == 0) and (wordLetterOffset == 0): continue if glyphName != '\n': glyph = thisFont[glyphName] # check for need of a new line if ((glyphName == '\n') and (not keepWords)) or \ (xAdvance + (glyph.width*sc*eA) >= width): xAdvance = 0 kerningValue = 0 yAdvance += (UPM*_line_Height)*sc _newLine = False previousGlyph = None previousGlyphGroups = None if (glyphName == '\n'): continue # kerning if glyphName in kernGroups.keys(): glyphGroups = kernGroups[glyphName] # filter out non kern groups glyphGroups = [group for group in glyphGroups if ('MMK' in group) or ('kern' in group)] if (previousGlyph is not None) and useKerning: for group in glyphGroups: for prevGroup in previousGlyphGroups: if kerning[(prevGroup,group)] is not None: kerningValue = kerning[(prevGroup,group)] glyphStart = time() # if word wrap if keepWords: # add each glyph of a word to the wordGlyph and skip the drawing # add metrics of each glyph to the wordGlyph if (glyphName != 'space') and (glyphName != '\n') and (i < nbrOfGlyphs-1): # draw kerning if word wrap if showKerning and (kerningValue != 0): save() scale(sc) fill(1, 0, 0, 0.5) thisKern = BezierPath() thisKern.moveTo((wordLetterOffset, descender)) thisKern.lineTo((wordLetterOffset, ascender)) thisKern.lineTo((wordLetterOffset + kerningValue, ascender)) thisKern.lineTo((wordLetterOffset + kerningValue, descender)) thisKern.closePath() wordKerning.append(thisKern) restore() wordGlyph.appendGlyph(glyph, (wordLetterOffset + kerningValue, 0)) wordGlyph.width += (glyph.width + tracking + kerningValue) wordGlyph.name += glyphName wordLetterOffset += (glyph.width + tracking + kerningValue) previousGlyph = glyph previousGlyphGroups = glyphGroups kerningValue = 0 continue # when a space is hit, or if it’s the end of the text # the wordglyph is passed as main glyph to be drawn # check if the word doesn’t exceed boundaries elif (glyphName == 'space') or (glyphName == '\n') or (i == nbrOfGlyphs-1): if (i == nbrOfGlyphs-1) and (glyphName != '\n'): wordGlyph.appendGlyph(glyph, (wordLetterOffset + kerningValue, 0)) wordGlyph.width += (glyph.width + tracking) wordGlyph.name += glyphName if glyphName != '\n': previousGlyph = glyph previousGlyphGroups = glyphGroups glyph = wordGlyph wordGlyph = RGlyph() wordGlyph.name = '' wordLetterOffset += tracking + kerningValue if (xAdvance + (glyph.width*sc*eA) >= width): xAdvance = 0 kerningValue = 0 yAdvance += (UPM*_line_Height)*sc # check for need of a new page if yAdvance + (UPM*sc) >= height: newPage() if showFrame: self.showFrame() if pageStamp: self.pageStamp() translate(xPos, yPos) xAdvance = 0 yAdvance = 0 kerningValue = 0 # Drawing, yay! save() if not keepWords: translate(xAdvance + (kerningValue*sc), -yAdvance) elif keepWords: translate(xAdvance, -yAdvance) scale(sc) # draw kerning if no word wrap if showKerning: save() fill(1, 0, 0, 0.5) if not keepWords: if kerningValue > 0: fill(.2, .9, 0, 0.5) rect(0, descender, -kerningValue, UPM) elif keepWords: for kern in wordKerning: drawPath(kern) wordKerning = [] restore() # draw glyphBox if showGlyphBox: glyphWidth = glyph.width save() fill() stroke(.5) rect(0, descender, glyphWidth, UPM) for h, c in [(0, (.5,)), (xHeight,(.5, .9, 0)), (capHeight,(.1, .4, .9))]: stroke(*c) line((0, h), (glyphWidth, h)) restore() # glyph color if len(color) == 3: fill(*color + (alpha,)) if alpha < 1: stroke(*color) elif len(color) == 4: cmykFill(*color + (alpha,)) if alpha < 1: cmykStroke(*color) else: fill(0) if alpha < 1:stroke(0) _drawGlyph(thisFont, glyph) if not keepWords: xAdvance += (glyph.width + tracking + kerningValue)*sc kerningValue = 0 previousGlyph = glyph previousGlyphGroups = glyphGroups elif keepWords and (glyphName != '\n'): xAdvance += (wordLetterOffset + spaceWidth + kerningValue)*sc wordLetterOffset = 0 kerningValue = 0 previousGlyph = glyph previousGlyphGroups = glyphGroups elif keepWords and (glyphName == '\n'): xAdvance = 0 kerningValue = 0 wordLetterOffset = 0 yAdvance += (UPM*_line_Height)*sc previousGlyph = None previousGlyphGroups = None restore() glyphStop = time() glyphDrawingTime += (glyphStop-glyphStart) * 1000 print 'average glyph drawing %0.2f ms, total %0.2f ms, %s glyphs drawn' % (glyphDrawingTime/nbrOfGlyphs, glyphDrawingTime, nbrOfGlyphs)
def set(self, glyphInput, keepWords=False): thisFont = self.thisFont kerning = self.kerning fontKeys = thisFont.keys() if isinstance(glyphInput, str) or isinstance(glyphInput, unicode): glyphRecord = self.stringToGlyphs(glyphInput) elif isinstance(glyphInput, list): glyphRecord = glyphInput suffixes = self.settings['suffix'] if len(suffixes) > 0: for i, glyphName in enumerate(glyphRecord): for suffix in suffixes: if glyphName + suffix in fontKeys: glyphRecord[i] = glyphName + suffix kernGroups = self.getKernGroups(glyphRecord) nbrOfGlyphs = len(glyphRecord) UPM = thisFont.info.unitsPerEm descender = thisFont.info.descender ascender = thisFont.info.ascender capHeight = thisFont.info.capHeight xHeight = thisFont.info.xHeight pointSize = self.type['size'] _line_Height = self.type['_line_Height'] tracking = self.type['tracking'] color = self.type['color'][0:] alpha = self.type['alpha'] x, y, width, height = self.canvas['posSize'] sc = pointSize / UPM xAdvance = yAdvance = 0 kerningValue = 0 # exceed allowance: to which extent can a word go beyond bounds eA = 0.97 wordLetterOffset = 0 spaceWidth = thisFont['space'].width glyphGroups = [] previousGlyph = None previousGlyphGroups = [] wordGlyph = RGlyph() wordGlyph.name = '' glyphDrawingTime = 0 wordKerning = [] useKerning = self.settings['useKerning'] showKerning = self.settings['showKerning'] showGlyphBox = self.settings['showGlyphBox'] showFrame = self.settings['showFrame'] pageStamp = self.settings['pageStamp'] newPage(*self.formats[self.preset]) if showFrame: self.showFrame() if pageStamp: self.pageStamp() # compensate for upward y coordinates xPos = x yPos = y + height - ((UPM + descender) * sc) translate(xPos, yPos) for i, glyphName in enumerate(glyphRecord): # filtering missing glyphs if (glyphName not in fontKeys) and (glyphName != '\n'): if '.notdef' in fontKeys: glyphName = '.notdef' else: continue # skip spaces at the start of a new line if (glyphName == 'space') and (xAdvance == 0) and (wordLetterOffset == 0): continue if glyphName != '\n': glyph = thisFont[glyphName] # check for need of a new line if ((glyphName == '\n') and (not keepWords)) or \ (xAdvance + (glyph.width*sc*eA) >= width): xAdvance = 0 kerningValue = 0 yAdvance += (UPM * _line_Height) * sc _newLine = False previousGlyph = None previousGlyphGroups = None if (glyphName == '\n'): continue # kerning if glyphName in kernGroups.keys(): glyphGroups = kernGroups[glyphName] # filter out non kern groups glyphGroups = [ group for group in glyphGroups if ('MMK' in group) or ('kern' in group) ] if (previousGlyph is not None) and useKerning: for group in glyphGroups: for prevGroup in previousGlyphGroups: if kerning[(prevGroup, group)] is not None: kerningValue = kerning[(prevGroup, group)] # glyphStart = time() # if word wrap if keepWords: # add each glyph of a word to the wordGlyph and skip the drawing # add metrics of each glyph to the wordGlyph if (glyphName != 'space') and (glyphName != '\n') and (i < nbrOfGlyphs - 1): # draw kerning if word wrap if showKerning and (kerningValue != 0): save() scale(sc) fill(1, 0, 0, 0.5) thisKern = BezierPath() thisKern.moveTo((wordLetterOffset, descender)) thisKern.lineTo((wordLetterOffset, ascender)) thisKern.lineTo( (wordLetterOffset + kerningValue, ascender)) thisKern.lineTo( (wordLetterOffset + kerningValue, descender)) thisKern.closePath() wordKerning.append(thisKern) restore() if len(glyph.components) > 0: glyph = _decompose(glyph) wordGlyph.appendGlyph(glyph, (wordLetterOffset + kerningValue, 0)) wordGlyph.width += (glyph.width + tracking + kerningValue) wordGlyph.name += glyphName wordLetterOffset += (glyph.width + tracking + kerningValue) previousGlyph = glyph previousGlyphGroups = glyphGroups kerningValue = 0 continue # when a space is hit, or if it’s the end of the text # the wordglyph is passed as main glyph to be drawn # check if the word doesn’t exceed boundaries elif (glyphName == 'space') or (glyphName == '\n') or (i == nbrOfGlyphs - 1): if (i == nbrOfGlyphs - 1) and (glyphName != '\n'): if len(glyph.components) > 0: glyph = _decompose(glyph) wordGlyph.appendGlyph( glyph, (wordLetterOffset + kerningValue, 0)) wordGlyph.width += (glyph.width + tracking) wordGlyph.name += glyphName if glyphName != '\n': previousGlyph = glyph previousGlyphGroups = glyphGroups glyph = wordGlyph wordGlyph = RGlyph() wordGlyph.name = '' wordLetterOffset += tracking + kerningValue if (xAdvance + (glyph.width * sc * eA) >= width): xAdvance = 0 kerningValue = 0 yAdvance += (UPM * _line_Height) * sc # check for need of a new page if yAdvance + (UPM * sc) >= height: newPage() if showFrame: self.showFrame() if pageStamp: self.pageStamp() translate(xPos, yPos) xAdvance = 0 yAdvance = 0 kerningValue = 0 # Drawing, yay! save() if not keepWords: translate(xAdvance + (kerningValue * sc), -yAdvance) elif keepWords: translate(xAdvance, -yAdvance) scale(sc) # draw kerning if no word wrap if showKerning: save() fill(1, 0, 0, 0.5) if not keepWords: if kerningValue > 0: fill(.2, .9, 0, 0.5) rect(0, descender, -kerningValue, UPM) elif keepWords: for kern in wordKerning: drawPath(kern) wordKerning = [] restore() # draw glyphBox if showGlyphBox: glyphWidth = glyph.width save() fill() stroke(.5) rect(0, descender, glyphWidth, UPM) for h, c in [(0, (.5, )), (xHeight, (.5, .9, 0)), (capHeight, (.1, .4, .9))]: stroke(*c) line((0, h), (glyphWidth, h)) restore() # glyph color if len(color) == 3: fill(*color + (alpha, )) if alpha < 1: stroke(*color) elif len(color) == 4: cmykFill(*color + (alpha, )) if alpha < 1: cmykStroke(*color) else: fill(0) if alpha < 1: stroke(0) glyphStart = time() glyph.setParent(thisFont) _drawGlyph(glyph) if not keepWords: xAdvance += (glyph.width + tracking + kerningValue) * sc kerningValue = 0 previousGlyph = glyph previousGlyphGroups = glyphGroups elif keepWords and (glyphName != '\n'): xAdvance += (wordLetterOffset + spaceWidth + kerningValue) * sc wordLetterOffset = 0 kerningValue = 0 previousGlyph = glyph previousGlyphGroups = glyphGroups elif keepWords and (glyphName == '\n'): xAdvance = 0 kerningValue = 0 wordLetterOffset = 0 yAdvance += (UPM * _line_Height) * sc previousGlyph = None previousGlyphGroups = None restore() glyphStop = time() glyphDrawingTime += (glyphStop - glyphStart) * 1000 print 'average glyph drawing %0.2f ms, total %0.2f ms, %s glyphs drawn' % ( glyphDrawingTime / nbrOfGlyphs, glyphDrawingTime, nbrOfGlyphs)
################# ################# def _drawGlyph(glyph): path = glyph.naked().getRepresentation("defconAppKit.NSBezierPath") drawPath(path) def _decompose(glyph): components = glyph.components font = glyph.getParent() decomposedGlyph = RGlyph() if font is not None: for component in components: decomponent = RGlyph() decomponent.appendGlyph(font[component.baseGlyph]) decomponent.scale((component.scale[0], component.scale[1])) decomponent.move((component.offset[0], component.offset[1])) decomposedGlyph.appendGlyph(decomponent) for contour in glyph.contours: decomposedGlyph.appendContour(contour) decomposedGlyph.width = glyph.width return decomposedGlyph class TypeSetter(object): def __init__(self, thisFont): if thisFont is None: return self.thisFont = thisFont