对于那些寻找 SCSS mixin 的人,包括woff2,SASS list.append对于有条件地添加源文件/格式很有用:
@mixin fface(
$path,
$family,
$type: "",
$weight: 400,
$style: normal,
$local1: null,
$local2: null,
$ttf: null,
$otf: null,
$eot: null,
$svg: null
) {
$src: null; // initialize an empty source path
// only load local files when both sources are present
@if $local1 and $local2 {
$src: append($src, local("#{$local1}"), comma);
$src: append($src, local("#{$local2}"), comma);
}
@if $otf {
$src: append($src, url("#{$path}#{$type}.otf") format("opentype"), comma);
}
// load default formats (woff and woff2)
$src: append($src, url("#{$path}#{$type}.woff2") format("woff2"), comma);
$src: append($src, url("#{$path}#{$type}.woff") format("woff"), comma);
// formats that should only be added at the end
@if $ttf {
$src: append($src, url("#{$path}#{$type}.ttf") format("truetype"), comma);
}
@if $svg {
$src: append($src, url("#{$path}#{$type}.svg##{$svg}") format("svg"), comma);
}
// the actual FONT FACE DECLARATION
@font-face {
font-family: $family;
// for compatibility reasons EOT comes first and is not appended
@if $eot {
src: url("#{$path}#{$type}.eot");
}
// load appended sources path
src: $src;
font-weight: $weight;
font-style: $style;
}
}
// USAGE
$dir: "assets/fonts/";
// declare family name
$familyName: "MyFont";
@include fface(
"#{$dir}#{$familyName}", $familyName, "-regular", 400, "normal",
"#{$familyName} Regular", "#{$familyName}-Regular", "ttf", "otf"
);
@include fface(
"#{$dir}#{$familyName}", $familyName, "-medium", 500, "normal",
"#{$familyName} Medium", "#{$familyName}-Medium", "ttf", "otf"
);
@include fface(
"#{$dir}#{$familyName}", $familyName, "-semibold", 600, "normal",
"#{$familyName} SemiBold", "#{$familyName}-SemiBold", "ttf", "otf"
);
// Material Icons
$familyName: "Material Icons"; // override previous value
$familyFileName: "MaterialIcons";
@include fface(
"#{$dir}#{$familyFileName}", $familyName, "-regular", 400, "normal",
$familyName, "#{$familyFileName}-Regular", "ttf", null, "eot"
);
@include fface(
"#{$dir}#{$familyFileName}", "#{$familyName} Outline", "-outline", 400, "normal",
"#{$familyName} Outline", "#{$familyFileName}-Outline", null, "otf", "eot"
);
.material-icons {
font-family: $familyName;
}
.material-icons-outline {
font-family: "#{$familyName} Outline";
}
该$type
参数用于在$family
.
如果您遇到无法解决的错误,请记得仔细检查您的字体目录 ( $dir
)。