Automatische Schriftfunktion basierend auf Schriftartmetriken
Durch das Aktivieren / Deaktivieren fontaine werden im Spielplatzprojekt die folgende Unterschiede zur Renderung / ohne Anpassung erforderlich:
| Vor | Nach | |
|---|---|---|
| Cls | 0.24 | 0.054 |
| Leistung | 92 | 100 |
Mit pnpm
pnpm add -D fontaine Oder mit npm
npm install -D fontaine Oder mit 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
} ) ,
] ,
} ,
} )Hinweis Wenn Sie Nuxt verwenden, lesen Sie die Nuxt-font-metrics, die
fontaineunter der Haube verwendet.
Wenn Ihre benutzerdefinierte Schriftart durch den Mechanismus von CSS -Variablen verwendet wird, müssen Sie eine Optimierung an Ihren CSS -Variablen vornehmen, um Fontaine eine helfende Hand zu geben. Docusaurus ist ein Beispiel dafür. Es verwendet die Variable --ifm-font-family-base um auf eine benutzerdefinierte Schriftart zu verweisen. Damit Fontaine die Variable mit der Schriftart verbinden kann, müssen wir dieser Variablen einen {Name of Font} override . Wie sieht das aus? Stellen Sie sich vor, wir hätten die benutzerdefinierten Schriftarten verwendet, auf die aus der Variablen --ifm-font-family-base verwiesen wird, würden wir die folgende Anpassung vornehmen:
:root {
/* ... */
- --ifm-font-family-base: 'Poppins';
+ --ifm-font-family-base: 'Poppins', 'Poppins override'; Hinter den Kulissen befindet sich eine "Poppins Override" @font-face -Regel, die von Fontaine erstellt wurde. Indem wir diese Override-Schriftfamilie zu unserer CSS-Variablen manuell hinzufügen, verwenden wir unsere Website mit den richtigen @font-face , die Fontaine generiert.
fontaine scannt Ihre @font-face -Regeln und generiert Fallback-Regeln mit den richtigen Metriken. Zum Beispiel:
@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 % ;
} Wenn Sie dann font-family: 'Roboto' , fügt fontaine die Schriftfamilie über die Schriftfamilie hin:
: root {
font-family : 'Roboto' ;
/* This becomes */
font-family : 'Roboto' , 'Roboto override' ;
}corepack enable (Verwenden Sie npm i -g corepack für node.js <16.10)pnpm installpnpm dev aus; Starten Sie einen Vite -Server mit dem Quellcode mit pnpm demo:dev Dies wäre nicht möglich gewesen, ohne:
Gemacht mit ❤️
Veröffentlicht unter MIT -Lizenz.