rougier / freetype-py

Python binding for the freetype library
Other
304 stars 88 forks source link

Can I directly modify anchor points of a glyph? #132

Closed SunHaozhe closed 3 years ago

SunHaozhe commented 3 years ago

Can I directly modify anchor points of a glyph? If yes, how can I do so?

For example, I want to randomly move each anchor point of a glyph by a small random amount of units, which creates a distorted version of that glyph.

It seems to me that FT_Outline defined in FreeType C++ version (https://www.freetype.org/freetype2/docs/reference/ft2-outline_processing.html#ft_outline) contains a field called points, the field points points to an array of n_points FT_Vector elements, giving the outline's point coordinates. Thus, if one is able to modify the value of those FT_Vector, one can create a distorted version of that glyph. Is this functionality supported by freetype-py? If not, is there any way of achieving the same effect?

Here, anchor points refer to the points that define the Bézier curves, thus the glyph outline.

By the way, RoboFont allows randomly moving anchor points, see here. However this is not a free and open software. Can we do the same thing using freetype-py somehow?

from random import randint

# get the current glyph
glyph = CurrentGlyph()

# set undo state
glyph.prepareUndo('move points randomly')

# loop over all contours in glyph
for contour in glyph:
    # loop over all points in contour
    for point in contour.points:
        # move point by a random distance
        point.x += randint(-20, 20)
        point.y += randint(-20, 20)

# update glyph
glyph.changed()

# restore undo state
glyph.performUndo()
HinTak commented 3 years ago

For font editing tasks like this it is more appropriate to use fontforge's python binding, for example. While I think freetype-py can probably be made/enhanced, it is not the right tool for such task.

HinTak commented 3 years ago

Fontforge (and its python binding) is free and open software...