Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding @font-face on the fly

I have a problem with JavaScript FileReader and/or CSS @font-face (or it could possibly be a HTML DOM issue - not sure).
I'm dragging a font file from desktop and dropping it into browser's window with a little help from HTML5 drag&drop, then receiving File object and trying to handle it.
Here's the JavaScript code which does this:

//getting the File object
var file = e.dataTransfer.files[0];
var reader = new FileReader();
reader.onload = function(event) {
    var contents = event.target.result;
    //most important thing here - appending style into HEAD (jQuery)
    $('head').append('<style type="text/css">@font-face { font-family:"myFont";
    src: url("'+contents+'"); }</style>');
};

reader.onerror = function(event) {
    console.error("File could not be read! Code " + event.target.error.code);
};

//read file
reader.readAsDataURL(file);

Final CSS appended to head look's like this:

<style type="text/css">
    @font-face {
        font-family: "myFont";
        src: url("data:;base64;Ahs5hD3..."); // here come's the URL of 'cached' font. A lots of letters and digits
    }
</style>

The CSS itself is appending without any problem. The problem is that the font-family isn't applied. After the whole magic I use Firebug (or it's Chrome equivalent) to set font-family of some elements to "myFont" but it applies default font (Times new roman I guess).

I know I can upload the font to achieve this (with PHP e.g.) but that's not the point.

Possible question here might be:
1. Is it possible to append CSS style on the fly and use it without reloading (probably TRUE if it is possible with firebug)?
2. Is it possible to use a font file from a not yet uploaded(local?) resource like that returned by javascript getAsDataUrl function (TRUE - it is possible with an image file)?
So, both things are possible in some circumstances, then what do I do wrong? (maybe file MIME type? Notice that in @font-face src parameter "data:" part is empty)

like image 209
matewka Avatar asked Dec 05 '12 21:12

matewka


People also ask

How do I add fonts to font face?

With the @font-face rule, web designers do not have to use one of the "web-safe" fonts anymore. In the @font-face rule you must first define a name for the font (e.g. myFirstFont), and then point to the font file.

How do you make a font face?

By allowing authors to provide their own fonts, @font-face eliminates the need to depend on the limited number of fonts users have installed on their computers. Usage: click the Add font(s) button, select the TTF, OTF, WOFF, WOFF2 or SVG fonts on your computer and click Convert.

What is font face in Word?

The @font-face CSS at-rule specifies a custom font with which to display text; the font can be loaded from either a remote server or a locally-installed font on the user's own computer.

How do I use face fonts in HTML?

You can use a <basefont> tag to set all of your text to the same size, face, and color. The font tag is having three attributes called size, color, and face to customize your fonts. To change any of the font attributes at any time within your webpage, simply use the <font> tag.


1 Answers

I don't have the time right now, to try it out, but as I've read your code, the issue may/should be, that you don't support a mime time in your data uri:

 src: url("data:;base64;Ahs5hD3..."

A rather "agressive" counter-example:

src: url(data:application/x-font-woff;charset=utf-8;base64,d09GRg

I'll get back to trying it out and digging into it.

like image 145
Ádám László Rocska Avatar answered Oct 04 '22 17:10

Ádám László Rocska