interpolation

interpolateValues(value1, value2, factor)

Interpolate between two values by a given factor.

Parameters
  • value1 (int or float) – A numerical value.

  • value2 (int or float) – Another numerical value.

  • factor (float) – The interpolation factor.

>>> from hTools3.modules.interpolation import interpolateValues
>>> interpolateValues(20, 120, 0.5)
70.0
interpolateTuples(tuple1, tuple2, steps)

Interpolate a given amount of steps between two tuples of arbitrary length.

Parameters
  • tuple1 (tuple) – A tuple of values.

  • tuple2 (tuple) – Another tuple of values.

  • steps (int) – The number of interpolation steps.

Returns

A list of interpolation steps between the two input tuples.

>>> from hTools3.modules.interpolation import interpolateTuples
>>> interpolateTuples((20, 50), (120, 100), 3))
[(20.0, 50.0), (70.0, 75.0), (120.0, 100.0)]
calculateFactorLinear(steps, step)

Calculate linear interpolation factor.

Parameters
  • steps (int) – The total number of steps.

  • step (int) – The index of the current step.

>>> from hTools3.modules.interpolation import calculateFactorLinear
>>> steps = 5
>>> for i in range(steps):
>>>     print(i, calculateFactorLinear(steps, i))
0 0.0
1 0.25
2 0.5
3 0.75
4 1.0
calculateStemLinear(v1, v2, steps, step)

Calculates the interpolated stem width between two given stems.

Parameters
  • v1 (int or float) – The first stem width.

  • v2 (int or float) – The second stem width.

  • steps (int) – The total number of steps.

  • step (int) – The index of the current step.

from hTools3.modules.interpolation import calculateStemLinear

stem1 = 30
stem2 = 170
steps = 7

for i in range(steps):
    stem = calculateStemLinear(stem1, stem2, steps, i)
    rect(0, 0, stem, 400)
    translate(stem + 10, 0)
calculateStemLucas(v1, v2, steps, step)

Calculates the interpolated stem width between two given stems using Lucas de Groot’s Interpolation Theory.

Parameters
  • v1 (int or float) – The first stem width.

  • v2 (int or float) – The second stem width.

  • steps (int) – The total number of steps.

  • step (int) – The index of the current step.

from hTools3.modules.interpolation import calculateStemLucas

stem1 = 30
stem2 = 170
steps = 7

for i in range(steps):
    stem = calculateStemLucas(stem1, stem2, steps, i)
    rect(0, 0, stem, 400)
    translate(stem + 10, 0)
getStem(glyph, beamY)

Get the horizontal stem width of a glyph at a given y position.

Parameters
  • glyph (RGlyph) – A glyph object.

  • beamY (int or float) – The y position of the horizontal measuring beam.

Returns

The horizontal stem width of the glyph as a float or integer.

>>> from hTools3.modules.interpolation import getStem
>>> g = CurrentGlyph()
>>> getStem(g, 200)
95
getStemFactor(stem, g1, g2, beamY=200, tolerance=5, increment=0.001, verbose=False, cycles=10000)
interpolateGlyphsLinear(g1, g2, steps, step)

Interpolates a new glyph between two given glyphs.

Parameters
  • g1 (RGlyph) – The first master glyph.

  • g2 (RGlyph) – The second master glyph.

  • steps (int) – The total number of steps.

  • step (int) – The index of the current step.

from hTools3.modules.interpolation import interpolateGlyphsLinear

f1 = AllFonts().getFontsByStyleName('555A')[0]
f2 = AllFonts().getFontsByStyleName('955A')[0]

g1 = f1['a']
g2 = f2['a']

steps = 5

translate(50, 200)
scale(0.3)

for i in range(steps):
    g = interpolateGlyphsLinear(g1, g2, steps, i)
    drawGlyph(g)
    translate(g.width, 0)
interpolateGlyphsLucas(g1, g2, steps, step, stem1=None, stem2=None, beamY=200, cycles=1000)

Interpolates a new glyph between two given glyphs using Lucas de Groot’s Interpolation Theory.

Warning

The current algorithm is super slow and not really useful in production.

condenseGlyph(glyph1, glyph2, glyph3, stem1, stem2, factor)

Generate a condensed glyph by interpolating between a regular and a bold glyph.

Parameters
  • glyph1 (RGlyph) – A regular glyph.

  • glyph2 (RGlyph) – A bold glyph.

  • glyph3 (RGlyph) – A glyph for the resulting condensed glyph.

  • stem1 (int or float) – The stem width of the regular font.

  • stem2 (int or float) – The stem width of the bold font.

  • factor (float) – The interpolation factor.

from hTools3.modules.interpolation import condenseGlyph
f1 = AllFonts().getFontsByStyleName('Roman')[0]
f2 = AllFonts().getFontsByStyleName('Bold')[0]
f3 = NewFont()
g1 = f1['a']
g2 = f2['a']
g3 = f3.newGlyph('a')
condenseGlyph(g1, g2, g3, 95, 143, 0.5)
condenseGlyphs(font1, font2, font3, stem1, stem2, factor, glyphNames)

Generate condensed glyphs by interpolating between regular and bold fonts.

Parameters
  • font1 (RGlyph) – A regular font.

  • font2 (RGlyph) – A bold font.

  • font3 (RGlyph) – A condesed font for the resulting glyphs.

  • stem1 (int or float) – The stem width of the regular font.

  • stem2 (int or float) – The stem width of the bold font.

  • factor (float) – The interpolation factor.

  • glyphNames (list) – A list of glyph names to condense.

from hTools3.modules.interpolation import condenseGlyphs
f1 = AllFonts().getFontsByStyleName('Roman')[0]
f2 = AllFonts().getFontsByStyleName('Bold')[0]
f3 = NewFont()
condenseGlyphs(f1, f2, f3, 95, 143, 0.5, list('abcd'))
interpolateStepsInFont(font, g1, g2, interSteps, extraSteps, prefix='result', mark=False, verbose=True, mode='linear')

Interpolate a range of steps between two glyphs in the same font.

Parameters
  • font (RFont) – A font object.

  • g1 (RGlyph) – A master glyph.

  • g2 (RGlyph) – Another master glyph.

  • interSteps (int) – The number of interpolation steps.

  • extraSteps (int) – The number of extrapolation steps.

  • prefix (str) – A prefix for the glyph names of the resulting glyphs.

  • mark (bool) – Mark the resulting glyphs and the master glyphs with different colors.

from hTools3.modules.interpolation import interpolateStepsInFont
f = CurrentFont()
g1 = f['o']
g2 = f['O']
interpolateStepsInFont(f, g1, g2, 6, 2, prefix='result', mark=True)
interpolateGlyphsInFont(font, glyphName1, glyphName2, factors, prefix='result', verbose=True)

Interpolate a range of steps between two glyphs in the same font using a given list of factors.

Parameters
  • font (RFont) – A font object.

  • glyphName1 (str) – The name of the first master glyph.

  • glyphName2 (str) – The name of the second master glyph.

  • factors (list) – A list of interpolation factors for the steps.

  • prefix (str) – A prefix for the glyph names of the resulting glyphs.

from hTools3.modules.interpolation import interpolateGlyphsInFont
f = CurrentFont()
interpolateGlyphsInFont(f, 'b', 'a', [0.3, 0.4, 0.5], prefix='result')