Fontlab Studio 5 Python 매크로 및 모듈에 대한 테스트 중심 개발.
Fakelab은 Python 코드 테스트를위한 Fontlab Studio 5 교체품입니다.
FontLab Studio 5 외부에서 FontLab 객체를 가져올 수 있도록 모든 것이 구현되고 테스트를 실행하기 위해서만 구현됩니다.
가상 환경에 Fakelab을 설치하는 것이 좋습니다. 따라서 Fontlab은 실제 Fontlab Studio 5에서 스크립트를 실행할 때 우연히 가짜 모듈을 가져 오지 않습니다. Python 2.7과 3 사이의 소동적 인 소동과 함께 살 수 있다면 Python 3에서 Fakelab을 실행할 수도 있습니다.
VFB 형식은 공개되지 않으므로 VFB를 저장하는 것은 지원되지 않지만 Font 객체를 JSON으로 저장할 수 있습니다.
Fakelab의 구현은 귀중한 비공식 Fontlab/Python API 참조와 Fontlab Studio에서 스크립트를 실행하고 응용 프로그램 충돌을 제외하고는 무엇을하는지 확인하는 것입니다.
가상 환경을 활성화 한 경우 :
$ pip install -e .그런 다음 FLO는 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 ================================