<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Yakshaving on bramp.net</title>
    <link>https://blog.bramp.net/</link>
    <description>Recent content in Yakshaving on bramp.net</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-GB</language>
    <lastBuildDate>Sun, 21 Jan 2018 16:03:36 -0800</lastBuildDate>
    <atom:link href="https://blog.bramp.net/tags/yakshaving/" rel="self" type="application/rss+xml" />
    
    <item>
      <title>Google Font Features</title>
      <link>https://blog.bramp.net/post/2018/01/21/google-font-features/</link>
      <pubDate>Sun, 21 Jan 2018 16:03:36 -0800</pubDate>
      
      <guid>https://blog.bramp.net/post/2018/01/21/google-font-features/</guid>
      <description><blockquote>
<p><strong>tl;dr Google Fonts doesn&rsquo;t supply fonts with OpenType features (such as old-style figures, or small-caps), but you can build and host the fonts yourself to support everything you need.</strong></p>
</blockquote>
<p>I recently posted a <a href="https://blog.bramp.net/post/2018/01/16/measuring-percentile-latency/">article which contained lots of numbers</a>. While I was proofreading the article, I didn’t quite liked how the numbers looked, sometime the digits were below the baseline, for example:</p>
<figure><img src="/post/2018/01/21/google-font-features/oldstyle.png" width="760" height="157"><figcaption>
      <h4>Oldstyle figures</h4>
    </figcaption>
</figure>

<p>Where I would have expected the top and bottom of each digit to be aligned:</p>
<figure><img src="/post/2018/01/21/google-font-features/lining.png" width="760" height="152"><figcaption>
      <h4>Lining figures</h4>
    </figcaption>
</figure>

<p>This made me flashback to all the typography I learnt when <a href="https://github.com/bramp/publication">working with LaTeX</a>. These two styles of figures are called old-style, and lining (or sometimes lowercase and uppercase numbers). The theory is that old-style numbers flow better when mixed with text. Recall, letters like q, j and p, all drop below the baseline, which makes the text nicer to read:</p>
<figure><img src="/post/2018/01/21/google-font-features/quickbrownfox.png" width="760" height="100"><figcaption>
      <h4>Example with characters below the baseline</h4>
    </figcaption>
</figure>

