Including a font in Angular.js (.ttf) - css

I have a .ttf font file that I need to use in my Angular.js application. I don't know how to import it and access it in my css files.
Could someone give me some direction in using this font file with Angular/CSS?

Including a font has nothing to do with angularjs. You have to declare it in a CSS file:
Take this chunk of my own as an example, declared in a stylesheet:
#font-face {
font-family: 'Durant';
src: url('../fonts/DurantBold.eot'); /* IE9 Compat Modes */
src: url('../fonts/DurantBold.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('../fonts/DurantBold.otf') format('opentype'), /* Legacy iOS */
url('../fonts/DurantBold.svg#Durant-Bold') format('svg'), /* Legacy iOS */
url('../fonts/DurantBold.ttf') format('truetype'), /* Safari, Android, iOS */
url('../fonts/DurantBold.woff') format('woff');
font-weight: bold;
}
Remember that the paths are relative to the css file.
Today, most of the file formats are supported by most browsers - I don't know, concretely, the incompatibilities among browsers and fonts. This style is somewhat old.
Besides, I have all of those files (1 file per font, per format, per weight variation, per style variation - highly frustrating). You will not include configs for files you don't have.
If you have only .ttf files you only need this snippet in the .css file:
#font-face {
font-family: 'Durant';
src: url('../fonts/DurantBold.ttf') format('truetype');
font-weight: bold;
}
remember to actually include the css file where you declared this chunk, in the page where you will use it. If you use states (ngRouter / ui.router), then include the font in the MAIN page, not in the partials.
remember to have the font files (.ttf) in a location accessible by the declaration in this css file either being:
Absolute URL / CDN: this needs no explanation.
relative-to-site url: a path starting with / refers the path being relative to document root, as always.
relative url: a path not starting with / is relative to the current css file.
I know I wrote that many times but it always causes headaches when forgotten.

Related

Failed to decode downloaded font, but using correct CSS Font Face

I am using the following code to import the google Font "Syncopate" into my shopify site:
#font-face {
font-family: 'Syncopate';
font-weight: 400;
font-style: normal;
src: url('//cdn.shopify.com/s/files/1/0633/5778/0153/t/2/assets/Syncopate-Regular.eot'); /* IE9 Compat Modes */
src: url('//cdn.shopify.com/s/files/1/0633/5778/0153/t/2/assets/Syncopate-Regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('//cdn.shopify.com/s/files/1/0633/5778/0153/t/2/assets/Syncopate-Regular.woff2') format('woff2'), /* Super Modern Browsers */
url('//cdn.shopify.com/s/files/1/0633/5778/0153/t/2/assets/Syncopate-Regular.woff') format('woff'), /* Pretty Modern Browsers */
url('//cdn.shopify.com/s/files/1/0633/5778/0153/t/2/assets/Syncopate-Regular.ttf') format('truetype'), /* Safari, Android, iOS */
url('//cdn.shopify.com/s/files/1/0633/5778/0153/t/2/assets/Syncopate-Regular.svg#Syncopate') format('svg'); /* Legacy iOS */
}
I downloaded the .tff file from google fonts and created the font-face with transformer.org.
In the console of greenjet.at I now get the following errors:
Failed to decode downloaded font (woff2, woff, ttf)
OTS parsing error: Size of decompressed WOFF 2.0 is less than compressed size
OTS parsing error: incorrect file size in WOFF header
OTS parsing error: FFTM: misaligned table
When implementing it with the google font link it works perfectly:
<link rel="stylesheet"
href="https://fonts.googleapis.com/css?family=Syncopate">
But it is necessary due to privacy issues with google to load the font locally.
What is the error here?
Is the shopify asset file uploader maybe using ASCII instead of the binary file transfer causing this error?
Add the code below, replacing highlighted elements of the code with actual data.
Font name > the name of the font. If the font has more than one word in its name, it will need "quotation marks";
Filename > the name of the file that was just uploaded to your theme including the file extension, for example, cosmic_sans_bold.ttf.
Format > the format of the font that was uploaded. For example, for Athena.ttf, this would be "TrueType".
#font-face {
font-family: "Font name";
src: url({{ "Filename" | asset_url }}) format("Format");
}
Available font formats: "woff", "woff2", "truetype", "opentype", "embedded-opentype" and "svg".
Fonts in your theme
Non-admin
Add the font files to the assets directory.
Create a #font-face CSS rule so that you can reference the font. Use the asset_url filter to output the URL for the font file:
#font-face {
font-family: "Font name";
src: url("{{ '[font-file-name]' | asset_url }}") format("[font-format]");
}
Shopify admin
Upload the font files to the Settings > Files section of the Shopify admin.
Create a #font-face CSS rule so that you can reference the font. Use the file_url filter to output the URL for the font file:
#font-face {
font-family: "Font name";
src: url("{{ '[font-file-name]' | file_url }}") format("[font-format]");
}
Otfs parsing errors usually indicate, your font files are not valid.
The aforementioned font file is definitely corrupt/not valid
Your #font-face rules looks fine.
Although, transfonter is usually pretty reliable, you might have applied options (subsetting, metrics adjustments), that made the file structure invalid.
Instead of converting the master truetype files, try to get all font-files via google web font helper..
Then try to upload/replace the font files.
Debugging: You should be able to download the new truetype font and doubleclick it in your Desktop environment (win, MacOs, Linux).
If your OS still bugging you about an invalid format - there's something wrong with the shopify uploader.
So you should contact the shopify support.
Maybe there's a bug in the file uploader (e.g. converting encodings).

Custom local fonts not working with webpack 5

I have some local fonts I want to use in my project. I've read a few tutorials and questions on this, and I'm following the reccomendations I've seen, but my fonts are not showing up properly in the browser. I am using webpack 5. In my webpack config:
module.exports = {
// ...
module: {
rules: [
// ...
{
test: /\.(woff|woff2|ttf)$/,
use: {
loader: "url-loader",
},
},
]
}
}
I have a bunch of .tff font files in my src/assets/fonts/ directory. I have a .scss file for global styles. In there, I define the font names and I want to use, and where webpack should find them:
#font-face {
font-family: "InterRegular";
src: url("../assets/fonts/Inter-Regular.ttf") format("truetype");
font-display: swap;
}
#font-face {
font-family: "InterMedium";
src: url("../assets/fonts/Inter-Medium.ttf") format("truetype");
font-display: swap;
}
#font-face {
font-family: "InterSemiBold";
src: url("../assets/fonts/Inter-SemiBold.ttf") format("truetype");
font-display: swap;
}
// etc
I'm fairly sure webpack is finding these, because if I get the path to the file wrong, webpack errors. I then try to apply the font:
html,
body {
font-family: "InterSemiBold", sans-serif;
}
There are no errors, but the font does not get applied to the page. When I look in my network tab, I can see that a font file is indeed being loaded:
But this is clearly not the InterSemiBold font. Regardless of what font I'm using, this strangely-named .tff file always shows this same, seriffed font.
Looking at the computed value of an element, I can see that the browser is reading the "InterSemiBold", sans-serif value of the font family, but still defaulting to Arial:
I have also tried loading in fonts using the file-loader with webpack, but that makes no difference, and many recommend using url-loader instead.
What am I doing wrong here? Why is my font not being loaded in and applied?
Your dev tools screenshot indicates your actual page/app style sheet expects the font-family name to be 'Inter'.
So you don't need different family names for each font-weight
and change your #font-face rules to something like this:
#font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 400;
src: url('../assets/fonts/Inter-Regular.ttf') format('truetype')
}
#font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 500;
src: url('../assets/fonts/Inter-Medium.ttf') format('truetype')
}
#font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 600;
src: url('../assets/fonts/Inter-SemiBold.ttf') format('truetype')
}
#font-face {
font-family: 'Inter';
font-style: normal;
font-weight: 700;
src: url('../assets/fonts/Inter-Bold.ttf') format('truetype')
}
Your #font-face rules should include a font-style value.
For italic styles you would change it to font-style: normal.
The font-url must use the exact file name of a font style (just a note, as some automatic font loaders rename the filenames internally or load updated files directly from Google - resulting in filenames like this "inter-v11-latin-800.ttf").
Since a browser can't automatically tell which intermediate weight would be e.g 'semi-bold' or 'light', you add specific numeric font-weight values which can be used to map all font-weights to different selectors like this:
body{
font-family:Inter;
font-size:16px;
}
.medium{
font-weight:500;
}
.semibold{
font-weight:600;
}
strong, h1, h2,
.button{
font-weight:700;
}
You might also double check your main css – it might also contain a separate #font-face declaration.
If everything is working fine, you should see the .tff files in dev tools just as defined in #font-face urls (e.g. "Inter-Regular.ttf")
Still not working?
Try to open the font via absolute URL in your browser.
Font file connection test example
Provided your compiled folder structure looks something like this:
the final URL is "myapp.com"
the main css is located under URL "myapp.com/css/main.css"
font files are located (at least according to your css/compiling code) in directory URL "myapp.com/assets/fonts/"
the actual font files should be available (downloadable) under URL
"myapp.com/assets/fonts/Inter-Regular.ttf"
If this doesn't work – you need to fix the URLs in your #font-face rule.
This especially important, if assets are copied/assembled during a compiling process to a new directory – so previously paths/URLs might not be "automagically" fixed.
Another cause might be inlined css – so the css becomes part of the compiled HTML <head> or <body> – relative paths/URLs might not work anymore => absolute paths could fix this (... albeit, any decent auto inlining script should be smart enough to translate relative to absolute URLs).
Compiled css
The final css might also include some overriding rules.
So check the finally compiled css in devtools and search for any #font-face rules – as a last resort: add a !important keyword to a font src to enforce the desired URL.
Font files might be corrupt?
Since the "inter" is available as free google webfont you could get a "fresh" copy via google webfonts helper
I was having the same problem as you with Webpack 5 and a custom local font, none of the above suggestions worked, but I just solved it, here's how: When I went to Google Fonts the only option was to download a TTF and that's what I had been trying to use. So, I visited the google-webfonts-helper website which gives you the code to put in your CSS file to make sure I was doing it correctly, and it instead had me download a WOFF and WOFF2 of the font. When I used these files the fonts rendered properly in my Chrome browser right away. I then found a few other forums from the past where people had issues with Chrome rendering TTF's and solved them by switching to WOFF formats. I don't know exactly why this works but it did.

