From 986f0e9d468c9db10db4408a71f968f531eee54d Mon Sep 17 00:00:00 2001 From: Frederic van der Essen Date: Tue, 19 Aug 2014 16:43:05 +0200 Subject: [PATCH] [FIX] point_of_sale: the receipt would not print correctly on first try --- addons/point_of_sale/static/lib/waitfont.js | 59 ++++++++++++++++++++ addons/point_of_sale/static/src/js/models.js | 23 +++++++- addons/point_of_sale/views/point_of_sale.xml | 1 + 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 addons/point_of_sale/static/lib/waitfont.js diff --git a/addons/point_of_sale/static/lib/waitfont.js b/addons/point_of_sale/static/lib/waitfont.js new file mode 100644 index 00000000000..e9359896d3f --- /dev/null +++ b/addons/point_of_sale/static/lib/waitfont.js @@ -0,0 +1,59 @@ +// http://stackoverflow.com/questions/4383226/using-jquery-to-know-when-font-face-fonts-are-loaded +(function(){ + function waitForWebfonts(fonts, callback) { + var loadedFonts = 0; + for(var i = 0, l = fonts.length; i < l; ++i) { + (function(font) { + var node = document.createElement('span'); + // Characters that vary significantly among different fonts + node.innerHTML = 'giItT1WQy@!-/#'; + // Visible - so we can measure it - but not on the screen + node.style.position = 'absolute'; + node.style.left = '-10000px'; + node.style.top = '-10000px'; + // Large font size makes even subtle changes obvious + node.style.fontSize = '300px'; + // Reset any font properties + node.style.fontFamily = 'sans-serif'; + node.style.fontVariant = 'normal'; + node.style.fontStyle = 'normal'; + node.style.fontWeight = 'normal'; + node.style.letterSpacing = '0'; + document.body.appendChild(node); + + // Remember width with no applied web font + var width = node.offsetWidth; + + node.style.fontFamily = font; + + var interval; + function checkFont() { + // Compare current width with original width + if(node && node.offsetWidth != width) { + ++loadedFonts; + node.parentNode.removeChild(node); + node = null; + } + + // If all fonts have been loaded + if(loadedFonts >= fonts.length) { + if(interval) { + clearInterval(interval); + } + if(loadedFonts == fonts.length) { + callback(); + return true; + } + } + }; + + if(!checkFont()) { + interval = setInterval(checkFont, 50); + } + })(fonts[i]); + } + } + window.waitForWebfonts = waitForWebfonts; +})(); + + diff --git a/addons/point_of_sale/static/src/js/models.js b/addons/point_of_sale/static/src/js/models.js index f41f03a4071..dfebe6b4261 100644 --- a/addons/point_of_sale/static/src/js/models.js +++ b/addons/point_of_sale/static/src/js/models.js @@ -277,6 +277,27 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal self.cashregisters = bankstatements; }, },{ + label: 'fonts', + loaded: function(self){ + var fonts_loaded = new $.Deferred(); + + // Waiting for fonts to be loaded to prevent receipt printing + // from printing empty receipt while loading Inconsolata + // ( The font used for the receipt ) + waitForWebfonts(['Lato','Inconsolata'], function(){ + fonts_loaded.resolve(); + }); + + // The JS used to detect font loading is not 100% robust, so + // do not wait more than 5sec + setTimeout(function(){ + fonts_loaded.resolve(); + },5000); + + return fonts_loaded; + }, + },{ + label: 'pictures', loaded: function(self){ self.company_logo = new Image(); self.company_logo.crossOrigin = 'anonymous'; @@ -327,7 +348,7 @@ function openerp_pos_models(instance, module){ //module is instance.point_of_sal loaded.resolve(); }else{ var model = self.models[index]; - self.pos_widget.loading_message(_t('Loading')+' '+(model.model || ''), progress); + self.pos_widget.loading_message(_t('Loading')+' '+(model.label || model.model || ''), progress); var fields = typeof model.fields === 'function' ? model.fields(self,tmp) : model.fields; var domain = typeof model.domain === 'function' ? model.domain(self,tmp) : model.domain; var context = typeof model.context === 'function' ? model.context(self,tmp) : model.context; diff --git a/addons/point_of_sale/views/point_of_sale.xml b/addons/point_of_sale/views/point_of_sale.xml index ae76f4b4bb3..9e52ffe582b 100644 --- a/addons/point_of_sale/views/point_of_sale.xml +++ b/addons/point_of_sale/views/point_of_sale.xml @@ -6,6 +6,7 @@