summaryrefslogtreecommitdiff
path: root/misc/pylib/robofab/objects/objectsRF.pyx
blob: 50e1f13a730b7debd092ec958cb66077dcc74aaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
"""UFO for GlifLib"""

from robofab import RoboFabError, RoboFabWarning
from robofab.objects.objectsBase import BaseFont, BaseKerning, BaseGroups, BaseInfo, BaseFeatures, BaseLib,\
		BaseGlyph, BaseContour, BaseSegment, BasePoint, BaseBPoint, BaseAnchor, BaseGuide, BaseComponent, \
		relativeBCPIn, relativeBCPOut, absoluteBCPIn, absoluteBCPOut, _box,\
		_interpolate, _interpolatePt, roundPt, addPt,\
		MOVE, LINE, CORNER, CURVE, QCURVE, OFFCURVE,\
		BasePostScriptFontHintValues, postScriptHintDataLibKey, BasePostScriptGlyphHintValues

import os


__all__ = [	"CurrentFont", 
		"CurrentGlyph", 'OpenFont',
		'RFont', 'RGlyph', 'RContour',
		'RPoint', 'RBPoint', 'RAnchor',
		'RComponent'
		]



def CurrentFont():
	return None

def CurrentGlyph():
	return None

def OpenFont(path=None, note=None):
	"""Open a font from a path. If path is not given, present the user with a dialog."""
	if not note:
		note = 'select a .ufo directory'
	if not path:
		from robofab.interface.all.dialogs import GetFolder
		path = GetFolder(note)
	if path:
		try:
			return RFont(path)
		except OSError:
			from robofab.interface.all.dialogs import Message
			Message("%s is not a valid .UFO font. But considering it's all XML, why don't  you have a look inside with a simple text editor."%(path))
	else:
		return None
		
def NewFont(familyName=None, styleName=None):
	"""Make a new font"""
	new = RFont()
	if familyName is not None:
		new.info.familyName = familyName
	if styleName is not None:
		new.info.styleName = styleName
	return new
	
def AllFonts():
	"""AllFonts can't work in plain python usage. It's really up to some sort of application
	to keep track of which fonts are open."""
	raise NotImplementedError
	

class PostScriptFontHintValues(BasePostScriptFontHintValues):
	"""	Font level PostScript hints object for objectsRF usage.
		If there are values in the lib, use those.
		If there are no values in the lib, use defaults.
		
		The psHints attribute for objectsRF.RFont is basically just the
		data read from the Lib. When the object saves to UFO, the 
		hints are written back to the lib, which is then saved.
		
	"""
	
	def __init__(self, aFont=None, data=None):
		self.setParent(aFont)
		BasePostScriptFontHintValues.__init__(self)
		if aFont is not None:
			# in version 1, this data was stored in the lib
			# if it is still there, guess that it is correct
			# move it to font info and remove it from the lib.
			libData = aFont.lib.get(postScriptHintDataLibKey)
			if libData is not None:
				self.fromDict(libData)
				del libData[postScriptHintDataLibKey]
		if data is not None:
			self.fromDict(data)

def getPostScriptHintDataFromLib(aFont, fontLib):
	hintData = fontLib.get(postScriptHintDataLibKey)
	psh = PostScriptFontHintValues(aFont)
	psh.fromDict(hintData)
	return psh
	
class PostScriptGlyphHintValues(BasePostScriptGlyphHintValues):
	"""	Glyph level PostScript hints object for objectsRF usage.
		If there are values in the lib, use those.
		If there are no values in the lib, be empty.
		
	"""
	def __init__(self, aGlyph=None, data=None):
		# read the data from the glyph.lib, it won't be anywhere else
		BasePostScriptGlyphHintValues.__init__(self)
		if aGlyph is not None:
			self.setParent(aGlyph)
			self._loadFromLib(aGlyph.lib)
		if data is not None:
			self.fromDict(data)
			
			
