axes - 有没有 "fast"方法来获取有关可变 Google 字体的信息?

标签 axes google-font-api google-fonts variable-fonts

我正在构建一个 UI 作为产品的一部分,以便轻松选择、选择 Google 字体并设置其样式。我受到可变字体的挑战,因为我找不到获取有关这些信息的好方法。 开发者 API 通过大型 JSON 字符串提供所有 Google 字体的元数据。然而,它似乎不包含任何允许开发人员辨别哪些字体是可变的数据。它们“看起来”都是标准字体。

有没有快速获取此类数据的方法?说到快速,我正在谈论类似于 Google Font 的开发人员 API 的东西,但包含各种可变字体的数据,其中包括:

  • 哪些字体是可变的?
  • 可变字体随哪些轴一起提供?

目前,我见过的探索可变字体及其轴的唯一推荐方法是将字体加载到网页中,并使用开发人员工具中的 Firefox 字体编辑器手动获取数据。但由于 Google Fonts 目前有 112 种可变字体,收集这些信息可能需要几天的时间。所以我的问题是: 有没有更快的方法来获取 Google Fonts 中可变字体的元数据?

最佳答案

我正在开发一个字体选择器插件,我遇到了类似的问题,所以我开始调查 google fonts main distribution site直到我找到我要找的东西。 Google 的字体网站执行对以下 API 端点的调用。

https://fonts.google.com/metadata/fonts

返回以下文本文件。

)]}'{"axisRegistry": [
{
  "tag": "FILL",
  "displayName": "Fill",
  "min": 0.0,
  "defaultValue": 0.0,
  "max": 1.0,
  "precision": -2,
  "description": "The Fill axis is intended to provide a treatment of the design that fills in transparent forms with opaque ones (and sometimes interior opaque forms become transparent, to maintain contrasting shapes). Transitions often occur from the center, a side, or a corner, but can be in any direction. This can be useful in animation or interaction to convey a state transition. The numbers indicate proportion filled, from 0 (no treatment) to 1 (completely filled).",
  "fallbacks": [
    {
      "name": "Normal",
      "value": 0.0
    },
    {
      "name": "Filled",
      "value": 1.0
    }
  ]
} ...],"familyMetadataList": [{
  "family": "Alegreya",
  "displayName": null,
  "category": "Serif",
  "size": 425570,
  "subsets": [
    "menu",
    "cyrillic",
    "cyrillic-ext",
    "greek",
    "greek-ext",
    "latin",
    "latin-ext",
    "vietnamese"
  ],
  "fonts": {
    "400": {
      "thickness": 4,
      "slant": 1,
      "width": 6,
      "lineHeight": 1.361
    },
    "400i": {
      "thickness": 4,
      "slant": 4,
      "width": 6,
      "lineHeight": 1.361
    },
    "500": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "500i": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "600": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "600i": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "700": {
      "thickness": 7,
      "slant": 1,
      "width": 7,
      "lineHeight": 1.361
    },
    "700i": {
      "thickness": 6,
      "slant": 4,
      "width": 6,
      "lineHeight": 1.361
    },
    "800": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "800i": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "900": {
      "thickness": 8,
      "slant": 1,
      "width": 7,
      "lineHeight": 1.361
    },
    "900i": {
      "thickness": 8,
      "slant": 4,
      "width": 6,
      "lineHeight": 1.361
    }
  },
  "axes": [
    {
      "tag": "wght",
      "min": 400.0,
      "max": 900.0,
      "defaultValue": 400.0
    }
  ],
  "unsupportedAxes": [],
  "designers": [
    "Juan Pablo del Peral",
    "Huerta Tipográfica"
  ],
  "lastModified": "2021-02-11",
  "dateAdded": "2011-12-19",
  "popularity": 159,
  "trending": 828,
  "defaultSort": 164,
  "androidFragment": null,
  "isNoto": false
}...],...}

请注意,虽然上面的内容看起来像 JSON 文件,但它会被视为文本文件,因此您必须从文本文件顶部删除这部分 )]}' ,这样您就可以将其解析为 JSON 文件。 唯一重要的顶级属性(就您的用例而言)是“familyMetadataList”属性,顾名思义,它包括所有字体元数据,其中包括任何给定字体具有的轴。 您必须循环“familyMetadataList” Prop 并查看字体的轴成员是否有一个不为空的数组,从那里我们可以推断它是一个可变字体。 您可以执行像这样简单的操作来确定哪种字体是可变的。

