TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Font with Built-In Syntax Highlighting

665 点作者 microflash9 个月前

46 条评论

jfk139 个月前
What a fun hack!<p>One small note: the post seems slightly confused about the use of OpenType features, as it calls for:<p><pre><code> font-feature-settings: &quot;colr&quot;, &quot;calt&quot;; </code></pre> but there&#x27;s no &#x27;colr&#x27; feature tag in the font&#x27;s OpenType layout tables, so that&#x27;s meaningless here.<p>I suppose this was intended to &quot;activate&quot; the color glyph table (COLR) in the font; but that isn&#x27;t an OpenType layout feature that would be controlled by font-feature-settings, and doesn&#x27;t need to be &quot;turned on&quot; like this.<p>(In addition, the &#x27;calt&#x27; feature is (according to the spec[1]) supposed to be active by default, so it shouldn&#x27;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&#x27;t seem to handle it.)<p>[1] <a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-gb&#x2F;typography&#x2F;opentype&#x2F;spec&#x2F;features_ae#calt" rel="nofollow">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-gb&#x2F;typography&#x2F;opentype&#x2F;spec&#x2F;f...</a>
评论 #41251583 未加载
评论 #41251484 未加载
ashton3149 个月前
This is a horrible, horrible hack and it&#x27;s terrifying and I am aghast at how amazing this is. Such a wonderful abuse of OpenType contextual alternatives. Well done and thank you for sharing!!
评论 #41251229 未加载
评论 #41252726 未加载
评论 #41252823 未加载
penteract9 个月前
I think the author has underestimated the power of the substitution rules - it looks like it should be possible to drag a state machine along, hence recognise any regular language. Something like the following should handle quoted strings (untested and needs every character to be listed out in the definitions of @Char, @CharQuoted and @NonQuoteQuoted):<p><pre><code> sub Quote @Char&#x27; by @CharQuoted; sub Backslash.quoted Backslash&#x27; by Backslash.quoted2; sub Backslash.quoted Quote&#x27; by Quote.escaped; sub [@NonQuoteQuoted Quote.escaped Backslash.quoted2] @Char&#x27; by @CharQuoted; </code></pre> I don&#x27;t know if context can be carried between different lines.
评论 #41257321 未加载
评论 #41258283 未加载
评论 #41259524 未加载
thro19 个月前
Kudos * 1000 ! ! !<p>Impossible before. It can&#x27;t be less invasive: _original text stays intact_ - no wrapping tags.. no JS.. just works with userContent.css.. - simply another dimension.<p>&gt; I&#x27;m also not an OpenType expert, so I&#x27;m sure the substitution logics could be improved upon. I&#x27;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.<p>- so, how far can it be improved then ?!<p>- what other font editors moreover to Glyph (mac only) have good support for <i>advanced contextual alternates</i> ?
评论 #41249775 未加载
评论 #41249478 未加载
评论 #41252382 未加载
评论 #41248737 未加载
kevingadd9 个月前
Really a minor nitpick, but &quot;It&#x27;s as fast as plain text, because it is plain text&quot; is definitely not true. Evaluating all these rules when shaping the text is not gonna be cheap, and if you overuse this type of font your end users&#x27; CPUs will probably be screaming. As would anyone who&#x27;s implemented parts of the OpenType spec from scratch before. Worse still, this doesn&#x27;t just impact rendering, since the output of shaping determines how <i>big</i> elements in the document are, so things like scrolling and window resizing and text selection can all lag.<p>Really fun to see a font that can do this though. Never would&#x27;ve expected it.
评论 #41251297 未加载
davidfstr9 个月前
I find it amusing that the motivation for creating a complex font program that supports syntax highlighting internally is the desire to avoid a complex syntax highlighter JavaScript library. The complexity is still there; it&#x27;s just been moved around.<p>Edit: Perhaps this is a reminder that <i>custom fonts</i> are a potential attack vector for security-sensitive websites since font rendering runs highly-complex programs, probably in a language that isn&#x27;t memory safe.
评论 #41257444 未加载
评论 #41271276 未加载
dotancohen9 个月前
If you read to the end, the page footer has this nice message:<p><pre><code> &gt; This site doesn&#x27;t use cookies or link to any third party site. </code></pre> I love this. Thank you!
评论 #41250856 未加载
luminarious9 个月前
This seems like something that could be automated with code? It feels like a genuinely useful way of solving highlighting for most common use cases.
评论 #41250819 未加载
评论 #41250868 未加载
评论 #41250810 未加载
extheat9 个月前
This is actually really cool. I can see the usages in textarea and inputs but also even in plain HTML documents for large code formatting. No need for exploding the DOM with tons of spans and other HTML tags, if you can have the hardware accelerated font rendering software take care of everything. I’d imagine that would save lots of memory and CPU.
评论 #41251727 未加载
评论 #41263046 未加载
评论 #41252752 未加载
PaulHoule9 个月前
I imagine you&#x27;d want to have something that works like a parser generator up front to create the rules. What&#x27;s not clear to me however is how expressive the rules are.
评论 #41251127 未加载
评论 #41250900 未加载
simpaticoder9 个月前
Great hack. Note that it&#x27;s approx the same size as baseline highlight.js at ~45kB. However it has the great benefit of functioning in contexts where javascript is disabled, at the cost of runtime configurability and narrower language support.
twiss9 个月前
There seems to be a bug in Chromium-based browsers where, if you type e.g. `color: blue` in the textarea, it only highlights the r before the colon, rather than the whole property name. If you copy+paste the text instead, it works properly. (Still, that doesn&#x27;t detract from this being really cool, of course!)
评论 #41259844 未加载
thro19 个月前
Just converted MonaspaceKrypton-SyntaxHighlighter-Regular.woff2 with FontCreator to .otf (or .ttf) and installed it in the system, then in Firefox changed <i>about:preferences -&gt; fonts -&gt; monospace</i> to <i>Monaspace Krypton</i>, unchecked <i>Allow pages to choose their own fonts ..</i> - and now I have:<p>*every* monospace text in the browser <i>*syntax colored * </i> :)<p>(as expected.. wherever it does make sense or not.. - viewsource, text files, parts of pages.. :)
评论 #41263027 未加载
niutech9 个月前
Great idea! No need to use Hightlight.js, Prism.js or CSS Custom Highlight API for syntax highlighting.<p>Could anybody make an online tool to choose another monospace font, programming language and color theme, and generate a syntax-highlighted font?
评论 #41356475 未加载
WillAdams9 个月前
Would this work if the font was used in a PDF typeset using LaTeX? (EDIT: when running on top of a TeX engine which can access OpenType, or some other tool (such as InDesign)?)
评论 #41251589 未加载
__MatrixMan__9 个月前
Sometimes you end up with code in one language that has string literals which contain code in another language.<p>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.
CrendKing9 个月前
How is this syntax highlighting? If you put &quot;&lt;div&gt;&quot; in quotes as string, it&#x27;s still green instead of white. Proper syntax highlighting must account for both languages and content in context, which can&#x27;t be done with just a bunch of simple static rules.
评论 #41253990 未加载
Aardwolf9 个月前
This is amazing and seems to <i>just work</i>, the only gotcha I see is that you&#x27;d need different versions of the font for different background colors
steeeeeve9 个月前
This is the coolest thing I&#x27;ve seen in a very long time.
zinekeller9 个月前
&gt; However, eg. PowerPoint doesn&#x27;t support OpenType (as far as I know).<p>Ironically, it is Microsoft which developed CPAL&#x2F;COLR (at least intially), and this is the least atrocious format across colored suggestions. The other options are SVG-in-OpenType (Adobe&#x2F;Mozilla; <a href="https:&#x2F;&#x2F;helpx.adobe.com&#x2F;fonts&#x2F;using&#x2F;ot-svg-color-fonts.html" rel="nofollow">https:&#x2F;&#x2F;helpx.adobe.com&#x2F;fonts&#x2F;using&#x2F;ot-svg-color-fonts.html</a>), PNG-in-OpenType&#x2F;SBIX (Apple; <a href="https:&#x2F;&#x2F;developer.apple.com&#x2F;fonts&#x2F;TrueType-Reference-Manual&#x2F;RM06&#x2F;Chap6sbix.html" rel="nofollow">https:&#x2F;&#x2F;developer.apple.com&#x2F;fonts&#x2F;TrueType-Reference-Manual&#x2F;...</a>), and CustomBitmap-in-OpenType&#x2F;CBDT (Google; <a href="https:&#x2F;&#x2F;fonts.google.com&#x2F;noto&#x2F;use" rel="nofollow">https:&#x2F;&#x2F;fonts.google.com&#x2F;noto&#x2F;use</a>).<p>All of them are now OpenType standards, for better or worse.
knome9 个月前
it seems like you should be able to use the same pattern you do for searching for keywords to handle string sections and comments, by assigning a .instring or .incomment state and having all characters check the prior character for .instring or .incomment and replacing itself with its .instring or .incomment variant as appropriate
评论 #41254036 未加载
8474_s9 个月前
Can&#x27;t you just make a single-word ligature and manipulate its appearance, instead of chaining single letters?
评论 #41253952 未加载
croes9 个月前
So Con #3 makes it rather unusable<p>&gt;For example, words within &lt;p&gt; tags that are JS keywords will be always highlighted
评论 #41253983 未加载
评论 #41253868 未加载
29athrowaway9 个月前
Cannot be themed or customized without regenerating the font.<p>Can be used when syntax highlighting is not available.
评论 #41250978 未加载
BigParm9 个月前
Imagine inputting syntax highlighted code while running inference w LLMs? Currently its hard to read what&#x27;s in your text area when asking ChatGPT a code question.<p>This is the most interesting post in a long time, bravo!
eigenvalue9 个月前
This is incredibly awesome. Looking forward to seeing this but with a nicer typeface (maybe Inconsolata or JetBrains Mono). I can actually see a lot of uses for this-- just displaying JSON in a nice way easily would be handy.
panic9 个月前
BTW, I&#x27;m not sure why it says adding syntax highlighting to a &lt;textarea&gt; is impossible -- as the linked post says, you can turn the text invisible and add styled text in a separate div at the same position.
评论 #41254007 未加载
amanzi9 个月前
I wonder if the author has experience with Excel, because some of those substitution &quot;monstrosities&quot; look similar to some monstrosities I&#x27;ve created with Excel formulas! Good work, this is an awesome hack.
frereubu9 个月前
Love how crazy this is. Small nit that shouldn&#x27;t take away from the enjoyment - one downside not mentioned is that brackets aren&#x27;t colour-coded based on depth, something I find very useful in my IDE.
turblety9 个月前
Really cool. Your demo would have been even better if you add contenteditable on there so the user can edit and see how it works:<p>&lt;pre contenteditable=&quot;plaintext-only&quot; class=&quot;&quot;&gt;&lt;code&gt;...
评论 #41254811 未加载
IncRnd9 个月前
I really like the idea of the hack, but I couldn&#x27;t bring myself to actually load the webpage - thinking of crazy font-code exfiltrating all of my emails that have ligatures.
thomasbachem9 个月前
Really awesome work, @california-og!<p>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.
hsfzxjy9 个月前
Would like to see a Harfbuzz WASM implementation for this :)
stabbles9 个月前
Could be useful for presentations :)
jborza9 个月前
Really cool! I can’t wait to try it on a blog using the techniques mentioned, nifty.
queuebert9 个月前
Damn, I was so excited to see Monaspace, but they used Krypton instead of Xenon.
hajimuz9 个月前
This is wild! It’s always mind blowing how people is hacking HTML&#x2F;CSS.
lovestaco9 个月前
Great explanation, never knew this was possible
atorodius9 个月前
very cool!<p>does anyone know the original target of this OpenType feature? Struggling to imagine where it would fit apart from code highlighting
评论 #41253598 未加载
PullJosh9 个月前
The design of this blog post is lovely! Despite seemingly having no right to be. It goes against all of my design instincts, but it somehow nails a vibe while remaining pleasant and easy on the eyes. I love it!
评论 #41253099 未加载
评论 #41254598 未加载
transfire9 个月前
That this can even be done is absolutely atrocious.<p>I heard about security issues related to fonts and wondered “how in the world”. This helps clarify why.<p>It simply should not be possible.
评论 #41253367 未加载
renewiltord9 个月前
Bloody hell, mate. This is ridiculously smart. Crazy clever hack.
altanis9 个月前
cool
dutzi9 个月前
Fonts are awesome, I once made a font that embeds (guitar) chords as ligatures, so when you write the they “stick” to the words they belong to.<p><a href="https:&#x2F;&#x2F;tunetype.com" rel="nofollow">https:&#x2F;&#x2F;tunetype.com</a>
评论 #41253999 未加载
评论 #41255033 未加载
评论 #41254062 未加载
评论 #41254032 未加载
评论 #41253736 未加载
评论 #41254591 未加载
评论 #41254585 未加载
Shorel9 个月前
As an experiment, it is awesome. We should strive to push the limits of technology and try new things. Kudos. I tip my hat to the creativity.<p>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!)<p>Just use a code editor with Syntax Highlighting! Vim solved this decades ago!<p>Very interesting read, but don&#x27;t try this at home.<p>Edit: Pandora&#x27;s box is opened, I can see Jira and GitHub rushing to add support for this on their websites starting tomorrow =)
评论 #41251032 未加载
评论 #41251084 未加载
评论 #41251083 未加载
russfink9 个月前
I’d make a font with a colored red squiggle over the apostrophe, because so many people misuse apostrophe’s. Cough.