Start adding a FontLoader class to isolate the font-loaded hack
This commit is contained in:
		
							parent
							
								
									bc7e2b0110
								
							
						
					
					
						commit
						07ca9bfec5
					
				
							
								
								
									
										53
									
								
								fonts.js
									
									
									
									
									
								
							
							
						
						
									
										53
									
								
								fonts.js
									
									
									
									
									
								
							| @ -80,6 +80,35 @@ var Fonts = { | |||||||
|   } |   } | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | var FontsLoader = { | ||||||
|  |   bind: function(fonts) { | ||||||
|  |     var worker = (typeof window == "undefined"); | ||||||
|  |     var ready = true; | ||||||
|  | 
 | ||||||
|  |     for (var i = 0; i < fonts.length; i++) { | ||||||
|  |       var font = fonts[i]; | ||||||
|  |       if (Fonts[font.name]) { | ||||||
|  |         ready = ready && !Fonts[font.name].loading; | ||||||
|  |         continue; | ||||||
|  |       } else { | ||||||
|  |         ready = false; | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       var obj = new Font(font.name, font.file, font.properties); | ||||||
|  | 
 | ||||||
|  |       var str = ""; | ||||||
|  |       var data = Fonts[font.name].data; | ||||||
|  |       var length = data.length; | ||||||
|  |       for (var j = 0; j < length; j++) | ||||||
|  |         str += String.fromCharCode(data[j]); | ||||||
|  | 
 | ||||||
|  |       worker ? obj.bindWorker(str) : obj.bindDOM(str); | ||||||
|  |     } | ||||||
|  |     return ready; | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| /** | /** | ||||||
|  * 'Font' is the class the outside world should use, it encapsulate all the font |  * 'Font' is the class the outside world should use, it encapsulate all the font | ||||||
|  * decoding logics whatever type it is (assuming the font type is supported). |  * decoding logics whatever type it is (assuming the font type is supported). | ||||||
| @ -113,13 +142,14 @@ var Font = (function () { | |||||||
|       return; |       return; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     var data; | ||||||
|     switch (properties.type) { |     switch (properties.type) { | ||||||
|       case "Type1": |       case "Type1": | ||||||
|         var cff = new CFF(name, file, properties); |         var cff = new CFF(name, file, properties); | ||||||
|         this.mimetype = "font/opentype"; |         this.mimetype = "font/opentype"; | ||||||
| 
 | 
 | ||||||
|         // Wrap the CFF data inside an OTF font file
 |         // Wrap the CFF data inside an OTF font file
 | ||||||
|         this.font = this.convert(name, cff, properties); |         data = this.convert(name, cff, properties); | ||||||
|         break; |         break; | ||||||
| 
 | 
 | ||||||
|       case "TrueType": |       case "TrueType": | ||||||
| @ -127,7 +157,7 @@ var Font = (function () { | |||||||
| 
 | 
 | ||||||
|         // Repair the TrueType file if it is can be damaged in the point of
 |         // Repair the TrueType file if it is can be damaged in the point of
 | ||||||
|         // view of the sanitizer
 |         // view of the sanitizer
 | ||||||
|         this.font = this.checkAndRepair(name, file, properties); |         data = this.checkAndRepair(name, file, properties); | ||||||
|         break; |         break; | ||||||
| 
 | 
 | ||||||
|       default: |       default: | ||||||
| @ -135,28 +165,12 @@ var Font = (function () { | |||||||
|         break; |         break; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     var data = this.font; |  | ||||||
|     Fonts[name] = { |     Fonts[name] = { | ||||||
|       data: data, |       data: data, | ||||||
|       properties: properties, |       properties: properties, | ||||||
|       loading: true, |       loading: true, | ||||||
|       cache: Object.create(null) |       cache: Object.create(null) | ||||||
|     } |     }; | ||||||
| 
 |  | ||||||
|     // Convert data to a string.
 |  | ||||||
|     var dataStr = ""; |  | ||||||
|     var length = data.length; |  | ||||||
|     for (var i = 0; i < length; ++i) |  | ||||||
|       dataStr += String.fromCharCode(data[i]); |  | ||||||
| 
 |  | ||||||
|     // Attach the font to the document. If this script is runnig in a worker,
 |  | ||||||
|     // call `bindWorker`, which sends stuff over to the main thread.
 |  | ||||||
|     if (typeof window != "undefined") { |  | ||||||
|       this.bindDOM(dataStr); |  | ||||||
|     } else { |  | ||||||
|       this.bindWorker(dataStr); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|   }; |   }; | ||||||
| 
 | 
 | ||||||
|   function stringToArray(str) { |   function stringToArray(str) { | ||||||
| @ -1420,6 +1434,7 @@ CFF.prototype = { | |||||||
|       i++; |       i++; | ||||||
|     } |     } | ||||||
|     error("failing with i = " + i + " in charstring:" + charstring + "(" + charstring.length + ")"); |     error("failing with i = " + i + " in charstring:" + charstring + "(" + charstring.length + ")"); | ||||||
|  |     return []; | ||||||
|   }, |   }, | ||||||
| 
 | 
 | ||||||
|   wrap: function wrap(name, charstrings, subrs, properties) { |   wrap: function wrap(name, charstrings, subrs, properties) { | ||||||
|  | |||||||
							
								
								
									
										35
									
								
								viewer.js
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								viewer.js
									
									
									
									
									
								
							| @ -3,7 +3,7 @@ | |||||||
| 
 | 
 | ||||||
| "use strict"; | "use strict"; | ||||||
| 
 | 
 | ||||||
| var pdfDocument, canvas, pageDisplay, pageNum, numPages, pageInterval; | var pdfDocument, canvas, pageDisplay, pageNum, numPages, pageTimeout; | ||||||
| function load(userInput) { | function load(userInput) { | ||||||
|     canvas = document.getElementById("canvas"); |     canvas = document.getElementById("canvas"); | ||||||
|     canvas.mozOpaque = true; |     canvas.mozOpaque = true; | ||||||
| @ -52,7 +52,7 @@ function gotoPage(num) { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function displayPage(num) { | function displayPage(num) { | ||||||
|     window.clearInterval(pageInterval); |     window.clearTimeout(pageTimeout); | ||||||
| 
 | 
 | ||||||
|     document.getElementById("pageNumber").value = num; |     document.getElementById("pageNumber").value = num; | ||||||
| 
 | 
 | ||||||
| @ -75,28 +75,12 @@ function displayPage(num) { | |||||||
|     page.compile(gfx, fonts); |     page.compile(gfx, fonts); | ||||||
|     var t2 = Date.now(); |     var t2 = Date.now(); | ||||||
| 
 | 
 | ||||||
|     var fontsReady = true; |     function loadFont() { | ||||||
| 
 |       if (!FontsLoader.bind(fonts)) { | ||||||
|     // Inspect fonts and translate the missing one
 |         pageTimeout = window.setTimeout(loadFont, 10); | ||||||
|     var count = fonts.length; |         return; | ||||||
|     for (var i = 0; i < count; i++) { |  | ||||||
|       var font = fonts[i]; |  | ||||||
|       if (Fonts[font.name]) { |  | ||||||
|         fontsReady = fontsReady && !Fonts[font.name].loading; |  | ||||||
|         continue; |  | ||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       new Font(font.name, font.file, font.properties); |  | ||||||
|       fontsReady = false; |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     function delayLoadFont() { |  | ||||||
|       for (var i = 0; i < count; i++) { |  | ||||||
|         if (Fonts[font.name].loading) |  | ||||||
|           return; |  | ||||||
|       } |  | ||||||
|       window.clearInterval(pageInterval); |  | ||||||
| 
 |  | ||||||
|       var t3 = Date.now(); |       var t3 = Date.now(); | ||||||
| 
 | 
 | ||||||
|       page.display(gfx); |       page.display(gfx); | ||||||
| @ -106,12 +90,7 @@ function displayPage(num) { | |||||||
|       var infoDisplay = document.getElementById("info"); |       var infoDisplay = document.getElementById("info"); | ||||||
|       infoDisplay.innerHTML = "Time to load/compile/fonts/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + "/" + (t4 - t3) + " ms"; |       infoDisplay.innerHTML = "Time to load/compile/fonts/render: "+ (t1 - t0) + "/" + (t2 - t1) + "/" + (t3 - t2) + "/" + (t4 - t3) + " ms"; | ||||||
|     }; |     }; | ||||||
| 
 |     loadFont(); | ||||||
|     if (fontsReady) { |  | ||||||
|       delayLoadFont(); |  | ||||||
|     } else { |  | ||||||
|       pageInterval = setInterval(delayLoadFont, 10); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function nextPage() { | function nextPage() { | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user