Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fontconfig does not recognize Cascadia as 'monospace' #118

Closed
nobeh opened this issue Sep 25, 2019 · 15 comments
Closed

fontconfig does not recognize Cascadia as 'monospace' #118

nobeh opened this issue Sep 25, 2019 · 15 comments

Comments

@nobeh
Copy link

nobeh commented Sep 25, 2019

Environment

Cascadia Code version number: 1909.16
Application (with version) used to display text: GNU fontconfig
OS platform and version: Ubuntu 19.04 on GNOME 3.32
fontconfig 2.13.1

Steps to reproduce

I am using fonts.conf as:

  <alias>
    <family>monospace</family>
    <prefer>
      <family>Cascadia Code</family>
    </prefer>
  </alias>

after updating the fontconfig and running fc-cache -rfv, Cascadia Code is listed as a new font however fc-query does not match the font with monospace:

$ fc-match monospace
DejaVuSansMono.ttf: "DejaVu Sans Mono" "Book"

where trying to query the font file:

$ fc-query ~/.fonts/Cascadia.ttf 
Pattern has 25 elts (size 32)
	family: "Cascadia Code"(s)
	familylang: "en"(s)
	style: "Regular"(s)
	stylelang: "en"(s)
	fullname: "Cascadia Code"(s)
	fullnamelang: "en"(s)
	slant: 0(i)(s)
	weight: 80(f)(s)
	width: 100(f)(s)
	spacing: 100(i)(s)
	foundry: "SAJA"(s)
	file: "~/.fonts/Cascadia.ttf"(s)
	index: 0(i)(s)
	outline: True(s)
	scalable: True(s)
	charset: 
	0000: 00000000 ffffffff ffffffff 7fffffff 00000000 00000000 00800000 00800000
	0020: 33000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
	0022: 00040000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
(s)
	lang: fj|ho|ia|ie|io|nr|om|so|ss|st|sw|ts|uz|xh|zu|kj|kwm|ms|ng|rn|rw|sn|za(s)
	fontversion: 125118709(i)(s)
	capability: "otlayout:DFLT otlayout:latn"(s)
	fontformat: "TrueType"(s)
	decorative: False(s)
	postscriptname: "CascadiaCode"(s)
	color: False(s)
	symbol: False(s)
	variable: False(s)

The expected behavior as common with fontconfig is that when monospace is replaced with a specific font, any application using monospace would get Cascadia Code as the actual font.

The expected behavior is there for example with Fira Code. In addition, if I manually set the monospace font to Cascadia Code, it works. So, maybe an issue with fontconfig or how to set the name in fonts.conf?

@madig
Copy link
Contributor

madig commented Sep 26, 2019

You might be stumbling over resolution order or something. Insert Cascadia Code as the first entry for monospace in /etc/fonts/conf.avail/60-latin.conf or whereever that is in your distro. If that works, you'll have to figure out how to prepend it from your personal fonts.conf.

@kdien
Copy link

kdien commented Sep 29, 2019

@nobeh I've always used /etc/fonts/local.conf and had the same issue just with this font, until I added binding="same" attribute for the <edit> tag. Here's the full file content

<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'fonts.dtd'>
<fontconfig>
    <match target="pattern">
        <test qual="any" name="family">
            <string>monospace</string>
        </test>
        <edit name="family" mode="assign" binding="same">
            <string>Cascadia Code</string>
        </edit>
    </match>
</fontconfig>

Maybe give it a try and see if it works for you.

@nobeh
Copy link
Author

nobeh commented Sep 30, 2019

@madig @kdien Thanks for the suggestions.

@kdien This works now for me both for binding="same" and binding="strong". That's still interesting to understand why we did not need the above config snippet for another monospace font but it is necessary for Cascadia Code?

@kdien
Copy link

kdien commented Oct 1, 2019

@nobeh yeah it's a bit baffling to me, though as mentioned I've always used this config (minus binding="same") for setting default monospace font. Probably just some missing metadata that makes Cascadia Code require that extra attribute. Despite being open source, this font is still a Windows-first font (bundled with the new Windows Terminal).

@DHowett-MSFT
Copy link

DHowett-MSFT commented Oct 1, 2019

still a Windows-first font

Not because we're malicious (edit: I said this not because anybody implied it, but because I wanted to be cheeky. Sorry :P), just because we don't know exactly what font metrics we need to ask for to make sure it works with freetype/xft/fontcache.

@DHowett-MSFT
Copy link

(If you know what we need to change, we'd love to try it!)

@kdien
Copy link

kdien commented Oct 1, 2019

@DHowett-MSFT I know you guys aren't malicious, otherwise there wouldn't even be this repo lol
As for what needs to be changed, I really have no idea since I'm just a font user, not a font designer/developer. "Missing metadata" was totally just a guess on my part. Maybe the fact that adding binding="same" made it work could somewhat point to something(?).

@madig
Copy link
Contributor

madig commented Oct 1, 2019

A quick look at https://gitlab.freedesktop.org/fontconfig/fontconfig/blob/master/src/fcfreetype.c#L2382-2450 shows that it may be looking at the number of different advance widths. The ligatures will derail that logic.

Maybe one needs to trick fontconfig into accepting it as a monospace font, but I find fontconfig configuration too obscure to know how :) The binding="same" might point to the way the rule was inserted.

@DHowett-MSFT
Copy link

I think our ligature advance widths are actually the same as normal characters -- they all hang off to the left so they render in the right place.

[ > ][ > ][ > ]
turns into
[LIG][LIG][>>>]
where
          [>>>]

renders like
[  >   >  |>  ]

where |] marks the cell.

The LIG glyphs have the same advances as the rest of the font to make sure there's space for the overhang on the left.

@kdien
Copy link

kdien commented Oct 2, 2019

