|
| 1 | +package com.labelzoom.api.util; |
| 2 | + |
| 3 | +/* Copyright (C) 2021, LabelZoom, a subsidiary of RJF Technology Solutions, Inc. |
| 4 | + * All rights reserved. Proprietary and confidential. |
| 5 | + * |
| 6 | + * This file is subject to the license terms found at |
| 7 | + * https://www.labelzoom.net/end-user-license-agreement/ |
| 8 | + * |
| 9 | + * The methods and techniques described herein are considered |
| 10 | + * confidential and/or trade secrets. |
| 11 | + * No part of this file may be copied, modified, propagated, |
| 12 | + * or distributed except as authorized by the license. |
| 13 | + */ |
| 14 | + |
| 15 | +import com.labelzoom.api.model.components.*; |
| 16 | +import com.labelzoom.api.model.components.barcodes.ABarcode; |
| 17 | +import com.labelzoom.api.model.components.barcodes.linear.ALinearBarcode; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +public class DpiUtility |
| 22 | +{ |
| 23 | + public static final float POINTS_TO_INCHES = 1.0f / 72.0f; // 1 pt = 1/72 inch |
| 24 | + public static final int INCHES_TO_POINTS = 72; |
| 25 | + |
| 26 | + private final double scalingFactor; |
| 27 | + private final double fontScalingFactor; |
| 28 | + |
| 29 | + public DpiUtility(final int sourceDpi, final int targetDpi, final int sourceFontDpi, final int targetFontDpi) |
| 30 | + { |
| 31 | + this.scalingFactor = (double)targetDpi / (double)sourceDpi; |
| 32 | + this.fontScalingFactor = (double)targetFontDpi / (double)sourceFontDpi; |
| 33 | + } |
| 34 | + |
| 35 | + private float applyResize(final float sourceValue) |
| 36 | + { |
| 37 | + return (float)(((double)sourceValue) * scalingFactor); |
| 38 | + } |
| 39 | + |
| 40 | + private int applyResize(final int sourceValue) |
| 41 | + { |
| 42 | + return Math.round(applyResize((float)sourceValue)); |
| 43 | + } |
| 44 | + |
| 45 | + private float applyFontResize(final float sourceValue) |
| 46 | + { |
| 47 | + return (float)(((double)sourceValue) * fontScalingFactor); |
| 48 | + } |
| 49 | + |
| 50 | + public CLabel resizeLabel(final CLabel label) |
| 51 | + { |
| 52 | + final CLabel clone = label.clone(true); |
| 53 | + clone.setHeight(applyResize(label.getHeight())); |
| 54 | + clone.setWidth(applyResize(label.getWidth())); |
| 55 | + shiftElements(clone.getElements()); |
| 56 | + return clone; |
| 57 | + } |
| 58 | + |
| 59 | + private void shiftElements(final List<AComponent> elements) |
| 60 | + { |
| 61 | + for (final AComponent component : elements) |
| 62 | + { |
| 63 | + component.setTop(applyResize(component.getTop())); |
| 64 | + component.setLeft(applyResize(component.getLeft())); |
| 65 | + if (component instanceof AFontComponent) |
| 66 | + { |
| 67 | + final AFontComponent fontComponent = (AFontComponent)component; |
| 68 | + fontComponent.setFontSize(applyFontResize(fontComponent.getFontSize())); |
| 69 | + if (fontComponent instanceof CVariableText) |
| 70 | + { |
| 71 | + final CVariableText variableText = (CVariableText) fontComponent; |
| 72 | + if (!variableText.isAutoSize()) |
| 73 | + { |
| 74 | + variableText.setWidth(applyResize(variableText.getWidth())); |
| 75 | + variableText.setHeight(applyResize(variableText.getHeight())); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + else if (component instanceof ABarcode) |
| 80 | + { |
| 81 | + if (component instanceof ALinearBarcode) |
| 82 | + { |
| 83 | + final ALinearBarcode linearBarcode = (ALinearBarcode)component; |
| 84 | + linearBarcode.setHeight(applyResize(linearBarcode.getHeight())); |
| 85 | + } |
| 86 | + } |
| 87 | + else if (component instanceof CLine) |
| 88 | + { |
| 89 | + final CLine line = (CLine)component; |
| 90 | + line.setLength(applyResize(line.getLength())); |
| 91 | + line.setThickness(Math.max(applyResize(line.getThickness()), 1)); // We don't ever want thickness to be less than 1, otherwise it won't show up in the design tab |
| 92 | + } |
| 93 | + else if (component instanceof CRectangle) |
| 94 | + { |
| 95 | + final CRectangle rectangle = (CRectangle) component; |
| 96 | + rectangle.setWidth(applyResize(rectangle.getWidth())); |
| 97 | + rectangle.setHeight(applyResize(rectangle.getHeight())); |
| 98 | + rectangle.setThickness(applyResize(rectangle.getThickness())); |
| 99 | + } |
| 100 | + else if (component instanceof CImage) |
| 101 | + { |
| 102 | + final CImage image = (CImage)component; |
| 103 | + image.setHorizontalScaling(applyResize(image.getHorizontalScaling())); |
| 104 | + image.setVerticalScaling(applyResize(image.getVerticalScaling())); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments