一種元數據生成器,可獲取和解析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 - 僅驗證可驗證。