Closed moi15moi closed 1 year ago
Oh interesting. (Sorry, didn't do a good job keeping on top of notifications)
I think https://learn.microsoft.com/en-us/windows/win32/api/dwrite_3/nf-dwrite_3-idwritefontcollection2-getmatchingfonts would might let us find this -- this interface only works on Windows 10 (everything else so far is Win7+)
The other option would be to expose GetInformationalStrings as font.information
or simillar and then we can just filter in Python. I guess it depends on your usecase?
The other option would be to expose GetInformationalStrings as
font.information
or simillar and then we can just filter in Python. I guess it depends on your usecase?
Not really. For example, if I wanna get the font Arial Narrow
, I would need to do something like that which doesn't make sense since I can't know that the family name is "Arial" to make it work:
from windows_fonts import FontCollection
fonts = FontCollection()
family = fonts['Arial']
for variant in family:
if variant.information["DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES"] == "Arial Narrow":
print(f"Found font at: {variant.filename}")
Got it. Using the GetMatchingFonts seems like the way to go then.
I've got some prototype code paths working (including my debug prints):
>>> c = __import__('windows_fonts').FontCollection()
>>> c.get_matching_fonts('Arial Narrow')
Set contains 4
Got font_ref IDWriteFontFaceReference(IUnknown(0x2332455d4a0))
Got font3 IDWriteFontFace3(IUnknown(0x233244f0a50))
Got ifamily IDWriteFontFamily(IUnknown(0x2332455e2b0))
Got family FontFamily(IDWriteFontFamily(IUnknown(0x2332455e2b0)))
<FontVariant name=Narrow, family=<FontFamily name="Arial">, style=Style.NORMAL weight=Weight.REGULAR>
The interface will likely be something like this once it's generalized:
c = FontCollection()
c.get_matching_variants(win32_family_names="Arial Narrow")
(I.e. you'll be able to search by any of the key names I added in #8 (one or more of them)
Cool :) Could you do an release on pypi?
v1.0.0 published on pypi btw.
Problem
The PR #5 introduce GetMatchingFonts. The problem from this is that DWrite seems to, sometimes, use the nameID 16 (typographic family name), so it can create problem when you search a font by the name of the nameID 1 (family name).
Solution
You could use GetInformationalStrings with the style
DWRITE_INFORMATIONAL_STRING_WIN32_FAMILY_NAMES
.Example:
Let's imagine you wanna search the font named
Arial Narrow
.Arial Narrow
isn't considered a family by windows, but if you open the font with fonttools, you will see that it has a nameID 1 (which is font family) that isArial narrow
.This mean that currently, it is impossible to find this font. It would be nice to have an alternative to find it.
Here is the NamingTable of Arial Narrow