Пример #1
0
def copy_groups_class_kerning(self, _stream="Downstream"):
    #
    if _stream == "Downstream":
        #
        print('EFO: Copying Kerning Class Groups')
        #
        EFO_groups_dir = os.path.join(self._in, self.EFO_groups_dir)
        EFO_class_kerning_groups_file = os.path.join(EFO_groups_dir,
                                                     'kerning.plist')
        UFO_class_kerning_groups_file = os.path.join(
            self.current_font_instance_directory, 'groups.plist')
        #
        copyfile(EFO_class_kerning_groups_file, UFO_class_kerning_groups_file)
        #
        print('\tCopied Kerning Class Groups: ', UFO_class_kerning_groups_file)
    else:
        #
        groups_dir = os.path.join(*(self.new_efo_dir, self.EFO_groups_dir))
        #
        generic_tools.make_dir(groups_dir)
        #
        UFO_class_kerning_groups_file = os.path.join(self.current_source_ufo,
                                                     "groups.plist")
        EFO_class_kerning_groups_file = os.path.join(*(self.new_efo_dir,
                                                       self.EFO_groups_dir,
                                                       "kerning.plist"))
        #
        copyfile(UFO_class_kerning_groups_file, EFO_class_kerning_groups_file)
Пример #2
0
def generate_contents_plist(self):
    #
    print('EFO: Generating UFO GLIF contents PLIST')
    #
    contents = {}
    #
    glyphs_directory = os.path.join(self.current_font_instance_directory,
                                    "glyphs")
    #
    generic_tools.make_dir(glyphs_directory)
    #
    UFO_contents_plist_file = os.path.join(glyphs_directory, 'contents.plist')
    #
    xmlTree = ET.parse(os.path.join(self._in, 'glyphlib.xml'))
    #
    for elem in xmlTree.iter():
        #
        if elem.tag == "glyph":
            #
            contents[elem.attrib.get(
                "name")] = elem.attrib.get("glif") + '.glif'
            #
    #
    contents_plist = generic_tools.json_to_plist(contents)
    #
    generic_tools.write_to_file(UFO_contents_plist_file, contents_plist)
    #
    print('\tGenerated UFO contents PLIST from glyphlib.xml: ',
          UFO_contents_plist_file)
Пример #3
0
def copy_glif_files(self, _stream):
    #
    if _stream == "Downstream":
        #
        print('EFO: Copying EFO GLIF Files to UFOs')
        #
        #
        EFO_glyphs_dir = os.path.join(self._in, self.EFO_glyphs_dir)
        #
        EFO_current_glyphs = os.path.join(EFO_glyphs_dir,
                                          self.current_font_file_name)
        UFO_glyphs_dir = os.path.join(self.current_font_instance_directory,
                                      'glyphs')
        #
        copy_tree(EFO_current_glyphs, UFO_glyphs_dir)
        #
        print('\tCopied GLIFS from EFO to UFO: ', UFO_glyphs_dir)
        #
    elif _stream == "Upstream":
        #
        #
        print('EFO: Copying UFO GLIF Files to EFO:',
              self.current_font_file_name)
        #
        if self._from_components:
            # Make Glyphs Directories
            uncomp_current_glyphs_dir = os.path.join(
                *(self.new_efo_dir, self.EFO_glyphs_dir,
                  self.current_font_file_name))
            current_glyphs_dir = uncomp_current_glyphs_dir.split("_compo")[0]
            generic_tools.make_dir(current_glyphs_dir)
            #
            if os.path.exists(os.path.dirname(current_glyphs_dir)):
                #
                #pass
                generic_tools.rm_dir(current_glyphs_dir)
                #
            #
            copytree(self.current_source_ufo_glyphs_dir,
                     current_glyphs_dir,
                     ignore=ignore_patterns("*contents.plist", 'glyphs*'))
            #
        else:
            # Make Glyphs Directories
            current_glyphs_dir = os.path.join(*(self.new_efo_dir,
                                                self.EFO_glyphs_dir,
                                                self.current_font_file_name))
            generic_tools.make_dir(current_glyphs_dir)
            #
            if os.path.exists(os.path.dirname(current_glyphs_dir)):
                #
                generic_tools.rm_dir(current_glyphs_dir)
                #
            #
            copytree(self.current_source_ufo_glyphs_dir,
                     current_glyphs_dir,
                     ignore=ignore_patterns("*contents.plist", 'glyphs*'))
        #
    #
