From 64a47d55ed93011f2ec18946cb031f557fcf491b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bl=C3=A4sta=20Johnny?= Date: Fri, 28 Oct 2022 15:51:04 +0200 Subject: [PATCH] fix: reset features style after print control --- src/controls/print/print-resize.js | 93 ++++++++++++++++++++++++------ 1 file changed, 75 insertions(+), 18 deletions(-) diff --git a/src/controls/print/print-resize.js b/src/controls/print/print-resize.js index 14f0e824e..a25254d6b 100644 --- a/src/controls/print/print-resize.js +++ b/src/controls/print/print-resize.js @@ -28,6 +28,10 @@ export default function PrintResize(options = {}) { let prevResolution = resolution; let isActive = false; let layersWithChangedSource = []; + let layersSaveStyle = {}; + let imageSavedScale = {}; + let strokeSavedWidth = {}; + let textSavedScale = {}; // Will become an issue if 150 dpi is no longer the "standard" dpi setting const multiplyByFactor = function multiplyByFactor(value) { @@ -40,6 +44,50 @@ export default function PrintResize(options = {}) { return value * (resolution / prevResolution); }; + // Resize features when DPI changes + const resizeFeature = function resizeFeature(style, feature, styleScale) { + const image = style.getImage(); + if (image) { + imageSavedScale[feature.ol_uid] = { scale: image.getScale() ? image.getScale() / image.getScale() : undefined }; + const imageScale = image.getScale() ? multiplyRelativeValueByFactor(image.getScale()) : styleScale; + image.setScale(imageScale); + } + const stroke = style.getStroke(); + if (stroke) { + strokeSavedWidth[feature.ol_uid] = { width: stroke.getWidth() ? stroke.getWidth() : undefined }; + const strokeWidth = stroke.getWidth() ? multiplyRelativeValueByFactor(stroke.getWidth()) : styleScale; + stroke.setWidth(strokeWidth); + } + const text = style.getText(); + if (text) { + textSavedScale[feature.ol_uid] = { scale: text.getScale() ? text.getScale() / text.getScale() : undefined }; + const textScale = text.getScale() ? multiplyRelativeValueByFactor(text.getScale()) : styleScale; + text.setScale(textScale); + } + }; + + // Reset features that was resized in DPI changes + const resetFeature = function resetFeature(style, layer, feature) { + const image = style.getImage(); + if (image) { + if (typeof layersSaveStyle[layer.get('name')].imageScale[feature.ol_uid].scale !== 'undefined') { + image.setScale(layersSaveStyle[layer.get('name')].imageScale[feature.ol_uid].scale); + } + } + const stroke = style.getStroke(); + if (stroke) { + if (typeof layersSaveStyle[layer.get('name')].strokeWidth[feature.ol_uid].width !== 'undefined') { + stroke.setWidth(layersSaveStyle[layer.get('name')].strokeWidth[feature.ol_uid].width); + } + } + const text = style.getText(); + if (text) { + if (typeof layersSaveStyle[layer.get('name')].textScale[feature.ol_uid].scale !== 'undefined') { + text.setScale(layersSaveStyle[layer.get('name')].textScale[feature.ol_uid].scale); + } + } + }; + const isValidSource = function isValidSource(source) { return (!(source instanceof OSM) && !(source instanceof XYZ) && !(source instanceof WMTS)); }; @@ -348,26 +396,17 @@ export default function PrintResize(options = {}) { const featureStyle = feature.getStyle(); if (featureStyle) { const styleScale = multiplyByFactor(1.5); - Array.from(featureStyle).forEach(style => { - const image = style.getImage(); - if (image) { - const imageScale = image.getScale() ? multiplyRelativeValueByFactor(image.getScale()) : styleScale; - image.setScale(imageScale); - } - const stroke = style.getStroke(); - if (stroke) { - const strokeWidth = stroke.getWidth() ? multiplyRelativeValueByFactor(stroke.getWidth()) : styleScale; - stroke.setWidth(strokeWidth); - } - const text = style.getText(); - if (text) { - const textScale = text.getScale() ? multiplyRelativeValueByFactor(text.getScale()) : styleScale; - text.setScale(textScale); - } - }); + if (Array.from(featureStyle).length === 0) { + resizeFeature(featureStyle, feature, styleScale); + } else { + Array.from(featureStyle).forEach(style => { + resizeFeature(style, feature); + }); + } feature.setStyle(featureStyle); } }); + layersSaveStyle[layer.get('name')] = { imageScale: imageSavedScale, strokeWidth: strokeSavedWidth, textScale: textSavedScale }; } } @@ -389,11 +428,29 @@ export default function PrintResize(options = {}) { const resetLayerScale = function resetLayerScale(layer) { const source = layer.getSource(); if (isVector(layer)) { + const features = source.getFeatures(); + let style = viewer.getStyle(layer.get('styleName')); + const clusterStyleName = layer.get('clusterStyle') ? layer.get('clusterStyle') : undefined; - style = Style.createStyle({ style: layer.get('styleName'), viewer, clusterStyleName }); + if (typeof layer.get('styleName') !== 'undefined') { + style = Style.createStyle({ style: layer.get('styleName'), viewer, clusterStyleName }); + } if (style) { layer.setStyle(style); + } else if (features) { + features.forEach(feature => { + const featureStyle = feature.getStyle(); + if (featureStyle) { + if (Array.from(featureStyle).length === 0) { + resetFeature(featureStyle, layer, feature); + } else { + Array.from(featureStyle).forEach(style => { + resetFeature(style, layer, feature); + }); + } + } + }); } }