使用真正的 url 导入 Google 字体到字体文件

Import Google Font using real url to font file

我在我的项目中使用 GoogleFonts,但出于某种原因,我无法在不使用 'embed' 系统的情况下导入字体。

例如,如果你想导入 Titan One 字体,你可以使用:

@import url('https://fonts.googleapis.com/css2?family=Titan+One&display=swap');

font-family: 'Titan One', cursive;

这个url实际上是指一个声明字体的css文件:

/* latin-ext */
@font-face {
  font-family: 'Titan One';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: local('Titan One'), local('TitanOne'), url(https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjCmjgm-khyk-RW.woff2) format('woff2');
  unicode-range: U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF;
}
/* latin */
@font-face {
  font-family: 'Titan One';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: local('Titan One'), local('TitanOne'), url(https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjClDgm-khykw.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

所以我决定跳过 @import 步骤直接从其静态 url 导入字体(此处:https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjCmjgm-khyk-RW.woff2):

<!DOCTYPE html>
<html>
<head>
  <style>
    @font-face {
      font-family: 'Titan One';
      font-style: normal;
      font-weight: 400;
      src: url(https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjCmjgm-khyk-RW.woff2);
    }
    
    h1 {
      font-family: 'Titan One';
    }
  </style>
</head>

<body>

  <h1>The @font-face Rule</h1>

</body>

</html>

但是如您所见,该字体不适用于我的标题。你知道错误在哪里吗?

您需要考虑 latin 文件而不是 latin-ext,但我建议您复制整个 Google 文件以确保其正常工作,因为 unicode-range很重要

The unicode-range CSS descriptor sets the specific range of characters to be used from a font defined by @font-face and made available for use on the current page.

The purpose of this descriptor is to allow the font resources to be segmented so that a browser only needs to download the font resource needed for the text content of a particular page. ref

@font-face {
  font-family: 'Titan One';
  font-style: normal;
  font-weight: 400;
  src: url(https://fonts.gstatic.com/s/titanone/v7/mFTzWbsGxbbS_J5cQcjClDgm-khykw.woff2);
}

h1 {
  font-family: 'Titan One';
}
<h1>The @font-face Rule</h1>