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 ================================