Load multiple weight custom font with the webfont loader - font-face

When defining a custom font with the webfont loader (repo here), we basically define the families loaded and the related URLs:
WebFont.load({
custom: {
families : [ "My font" ],
urls : [ "assets/css/fonts.css" ]
}
});
But, it seems the loader don't detect multiple weight defined for the same font in the css file:
#font-face {
font-family: 'My font';
src: url("../fonts/my-font.eot");
font-weight: normal;
font-style: normal;
}
#font-face {
font-family: 'My font';
src: url("../fonts/my-font.eot");
font-weight: bold;
font-style: normal;
}
And so, the loader trigger the active event when the first font is loaded. This can be confirmed if we check the fontactive event who'll only be triggered once:
WebFont.load({
fontactive: function( fontname, fontdescription ) {
console.log( fontname, fontdescription );
// Only trigger once `My font, n4`
}
});
So, is there a way tell the webfont loader there's multiple weight to get (a bit like their google webfonts interface)?
(A fix can be to use multiple names for each font weight, but that's not the solution I'm searching for here)

I'm one of the developers of the webfontloader. You are correct that the custom module does not support loading multiple variations. Luckily we recently added support for this, so if you upgrade your version of the webfontloader (or use the one on the Google CDN) you'll get support for it.
You can use it like:
WebFont.load({
custom: {
families: ['My Font', 'My Other Font:n4,i4,n7'],
urls: ['/fonts.css']
}
});
To load the 'n4', 'i4' and 'n7' variations of "My Other Font".

Related

How to load scss variables and fonts file from node_modules in Vite

I am using nuxt-vite in a SSR nuxt project, here is my code
nuxt.config.js
module.exports = {
css: [
'~/assets/fonts.scss',
]
}
I want to use #mdi/font in my project, and here is where it located in node_modules:
First, I try to import the css directly:
#import '#mdi/font/css/materialdesignicons.css
But the browser can not find the fonts files
Then, I try to load the raw scss variables just like what I did when using webpack whick works without any issues.
#import "#mdi/font/scss/variables";
#import "#mdi/font/scss/functions";
#font-face {
font-family: '#{$mdi-font-name}';
src: url('#mdi/font/fonts/#{$mdi-filename}-webfont.eot?v=#{$mdi-version}');
src: url('#mdi/font/fonts/#{$mdi-filename}-webfont.eot?#iefix&v=#{$mdi-version}') format('embedded-opentype'),
url('#mdi/font/fonts/#{$mdi-filename}-webfont.woff2?v=#{$mdi-version}') format('woff2'),
url('#mdi/font/fonts/#{$mdi-filename}-webfont.woff?v=#{$mdi-version}') format('woff'),
url('#mdi/font/fonts/#{$mdi-filename}-webfont.ttf?v=#{$mdi-version}') format('truetype');
font-weight: normal;
font-style: normal;
}
#import "#mdi/font/scss/core";
#import "#mdi/font/scss/icons";
#import "#mdi/font/scss/extras";
#import "#mdi/font/scss/animated";
But the vite can not load the scss variables correctly:
How can I use the fonts in node_modules when using vite?
I googled it and read the doc but helped little.
Greate thanks to anyone help!
add this to nuxt.config.js
vite: {
css: {
preprocessorOptions: {
scss: {
additionalData: `#import "~/assets/styles/Variables.scss";\n#import "~/assets/styles/Mixins.scss";\n`
}
}
}
},

Tailwind css laravel mix add fonts

I am trying to use tailwndo css for a project in laravel and I would like to maintain the nunito font for the whole app but Tailwind has its own font set. Does anybody know how to change it?
Connect the font to the project (as you usually do) and just add your font to tailwind.config.js
module.exports = {
theme: {
fontFamily: {
'sans': ['-apple-system', 'BlinkMacSystemFont', ...],
'serif': ['Georgia', 'Cambria', ...],
'mono': ['SFMono-Regular', 'Menlo', ...],
'your-font': ['Your Font', ...]
}
}
}
https://tailwindcss.com/docs/font-family/#app
tailwind.config.js :
theme: {
extend: {
fontFamily: {
body: ['Rowdies']
}
}
},
css\app.css
#import url('https://fonts.googleapis.com/css2?family=Rowdies:wght#300&display=swap');
shell:
npm run dev
or
npm run watch
now you can use .font-body class in any tag that you want
for example:
<body class="font-body">
<h1>Hello World!</h1>
</body>
font + body = font-body
fontFamily: {
body: ['Open Sans']
}
(you can change body)

laravel-mix / webpack, ignore sass loaded file (font) to be exported

I am trying to skip a dependency loaded font (with url() ) to be exported.
I used ignore-loader module.
mix.webpackConfig({
module: {
rules: [
{
test: /context-menu-icons\.(eot|ttf|otf|woff|woff2)$/,
loader: 'ignore-loader'
}
]
}
});
The issue is it produce empty files. The behavior I am expecting is to not output these files at all.
The reason for this is this in a dependency in node_modules
#font-face {
font-family: '#{$context-menu-icon-font-name}';
src: url('#{$context-menu-icon-font-path}#{$context-menu-icon-font-name}.eot?#{$context-menu-icons-cachebust}');
src: url('#{$context-menu-icon-font-path}#{$context-menu-icon-font-name}.eot?#{$context-menu-icons-cachebust}#iefix') format('embedded-opentype'),
url('#{$context-menu-icon-font-path}#{$context-menu-icon-font-name}.woff2?#{$context-menu-icons-cachebust}') format('woff2'),
url('#{$context-menu-icon-font-path}#{$context-menu-icon-font-name}.woff?#{$context-menu-icons-cachebust}') format('woff'),
url('#{$context-menu-icon-font-path}#{$context-menu-icon-font-name}.ttf?#{$context-menu-icons-cachebust}') format('truetype');
font-weight: normal;
font-style: normal;
}
Is there a way to just ignore these files from output / copy in my dist folder ?
I could not find a solution because, font-face is loaded from a scss file.
So using options with file-loader to prevent emitting files won't work.
There would be a string in these file with module.exports = ...
In all cases the font files would be emmited empty or not.
So I used clean-webpack-plugin with these options
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
...
mix.webpackConfig({
plugins: [
new CleanWebpackPlugin({
// dry: true,
verbose: true,
// Automatically remove all unused webpack assets on rebuild
cleanStaleWebpackAssets: false,
// Do not allow removal of current webpack assets
protectWebpackAssets: false,
cleanOnceBeforeBuildPatterns: [
'**/*', // clean all
'!views','!views/**/*', // ignore views folder
'!lang','!lang/**/*', // ignore lang folder
],
cleanAfterEveryBuildPatterns: [
'fonts/vendor' // remove fonts/vendor folder after build and watch
],
}),
]
});
...

