typemytype / drawbot

http://www.drawbot.com
Other
402 stars 63 forks source link

Some drawBot methods do not accept pathlib.Path() object as argument #449

Closed roberto-arista closed 2 years ago

roberto-arista commented 2 years ago

hey @typemytype and @justvanrossum!

I realized that some methods from the DrawBot API accept pathlib.Path() objects, so I assumed that any method dealing with files on disk would do that as well. But a few of them do not.

Here I collected all the methods from the public API that deal with files in Drawbot (did I miss anything?):

#!/usr/bin/env python3

# ------------------------------- #
# DrawBot and pathlib.Path object #
# ------------------------------- #

# -- Modules -- #
import drawBot as dB
from pathlib import Path

# -- Variables -- #
fontPath = Path('IBMPlexMono-Light.otf')
imagePath = Path('drawBot.png')

# -- Instructions -- #
if __name__ == '__main__':
    # Formatted String
    fs = dB.FormattedString()
    fs.font(fontNameOrPath=fontPath)
    fs.fallbackFont(fontNameOrPath=fontPath)
    print(fs.listOpenTypeFeatures(fontNameOrPath=fontPath))
    print(fs.listFontVariations(fontNameOrPath=fontPath))
    print(fs.listNamedInstances(fontNameOrPath=fontPath))
    dB.text(fs, (100, 100))

    # Text functions
    # dB.installFont(fontPath)
    # dB.uninstallFont(fontPath)
    dB.font(fontNameOrPath=fontPath)
    dB.fallbackFont(fontNameOrPath=fontPath)
    dB.listOpenTypeFeatures(fontNameOrPath=fontPath)
    dB.listFontVariations(fontNameOrPath=fontPath)
    dB.listNamedInstances(fontNameOrPath=fontPath)

    # image
    dB.image(path=imagePath, position=(0, 0))
    # dB.imageSize(path=imagePath)
    # dB.imagePixelColor(imagePath, (0, 0))
    # dB.imageResolution(path=imagePath)
    dB.numberOfPages(path=imagePath.with_suffix('.pdf'))

    # imageObject
    im = dB.ImageObject()
    # im.open(path=imagePath)

    # saving
    dB.saveImage(Path('pathlib.pdf'))

The ones commented out, raise an Exception if a Path() is provided. They work fine if the Path() is converted to string

#!/usr/bin/env python3

# ------------------------------- #
# DrawBot and pathlib.Path object #
# ------------------------------- #

# -- Modules -- #
import drawBot as dB
from pathlib import Path

# -- Variables -- #
fontPath = Path('IBMPlexMono-Light.otf')
imagePath = Path('drawBot.png')

# -- Instructions -- #
if __name__ == '__main__':
    # Formatted String
    fs = dB.FormattedString()
    fs.font(fontNameOrPath=fontPath)
    fs.fallbackFont(fontNameOrPath=fontPath)
    print(fs.listOpenTypeFeatures(fontNameOrPath=fontPath))
    print(fs.listFontVariations(fontNameOrPath=fontPath))
    print(fs.listNamedInstances(fontNameOrPath=fontPath))
    dB.text(fs, (100, 100))

    # Text functions
    dB.installFont(f'{fontPath}')
    dB.uninstallFont(f'{fontPath}')
    dB.font(fontNameOrPath=fontPath)
    dB.fallbackFont(fontNameOrPath=fontPath)
    dB.listOpenTypeFeatures(fontNameOrPath=fontPath)
    dB.listFontVariations(fontNameOrPath=fontPath)
    dB.listNamedInstances(fontNameOrPath=fontPath)

    # image
    dB.image(path=imagePath, position=(0, 0))
    dB.imageSize(path=f'{imagePath}')
    dB.imagePixelColor(f'{imagePath}', (0, 0))
    dB.imageResolution(path=f'{imagePath}')
    dB.numberOfPages(path=imagePath.with_suffix('.pdf'))

    # imageObject
    im = dB.ImageObject()
    im.open(path=f'{imagePath}')

    # saving
    dB.saveImage(Path('pathlib.pdf'))

If you want, next week I can try to submit a pull request to align the behaviour of the methods not accepting a pathlib object.

👋

roberto-arista commented 2 years ago

assuming these files in the same folder of the script Archive.zip

justvanrossum commented 2 years ago

PR welcome indeed.

justvanrossum commented 2 years ago

Did #454 solve everything you wanted? If so, this can be closed. Thanks again!