Пример #4
0
	def efo_to_svgs(self):
		#
		efo_fontinfo.read_efo_json_fontinfo(self)
		#
		print('UFO2SVG: Started Conversion of EFO to SVGs')
		#
		self.font_files = efo_fontinfo.get_font_file_array(self)
		self.given_fonts = self._fonts.split(',')
		self.current_font_family_glyphs_directory = os.path.join(self._in,self.EFO_glyphs_dir)
		self.current_font_family_vectors_directory = os.path.join(self._in,self.EFO_vectors_dir)
		#
		generic_tools.empty_dir(self.current_font_family_vectors_directory)
		#
		faults = generic_tools.check_given_fonts_exist(self._fonts, self.font_files)
		#
		if faults == False:
			#
			print('\tGIVEN FONTS EXIST CONTINUING')
			#
			for gf in self.given_fonts:
				#
				self.return_temp_and_vector_dirs(gf)
				#
				generic_tools.make_dir(self.current_font_family_vectors_directory)
				generic_tools.make_dir(self.current_font_instance_vectors_directory)
				#
				f = OpenFont(self.current_font_instance_temp_directory)
				#
				UFO_to_SVG = UFO2SVG(f)
				#
				convertUFOToSVGFiles(self, f, self.current_font_instance_name)
				#
			#
		else:
			#
			print('\tGIVEN FONTS INCONSISTENT ABORTING')
Пример #5
0
def copy_kerning(self, kerning_type="class", _stream="Downstream"):
    #
    if _stream == "Downstream":
        #
        print('EFO: Copying Class Based Kerning')
        #
        EFO_kerning_dir = os.path.join(
            os.path.join(self._in, self.EFO_kerning_dir), kerning_type)
        #
        EFO_class_kerning_groups_file = os.path.join(
            EFO_kerning_dir, self.current_font_file_name + '.plist')
        UFO_class_kerning_groups_file = os.path.join(
            self.current_font_instance_directory, 'kerning.plist')
        #
        #if (os.path.isdir(EFO_class_kerning_groups_file)):
        #
        copyfile(EFO_class_kerning_groups_file, UFO_class_kerning_groups_file)
        #
        print('\tCopied Kerning ' + kerning_type + ': ',
              UFO_class_kerning_groups_file)
        #
    else:
        # self.current_font_family_name, self.current_font_instance_name,
        kerning_dir = os.path.join(self.new_efo_dir, "kerning")
        kerning_dir_flat = os.path.join(kerning_dir, "flat")
        kerning_dir_class = os.path.join(kerning_dir, "class")
        #
        generic_tools.make_dir(kerning_dir)
        generic_tools.make_dir(kerning_dir_flat)
        generic_tools.make_dir(kerning_dir_class)
        #
        UFO_kerning_file = os.path.join(self.current_source_ufo,
                                        "kerning.plist")
        EFO_kerning_file = os.path.join(
            *(self.new_efo_dir, "kerning", kerning_type,
              self.current_font_file_name + ".plist"))
        #
        if "_krn" in self.current_font_file_name:
            #
            self.current_font_file_name = self.current_font_file_name.replace(
                '_krn', '')
            #
        #
        #
        copyfile(UFO_kerning_file, EFO_kerning_file)
        #print(self.current_source_ufo, efo_groups_file)
        #
Пример #6
0
    def _ufos_to_efo(self,
                     _to_copy=[
                         "metainfo", "features", "glyphs", "fontinfo",
                         "kerning", "lib"
                     ],
                     _ufos_to_temp=True,
                     _from_compress=False):
        #
        read_efo_json_fontinfo(self, "Upstream")
        #
        self.font_files = get_font_file_array(self)
        #
        print('UFOs to EFO')
        #
        self.current_font_family_name = generic_tools.sanitize_string(
            self.fontinfo[0]["shared_info"]["familyName"])
        #
        if self._out != "Current":
            #
            self.new_efo_dir = self._out  #os.path.join(*Path(self._in).parts[:-1])
            #
        else:
            #
            self.new_efo_dir = os.path.join(*Path(self._in).parts[:-1])
            #
        #
        temp_dir = os.path.join(self.new_efo_dir, self.EFO_temp)
        vectors_dir = os.path.join(self.new_efo_dir, self.EFO_vectors)
        features_dir = os.path.join(self.new_efo_dir, self.EFO_features_dir)
        #
        EFO_fontinfo_data = {}
        #
        new_info = copy.deepcopy(self.fontinfo)
        #
        x = 0
        #
        for f in self.font_files:
            #
            self.current_font_file_name = f
            self.current_font_instance_name = generic_tools.sanitize_string(
                self.current_font_family_name + ' ' +
                self.current_font_file_name)
            #
            if _from_compress:
                #
                self.current_source_ufo = os.path.join(
                    self.current_source_ufo_family,
                    self.current_font_instance_name + '.ufo')
                #
            else:
                #
                self.current_source_ufo_family = os.path.join(
                    self.new_efo_dir, self.current_font_family_name)
                #
            #
            self.current_source_ufo = os.path.join(
                self.current_source_ufo_family,
                self.current_font_instance_name + '.ufo')
            #
            self.current_source_ufo_glyphs_dir = os.path.join(
                self.current_source_ufo, 'glyphs')
            self.current_source_efo_features_dir = os.path.join(
                self.new_efo_dir, 'features')

            self.current_source_ufo_lib = os.path.join(
                *(self.new_efo_dir, self.current_font_family_name,
                  self.current_font_instance_name + '.ufo', 'lib.plist'))
            EFO_glyphlib_xml = os.path.join(self.new_efo_dir, 'glyphlib.xml')
            #
            #
            if x == 0:  # once
                #
                if "metainfo" in _to_copy:
                    #
                    copy_metainfo(self, "Upstream")
                    #
                #
                if "lib" in _to_copy:
                    #
                    lib_to_glyphlib.lib_to_glyphlib(
                        self.current_source_ufo_lib, EFO_glyphlib_xml)
                    #
                #
                generic_tools.make_dir(temp_dir)
                generic_tools.make_dir(vectors_dir)
                #
                if "features" in _to_copy:
                    #
                    generic_tools.make_dir(features_dir)
                    #
                #
            #
            if "glyphs" in _to_copy:
                #
                copy_glif_files(self, "Upstream")
                #
            #
            if "fontinfo" in _to_copy:
                #
                update_font_info(self, new_info, f)
                #
            #
            if "features" in _to_copy:
                #
                generic_tools.make_dir(
                    os.path.join(self.current_source_efo_features_dir,
                                 "kern_fea"))
                #
                #
                split_fea(self, _from_compress)
                #
            #
            has_groups = generic_tools.determine_kerning_type_ufo(
                self.current_source_ufo)
            #
            if has_groups:
                #
                if "kerning" in _to_copy:
                    #
                    copy_kerning(self, "class", "Upstream")
                    copy_groups_class_kerning(self, "Upstream")
                    #
                #
            else:
                #
                if "kerning" in _to_copy:
                    #
                    copy_kerning(self, "flat", "Upstream")
                    #
                #
            #
            x = x + 1
            #
        if "fontinfo" in _to_copy:
            # Save updated font info
            EFO_json_fontinfo = os.path.join(self.new_efo_dir, 'fontinfo.json')
            #
            with open(EFO_json_fontinfo, "w") as fi:
                #
                json.dump(new_info, fi, indent=4, sort_keys=True)
                #
            #
        #
        if _ufos_to_temp:
            #Move UFO Source font family to EFO temp
            copy_tree(self.current_source_ufo_family,
                      os.path.join(temp_dir, self.current_font_family_name))
            #
            #Remove UFO Source font family from root EFO dir
            generic_tools.rm_dir(self.current_source_ufo_family)
            #
            print("UFO Family Transfered To EFO/temp: ",
                  self.current_source_ufo_family)
