Exemplo n.º 1
0
    def save(self):
        if self.save_to_default_layer:
            self.defcon_font.save()
        else:
            """
            XXX A real hack here XXX
            RoboFont did not support layers (UFO3 feature) until version 3.
            So in order to allow editing (in RF 1.x) UFOs that contain
            a processed glyphs layer, checkoutlinesufo generates UFOs that
            are structured like UFO3, but advertise themselves as UFO2.
            To achieve this, the code below hacks ufoLib to surgically save
            only the processed layer.
            This hack is only performed if the original UFO is format 2.

            NOTE: this is deprecated and will be removed from AFDKO.
            """
            writer = UFOWriter(
                self.defcon_font.path, formatVersion=self.ufo_format)
            writer.layerContents[
                PROCD_GLYPHS_LAYER_NAME] = PROCD_GLYPHS_LAYER
            layers = self.defcon_font.layers
            layer = layers[PROCD_GLYPHS_LAYER_NAME]

            if self.ufo_format == UFOFormatVersion.FORMAT_2_0:
                # Override the UFO's formatVersion. This disguises a UFO2 to
                # be seen as UFO3 by ufoLib, thus enabling it to write the
                # layer without raising an error.
                warn_txt = ("Using a ‘hybrid’ UFO2-as-UFO3 is deprecated and "
                            "will be removed from AFDKO by the end of 2020. "
                            "This behavior (hack) was primarily to support "
                            "older versions of RoboFont which did not support "
                            "UFO3/layers. RoboFont 3 now supports UFO3 so the "
                            "hack is no longer required. Please update your "
                            "toolchain as needed.")
                warnings.warn(warn_txt, category=FutureWarning)
                writer._formatVersion = UFOFormatVersion.FORMAT_3_0

            glyph_set = writer.getGlyphSet(
                layerName=PROCD_GLYPHS_LAYER_NAME, defaultLayer=False)
            writer.writeLayerContents(layers.layerOrder)

            if self.ufo_format == UFOFormatVersion.FORMAT_2_0:
                # Restore the UFO's formatVersion to the original value.
                # This makes the glif files be set to format 1 instead of 2.
                glyph_set.ufoFormatVersionTuple = UFOFormatVersion.FORMAT_2_0

            layer.save(glyph_set)

        if self.font_type == UFO_FONT_TYPE:
            ufotools.regenerate_glyph_hashes(self.ufo_font_hash_data)
            # Write the hash data, if it has changed.
            self.ufo_font_hash_data.close()

        elif self.font_type == TYPE1_FONT_TYPE:
            args = ['tx', '-t1']
            if self.font_format == 'PFB':
                args.append('-pfb')
            if not run_shell_command(
                    args + [self.temp_ufo_path, self.font_path]):
                raise FocusFontError('Failed to convert UFO font to Type 1.')

        else:
            temp_cff_path = get_temp_file_path()
            if not run_shell_command([
                    'tx', '-cff', '+S', '+b', '-std',
                    self.temp_ufo_path, temp_cff_path], suppress_output=True):
                raise FocusFontError('Failed to convert UFO font to CFF.')

            if self.font_type == CFF_FONT_TYPE:
                copy2(temp_cff_path, self.font_path)

            else:  # OTF_FONT_TYPE
                if not run_shell_command([
                        'sfntedit', '-a',
                        f'CFF={temp_cff_path}', self.font_path]):
                    raise FocusFontError('Failed to add CFF table to OTF.')
Exemplo n.º 2
0
    def save(self):
        if self.save_to_default_layer:
            self.defcon_font.save()
        else:
            """
            XXX A real hack here XXX
            RoboFont did not support layers (UFO3 feature) until version 3.
            So in order to allow editing (in RF 1.x) UFOs that contain
            a processed glyphs layer, checkoutlinesufo generates UFOs that
            are structured like UFO3, but advertise themselves as UFO2.
            To achieve this, the code below hacks ufoLib to surgically save
            only the processed layer.
            This hack is only performed if the original UFO is format 2.
            """
            writer = UFOWriter(self.defcon_font.path,
                               formatVersion=self.ufo_format)
            writer.layerContents[PROCD_GLYPHS_LAYER_NAME] = PROCD_GLYPHS_LAYER
            layers = self.defcon_font.layers
            layer = layers[PROCD_GLYPHS_LAYER_NAME]

            if self.ufo_format == 2:
                # Override the UFO's formatVersion. This disguises a UFO2 to
                # be seen as UFO3 by ufoLib, thus enabling it to write the
                # layer without raising an error.
                writer._formatVersion = 3

            glyph_set = writer.getGlyphSet(layerName=PROCD_GLYPHS_LAYER_NAME,
                                           defaultLayer=False)
            writer.writeLayerContents(layers.layerOrder)

            if self.ufo_format == 2:
                # Restore the UFO's formatVersion to the original value.
                # This makes the glif files be set to format 1 instead of 2.
                glyph_set.ufoFormatVersion = self.ufo_format

            layer.save(glyph_set)

        if self.font_type == UFO_FONT_TYPE:
            ufotools.regenerate_glyph_hashes(self.ufo_font_hash_data)
            # Write the hash data, if it has changed.
            self.ufo_font_hash_data.close()

        elif self.font_type == TYPE1_FONT_TYPE:
            args = ['tx', '-t1']
            if self.font_format == 'PFB':
                args.append('-pfb')
            if not run_shell_command(args +
                                     [self.temp_ufo_path, self.font_path]):
                raise FocusFontError('Failed to convert UFO font to Type 1.')

        else:
            temp_cff_path = get_temp_file_path()
            if not run_shell_command([
                    'tx', '-cff', '+S', '+b', '-std', self.temp_ufo_path,
                    temp_cff_path
            ],
                                     suppress_output=True):
                raise FocusFontError('Failed to convert UFO font to CFF.')

            if self.font_type == CFF_FONT_TYPE:
                copy2(temp_cff_path, self.font_path)

            else:  # OTF_FONT_TYPE
                if not run_shell_command(
                    ['sfntedit', '-a', f'CFF={temp_cff_path}', self.font_path
                     ]):
                    raise FocusFontError('Failed to add CFF table to OTF.')