Font with Built-In Syntax Highlighting
The article presents a method for syntax highlighting in hand-coded websites by embedding it into a font using OpenType features, offering a simpler solution with limitations in flexibility and complexity.
Read original articleThe article discusses the challenges of hand-coding websites, particularly the difficulty of implementing syntax highlighting for code snippets without relying on external libraries. The author proposes a novel solution by embedding syntax highlighting directly into a font using OpenType features, specifically the COLR table for color and contextual alternates for text substitution. This method allows for syntax highlighting without JavaScript or additional CSS themes, resulting in cleaner HTML and faster performance. The modified font, Monaspace Krypton, includes colored glyphs that replace specific code syntax with their colored variants. While this approach simplifies the process and eliminates the need for maintenance, it has limitations, such as the inability to easily modify the syntax highlighter or support complex highlighting scenarios. The author provides a demonstration of this technique and outlines both the advantages and drawbacks, emphasizing that while it is not a complete replacement for traditional syntax highlighters, it serves well for simpler needs.
- The article explores the challenges of hand-coding websites, focusing on syntax highlighting.
- A solution is proposed using OpenType features to embed syntax highlighting directly into a font.
- The modified font, Monaspace Krypton, allows for syntax highlighting without JavaScript.
- This method simplifies code presentation but has limitations in flexibility and complexity.
- The author provides a demonstration and discusses the pros and cons of this approach.
Related
Microfeatures I love in blogs and personal websites
The article explores microfeatures for blogs and websites inspired by programming concepts. It highlights sidenotes, navigation tools, progress indicators, and interactive elements to improve user experience subtly. Examples demonstrate practical implementations.
Beyond monospace: the search for the perfect coding font
Designing coding fonts involves more than monospacing. Key considerations include hyphens resembling minus signs, aligning symbols, distinguishing zero from O, and ensuring clarity for developers and type designers. Testing with proofing strings is recommended.
Writing HTML by hand is easier than debugging your static site generator
The blog author discusses challenges of static site generators versus manual HTML coding, citing setup complexities and advocating for simplicity, stability, and control in website management. Emphasizes static data benefits.
Why I Prefer RST to Markdown
The author prefers reStructured Text (rST) over Markdown for technical documentation, citing its complex structure, custom directives, and better management of content across formats, despite its less attractive syntax.
Features I'd like to see in future IDEs
Proposed improvements for IDEs include queryable expressions for debugging, coloration of comments, embedding images, a personal code vault for snippets, and commit masks to prevent accidental code commits.
- Many commenters express excitement about the innovative approach, calling it a "fun hack" and praising its simplicity and effectiveness.
- Some users point out limitations, such as issues with background color compatibility and the inability to customize themes without regenerating the font.
- Technical critiques arise regarding the use of OpenType features, with suggestions for improvements and clarifications on how certain features should be implemented.
- Several commenters highlight potential applications and use cases, including in text areas and for presentations, while others question the practicality of the method compared to traditional syntax highlighting tools.
- There is a mix of admiration for the creativity involved and caution regarding the implications of using fonts in this way, with some expressing concerns about security and performance.
One small note: the post seems slightly confused about the use of OpenType features, as it calls for:
font-feature-settings: "colr", "calt";
but there's no 'colr' feature tag in the font's OpenType layout tables, so that's meaningless here.I suppose this was intended to "activate" the color glyph table (COLR) in the font; but that isn't an OpenType layout feature that would be controlled by font-feature-settings, and doesn't need to be "turned on" like this.
(In addition, the 'calt' feature is (according to the spec[1]) supposed to be active by default, so it shouldn't be necessary to explicitly set it either. And indeed, the font works for me in both Firefox and Chrome without this rule; sadly Safari doesn't seem to handle it.)
[1] https://learn.microsoft.com/en-gb/typography/opentype/spec/f...
sub Quote @Char' by @CharQuoted;
sub Backslash.quoted Backslash' by Backslash.quoted2;
sub Backslash.quoted Quote' by Quote.escaped;
sub [@NonQuoteQuoted Quote.escaped Backslash.quoted2] @Char' by @CharQuoted;
I don't know if context can be carried between different lines.Impossible before. It can't be less invasive: _original text stays intact_ - no wrapping tags.. no JS.. just works with userContent.css.. - simply another dimension.
> I'm also not an OpenType expert, so I'm sure the substitution logics could be improved upon. I'm open to sharing the modified source file to anyone interested. If you have any ideas, suggestions or feedback, let me know. You can reach me at hlotvonen@gmail.com.
- so, how far can it be improved then ?!
- what other font editors moreover to Glyph (mac only) have good support for advanced contextual alternates ?
Really fun to see a font that can do this though. Never would've expected it.
Edit: Perhaps this is a reminder that custom fonts are a potential attack vector for security-sensitive websites since font rendering runs highly-complex programs, probably in a language that isn't memory safe.
> This site doesn't use cookies or link to any third party site.
I love this. Thank you!*every* monospace text in the browser *syntax colored * :)
(as expected.. wherever it does make sense or not.. - viewsource, text files, parts of pages.. :)
Could anybody make an online tool to choose another monospace font, programming language and color theme, and generate a syntax-highlighted font?
It would be so cool if you could override the contextual language (likely determined by file extension or explicitly in markdown or whatever) with a different one just by applying a font.
Ironically, it is Microsoft which developed CPAL/COLR (at least intially), and this is the least atrocious format across colored suggestions. The other options are SVG-in-OpenType (Adobe/Mozilla; https://helpx.adobe.com/fonts/using/ot-svg-color-fonts.html), PNG-in-OpenType/SBIX (Apple; https://developer.apple.com/fonts/TrueType-Reference-Manual/...), and CustomBitmap-in-OpenType/CBDT (Google; https://fonts.google.com/noto/use).
All of them are now OpenType standards, for better or worse.
>For example, words within <p> tags that are JS keywords will be always highlighted
Can be used when syntax highlighting is not available.
This is the most interesting post in a long time, bravo!
<pre contenteditable="plaintext-only" class=""><code>...
I would love to see separate fonts for HTML, CSS, and JS only, as I would want to use it for lightweight HTML highlighting in textareas in a CMS.
does anyone know the original target of this OpenType feature? Struggling to imagine where it would fit apart from code highlighting
I heard about security issues related to fonts and wondered “how in the world”. This helps clarify why.
It simply should not be possible.
As something practical, the author started with the wrong tool for the job (JavaScript), and then used an even more wrong tool for the same job (The font itself!)
Just use a code editor with Syntax Highlighting! Vim solved this decades ago!
Very interesting read, but don't try this at home.
Edit: Pandora's box is opened, I can see Jira and GitHub rushing to add support for this on their websites starting tomorrow =)
Related
Microfeatures I love in blogs and personal websites
The article explores microfeatures for blogs and websites inspired by programming concepts. It highlights sidenotes, navigation tools, progress indicators, and interactive elements to improve user experience subtly. Examples demonstrate practical implementations.
Beyond monospace: the search for the perfect coding font
Designing coding fonts involves more than monospacing. Key considerations include hyphens resembling minus signs, aligning symbols, distinguishing zero from O, and ensuring clarity for developers and type designers. Testing with proofing strings is recommended.
Writing HTML by hand is easier than debugging your static site generator
The blog author discusses challenges of static site generators versus manual HTML coding, citing setup complexities and advocating for simplicity, stability, and control in website management. Emphasizes static data benefits.
Why I Prefer RST to Markdown
The author prefers reStructured Text (rST) over Markdown for technical documentation, citing its complex structure, custom directives, and better management of content across formats, despite its less attractive syntax.
Features I'd like to see in future IDEs
Proposed improvements for IDEs include queryable expressions for debugging, coloration of comments, embedding images, a personal code vault for snippets, and commit masks to prevent accidental code commits.