#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
from fontTools.ttLib import TTFont

def modify_font_metrics(input_font_path, output_font_path, scale=1.1):
    font = TTFont(input_font_path)

    hhea = font['hhea']

    original_ascender = hhea.ascent
    original_descender = hhea.descent
    original_lineGap = hhea.lineGap

    hhea.ascent = int(original_ascender * scale)
    hhea.descent = int(original_descender * scale)
    hhea.lineGap = int(original_lineGap * scale)

    os2 = font['OS/2']

    os2.sTypoAscender = int(os2.sTypoAscender * scale)
    os2.sTypoDescender = int(os2.sTypoDescender * scale)
    os2.sTypoLineGap = int(os2.sTypoLineGap * scale)
    os2.usWinAscent = int(os2.usWinAscent * scale)
    os2.usWinDescent = int(os2.usWinDescent * scale)
    os2.sxHeight = int(os2.sxHeight * scale)
    os2.sCapHeight = int(os2.sCapHeight * scale)

    name_table = font['name']
    scale_suffix = f" scale{int(scale * 100)}"

    for name_record in name_table.names:
        # Change name inside: "Font Family Name", "Full Font Name", "PostScript Name"
        if name_record.nameID in [1, 4, 6]:
            new_name = name_record.toUnicode() + scale_suffix
            name_record.string = new_name.encode(name_record.getEncoding())

    font.save(output_font_path)

# Sample
input_font_path = "./IBMPlexMono-Regular.ttf"
output_font_path = "./IBMPlexMono-Regular-modified2.ttf"
modify_font_metrics(input_font_path, output_font_path, scale=1.5)