const variableFonts=[];
const googleFontJSON = {
 "familyMetadataList": [
 {
  "family": "Alegreya",
  "displayName": null,
  "category": "Serif",
  "size": 425570,
  "subsets": [
    "menu",
    "cyrillic",
    "cyrillic-ext",
    "greek",
    "greek-ext",
    "latin",
    "latin-ext",
    "vietnamese"
  ],
  "fonts": {
    "400": {
      "thickness": 4,
      "slant": 1,
      "width": 6,
      "lineHeight": 1.361
    },
    "400i": {
      "thickness": 4,
      "slant": 4,
      "width": 6,
      "lineHeight": 1.361
    },
    "500": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "500i": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "600": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "600i": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "700": {
      "thickness": 7,
      "slant": 1,
      "width": 7,
      "lineHeight": 1.361
    },
    "700i": {
      "thickness": 6,
      "slant": 4,
      "width": 6,
      "lineHeight": 1.361
    },
    "800": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "800i": {
      "thickness": null,
      "slant": null,
      "width": null,
      "lineHeight": 1.361
    },
    "900": {
      "thickness": 8,
      "slant": 1,
      "width": 7,
      "lineHeight": 1.361
    },
    "900i": {
      "thickness": 8,
      "slant": 4,
      "width": 6,
      "lineHeight": 1.361
    }
  },
  "axes": [
    {
      "tag": "wght",
      "min": 400.0,
      "max": 900.0,
      "defaultValue": 400.0
    }
  ],
  "unsupportedAxes": [],
  "designers": [
    "Juan Pablo del Peral",
    "Huerta Tipográfica"
  ],
  "lastModified": "2021-02-11",
  "dateAdded": "2011-12-19",
  "popularity": 159,
  "trending": 828,
  "defaultSort": 164,
  "androidFragment": null,
  "isNoto": false
},
    {
      "family": "Alegreya SC",
      "displayName": null,
      "category": "Serif",
      "size": 381295,
      "subsets": [
        "menu",
        "cyrillic",
        "cyrillic-ext",
        "greek",
        "greek-ext",
        "latin",
        "latin-ext",
        "vietnamese"
      ],
      "fonts": {
        "400": {
          "thickness": 4,
          "slant": 1,
          "width": 7,
          "lineHeight": 1.361
        },
        "400i": {
          "thickness": 4,
          "slant": 4,
          "width": 7,
          "lineHeight": 1.361
        },
        "500": {
          "thickness": null,
          "slant": null,
          "width": null,
          "lineHeight": 1.361
        },
        "500i": {
          "thickness": null,
          "slant": null,
          "width": null,
          "lineHeight": 1.361
        },
        "700": {
          "thickness": 6,
          "slant": 1,
          "width": 7,
          "lineHeight": 1.361
        },
        "700i": {
          "thickness": 6,
          "slant": 3,
          "width": 7,
          "lineHeight": 1.361
        },
        "800": {
          "thickness": null,
          "slant": null,
          "width": null,
          "lineHeight": 1.361
        },
        "800i": {
          "thickness": null,
          "slant": null,
          "width": null,
          "lineHeight": 1.361
        },
        "900": {
          "thickness": 8,
          "slant": 1,
          "width": 7,
          "lineHeight": 1.361
        },
        "900i": {
          "thickness": 8,
          "slant": 3,
          "width": 7,
          "lineHeight": 1.361
        }
      },
      "axes": [],
      "unsupportedAxes": [],
      "designers": [
        "Juan Pablo del Peral",
        "Huerta Tipográfica"
      ],
      "lastModified": "2021-03-24",
      "dateAdded": "2011-12-19",
      "popularity": 436,
      "trending": 249,
      "defaultSort": 443,
      "androidFragment": null,
      "isNoto": false
    }
]}; // The array of font meta data
googleFontJSON.familyMetadataList.forEach(font => {     
  if (font.axes.length) {
    font.isVariable=true;
  } else {
    font.isVariable=false;
  }
});
console.log(googleFontJSON);

您如何分析数据当然完全是您自己的特权。 祝你的项目顺利,史蒂文先生。 您还可以通过在 https://fonts.google.com/metadata/fonts 找到的 JSON 文件中的 axis 注册表属性获取有关任何给定可变字体的轴的更多信息。 。 只需检查精度支柱即可。 例如,“opsz”和“wdth”等步长为 0.1 的轴的精度设置为 -1,“CASL”和“MONO”等步长为 0.01 的轴的精度设置为 -2。

  "axisRegistry": [
    {
      "tag": "opsz",
      "displayName": "Optical size",
      "min": 6.0,
      "defaultValue": 14.0,
      "max": 144.0,
      "precision": -1, //<=== Here
      "description": "Adapt the ...",
      "fallbacks": [
        {
          "name": "6pt",
          "value": 6.0
        },
        {
          "name": "7pt",
          "value": 7.0
        }...
      ]
    },...

关于axes - 有没有 "fast"方法来获取有关可变 Google 字体的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69017409/

相关文章:

css - 在 svg 中的样式标签上添加多个谷歌字体

css - 字母间距不适用于特定的双字母

css - 添加 Google 字体似乎会破坏/禁用我的滚动捕捉设置

python - 第二个 y 轴和垂直线

python - 正确设置 matplotlib 3d 图中的轴限制?

python - 如何在 Plotly 中作为查看器启用和禁用对数刻度?

Rmarkdown 不会将 Montserrat 字体编织成 PDF

vba - VBA Excel系列中的Y轴对称

fonts - Google 字体无法正常工作,出现错误 "No ' Access-Control-Allow-Origin' header is present on the requested resource”

css - 使用 Google 字体时,Bootstrap 图标 (Glyphicons) 位置稍微向上,我该如何解决这个问题?