Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

using a non standard fontready loading event for @font-face

  • Loading branch information...
commit d859862cd3faa3b53b647374411227d30eb4a14d 1 parent 921c1e8
ScottDowne authored April 04, 2012

Showing 1 changed file with 23 additions and 50 deletions. Show diff stats Hide diff stats

  1. 73  src/fonts.js
73  src/fonts.js
@@ -469,7 +469,6 @@ var FontLoader = {
469 469
   // be ordered before the load in the subdocument.
470 470
   prepareFontLoadEvent: function fontLoaderPrepareFontLoadEvent(rules, names,
471 471
                                                                 fonts) {
472  
-      /** Hack begin */
473 472
       // There's no event when a font has finished downloading so the
474 473
       // following code is a dirty hack to 'guess' when a font is
475 474
       // ready.  This code will be obsoleted by Mozilla bug 471915.
@@ -501,6 +500,29 @@ var FontLoader = {
501 500
         return;
502 501
       }
503 502
 
  503
+      document.addEventListener( "fontready", function( e ) {
  504
+        for (var i = 0, ii = fonts.length; i < ii; ++i) {
  505
+          var font = fonts[i];
  506
+          font.loading = false;
  507
+        }
  508
+        var evt = document.createEvent('Events');
  509
+        evt.initEvent('pdfjsFontLoad', true, false);
  510
+        document.documentElement.dispatchEvent(evt);
  511
+      }, false );
  512
+
  513
+      var fileref=document.createElement("style"),
  514
+          innerHTML = "";
  515
+      fileref.setAttribute("rel", "stylesheet");
  516
+      fileref.setAttribute("type", "text/css");
  517
+
  518
+      for (var i = 0, ii = rules.length; i < ii; ++i) {
  519
+        innerHTML += rules[0];
  520
+      }
  521
+
  522
+      fileref.innerHTML = innerHTML;
  523
+
  524
+      document.getElementsByTagName("head")[0].appendChild(fileref);
  525
+
504 526
       var div = document.createElement('div');
505 527
       div.setAttribute('style',
506 528
                        'visibility: hidden;' +
@@ -512,55 +534,6 @@ var FontLoader = {
512 534
       }
513 535
       div.innerHTML = html;
514 536
       document.body.appendChild(div);
515  
-
516  
-      if (!this.listeningForFontLoad) {
517  
-        window.addEventListener(
518  
-          'message',
519  
-          function fontLoaderMessage(e) {
520  
-            var fontNames = JSON.parse(e.data);
521  
-            for (var i = 0, ii = fonts.length; i < ii; ++i) {
522  
-              var font = fonts[i];
523  
-              font.loading = false;
524  
-            }
525  
-            var evt = document.createEvent('Events');
526  
-            evt.initEvent('pdfjsFontLoad', true, false);
527  
-            document.documentElement.dispatchEvent(evt);
528  
-          },
529  
-          false);
530  
-        this.listeningForFontLoad = true;
531  
-      }
532  
-
533  
-      // XXX we should have a time-out here too, and maybe fire
534  
-      // pdfjsFontLoadFailed?
535  
-      var src = '<!DOCTYPE HTML><html><head>';
536  
-      src += '<style type="text/css">';
537  
-      for (var i = 0, ii = rules.length; i < ii; ++i) {
538  
-        src += rules[i];
539  
-      }
540  
-      src += '</style>';
541  
-      src += '<script type="application/javascript">';
542  
-      var fontNamesArray = '';
543  
-      for (var i = 0, ii = names.length; i < ii; ++i) {
544  
-        fontNamesArray += '"' + names[i] + '", ';
545  
-      }
546  
-      src += '  var fontNames=[' + fontNamesArray + '];\n';
547  
-      src += '  window.onload = function fontLoaderOnload() {\n';
548  
-      src += '    parent.postMessage(JSON.stringify(fontNames), "*");\n';
549  
-      src += '  }';
550  
-      // Hack so the end script tag isn't counted if this is inline JS.
551  
-      src += '</scr' + 'ipt></head><body>';
552  
-      for (var i = 0, ii = names.length; i < ii; ++i) {
553  
-        src += '<p style="font-family:\'' + names[i] + '\'">Hi</p>';
554  
-      }
555  
-      src += '</body></html>';
556  
-      var frame = document.createElement('iframe');
557  
-      frame.src = 'data:text/html,' + src;
558  
-      frame.setAttribute('style',
559  
-                         'visibility: hidden;' +
560  
-                         'width: 10px; height: 10px;' +
561  
-                         'position: absolute; top: 0px; left: 0px;');
562  
-      document.body.appendChild(frame);
563  
-      /** Hack end */
564 537
   }
565 538
 };
566 539
 

0 notes on commit d859862

Please sign in to comment.
Something went wrong with that request. Please try again.