class RFont(BaseFont):
	"""UFO font object which reads and writes glif, and keeps the data in memory in between.
	Bahviour:
		- comparable to Font
		- comparable to GlyphSet so that it can be passed to Glif widgets
	"""
	
	_title = "RoboFabFont"
	
	def __init__(self, path=None):
		BaseFont.__init__(self)
		if path is not None:
			self._path = os.path.normpath(os.path.abspath(path))
		else:
			self._path = None
		self._object = {}
		
		self._glyphSet = None
		self._scheduledForDeletion = []	# this is a place for storing glyphs that need to be removed when the font is saved
		
		self.kerning = RKerning()
		self.kerning.setParent(self)
		self.info = RInfo()
		self.info.setParent(self)
		self.features = RFeatures()
		self.features.setParent(self)
		self.groups = RGroups()
		self.groups.setParent(self)
		self.lib = RLib()
		self.lib.setParent(self)
		if path:
			self._loadData(path)
		else:
			self.psHints = PostScriptFontHintValues(self)
			self.psHints.setParent(self)
		
	def __setitem__(self, glyphName, glyph):
		"""Set a glyph at key."""
		self._object[glyphName] = glyph
	
	def __cmp__(self, other):
		"""Compare this font with another, compare if they refer to the same file."""
		if not hasattr(other, '_path'):
			return -1
		if self._object._path == other._object._path and self._object._path is not None: 
			return 0
		else:
			return -1
	
	def __len__(self):
		if self._glyphSet is None:
			return 0
		return len(self._glyphSet)
	
	def _loadData(self, path):
		from robofab.ufoLib import UFOReader
		reader = UFOReader(path)
		fontLib = reader.readLib()
		# info
		reader.readInfo(self.info)
		# kerning
		self.kerning.update(reader.readKerning())
		self.kerning.setChanged(False)
		# groups
		self.groups.update(reader.readGroups())
		# features
		if reader.formatVersion == 1:
			# migrate features from the lib
			features = []
			classes = fontLib.get("org.robofab.opentype.classes")
			if classes is not None:
				del fontLib["org.robofab.opentype.classes"]
				features.append(classes)
			splitFeatures = fontLib.get("org.robofab.opentype.features")
			if splitFeatures is not None:
				order = fontLib.get("org.robofab.opentype.featureorder")
				if order is None:
					order = splitFeatures.keys()
					order.sort()
				else:
					del fontLib["org.robofab.opentype.featureorder"]
				del fontLib["org.robofab.opentype.features"]
				for tag in order:
					oneFeature = splitFeatures.get(tag)
					if oneFeature is not None:
						features.append(oneFeature)
			features = "\n".join(features)
		else:
			features = reader.readFeatures()
		self.features.text = features
		# hint data
		self.psHints = PostScriptFontHintValues(self)
		if postScriptHintDataLibKey in fontLib:
			del fontLib[postScriptHintDataLibKey]
		# lib
		self.lib.update(fontLib)
		# glyphs
		self._glyphSet = reader.getGlyphSet()
		self._hasNotChanged(doGlyphs=False)

	def _loadGlyph(self, glyphName):
		"""Load a single glyph from the glyphSet, on request."""
		from robofab.pens.rfUFOPen import RFUFOPointPen
		g =  RGlyph()
		g.name = glyphName
		pen = RFUFOPointPen(g)
		self._glyphSet.readGlyph(glyphName=glyphName, glyphObject=g, pointPen=pen)
		g.setParent(self)
		g.psHints._loadFromLib(g.lib)
		self._object[glyphName] = g
		self._object[glyphName]._hasNotChanged()
		return g
		
	#def _prepareSaveDir(self, dir):
	#	path = os.path.join(dir, 'glyphs')
	#	if not os.path.exists(path):
	#		os.makedirs(path)

	def _hasNotChanged(self, doGlyphs=True):
		#set the changed state of the font
		if doGlyphs:
			for glyph in self:
				glyph._hasNotChanged()
		self.setChanged(False)
	
	#
	# attributes
	#
	
	def _get_path(self):
		return self._path
	
	path = property(_get_path, doc="path of the font")
		
	#
	# methods for imitating GlyphSet?
	#
			
	def keys(self):
		# the keys are the superset of self._objects.keys() and
		# self._glyphSet.keys(), minus self._scheduledForDeletion
		keys = self._object.keys()
		if self._glyphSet is not None:
			keys.extend(self._glyphSet.keys())
		d = dict()
		for glyphName in keys:
			d[glyphName] = None
		for glyphName in self._scheduledForDeletion:
			if glyphName in d:
				del d[glyphName]
		return d.keys()

	def has_key(self, glyphName):
		# XXX ditto, see above.
		if self._glyphSet is not None:
			hasGlyph = glyphName in self._object or glyphName in self._glyphSet
		else:
			hasGlyph = glyphName in self._object
		return hasGlyph and not glyphName in self._scheduledForDeletion
	
	__contains__ = has_key
	
	def getWidth(self, glyphName):
		if self._object.has_key(glyphName):
			return self._object[glyphName].width
		raise IndexError		# or return None?
		
	def getReverseComponentMapping(self):
		"""
		Get a reversed map of component references in the font.
		{
		'A' : ['Aacute', 'Aring']
		'acute' : ['Aacute']
		'ring' : ['Aring']
		etc.
		}
		"""
		# a NON-REVERESED map is stored in the lib.
		# this is done because a reveresed map could
		# contain faulty data. for example: "Aacute" contains
		# a component that references "A". Glyph "Aacute" is
		# then deleted. The reverse map would still say that
		# "A" is referenced by "Aacute" even though the
		# glyph has been deleted. So, the stored lib works like this:
		# {
		# 'Aacute' : [
		#		# the last known mod time of the GLIF
		#		1098706856.75,
		#		# component references in a glyph
		#		['A', 'acute']
		#		]
		# }
		import time
		import os
		import re
		componentSearch_RE = re.compile(
			"<component\s+"		# <component
			"[^>]*?"			# anything EXCEPT >
			"base\s*=\s*[\"\']"		# base="
			"(.*?)"			# foo
			"[\"\']"			# "
			)
		rightNow = time.time()
		libKey = "org.robofab.componentMapping"
		previousMap = None
		if self.lib.has_key(libKey):
			previousMap = self.lib[libKey]
		basicMap = {}
		reverseMap = {}
		for glyphName in self.keys():
			componentsToMap = None
			modTime = None
			# get the previous bits of data
			previousModTime = None
			previousList = None
			if previousMap is not None and previousMap.has_key(glyphName):
				previousModTime, previousList = previousMap[glyphName]
			# the glyph has been loaded.
			# simply get the components from it.
			if self._object.has_key(glyphName):
				componentsToMap = [component.baseGlyph for component in self._object[glyphName].components]
			# the glyph has not been loaded.
			else:
				glyphPath = os.path.join(self._glyphSet.dirName, self._glyphSet.contents[glyphName])
				scanGlyph = True
				# test the modified time of the GLIF
				fileModTime = os.path.getmtime(glyphPath)
				if previousModTime is not None and fileModTime == previousModTime:
					# the GLIF almost* certianly has not changed.
					# *theoretically, a user could replace a GLIF
					# with another GLIF that has precisely the same
					# mod time. 
					scanGlyph = False
					componentsToMap = previousList
					modTime = previousModTime
				else:
					# the GLIF is different
					modTime = fileModTime
				if scanGlyph:
					# use regex to extract component
					# base glyphs from the file
					f = open(glyphPath, 'rb')
					data = f.read()
					f.close()
					componentsToMap = componentSearch_RE.findall(data)
			if componentsToMap is not None:
				# store the non-reversed map
				basicMap[glyphName] = (modTime, componentsToMap)
				# reverse the map for the user
				if componentsToMap:
					for baseGlyphName in componentsToMap:
						if not reverseMap.has_key(baseGlyphName):
							reverseMap[baseGlyphName] = []
						reverseMap[baseGlyphName].append(glyphName)
				# if a glyph has been loaded, we do not store data about it in the lib.
				# this is done becuase there is not way to determine the proper mod time
				# for a loaded glyph.
				if modTime is None:
					del basicMap[glyphName]
		# store the map in the lib for re-use
		self.lib[libKey] = basicMap
		return reverseMap
		

	def save(self, destDir=None, doProgress=False, formatVersion=2):
		"""Save the Font in UFO format."""
		# XXX note that when doing "save as" by specifying the destDir argument
		# _all_ glyphs get loaded into memory. This could be optimized by either
		# copying those .glif files that have not been edited or (not sure how
		# well that would work) by simply clearing out self._objects after the
		# save.
		from robofab.ufoLib import UFOWriter
		from robofab.tools.fontlabFeatureSplitter import splitFeaturesForFontLab
		# if no destination is given, or if
		# the given destination is the current
		# path, this is not a save as operation
		if destDir is None or destDir == self._path:
			saveAs = False
			destDir = self._path
		else:
			saveAs = True
		# start a progress bar
		nonGlyphCount = 5
		bar = None
		if doProgress:
			from robofab.interface.all.dialogs import ProgressBar
			bar = ProgressBar("Exporting UFO", nonGlyphCount + len(self._object.keys()))
		# write
		writer = UFOWriter(destDir, formatVersion=formatVersion)
		try:
			# make a shallow copy of the lib. stuff may be added to it.
			fontLib = dict(self.lib)
			# info
			if bar:
				bar.label("Saving info...")
			writer.writeInfo(self.info)
			if bar:
				bar.tick()
			# kerning
			if self.kerning.changed or saveAs:
				if bar:
					bar.label("Saving kerning...")
				writer.writeKerning(self.kerning.asDict())
				if bar:
					bar.tick()
			# groups
			if bar:
				bar.label("Saving groups...")
			writer.writeGroups(self.groups)
			if bar:
				bar.tick()
			# features
			if bar:
				bar.label("Saving features...")
			features = self.features.text
			if features is None:
				features = ""
			if formatVersion == 2:
				writer.writeFeatures(features)
			elif formatVersion == 1:
				classes, features = splitFeaturesForFontLab(features)
				if classes:
					fontLib["org.robofab.opentype.classes"] = classes.strip() + "\n"
				if features:
					featureDict = {}
					for featureName, featureText in features:
						featureDict[featureName] = featureText.strip() + "\n"
					fontLib["org.robofab.opentype.features"] = featureDict
					fontLib["org.robofab.opentype.featureorder"] = [featureName for featureName, featureText in features]
			if bar:
				bar.tick()
			# lib
			if formatVersion == 1:
				fontLib[postScriptHintDataLibKey] = self.psHints.asDict()
			if bar:
				bar.label("Saving lib...")
			writer.writeLib(fontLib)
			if bar:
				bar.tick()
			# glyphs
			glyphNameToFileNameFunc = self.getGlyphNameToFileNameFunc()

			glyphSet = writer.getGlyphSet(glyphNameToFileNameFunc)
			if len(self._scheduledForDeletion) != 0:
				if bar:
					bar.label("Removing deleted glyphs...")
				for glyphName in self._scheduledForDeletion:
					if glyphSet.has_key(glyphName):
						glyphSet.deleteGlyph(glyphName)
				if bar:
					bar.tick()
			if bar:
				bar.label("Saving glyphs...")
			count = nonGlyphCount
			if saveAs:
				glyphNames = self.keys()
			else:
				glyphNames = self._object.keys()
			for glyphName in glyphNames:
				glyph = self[glyphName]
				glyph.psHints._saveToLib(glyph.lib)
				glyph._saveToGlyphSet(glyphSet, glyphName=glyphName, force=saveAs)
				if bar and not count % 10:
					bar.tick(count)
				count = count + 1
			glyphSet.writeContents()
			self._glyphSet = glyphSet
		# only blindly stop if the user says to
		except KeyboardInterrupt:
			bar.close()
			bar = None
		# kill the progress bar
		if bar:
			bar.close()
		# reset internal stuff
		self._path = destDir
		self._scheduledForDeletion = []
		self.setChanged(False)

	def newGlyph(self, glyphName, clear=True):
		"""Make a new glyph with glyphName
		if the glyph exists and clear=True clear the glyph"""
		if clear and glyphName in self:
			g = self[glyphName]
			g.clear()
			w = self.info.postscriptDefaultWidthX
			if w is None:
				w = 0
			g.width = w
			return g
		g = RGlyph()
		g.setParent(self)
		g.name = glyphName
		w = self.info.postscriptDefaultWidthX
		if w is None:
			w = 0
		g.width = w
		g._hasChanged()
		self._object[glyphName] = g
		# is the user adding a glyph that has the same
		# name as one that was deleted earlier?
		if glyphName in self._scheduledForDeletion:
			self._scheduledForDeletion.remove(glyphName)
		return self.getGlyph(glyphName)
		
	def insertGlyph(self, glyph, name=None):
		"""returns a new glyph that has been inserted into the font"""
		if name is None:
			name = glyph.name
		glyph = glyph.copy()
		glyph.name = name
		glyph.setParent(self)
		glyph._hasChanged()
		self._object[name] = glyph
		# is the user adding a glyph that has the same
		# name as one that was deleted earlier?
		if name in self._scheduledForDeletion:
			self._scheduledForDeletion.remove(name)
		return self.getGlyph(name)
		
	def removeGlyph(self, glyphName):
		"""remove a glyph from the font"""
		# XXX! Potential issue with removing glyphs.
		# if a glyph is removed from a font, but it is still referenced
		# by a component, it will give pens some trouble.
		# where does the resposibility for catching this fall?
		# the removeGlyph method? the addComponent method
		# of the various pens? somewhere else? hm... tricky.
		#
		#we won't actually remove it, we will just store it for removal
		# but only if the glyph does exist
		if self.has_key(glyphName) and glyphName not in self._scheduledForDeletion:
			self._scheduledForDeletion.append(glyphName)
		# now delete the object
		if self._object.has_key(glyphName):
			del self._object[glyphName]
		self._hasChanged()
		
	def getGlyph(self, glyphName):
		# XXX getGlyph may have to become private, to avoid duplication
		# with __getitem__
		n = None
		if self._object.has_key(glyphName):
			# have we served this glyph before? it should be in _object
			n = self._object[glyphName]
		else:
			# haven't served it before, is it in the glyphSet then?
			if self._glyphSet is not None and glyphName in self._glyphSet:
				# yes, read the .glif file from disk
				n = self._loadGlyph(glyphName)
		if n is None:
			raise KeyError, glyphName
		return n


class RGlyph(BaseGlyph):
	
	_title = "RGlyph"
	
	def __init__(self):
		BaseGlyph.__init__(self)
		self.contours = []
		self.components = []
		self.anchors = []
		self._unicodes = []
		self.width = 0
		self.note = None
		self._name = "Unnamed Glyph"
		self.selected = False
		self._properties = None
		self._lib = RLib()
		self._lib.setParent(self)
		self.psHints = PostScriptGlyphHintValues()
		self.psHints.setParent(self)

	def __len__(self):
		return len(self.contours) 

	def __getitem__(self, index):
		if index < len(self.contours):
			return self.contours[index]
		raise IndexError
	
	def _hasNotChanged(self):
		for contour in self.contours:
			contour.setChanged(False)
			for segment in contour.segments:
				segment.setChanged(False)
				for point in segment.points:
					point.setChanged(False)
		for component in self.components:
			component.setChanged(False)
		for anchor in self.anchors:
			anchor.setChanged(False)
		self.setChanged(False)
	
	#
	# attributes
	#
	
	def _get_lib(self):
		return self._lib
	
	def _set_lib(self, obj):
		self._lib.clear()
		self._lib.update(obj)
	
	lib = property(_get_lib, _set_lib)
	
	def _get_name(self):
		return self._name
	
	def _set_name(self, value):
		prevName = self._name
		newName = value
		if newName == prevName:
			return
		self._name = newName
		self.setChanged(True)
		font = self.getParent()
		if font is not None:
			# but, this glyph could be linked to a
			# FontLab font, because objectsFL.RGlyph.copy()
			# creates an objectsRF.RGlyph with the parent
			# set to an objectsFL.RFont object. so, check to see
			# if this is a legitimate RFont before trying to 
			# do the objectsRF.RFont glyph name change
			if isinstance(font, RFont):
				font._object[newName] = self
				# is the user changing a glyph's name to the
				# name of a glyph that was deleted earlier?
				if newName in font._scheduledForDeletion:
					font._scheduledForDeletion.remove(newName)
				font.removeGlyph(prevName)
	
	name = property(_get_name, _set_name)
	
	def _get_unicodes(self):
		return self._unicodes
	
	def _set_unicodes(self, value):
		if not isinstance(value, list):
			raise RoboFabError, "unicodes must be a list"
		self._unicodes = value
		self._hasChanged()
			
	unicodes = property(_get_unicodes, _set_unicodes, doc="all unicode values for the glyph")
	
	def _get_unicode(self):
		if len(self._unicodes) == 0:
			return None
		return self._unicodes[0]
	
	def _set_unicode(self, value):
		uni = self._unicodes
		if value is not None:
			if value not in uni:
				self.unicodes.insert(0, value)
			elif uni.index(value) != 0:
				uni.insert(0, uni.pop(uni.index(value)))
				self.unicodes = uni
		
	unicode = property(_get_unicode, _set_unicode, doc="first unicode value for the glyph")
	
	def getPointPen(self):
		from robofab.pens.rfUFOPen import RFUFOPointPen
		return RFUFOPointPen(self)

	def appendComponent(self, baseGlyph, offset=(0, 0), scale=(1, 1)):
		"""append a component to the glyph"""
		new = RComponent(baseGlyph, offset, scale)
		new.setParent(self)
		self.components.append(new)
		self._hasChanged()
		
	def appendAnchor(self, name, position, mark=None):
		"""append an anchor to the glyph"""
		new = RAnchor(name, position, mark)
		new.setParent(self)
		self.anchors.append(new)
		self._hasChanged()
	
	def removeContour(self, index):
		"""remove  a specific contour from the glyph"""
		del self.contours[index]
		self._hasChanged()
		
	def removeAnchor(self, anchor):
		"""remove  a specific anchor from the glyph"""
		del self.anchors[anchor.index]
		self._hasChanged()
	
	def removeComponent(self, component):
		"""remove  a specific component from the glyph"""
		del self.components[component.index]
		self._hasChanged()
			
	def center(self, padding=None):
		"""Equalise sidebearings, set to padding if wanted."""
		left = self.leftMargin
		right = self.rightMargin
		if padding:
			e_left = e_right = padding
		else:
			e_left = (left + right)/2
			e_right = (left + right) - e_left
		self.leftMargin = e_left
		self.rightMargin = e_right
	
	def decompose(self):
		"""Decompose all components"""
		for i in range(len(self.components)):
			self.components[-1].decompose()
		self._hasChanged()
			
	def clear(self, contours=True, components=True, anchors=True, guides=True):
		"""Clear all items marked as True from the glyph"""
		if contours:
			self.clearContours()
		if components:
			self.clearComponents()
		if anchors:
			self.clearAnchors()
		if guides:
			self.clearHGuides()
			self.clearVGuides()
	
	def clearContours(self):
		"""clear all contours"""
		self.contours = []
		self._hasChanged()
	
	def clearComponents(self):
		"""clear all components"""
		self.components = []
		self._hasChanged()
		
	def clearAnchors(self):
		"""clear all anchors"""
		self.anchors = []
		self._hasChanged()
		
	def clearHGuides(self):
		"""clear all horizontal guides"""
		self.hGuides = []
		self._hasChanged()
	
	def clearVGuides(self):
		"""clear all vertical guides"""
		self.vGuides = []
		self._hasChanged()
		
	def getAnchors(self):
		return self.anchors
	
	def getComponents(self):
		return self.components
	
	#
	# stuff related to Glyph Properties
	#
	


class RContour(BaseContour):
	
	_title = "RoboFabContour"
	
	def __init__(self, object=None):
		#BaseContour.__init__(self)
		self.segments = []
		self.selected = False
		
	def __len__(self):
		return len(self.segments) 

	def __getitem__(self, index):
		if index < len(self.segments):
			return self.segments[index]
		raise IndexError
		
	def _get_index(self):
		return self.getParent().contours.index(self)
		
	def _set_index(self, index):
		ogIndex = self.index
		if index != ogIndex:
			contourList = self.getParent().contours
			contourList.insert(index, contourList.pop(ogIndex))
			
	
	index = property(_get_index, _set_index, doc="index of the contour")
	
	def _get_points(self):
		points = []
		for segment in self.segments:
			for point in segment.points:
				points.append(point)
		return points
	
	points = property(_get_points, doc="view the contour as a list of points")
	
	def _get_bPoints(self):
		bPoints = []
		for segment in self.segments:
			segType = segment.type
			if segType == MOVE:
				bType = CORNER
			elif segType == LINE:
				bType = CORNER
			elif segType == CURVE:
				if segment.smooth:
					bType = CURVE
				else:
					bType = CORNER
			else:
				raise RoboFabError, "encountered unknown segment type"
			b = RBPoint()
			b.setParent(segment)
			bPoints.append(b)
		return bPoints

	bPoints = property(_get_bPoints, doc="view the contour as a list of bPoints")
	
	def appendSegment(self, segmentType, points, smooth=False):
		"""append a segment to the contour"""
		segment = self.insertSegment(index=len(self.segments), segmentType=segmentType, points=points, smooth=smooth)
		return segment
		
	def insertSegment(self, index, segmentType, points, smooth=False):
		"""insert a segment into the contour"""
		segment = RSegment(segmentType, points, smooth)
		segment.setParent(self)
		self.segments.insert(index, segment)
		self._hasChanged()
		return segment
		
	def removeSegment(self, index):
		"""remove a segment from the contour"""
		del self.segments[index]
		self._hasChanged()
				
	def reverseContour(self):
		"""reverse the contour"""
		from robofab.pens.reverseContourPointPen import ReverseContourPointPen
		index = self.index
		glyph = self.getParent()
		pen = glyph.getPointPen()
		reversePen = ReverseContourPointPen(pen)
		self.drawPoints(reversePen)
		# we've drawn the reversed contour onto our parent glyph,
		# so it sits at the end of the contours list:
		newContour = glyph.contours.pop(-1)
		for segment in newContour.segments:
			segment.setParent(self)
		self.segments = newContour.segments
		self._hasChanged()
	
	def setStartSegment(self, segmentIndex):
		"""set the first segment on the contour"""
		# this obviously does not support open contours
		if len(self.segments) < 2:
			return
		if segmentIndex == 0:
			return
		if segmentIndex > len(self.segments)-1:
			raise IndexError, 'segment index not in segments list'
		oldStart = self.segments[0]
		oldLast = self.segments[-1]
		 #check to see if the contour ended with a curve on top of the move
		 #if we find one delete it,
		if oldLast.type == CURVE or oldLast.type == QCURVE:
			startOn = oldStart.onCurve
			lastOn = oldLast.onCurve
			if startOn.x == lastOn.x and startOn.y == lastOn.y:
				del self.segments[0]
				# since we deleted the first contour, the segmentIndex needs to shift
				segmentIndex = segmentIndex - 1
		# if we DO have a move left over, we need to convert it to a line
		if self.segments[0].type == MOVE:
			self.segments[0].type = LINE
		# slice up the segments and reassign them to the contour
		segments = self.segments[segmentIndex:]
		self.segments = segments + self.segments[:segmentIndex]
		# now, draw the contour onto the parent glyph
		glyph = self.getParent()
		pen = glyph.getPointPen()
		self.drawPoints(pen)
		# we've drawn the new contour onto our parent glyph,
		# so it sits at the end of the contours list:
		newContour = glyph.contours.pop(-1)
		for segment in newContour.segments:
			segment.setParent(self)
		self.segments = newContour.segments
		self._hasChanged()


class RSegment(BaseSegment):
	
	_title = "RoboFabSegment"
	
	def __init__(self, segmentType=None, points=[], smooth=False):
		BaseSegment.__init__(self)
		self.selected = False
		self.points = []
		self.smooth = smooth
		if points:
			#the points in the segment should be RPoints, so create those objects
			for point in points[:-1]:
				x, y = point
				p = RPoint(x, y, pointType=OFFCURVE)
				p.setParent(self)
				self.points.append(p)
			aX, aY = points[-1]
			p = RPoint(aX, aY, segmentType)
			p.setParent(self)
			self.points.append(p)
		
	def _get_type(self):
		return self.points[-1].type
	
	def _set_type(self, pointType):
		onCurve = self.points[-1]
		ocType = onCurve.type
		if ocType == pointType:
			return
		#we are converting a cubic line into a cubic curve
		if pointType == CURVE and  ocType == LINE:
			onCurve.type = pointType
			parent = self.getParent()
			prev = parent._prevSegment(self.index)
			p1 = RPoint(prev.onCurve.x, prev.onCurve.y, pointType=OFFCURVE)
			p1.setParent(self)
			p2 = RPoint(onCurve.x, onCurve.y, pointType=OFFCURVE)
			p2.setParent(self)
			self.points.insert(0, p2)
			self.points.insert(0, p1)
		#we are converting a cubic move to a curve
		elif pointType == CURVE and ocType == MOVE:
			onCurve.type = pointType
			parent = self.getParent()
			prev = parent._prevSegment(self.index)
			p1 = RPoint(prev.onCurve.x, prev.onCurve.y, pointType=OFFCURVE)
			p1.setParent(self)
			p2 = RPoint(onCurve.x, onCurve.y, pointType=OFFCURVE)
			p2.setParent(self)
			self.points.insert(0, p2)
			self.points.insert(0, p1)
		#we are converting a quad curve to a cubic curve
		elif pointType == CURVE and ocType == QCURVE:
			onCurve.type == CURVE
		#we are converting a cubic curve into a cubic line
		elif pointType == LINE and ocType == CURVE:
			p = self.points.pop(-1)
			self.points = [p]
			onCurve.type = pointType
			self.smooth = False
		#we are converting a cubic move to a line
		elif pointType == LINE and ocType == MOVE:
			onCurve.type = pointType
		#we are converting a quad curve to a line:
		elif pointType == LINE and ocType == QCURVE:
			p = self.points.pop(-1)
			self.points = [p]
			onCurve.type = pointType
			self.smooth = False	
		# we are converting to a quad curve where just about anything is legal
		elif pointType == QCURVE:
			onCurve.type = pointType
		else:
			raise RoboFabError, 'unknown segment type'
			
	type = property(_get_type, _set_type, doc="type of the segment")
	
	def _get_index(self):
		return self.getParent().segments.index(self)
		
	index = property(_get_index, doc="index of the segment")
	
	def insertPoint(self, index, pointType, point):
		x, y = point
		p = RPoint(x, y, pointType=pointType)
		p.setParent(self)
		self.points.insert(index, p)
		self._hasChanged()
	
	def removePoint(self, index):
		del self.points[index]
		self._hasChanged()
		

