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

getComputedStyle() does not respect the Fallback weights algorithm #22496

Open
dumblob opened this issue Nov 23, 2022 · 0 comments
Open

getComputedStyle() does not respect the Fallback weights algorithm #22496

dumblob opened this issue Nov 23, 2022 · 0 comments
Labels
Content:CSS Cascading Style Sheets docs effort: small This task is a small effort. help wanted If you know something about this topic, we would love your help!

Comments

@dumblob
Copy link

dumblob commented Nov 23, 2022

MDN URL

https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight

What specific section or headline is this issue about?

No response

What information was incorrect, unhelpful, or incomplete?

There is missing information that getComputedStyle() on an element with a weight style which the font does not support actually does not return the real shown font weight but the weight style number thus not reflecting the result of the Fallback weights algorithm.

What did you expect to see?

A warning that getComputedStyle() has unintuitive behavior regarding font weight (especially surprising when one uses a variable font which supports "infinite" range of weights like Inter).

Do you have any supporting links, references, or citations?

No response

Do you have anything more you want to share?

Hacky test code changing font weight based on font size:

  function f( e, x ){
    return Math.trunc( parseInt( window.getComputedStyle( e )['font-weight'], 10 ) *
        // 14 font size => 1.005 font weight coefficient, 32 => 1.11, ...
        ( (Math.abs(x -14) * (1.11 -1.005)/(32 -14) ) +1.005 ) )
  }
  for( const e of document.querySelectorAll( '*, *::after, *::before, *::first-letter, *::first-line, *::selection, *::backdrop' ).values() ){
    try {
      const x = parseFloat( window.getComputedStyle( e )['font-size'] )
      const y = f( e, x )
      e.style.setProperty( 'font-weight',  y, 'important' )
      // not a variable font (or one which does not support fine-grained weight settings)
      // does not work as of 20221123_124058+0100 in newest Chromium
      //if( f() !== y ){
        e.style.setProperty( 'font-family', 'Inter var', 'important' )
      //}
    } catch {}
  }
@font-face {
  font-family: 'Inter var';
  font-style: oblique 0deg 10deg;
  font-weight: 100 900;
  font-display: swap;
  src: url( 'https://rsms.me/font-files/Inter.var.woff2?v=3.19' ) format( 'woff2' );
  font-feature-settings: "zero" 1, "ss01" 1;
}

MDN metadata

Page report details
@github-actions github-actions bot added Content:CSS Cascading Style Sheets docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Nov 23, 2022
@sideshowbarker sideshowbarker added help wanted If you know something about this topic, we would love your help! effort: small This task is a small effort. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Nov 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:CSS Cascading Style Sheets docs effort: small This task is a small effort. help wanted If you know something about this topic, we would love your help!
Projects
None yet
Development

No branches or pull requests

2 participants