Add Background image in SCSS with rollup bundle?

I can't able to use image path in scss file as background with rollup bundle.
My rollup config looks like this.
import typescript from 'rollup-plugin-typescript2';
import commonjs from 'rollup-plugin-commonjs';
import external from 'rollup-plugin-peer-deps-external';
import resolve from 'rollup-plugin-node-resolve';
import scss from 'rollup-plugin-scss';
import image from 'rollup-plugin-img'; // module solves image add in source
import pkg from './package.json';
export default {
input: 'src/index.tsx',
output: [
{
file: pkg.main,
format: 'cjs',
exports: 'named',
sourcemap: true,
},
{
file: pkg.module,
format: 'es',
exports: 'named',
sourcemap: true,
},
],
plugins: [
image({
extensions: /\.(png|jpg|jpeg|gif|svg)$/,
limit: 10000
}),
scss(),
external(),
resolve({
broswer: true
}),
typescript({
rollupCommonJSResolveHack: true,
exclude: '**/__tests__/**',
clean: true,
}),
commonjs({
include: ['node_modules/**'],
namedExports: {
'node_modules/react/react.js': ['Children', 'Component', 'PropTypes', 'createElement'],
'node_modules/react-dom/index.js': ['render'],
},
})
],
};
I am trying to add an image path in scss.
.chipsComponent > div:last-child {
background: url("/assets/images/Close_Circle.svg") no-repeat;
background-size: cover;
height: 16px;
width: 16px;
float: right;
cursor: pointer;
}
image path is adding in browser but rollup is not serving asset folder.Attached screesnhot
The image should show in circled chips(red).
Have I missed anything in rollup config or scss image usage is wrong?
Thanks in Advance
The scss line below is not actually importing the image:
background: url("/assets/images/Close_Circle.svg") no-repeat;
The rollup plugin rollup-plugin-img relies on resources being used via import declarations, like in a js file:
import logo from './rollup.png';
You have two options:
Write your own SCSS processor plugin that resolves url(...) references to copy over assets into your bundle directory.
Use a more manual rollup plugin like rollup-plugin-copy.
With rollup-plugin-copy you can use the following code in your rollup config:
plugins: [
copy({
targets: [
{ src: 'src/assets/images', dest: 'dist/assets/images' },
],
}),
This will copy over all of your images from your source folder to your bundle output.

#include font-face SCSS issue

While trying to have my SCSS import some fonts I encountered the following:
I exactly copied the docs from the compass website, but when the CSS is being compiled Compass adds random numbers behind my src URLs. The SCSS code I wrote and the resulting CSS looks like this
SCSS
#include font-face("NexaBold", font-files("nexa_bold-webfont.woff", "nexa_bold-webfont.ttf", "nexa_bold-webfont.svg", "nexa_bold-webfont.eot"));
Resulting CSS
#font-face {
font-family: "NexaBold";
src: url('/fonts/nexa_bold-webfont.woff?1417439024') format('woff'), url('/fonts/nexa_bold-webfont.ttf?1417439024') format('truetype'), url('/fonts/nexa_bold-webfont.svg?1417439024') format('svg'), url('/fonts/nexa_bold-webfont.eot?1417439024') format('embedded-opentype');
}
Thanks!
Random numbers were added because browser cache fonts base on url, then these random numbers cause every time you compile your codes and put it in your html, it download fonts again.
I have Visual Studio 2013 and compile your code with sass and the result is:
#font-face {
font-family: "NexaBold";
src: font-files("nexa_bold-webfont.woff", "nexa_bold-webfont.ttf", "nexa_bold-webfont.svg", "nexa_bold-webfont.eot"); }
and here is my compass source for font-face mixin:
#mixin font-face(
$name,
$font-files,
$eot: false,
$weight: false,
$style: false
) {
$iefont: unquote("#{$eot}?#iefix");
#font-face {
font-family: quote($name);
#if $eot {
src: font-url($eot);
$font-files: font-url($iefont) unquote("format('eot')"), $font-files;
}
src: $font-files;
#if $weight {
font-weight: $weight;
}
#if $style {
font-style: $style;
}
}
}
if you look my compass version doesn't add any random number at the end of file path.
I myself suggest you to use font-face without compass, use code below:
#font-face {
font-family: 'IranSans';
src: url('/css/fonts/IranSans.eot'); /* IE9 Compat Modes */
src: url('/css/fonts/IranSans.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('/css/fonts/IranSans.woff') format('woff'), /* Modern Browsers */
url('/css/fonts/IranSans.ttf') format('truetype'), /* Safari, Android, iOS */
url('/css/fonts/IranSans.svg') format('svg'); /* Legacy iOS */
}
Just add this line to your config.rb:
asset_cache_buster :none

Resources