-
Notifications
You must be signed in to change notification settings - Fork 715
[css-highlight-api] Support for some font styles and decorations #8355
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
Comments
Decorations like underlines and strikethroughs are already supported via Sebastian |
Oh nice! I was basing my assumption on an outdated article apparently. A way to make text italic and/or bold seems to be the only thing missing before this API can be used fully for syntax highlighting then. I mean it's not the end of the world if those are not supported, but ideally a modern syntax highlighter should support those. |
Custom highlights support the same set of properties as all other highlight pseudos. These are defined in the CSS pseudo spec: https://drafts.csswg.org/css-pseudo-4/#highlight-styling. @fantasai Has adding support for bold/italic come up in past discussions? |
I would like to see this as well. However, I suspect that bold and italic would be considered "layout-affecting" because font weight and style can make things wider or narrower. |
@samuelbradshaw not if the font is monospaced, presumably. Like in an editor kind of situation we want each character to have the same height and width as any other character, it doesn't matter if it's italic or bold or not. |
To avoid being layout affecting, we'd need more than just a monospaced font. We'd need the two fonts we're swapping from/to to be:
All in all, swapping fonts is, in the general case, layout affecting, and I doubt that's going to be practical here. |
I feel like this could be special cased in practice though, like if the user explicitly wants to make the font bold or italic via custom highlights then hopefully the browser could measure the box that a regular character would occupy, render the bold or italic character in there, and chop away anything that overflows, forcing the highlight to not be layout affecting, whether it would have been layout affecting all things considered or not. Like I think there's enough good that would be unlocked by this capability that is worth considering. |
Another property missing seems |
|
All of the text-decoration-* properties are supported, including color. From the Pseudos 4 spec: "text-decoration ... and its associated properties (including text-underline-position and text-underline-offset)". The spec is only calling out the text decoration properties that begin with text-underline- to be clear that those are decoration properties. |
@schenney-chromium empirically under Chrome v116.0.5845.97, under Electron v26.1.0, |
Is it possible to support |
@fabiospampinato I have no idea why that would not be working. There are numerous WPT tests covering the feature that might guide how to use it (see https://wpt.fyi/results/css/css-highlight-api/painting?label=experimental&label=master&aligned). It might just be that the feature is not enabled. @yisibl Background-image poses significant challenges, first among them being the definition of the positioning area to allow for good behavior on highlights of numerous sizes and shapes, with different fonts and highlighted content. I don't think there's any desire to standardize such a thing at this time. |
What if we added this with the mentioned bullets as guidelines to authors? There are fonts out there that tick all boxes I think, e.g. https://fonts.google.com/specimen/Roboto+Mono/tester The author need is syntax highlighting source code, with a special mention of highlighting markdown à la iA Writer: |
@fabiospampinato, could you emulate bold by using For those familiar with the spec and what might be considered "layout-affecting," would |
Not really, the effect is pretty different 🤔 Ideally I think we want the real thing, real bold variants, when it's possible to do that. For some characters of some fonts this should be implementable, i.e. it's possible for bold characters to occupy the same space as non-bold characters, especially in monospaced fonts using for development, which is where the Highlight API provides sort of a step change improvement. |
For plaintext font styling (bold/ italic/ ...) without excessive markup, OpenType contextual alternates are another option (unlike the css highlight API this also works with Instead of defining syntax rules in javascript, they are defined within the font file using the OpenType Feature File Specification. These rules enable character substitutions, such as replacing regular characters with their italic or bold variants. (Intro into OpenType programming). Additionally one could use certain markup characters, e.g. markdown to annotate the plaintext with a customised font (example, demo). This technique can be combined with Colr fonts, as demonstrated in this example (original article), allowing for fully featured styling and syntax highlighting without any html markup transformation. ![]() With this approach, only hyperlinks would require rendering as actual DOM nodes. There are some discussions here on hackernews regarding performance of shifting syntax highlighting from JS to the font. … Though I would favour if |
The additional text decoration styles work great on Chrome! Unfortunately, they're not yet supported in Safari. I haven't found a good workaround so far (other than waiting and hoping for a fix). See https://stackoverflow.com/questions/79060854/css-custom-highlight-api-with-underlined-text-on-safari |
Uh oh!
There was an error while loading. Please reload this page.
I'm currently building a new code editor for the web that uses the CSS Custom Highlight API for syntax highlighting, with the idea being that it would simplify things significantly while at the same time improve performance.
But, some basic forms of font styles and decoration (italic, bold, underline, strikethrough) are not supported by this API. As I understand it only properties that can be applied efficiently are supported, but arguably a subset of the
font-style
andfont-decoration
properties can be applied efficiently in some scenarios, even if this capability were limited to monospaced fonts it would unconstraint my use case, which would be amazing.As far as I know the folks at VS Code are also exploring using highlights for syntax highlighting, which would potentially enable optimized syntax highlighting for millions of developers worldwide, so it'd be extra nice if highlights could be used for 100% of normal grammar-based syntax highlighting, rather than the current 99%.
The text was updated successfully, but these errors were encountered: