Fontlab Studio 5 Python宏和模块的测试驱动开发。
Fakelab是Fontlab Studio 5用于测试Python代码的替换。
一切仅实施,甚至是使Fontlab Studio 5以外的可导入的Fontlab对象并运行测试。
建议在虚拟环境中安装fakelab,以便Fontlab在实际Fontlab Studio 5中运行脚本时不会意外导入假模块。如果您可以使用Python 2.7和3之间的Incompatibilite,则可以在Python 3中运行Fakelab。
不支持保存VFB,因为VFB格式不是公开的,但是您可以将Font对象保存为JSON。
Fakelab的实现是基于宝贵的非官方Fontlab/Python API参考,以及在Fontlab Studio中运行脚本并检查其所做的工作,除了崩溃应用程序。
当您激活虚拟环境时:
$ pip install -e .然后,FL在Fontlab Studio以外的地方可以进口:
from FL import fl , Font
# Make an empty font
f = Font ()
# Add the font to the mock app
fl . Add ( f )
# Close the font
fl . Close ()如果您在虚拟环境中运行,并且需要使您的Fontlab模块可导入,请在虚拟环境的站点包装目录中添加.pth文件:
# fl5modules.pth
/Users/<yourname>/Code/FLMacros/System/Modules
Fakelab是一项正在进行的工作,并且只能在我自己的脚本中实施。我必须承认,其中大部分仍然没有测试。
如果您在编写测试时遇到NotImplementedError ,那么您可以在这里擅长改善该项目;)我很乐意接受您的拉请请求。另外,打开一个问题,给我买咖啡,并希望我的孩子们独自一人,这样我可以找到一些时间来处理您的问题。
开发无自动测试的脚本实际上仅适用于非常小的项目。为了确保模块或脚本的结果,您应该始终编写测试。这通常是使用pytest完成的。
假设您有一个fontlab脚本可以选择包含组件的字形。如果您有针对Fontlab的工具集合,则此脚本可能包括两个部分:Fontlab的宏工具栏中列出的一个脚本,以及一个实现逻辑的Python模块,该模块由工具栏脚本调用。
Studio 5
+- Macros
+- Selection
+- Select Composites.py
+- System
+- Modules
+- fakeLabDemo
+- selection
+- __init__.py
+- composites.py
Select Composites.py脚本看起来像这样:
#FLM: Select composites
# Studio 5/Macros/Selection/Select Composites.py
from fakeLabDemo . selection . composites import selectComposites
selectComposites ( fl . font )和模块:
# Studio 5/Macros/System/Modules/fakeLabDemo/selection/composites.py
from __future__ import absolute_import , division , print_function
from FL import fl
def getFontIndex ( font ):
"""
Get the index of the supplied font.
We must iterate through the open fonts and compare file names,
because the == operator can not compare the font objects directly.
(FL font objects get a different id() each time they are called)
:param font: A title for the dialog.
:type font: :py:class:`FL.Font`
"""
for i in range ( len ( fl )):
cf = fl [ i ]
if cf . file_name == font . file_name :
if font . file_name is None :
if (
cf . family_name == font . family_name
and cf . style_name == font . style_name
):
return ( cf , i )
else :
return ( cf , i )
# Font was not found, probably there are no open fonts
return ( None , - 1 )
def setSelection ( font , glyph_names ):
"""
Set glyphs from the glyph_names list as selected in the font window.
"""
f , i = getFontIndex ( font )
if i > - 1 :
fl . ifont = i
fl . Unselect ()
for n in glyph_names :
fl . Select ( n )
def selectComposites ( font ):
"""
Select composites in font.
"""
setSelection (
font ,
[
glyph . name
for glyph in font . glyphs
if glyph . components
]
)我们如何确定此脚本应该做什么?对于Pytest,我们将另一个并行文件夹结构添加到现有结构:
Studio 5
+- Macros
+- Selection
+- Select Composites.py
+- System
+- Modules
+- fakeLabDemo
+- selection
+- __init__.py
+- composites.py
+- tests
+- fakeLabDemo
+- selection
+- composites_test.py
文件composites_test.py被称为类似于与其相关的模块文件的命名,是我们将实现测试的地方:
# Studio 5/Macros/System/Modules/tests/fakeLabDemo/selection/composites_test.py
import pytest
from FL import fl , Component , Font , Glyph , Point
from fakeLabDemo . selection . composites import selectComposites
def test_selectComposites ():
# Construct a fake FontLab font object
font = Font ()
g = Glyph ( 1 )
g . name = "A"
g . width = 500
g . unicode = 0x41
font . glyphs . append ( g )
g = Glyph ( 1 )
g . name = "dieresis"
g . width = 500
g . unicode = 0xA8
font . glyphs . append ( g )
g = Glyph ( 1 )
g . name = "Adieresis"
g . width = 500
g . unicode = 0xC4
g . components . append ( Component ( 0 ))
g . components . append ( Component ( 1 , Point ( 0 , 300 )))
font . glyphs . append ( g )
# Add the font to the FL object
fl . Add ( font )
fl . UpdateFont ()
# Run our script to be tested on the font
selectComposites ( fl . font )
# You could save the fake font to JSON instead of VFB.
# fl.font.Save("test_composites.vfb.json")
# Test if the correct glyphs have been selected
assert fl . Selected ( 0 ) == 0
assert fl . Selected ( 1 ) == 0
assert fl . Selected ( 2 ) == 1
# Close the fake font
fl . Close ()如您所见,您可以像Fontlab内部一样使用对象。您只是无法从现有VFB打开字体,这要容易得多。但是VFB文件格式不是公开的。
相反,您必须使用FL Python API构建测试字体。
当您的虚拟环境处于活动状态时,在终端窗口中调用测试脚本:
cd " Studio 5/Macros/System/Modules "
python -m pytest tests/fakeLabDemo/selection/composites_test.py如果一切都奏效,您将看到这样的输出:
============================ test session starts ===============================
platform darwin -- Python 3.8.7, pytest-6.0.1, py-1.9.0, pluggy-0.13.1
rootdir: /Users/jens/Code/fakelab
collected 1 item
tests/fakeLabDemo/selection/composites_test.py . [100%]
============================= 1 passed in 0.02s ================================