コード例 #1
0
    def table_execute(self):
        execute_prompt = QtGui.QMessageBox.question(
            self, 'Please confirm',
            'Are you sure that you want to create a new axis and append it to the font?',
            QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)

        if execute_prompt == QtGui.QMessageBox.Yes:
            # - Init
            tab_process_masters = self.tab_masters.getTable()

            # - Process masters: Duplicate
            print '\nSTEP:\t Duplicating masters. ' + '-' * 50
            for index, master_dict in tab_process_masters.iteritems():
                new_name = master_dict['Master Name']
                old_name = self.active_font.pMasters.names[int(index)]
                self.active_font.pMasters.add(
                    new_name, True, self.active_font.fl, old_name, True, False,
                    self.active_font.pMasters.locate(old_name))
                print 'ADD:\t Master: %s; Source: %s.' % (new_name, old_name)

            self.active_font.update()
            self.active_font.updateObject(
                self.active_font.fl,
                'Master table updated!\tFont: %s' % self.active_font.path)

            print '\nSTEP:\t Transforming masters. ' + '-' * 50
            for index, master_dict in tab_process_masters.iteritems():
                # - Init
                process_layer = master_dict['Master Name']
                process_angle = radians(float(master_dict['Angle']))
                process_shift = float(master_dict['Shift'])
                new_transform = QtGui.QTransform().shear(process_angle,
                                                         0.).translate(
                                                             process_shift, 0.)

                # - Process glyph: Transform layer
                for wGlyph in self.active_font.pGlyphs():

                    if wGlyph.name not in self.exclude_list:
                        wLayer = wGlyph.layer(process_layer)

                        if wLayer is not None:
                            # -- Transform at origin
                            wBBox = wLayer.boundingBox
                            wCenter = (wBBox.width() / 2 + wBBox.x(),
                                       wBBox.height() / 2 + wBBox.y())
                            transform_to_origin = QtGui.QTransform().translate(
                                -wCenter[0], -wCenter[1])
                            transform_from_origin = QtGui.QTransform(
                            ).translate(*wCenter)

                            # -- Apply Transform
                            wLayer.applyTransform(transform_to_origin)
                            wLayer.applyTransform(new_transform)
                            wLayer.applyTransform(transform_from_origin)

                            wGlyph.update()
                            #print 'MODIFY:\t Glyph: %s;\tLayer: %s.' %(wGlyph.name, process_layer)
                            #wGlyph.updateObject(wGlyph.fl, ' Glyph: %s; Layer: %s' %(wGlyph.name, process_layer))
                        else:
                            print 'WARN:\t Glyph: %s;\tLayer: %s.\tGlyph is missing Outline or Layer! ' % (
                                wGlyph.name, process_layer)
                    else:
                        print 'SKIP:\t Glyph: %s;\tExcluded from processing!' % wGlyph.name

            self.active_font.update()
            self.active_font.updateObject(
                self.active_font.fl,
                'Glyphs Processed;\tFont: %s.' % self.active_font.path)

            # - Process Font: Add new Axis
            print '\nSTEP:\t Building axis. ' + '-' * 50

            # -- Init
            new_axis = fl6.flAxis(self.cmb_axis_name.currentText,
                                  self.cmb_axis_short.currentText,
                                  self.cmb_axis_tag.currentText)

            # -- Process
            self.active_font.pSpace.add(new_axis)

            self.active_font.update()
            self.active_font.updateObject(
                self.active_font.fl, 'Axis: %s;\tFont: %s' %
                (self.cmb_axis_name.currentText, self.active_font.path))

            print '\nDONE:\t %s (%s)' % (app_name, app_version)

        else:
            print '\nABORT:\t %s (%s)\nWARN:\t No action taken!' % (
                app_name, app_version)