Yeah I don't think it's the ligatures either since Fira Code works fine without the extra config.

@aaronbell
Copy link
Collaborator

OK, so I took a look at the Glyphs source for Fira Code and the only thing that stood out to me as a possible reason why you're running into this issue is that Cascadia Code has a partial PANOSE set, whereas Fira Code has a complete one.

PANOSE is an older system intended to help OS find a fallback font when a given font is not available. So if it knew it was looking for a black weight font with bracketed serifs, in normal width, PANOSE could tell them if XXX font has those characteristics. However, IIRC very few applications actually implemented this functionality and so font foundries kind of stopped bothering to implement it. Maybe GNU fontconfig cares about that?

For fun I also looked at DejaVu Sans Mono and it also has a full PANOSE.

No idea if it'll actually solve the issue, but we can give it a go.

@aaronbell aaronbell mentioned this issue Nov 12, 2019
17 tasks
@aaronbell
Copy link
Collaborator

I believe I may have tracked down the reason for this issue. Per this discussion, older implementations of Freetype (and Windows??) can have issues is not every glyph in a font is the same width.

Cascadia Code includes Combining Diacritical Marks that are zero width. As such, Freetype does not interpret these fonts as monospaced. Deja Vu Sans Mono, on the other hand, does not include these characters at all which may help it with being recognized by fontconfig.

By using the "binding" attribute, you're essentially telling the system, "don't try to interpret anything or change anything, just use this font". That is probably bypassing Freetype's normal process. So that is why the font does work.

The above aside, I note that the latest version of Fira Code also has zero width characters, but per @nobeh that font worked ok? So there might be another issue still.

@aaronbell
Copy link
Collaborator

Hi @nobeh, can you try the latest version and let me know if the situation is improved? Else I'll close this issue. Thanks!

@nobeh
Copy link
Author

nobeh commented Nov 24, 2019

@aaronbell Thanks a lot for all the details and nice work on this ticket. I just tried the latest version of the font. I confirm that with the initial fonts.conf that I posted above, the font is now consistently working across different apps such edits, terminal and browsers. This is also what fc-query shows which is different from the version I reported for:

Pattern has 25 elts (size 32)
    family: "Cascadia Code"(s)
    familylang: "en"(s)
    style: "Regular"(s)
    stylelang: "en"(s)
    fullname: "Cascadia Code Regular"(s)
    fullnamelang: "en"(s)
    slant: 0(i)(s)
    weight: 80(f)(s)
    width: 100(f)(s)
    spacing: 100(i)(s)
    foundry: "SAJA"(s)
    file: "./Cascadia.ttf"(s)
    index: 0(i)(s)
    outline: True(s)
    scalable: True(s)
    charset: 
    0000: 00000000 ffffffff ffffffff 7fffffff 00000000 ffffffff ffffffff ffffffff
    0001: ffffffff ffffffff ffffffff ffffffff 20048000 00018003 00000000 fc000cc0
    0002: 0f000000 008c0000 02000000 00040000 00000000 18000000 3f0002c0 00000000
    0003: 08041fdf 000001c8 00000000 40300000 ffffd7f0 fffffffb 0080ffff 00000000
    0004: ffffffff ffffffff ffffffff 00000000 0ccf0000 0ccfc00c 03008001 0000c30c
    000e: 00000000 80000000 00000000 00000000 00000000 00000000 00000000 00000000
    001e: 00000000 00000000 00000000 00000000 4000003f ffffffff ffffffff 03ffffff
    0020: 7fb80000 560d0147 00000010 83f10000 000003ff 77367fdb 00000000 00000000
    0021: 00c80020 00004044 78000000 00000000 003f0000 00000100 00000000 00000000
    0022: c6268044 00000a00 00000100 00000033 00000000 00000000 00000000 00000000
    0023: 00010000 00000003 00000000 00000000 00000000 00008000 00000000 00000000
    0025: ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
    0026: 02000000 00000000 00000005 00000469 00000000 00000000 00000000 00000000
    0027: 00080000 00000000 00000000 00003080 00000000 00000000 00000000 00000000
    0028: ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
    002b: fffc0000 0000ffff 00000000 00000000 00000000 00000000 00000de0 00000000
(s)
    lang: aa|af|av|ay|az-az|be|bg|bi|bin|br|bs|bua|ca|ce|ch|co|cs|cy|da|de|el|en|eo|es|et|eu|fi|fj|fo|fr|fur|fy|gd|gl|gn|gv|haw|ho|hr|hu|ia|ig|id|ie|ik|io|is|it|kaa|ki|kk|kl|kum|kw|ky|la|lb|lez|lt|lv|mg|mh|mk|mo|mt|nb|nds|nl|nn|no|nr|nso|ny|oc|om|os|pl|pt|rm|ro|ru|se|sel|sk|sl|sm|sma|smj|smn|so|sq|sr|ss|st|sv|sw|tg|tk|tl|tn|to|tr|ts|tt|tyv|uk|uz|vi|vo|vot|wa|wen|wo|xh|yap|zu|an|crh|csb|fil|hsb|ht|jv|kj|ku-tr|kwm|lg|li|mn-mn|ms|na|ng|nv|pap-an|pap-aw|rn|rw|sc|sg|sn|su|ty|za(s)
    fontversion: 125253059(i)(s)
    capability: "otlayout:DFLT otlayout:cyrl otlayout:latn"(s)
    fontformat: "TrueType"(s)
    decorative: False(s)
    postscriptname: "CascadiaCode-Regular"(s)
    color: False(s)
    symbol: False(s)
    variable: False(s)

and

$ fc-match monospace
Cascadia.ttf: "Cascadia Code" "Regular"

❤️ your attention and work on this!

@aaronbell
Copy link
Collaborator

So glad it is working for you now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants