Home > OS >  Cannot load font 'Press Start 2P' in SVG
Cannot load font 'Press Start 2P' in SVG

Time:07-23

I have the following svg I need to render in as a standalone svg file:

<svg width="89" height="55" viewBox="0 0 89 55" fill="none" xmlns="http://www.w3.org/2000/svg">
    <style>
    @font-face {
        font-family: 'Press Start 2P';
        src: url('https://fonts.googleapis.com/css?family=Press Start 2P');
    }
    </style>
    <rect width="89" height="55" fill="black" />
    <rect x="5.5" y="5.5" width="78" height="44" fill="#0000FF" />
    <text text-anchor="middle" x="44.5" y="31" fill=" white" font-family="Press Start 2P" font-size="12" letter-spacing="0em">1234</text>
    <rect x="78" y="45" width="9" height="8" fill="#FF4F0A" />
    <rect x="5.5" y="5.5" width="78" height="44" stroke="#FF4F0A" stroke-width="3" />
    <path d="M81.4286 51V50.4286H81V49.8571H81.8571V50.4286H83.1429V49.2857H81.4286V48.7143H81V47.5714H81.4286V47H83.5714V47.5714H84V48.1429H83.1429V47.5714H81.8571V48.7143H83.5714V49.2857H84V50.4286H83.5714V51H81.4286Z" fill="#0000FF" />
</svg>

I am trying to use a given font (Press Start 2P) but it does not render, neither locally when I open the svg file nor with the following data uri

data:image/svg xml,    <style>    @font-face {        font-family: 'Press Start 2P';        src: url('https://fonts.googleapis.com/css?family=Press+Start+2P');    }    </style>            1234            

EDIT

After comment, I tried to embed the whole font but still does not work

<svg width="89" height="55" viewBox="0 0 89 55" fill="none" xmlns="http://www.w3.org/2000/svg">
    <defs>
        <style>
        @font-face {
            font-family: "Press Start 2P";
            src: url(data:application/font-woff;charset=utf-8;base64,..) format("woff");
            font-weight:normal;font-style:normal;
        }
    </style>
    </defs>
    <rect width="89" height="55" fill="#E5E5E5" />
    <rect width="89" height="55" fill="#29296E" />
    <rect x="5.5" y="5.5" width="78" height="44" fill="#0000FF" />
    <text text-anchor="middle" x="44.5" y="31" fill=" white" font-family="Press Start 2P" font-size="12">
    {}azer
    </text>
    <rect x="5.5" y="5.5" width="78" height="44" stroke="#FF4F0A" stroke-width="3" />
    <rect x="47" y="45" width="41" height="8" fill="#FF4F0A" />
    <text text-anchor="end" x="86" y="52" fill=" white" font-family="Press Start 2P" font-size="8" letter-spacing="0em">
    {}
    </text>
</svg>

the base64 comes from the base64 encoding of the .ttf file

CodePudding user response:

Your first example doesn't work since you're using a css URL as a font file URL.

Open your google font URL and copy the actual @font-face rule The returned css depends on your current browser – yeah, user agent sniffing!

  @font-face {
    font-family: 'Press Start 2P';
    font-style: normal;
    font-weight: 400;
    src: url(https://fonts.gstatic.com/s/pressstart2p/v14/e3t4euO8T-267oIAQAu6jDQyK3nVivM.woff2) format('woff2');
  } 

Otherwise use a service like google web fonts helper to get the actual font file URLs.

False friends: css properties vs. svg attributes

Svg's font related attributes like font-family apparently don't work well with css style definitions.

Better apply font-family within the <style> element like so:

<svg viewBox="0 0 89 55" fill="none" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <style>
      @font-face {
        font-family: 'Press Start 2P';
        font-style: normal;
        font-weight: 400;
        src: url(https://fonts.gstatic.com/s/pressstart2p/v14/e3t4euO8T-267oIAQAu6jDQyK3nVivM.woff2) format('woff2');
      }
      text {
        font-family: 'Press Start 2P';
        font-size: 12;
      }
    </style>
  </defs>
  <rect width="89" height="55" fill="black" />
  <rect x="5.5" y="5.5" width="78" height="44" fill="#0000FF" />
  <text text-anchor="middle" x="44.5" y="31" fill=" white">1234</text>
  <rect x="78" y="45" width="9" height="8" fill="#FF4F0A" />
  <rect x="5.5" y="5.5" width="78" height="44" stroke="#FF4F0A" stroke-width="3" />
</svg>

Embedded fonts

Some services provide a way to generate/convert font files to base64 data URLs like transfonter

Embedded font example

<svg viewBox="0 0 89 55" fill="none" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <style>
      @font-face {
        font-family: 'Press Start 2P';
        font-style: normal;
        font-weight: 400;
        src: url('data:font/woff2;charset=utf-8;base64,d09GMgABAAAAAArEABEAAAAAGKwAAApoAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGhYbIBwqBmAAPAiBYgm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TIqBcWqDEhn1ii261C52kMqbQcuLZlz9arYVAtx6WL72iE 8YlTcdu6IumVpC1TE821riZtSXM1aVka6kPOSdemVjOG8hH/D1mSNGCTQqwxgoUIFTb PmQaNL14q7P/lOjmAR/tvwx nUKpfHy5u QcR1JvREcf0Wv0VD1M4 rQNgtyh1wonxETGZXPh12qX6pdKgMcmkLuGLxU4bR/hur7HF3b0e9zru0foGl2odwz6qUgnvNPdfMEVM/yPB19Qm1QM9UohZtJT H/ffymUyau/IhpGdUnAAA=') format('woff2');
        font-weight: normal;
        font-style: normal;
      }

      text {
        font-family: 'Press Start 2P';
        font-size: 12;
      }
    </style>
  </defs>
  <rect width="89" height="55" fill="black" />
  <rect x="5.5" y="5.5" width="78" height="44" fill="#0000FF" />
  <text text-anchor="middle" x="44.5" y="31" fill=" white">1234</text>
  <rect x="78" y="45" width="9" height="8" fill="#FF4F0A" />
  <rect x="5.5" y="5.5" width="78" height="44" stroke="#FF4F0A" stroke-width="3" />
</svg>

If your svg only uses google webfonts, you can also try vecta's nano optimiser – it can retrieve these fonts and include them as font subsets ( including only needed characters/glyphs).

  • Related