Пример #7
0
    def _efo_to_ufos(self, _fonts='', _flatten=False, _kerning_type="class"):
        #
        '''
		Export UFOs from EFO:
			
			Read /EFO/font_info.json

				For Every weight/font:

					Actions: 
						features.fea:
							combine all .fea files in /features folder
						fontinfo.plist
							read /font_info.json
							generate combined "shared" and per weight fontinfo.plist
						groups.plist
							copy /groups/kerning.plist to every UFO renamed as groups.plist
						kerning.plist
							copy /kerning/class/weight.plist to every UFO renamed as kerning.plist
						lib.plist
							read /glyphlib.xml and create lib.plist for every UFO
						metainfo.plist
							copy metainfo.plist to every UFO
						metainfo.plist
							read /glyphlib.xml and create glyphs/contents.plist for every UFO
						glyphs
							copy EFO glyphs to UFO glyphs for every UFO
							

		'''
        #
        #
        if _fonts != '':

            self._fonts = _fonts
            self.font_files = self._fonts.split(',')
            faults = generic_tools.check_given_fonts_exist(
                _fonts, self.font_files)
            _font_files = self.font_files

        else:

            faults = False
            _font_files = self.font_files
        #
        #
        #
        if faults == False:
            #
            print('\tGIVEN FONTS EXIST CONTINUING')
            #
            self.all_exported_ufo_dst = []
            #
            for f in _font_files:
                #
                self.current_font_file_name = f
                self.current_font_instance_name = generic_tools.sanitize_string(
                    self.current_font_family_name + ' ' +
                    self.current_font_file_name)
                self.current_font_instance_directory = os.path.join(
                    self.current_font_family_directory,
                    self.current_font_instance_name + '.ufo')
                self.current_fontinfo = get_font_info_for_weight(self)
                #
                generic_tools.make_dir(self.current_font_family_directory)
                generic_tools.make_dir(self.current_font_instance_directory)
                #
                self.all_exported_ufo_dst.append(
                    {f: self.current_font_instance_directory})
                #
                print('______________________________\n')
                print(self.current_font_instance_name)
                print('\n')
                #
                generate_fontinfo(self)
                combine_fea(self)
                #
                kerning_to_copy = _kerning_type  # class / flat
                #
                if kerning_to_copy == "class":
                    #
                    copy_kerning(self, "class", "Downstream")
                    copy_groups_class_kerning(self, "Downstream")
                    #
                else:
                    #
                    copy_kerning(self, "flat", "Downstream")
                    remove_groups_class_kerning(self)
                    #
                #
                generate_lib(self)
                copy_metainfo(self, "Downstream")
                #
                generate_contents_plist(self)
                copy_glif_files(self, "Downstream")
                #
                if _flatten == True:
                    #
                    comp_tools.flatten_components(
                        self.current_font_instance_directory)
                    #
                #
            #
        else:
            #
            print('\tGIVEN FONTS INCONSISTENT ABORTING')
