commit e81ef8983b1cfe20e1136e1e5129da497edea69b
parent 3fb738bce9d3c37917e9876596005e91cb0d44ea
Author: Sergej Orlov <wladimirych@gmail.com>
Date: Sun, 19 Mar 2017 02:13:47 +0300
[print: scale overlay] print scale as min-max range if scale differs on nortrhern and southern edges
Diffstat:
1 file changed, 41 insertions(+), 7 deletions(-)
diff --git a/src/lib/leaflet.control.printPages/decoration.scale.js b/src/lib/leaflet.control.printPages/decoration.scale.js
@@ -1,5 +1,45 @@
import {PrintStaticLayer} from './decorations';
+
+function pageScaleRange(printOptions) {
+ const pageSize = printOptions.destPixelSize.divideBy(printOptions.resolution / 25.4);
+ const bounds = printOptions.latLngBounds;
+ const southLen = bounds.getSouthEast().distanceTo(bounds.getSouthWest());
+ const northLen = bounds.getNorthEast().distanceTo(bounds.getNorthWest());
+ const nScale = Math.round(northLen / pageSize.x * 10);
+ const sScale = Math.round(southLen / pageSize.x * 10);
+ return {
+ min: Math.min(nScale, sScale, printOptions.scale),
+ max: Math.max(nScale, sScale, printOptions.scale)
+ }
+}
+
+function formatScale(nominalScale, scaleRange) {
+ const threshold = 0.05;
+ if (Math.abs(nominalScale - scaleRange.min) / nominalScale > threshold || Math.abs(nominalScale - scaleRange.max) / nominalScale > threshold) {
+ let unit;
+ scaleRange = Object.assign({}, scaleRange);
+ if (scaleRange.min >= 1000) {
+ scaleRange.min /= 1000;
+ scaleRange.max /= 1000;
+ unit = 'km'
+ } else {
+ unit = 'm'
+ }
+ return `${scaleRange.min} – ${scaleRange.max} ${unit} in 1 cm`;
+ } else {
+ let unit;
+ if (nominalScale >= 1000) {
+ nominalScale /= 1000;
+ unit = 'km';
+ } else {
+ unit = 'm';
+ }
+ return `${nominalScale} ${unit} in 1 cm`;
+ }
+}
+
+
class OverlayScale extends PrintStaticLayer {
fontSizeMm = 3;
font = 'verdana';
@@ -7,13 +47,7 @@ class OverlayScale extends PrintStaticLayer {
_drawRaster(canvas, printOptions) {
const ctx = canvas.getContext('2d');
- let scale = Math.round(printOptions.scale);
- let unit = 'm';
- if (scale >= 1000) {
- scale /= 1000;
- unit = 'km'
- }
- let caption = `${scale} ${unit} in 1 cm`;
+ let caption = formatScale(printOptions.scale, pageScaleRange(printOptions));
if (printOptions.pagesCount > 1) {
caption += ` | Page ${printOptions.pageLabel} / ${printOptions.pagesCount}`;
}