class RBPoint(BaseBPoint):
	
	_title = "RoboFabBPoint"
		
	def _setAnchorChanged(self, value):
		self._anchorPoint.setChanged(value)
	
	def _setNextChanged(self, value):
		self._nextOnCurve.setChanged(value)	
		
	def _get__parentSegment(self):
		return self.getParent()
		
	_parentSegment = property(_get__parentSegment, doc="")
	
	def _get__nextOnCurve(self):
		pSeg = self._parentSegment
		contour = pSeg.getParent()
		#could this potentially return an incorrect index? say, if two segments are exactly the same?
		return contour.segments[(contour.segments.index(pSeg) + 1) % len(contour.segments)]
	
	_nextOnCurve = property(_get__nextOnCurve, doc="")
	
	def _get_index(self):
		return self._parentSegment.index
	
	index = property(_get_index, doc="index of the bPoint on the contour")


class RPoint(BasePoint):
	
	_title = "RoboFabPoint"
	
	def __init__(self, x=0, y=0, pointType=None, name=None):
		self.selected = False
		self._type = pointType
		self._x = x
		self._y = y
		self._name = name
		
	def _get_x(self):
		return self._x
		
	def _set_x(self, value):
		self._x = value
		self._hasChanged()
	
	x = property(_get_x, _set_x, doc="")

	def _get_y(self):
		return self._y
	
	def _set_y(self, value):
		self._y = value
		self._hasChanged()

	y = property(_get_y, _set_y, doc="")
	
	def _get_type(self):
		return self._type
	
	def _set_type(self, value):
		self._type = value
		self._hasChanged()

	type = property(_get_type, _set_type, doc="")
	
	def _get_name(self):
		return self._name
	
	def _set_name(self, value):
		self._name = value
		self._hasChanged()

	name = property(_get_name, _set_name, doc="")

		
