Include Local Files in Browser Extension? - html

I'm currently developing a browser extension for Chrome and Safari. To be more specific, a tooltip appears when the user hovers over certain keywords. Currently, i'm using #font-face with a link to certain fonts hosted on my website. The problem is - they take a long time to load. Is it possible to include fonts locally in the browser extensions instead of linking to them externally? Can you just package the fonts (or even images?) with the other extension files?

Yes you can, it is quite common. Just make sure you have permissions to distribute all fonts/images that you did not author. Owning a personal license doesn't count.
Every extension is saved locally, whether the code that makes it function requires internet access and server-side scripting or not. A Chrome extension is just a directory saved as a crx archive, which is literally a renamed zip file. The directory, along with the necessary Developer info and metadata, is just like that of any other site, with html, and css, javascript, fonts, images, etc. Obviously if your extension does use server-side scripts, those need to be hosted. I'm sure you know most of this if you are building extensions, but I am elaborating so beginners can follow along.
Generally speaking, fonts are tiny files (rarely over 200k unless they are grunge type, or you are embedding way more characters than necessary) and so if they are self-hosted and taking too long to load, the problem is likely with your host. If I were you, I would use Google Fonts which don't need to be self-hosted and are free.
There are several ways
to embed them, no matter which you option you choose. But here is an example of how to link to fonts stored locally within the .crx file, <style> tags go between the <head> tags:
<style>
#font-face {
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: local('Open Sans'), local('OpenSans'), url("fonts/OpenSans-Regular-webfont.woff") format("woff");
}
#font-face {
font-family: 'Open Sans';
font-style: italic;
font-weight: 400;
src: local('Open Sans Italic'), local('OpenSans-Italic'), url("fonts/OpenSans-Italic-webfont.woff") format("woff");
}
</style>
Obviously, all paths are relative to the html file in your crx directory.

Related

Best method to load web fonts in case of caching and load time

There are several ways to embed a font on your web app.
Method A) Use google fonts CDN like this (in HTML):
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght#100&display=swap" rel="stylesheet">
Method B) Use your custom font like this (in CSS):
#font-face {
font-family: "Open Sans Regular";
src: url(../../fonts/OpenSans-Regular.ttf) format("truetype");
}
Method C) Convert your font to base64 string and use it like this:
#font-face {
font-family: 'Open Sans Regular';
src: url(data:application/font-woff;charset=utf-8;base64,d09GRgABAAAAAQeYABIAAAABv1gAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAEHfAAAABwAAAAcaMGk4EdERUYAAOeAAAAAHQAAAB4AJgOxR1BPUwAA6XgAMk1MYsAAAAAyehMTA==) format('woff');
font-weight: normal;
font-style: normal;
}
Let's say I have 10 iFrames inside my web app and each one uses multiple fonts.
What are the pros and cons of each method for using inside the iframes?
If I want the best method for caching and load time which one do you recommend? I don't want that micro-moment that fonts are not loaded and the fallback fonts are used by the browser.
Note: Using Method C I can put all the fonts inside one CSS file and link it to each iframe.
Using a CDN for delivering your static files such as CSS, JS, images, and other files are commonly preferred. This is because once your files are cached on the CDN's edge servers, your site visitors will be delivered static content from the closest point of presence (PoP) instead of the origin server.
In the majority of cases, this shortens the distance between the client and the server and thus helps improve loading times without adding any additional HTTP requests. This also helps in other areas such as increasing redundancy, taking a load off the origin, etc.
Source
But it also depends on your site!
As Craig Buckler writes on sitepoint
CDNs are an incredibly useful resource but remember to consider the consequences. In practice, most sites will benefit if they load jQuery, the HTML5 shim, and font files from a public CDN. For busier sites, the speed improvements and cost savings of a private CDN are hard to ignore.

Font won't load on server

A specific font with format .ttf is not loading on the webserver. Its an adobe font called Edwardian Script that ive been using from the adobe typekit.
I have tried to load this font on the live website but it doesn't seem to want to load.
I have tried changing file paths, i have added the format("truetype") as suggested answer in another SO Question but this didn't work either.
I had thought about loading it from google fonts but it costs money here and we already have this font in our libraries.
Ive been emptying caches and retrying on a separate computer.
I might note that it does show in Dreamweaver so I've been using a separate computer to confirm if its corrected.
I feel its a file path issue but i don't seem to be getting the right path.
Its hosted with cPanel in a public_html folder within a folder called fonts
public_html > fonts > Edwardian-Script > Edwardian-Script-ITC.ttf
CSS
#font-face {
font-family: Edwardian Script ITC;
src: url('/fonts/Edwardian-Script/Edwardian-Script-ITC.ttf') format("truetype");
}
.logo-text {
font-family: Edwardian Script ITC;
text-transform: none;
font-weight: 100;
font-kerning: none;
font-stretch: normal;
letter-spacing: normal;
}
HTML
<h3 class="logo-text"> Header Title </h3>
Expected to see loaded font from the server fonts folder
Maybe you need to add MIME Types on your server.
Log in to cPanel.
In the ADVANCED section of the cPanel home screen, click MIME Types

Adding custom font to asp.net application

I have never used a custom font before and have been trying to add it into my application. I have downloaded this font onto my computer and have made a separate file called 'Font' and in that i have put the font file.
I went into my CSS and have wrote this code:
#font-face {
font-family:'Open Sans';
src: url('../Font/OpenSans-Light.ttf');
}
The font isn't working at all and I'm wondering why it is not working?
Any ideas?
I am applying it to different sections, I should of said:
.title {
font-family: 'Open Sans';
font-size: 25px;
margin: 0 auto;
color: #52a3cc;
text-align: center;
}
I get this error message in IE Developer Tools:
"#font-face failed OpenType embedding permission check. Permission must be Installable."
You code should work if you can download http://www.yoursite.com/CorrectPath/OpenSans-Light.ttf in a browser.
If you cannot download it, you need to add at MIME types like SethG suggested.
Other thought
Open Sans is available via Google Fonts, so you do not have to host it by yourself.
I've had issues with this in the past. If you're using IIS Express to test this, you might have issues. For some reason, when IIS doesn't have a MIME type with which to serve content, it won't serve it. Make sure that whatever version of IIS you're using, be it IIS Express or IIS, has a MIME type for .ttf files.

how or if could I ship helvetica or similar fonts with my web-app?

I have a web-app (Java/HTML based) on cloud. Client will be accessing it in IE/Chrome or Mozilla.
I want to use Helvetica or any similar fonts but they are, by default, not available in systems (windows/IE/Chrome/Mozilla). Is there a way I can ship these fonts with my project?
Secondly are there any good free similar ones, which dont require licensing?
But most importantly, how is it possible technically? Do I put that as resource in my web-app? If yes, how? and would it then prompt users to download or install it - because thats a no (we cant ship installables with this product).
Thanks
Unless you have a license for the font, you can't include it with your project.
You can use something like
Google Fonts - free
Font Squirrel - free. Option to include fonts that you have licenses for.
Type Kit - different licensing options.
Font Squirrel is a good way to "package" it because it is just CSS + files for the fonts, as the others are cloud-based. You can filter by "web fonts" and then download the "Web Font Kit" which has everything you need. Easy!
To complement MikeSmithDev’s answer with a technical point: You can use the #font-face technique (described on many pages also frequently asked about at SO) just as you would on a web page, but you need not refer to font files in a server. You can simply include the .wof, .eot etc. files in the application package and refer to them with relative URLs, as in
#font-face {
font-family: 'Source Sans Pro';
src: url('sourcesanspro-regular-webfont.eot');
src: url('sourcesanspro-regular-webfont.eot?#iefix') format('embedded-opentype'),
url('sourcesanspro-regular-webfont.woff') format('woff'),
url('sourcesanspro-regular-webfont.ttf') format('truetype');
}
body { font-family: Source Sans Pro }
Although expressions like “downloadable fonts” and “web fonts” are often used, the technique works fine this way, too, without needing any download (apart from getting the application of course) or Internet connection when using the application.
So the user will not be prompted for allowing download, and he does not need to install the fonts; they are directly used by the browser.

How to load self hosted font files if the GoogleFont service is unavailable?

I want to use a font collection from Google Fonts directory. I selected the styles and include the CSS link tag in my project's template.
Alternatively, with Google Fonts you can also download the collection, and what you get is a zip file with all the font's styles.
I can create a CSS equivalent of the one Google gives me to include in the HTML, so I want to provide the self hosted font files as fallback, if the visitor can't access the Google Font API.
How do I setup this, and preventing both the Google font file and the self hosted font file from being downloaded? If the user does has access to Google Fonts, it's browser shouldn't download the self hosted version of the font.
I would recommend just self hosting them. This is fontsprings' "bullet-proof" font-face syntax.
#font-face {
font-family: 'MyFontFamily';
src: url('myfont-webfont.eot?#iefix') format('embedded-opentype'),
url('myfont-webfont.woff') format('woff'),
url('myfont-webfont.ttf') format('truetype'),
url('myfont-webfont.svg#svgFontName') format('svg');
}
Having all four of these set will ensure that it works across browsers. Just make sure to have your font in all four types. Font Squirrel has great kits for fontface as well.
You have 3 options:
Use 2 sets of #font-face at-rules, using different font-family names (e.g. "Droid Sans" and "Droid Sans Local"), and then using a font stack like "Droid Sans", "Droid Sans Local", Helvetica, Arial, sans-serif. However, this will cause both fonts to be downloaded, increasing load time.
Use a single set of #font-face at-rules, but use 2 sets of src attributes. This too may increase loading time if the browser decides to download all the font files specified.
Do mirroring at the network layer via DNS, like most CDNs do. This requires network setup, but your CSS would be unchanged, and it's most transparent to the browser, requiring no extra downloads.
Google Web Fonts is already employing the 3rd option, so I personally wouldn't bother if you're already using a CDN-hosted source. But it may be worthwhile if you're using fonts from a less reliable source. But if you're going to go through the effort to set this up for your fonts, why not set it up for all of your static resources (images, stylesheets, JS, etc.)? The most logical course of action is to just get a free or paid CDN to host all of your static assets on.