Exemplo n.º 1
0
 def _validate_subsetting(self, _entry):
     try:
         parse_unicodes(self.custom.get_text())
         if Gtk.StyleContext.has_class(self.custom.get_style_context(),
                                       'error'):
             Gtk.StyleContext.remove_class(self.custom.get_style_context(),
                                           'error')
     except Exception:
         if not Gtk.StyleContext.has_class(self.custom.get_style_context(),
                                           'error'):
             Gtk.StyleContext.add_class(self.custom.get_style_context(),
                                        'error')
Exemplo n.º 2
0
def generate_subset(unicode_range, flavor, font_file, output_dir):
    """
    Generate font subset.
    You can do the same with the following command.
    $ pyftsubset YOUR_FONT.otf \
    --unicodes=U+943a-943b \
    --layout-features='*' \
    --flavor=woff \
    --name-IDs='*' \
    --output-file=style/font-subsets/YOUR_FONT-subset-1.woff
    """
    args = ["--layout-features='*'", "--flavor=%s" % flavor]
    options = Options()
    options.parse_opts(args)
    subsetter = Subsetter(options)
    font = load_font(font_file, options)

    subsetter.populate(unicodes=parse_unicodes(unicode_range))
    subsetter.subset(font)

    font_path = Path(font_file)
    name = font_path.stem
    unicode_range_hash = _get_unicode_range_hash(unicode_range)
    outfile = "%s/%s/%s-subset-%s.%s" % (
        output_dir,
        FONT_DIR,
        name,
        unicode_range_hash,
        flavor,
    )
    save_font(font, outfile, options)
    font.close()
Exemplo n.º 3
0
    def __generateForWeight(self, weight: str, font: Font) -> None:
        package = self.__core.package
        base_dir = self.__core.directories.webfonts
        output_dir = base_dir.joinpath(f"./{package.version}/{weight}")
        font_path = self.__core.findFontfilePath(font)

        output_dir.mkdir(parents=True, exist_ok=True)
        metadata = self.__generateMetadata()

        fake = Faker()
        fake.seed(package.id)
        subset_fontname: str = fake.name()

        options = Options()
        options.font_number = font.number
        options.hinting = False
        options.desubroutinize = True
        options.drop_tables += [
            'FFTM', 'PfEd', 'TeX', 'BDF', 'cvt', 'fpgm', 'prep', 'gasp',
            'VORG', 'CBDT', 'CBLC', 'sbix'
        ]
        for ignored in ['rvrn', 'locl']:
            options.layout_features.remove(ignored)

        for unicodes_file in FILE_DIR.UNICODE_TEXT.glob('./**/*.txt'):
            idx = unicodes_file.stem
            unicodes: List[str] = []

            with open(unicodes_file, 'r') as unicode_read_io:
                for line in unicode_read_io.readlines():
                    unicodes.extend(parse_unicodes(line.split('#')[0]))

            with load_font(font_path, options) as ttfont:
                subsetter = Subsetter(options=options)
                subsetter.populate(unicodes=unicodes)
                subsetter.subset(ttfont)

                for record in ttfont['name'].names:
                    if record.nameID == NAME_ID.COPYRIGHT:
                        record.string = '\n'.join(package.copyrights)
                    elif record.nameID in FAMILY_RELATED_NAME_ID:
                        record.string = subset_fontname

                woff_file = output_dir.joinpath(f"{idx}.woff")
                with open(woff_file, 'wb') as woff_write_io:
                    options.flavor = 'woff'
                    ttfont.flavorData = WOFFFlavorData()
                    ttfont.flavorData.metaData = metadata
                    save_font(ttfont, woff_write_io, options)

                woff2_file = output_dir.joinpath(f"{idx}.woff2")
                with open(woff2_file, 'wb') as woff2_write_io:
                    options.flavor = 'woff2'
                    ttfont.flavorData = WOFF2FlavorData()
                    ttfont.flavorData.metaData = metadata
                    save_font(ttfont, woff2_write_io, options)
    def _generate_font(self, filename, data):
        name = data['name-slug']
        self._append_log(_('Generating fonts for %s:' % data['name']),
                         bold=True)

        if self.ranges:
            for range, unicodes in self.ranges.items():
                font = TTFont(filename)
                subs = Subsetter()

                subs.populate(unicodes=parse_unicodes(unicodes))
                subs.subset(font)
                self._write_font(font, data, range=range)
                font.close()
                del font
        else:
            font = TTFont(filename)
            self._write_font(font, data)
            font.close()
Exemplo n.º 5
0
    def get_subsetting(self):
        ranges = {}
        preset_ranges = {
            'latin':
            'U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,' +
            'U+02DC,U+2000-206F,U+2074,U+20AC,U+2122,U+2212,U+2215',
            'latin-ext':
            'U+0100-024F,U+0259,U+1E00-1EFF,U+20A0-20CF,U+2C60-2C7F,' +
            'U+A720-A7FF',
            'cyrillic':
            'U+0400-045F,U+0490-0491,U+04B0-04B1,U+2116',
            'cyrillic-ext':
            'U+0460-052F,U+1C80-1C88,U+20B4,U+2DE0-2DFF,' +
            'U+A640-A69F,U+FE2E-FE2F',
            'greek':
            'U+0370-03FF',
            'greek-ext':
            'U+1F00-1FFF',
            'vietnamese':
            'U+0102-0103,U+0110-0111,U+1EA0-1EF9,U+20AB',
            'devanagari':
            'U+0900-097F,U+1CD0-1CF6,U+1CF8-1CF9,U+200B-200D,' +
            'U+20A8,U+20B9,U+25CC,U+A830-A839,U+A8E0-A8FB'
        }

        if not self.subsetting.get_enable_expansion():
            return None

        for (button, name) in self.subset_btns:
            if button.get_active():
                ranges[name] = preset_ranges[name]

        try:
            if parse_unicodes(self.custom.get_text()):
                ranges['custom'] = self.custom.get_text()
        except Exception:
            pass

        if len(ranges) == 0:
            return None
        return ranges
Exemplo n.º 6
0
def _intoURDict(uranges):
    urdict = {}
    for urname, urange in uranges:
        unicodes = parse_unicodes(urange)
        urdict[urname] = (urange, unicodes)
    return urdict