Adding custom font to laravel Applications

So, I am confused by this:
#font-face {
font-family:'rift';
src: url('../vendor/fonts/Fort Foundry - Rift-Bold.otf') format('otf');
font-style: 'normal';
font-weight: '700';
}
It lives in public/vendor/fonts/ and whats confusing is that unless I physically install the font - the font doesn't render properly.
I thought the whole aspect of using custom fonts was that the end user did not have to have them installed on their machine, that it should just work. Am I missing something when it comes to using custom fonts in css?
I get no errors in the console with or with out the font installed. Is there a way to verify that it is actually working? Or is that why we have things like fallbacks?
.site-name{
color: #fff;
font-size: 22px;
font-family: rift, sans-serif; // Fallbacks
}
Can some one explain the concept of using custom fonts and if I am setting it up properly in css? From a Laravel perspective.
You can host the font files on your server, to make use of a custom font on your site. Visitors are not required to have the font installed on their machine.
Make sure to choose a file name without spaces and special chars. You'll need to offer different formats of the font (like .eot, .woff2,...) to make it work across the different browsers and operating systems. Otherwise, the fallback font (in your case sans-serif) will be used.
Check out the article Using #font-face on CSS-Tricks. This is how your CSS could look like:
#font-face {
font-family: 'rift';
font-style: 'normal';
font-weight: '700';
src: url('../vendor/fonts/fort-foundry.eot'); /* IE9 Compat Modes */
src: url('../vendor/fonts/fort-foundry.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('../vendor/fonts/fort-foundry.woff2') format('woff2'), /* Super Modern Browsers */
url('../vendor/fonts/fort-foundry.woff') format('woff'), /* Pretty Modern Browsers */
url('../vendor/fonts/fort-foundry.ttf') format('truetype'), /* Safari, Android, iOS */
url('../vendor/fonts/fort-foundry.svg#FortFoundry') format('svg'); /* Legacy iOS */
}
Troubleshooting
Visit your site and open the Developer Tools. Go to the tab Console.
Reload the page and look out for errors to make sure that the path to your font files is correct. If it's not, there will be an error 404 visible in the logs.

How to make custom fonts visible to all users?

I 'm currently building a website for a language with another person and there is a problem, because a font is necessary to show the alphabet of this language, for which normal characters don't exist. But apparently, however I can view the font, he can't. It is possible for us to put a download-link to the font at our homepage, but I would prefer every user to see it immediately. The CSS is:
#font-face {
font-family: gothicw;
src: url("gothic.eot") /* EOT file for IE */
}
#font-face {
font-family: gothicw;
src: url("gothic.ttf") /* TTF file for CSS3 browsers */

Data URI in embedded font declaration (#font-face) breaks IE < 9

I have a CSS file with a #font-face declaration that embeds the font file via a data URI:
#font-face {
font-family: 'Custom-Font';
src: url('eot/font.eot');
src: url('eot/font.eot?#iefix') format('embedded-opentype'),
/* ugly FF same-Origin workaround... */
url("data:application/octet-stream;base64,d09GRgABAAAAA ... ") format('woff'),
url('ttf/font.ttf') format('truetype'),
url('svg/font.svg#Custom-Font') format('svg');
}
Embedding the font with the data URI causes IE < 9 to not load the font. If I remove the line (or change it back to reference the .woff file), IE will load the font.
What about this CSS confuses IE?
Background: I have a page which loads embedded fonts from a different domain (a CDN). Unfortunately, Mozilla requires a CORS header (Access-Control-Allow-Origin) on embedded fonts served from different domains (an abuse of CORS and terrible idea in my opinion). For reasons beyond my control (bureaucracy), I'm unable to get the CORS header sent out on font files, so I'm stuck with the sub-optimal situation of embedding the font file in the CSS file via a data URI.
I had the same problem. Moving the embedded font into a different declaration worked for me.
#font-face {
/* Non-FF */
font-family: 'Custom-Font';
src: url('eot/font.eot');
src: url('eot/font.eot?#iefix') format('embedded-opentype'),
url('svg/font.svg#Custom-Font') format('svg');
}
#font-face {
/* FF same-Origin workaround... */
font-family: 'Custom-Font';
src: url(data:font/truetype;charset=utf-8;base64,d09GRgABAAAAA...) format('truetype');
}
The maximum URL length has probably been exceeded.
Remember that older versions of IE adds everything between the ? and the last '); (including the data URI).
So in your case the solution would be to use another method (Mo' Bulletproofer for example).

Resources