I’m trying to configure Anki to use system fonts when present, and fall back to font files in collection.media if not. I also want to use both the regular and bold version of the same font.
My understanding is that the following syntax should work, and it technically does. However, it seems like it doesn’t find the local bold font, as the font does not load if the fallback font sources are removed.
This is on a Windows machine.
EDIT:
Replacing the local bold font with the regular version of another fonts works. In other words, it seems to specifically be a problem with accessing alternate weights.
Note:
I couldn’t find .ttf file after a bit of googling, so in the example above, I used .otf file downloaded from this page. Also, I used a yet-to-be-released version of AnkiWebView Inspector add-on to illustrate which font the text is rendered in within a single screenshot.
The .ttf file is from this repo. I use it as the original .otf loaded significantly slower when I attempted to use it in Ankidroid a while back.
Using your styling as a base, compare the following three outputs:
Using your styling as-is:
Using only the font file [2]:
Using only the local font [3]:
My conclusion is that the local bold font is never accessed, and Anki instead falls back to the font file. Otherwise, all three of these ought to be the same.
I may have misunderstood the meaning of your first post a bit, as I have a limited understanding of English.
At the time I made my previous post, I had tested the template without installing that font on my OS, so now I installed the font on my OS and tested it again, and the bold-weight part did not work.
As you said, local fonts specified with @font-facelocal() do not seem to have access to weights other than normal.
One of the workarounds I can quickly think of is as follows:
.card {
font-size: 64px;
text-align: center;
}
@font-face {
font-family: "myHanSerif";
font-weight: 200;
src: url("_SourceHanSerifJP-ExtraLight.ttf");
}
@font-face {
font-family: "myHanSerif";
font-weight: normal;
src: url("_SourceHanSerifJP-Regular.ttf");
}
@font-face {
font-family: "myHanSerif";
font-weight: bold;
src: url("_SourceHanSerifJP-Bold.ttf");
}
/* First, look for "源ノ明朝 JP" font installed on your system,
and if it is not found, then look for "_SourceHanSerifJP-***.ttf"
in the "collection.media" folder. */
.han-serif {
font-family: "源ノ明朝 JP", "myHanSerif";
}
.extra-light-weight {
font-weight: 200;
}
.normal-weight {
font-weight: normal;
}
.bold-weight {
font-weight: bold;
}
Screencast GIF:
(It seems that gif files cannot be zoomed to their original size on this forum, unlike jpg and png files. If the size is too small for you to see, open the gif directly in a new tab.)
This seems like the way to go on Linux as well, since it looks like fontconfig will return another font instead of falling back to @font-faceurl() if the font requested in @font-facelocal() is missing.
I wonder if the behavior of @font-facelocal() is an issue with Anki or the OS, though.