<s> '''<STR_LIT>''' <EOL> import os <EOL> import sys <EOL> from robofab . world import CurrentGlyph , CurrentFont <EOL> import RFUtils <EOL> RFUtils . hackPathIfNecessary ( ) <EOL> from tfs . common . TFSPath import TFSPath <EOL> from tfs . common . TFSSegment import TFSSegment <EOL> from tfs . common . TFSPoint import TFSPoint , TFSPoint0 <EOL> from tfs . common . TFSGlyph import TFSGlyph <EOL> import tfs . common . TFSIntersection as TFSIntersection <EOL> def processGlyph ( glyph , glyphName , paths , updateControlPoints ) : <EOL> '''<STR_LIT>''' <EOL> allPoints = [ ] <EOL> for path in paths : <EOL> for segment in path . segments : <EOL> allPoints . append ( segment . startPoint ( ) ) <EOL> allPoints . append ( segment . endPoint ( ) ) <EOL> selectedPoints = [ point for point in allPoints if point . selected ] <EOL> selectedPoints = set ( selectedPoints ) <EOL> print '<STR_LIT>' , selectedPoints <EOL> if len ( selectedPoints ) < <NUM_LIT:1> : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> pointSum = TFSPoint0 ( ) <EOL> for point in selectedPoints : <EOL> pointSum = pointSum . plus ( point ) <EOL> pointAverage = pointSum . scale ( <NUM_LIT:1.0> / len ( selectedPoints ) ) <EOL> pointAverage = pointAverage . roundWithDefaultPrecision ( ) <EOL> def updateSegment ( segment ) : <EOL> startPoint = segment . startPoint ( ) <EOL> if segment . startPoint ( ) in selectedPoints : <EOL> startPoint = pointAverage <EOL> endPoint = segment . endPoint ( ) <EOL> if segment . endPoint ( ) in selectedPoints : <EOL> endPoint = pointAverage <EOL> if startPoint == endPoint : <EOL> return None <EOL> newPoints = [ startPoint , ] <EOL> if len ( segment . points ) == <NUM_LIT:2> : <EOL> pass <EOL> elif len ( segment . points ) == <NUM_LIT:3> : <EOL> if updateControlPoints : <EOL> controlPoint0 = TFSIntersection . getIntersectPoint ( startPoint , <EOL> startPoint . plus ( segment . startTangent ( ) ) , <EOL> endPoint , <EOL> endPoint . plus ( segment . endTangent ( ) ) ) <EOL> else : <EOL> controlPoint0 = segment . points [ <NUM_LIT:1> ] <EOL> newPoints += [ controlPoint0 , ] <EOL> elif len ( segment . points ) == <NUM_LIT:4> : <EOL> if updateControlPoints : <EOL> factor = startPoint . distanceTo ( endPoint ) / segment . endPointDistance ( ) <EOL> controlPoint0 = startPoint . plus ( segment . startTangent ( ) . scale ( segment . startVector ( ) . length ( ) * factor ) ) <EOL> controlPoint1 = endPoint . plus ( segment . endTangent ( ) . scale ( segment . endVector ( ) . length ( ) * - factor ) ) <EOL> else : <EOL> controlPoint0 = segment . points [ <NUM_LIT:1> ] <EOL> controlPoint1 = segment . points [ <NUM_LIT:2> ] <EOL> newPoints += [ controlPoint0 , controlPoint1 , ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' + segment . description ( ) ) <EOL> newPoints += [ endPoint , ] <EOL> return TFSSegment ( * newPoints ) <EOL> newPaths = [ ] <EOL> for path in paths : <EOL> newSegments = [ ] <EOL> for segment in path . segments : <EOL> if segment . startPoint ( ) in selectedPoints or segment . endPoint ( ) in selectedPoints : <EOL> newSegment = updateSegment ( segment ) <EOL> if newSegment is not None : <EOL> newSegments . append ( newSegment ) <EOL> else : <EOL> newSegments . append ( segment ) <EOL> newPaths . append ( TFSPath ( True , * newSegments ) ) <EOL> return newPaths <EOL> def mergeSelectedPointsInGlyph ( rfglyph , updateControlPoints ) : <EOL> rfglyph . prepareUndo ( "<STR_LIT>" ) <EOL> glyph = TFSGlyph ( rfglyph ) <EOL> glyphName = hex ( glyph . unicode ) if glyph . unicode is not None else '<STR_LIT>' <EOL> paths = glyph . getContours ( setSelected = True ) <EOL> paths = processGlyph ( glyph , glyphName , paths , updateControlPoints ) <EOL> if paths is not None : <EOL> glyph . setContours ( paths ) <EOL> rfglyph . performUndo ( ) <EOL> def mergeSelectedPointsInCurrentGlyph ( updateControlPoints ) : <EOL> glyph = CurrentGlyph ( ) <EOL> if glyph is None : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> mergeSelectedPointsInGlyph ( glyph , updateControlPoints ) </s>
