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 1/3] 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); + }); + } + } + }); } } From 77aad694caa9df3c4f76132664653147ceadd52c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bl=C3=A4sta=20Johnny?= Date: Wed, 2 Nov 2022 16:37:31 +0100 Subject: [PATCH 2/3] Check for empty style --- src/controls/print/print-resize.js | 66 ++++++++++++++++-------------- 1 file changed, 35 insertions(+), 31 deletions(-) diff --git a/src/controls/print/print-resize.js b/src/controls/print/print-resize.js index a25254d6b..a7ae9f194 100644 --- a/src/controls/print/print-resize.js +++ b/src/controls/print/print-resize.js @@ -46,44 +46,48 @@ export default function PrintResize(options = {}) { // 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); + if (!Array.isArray(style)) { + 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); + if (!Array.isArray(style)) { + 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 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 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); + } } } }; From 01d47462ed4ab11096731cdb75416fa35bb961e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johnny=20Bl=C3=A4sta?= Date: Thu, 12 Jan 2023 16:07:41 +0100 Subject: [PATCH 3/3] Fix for saving original values --- src/controls/print/print-resize.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/controls/print/print-resize.js b/src/controls/print/print-resize.js index a7ae9f194..93c159672 100644 --- a/src/controls/print/print-resize.js +++ b/src/controls/print/print-resize.js @@ -49,19 +49,25 @@ export default function PrintResize(options = {}) { if (!Array.isArray(style)) { const image = style.getImage(); if (image) { - imageSavedScale[feature.ol_uid] = { scale: image.getScale() ? image.getScale() / image.getScale() : undefined }; + if (!(feature.ol_uid in imageSavedScale)) { + 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 }; + if (!(feature.ol_uid in strokeSavedWidth)) { + 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 }; + if (!(feature.ol_uid in textSavedScale)) { + textSavedScale[feature.ol_uid] = { scale: text.getScale() ? text.getScale() / text.getScale() : undefined }; + } const textScale = text.getScale() ? multiplyRelativeValueByFactor(text.getScale()) : styleScale; text.setScale(textScale); }