stretchy.py (2815B)
1 #!/usr/bin/env python3 2 3 from utils import mathfont 4 import fontforge 5 6 # Create a WOFF font with glyphs for all the operator strings. 7 font = mathfont.create("stretchy", "Copyright (c) 2021-2024 Igalia S.L.") 8 9 font.math.AxisHeight = 0 10 11 # Set parameters for stretchy tests. 12 font.math.MinConnectorOverlap = mathfont.em // 2 13 14 # Make sure that underover parameters don't add extra spacing. 15 font.math.LowerLimitBaselineDropMin = 0 16 font.math.LowerLimitGapMin = 0 17 font.math.StretchStackBottomShiftDown = 0 18 font.math.StretchStackGapAboveMin = 0 19 font.math.UnderbarVerticalGap = 0 20 font.math.UnderbarExtraDescender = 0 21 font.math.UpperLimitBaselineRiseMin = 0 22 font.math.UpperLimitGapMin = 0 23 font.math.StretchStackTopShiftUp = 0 24 font.math.StretchStackGapBelowMin = 0 25 font.math.OverbarVerticalGap = 0 26 font.math.AccentBaseHeight = 0 27 font.math.OverbarExtraAscender = 0 28 29 # These two characters will be stretchable in both directions. 30 horizontalArrow = 0x295A # LEFTWARDS HARPOON WITH BARB UP FROM BAR 31 verticalArrow = 0x295C # UPWARDS HARPOON WITH BARB RIGHT FROM BAR 32 upDownArrowWithBase = 0x21A8 # UP DOWN ARROW WITH BASE 33 34 mathfont.createSizeVariants(font, aUsePUA=True, aCenterOnBaseline=False) 35 36 # Add stretchy vertical and horizontal constructions for the horizontal arrow. 37 mathfont.createSquareGlyph(font, horizontalArrow) 38 mathfont.createStretchy(font, horizontalArrow, True) 39 mathfont.createStretchy(font, horizontalArrow, False) 40 41 # Add stretchy vertical and horizontal constructions for the vertical arrow. 42 mathfont.createSquareGlyph(font, verticalArrow) 43 mathfont.createStretchy(font, verticalArrow, True) 44 mathfont.createStretchy(font, verticalArrow, False) 45 46 # U+21A8 stretches vertically using two size variants: a base glyph (of height 47 # half an em) and taller glyphs (of heights 1, 2, 3 and 4 em). 48 g = font.createChar(upDownArrowWithBase) 49 mathfont.drawRectangleGlyph(g, mathfont.em, mathfont.em/2, 0) 50 font[upDownArrowWithBase].verticalVariants = "uni21A8 v0 v1 v2 v3" 51 52 mathfont.save(font) 53 54 55 # Create a font to test RTL operators. 56 font = mathfont.create("stretchy-text-direction-asymetrical", "Copyright (c) 2025 Igalia S.L.") 57 58 # Left and right parentheses and brackets, in the following order: { ) } ( 59 codePoints = (0x007B, 0x0029, 0x007D, 0x0028) 60 61 for i, point in enumerate(codePoints): 62 em = mathfont.em 63 width = int(em * (i + 1)) 64 ascent = em 65 66 glyph = font.createChar(point) 67 mathfont.drawRectangleGlyph(glyph, width, ascent) 68 69 for size in (0, 1, 2, 3): 70 g = font.createChar(-1, "v%d%d" % (size, i)) 71 mathfont.drawRectangleGlyph(g, width, (size + 1) * ascent, 0) 72 73 font[point].verticalVariants = "v0%d v1%d v2%d v3%d" % ((i,) * 4) 74 font[point].verticalComponents = \ 75 (("v2%d" % i, False, 0, width, 3 * em), 76 ("v1%d" % i, True, em, width, 2 * em)) 77 78 mathfont.save(font)