예제 #1
0
def mock_font():
    f_path = tempfile.NamedTemporaryFile()
    ttf = minimalTTF()
    ttf.font.save(f_path.name)
    dfont = DFont(f_path.name)
    dfont.builder = FontBuilder(font=dfont.ttfont)
    f_path.close()
    return dfont
예제 #2
0
def run_plot_glyphs(font_path, out):
    font_filename = os.path.basename(font_path)[:-4]
    dfont = DFont(font_path)
    if dfont.is_variable:
        for coords in dfont.instances_coordinates:
            dfont.set_variations(coords)
            img_out = os.path.join(
                out, "%s_%s.png" %
                (font_filename, _instance_coords_to_filename(coords)))
            dfont.glyphs.to_png(img_out, limit=100000)
    else:
        img_out = os.path.join(out, font_filename + ".png")
        dfont.glyphs.to_png(dst=img_out)
예제 #3
0
    def test_ft_hint_mode(self):
        self._path = os.path.dirname(__file__)
        font_path = os.path.join(self._path, 'data', 'Play-Regular.ttf')

        unhinted_font = DFont(font_path,
                              ft_load_glyph_flags=FTHintMode.UNHINTED)
        # gid 4 = A
        unhinted_font.ftfont.load_glyph(
            4, flags=unhinted_font.ft_load_glyph_flags)
        unhinted_bitmap = unhinted_font.ftslot.bitmap

        hinted_font = DFont(font_path, ft_load_glyph_flags=FTHintMode.NORMAL)
        hinted_font.ftfont.load_glyph(4, flags=hinted_font.ft_load_glyph_flags)
        hinted_bitmap = hinted_font.ftslot.bitmap

        self.assertNotEqual(unhinted_bitmap.buffer, hinted_bitmap.buffer)
예제 #4
0
def main():
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=RawTextHelpFormatter)
    parser.add_argument('font')
    parser.add_argument('dump', choices=CHOICES)
    parser.add_argument('-s', '--strings-only', action='store_true')
    parser.add_argument('-ol', '--output-lines', type=int, default=100)
    parser.add_argument('-md', '--markdown', action='store_true')
    parser.add_argument('-i',
                        '--vf-instance',
                        help='Variable font instance to diff')
    parser.add_argument('-r', '--render-path', help="Path to generate png to")
    args = parser.parse_args()

    font = DFont(args.font)

    if font.is_variable and not args.vf_instance:
        raise Exception("Include a VF instance to dump e.g -i wght=400")

    if args.vf_instance:
        variations = {
            s.split('=')[0]: float(s.split('=')[1])
            for s in args.vf_instance.split(", ")
        }
        font.set_variations(variations)

    table = getattr(font, args.dump, False)
    if not table:
        print(("Font doesn't have {} table".format(args.dump)))
        exit()
    report_len = len(table)

    if args.markdown:
        print(table.to_md(args.output_lines, strings_only=args.strings_only))
    else:
        print(table.to_txt(args.output_lines, strings_only=args.strings_only))

    if args.output_lines < report_len:
        print(("Showing {} out of {} items. Increase the flag -ol "
               "to view more".format(args.output_lines, report_len)))
    if args.render_path:
        table.to_png(args.render_path, limit=args.output_lines)
예제 #5
0
 def plot_glyphs(self):
     logger.info("Running plot glyphs")
     out = os.path.join(self.out, "plot_glyphs")
     mkdir(out)
     fonts = [f.reader.file.name for f in self.fonts]
     for font in fonts:
         font_filename = os.path.basename(font)[:-4]
         dfont = DFont(font)
         if dfont.is_variable:
             for _, coords in dfont.instances_coordinates.items():
                 dfont.set_variations(coords)
                 img_out = os.path.join(
                     out,
                     "%s_%s.png"
                     % (font_filename, self._instance_coords_to_filename(coords)),
                 )
                 dfont.glyphs.to_png(img_out, limit=100000)
         else:
             img_out = os.path.join(out, font_filename + ".png")
             dfont.glyphs.to_png(dst=img_out)
예제 #6
0
 def __init__(self, path, family_name=None, style_name=None):
     self.path = path
     self.filename = os.path.basename(self.path)[:-4]
     self.font = DFont(self.path)
     self._is_vf = self.is_vf
     if not family_name:
         self.family_name = familyname_from_filename(self.filename)
     else:
         self.family_name = family_name
     self.styles = []
     if style_name and not self._is_vf:
         style = FontStyle(style_name, self)
         self.styles.append(style)
     elif not style_name and not self._is_vf:
         style_name = stylename_from_filename(self.filename)
         style = FontStyle(style_name, self)
         self.styles.append(style)
     elif self._is_vf:
         self._get_vf_styles()
     self.axes = self._get_axes()
예제 #7
0
def main():
    parser = argparse.ArgumentParser(description=__doc__,
                                     formatter_class=RawTextHelpFormatter)
    parser.add_argument('--version', action='version', version=__version__)

    parser.add_argument('font_before')
    parser.add_argument('font_after')
    parser.add_argument('-td',
                        '--to_diff',
                        nargs='+',
                        choices=CHOICES,
                        default='*',
                        help="Categories to diff. '*' diffs everything")

    parser.add_argument('-ol',
                        '--output-lines',
                        type=int,
                        default=50,
                        help="Amout of rows to report for each diff table")
    formatter_group = parser.add_mutually_exclusive_group(required=False)
    formatter_group.add_argument('-txt',
                                 '--txt',
                                 action='store_true',
                                 default=True,
                                 help="Output report as txt.")
    formatter_group.add_argument('-md',
                                 '--markdown',
                                 action='store_true',
                                 default=False,
                                 help="Output report as markdown.")
    formatter_group.add_argument('-html',
                                 '--html',
                                 action='store_true',
                                 default=False,
                                 help="Output report as html.")
    parser.add_argument('-v',
                        '--verbose',
                        action='store_true',
                        help='Output verbose reports')
    parser.add_argument('-l',
                        '--log-level',
                        choices=('INFO', 'DEBUG', 'WARN'),
                        default='INFO')
    parser.add_argument('-i',
                        '--vf-instance',
                        default=None,
                        help='Set vf variations e.g "wght=400"')

    parser.add_argument('--marks_thresh',
                        type=int,
                        default=0,
                        help="Ignore modified marks under this value")
    parser.add_argument('--mkmks_thresh',
                        type=int,
                        default=0,
                        help="Ignore modified mkmks under this value")
    parser.add_argument('--kerns_thresh',
                        type=int,
                        default=0,
                        help="Ignore modified kerns under this value")
    parser.add_argument('--glyphs_thresh',
                        type=float,
                        default=0,
                        help="Ignore modified glyphs under this value")
    parser.add_argument('--metrics_thresh',
                        type=int,
                        default=0,
                        help="Ignore modified metrics under this value")
    parser.add_argument('--cbdt_thresh',
                        type=float,
                        default=0,
                        help="Ignore modified CBDT glyphs under this value")
    parser.add_argument('-rd',
                        '--render_diffs',
                        action='store_true',
                        help=("Render glyphs with hb-view and compare "
                              "pixel diffs."))
    parser.add_argument('-r',
                        '--render-path',
                        help="Path to generate before and after gifs to.")
    args = parser.parse_args()

    logger = logging.getLogger("fontdiffenator")
    logger.setLevel(args.log_level)

    diff_options = dict(
        marks_thresh=args.marks_thresh,
        mkmks_thresh=args.mkmks_thresh,
        kerns_thresh=args.kerns_thresh,
        glyphs_thresh=args.glyphs_thresh,
        metrics_thresh=args.metrics_thresh,
        cbdt_thresh=args.cbdt_thresh,
        to_diff=args.to_diff,
        render_diffs=args.render_diffs,
        render_path=args.render_path,
        html_output=args.html,
    )
    font_before = DFont(args.font_before)
    font_after = DFont(args.font_after)
    font_matcher(font_before, font_after, args.vf_instance)

    diff = DiffFonts(font_before, font_after, diff_options)

    if args.render_path:
        diff.to_gifs(args.render_path, args.output_lines)

    if args.markdown:
        print(diff.to_md(args.output_lines))
    elif args.html:
        print(diff.to_html(args.output_lines, image_dir=args.render_path))
    else:
        print(diff.to_txt(args.output_lines))