<p>However, my article had many numbers on the page, sometimes within tables, where old-style just made the numbers look odd. I looked for a way to force the lining style throughout. I quickly found the CSS styling:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-css" data-lang="css"><span class="line"><span class="cl"><span class="nt">body</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">           <span class="k">font-variant-numeric</span><span class="p">:</span> <span class="n">lining-nums</span><span class="p">;</span> 
</span></span><span class="line"><span class="cl">  <span class="kp">-webkit-</span><span class="k">font-feature-settings</span><span class="p">:</span> <span class="s2">&#34;lnum&#34;</span> <span class="kc">on</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">     <span class="kp">-moz-</span><span class="k">font-feature-settings</span><span class="p">:</span> <span class="s2">&#34;lnum&#34;</span> <span class="kc">on</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">      <span class="kp">-ms-</span><span class="k">font-feature-settings</span><span class="p">:</span> <span class="s2">&#34;lnum&#34;</span> <span class="kc">on</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">          <span class="k">font-feature-settings</span><span class="p">:</span> <span class="s2">&#34;lnum&#34;</span> <span class="kc">on</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>Sadly when I applied this to my site, it did nothing. I wondered if perhaps the font did not support lining figures. A quick search led me to <a href="https://stackoverflow.com/questions/28098992/google-fonts-lining-numbers">Stack Overflow</a> that implied both the font I was using, <a href="https://fonts.google.com/specimen/Raleway">Raleway</a>, and Google Fonts (which hosted the font) did in fact support lining.</p>
<p>So I went deeper down the rabbit hole to figure out what was going wrong. I wanted to confirm for myself that the font supported lining figures. I searched for a while for a simple CLI that would inspect the <a href="https://en.wikipedia.org/wiki/Web_Open_Font_Format">WOFF</a>/<a href="https://en.wikipedia.org/wiki/TrueType">TTF</a> files and tell me what they contained. Sadly, the best I could find was <a href="https://fontforge.github.io/">FontForge</a>, a GUI. That worked, and confirmed the fonts being served by Google did not contain the lining feature, or in fact any feature other than basic ligatures.</p>
<p>Later I found this <a href="https://github.com/google/fonts/issues/1335">GitHub issue</a> which confirmed all features were stripped from the font. So I sought out a way to rebuild the Google font to keep the lining figures.</p>
<p>Before that, I started to <a href="http://sethgodin.typepad.com/seths_blog/2005/03/dont_shave_that.html">shave another yak</a>, and decided to create a CLI tool that would easily display the font features. I came across a Go library, <a href="https://github.com/ConradIrwin/font">SFNT</a> that can parse OpenType fonts. Sadly it didn’t implement the parsing of the features. A few hours later, I read the <a href="http://www.adobe.com/devnet/opentype/afdko/topic_feature_file_syntax.html">OpenType spec</a> and sent them a <a href="https://github.com/ConradIrwin/font/pull/3">pull request</a> to add this functionality. Now I can easily confirm from the command line what features are supported.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">$ font features raleway-v12-latin-ext_latin-regular.woff
</span></span><span class="line"><span class="cl">Glyph Substitution Table <span class="o">(</span>GSUB<span class="o">)</span>:
</span></span><span class="line"><span class="cl">	Script <span class="s2">&#34;latn&#34;</span> <span class="o">(</span>Latin<span class="o">)</span>:
</span></span><span class="line"><span class="cl">		Default Language:
</span></span><span class="line"><span class="cl">			Feature <span class="s2">&#34;liga&#34;</span> <span class="o">(</span>Standard Ligatures<span class="o">)</span>
</span></span></code></pre></div><p>I decided to play around with <a href="https://developers.google.com/fonts/docs/developer_api">Google Font API</a>, and then eventually the unoffical (but awesome) <a href="https://google-webfonts-helper.herokuapp.com/fonts/raleway">google-webfonts-helper</a> (a hassle-free way to self-host Google Fonts). However, no combination of options would make the font contain the lining figures.</p>
<p>Since the Google Fonts are open source, I downloaded the <a href="https://github.com/google/fonts/tree/master/ofl/raleway">source TTF of the font</a>, and double-checked it did indeed contain the feature:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">$ font features Raleway-Regular.ttf 
</span></span><span class="line"><span class="cl">Glyph Substitution Table <span class="o">(</span>GSUB<span class="o">)</span>:
</span></span><span class="line"><span class="cl">  Script <span class="s2">&#34;latn&#34;</span> <span class="o">(</span>Latin<span class="o">)</span>:
</span></span><span class="line"><span class="cl">    Default Language:
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;aalt&#34;</span> <span class="o">(</span>Access All Alternates<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;dlig&#34;</span> <span class="o">(</span>Discretionary Ligatures<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;liga&#34;</span> <span class="o">(</span>Standard Ligatures<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;lnum&#34;</span> <span class="o">(</span>Lining Figures<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;onum&#34;</span> <span class="o">(</span>Oldstyle Figures<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;salt&#34;</span> <span class="o">(</span>Stylistic Alternates<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;smcp&#34;</span> <span class="o">(</span>Small Capitals<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;ss01&#34;</span> <span class="o">(</span>Stylistic Set 1<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;ss02&#34;</span> <span class="o">(</span>Stylistic Set 2<span class="o">)</span>
</span></span></code></pre></div><p>So my next idea was to take the original Raleway-Regular.ttf and convert it to <a href="https://en.wikipedia.org/wiki/Web_Open_Font_Format">WOFF</a> and <a href="https://www.w3.org/TR/WOFF2/">WOFF2</a>, and strip out the bits I don’t need. Just how Google Fonts does, to ensure the resulting files are lean and performant.</p>
<p>I couldn’t find the pipeline Google Fonts uses to process the files, so I instead took it upon myself to figure this out. I started by using <code>pyftsubset</code> (part of <a href="https://github.com/fonttools/fonttools">FontTools</a>) to remove unneeded character sets, features, and other parts from the original TTF file.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">$ pip install fonttools
</span></span><span class="line"><span class="cl">$ pyftsubset Raleway-Regular.ttf --layout-features<span class="o">=</span><span class="s1">&#39;*&#39;</span> --unicodes<span class="o">=</span><span class="s2">&#34;U+0000-00FF, U+0100-024F, U+0131, U+0152-0153, U+02DA, U+02DC, U+02BB-02BC, U+02C6, U+0259, U+0370-03FF, U+1E00-1EFF, U+2000-206F, U+2070-209F, U+2074, U+20A0-20CF, U+2122, U+2150-218F, U+2200-22FF, U+2C60-2C7F, U+A720-A7FF&#34;</span> --output-file<span class="o">=</span>Raleway-Regular.subset.ttf
</span></span></code></pre></div><p>Now I had a TTF file with all the features, but only the subset of characters I use on my site. Next I needed to convert this this file to all the <a href="https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/webfont-optimization">recommended font formats</a>, so my site would look nice in IE, Chrome, Android and iOS. The resulting CSS would look like this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-css" data-lang="css"><span class="line"><span class="cl"><span class="p">@</span><span class="k">font-face</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="nt">font-family</span><span class="o">:</span> <span class="s1">&#39;Raleway&#39;</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">  <span class="nt">src</span><span class="o">:</span> <span class="nt">url</span><span class="o">(</span><span class="s1">&#39;raleway-regular.subset.eot&#39;</span><span class="o">);</span>                           <span class="c">/* IE9 Compat Modes */</span>
</span></span><span class="line"><span class="cl">  <span class="nt">src</span><span class="o">:</span> <span class="nt">local</span><span class="o">(</span><span class="s1">&#39;Raleway&#39;</span><span class="o">),</span> <span class="nt">local</span><span class="o">(</span><span class="s1">&#39;Raleway-Regular&#39;</span><span class="o">),</span>
</span></span><span class="line"><span class="cl">       <span class="nt">url</span><span class="o">(</span><span class="s1">&#39;raleway-regular.subset.eot?#iefix&#39;</span><span class="o">)</span> <span class="nt">format</span><span class="o">(</span><span class="s1">&#39;embedded-opentype&#39;</span><span class="o">),</span> <span class="c">/* IE6-IE8 */</span>
</span></span><span class="line"><span class="cl">       <span class="nt">url</span><span class="o">(</span><span class="s1">&#39;raleway-regular.subset.woff2&#39;</span><span class="o">)</span> <span class="nt">format</span><span class="o">(</span><span class="s1">&#39;woff2&#39;</span><span class="o">),</span>    <span class="c">/* Super Modern Browsers */</span>
</span></span><span class="line"><span class="cl">       <span class="nt">url</span><span class="o">(</span><span class="s1">&#39;raleway-regular.subset.woff&#39;</span><span class="o">)</span> <span class="nt">format</span><span class="o">(</span><span class="s1">&#39;woff&#39;</span><span class="o">),</span>     <span class="c">/* Pretty Modern Browsers */</span>
</span></span><span class="line"><span class="cl">       <span class="nt">url</span><span class="o">(</span><span class="s1">&#39;raleway-regular.subset.ttf&#39;</span><span class="o">)</span> <span class="nt">format</span><span class="o">(</span><span class="s1">&#39;truetype&#39;</span><span class="o">),</span>    <span class="c">/* Safari, Android, iOS */</span>
</span></span><span class="line"><span class="cl">       <span class="nt">url</span><span class="o">(</span><span class="s1">&#39;raleway-regular.subset.svg#ralewayregular&#39;</span><span class="o">)</span> <span class="nt">format</span><span class="o">(</span><span class="s1">&#39;svg&#39;</span><span class="o">);</span>    <span class="c">/* Legacy iOS */</span>
</span></span><span class="line"><span class="cl">  <span class="nt">font-style</span><span class="o">:</span> <span class="nt">normal</span><span class="o">;</span>
</span></span><span class="line"><span class="cl">  <span class="nt">font-weight</span><span class="o">:</span> <span class="nt">400</span><span class="o">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></div><p>I again tried to use <code>pyftsubset</code> to save the files in the required formats. This worked well for TTF, WOFF, and WOFF2. But didn’t support <a href="https://en.wikipedia.org/wiki/Embedded_OpenType">EOT</a> or <a href="http://caniuse.com/svg-fonts">SVG</a> fonts:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">$ pip install zopfli
</span></span><span class="line"><span class="cl">$ pip install brotli
</span></span><span class="line"><span class="cl">$ pyftsubset ... --flavor<span class="o">=</span>woff --with-zopfli --output-file<span class="o">=</span>Raleway-Regular.subset.woff
</span></span><span class="line"><span class="cl">$ pyftsubset ... --flavor<span class="o">=</span>woff2 --output-file<span class="o">=</span>Raleway-Regular.subset.woff2
</span></span></code></pre></div><p>So instead I searched for a all-in-one solution to converting fonts. I found numerous websites that offered to do it, the one I settled on was <a href="https://www.fontsquirrel.com/tools/webfont-generator">fontsquirrel.com</a>. Here I used the expert feature, to control exactly what was in the font, and to produce compressed versions in all file formats. I originally tried to use the subsetting feature on fontsquirrel, but I couldn’t get it to maintain all the features I needed, so I used <code>pyftsubset</code> locally instead.</p>
<p>After fontsquirrel.com produced the fonts, I checked it contained the features, and compared the resulting file sizes:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl">$ ls -ltr
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># Google Fonts</span>
</span></span><span class="line"><span class="cl"> 96K  raleway-v12-latin-ext_latin-regular.ttf
</span></span><span class="line"><span class="cl"> 40K  raleway-v12-latin-ext_latin-regular.woff
</span></span><span class="line"><span class="cl"> 31K  raleway-v12-latin-ext_latin-regular.woff2
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1"># My versions</span>
</span></span><span class="line"><span class="cl">140K raleway-regular.subset-webfont.ttf
</span></span><span class="line"><span class="cl"> 61K raleway-regular.subset-webfont.woff
</span></span><span class="line"><span class="cl"> 46K raleway-regular.subset-webfont.woff2
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">$ font features raleway-regular.subset-webfont.woff
</span></span><span class="line"><span class="cl">Glyph Substitution Table <span class="o">(</span>GSUB<span class="o">)</span>:
</span></span><span class="line"><span class="cl">  Script <span class="s2">&#34;latn&#34;</span> <span class="o">(</span>Latin<span class="o">)</span>:
</span></span><span class="line"><span class="cl">    Default Language:
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;aalt&#34;</span> <span class="o">(</span>Access All Alternates<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;dlig&#34;</span> <span class="o">(</span>Discretionary Ligatures<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;liga&#34;</span> <span class="o">(</span>Standard Ligatures<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;lnum&#34;</span> <span class="o">(</span>Lining Figures<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;onum&#34;</span> <span class="o">(</span>Oldstyle Figures<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;salt&#34;</span> <span class="o">(</span>Stylistic Alternates<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;smcp&#34;</span> <span class="o">(</span>Small Capitals<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;ss01&#34;</span> <span class="o">(</span>Stylistic Set 1<span class="o">)</span>
</span></span><span class="line"><span class="cl">      Feature <span class="s2">&#34;ss02&#34;</span> <span class="o">(</span>Stylistic Set 2<span class="o">)</span>
</span></span></code></pre></div><p>The file size didn&rsquo;t vary too much, and thus it was a simple matter of <a href="https://blog.bramp.net/fonts/raleway-regular.subset-webfont.woff2">uploading the fonts</a> to my blog, and updating the CSS.</p>
<p class="text-center">
<span class="onum" style="text-decoration: red underline overline; font-size: 3.5em">1234567890</span> &nbsp;vs&nbsp; <span class="lnum" style="text-decoration: red underline overline; font-size: 3.5em">1234567890</span>
</p></description>
    </item>
    
  </channel>
</rss>
