Skip to content

Commit

Permalink
fix: reset features style after print control
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyblasta committed Oct 28, 2022
1 parent 71afbab commit 64a47d5
Showing 1 changed file with 75 additions and 18 deletions.
93 changes: 75 additions & 18 deletions src/controls/print/print-resize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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));
};
Expand Down Expand Up @@ -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 };
}
}

Expand All @@ -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);
});
}
}
});
}
}

Expand Down

0 comments on commit 64a47d5

Please sign in to comment.