예제 #8
0
    def diffenator(self, **kwargs):
        logger.info("Running Diffenator")
        dst = os.path.join(self.out, "Diffenator")
        mkdir(dst)
        for style in self.matching_instances:
            font_before = DFont(self.instances_before[style]['filename'])
            font_after = DFont(self.instances[style]['filename'])
            out = os.path.join(dst, style)
            if font_after.is_variable and not font_before.is_variable:
                font_after.set_variations_from_static(font_before)

            elif not font_after.is_variable and font_before.is_variable:
                font_before.set_variations_from_static(font_after)

            elif font_after.is_variable and font_before.is_variable:
                coordinates = self.instances_before[style]['coordinates']
                font_after.set_variations(coordinates)
                font_before.set_variations(coordinates)

            # TODO add settings
            diff = DiffFonts(font_before, font_after, {"render_diffs": True})
            diff.to_gifs(dst=out)
            diff.to_txt(20, os.path.join(out, "report.txt"))
            diff.to_md(20, os.path.join(out, "report.md"))
            diff.to_html(20, os.path.join(out, "report.html"), image_dir=".")
예제 #9
0
def run_diffenator(font_before, font_after, out, thresholds):
    font_before = DFont(font_before)
    font_after = DFont(font_after)

    if font_after.is_variable and not font_before.is_variable:
        font_after.set_variations_from_static(font_before)

    elif not font_after.is_variable and font_before.is_variable:
        font_before.set_variations_from_static(font_after)

    elif font_after.is_variable and font_before.is_variable:
        # TODO get wdth and slnt axis vals
        variations = {"wght": font_before.ttfont["OS/2"].usWeightClass}
        font_after.set_variations(variations)
        font_before.set_variations(variations)

    diff = DiffFonts(font_before, font_after, settings=thresholds)
    diff.to_gifs(dst=out)
    diff.to_txt(20, os.path.join(out, "report.txt"))
    diff.to_md(20, os.path.join(out, "report.md"))
    diff.to_html(20, os.path.join(out, "report.html"), image_dir=".")
예제 #10
0
    def diffenator(self, **kwargs):
        logger.info("Running Diffenator")
        dst = os.path.join(self._out, "Diffenator")
        mkdir(dst)
        for style in self._shared_instances:
            font_before = DFont(self._instances_before[style])
            font_after = DFont(self._instances[style])
            out = os.path.join(dst, style)
            if font_after.is_variable and not font_before.is_variable:
                font_after.set_variations_from_static(font_before)

            elif not font_after.is_variable and font_before.is_variable:
                font_before.set_variations_from_static(font_after)

            elif font_after.is_variable and font_before.is_variable:
                # TODO get wdth and slnt axis vals
                variations = {"wght": font_before.ttfont["OS/2"].usWeightClass}
                font_after.set_variations(variations)
                font_before.set_variations(variations)

            # TODO add settings
            diff = DiffFonts(font_before, font_after, {"render_diffs": True})
            diff.to_gifs(dst=out)
            diff.to_txt(20, os.path.join(out, "report.txt"))
            diff.to_md(20, os.path.join(out, "report.md"))
            diff.to_html(20, os.path.join(out, "report.html"), image_dir=".")