フォントメトリックに基づく自動フォントフォールバック
Playground Projectでは、 fontaineを有効化 /無効にして、カスタマイズは必要ありませ/ 。
| 前に | 後 | |
|---|---|---|
| CLS | 0.24 | 0.054 |
| パフォーマンス | 92 | 100 |
pnpmで
pnpm add -D fontaineまたは、 npmで
npm install -D fontaineまたは、 yarnで
yarn add -D fontaine import { FontaineTransform } from 'fontaine'
// Astro config - astro.config.mjs
import { defineConfig } from 'astro/config'
const options = {
fallbacks : [ 'BlinkMacSystemFont' , 'Segoe UI' , 'Helvetica Neue' , 'Arial' , 'Noto Sans' ] ,
// You may need to resolve assets like `/fonts/Roboto.woff2` to a particular directory
resolvePath : id => `file:///path/to/public/dir ${ id } ` ,
// overrideName: (originalName) => `${name} override`
// sourcemap: false
// skipFontFaceGeneration: (fallbackName) => fallbackName === 'Roboto override'
}
// Vite
export default {
plugins : [ FontaineTransform . vite ( options ) ]
}
// Next.js
export default {
webpack ( config ) {
config . plugins = config . plugins || [ ]
config . plugins . push ( FontaineTransform . webpack ( options ) )
return config
} ,
}
// Docusaurus plugin - to be provided to the plugins option of docusaurus.config.js
// n.b. you'll likely need to require fontaine rather than importing it
const fontaine = require ( 'fontaine' )
function fontainePlugin ( _context , _options ) {
return {
name : 'fontaine-plugin' ,
configureWebpack ( _config , _isServer ) {
return {
plugins : [
fontaine . FontaineTransform . webpack ( options ) ,
] ,
}
} ,
}
}
// Gatsby config - gatsby-node.js
const { FontaineTransform } = require ( 'fontaine' )
exports . onCreateWebpackConfig = ( { stage , actions , getConfig } ) => {
const config = getConfig ( )
config . plugins . push ( FontaineTransform . webpack ( options ) )
actions . replaceWebpackConfig ( config )
}
export default defineConfig ( {
integrations : [ ] ,
vite : {
plugins : [
FontaineTransform . vite ( {
fallbacks : [ 'Arial' ] ,
resolvePath : id => new URL ( `./public ${ id } ` , import . meta . url ) , // id is the font src value in the CSS
} ) ,
] ,
} ,
} )注Nuxtを使用している場合は、ボンネットの下で
fontaineを使用するNuxt-Font-Metricsをご覧ください。
カスタムフォントがCSS変数のメカニズムを介して使用される場合、フォンテーヌに援助の手を与えるためにCSS変数を微調整する必要があります。 Docusaurusはこの例であり、 --ifm-font-family-base変数を使用してカスタムフォントを参照します。フォンテーヌが変数をフォントに接続できるため、 {Name of Font} override辞をその変数に追加する必要があります。これはどのように見えますか? --ifm-font-family-base変数から参照されているカスタムフォントポピンを使用していたと想像してください。次の調整を行います。
:root {
/* ... */
- --ifm-font-family-base: 'Poppins';
+ --ifm-font-family-base: 'Poppins', 'Poppins override';舞台裏では、フォンテーヌによって作成された「ポピンオーバーライド」 @font-faceルールがあります。このオーバーライドフォントファミリをCSS変数に手動で追加することにより、フォンテーヌが生成する正しいフォントメトリックを使用して、当社のサイトをFallback @font-faceルールを使用させます。
fontaine 、 @font-faceルールをスキャンし、正しいメトリックを使用してフォールバックルールを生成します。例えば:
@font-face {
font-family : 'Roboto' ;
font-display : swap;
src : url ( '/fonts/Roboto.woff2' ) format ( 'woff2' ) , url ( '/fonts/Roboto.woff' )
format ( 'woff' );
font-weight : 700 ;
}
/* This additional font-face declaration will be added to your CSS. */
@font-face {
font-family : 'Roboto override' ;
src : local ( 'BlinkMacSystemFont' ) , local ( 'Segoe UI' ) , local ( 'Helvetica Neue' ) ,
local ( 'Arial' ) , local ( 'Noto Sans' );
ascent-override : 92.7734375 % ;
descent-override : 24.4140625 % ;
line-gap-override : 0 % ;
}次に、 font-family: 'Roboto'使用するたびに、 fontaineフォントファミリーにオーバーライドを追加します。
: root {
font-family : 'Roboto' ;
/* This becomes */
font-family : 'Roboto' , 'Roboto override' ;
}corepack enable ( npm i -g corepack for node.js <16.10を使用)pnpm installを使用して依存関係をインストールしますpnpm devを使用してインタラクティブテストを実行します。 pnpm demo:dev これはできなかったでしょう:
❤️で作られています
MITライセンスに基づいて公開されています。