AviSynth / AviSynthPlus

AviSynth with improvements
http://avs-plus.net
930 stars 74 forks source link

Subtitle font_filename with already installed fonts #377

Open determin1st opened 6 months ago

determin1st commented 6 months ago

when there is installed "Input Mono" fontset, the

Subtitle("test", font_filename="InputMono-Italic.ttf")

does not output with italic version of "Input Mono", but instead it uses system installed default regular font.

qyot27 commented 6 months ago

Is the .ttf file you're trying to load in the same directory as the script? font_filename is for non-installed fonts, i.e. you're going to give it an absolute or relative path to an actual .ttf file. If the fonts are already installed, you need to use font with the reported name of the font, not the filename.

determin1st commented 6 months ago

i tried both absolute and relative paths. the problem is that the "Font Name" is occupied, because the font is already installed, but it is installed as a group of files which have the same "Font Name" name. so i when i put the correct path, it uses the regular font from that group.

qyot27 commented 6 months ago

font takes modifiers just fine.

BlankClip(pixel_type="RGBP")
Subtitle("InputMono ExLight",font="InputMono ExLight")
Subtitle("InputMono Bold",font="InputMono Bold",align=4)
Subtitle("InputMono Italic",font="InputMono Italic",align=1)

Subtitle("InputMono ExLightIta",font="InputMono ExLightIta",align=8)
Subtitle("InputMono LightIta",font="InputMono LightIta",align=5)
Subtitle("InputMono ThinIta",font="InputMono ThinIta",align=2)

Subtitle("InputMono Light",font="InputMono Light",align=9)
Subtitle("InputMono",font="InputMono",align=6)
Subtitle("InputMono Black",font="InputMono Black",align=3)

test

determin1st commented 6 months ago

oke, "modifiers" (arent in manual) might workaround a name based collision

qyot27 commented 6 months ago

I mean, I call them 'modifiers' but they are part of the reported name of the font in the Windows Font Manager if you look at the title bar. It can be trickier if you're viewing them in some other font application, though - on Ubuntu Budgie, the font viewer doesn't show that the name for Thin Italic is 'ThinIta' (it just lists 'Thin Italic' a couple of times as the Style name and in smaller text underneath the family name in the title bar), and it very well might be that fontconfig treats naming conventions differently than the Windows system fonts do, or the macOS one does (if it doesn't also just use fontconfig), which could potentially change the behavior if different OSes handle font names in different ways.

Not that any of those asides matter in this issue, since font styling in AviSynth+'s internal Subtitle/MessageClip-based functions is only available on Windows at the moment.

determin1st commented 6 months ago

i dont understand why a name conflict should appear in the case of explicit file parameter. the name, as i understand it, is only a hint for some resource api fetcher. yes, those names appear in the font manager's titlebar as you say.

pinterf commented 6 months ago

For additional fonts the AddFontResourceExwindows API is called. It simply makes the font available for any later use. But for Subtitle you still have to specify which font would you use from Windows' font set. Once loaded, subsequent calls do not need another external reference. The recource is added already at the first call. I found this example from time when it was added to Avisynth.

Subtitle("Roboto Test", x=-1, y=50, font_width=0, font="Dancing Script1002", font_filename="DancingScript-Regular.ttf")
Subtitle("Roboto Test", x=-1, y=75, font_width=0, font="Dancing Script1002")
determin1st commented 6 months ago

For additional fonts the AddFontResourceExwindows API is called. It simply makes the font available for any later use. But for Subtitle you still have to specify which font would you use from Windows' font set. Once loaded, subsequent calls do not need another external reference. The recource is added already at the first call. I found this example from time when it was added to Avisynth.

Subtitle("Roboto Test", x=-1, y=50, font_width=0, font="Dancing Script1002", font_filename="DancingScript-Regular.ttf")
Subtitle("Roboto Test", x=-1, y=75, font_width=0, font="Dancing Script1002")

for example, could be:

fontName = LoadFont("DancingScript-Regular.ttf")
Subtitle("Roboto Test", x=-1, y=50, font_width=0, font=fontName)
Subtitle("Roboto Test", x=-1, y=75, font_width=0, font=fontName)
pinterf commented 6 months ago

A single font file can contain multiple fonts, with different family names. Otherwise, yes, good idea, but the imaginary LoadFont may not be appropriate if the font file would contain multiple names. But I admit that for most TTF font files this may work.

This is a powershell script for listing the family names for a variable font file. Download for example Google Inter https://fonts.google.com/specimen/Inter.

Add-Type -AssemblyName System.Drawing
$fontCollection = New-Object System.Drawing.Text.PrivateFontCollection
$fontCollection.AddFontFile("c:\use_absolute_path\Inter-VariableFont_slnt,wght.ttf")
$fontFamilies = $fontCollection.Families
$fontFamilies | ForEach-Object { $_.Name }

result is:

Inter
Inter Black
Inter Black Italic
Inter ExtraBold
Inter ExtraBold Italic
Inter ExtraLight
Inter ExtraLight Italic
Inter Italic
Inter Light
Inter Light Italic
Inter Medium
Inter Medium Italic
Inter SemiBold
Inter SemiBold Italic
Inter Thin
Inter Thin Italic
determin1st commented 6 months ago

oke, i see, there are no arrays in avisynth.. so thats problematic.

by reusing some extension system of, for example, PHP or Python, a kind of cross-platform solution could have existed:

$fonts = AviSynth\LoadFont("DancingScript-Regular.ttf");
var_dump($fonts);
$clip  = AviSynth\Subtitle($clip, "Roboto Test", x: -1, y: 50, font_width: 0, font: $fonts[0]);
$clip  = AviSynth\Subtitle($clip, "Roboto Test", x: -1, y: 75, font_width: 0, font: $fonts[0]);

but those are way more imaginary than LoadFont().

pinterf commented 6 months ago

Actually we have arrays in Avisynth+ So this would be valid:

fontNames = LoadFont("MultiFont.ttf")
Subtitle("Roboto Test", x=-1, y=50, font_width=0, font=fontNames[0])
Subtitle("Roboto Test", x=-1, y=75, font_width=0, font=fontNames[1])

My problem that this is supposed to work in "Text" for fixed bdf fonts as well, and the resourses must be free'd up when script environment is closing (I'm already thinking about the actual windows-linux-SubTitle-Text implementation, which would not be easy)