Пример #8
0
def flatten_components(ufo_dir):
	#
	print('COMP: Flatten UFO Components')
	#
	reader = ufoLib.UFOReader(ufo_dir, validate=True)
	#
	source_dir = os.path.join(ufo_dir,'glyphs')
	target_dir = os.path.join(ufo_dir,'glyphs_flat')
	generic_tools.make_dir(target_dir)
	#
	copy_tree(source_dir, target_dir)
	#
	t_dir = target_dir#/media/root/Malysh1/winshm/advent_repo/Advent/_/exp/advent_pro_fmm/test.ufo/glyphs'
	#print('INPUT', ufo_dir)
	#print('OUTPUT', t_dir)
	#
	ufoWriter = ufoLib.GlyphSet(t_dir)
	#
	inGlyphSet = reader.getGlyphSet()
	#
	for glyphName in inGlyphSet.keys():
		#
		g = inGlyphSet[glyphName]
		#
		text = inGlyphSet.getGLIF(glyphName)
		comp = ufoLib.glifLib._fetchComponentBases(text)
		#
		if len(comp):
			#
			new_outline = []
			#
			source_glyph = os.path.join(t_dir,generic_tools.glyphNameToFileName(glyphName))
			#
			target_elem = ET.parse(source_glyph)
			target_dest = target_elem.find('outline')
			#
			for co in comp:
				#
				comp_source = inGlyphSet.getGLIF(co)
				tree = ET.fromstring(comp_source)
				outl = tree.find('outline')
				#
				for x in outl:
					#
					contour = ET.Element("contour")
					#
					for point in x:
						#
						contour.append(point)
						#
					#
					if len(contour):
						#
						target_dest.append(contour)
						#
					#
				#
			#
			for elem in target_elem.iter():
				for child in list(elem):
					if child.tag == 'component':
						elem.remove(child)
					elif child.tag == 'contour':
						#
						for point in list(child):
							#	
							if "move" in str(point.attrib):
								elem.remove(child)
								break
			#
			
			xml_str = ET.tostring(target_elem.getroot(), method='xml').decode().replace("'", '"')
			#print(ElementTree.tostring(svgGlyph, method='xml'))
			#
			#data = ET.tostring(target_elem.getroot())
			f = open(source_glyph, "w")
			f.write('<?xml version="1.0" encoding="UTF-8"?>\n'+xml_str) 
			#target_elem.write(source_glyph, encoding='utf8')
			#
		else:
			g.drawPoints(None) 
			ufoWriter.writeGlyph(glyphName, g, g.drawPoints)
		#
	#
	ufoWriter.writeContents()
	#
	time.sleep(2)
	#
	generic_tools.empty_dir(source_dir)
	#
	generic_tools.rm_dir(source_dir)
	os.rename(target_dir, source_dir)
Пример #9
0
	def extract_similarity(self):
		#
		efo_fontinfo.read_efo_json_fontinfo(self)
		#
		print('SIMEX: Started Similarity Extraction')
		#
		self.font_files = efo_fontinfo.get_font_file_array(self)
		self.given_fonts = self._font.split(',')
		#self.current_font_family_glyphs_directory = os.path.join(self._in,self.EFO_glyphs_dir)
		#self.current_font_family_vectors_directory = os.path.join(self._in,self.EFO_vectors_dir)
		#
		faults = generic_tools.check_given_fonts_exist(self._font, self.font_files)
		#
		#
		if faults == False:
			#
			print('\tGIVEN FONTS EXIST CONTINUING')
			#
			all_dst = []
			#
			for gf in self.given_fonts:
				#
				print('\tSIMEX: Extracting Similarity from:', gf)
				#
				self.return_self_dirs(gf)
				#
				ufo_src_path = self.current_font_instance_directory
				#
				srcUfoFont = TFSFontFromFile(ufo_src_path)
				#
				glyph_nums = get_glyphs(srcUfoFont, check_list, checking)
				#
				if self._purpose == "comp":
					max_diff = max_diff_comp
					print('\tExtracting Similarity for Components (1 to 1)')
				else:
					max_diff = max_diff_kern
					print('\tExtracting Similarity for Kerning '+str(max_diff_kern)+'[left, center, right]'+' edit : max_diff_kern in SIMEX init for custom diff values.')
				#
				in_mils = init_permut(glyph_nums, 'right', max_diff)
				#
				mil_dict = in_mils[0]
				all_mils = in_mils[1]
				#
				#
				uni_groups = unique_groups(all_mils)
				#
				r = json.dumps(uni_groups)
				#
				parsed = json.loads(r)
				parse_dumped = json.dumps(parsed, indent=4, sort_keys=False)
				#

				#
				time_now = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
				filename = gf+'.json'
				#
				init_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.SIMEX_temp)
				#
				generic_tools.make_dir(init_path)
				#
				dst_dir = os.path.abspath(os.path.join(init_path, filename))
				#
				all_dst.append(dst_dir)
				#
				with open(dst_dir, 'w') as the_file:
					#
					the_file.write(parse_dumped)
					the_file.close()
					#
				#
				print('\n\tSIMEX: Done Extracting Similarity and Saved:', dst_dir)
				#
			#
			if self._compress == "Yes":
				#
				for x in all_dst:
					#
					if self._purpose == "comp":
						#
						print('\tCOMPRESSING to COMPONENTS PLIST: ',x)
						#
						build_component_group_plist(x, self.EFO_groups_dir)
					else:
						#
						print('\tCOMPRESSING to KERNING PLIST: ',x)
						#
						build_kerning_group_plist(x, self.EFO_groups_dir)
					#
				#
			#
			generic_tools.empty_dir(os.path.join(self._in, self.EFO_temp))
			#
		else:
			#
			print('\tGIVEN FONTS INCONSISTENT ABORTING')
