Pengembangan uji-driven untuk FontLab Studio 5 Macro dan Modul Python.
Fakelab adalah penggantian Fontlab Studio 5 untuk menguji kode Python.
Semuanya hanya diimplementasikan sejauh membuat objek FontLab dapat diimpor di luar FontLab Studio 5, dan menjalankan tes.
Disarankan untuk menginstal Fakelab di lingkungan virtual sehingga FontLab tidak akan secara tidak sengaja mengimpor modul palsu saat menjalankan skrip di FontLab Studio 5 yang sebenarnya. Jika Anda dapat hidup dengan Incompatibilites antara Python 2.7 dan 3, Anda juga dapat menjalankan Fakelab di Python 3.
Menyimpan VFB tidak didukung, karena format VFB tidak publik, tetapi Anda dapat menyimpan objek Font sebagai JSON.
Implementasi Fakelab didasarkan pada referensi API FontLab/Python tidak berharga yang tak ternilai, dan dari menjalankan skrip di FontLab Studio dan memeriksa apa yang mereka lakukan, terlepas dari menabrak aplikasi.
Ketika Anda telah mengaktifkan lingkungan virtual Anda:
$ pip install -e .FL kemudian dapat diimpor di luar 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 () Jika Anda berjalan di lingkungan virtual, dan perlu membuat modul fontlab Anda dapat diimpor, tambahkan file .pth di direktori paket-situs lingkungan virtual Anda:
# fl5modules.pth
/Users/<yourname>/Code/FLMacros/System/Modules
Fakelab adalah pekerjaan yang sedang berlangsung, dan hanya diimplementasikan sejauh yang saya butuhkan di skrip saya sendiri. Saya harus mengakui bahwa sebagian besar dari mereka masih belum memiliki tes.
Jika Anda menemukan NotImplementedError saat menulis tes, di sinilah Anda dapat unggul dalam membantu meningkatkan proyek ini;) Saya akan dengan senang hati menerima permintaan tarikan Anda. Atau, buka masalah, belikan saya kopi, dan berharap anak -anak saya meninggalkan saya sendiri sehingga saya dapat menemukan waktu untuk mengatasi masalah Anda.
Mengembangkan skrip tanpa pengujian otomatis benar -benar hanya untuk proyek yang sangat kecil. Untuk memastikan hasil modul atau skrip, Anda harus selalu menulis tes. Ini biasanya dilakukan dengan menggunakan pytest.
Mari kita asumsikan Anda memiliki skrip fontlab untuk memilih mesin terbang yang berisi komponen. Jika Anda memiliki koleksi alat sendiri untuk FontLab, skrip ini dapat terdiri dari dua bagian: satu skrip yang tercantum dalam toolbar makro FontLab, dan satu modul Python yang menerapkan logika, yang disebut oleh skrip bilah alat.
Studio 5
+- Macros
+- Selection
+- Select Composites.py
+- System
+- Modules
+- fakeLabDemo
+- selection
+- __init__.py
+- composites.py
Skrip Select Composites.py terlihat seperti ini:
#FLM: Select composites
# Studio 5/Macros/Selection/Select Composites.py
from fakeLabDemo . selection . composites import selectComposites
selectComposites ( fl . font )Dan modulnya:
# 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
]
)Bagaimana kita bisa yakin skrip ini melakukan apa yang seharusnya dilakukan? Untuk Pytest, kami menambahkan struktur folder paralel lain ke struktur yang ada:
Studio 5
+- Macros
+- Selection
+- Select Composites.py
+- System
+- Modules
+- fakeLabDemo
+- selection
+- __init__.py
+- composites.py
+- tests
+- fakeLabDemo
+- selection
+- composites_test.py
File composites_test.py , yang dinamai analog dengan file modul yang berkaitan dengan, adalah tempat kami akan menerapkan tes kami:
# 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 ()Seperti yang Anda lihat, Anda dapat menggunakan objek seperti yang Anda lakukan di dalam FontLab. Anda tidak bisa membuka font dari VFB yang ada, yang akan jauh lebih mudah. Tetapi format file VFB tidak publik.
Sebaliknya, Anda harus membangun font uji menggunakan API FL Python.
Ajak skrip uji di jendela terminal saat lingkungan virtual Anda aktif:
cd " Studio 5/Macros/System/Modules "
python -m pytest tests/fakeLabDemo/selection/composites_test.pyJika semuanya berhasil, Anda akan melihat beberapa output seperti ini:
============================ 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 ================================