All files / src/app/map/printer printer.service.ts

50% Statements 29/58
100% Branches 0/0
50% Functions 2/4
50% Lines 28/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194                        1x                     11x             1x 11x 11x   11x 11x 11x                                                                                                                     11x   11x   11x 11x 11x 11x 11x 11x   11x 11x 11x 11x 11x 11x   11x 11x 11x 11x                     1x                                                                                                                                 1x  
import { Injectable } from '@angular/core';
 
import {
  printHeightMm,
  printWidthMm,
  mmToInchMultiplier,
  dpi
} from '../map-defaults';
 
@Injectable({
  providedIn: 'root'
})
export class PrinterService {
 
  iFrameElement: HTMLIFrameElement;
  imgElement: HTMLImageElement;
  titleElement: HTMLParagraphElement;
  idElement: HTMLParagraphElement;
  brailleTitleElement: HTMLParagraphElement;
  brailleIdElement: HTMLParagraphElement;
 
  constructor() {
    if (window.navigator.userAgent.toLowerCase().includes('chrome')) {
      this.initPrintFrame();
    }
  }
 
  /**
   * Initialize the frame for printing the map.
   */
  private initPrintFrame() {
    this.iFrameElement = document.createElement('iframe');
    document.body.appendChild(this.iFrameElement);
 
    const style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = `
    @page {
      size: landscape A3;
      margin-left: 28mm;
      margin-right: 28mm;
      margin-top: 10mm;
      margin-bottom: 0;
    }
 
    @font-face {
      font-family: 'braille-custom';
      src: url('/assets/braille.ttf');
    }
 
    html, body {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
      text-align: center;
    }
 
    img {
      border: 2mm solid black;
      margin: 0;
      padding: 0;
    }
 
    #text-container {
      margin: 2mm 0 0;
      padding: 0;
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
 
    #text-container p {
      margin: 0;
      padding: 0;
      font-size: 10mm;
      font-family: Arial, Helvetica, sans-serif;
    }
 
    #braille-container {
      margin: 2mm 0 0;
      padding: 0;
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
 
    #braille-container p {
      margin: 0;
      padding: 0;
      font-size: 10mm;
      font-family: 'braille-custom';
    }`;
 
    if (this.iFrameElement.contentDocument) {
      this.iFrameElement.contentDocument.head.appendChild(style);
 
      this.imgElement = document.createElement('img');
 
      const textContainer = document.createElement('div');
      textContainer.id = 'text-container';
      this.titleElement = document.createElement('p');
      this.idElement = document.createElement('p');
      textContainer.appendChild(this.titleElement);
      textContainer.appendChild(this.idElement);
 
      const brailleContainer = document.createElement('div');
      brailleContainer.id = 'braille-container';
      this.brailleTitleElement = document.createElement('p');
      this.brailleIdElement = document.createElement('p');
      brailleContainer.appendChild(this.brailleTitleElement);
      brailleContainer.appendChild(this.brailleIdElement);
 
      const body = this.iFrameElement.contentDocument.body;
      body.appendChild(this.imgElement);
      body.appendChild(textContainer);
      body.appendChild(brailleContainer);
    }
  }
 
  /**
   * Print the map inside of a MapView with a title and id.
   *
   * @param id The id of the map to print.
   * @param title The title of the map to print.
   * @param mapView The MapView containing the map to print.
   */
  async printMap(
    id: string,
    title: string,
    mapView: __esri.MapView
  ) {
    // Compute the ideal width and height for the map to print.
    const desiredRatio = (mapView.extent.xmax - mapView.extent.xmin) / (mapView.extent.ymax - mapView.extent.ymin);
 
    const containerWidth = printWidthMm * mmToInchMultiplier * dpi;
    const containerHeight = printHeightMm * mmToInchMultiplier * dpi;
    const containerAspectRatio = containerWidth / containerHeight;
 
    let height: number;
    let width: number;
 
    if (desiredRatio < containerAspectRatio) {
      height = containerHeight;
      width = height * desiredRatio;
    } else {
      width = containerWidth;
      height = width / desiredRatio;
    }
 
    const screenshot = await mapView.takeScreenshot({
      height: height,
      width: width,
    });
    const imgUrl = screenshot.dataUrl;
 
    if (window.navigator.userAgent.toLowerCase().includes('chrome')) {
      this.titleElement.innerText = title;
      this.idElement.innerText = id;
      this.brailleTitleElement.innerText = title;
      this.brailleIdElement.innerText = id;
 
      const imageLoadPromise = new Promise(
        (resolve) => {
          this.imgElement.addEventListener('load', resolve);
          this.imgElement.src = imgUrl;
        }
      );
      await imageLoadPromise;
 
      setTimeout(() => {
        if (this.iFrameElement.contentWindow) {
          this.iFrameElement.contentWindow.print();
        }
      }, 100);
 
    } else {
      // In Firefox, the IFrame for printing must be initialised at the moment of
      // printing only (otherwise it'll be empty).
      this.initPrintFrame();
      this.titleElement.innerText = title;
      this.idElement.innerText = id;
      this.brailleTitleElement.innerText = title;
      this.brailleIdElement.innerText = id;
 
      this.imgElement.src = imgUrl;
 
      if (this.iFrameElement.contentWindow) {
        this.iFrameElement.contentWindow.print();
      }
    }
  }
}