Пример #10
0
def flatten_components(ufo_dir, export=True, save_glif=True):
    #
    print('COMP: Flatten UFO Components')
    #
    print(ufo_dir)
    #
    reader = ufoLib.UFOReader(ufo_dir, validate=True)
    #
    if export == True:
        #
        source_dir = os.path.join(ufo_dir, 'glyphs')
        target_dir = os.path.join(ufo_dir, 'glyphs_flat')
        generic_tools.make_dir(target_dir)
        #
        copy_tree(source_dir, target_dir)
        #
        t_dir = target_dir  #/media/root/Malysh1/winshm/advent_repo/Advent/_/exp/advent_pro_fmm/test.ufo/glyphs'
        print('INPUT', ufo_dir)
        print('OUTPUT', t_dir)
        #
        ufoWriter = ufoLib.GlyphSet(t_dir)
    #
    inGlyphSet = reader.getGlyphSet()
    #
    for glyphName in inGlyphSet.keys():
        #
        #print(glyphName, get_name)
        #
        g = inGlyphSet[glyphName]
        #
        text = inGlyphSet.getGLIF(glyphName)
        comp = ufoLib.glifLib._fetchComponentBases(text)
        #
        source_glyph = os.path.join(
            t_dir,
            generic_tools.glyphNameToFileName(glyphName) + '.glif')
        #
        g.drawPoints(None)
        #
        if export:
            #
            ufoWriter.writeGlyph(glyphName, g, g.drawPoints)
        #
        #ufoWriter.writeContents()
        #
        with open(source_glyph, 'r') as rf:
            #
            glif_data = rf.read()
            #
            with open(source_glyph, 'w') as wf:
                #
                target_elem = ET.fromstring(glif_data)
                target_dest = target_elem.find('outline')
                #
                for co in comp:
                    #
                    comp_source = inGlyphSet.getGLIF(co)
                    tree = ET.fromstring(comp_source)
                    outl = tree.find('outline')
                    #
                    for x in outl:
                        #
                        contour = ET.Element("contour")
                        #
                        for point in x:
                            #
                            contour.append(point)
                            #
                        #
                        if len(contour):
                            #
                            target_dest.append(contour)
                            #
                        #
                    #
                #
                for elem in target_elem.iter():
                    #
                    #print(elem.tag)
                    #
                    for child in list(elem):
                        if child.tag == 'component':
                            elem.remove(child)
                        elif child.tag == 'contour':
                            #
                            for point in list(child):
                                #
                                if "move" in str(point.attrib):
                                    elem.remove(child)
                                    break
                #
                anchors = target_elem.findall('anchor')
                #
                for anchor in anchors:
                    target_elem.remove(anchor)
                #
                xml_str = ET.tostring(
                    target_elem,
                    method='xml').decode().replace("'", '"').replace(
                        '</contour><contour>',
                        '</contour>\n    <contour>').replace(
                            '<contour><point', '    <contour>\n      <point')
                new_xml = LET.fromstring(xml_str)
                new_xml_str = LET.tostring(new_xml,
                                           encoding='utf8',
                                           method="xml",
                                           xml_declaration=False,
                                           pretty_print=True).decode()
                clean_xml_str = BeautifulSoup(xml_str, "xml").prettify(
                )  #minidom.parseString(xml_str).toprettyxml(indent="   ")
                #
                if save_glif == True:
                    #
                    f = open(source_glyph, "w")
                    f.write(clean_xml_str)
                    f.close()
                    #
                else:
                    #
                    f.close()
                    #
                    #
                #if get_name == glyphName:
                #
                #return clean_xml_str
                #
                #
            #
        #
    #
    if export == True:
        #
        time.sleep(2)
        #
        generic_tools.empty_dir(source_dir)
        #
        generic_tools.rm_dir(source_dir)
        os.rename(target_dir, source_dir)
        #