Last active
October 8, 2024 07:15
-
-
Save ravshansbox/8b50e5b815ffdb5825608812c82baa6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fontforge | |
import sys | |
def increase_line_spacing(input_font, output_font, increase_factor): | |
font = fontforge.open(input_font) | |
# Store the original values | |
original_em = font.em | |
original_ascent = font.ascent | |
original_descent = font.descent | |
# Calculate the additional line gap | |
additional_line_gap = int(original_em * (increase_factor - 1)) | |
# Adjust the OS/2 metrics to increase line spacing | |
font.os2_typoascent = original_ascent | |
font.os2_typodescent = -original_descent | |
font.os2_typolinegap = font.os2_typolinegap + additional_line_gap | |
# Adjust win ascent and descent to accommodate the increased line spacing | |
font.os2_winascent = original_ascent + (additional_line_gap // 2) | |
font.os2_windescent = original_descent + (additional_line_gap // 2) | |
# Ensure hhea table metrics are consistent | |
font.hhea_ascent = font.os2_winascent | |
font.hhea_descent = -font.os2_windescent | |
font.hhea_linegap = 0 | |
# Generate the new font | |
font.generate(output_font) | |
print(f"New font saved as {output_font}") | |
if __name__ == "__main__": | |
if len(sys.argv) != 4: | |
print("Usage: fontforge -script script.py input_font.ttf output_font.ttf increase_factor") | |
sys.exit(1) | |
input_font = sys.argv[1] | |
output_font = sys.argv[2] | |
increase_factor = float(sys.argv[3]) | |
increase_line_spacing(input_font, output_font, increase_factor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment