Advertisement
ChristophKnoth

Designing Letters 2

Mar 27th, 2011
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 34.34 KB | None | 0 0
  1. """ This application helps designing letters. """
  2.  
  3. #__author__ = 'Christoph Knoth, ECAL/Ecole cantonale d art de Lausanne'
  4.  
  5.  
  6. from math import atan2, atan, pi
  7. import wx
  8. import wx.lib.scrolledpanel as scrolled
  9.  
  10.  
  11. ########################################################################
  12. # preferences vars
  13.  
  14. #DebugMode = False
  15. DebugMode = True
  16.  
  17. NodeNames = False
  18. #NodeNames = True
  19.  
  20. ShowOffCurvePoints = False
  21. ShowOffCurvePoints = True
  22.  
  23. #sortedLetters = False
  24. #sortedLetters = True
  25.  
  26.  
  27. row = 0
  28. letter = 0
  29. globalWidth = 500.
  30.  
  31. StrokeWidth = 6.
  32.  
  33. PenWidth = 10. #thickness
  34.  
  35. ConnectionDotColor= (1, 0, 0)
  36.  
  37. ##################
  38. # type based Variables
  39.  
  40.  
  41. #connectionType = "straight"  #straight only
  42. connectionType = "round" #straight and round mixed
  43.  
  44.  
  45. baseline = 0.
  46. xHeight = 400.
  47. xHeightSave = xHeight
  48.  
  49. descHeight = -300.
  50.  
  51. SerifHeight = StrokeWidth * 0.9
  52. xHeightSerif = xHeight - SerifHeight
  53.  
  54. capHeight = xHeight*15/10.
  55.  
  56.  
  57. # things that have to do with the beginning and ending of strokes or with arms and ...
  58. openess = 0.2 #0 very open -> too 0.5 closed
  59. openAngle = 1.5
  60.  
  61.  
  62. StemShoulderConnectionHeight = xHeight * 6/10.
  63. BottomBowlConnectionHeight = xHeight * 3/10.
  64.  
  65. rightOpenContourFromTopCaps = (1 - openess) * capHeight + baseline
  66. rightOpenContourFromBottomCaps = openess * capHeight + baseline
  67.  
  68. rightOpenContourFromTopMinors = (1 - openess) * xHeight + baseline
  69. rightOpenContourFromBottomMinors = openess * xHeight + baseline
  70.  
  71. left = 0.
  72.  
  73. offsetdrawing = 200.
  74.  
  75.  
  76. #roundness / curviness
  77.    
  78. exponent = 6. #if bigger it makes the extrema: very thin and extended more pronounced, if smaller there is no squariness in thin and wide letters
  79. stretchFactor = pi/800. #the perfect roundness is at this point
  80. minimumCurveHori = 0.58
  81. minimumCurveVerti = 0.6
  82.  
  83.  
  84.  
  85. ########################################################################
  86. # Viewport
  87.  
  88. ScaleFactor = 0.27
  89.  
  90.  
  91. #############
  92. # Colors Display
  93.  
  94. #wx.Colour(r, g, b, wx.ALPHA_OPAQUE)
  95. blue = wx.Colour(0, 255, 0, 255)
  96. black = wx.Colour(0, 0, 0, 255)
  97. HandlePenWidth = 4.
  98. HandleOvalWidth = 5
  99.            
  100.  
  101.  
  102. # Class that defines a Node
  103.  
  104. class Node(object):
  105.     """Defining a Node with the parameters x, y and its name.
  106.    Draws an oval at the position of the node with the name next to it."""
  107.    
  108.     fonts = 10. / ScaleFactor
  109.     fontName = "Helvetica"  
  110.     nameOffset = 15, 15
  111.     dotSize = StrokeWidth/ 2.
  112.    
  113.     def __init__(self, x, y, name=""):
  114.         self.x = x
  115.         self.y = y
  116.         self.name = name
  117.        
  118.     def draw(self,gc):
  119.        
  120.         if DebugMode:
  121.             gc.SetPen(wx.Pen(black, PenWidth))
  122.         else:
  123.             gc.SetPen(wx.Pen(blue, PenWidth))
  124.  
  125.  
  126. class Connection(object):
  127.     """Defining a connection with the start and end point and a name.
  128.    Draws a stroke from the start to the end."""
  129.    
  130.     def __init__(self, start, end, name=""):
  131.         self.start = start
  132.         self.end = end
  133.         self.name = name
  134.         self._parent = None
  135.            
  136.     def setParent(self, obj):
  137.         self._parent = obj
  138.        
  139.     def getParent(self):
  140.         return self._parent
  141.    
  142.     def getAngle(self):
  143.         xDiff = self.end.x - self.start.x
  144.         yDiff = self.end.y - self.start.y
  145.         angle = atan2(xDiff, yDiff)
  146.         return angle
  147.        
  148.     def getPreviousConnection(self):
  149.         p = self.getParent()
  150.         i = p.index(self)
  151.         if i <= 0:
  152.             return None
  153.         elif i >= len(p):
  154.             return None
  155.         return p[i-1]
  156.              
  157.     def getFollowingConnection(self):
  158.         p = self.getParent()
  159.         i = p.index(self)
  160.         if i <= 0:
  161.             return None
  162.         elif i >= len(p):
  163.             return None
  164.         return p[i]
  165.        
  166.     def draw(self,gc):
  167.                
  168.         #print self.getAngle() #works
  169.        
  170.         #print self.getPreviousConnection() # does not work
  171.         #print self.getFollowingConnection()
  172.        
  173.         debugPath=gc.CreatePath()
  174.         path=gc.CreatePath()
  175.        
  176.         path.MoveToPoint(self.start.x, self.start.y)
  177.        
  178.         offCurvePoint1x = self.start.x
  179.         offCurvePoint1y = self.start.y
  180.         offCurvePoint2x = self.end.x
  181.         offCurvePoint2y = self.end.y
  182.        
  183.        
  184.         #test if there is any roundness involved. if yes, check in which direction the roundness points
  185.        
  186.         if any(x in self.name for x in ['rb', 'lb', 'rt', 'lt']):
  187.             if ((
  188.                     'lt' in self.name and self.start.x >= self.end.x and self.start.y >= self.end.y
  189.                 ) or (
  190.                     'rt' in self.name and self.start.x <= self.end.x and self.start.y >= self.end.y
  191.                 ) or (
  192.                     'lb' in self.name and self.start.x >= self.end.x and self.start.y <= self.end.y
  193.                 ) or (
  194.                     'rb' in self.name and self.start.x <= self.end.x and self.start.y <= self.end.y
  195.                 )):
  196.                 offCurvePoint1xMultiplicator = -1
  197.                 offCurvePoint1yMultiplicator = 0
  198.                 offCurvePoint2xMultiplicator = 0
  199.                 offCurvePoint2yMultiplicator = 1
  200.             else:
  201.                 offCurvePoint1xMultiplicator = 0
  202.                 offCurvePoint1yMultiplicator = -1
  203.                 offCurvePoint2xMultiplicator = 1
  204.                 offCurvePoint2yMultiplicator = 0
  205.  
  206.             distanceHori = self.start.x - self.end.x
  207.             distanceVerti = self.start.y - self.end.y  
  208.  
  209.             # no division by zero
  210.             if distanceHori == 0:
  211.                 distanceHori = distanceHori + 0.00001
  212.            
  213.             if distanceVerti == 0:
  214.                 distanceVerti = distanceVerti + 0.00001
  215.                        
  216.             CurveHori = distanceHori * ((1-minimumCurveHori) * 2./pi * (atan(stretchFactor*(distanceVerti / distanceHori ) **exponent)) + minimumCurveHori)
  217.             CurveVerti = distanceVerti * ((1-minimumCurveVerti) * 2./pi * (atan(stretchFactor*(distanceHori / distanceVerti ) **exponent)) +  minimumCurveVerti)
  218.            
  219.             offCurvePoint1x = self.start.x + offCurvePoint1xMultiplicator * CurveHori
  220.             offCurvePoint1y = self.start.y + offCurvePoint1yMultiplicator * CurveVerti
  221.  
  222.             offCurvePoint2x = self.end.x   + offCurvePoint2xMultiplicator * CurveHori
  223.             offCurvePoint2y = self.end.y   + offCurvePoint2yMultiplicator * CurveVerti
  224.                                
  225.             if DebugMode:
  226.                 if "rb" in self.name:
  227.                     gc.SetPen(wx.Pen(black, HandlePenWidth))
  228.                 elif "lb" in self.name:
  229.                     gc.SetPen(wx.Pen(black, HandlePenWidth))
  230.                 elif "rt" in self.name:
  231.                     gc.SetPen(wx.Pen(black, HandlePenWidth))
  232.                 elif "lt" in self.name:
  233.                     gc.SetPen(wx.Pen(black, HandlePenWidth))
  234.                 else:
  235.                     gc.SetPen(wx.Pen(black, HandlePenWidth))
  236.                
  237.                 if ShowOffCurvePoints and (("rb" in self.name) or ("lb" in self.name) or ("rt" in self.name) or ("lt" in self.name)):
  238.                
  239.                     debugPath.MoveToPoint(self.start.x, self.start.y)
  240.                     debugPath.AddLineToPoint(offCurvePoint1x, offCurvePoint1y)
  241.                     debugPath.MoveToPoint(offCurvePoint2x, offCurvePoint2y)
  242.                     debugPath.AddLineToPoint(self.end.x, self.end.y)
  243.                    
  244.                     gc.DrawEllipse(offCurvePoint1x-HandleOvalWidth/2., offCurvePoint1y-HandleOvalWidth/2., HandleOvalWidth, HandleOvalWidth)
  245.                     gc.DrawEllipse(offCurvePoint2x-HandleOvalWidth/2., offCurvePoint2y-HandleOvalWidth/2., HandleOvalWidth, HandleOvalWidth)
  246.                    
  247.                 gc.DrawPath(debugPath)
  248.                    
  249.                 if "rb" in self.name:
  250.                     gc.SetPen(wx.Pen(blue, PenWidth))
  251.                 elif "lb" in self.name:
  252.                     gc.SetPen(wx.Pen("#0000FF", PenWidth))
  253.                 elif "rt" in self.name:
  254.                     gc.SetPen(wx.Pen("#FF0000", PenWidth))
  255.                 elif "lt" in self.name:
  256.                     gc.SetPen(wx.Pen("#FFFF00", PenWidth))
  257.            
  258.             #if debug is off make it black
  259.             else:
  260.                     gc.SetPen(wx.Pen(black, PenWidth))
  261.  
  262.             path.AddCurveToPoint(offCurvePoint1x, offCurvePoint1y, offCurvePoint2x, offCurvePoint2y, self.end.x, self.end.y)
  263.        
  264.        
  265.         #if there is no roundness, it is probably a straight line, so draw it
  266.         else:
  267.             gc.SetPen(wx.Pen(black, PenWidth))
  268.             path.AddLineToPoint(self.end.x, self.end.y)
  269.        
  270.        
  271.         gc.DrawPath(path)
  272.  
  273.        
  274. class Stroke(object):
  275.  
  276.     def __init__(self, connections):
  277.         self.connections = connections
  278.         for connection in self.connections:
  279.             connection.setParent(self.connections)
  280.  
  281.     def subscribeStrokes(self):
  282.         for connection in self.connections:
  283.             connection.setParent(self)
  284.    
  285.     def __len__(self):
  286.         return len(self.connections)
  287.    
  288.     def index(self, obj):
  289.         if obj in self.connections:
  290.             return None
  291.         return self.connections.index(obj)
  292.    
  293.     def getStrokes(self, index):
  294.         return self.connections[index]
  295.  
  296.     def draw(self,gc):
  297.         for c in self.connections:
  298.             #c1=c.getPreviousConnection()
  299.             #c2=c.getFollowingConnection()
  300.            
  301.             c.draw(gc)
  302.             #if c1 != None:
  303.             #    c1.draw(gc)
  304.             #    print c.getAngle()
  305.                
  306.             #if c.getFollowingConnection()
  307.  
  308.  
  309.  
  310. # Class that defines a Childconnection of two Nodes
  311. # for now it splits those two connections and connects the cutting points useful for A, H,  for B, E, F,P, R, T, X, Y i need something else i guess?
  312.  
  313. class ccCrossbar(object):
  314.     """Takes two connections and draws a crossbar in between them"""
  315.    
  316.     def __init__(self, firstConnection, lastConnection, t1=.51, t2=.51):
  317.         self.firstConnection = firstConnection
  318.         self.lastConnection = lastConnection
  319.        
  320.         self.t1 = t1
  321.         self.t2 = t2
  322.  
  323.        
  324.     def draw(self,gc):
  325.         firstDiffX = self.firstConnection.end.x - self.firstConnection.start.x
  326.         firstDiffY = self.firstConnection.end.y - self.firstConnection.start.y
  327.        
  328.         lastDiffX = self.lastConnection.end.x - self.lastConnection.start.x
  329.         lastDiffY = self.lastConnection.end.y - self.lastConnection.start.y
  330.        
  331.         s = Node(self.firstConnection.start.x + firstDiffX*self.t1, self.firstConnection.start.y + firstDiffY*self.t1)
  332.         s.draw(gc)
  333.        
  334.         e = Node(self.lastConnection.start.x + lastDiffX* self.t2, self.lastConnection.start.y + lastDiffY* self.t2)
  335.         e.draw(gc)
  336.        
  337.         if DebugMode:
  338.             gc.SetPen(wx.Pen("#AAAAAA", PenWidth))
  339.         else:
  340.             gc.SetPen(wx.Pen(black, PenWidth))
  341.            
  342.        
  343.        
  344.         debugPath=gc.CreatePath()
  345.         path=gc.CreatePath()
  346.        
  347.         path.MoveToPoint(s.x, s.y)
  348.         path.AddLineToPoint(e.x, e.y)
  349.        
  350.         gc.DrawPath(debugPath)
  351.         gc.DrawPath(path)
  352.            
  353.        
  354. class Glyph(object):
  355.  
  356.     def __init__(self, strokes):
  357.         self.strokes = strokes
  358.  
  359.     def subscribeStrokes(self):
  360.         for stroke in self.strokes:
  361.             stroke.setParent(self)
  362.    
  363.     def __len__(self):
  364.         return len(self.strokes)
  365.    
  366.     def index(self, obj):
  367.         if obj in self.strokes:
  368.             return None
  369.         return self.strokes.index(obj)
  370.    
  371.     def getStrokes(self, index):
  372.         return self.strokes[index]
  373.    
  374.     def draw(self,gc):
  375.         for c in self.strokes:
  376.             c.draw(gc)
  377.            
  378.        
  379.        
  380. class Info(object):
  381.     def __init__(self):
  382.         self.unitsPerEm = 1000
  383.  
  384.  
  385. class Font(dict):
  386.     """ Class that defines a Font as a Dictonary with its inherited Glyphs"""
  387.        
  388.     def _get_info(self):
  389.         if not hasattr(self, "_info"):
  390.             self._info = Info()
  391.         return self._info
  392.    
  393.     info = property(_get_info)
  394.    
  395.     def draw(self,gc):
  396.         print "Font.draw"
  397.         row = 0
  398.         translateWidth = globalWidth
  399.         line = 0
  400.  
  401.         for key in sorted(self):
  402.             glyph = self[key]
  403.             glyph.draw(gc)
  404.            
  405.             #make rows that letters wont go out of the screen
  406.            
  407.             gc.Translate((glyph.individualWidth * 1.4), 0)
  408.             row = row + 1
  409.            
  410.             translateWidth = translateWidth + glyph.individualWidth * 1.4
  411.  
  412.             if translateWidth > globalWidth * 8:
  413.                 line = line + 1
  414.                 #print "line", line    
  415.                 row = 0
  416.                 lineHeight = max(capHeight,xHeight)
  417.                 gc.Translate(-translateWidth + globalWidth , -lineHeight*1.5)
  418.                 translateWidth = globalWidth
  419.  
  420.                        
  421.     def createGlyphs(self):
  422.         functionName = "_glyph_"
  423.        
  424.         #cycle through all the names of def in font
  425.         for key in dir(self):
  426.             if functionName in key:
  427.                 name = key.replace(functionName, "")
  428.                 #print key
  429.                 #print(name)
  430.                 #call _glyph_function
  431.                 getattr(self, key)(name)
  432.  
  433.    
  434.  
  435.     def _glyph_A(self, name):
  436.         #print "initializing ", name
  437.  
  438.         individualWidth = globalWidth*0.9
  439.  
  440.         n = {0:0}
  441.         n[1] = Node(left, baseline, "left, bottom")    
  442.         n[2] = Node((left + individualWidth)/ 2., capHeight+baseline, "top, middle")
  443.    
  444.         c = {0:0}
  445.         c[1] = Connection(n[1], n[2], "straight")
  446.    
  447.        
  448.         n[3] = Node(individualWidth, baseline, "right, bottom")
  449.        
  450.         c[2] = Connection(n[3], n[2], "straight")
  451.    
  452.         cb = ccCrossbar(c[1], c[2], .3, .3)
  453.    
  454.         self[name] = Glyph([c[1], c[2], cb])
  455.         self[name].individualWidth = individualWidth
  456.        
  457.  
  458.     def _glyph_B(self, name):
  459.  
  460.         individualWidth = globalWidth*0.6
  461.         n = {0:0}
  462.         c = {0:0}
  463.         s = {0:0}
  464.        
  465.         #top bowl
  466.         n[1] = Node( left, capHeight + baseline, "left, top") #make list
  467.         n[2] = Node( individualWidth * 600/self.info.unitsPerEm, capHeight + baseline, "top bowl, start curve, right ")  
  468.         n[3] = Node( individualWidth * 960/1000, (capHeight + baseline) * 760/1000, "right extrema, bowl, middle curve")  
  469.         n[4] = Node( individualWidth* 600/1000, (capHeight + baseline) / 2., "right, bowl, curve, vertical middle of letter")
  470.         n[5] = Node( left, (capHeight + baseline) / 2., "touching stem, middle vertical, joint")
  471.        
  472.         #connecting all nodes
  473.         c[1] = Connection(n[1], n[2])        
  474.         c[2] = Connection(n[2], n[3], "rt")
  475.         c[3] = Connection(n[3], n[4], "rb")
  476.         c[4] = Connection(n[4], n[5])    
  477.        
  478.         s[1] = Stroke([ c[1], c[2], c[3], c[4] ])
  479.        
  480.  
  481.  
  482.         #bottom bowl
  483.        
  484.         n[5] = Node( individualWidth * 600/1000, (capHeight + baseline) / 2., "rightStart")
  485.         n[6] = Node( individualWidth * 1050/1000, (capHeight + baseline) * 1/4, "rightStart")
  486.         n[7] = Node( individualWidth * 600/1000, baseline, "rightStart")
  487.         n[8] = Node( left, baseline, "rightStart")
  488.        
  489.         c[5] = Connection(n[5], n[6], "rt")        
  490.         c[6] = Connection(n[6], n[7], "rb")
  491.         c[7] = Connection(n[7], n[8])
  492.  
  493.         s[2] = Stroke([c[5], c[6], c[7]])        
  494.  
  495.  
  496.         #stem
  497.         c[8] = Connection(n[1], n[8])
  498.  
  499.         s[3] = Stroke([c[8]])        
  500.        
  501.         self[name] = Glyph([s[1], s[2], s[3]])
  502.         self[name].individualWidth = individualWidth
  503.  
  504.  
  505.     def _glyph_C(self, name):
  506.         #print "initializing ", name
  507.         individualWidth = globalWidth*0.7
  508.        
  509.         n1 = Node( individualWidth, rightOpenContourFromTopCaps, "rightTopStart")
  510.         n2 = Node( (left + individualWidth)/ 2., capHeight+baseline, "middleTop")
  511.         n3 = Node( left, (capHeight)/ 2. + baseline, "leftMiddle")
  512.         n4 = Node( (left + individualWidth)/ 2., baseline, "middleBottom")
  513.         n5 = Node( individualWidth, rightOpenContourFromBottomCaps, "right Bottom End")
  514.        
  515.         c1 = Connection(n1, n2, "rt")        
  516.         c2 = Connection(n2, n3, "lt")
  517.         c3 = Connection(n3, n4, "lb")
  518.         c4 = Connection(n4, n5, "rb")
  519.        
  520.         s1 = Stroke([c1, c2, c3, c4])
  521.        
  522.         self[name] = Glyph([s1])
  523.         self[name].individualWidth = individualWidth
  524.        
  525.    
  526.     def _glyph_E(self, name):
  527.  
  528.         individualWidth = globalWidth*0.55
  529.        
  530.         n1 = Node( individualWidth, capHeight + baseline, "rightStart")  
  531.         n2 = Node( left, capHeight + baseline, "middleTop")
  532.         n3 = Node( left, (capHeight)/ 2. + baseline, "leftMiddle")
  533.         n4 = Node( individualWidth, (capHeight)/ 2. + baseline, "leftMiddle")
  534.         n5 = Node( left, baseline, "middleBottom")
  535.         n6 = Node( individualWidth, baseline, "rightEnd")
  536.        
  537.         c1 = Connection(n1, n2, "top stroke")        
  538.         c2 = Connection(n2, n3, "left stroke")
  539.         c3 = Connection(n3, n4, "middle bar")
  540.         c4 = Connection(n3, n5)
  541.         c5 = Connection(n5, n6, "bottom stroke")
  542.        
  543.         s1 = Stroke([c1, c2, c3, c4, c5])
  544.            
  545.         self[name] = Glyph([s1])
  546.         self[name].individualWidth = individualWidth
  547.      
  548.  
  549.     def _glyph_H(self, name):
  550.        
  551.         individualWidth = globalWidth * 0.7
  552.        
  553.         s = Node(left, baseline, "left")    
  554.         e = Node(left, capHeight + baseline, "top")
  555.    
  556.         c = Connection(s, e)
  557.    
  558.         s = Node(left + individualWidth, baseline, "left")    
  559.         e = Node(left + individualWidth, capHeight + baseline, "top")
  560.         c2 = Connection(s, e)
  561.    
  562.         cc = ccCrossbar(c, c2, .5, .5)
  563.    
  564.         self[name] = Glyph([c, c2, cc])
  565.         self[name].individualWidth = individualWidth  
  566.                
  567.  
  568.     def _glyph_M1(self, name):
  569.        
  570.         individualWidth = globalWidth * 1.0
  571.        
  572.         n1 = Node(left, baseline, "leftStart")  
  573.         n2 = Node(2/10. * (left + individualWidth), capHeight + baseline, "leftTop")
  574.         n3 = Node(1/ 2. * (left + individualWidth), baseline, "middleBottom")
  575.         n4 = Node(8/10. * (left + individualWidth), capHeight + baseline, "rightTop")
  576.         n5 = Node(left + individualWidth, baseline, "rightEnd")
  577.        
  578.         c1 = Connection(n1, n2)        
  579.         c2 = Connection(n2, n3)
  580.         c3 = Connection(n3, n4)
  581.         c4 = Connection(n4, n5)
  582.        
  583.         s = Stroke([c1, c2, c3, c4])
  584.            
  585.         self[name] = Glyph([s])
  586.         self[name].individualWidth = individualWidth
  587.        
  588.  
  589.     def _glyph_N(self, name):
  590.         individualWidth = globalWidth * 0.7
  591.        
  592.         n1 = Node(left, baseline, "leftTop")
  593.         n2 = Node(left, capHeight, "middleBottom")
  594.         n3 = Node(left + individualWidth, baseline, "rightTop")
  595.         n4 = Node(left + individualWidth, capHeight, "rightEnd")
  596.        
  597.         c1 = Connection(n1, n2)
  598.         c2 = Connection(n2, n3)
  599.         c3 = Connection(n3, n4)
  600.            
  601.         s = Stroke([c1, c2, c3])
  602.            
  603.         self[name] = Glyph([s])
  604.        
  605.         self[name].individualWidth = individualWidth      
  606.        
  607.  
  608.     def _glyph_U(self, name):
  609.         #print "initializing ", name
  610.         individualWidth = globalWidth*0.8
  611.        
  612.         n={0:0}
  613.         n[1] = Node( left, capHeight+baseline, "middleTop")
  614.         n[2] = Node( left, capHeight * 1/3 + baseline, "leftMiddle")
  615.         n[3] = Node( (left + individualWidth) / 2., baseline, "middleBottom")
  616.         n[4] = Node( individualWidth, capHeight * 1/3  + baseline, "rightEnd")
  617.         n[5] = Node( individualWidth, capHeight, "rightEnd")
  618.        
  619.         c1 = Connection(n[1], n[2],)        
  620.         c2 = Connection(n[2], n[3], "lb")
  621.         c3 = Connection(n[3], n[4], "rb")
  622.         c4 = Connection(n[4], n[5],)
  623.            
  624.         s = Stroke([c1, c2, c3, c4])
  625.            
  626.         self[name] = Glyph([s])
  627.         self[name].individualWidth = individualWidth
  628.  
  629.    
  630.     def _glyph_W2(self, name):
  631.         individualWidth = globalWidth * 1.2
  632.        
  633.         n1 = Node(left, capHeight, "leftStart")  
  634.         n2 = Node(1/3.*(left + individualWidth), baseline, "leftTop")
  635.         n3 = Node(3/5.*(left + individualWidth), capHeight, "middleBottom")
  636.        
  637.         n4 = Node(2/5.*(left + individualWidth), capHeight, "middleBottom")
  638.         n5 = Node(2/3.*(left + individualWidth), baseline, "rightTop")
  639.         n6 = Node(left + individualWidth, capHeight, "rightEnd")
  640.        
  641.         c1 = Connection(n1, n2)        
  642.         c2 = Connection(n2, n3)
  643.         c3 = Connection(n4, n5)
  644.         c4 = Connection(n5, n6)
  645.            
  646.            
  647.         stroke1 = Stroke([c1, c2])
  648.         stroke2 = Stroke([c3, c4])
  649.  
  650.         self[name] = Glyph([stroke1, stroke2])
  651.         self[name].individualWidth = individualWidth  
  652.        
  653.        
  654.     def _glyph_n1(self, name):
  655.         individualWidth = globalWidth * 0.6
  656.  
  657.         #stem
  658.        
  659.         n1 = Node(left, baseline, "leftTop")
  660.         n2 = Node(left, xHeight, "middleBottom")
  661.        
  662.         c1 = Connection(n1, n2)
  663.        
  664.         stroke1 = Stroke([c1])
  665.  
  666.        
  667.         #bogen
  668.        
  669.         n3 = Node( left, StemShoulderConnectionHeight, "rightTop")
  670.        
  671.         n4 = Node( (left + individualWidth)*550/1000 , xHeight, "rightTop")
  672.         n5 = Node(left + individualWidth, xHeight*650/1000, "rightEnd")
  673.         n6 = Node(left + individualWidth, baseline, "rightEnd")
  674.  
  675.         c2 = Connection(n3, n4, "lt")
  676.         c3 = Connection(n4, n5, "rt")
  677.         c4 = Connection(n5, n6)
  678.        
  679.         stroke2 = Stroke([c2,c3,c4])
  680.            
  681.         self[name] = Glyph([stroke1, stroke2])
  682.         self[name].individualWidth = individualWidth  
  683.  
  684.  
  685.     def _glyph_a1(self, name):
  686.         individualWidth = globalWidth * 0.65
  687.  
  688.         n1 = Node(left + individualWidth, StemShoulderConnectionHeight, "rightTopStartBowl")
  689.         n2 = Node( (left + individualWidth) / 2. , xHeight, "MiddleTopBowl")
  690.         n3 = Node( left, xHeight / 2., "leftHorizontalMiddle")
  691.         n4 = Node( (left + individualWidth) / 2. , baseline, "rightTop")
  692.         n5 = Node(left + individualWidth, BottomBowlConnectionHeight, "rightEnd")
  693.  
  694.         c1 = Connection(n1, n2, "rt")
  695.         c2 = Connection(n2, n3, "lt")
  696.         c3 = Connection(n3, n4, "lb")
  697.         c4 = Connection(n4, n5, "rb")
  698.        
  699.         stroke1 = Stroke([c1, c2, c3, c4])
  700.  
  701.  
  702.         #stem
  703.         st1 = Node(left + individualWidth, baseline, "leftTop")
  704.         st2 = Node(left + individualWidth, xHeight, "middleBottom")
  705.        
  706.         cs1 = Connection(st1, st2)
  707.        
  708.         stroke2 = Stroke([cs1])
  709.  
  710.            
  711.         self[name] = Glyph([stroke1, stroke2])
  712.         self[name].individualWidth = individualWidth  
  713.        
  714.  
  715.     def _glyph_a2(self, name):
  716.         individualWidth = globalWidth * 0.65
  717.  
  718.         n1 = Node(left + individualWidth, xHeight * 300/1000, "rightTopStartBowl")
  719.         n2 = Node( (left + individualWidth) / 2. , xHeight* 6/10, "MiddleTopBowl")
  720.         n3 = Node( left, xHeight * 300/1000, "leftHorizontalMiddle")
  721.         n4 = Node( (left + individualWidth) / 2. , baseline, "rightTop")
  722.         n5 = Node(left + individualWidth, xHeight * 300/1000, "rightEnd")
  723.  
  724.         c1 = Connection(n1, n2, "rt")
  725.         c2 = Connection(n2, n3, "lt")
  726.         c3 = Connection(n3, n4, "lb")
  727.         c4 = Connection(n4, n5, "rb")
  728.  
  729.         stroke1 = Stroke([c1, c2, c3, c4])
  730.  
  731.  
  732.         #stem
  733.         st1 = Node(left, xHeight*750/1000, "left Arm, Start")
  734.         st2 = Node(left + individualWidth/ 2., xHeight, "MiddleTop")
  735.         st3 = Node(left + individualWidth, xHeight*750/1000, "Right Shoulder")
  736.         st4 = Node(left + individualWidth, baseline, "leftTop")
  737.        
  738.         cs1 = Connection(st1, st2, "lt")
  739.         cs2 = Connection(st2, st3, "rt")
  740.         cs3 = Connection(st3, st4)        
  741.        
  742.         stroke2 = Stroke([cs1, cs2, cs3])
  743.  
  744.            
  745.         self[name] = Glyph([stroke1, stroke2])
  746.         self[name].individualWidth = individualWidth  
  747.        
  748.  
  749.     def _glyph_c(self, name):
  750.         individualWidth = globalWidth * 0.65
  751.  
  752.         n1 = Node(left + individualWidth, rightOpenContourFromTopMinors, "rightTopStartBowl")
  753.         n2 = Node( (left + individualWidth) / 2. , xHeight, "MiddleTopBowl")
  754.         n3 = Node( left, xHeight/ 2., "leftHorizontalMiddle")
  755.         n4 = Node( (left + individualWidth) / 2. , baseline, "rightTop")
  756.         n5 = Node(left + individualWidth, rightOpenContourFromBottomMinors, "rightEnd")
  757.  
  758.         c1 = Connection(n1, n2, "rt start")
  759.         c2 = Connection(n2, n3, "lt")
  760.         c3 = Connection(n3, n4, "lb")
  761.         c4 = Connection(n4, n5, "rb end")
  762.  
  763.         s = Stroke([c1, c2, c3, c4])
  764.            
  765.         self[name] = Glyph([s])
  766.         self[name].individualWidth = individualWidth              
  767.  
  768.  
  769.     def _glyph_e3(self, name):
  770.         individualWidth = globalWidth * 0.65
  771.  
  772.         #crossbar
  773.        
  774.         n1 = Node(left, xHeight / 2., "crossbar, left")
  775.         n2 = Node((left + individualWidth) * 950 / 1000 , xHeight / 2., "crossbar, right")
  776.  
  777.         #round stroke
  778.         n3 = Node(left + individualWidth, xHeight * 2/3, "right, top, bowl")
  779.         n4 = Node( (left + individualWidth) / 2. , xHeight, "vertical middle, top, bowl")
  780.         n5 = Node( left, xHeight / 2., "left, vertical middle")
  781.         n6 = Node( (left + individualWidth) / 2. , baseline, "horizontal, middle, bottom")
  782.         n7 = Node(left + individualWidth, rightOpenContourFromBottomMinors, "right, bottom, arm")
  783.  
  784.         c1 = Connection(n1, n2)
  785.         c2 = Connection(n2, n3, "rb")
  786.         c3 = Connection(n3, n4, "rt")
  787.         c4 = Connection(n4, n5, "lt")
  788.         c5 = Connection(n5, n6, "lb")
  789.         c6 = Connection(n6, n7, "rb")
  790.  
  791.         s1 = Stroke([c1])
  792.         s2 = Stroke([c2, c3, c4, c5, c6])
  793.  
  794.         self[name] = Glyph([s1, s2])
  795.         self[name].individualWidth = individualWidth        
  796.  
  797.  
  798.     def _glyph_g(self, name):
  799.         individualWidth = globalWidth * 0.7
  800.  
  801.         n1 = Node(left + individualWidth, xHeight * 600/1000, "rightTopStartBowl")
  802.         n2 = Node( (left + individualWidth) / 2. , xHeight, "MiddleTopBowl")
  803.         n3 = Node( left, xHeight  * 600/1000, "leftHorizontalMiddle")
  804.         n4 = Node( (left + individualWidth) / 2. , xHeight*200/1000, "rightTop")
  805.        
  806.         c={0:0}
  807.         c[1] = Connection(n1, n2, "rt")
  808.         c[2] = Connection(n2, n3, "lt")
  809.         c[3] = Connection(n3, n4, "lb")
  810.         c[4] = Connection(n4, n1, "rb")
  811.        
  812.         s={0:0}
  813.         s[1] = Stroke([c[1], c[2], c[3], c[4]])
  814.        
  815.        
  816.         #lower thingy
  817.         n5 = Node( (left+ individualWidth)*0.1 , xHeight*0.3, "rightTop")
  818.         n6 = Node( left , xHeight*0.15, "rightTop")
  819.         n7 = Node( (left + individualWidth)*0.2 , baseline, "rightTop")
  820.         n8 = Node( (left + individualWidth)*0.7 , baseline, "rightTop")
  821.         n9 = Node( left + individualWidth, descHeight / 2., "leftHorizontalMiddle")
  822.         n10 = Node( (left + individualWidth) / 2. , descHeight, "MiddleTopBowl")
  823.         n11 = Node(left, descHeight * 500/1000, "rightTopStartBowl")
  824.                
  825.         c[5] = Connection(n5, n6, "lt")
  826.         c[6] = Connection(n6, n7, "lb")
  827.         c[7] = Connection(n7, n8, "")
  828.         c[8] = Connection(n8, n9, "rt")
  829.         c[9] = Connection(n9, n10, "rb")
  830.         c[10] = Connection(n10, n11, "lb")
  831.        
  832.         s[2] = Stroke([c[5], c[6], c[7], c[8], c[9], c[10]])
  833.  
  834.         self[name] = Glyph([s[1],s[2]])
  835.         self[name].individualWidth = individualWidth  
  836.              
  837.        
  838.  
  839.     def _glyph_o(self, name):
  840.         individualWidth = globalWidth * 0.7
  841.  
  842.         n1 = Node(left + individualWidth, xHeight * 500/1000, "rightTopStartBowl")
  843.         n2 = Node( (left + individualWidth) / 2. , xHeight, "MiddleTopBowl")
  844.         n3 = Node( left, xHeight / 2., "leftHorizontalMiddle")
  845.         n4 = Node( (left + individualWidth) / 2. , baseline, "rightTop")
  846.        
  847.         c={0:0}
  848.         c[1] = Connection(n1, n2, "rt")
  849.         c[2] = Connection(n2, n3, "lt")
  850.         c[3] = Connection(n3, n4, "lb")
  851.         c[4] = Connection(n4, n1, "rb")
  852.        
  853.         s1 = Stroke([c[1], c[2], c[3], c[4]])
  854.  
  855.         self[name] = Glyph([s1])
  856.         self[name].individualWidth = individualWidth  
  857.  
  858.  
  859.     def _glyph_s1(self, name):
  860.         individualWidth = globalWidth * 0.6
  861.        
  862.         n={0:0}
  863.         n[1] = Node(left + individualWidth, rightOpenContourFromTopMinors, "right top bowl")
  864.         n[2] = Node( (left + individualWidth) / 2. , xHeight, "MiddleTopBowl")
  865.         n[3] = Node( left, xHeight*800/1000, "left top bowl middle")
  866.         n[4] = Node( left  + individualWidth*300/1000, xHeight*600/1000, "tob bowl, connection to straight middle part")
  867.         n[5] = Node( left  + individualWidth*700/1000, xHeight*500/1000, "lower bowl, connection to straight middle part")
  868.         n[6] = Node( left  + individualWidth, xHeight*250/1000, "right, verical middle of bottom bowl")
  869.         n[7] = Node( left  + individualWidth*500/1000, baseline, "bottom, horizontal middle")
  870.         n[8] = Node( left, rightOpenContourFromBottomMinors, "left, vertical middle of bottom bowl")
  871.        
  872.         c={0:0}
  873.         c[1] = Connection(n[1], n[2], "rt arm")
  874.         c[2] = Connection(n[2], n[3], "lt")
  875.         c[3] = Connection(n[3], n[4], "lb")
  876.         c[4] = Connection(n[4], n[5], "crossbar straight")
  877.         c[5] = Connection(n[5], n[6], "rt")
  878.         c[6] = Connection(n[6], n[7], "rb")
  879.         c[7] = Connection(n[7], n[8], "lb")
  880.            
  881.         s1 = Stroke([c[1], c[2], c[3], c[4], c[5], c[6], c[7] ])
  882.  
  883.         self[name] = Glyph([s1])
  884.         self[name].individualWidth = individualWidth              
  885.              
  886.  
  887.     def _glyph_z(self, name):
  888.         individualWidth = globalWidth * 0.55
  889.  
  890.         n1 = Node(left, xHeight, "rightTopStartBowl")
  891.         n2 = Node( (left + individualWidth), xHeight, "MiddleTopBowl")
  892.         n3 = Node( left, baseline, "leftHorizontalMiddle")
  893.         n4 = Node( (left + individualWidth) , baseline, "rightTop")
  894.  
  895.         c={0:0}        
  896.         c[1] = Connection(n1, n2)
  897.         c[2] = Connection(n2, n3)
  898.         c[3] = Connection(n3, n4)
  899.  
  900.         s1 = Stroke([c[1], c[2], c[3]])
  901.  
  902.         self[name] = Glyph([s1])
  903.         self[name].individualWidth = individualWidth
  904.  
  905.                        
  906.    
  907. class Path(object):
  908.     def paint(self,gc):
  909.         print "Path Drawn"
  910.  
  911.         f = Font()
  912.         f.createGlyphs()
  913.        
  914.         print "drawing font"
  915.         f.draw(gc)
  916.  
  917.  
  918.  
  919. class LetterPanel(scrolled.ScrolledPanel):
  920.  
  921.     def __init__(self,parent=None,id=-1):
  922.         scrolled.ScrolledPanel.__init__(self, parent, id, pos=(0, 0), size=(wx.DisplaySize()[0]-400, wx.DisplaySize()[1]-220), style = wx.TAB_TRAVERSAL|wx.SUNKEN_BORDER, name="LetterPanel" )
  923.         #wx.Panel.__init__(self, parent, id, pos=(0, 0), size=(wx.DisplaySize()[0]-400, wx.DisplaySize()[1]-200), style=wx.TAB_TRAVERSAL)
  924.        
  925.         self.SetAutoLayout(True)
  926.          
  927.         self.SetBackgroundColour("#FFFFFF")
  928.         self.Bind(wx.EVT_PAINT,self.onPaint)
  929.         self.SetDoubleBuffered(True)
  930.         self.path=Path()
  931.  
  932.     def onPaint(self, event):
  933.         event.Skip()
  934.  
  935.         dc = wx.PaintDC(self)
  936.         dc.BeginDrawing()
  937.         gc = wx.GraphicsContext.Create(dc)
  938.                
  939.         gc.Scale(ScaleFactor, -ScaleFactor)
  940.         gc.Translate(ScaleFactor+100, ScaleFactor - 800)
  941.  
  942.         gc.PushState()
  943.  
  944.         self.path.paint(gc)
  945.         gc.PopState()
  946.         dc.EndDrawing()
  947.        
  948.  
  949. class SliderPanel(wx.Panel):
  950.     """
  951.    class SliderPanel creates a panel with 2 sliders on it, inherits wx.Panel
  952.    putting your components/widgets on a panel gives additional versatility
  953.    """
  954.  
  955.     def __init__(self, parent, id):
  956.         # create a panel
  957.         #,pos=(0, wx.DisplaySize()[1]-400), size=(400,wx.DisplaySize()[1]-200)
  958.         wx.Panel.__init__(self, parent, id, pos=(wx.DisplaySize()[0]-400, 0), size=(400,wx.DisplaySize()[1]-200))
  959.         self.SetBackgroundColour("white")
  960.  
  961.         # wx.SL_VERTICAL  displays the slider vertically
  962.         # wx.SL_HORIZONTAL  displays the slider horizontally
  963.         # wx.SL_AUTOTICKS  displays tick marks
  964.         # wx.SL_LABELS  displays minimum, maximum and value labels
  965.  
  966.         # initial value = 100, min value = 0, max value = 400
  967.         self.xHeightSlider = wx.Slider(self, -1, 100, 0, 400, (10, 10), (300, 50), wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
  968.         self.slider2 = wx.Slider(self, -1, 100, 0, 400, (10, 50), (300, 50), wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS)
  969.         # respond to changes in slider position ...
  970.         self.Bind(wx.EVT_SLIDER, self.sliderUpdate)
  971.        
  972.     def sliderUpdate(self, event):
  973.         self.xHeightSliderValue = self.xHeightSlider.GetValue()
  974.         self.pos2 = self.slider2.GetValue()
  975.         str1 = "xHeightSliderValue = %d   pos2 = %d" % (self.xHeightSliderValue, self.pos2)
  976.         global xHeight  
  977.         global xHeightSave  
  978.         xHeight = xHeightSave * self.xHeightSliderValue/100.
  979.         global StemShoulderConnectionHeight
  980.         StemShoulderConnectionHeight = xHeight * 6/10.
  981.         global BottomBowlConnectionHeight
  982.         BottomBowlConnectionHeight = xHeight * 3/10.
  983.         global rightOpenContourFromTopCaps
  984.         rightOpenContourFromTopCaps = (1 - openess) * capHeight + baseline
  985.         global rightOpenContourFromTopCaps
  986.         rightOpenContourFromBottomCaps = openess * capHeight + baseline
  987.         global rightOpenContourFromTopMinors
  988.         rightOpenContourFromTopMinors = (1 - openess) * xHeight + baseline
  989.         global rightOpenContourFromBottomMinors
  990.         rightOpenContourFromBottomMinors = openess * xHeight + baseline
  991.  
  992.        
  993.         # display current slider positions in the frame's title
  994.         frame.SetTitle(str1)
  995.  
  996.  
  997. class LetterFrame(wx.Frame):
  998.     def __init__(self, parent, title):
  999.        
  1000.         #opening a window
  1001.         wx.Frame.__init__(self, parent, title=title, pos=(0, 22), size=(wx.DisplaySize()[0],wx.DisplaySize()[1]-200))
  1002.        
  1003.         self.LetterPanel=LetterPanel(self,-1)
  1004.         self.SliderPanel=SliderPanel(self,-1)
  1005.  
  1006.         self.Show(True)
  1007.  
  1008.  
  1009. app = wx.App(False)
  1010. frame = LetterFrame(None,"Type as Graph")
  1011. app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement