1

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?

0

I found the solution. The issue was that TeX doesn't support numbers in macro names, so I'm now forming the font identifier based on the binary representation of the index of the font name.

import settings;
import fontsize;
settings.tex="lualatex";
outformat="pdf";

size(9cm);
defaultpen(fontsize(10pt));
defaultpen(0.1);

string bin(int n)
{
    int i;
    string res;
    for (i = intMax; i > 0; i = (int)(i / 2)) {
    if (AND(n, i) != 0) { res += "a"; } else { res += "b"; } }
    return substr(res, 48);
}

string[] font_names = {"Courier New", "Arial"};
texpreamble("\usepackage{fontspec}");
for (int i = 0; i < font_names.length; ++i)
{
    string preamble = "\newfontfamily\myfont" + bin(i) + "{" + font_names[i] +"}";
    string tp = "{\myfont" + bin(i) + " o}";
    write(preamble);
    write(tp);
    texpreamble(preamble);
    draw(shift((i + 1) % 5, -ceil((i + 1) / 5)) * scale(0.1) * texpath(tp));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.