Previous question: Changing font within Asymptote. @marmot has successfully solved the case of two fonts but in reality I need to generate paths from letters of different fonts whose system names are stored in an array. Using the logic of the case of two fonts, I came up with this:
import settings;
import fontsize;
settings.tex="lualatex";
outformat="pdf";
size(9cm);
defaultpen(fontsize(10pt));
defaultpen(0.1);
string[] font_names = {"Courier New", "Arial"};
texpreamble("\usepackage{fontspec}");
for (int i = 0; i < font_names.length; ++i)
{
string preamble = "\newfontfamily\myfont" + (string)i + "{" + font_names[i] +"}";
string tp = "{\myfont" + (string)i + " a}";
write(preamble);
write(tp);
texpreamble(preamble);
draw(shift((i + 1) % 5, -ceil((i + 1) / 5)) * scale(0.1) * texpath(tp));
}
Pending question: I tried the following but it failed to switch the font:
import settings;
import fontsize;
settings.tex="lualatex";
outformat="pdf";
size(9cm);
defaultpen(fontsize(10pt));
defaultpen(0.1);
texpreamble("\usepackage{fontspec}\setmainfont{Courier New}");
draw(scale(0.1) * texpath("text"));
texreset();
texpreamble("\usepackage{fontspec}\setmainfont{Arial}");
draw(shift(3, 0) * scale(0.1) * texpath("text"));
What does texreset() do exactly?