コード例 #2
0
ファイル: G-InsertElement.py プロジェクト: twardoch/TypeRig
	def process(self):
		# - Init
		self.active_font = pFont()
		current_glyph = pGlyph()
		getUniGlyph = lambda c: self.active_font.fl.findUnicode(ord(c)).name if all(['uni' not in c, '.' not in c, '_' not in c]) else c
		process_layers = [self.cmb_layer.currentText] if self.cmb_layer.currentText != 'All masters' else self.active_font.masters()	
		
		# - Parse input ------------------------------------------------------------
		for line in self.txt_editor.toPlainText().splitlines():
			# - Init
			process_glyphs = {}
			dst_store, src_store = [], []
			w_layer = syn_passlayer # Pass all commands - no specific layer selected

			if syn_insert in line and syn_comment not in line:
				init_parse = line.split(syn_insert)

				if len(init_parse) == 2: # No specific layer given
					left, rigth = init_parse
				
				elif len(init_parse) == 3: # Layer explicitly set
					w_layer, left, rigth = init_parse
					w_layer = w_layer.strip()
				
				else: 
					print 'ERROR:\tInvalid syntax! Skipping Line: %s\n' %line
					continue

				# - Set basics
				#dst_store = [getUniGlyph(name) if syn_currglyph not in name else current_glyph.name for name in rigth.split()]
				dst_store = [name if syn_currglyph not in name else current_glyph.name for name in rigth.split()]
				src_temp = [item.strip().split(syn_pos) for item in left.split()]
				src_temp = [[item[0], item[1].split(syn_transform)] if len(item) > 1 else item for item in src_temp]
				
				process_glyphs = {glyph:src_temp for glyph in dst_store}
			
			# - Process ------------------------------------------------------------
			for layer in process_layers:
				# - Process only specified layer or all
				if layer == w_layer or w_layer == syn_passlayer:
					
					for glyph_name, insert_command in process_glyphs.iteritems():
						# - Set working glyph
						w_glyph = self.active_font.glyph(glyph_name)

						# - Process insertions
						for insert in insert_command:
							if len(insert):
								# - Init
								# -- Shape retrieval and origin determination
								if len(insert[0]):
									if syn_bboxBL in insert[0]: # Shape origin: measured at Shapes BBox Bottom Left
										insert_name = insert[0].replace(syn_bboxBL, '')
										w_shape = self.active_font.findShape(insert_name, layer)
										insert_origin = Coord(w_shape.boundingBox.x(), w_shape.boundingBox.y())

									elif syn_bboxBR in insert[0]: # Shape origin: measured at Shapes BBox Bottom Right
										insert_name = insert[0].replace(syn_bboxBR, '')
										w_shape = self.active_font.findShape(insert_name, layer)
										insert_origin = Coord(w_shape.boundingBox.x() + w_shape.boundingBox.width(), w_shape.boundingBox.y())

									elif syn_bboxTL in insert[0]: # Shape origin: measured at Shapes BBox Top Left
										insert_name = insert[0].replace(syn_bboxTL, '')
										w_shape = self.active_font.findShape(insert_name, layer)
										insert_origin = Coord(w_shape.boundingBox.x(), w_shape.boundingBox.y() + w_shape.boundingBox.height())

									elif syn_bboxTR in insert[0]: # Shape origin: measured at Shapes BBox Top Right
										insert_name = insert[0].replace(syn_bboxTR, '')
										w_shape = self.active_font.findShape(insert_name, layer)
										insert_origin = Coord(w_shape.boundingBox.x() + w_shape.boundingBox.height(), w_shape.boundingBox.y() + w_shape.boundingBox.width())
									
									elif syn_label in insert[0]: # Shape origin: At source Glyphs Labeled Node
										insert_name, node_label = insert[0].split(syn_label)
										for glyph in self.active_font.pGlyphs():
											w_shape = glyph.findShape(insert_name, layer)
											
											if w_shape is not None:
												insert_origin = Coord(glyph.findNodeCoords(node_label, layer))
												break											

										
									else: # Shape origin: Not set
										insert_name = insert[0]
										w_shape = self.active_font.findShape(insert_name, layer)
										insert_origin = Coord(0,0)
								else:
									print 'ERROR:\tInvalid command! Skipping insertion command: %s\n' %insert
									continue

								# -- In-glyph positioning
								insert_position = None

								if len(insert) == 1: # Position: Simplest case no positional tags
									insert_coord = Coord((0,0))
								else:
									if len(insert[1]):
										w_bbox = w_glyph.getBounds(layer)

										if syn_currnode == insert[1][0]: # Position: Destination Glyphs Currently selected node
											position = w_glyph.selectedCoords(layer, applyTransform=True)
											insert_position = position[0] if len(position) else None

										elif syn_bboxBL == insert[1][0]: # Position: Destination Glyphs BBox Bottom Left
											insert_position = (w_bbox.x(), w_bbox.y())

										elif syn_bboxBR == insert[1][0]: # Position: Destination Glyphs BBox Bottom Right
											insert_position = (w_bbox.x() + w_bbox.width(), w_bbox.y())

										elif syn_bboxTL == insert[1][0]: # Position: Destination Glyphs BBox Top Left
											insert_position = (w_bbox.x(), w_bbox.y() + w_bbox.height())

										elif syn_bboxTR == insert[1][0]: # Position: Destination Glyphs BBox Top Right
											insert_position = (w_bbox.x() + w_bbox.height(), w_bbox.y() + w_bbox.width())
										
										elif syn_label in insert[1][0]: # Position: Destination Glyphs Labeled Node
											insert_position = w_glyph.findNodeCoords(insert[1][0].strip(syn_label), layer)

										elif syn_anchor in insert[1][0]: # Position: Destination Glyphs Anchor
											insert_position = w_glyph.findAnchorCoords(insert[1][0].strip(syn_anchor), layer)

										elif syn_coordsep in insert[1][0]: # Position: Destination Glyphs Coordinates
											insert_position = eval('(%s)' %insert[1][0])

										if len(insert[1]) > 1: # Positional correction in format (x,y)
											insert_correction = Coord(eval('(%s)' %insert[1][1]))
										else:
											insert_correction = Coord((0,0))

									if insert_position is None: 
										print 'ERROR:\tInvalid positional tags! Skipping insertion command: %s\n' %insert
										continue

									# - Set insertion coordinates	
									insert_coord = Coord(insert_position) + insert_correction
																	
								# - Insert and reposition
								# !!! A quirky way of adding shapes follows
								# !!! This is so very wrong - adding the shape twice and removing the first,
								# !!! forces FL to make a proper clone of the shape!?
								temp_shape = w_glyph.addShape(w_shape, layer) # A dummy that helps ??!
								new_shape = w_glyph.addShape(w_shape, layer)
								w_glyph.layer(layer).removeShape(temp_shape)

								new_shape.assignStyle(w_shape) # The only way to copy the 'non-spacing' property for now

								new_position = insert_coord - insert_origin
								new_transform = QtGui.QTransform(1, 0, 0, 0, 1, 0, new_position.x, new_position.y, 1)
								new_shape.transform = new_transform
								
								w_glyph.update()
								#print 'New: %s; Insert: %s; Origin: %s' %(new_position, insert_coord, insert_origin)

						# - Finish
						w_glyph.updateObject(w_glyph.fl, 'Shapes inserted to glyph: %s' %w_glyph.name, verbose=False)

			print 'DONE:\t Glyphs processed: %s' %' '.join(dst_store)
				
		print 'Done.'