How to tell fontspec to load a font using a specific glyph variant not available as a CharacterVariant
The following Python script for FontForge creates
#!/usr/bin/env python2
import fontforge
import os.path
font_files = [
'Vollkorn-Bold.otf',
'Vollkorn-BoldItalic.otf',
'Vollkorn-Italic.otf',
'Vollkorn-Regular.otf',
]
def main():
for font_file_name in font_files:
make_new_font(font_file_name)
def get_file_name_stem(font_file_name):
name_without_directory = os.path.basename(font_file_name)
name_stem = os.path.splitext(name_without_directory)[0]
return name_stem
def make_new_font(font_file_name):
name_stem = get_file_name_stem(font_file_name)
feature_file_name = name_stem + '.fea'
new_font_file_name = name_stem + '-ss02.otf'
print('Font file: ' + font_file_name)
font = fontforge.open(font_file_name)
print('Font name: ' + font.fontname)
print('=> ' + feature_file_name)
font.generateFeatureFile(feature_file_name)
print('Lookup names:')
for lookup in font.gsub_lookups:
print(' ' + lookup)
lookup_ss01_name = "'ss01' Style Set 1 lookup 24"
lookup_ss02_name = "'ss02' Style Set 2 lookup 25"
subtable_ss02_name = "'ss02' Style Set 2 lookup 25 subtable"
(type_name,
flags,
feature_script_lang_tuples) = font.getLookupInfo(lookup_ss01_name)
print('Lookup(' + lookup_ss01_name + '): ')
print(' Type: ' + type_name)
print(' Flags: ' + ', '.join(flags))
print(' Feature/Script/Languages: ')
script_lang_ss01 = None
for (feature, script_lang) in feature_script_lang_tuples:
print(' ' + feature + ':')
for (script, languages) in script_lang:
language_list = ', '.join([l.rstrip() for l in languages])
print(' ' + script + ': ' + language_list)
if feature == 'ss01':
script_lang_ss01 = script_lang
if script_lang_ss01 is None:
raise('Feature ss01 not found in Style Set 1')
print(' Subtables:')
for subtable in font.getLookupSubtables(lookup_ss01_name):
print(' ' + subtable)
font.addLookup(lookup_ss02_name,
type_name,
flags,
(('ss02', script_lang_ss01),),
lookup_ss01_name)
font.addLookupSubtable(lookup_ss02_name, subtable_ss02_name)
glyph_one = [glyph for glyph in font.selection.select('one').byGlyphs][0]
glyph_one.addPosSub(subtable_ss02_name, 'one.ss01')
print('=> ' + new_font_file_name)
font.generate(new_font_file_name)
if __name__ == '__main__':
main()
It generates files Vollkorn-Regular-ss02.otf
, ... with an additional stylistic set 02 for the replacement of glyph one
with one.ss01
and which
can be activated:
\documentclass{article}
\usepackage{fontspec}
\setmainfont{Vollkorn-Regular-ss02.otf}[
StylisticSet=2,
ItalicFont=Vollkorn-Italic-ss02.otf,
BoldFont=Vollkorn-Bold-ss02.otf,
BoldItalicFont=Vollkorn-BoldItalic-ss02.otf,
]
\begin{document}
1234567890
\bfseries 1234567890
\itshape 1234567890
\mdseries 1234567890
\end{document}
Result with XeLaTeX:
I am posting here the approach with lualatex
which was alluded to by @WillRobertson in his comment
I created in my working repertory file testvollkornlua.fff
with contents (I am not sure the first two lines are needed, and the stuff with Kerning was for testing purposes).
languagesystem DFLT dflt;
languagesystem latn dflt;
feature oneb {
sub one by one.ss01;
} oneb;
# Kerning for testing only
feature kern {
pos \A \Y +5000;
pos \a \y +10000;
} kern;
Then with the following source :
\documentclass{article}
\usepackage{fontspec}
\setmainfont{Vollkorn}[%
FeatureFile={\jobname.fff},
RawFeature={+oneb},
UprightFont={* -Regular},
ItalicFont={* -Italic},
BoldFont={* -Bold},
BoldItalicFont={* -BoldItalic},
Extension={.otf}]
\begin{document}
1234567890
\bfseries 1234567890
\itshape 1234567890
\mdseries 1234567890
AY
ay
\end{document}
% Local Variables:
% TeX-engine: luatex
% End:
I obtain the desired output (the last two lines were there to check initially if the feature file was found in case I had badly coded feature "oneb"; I indeed had a syntax error (missing "oneb;") and as a result nothing was functional, thus it did not help, I didn't know if the feature file had been found or not until I had fixed it.
Still hoping for a xelatex
solution.