English 中文(简体)
How to tell when an @font-face rule is applied
原标题:

Is there any way to tell when an @font-face rule is applied? I m creating @font-face rules that use data: URIs from a font file requested with an synchronous XMLHttpRequest in JavaScript and then write text to a canvas element using the font right away. The thing is that it doesn t actually use the font when drawing the text for the first few milliseconds. My current solution is to block execution for a few milliseconds by sending a synchronous XMLHttpRequest, which is a terrible solution.

I cannot make this asynchronous as it is for implementing Processing s loadFont() function, which is synchronous, in Processing.js.

I would prefer that the solution not check the dimensions of character as there s no guarantee that the font has a certain character and that its dimensions are different from the same character of the current font.

最佳回答

Short answer: browsers are not good enough yet to let us test for "loaded and ready for use" without actually using the font and verifying it.

Long answer: Pjs comes with a built in font validator for font preloads (related to https://github.com/pomax/font.js) but as you point out, that will not work with @font-face rules that use a data-uri. A workaround that I would suggest (although I ve not tried this yet. I m just guessing it ll work based on my work on Processing.js and font loading in browsers) is to use two PGraphic offscreen buffers. Make them both white background with black fill text, do a text("X",0,0) on the first buffer, and then after your font load, use the second buffer to perform the same text("X",0,0"). Grab the pixel[] for each buffer, and do a side by side comparison:

boolean verifyFontLoad() {
  buffer1.loadPixels();
  buffer2.loadPixels();
  for (int i=0; i<buffer1.pixels.length; i++) {
    if (buffer1.pixels[i] != buffer2.pixels[i]) {
      return false; }}
  return true;
}

when you load your font, have a tracking boolean somewhere that indicates font load state, intiliased to "false", and after loading your font, at the start of draw() call

void draw() {
  // code that does not rely on your font
  [...]

  // check state
  if(!fontLoaded) { fontLoaded = verifyFontLoaded(); }
  // not ready? "stop" drawing.
  if(!fontLoaded) { return; }
  // font ready. run code that relies on the font being ready
  else {
    // code that depends on your font being available goes here.
  }
}

Now at least you can avoid the first few frames where the browser has finished unpacking your data-uri font and loaded it as @font-face resource, but hasn t had a time to pass it over to the Canvas2D rendering system yet.

(styling a DOM element at this point with the font would actually work fine, it s actually getting it handed over to Canvas2D that is causing it to not be usable one or more frames)

问题回答

Even though you don t prefer a dimension check, that is how the Modernizr @font-face feature detection works and may be the best way:

Modernizr embeds a tiny font glyph using a data: URI. In this, the single "." character is created; it is then applied to a temporary element whose innerHTML is set to ........ and whose width is measured, first with a basic serif font and then with the custom font applied. If the width is different, we know the browser rendered the new font data we supplied.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签