|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Yatta and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Yatta - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.draw2d.internal; |
| 14 | + |
| 15 | +import java.util.Objects; |
| 16 | + |
| 17 | +import org.eclipse.swt.graphics.Font; |
| 18 | +import org.eclipse.swt.graphics.FontMetrics; |
| 19 | +import org.eclipse.swt.graphics.GC; |
| 20 | +import org.eclipse.swt.widgets.Control; |
| 21 | + |
| 22 | +import org.eclipse.draw2d.geometry.Dimension; |
| 23 | + |
| 24 | +/** |
| 25 | + * Provides miscellaneous Figure operations calculated with the zoom context of |
| 26 | + * the the provided {@code Drawable}. |
| 27 | + * |
| 28 | + * All GC related operations are mirrored from {@code FigureUtilities} |
| 29 | + */ |
| 30 | +public class DrawableFigureUtilities { |
| 31 | + private final GC gc; |
| 32 | + private Font appliedFont; |
| 33 | + private FontMetrics metrics; |
| 34 | + |
| 35 | + public DrawableFigureUtilities(Control source) { |
| 36 | + gc = new GC(source); |
| 37 | + source.addDisposeListener(e -> { |
| 38 | + gc.dispose(); |
| 39 | + }); |
| 40 | + appliedFont = gc.getFont(); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Returns the FontMetrics associated with the passed Font. |
| 45 | + * |
| 46 | + * @param f the font |
| 47 | + * @return the FontMetrics for the given font |
| 48 | + * @see GC#getFontMetrics() |
| 49 | + */ |
| 50 | + public FontMetrics getFontMetrics(Font f) { |
| 51 | + setFont(f); |
| 52 | + if (metrics == null) { |
| 53 | + metrics = gc.getFontMetrics(); |
| 54 | + } |
| 55 | + return metrics; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Returns the dimensions of the String <i>s</i> using the font <i>f</i>. Tab |
| 60 | + * expansion and carriage return processing are performed. |
| 61 | + * |
| 62 | + * @param s the string |
| 63 | + * @param f the font |
| 64 | + * @return the text's dimensions |
| 65 | + * @see GC#textExtent(String) |
| 66 | + */ |
| 67 | + protected org.eclipse.swt.graphics.Point getTextDimension(String s, Font f) { |
| 68 | + setFont(f); |
| 69 | + return gc.textExtent(s); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Returns the dimensions of the String <i>s</i> using the font <i>f</i>. No tab |
| 74 | + * expansion or carriage return processing will be performed. |
| 75 | + * |
| 76 | + * @param s the string |
| 77 | + * @param f the font |
| 78 | + * @return the string's dimensions |
| 79 | + * @see GC#stringExtent(java.lang.String) |
| 80 | + */ |
| 81 | + protected org.eclipse.swt.graphics.Point getStringDimension(String s, Font f) { |
| 82 | + setFont(f); |
| 83 | + return gc.stringExtent(s); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Returns the Dimensions of the given text, converting newlines and tabs |
| 88 | + * appropriately. |
| 89 | + * |
| 90 | + * @param text the text |
| 91 | + * @param f the font |
| 92 | + * @return the dimensions of the given text |
| 93 | + */ |
| 94 | + public Dimension getTextExtents(String text, Font f) { |
| 95 | + return new Dimension(getTextDimension(text, f)); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns the Dimensions of <i>s</i> in Font <i>f</i>. |
| 100 | + * |
| 101 | + * @param s the string |
| 102 | + * @param f the font |
| 103 | + * @return the dimensions of the given string |
| 104 | + */ |
| 105 | + public Dimension getStringExtents(String s, Font f) { |
| 106 | + return new Dimension(getStringDimension(s, f)); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Returns the Dimensions of the given text, converting newlines and tabs |
| 111 | + * appropriately. |
| 112 | + * |
| 113 | + * @param s the string |
| 114 | + * @param f the font |
| 115 | + * @param result the Dimension that will contain the result of this calculation |
| 116 | + */ |
| 117 | + public void getTextExtents(String s, Font f, Dimension result) { |
| 118 | + org.eclipse.swt.graphics.Point pt = getTextDimension(s, f); |
| 119 | + result.width = pt.x; |
| 120 | + result.height = pt.y; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Returns the width of <i>s</i> in Font <i>f</i>. |
| 125 | + * |
| 126 | + * @param s the string |
| 127 | + * @param f the font |
| 128 | + * @return the width |
| 129 | + */ |
| 130 | + public int getTextWidth(String s, Font f) { |
| 131 | + return getTextDimension(s, f).x; |
| 132 | + } |
| 133 | + |
| 134 | + private void setFont(Font f) { |
| 135 | + if (Objects.equals(appliedFont, f)) { |
| 136 | + return; |
| 137 | + } |
| 138 | + gc.setFont(f); |
| 139 | + appliedFont = f; |
| 140 | + metrics = null; |
| 141 | + } |
| 142 | +} |
0 commit comments