changeset 1654:39137da83b0b

fix print.js, no need for ua-parser.js anymore
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 17 Nov 2020 09:32:40 +0100
parents 2e3b2ed239b9
children 092af2150f67
files Applications/StoneWebViewer/Resources/Graveyard/print-2.js Applications/StoneWebViewer/WebApplication/index.html Applications/StoneWebViewer/WebApplication/print.js Applications/StoneWebViewer/WebAssembly/StoneWebViewer.cpp
diffstat 4 files changed, 108 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Applications/StoneWebViewer/Resources/Graveyard/print-2.js	Tue Nov 17 09:32:40 2020 +0100
@@ -0,0 +1,92 @@
+/**
+ * Stone of Orthanc
+ * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
+ * Department, University Hospital of Liege, Belgium
+ * Copyright (C) 2017-2020 Osimis S.A., Belgium
+ *
+ * This program is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU Affero General Public License
+ * as published by the Free Software Foundation, either version 3 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+
+function beforePrint(event) {
+  var body = $('body');
+  body.addClass('print');
+
+  // because firefox does not support/executes codes after the cloned document as been rendered
+  // https://bugzilla.mozilla.org/show_bug.cgi?format=default&id=1048317
+  // we cannot calculate using the good body size for the clone document
+  // so we have to hardcode the body width (meaning we can only renders in A4 in firefox);
+  var uaParser = new UAParser();
+  var isFirefox = (uaParser.getBrowser().name === 'Firefox');
+  var isIE = (uaParser.getBrowser().name === 'IE');
+  var isEdge = (uaParser.getBrowser().name === 'Edge');
+  //console.log('ua parser', uaParser.getBrowser());
+  
+  if (isFirefox || isIE || isEdge) {
+    if (0) {
+      // This is Letter
+      body.css('width', '8.5in');
+      body.css('height', '11in');
+    } else {
+      // This is A4
+      body.css('width', '210mm');
+      body.css('height', '296mm');  // If using "297mm", Firefox creates a second blank page
+    }
+  }
+
+  $('#viewport canvas').each(function(key, canvas) {
+    if ($(canvas).is(':visible')) {
+      $(canvas).width($(canvas).parent().width());
+      $(canvas).height($(canvas).parent().height());
+    }
+  });
+
+  stone.FitForPrint();
+};
+
+
+function afterPrint() {
+  var body = $('body');
+  body.removeClass('print');
+  body.css('width', '100%');
+  body.css('height', '100%');
+  $('#viewport canvas').css('width', '100%');
+  $('#viewport canvas').css('height', '100%');
+  
+  stone.FitForPrint();
+}
+
+
+window.addEventListener('beforeprint', function(event) {
+  beforePrint(event);
+});
+
+
+window.matchMedia('print').addListener(function(mql) {
+  if (mql.matches) {
+    // webkit equivalent of onbeforeprint
+    beforePrint();
+  }
+});
+
+
+window.addEventListener('afterprint', function() {
+  afterPrint();
+});
+
+
+/*vm.cancelPrintMode = function() {
+  afterPrint();
+}
+*/
--- a/Applications/StoneWebViewer/WebApplication/index.html	Mon Nov 16 22:17:01 2020 +0100
+++ b/Applications/StoneWebViewer/WebApplication/index.html	Tue Nov 17 09:32:40 2020 +0100
@@ -616,7 +616,8 @@
         <div v-bind:id="prefix + '-container'"
              style="position: absolute; left: 0; top: 0; width:100%;height:100%;overflow:auto;line-height: 0px;">
           <canvas v-bind:id="prefix + '-canvas'"
-                  style="position: absolute; top:0px; left:0px;"></canvas>
+                  style="position: absolute; top:0px; left:0px;"
+                  oncontextmenu="return false"></canvas>
         </div>
 
         <div class="wv-overlay">
@@ -645,8 +646,6 @@
     <script src="js/axios.min.js"></script>
     <script src="js/pdf.js"></script>
     
-    <script src="ua-parser.js"></script>
-    
     <script src="stone.js"></script>
     <script src="pdf-viewer.js"></script>   <!-- Must be before inclusion of "app.js" -->
     <script src="app.js"></script>
--- a/Applications/StoneWebViewer/WebApplication/print.js	Mon Nov 16 22:17:01 2020 +0100
+++ b/Applications/StoneWebViewer/WebApplication/print.js	Tue Nov 17 09:32:40 2020 +0100
@@ -23,32 +23,23 @@
   var body = $('body');
   body.addClass('print');
 
-  // because firefox does not support/executes codes after the cloned document as been rendered
-  // https://bugzilla.mozilla.org/show_bug.cgi?format=default&id=1048317
-  // we cannot calculate using the good body size for the clone document
-  // so we have to hardcode the body width (meaning we can only renders in A4 in firefox);
-  var uaParser = new UAParser();
-  var isFirefox = (uaParser.getBrowser().name === 'Firefox');
-  var isIE = (uaParser.getBrowser().name === 'IE');
-  var isEdge = (uaParser.getBrowser().name === 'Edge');
-  //console.log('ua parser', uaParser.getBrowser());
-  
-  if (isFirefox || isIE || isEdge) {
-    if (0) {
-      // This is Letter
-      body.css('width', '8.5in');
-      body.css('height', '11in');
-    } else {
-      // This is A4
-      body.css('width', '210mm');
-      body.css('height', '296mm');  // If using "297mm", Firefox creates a second blank page
-    }
+  if (0) {
+    // This is Letter
+    body.css('width', '8.5in');
+    body.css('height', '11in');
+  } else {
+    // This is A4
+    body.css('width', '210mm');
+    body.css('height', '296mm');  // If using "297mm", Firefox creates a second blank page
   }
 
+  // https://webglfundamentals.org/webgl/lessons/webgl-resizing-the-canvas.html
+  var realToCSSPixels = window.devicePixelRatio;
+
   $('#viewport canvas').each(function(key, canvas) {
     if ($(canvas).is(':visible')) {
-      $(canvas).width($(canvas).parent().width());
-      $(canvas).height($(canvas).parent().height());
+      $(canvas).width(Math.floor(realToCSSPixels * $(canvas).get(0).clientWidth));
+      $(canvas).height(Math.floor(realToCSSPixels * $(canvas).get(0).clientHeight));
     }
   });
 
@@ -73,8 +64,7 @@
 });
 
 
-var printMedia = window.matchMedia('print');
-printMedia.addListener(function(mql) {
+window.matchMedia('print').addListener(function(mql) {
   if (mql.matches) {
     // webkit equivalent of onbeforeprint
     beforePrint();
--- a/Applications/StoneWebViewer/WebAssembly/StoneWebViewer.cpp	Mon Nov 16 22:17:01 2020 +0100
+++ b/Applications/StoneWebViewer/WebAssembly/StoneWebViewer.cpp	Tue Nov 17 09:32:40 2020 +0100
@@ -2117,7 +2117,6 @@
     //static const double MAX_DISTANCE = 0.5;   // 0.5 cm => TODO parameter?
     static const double MAX_DISTANCE = std::numeric_limits<double>::infinity();
 
-    OrthancStone::LinearAlgebra::Print(p);
     size_t frameIndex;
     if (cursor_.get() != NULL &&
         frames_.get() != NULL &&