class RAnchor(BaseAnchor):
	
	_title = "RoboFabAnchor"
	
	def __init__(self, name=None, position=None, mark=None):
		BaseAnchor.__init__(self)
		self.selected = False
		self.name = name
		if position is None:
			self.x = self.y = None
		else:
			self.x, self.y = position
		self.mark = mark
		
	def _get_index(self):
		if self.getParent() is None: return None
		return self.getParent().anchors.index(self)
	
	index = property(_get_index, doc="index of the anchor")
	
	def _get_position(self):
		return (self.x, self.y)
	
	def _set_position(self, value):
		self.x = value[0]
		self.y = value[1]
		self._hasChanged()
	
	position = property(_get_position, _set_position, doc="position of the anchor")
	
	def move(self, pt):
		"""Move the anchor"""
		self.x = self.x + pt[0]
		self.y = self.y + pt[0]
		self._hasChanged()

		
class RComponent(BaseComponent):
	
	_title = "RoboFabComponent"
	
	def __init__(self, baseGlyphName=None, offset=(0,0), scale=(1,1), transform=None):
		BaseComponent.__init__(self)
		self.selected = False
		self._baseGlyph = baseGlyphName
		self._offset = offset
		self._scale = scale
		if transform is None:
			xx, yy = scale
			dx, dy = offset
			self.transformation = (xx, 0, 0, yy, dx, dy)
		else:
			self.transformation = transform
		
	def _get_index(self):
		if self.getParent() is None: return None
		return self.getParent().components.index(self)
		
	index = property(_get_index, doc="index of the component")
	
	def _get_baseGlyph(self):
		return self._baseGlyph
		
	def _set_baseGlyph(self, glyphName):
		# XXXX needs to be implemented in objectsFL for symmetricity's sake. Eventually.
		self._baseGlyph = glyphName
		self._hasChanged()
		
	baseGlyph = property(_get_baseGlyph, _set_baseGlyph, doc="")

	def _get_offset(self):
		""" Get the offset component of the transformation.="""
		(xx, xy, yx, yy, dx, dy) = self._transformation
		return dx, dy
	
	def _set_offset(self, value):
		""" Set the offset component of the transformation."""
		(xx, xy, yx, yy, dx, dy) = self._transformation
		self._transformation = (xx, xy, yx, yy, value[0], value[1])
		self._hasChanged()
		
	offset = property(_get_offset, _set_offset, doc="the offset of the component")

	def _get_scale(self):
		""" Return the scale components of the transformation."""
		(xx, xy, yx, yy, dx, dy) = self._transformation
		return xx, yy
	
	def _set_scale(self, scale):
		""" Set the scale component of the transformation.
			Note: setting this value effectively makes the xy and yx values meaningless.
			We're assuming that if you're setting the xy and yx values, you will use
			the transformation attribute rather than the scale and offset attributes.
		"""
		xScale, yScale = scale
		(xx, xy, yx, yy, dx, dy) = self._transformation
		self._transformation = (xScale, xy, yx, yScale, dx, dy)
		self._hasChanged()
		
	scale = property(_get_scale, _set_scale, doc="the scale of the component")
	
	def _get_transformation(self):
		return self._transformation
	
	def _set_transformation(self, transformation):
		assert len(transformation)==6, "Transformation matrix must have 6 values"
		self._transformation = transformation
	
	transformation = property(_get_transformation, _set_transformation, doc="the transformation matrix of the component")
		
	def move(self, pt):
		"""Move the component"""
		(xx, xy, yx, yy, dx, dy) = self._transformation
		self._transformation = (xx, xy, yx, yy, dx+pt[0], dy+pt[1])
		self._hasChanged()
	
	def decompose(self):
		"""Decompose the component"""
		baseGlyphName = self.baseGlyph
		parentGlyph = self.getParent()
		# if there is no parent glyph, there is nothing to decompose to
		if baseGlyphName is not None and parentGlyph is not None:
			parentFont = parentGlyph.getParent()
			# we must have a parent glyph with the baseGlyph
			# if not, we will simply remove the component from
			# the parent glyph thereby decomposing the component
			# to nothing.
			if parentFont is not None and parentFont.has_key(baseGlyphName):
				from robofab.pens.adapterPens import TransformPointPen
				baseGlyph = parentFont[baseGlyphName]
				for contour in baseGlyph.contours:
					pointPen = parentGlyph.getPointPen()
					transPen = TransformPointPen(pointPen, self._transformation)
					contour.drawPoints(transPen)
			parentGlyph.components.remove(self)
	
		
class RKerning(BaseKerning):
	
	_title = "RoboFabKerning"

		
class RGroups(BaseGroups):
	
	_title = "RoboFabGroups"
	
class RLib(BaseLib):
	
	_title = "RoboFabLib"

		
class RInfo(BaseInfo):
	
	_title = "RoboFabFontInfo"

class RFeatures(BaseFeatures):

	_title = "RoboFabFeatures"