一种元数据生成器,可获取和解析Google字体API,主要用于Fontsource MonorePo。
从npm安装软件包:
npm install google-font-metadata该项目导出以下数据:
import {
APIv1 ,
APIv2 ,
APIVariable ,
APIIconStatic ,
APIIconVariable ,
APILicense ,
APIRegistry ,
} from "google-font-metadata" ;
const {
APIv1 ,
APIv2 ,
APIVariable ,
APIIconStatic ,
APIIconVariable ,
APILicense ,
APIRegistry ,
} = require ( "google-font-metadata" ) ;
console . dir ( APIv2 ) ; 使用Google字体CSS APIV1,其中包含每个子集的不同字体文件,但不包括Unicode-范围值。通常不建议这样做。
它导出data/google-fonts-v1.json 。
{
...
"abel" : {
"family" : " Abel " ,
"id" : " abel " ,
"subsets" : [ " latin " ],
"weights" : [ 400 ],
"styles" : [ " normal " ],
"variants" : {
"400" : {
"normal" : {
"latin" : {
"url" : {
"woff2" : " https://fonts.gstatic.com/s/abel/v18/MwQ5bhbm2POE2V9BPQ.woff2 " ,
"woff" : " https://fonts.gstatic.com/s/abel/v18/MwQ5bhbm2POE2V9BOw.woff " ,
"truetype" : " https://fonts.gstatic.com/s/abel/v18/MwQ5bhbm2POE2V9BOA.ttf "
}
}
}
}
},
"defSubset" : " latin " ,
"lastModified" : " 2022-04-20 " ,
"version" : " v18 " ,
"category" : " sans-serif "
},
...
}使用Google字体CSS APIV2,并包含每个子集的Unicode-范围值。但是,API将一个文件中包含的所有子集提供了ttf文件,因此在同一子集中的这些文件类型的所有链接都可以针对每个重量和样式的相同链接。 woff2和woff文件单独分配每个子集。
导出data/google-fonts-v2.json 。
{
...
"abel" : {
"family" : " Abel " ,
"id" : " abel " ,
"subsets" : [ " latin " ],
"weights" : [ 400 ],
"styles" : [ " normal " ],
"unicodeRange" : {
"latin" : " 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 "
},
"variants" : {
"400" : {
"normal" : {
"latin" : {
"url" : {
"woff2" : " https://fonts.gstatic.com/s/abel/v18/MwQ5bhbm2POE2V9BPQ.woff2 " ,
"woff" : " https://fonts.gstatic.com/s/abel/v18/MwQ5bhbm2POE6Vs.woff " ,
"truetype" : " https://fonts.gstatic.com/s/abel/v18/MwQ5bhbm2POE6Vg.ttf "
}
}
}
}
},
"defSubset" : " latin " ,
"lastModified" : " 2022-04-20 " ,
"version" : " v18 " ,
"category" : " sans-serif "
},
...
}请注意,带有大雕文的字体,例如日语,韩语或中文,分为许多使用Unicode-range @fontface Selector的较小编号的子集。一个示例是noto sans jp,它返回以下内容:
{
...
"noto-sans-jp" : {
"family" : " Noto Sans JP " ,
"id" : " noto-sans-jp " ,
"subsets" : [ " japanese " , " latin " ],
"weights" : [ 100 , 300 , 400 , 500 , 700 , 900 ],
"styles" : [ " normal " ],
"unicodeRange" : {
"[0]" : " U+25ee8,...,U+2f9f4 " ,
"[1]" : " U+1f235-1f23b,...,U+25ed8 " ,
...
"[119]" : " U+20,...,U+ff0e "
},
"variants" : {
"100" : {
"normal" : {
"[0]" : {
"url" : {
"woff2" : " https://fonts.gstatic.com/s/notosansjp/v42/-F6ofjtqLzI2JPCgQBnw7HFQoggPkENvl4B0ZLgOquiXidBa3qHiDcp2RQ.0.woff2 " ,
"woff" : " https://fonts.gstatic.com/s/notosansjp/v42/-F62fjtqLzI2JPCgQBnw7HFoxQII2lcnk-AFfrgQrvWXpdFg3KXxAMsKMbdN.0.woff " ,
"opentype" : " https://fonts.gstatic.com/s/notosansjp/v42/-F6ofjtqLzI2JPCgQBnw7HFQoggM.otf "
}
},
...,
},
...,
}
}
}
...
}刮擦Google字体目录,并使用Google字体API生成所有相关的轴定义并下载变体元数据。您可以在此处学习更多可变字体轴。
有3个默认变体:
wght仅链接到只有wght轴的字体文件。standard - 一组默认字体,包括wght, wdth, slnt, opsz Axis'(如果有)。full - 指向其中包含所有轴的字体文件。此外,为字体中的每个唯一轴生成一个变体,例如,如果存在wdth ,则将存在variants.wdth.normal.latin 。请注意, wght轴也包含在每个唯一的自定义变体中。
请注意,如果该分类的字体中没有相关轴,则可能不存在standard或full变体。这是为了防止具有不同名称的重复变体。
导出data/variable.json 。
{
...
"akshar" : {
"family" : " Akshar " ,
"id" : " akshar " ,
"axes" : {
"wght" : { "default" : " 400 " , "min" : " 300 " , "max" : " 700 " , "step" : " 1 " }
},
"variants" : {
"wght" : {
"normal" : {
"devanagari" : " https://fonts.gstatic.com/s/akshar/v5/Yq6V-LyHWTfz9rGCpR5lhOc.woff2 " ,
"latin-ext" : " https://fonts.gstatic.com/s/akshar/v5/Yq6V-LyHWTfz9rGCqh5lhOc.woff2 " ,
"latin" : " https://fonts.gstatic.com/s/akshar/v5/Yq6V-LyHWTfz9rGCpB5l.woff2 "
}
},
"full" : {
"normal" : {
"devanagari" : " https://fonts.gstatic.com/s/akshar/v5/Yq6V-LyHWTfz9rGCpR5lhOc.woff2 " ,
"latin-ext" : " https://fonts.gstatic.com/s/akshar/v5/Yq6V-LyHWTfz9rGCqh5lhOc.woff2 " ,
"latin" : " https://fonts.gstatic.com/s/akshar/v5/Yq6V-LyHWTfz9rGCpB5l.woff2 "
}
},
"standard" : {
"normal" : {
"devanagari" : " https://fonts.gstatic.com/s/akshar/v5/Yq6V-LyHWTfz9rGCpR5lhOc.woff2 " ,
"latin-ext" : " https://fonts.gstatic.com/s/akshar/v5/Yq6V-LyHWTfz9rGCqh5lhOc.woff2 " ,
"latin" : " https://fonts.gstatic.com/s/akshar/v5/Yq6V-LyHWTfz9rGCpB5l.woff2 "
}
}
}
},
...
}请注意,某些字体(例如间或递归)具有SLNT轴,这意味着它们在CSS中的font-style在full上不会normal或italic不正常,但oblique x deg x deg 。请参阅CSS测试固定装置以进行递归。虽然仍在元数据中显示normal , max如果开发人员使用recursive.axes.slnt属性的min生成CSS,则可以包括oblique样式。
这些是从npx gfm generate [key]命令中生成的对象的数组。您不太可能使用此。
import { APIDirect , APIVariableDirect } from "google-font-metadata" ;
const { APIDirect , APIVariableDirect } = require ( "google-font-metadata" ) ;导出data/api-response.json和data/variable-response.json 。
刮擦Google字体归因页面并返回可读对象。
{
...
"abel" : {
"id" : " abel " ,
"authors" : {
"copyright" : " Copyright 2011, Matthew Desmond with Reserved Font Name Abel. " ,
"website" : " http://www.madtype.com " ,
"email" : " [email protected] "
},
"license" : {
"type" : " SIL Open Font License, 1.1 " ,
"url" : " http://scripts.sil.org/OFL "
},
"original" : " Copyright (c) 2011, Matthew Desmond (http://www.madtype.com | [email protected]), with Reserved Font Name Abel. "
},
...
}导出data/licenses.json
{
...
{
"name" : " Thick Stroke " ,
"tag" : " XOPQ " ,
"min" : -1000 ,
"max" : 2000 ,
"default" : 88 ,
"precision" : 0
},
...
}导出data/axis-registry.json
您可以参考src/index.ts和src/data.ts以查看所有导出。
您可以使用gfm CLI工具以Google API的新结果来更新元数据。
npx gfm generate [key] - 获取默认的Google字体API,可用于以后解析。必须在npx gfm parse之前调用。
标志:
-n, --normal - 仅获取用于APIV1和APIV2的普通Google开发人员API。-v, --variable仅刮擦可变轴页。注意key如果传递此选项,则不需要给出。您可以从这里获得Google字体API key 。另外,您可以将.env文件与API_KEY=keyvalue一起使用,而不是在命令中提供一个密钥参数。
npx gfm parse - 通过Google字体CSS API解析,并使用generate命令数据生成完整的元数据。
标志:
-1, --v1仅解析和更新APIV1。-2, --v2仅解析和更新APIV2。-v, --variable仅解析和更新可示意。-l, --license仅解析和更新apilicense。-f, --force这会跳过缓存并强制解析每一个字体。--no-validate - 完成解析后,它跳过援引npx gfm validate 。 npx gfm validate - 助手命令使用模式验证您现有的元数据。这会自动使用npx gfm parse 。
标志:
-1, --v1仅验证APIV1。-2, --v2仅验证APIV2。-v, --variable - 仅验证可验证。