1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package ohos.devtools.views.trace; 17 18 import ohos.devtools.views.trace.util.Utils; 19 20 import java.awt.AlphaComposite; 21 import java.awt.Graphics; 22 import java.awt.Graphics2D; 23 import java.awt.Rectangle; 24 import java.awt.geom.Rectangle2D; 25 26 import static ohos.devtools.views.trace.TracePanel.DURATION; 27 import static ohos.devtools.views.trace.TracePanel.endNS; 28 import static ohos.devtools.views.trace.TracePanel.startNS; 29 30 /** 31 * Common util 32 * 33 * @since 2021/5/13 16:40 34 */ 35 public class Common { 36 private static final int LEN = 3; 37 private static final int LEFT_PADDING = 2; 38 private static final int RIGHT_PADDING = 2; 39 40 /** 41 * Get the string Rect 42 * 43 * @param str str 44 * @param graphics graphics 45 * @return int 46 */ getStringRect(Graphics graphics, String str)47 public static Rectangle2D getStringRect(Graphics graphics, String str) { 48 return graphics.getFontMetrics(graphics.getFont()).getStringBounds(str, graphics); 49 } 50 51 /** 52 * x coordinate to time (unit ns) 53 * 54 * @param coordX coordX 55 * @param rect rect 56 * @return time 57 */ x2ns(final int coordX, Rectangle rect)58 public static long x2ns(final int coordX, Rectangle rect) { 59 return x2ns(coordX, rect, DURATION); 60 } 61 62 /** 63 * x coordinate to time (unit ns) 64 * 65 * @param coordX coordX 66 * @param rect rect 67 * @param duration duration 68 * @return long time 69 */ x2ns(final int coordX, Rectangle rect, long duration)70 public static long x2ns(final int coordX, Rectangle rect, long duration) { 71 return (long) ((coordX - rect.getX()) * duration / (rect.getWidth() - Utils.getX(rect))); 72 } 73 74 /** 75 * time to x coordinate 76 * 77 * @param ns ns 78 * @param rect rect 79 * @return x coordinate 80 */ ns2x(long ns, Rectangle rect)81 public static double ns2x(long ns, Rectangle rect) { 82 return ns2x(ns, rect, DURATION); 83 } 84 85 /** 86 * time to x coordinate 87 * 88 * @param ns ns 89 * @param rect rect 90 * @param duration duration 91 * @return x coordinate 92 */ ns2x(long ns, Rectangle rect, long duration)93 public static double ns2x(long ns, Rectangle rect, long duration) { 94 if (endNS == 0) { 95 endNS = duration; 96 } 97 double xSize = (ns - startNS) * rect.getWidth() / (endNS - startNS); 98 if (xSize < 0) { 99 xSize = 0; 100 } 101 if (xSize > rect.getWidth()) { 102 xSize = rect.getWidth(); 103 } 104 return xSize; 105 } 106 107 /** 108 * time to x coordinate by duration 109 * 110 * @param ns ns 111 * @param rect rect 112 * @param duration duration 113 * @return x coordinate 114 */ nsToXByDur(long ns, Rectangle rect, long duration)115 public static double nsToXByDur(long ns, Rectangle rect, long duration) { 116 double xSize = ns * rect.getWidth() / duration; 117 if (xSize < 0) { 118 xSize = 0; 119 } 120 if (xSize > rect.getWidth()) { 121 xSize = rect.getWidth(); 122 } 123 return xSize; 124 } 125 126 /** 127 * set alpha 128 * 129 * @param g2 g2 130 * @param alpha alpha 131 */ setAlpha(Graphics2D g2, float alpha)132 public static void setAlpha(Graphics2D g2, float alpha) { 133 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); 134 } 135 136 /** 137 * draw String in the Center 138 * 139 * @param graphics graphics 140 * @param str str 141 * @param rectangle rectangle 142 */ drawStringCenter(Graphics2D graphics, String str, final Rectangle rectangle)143 public static void drawStringCenter(Graphics2D graphics, String str, final Rectangle rectangle) { 144 Rectangle2D minBound = 145 graphics.getFontMetrics(graphics.getFont()).getStringBounds("m...", graphics); // show string min rect 146 if (rectangle.width < minBound.getWidth() || rectangle.height < minBound.getHeight()) { 147 return; 148 } 149 Rectangle2D bounds = graphics.getFontMetrics(graphics.getFont()).getStringBounds(str, graphics); 150 double chartWidth = bounds.getWidth() / str.length(); // The width of each character 151 152 // How many characters can be displayed in the rectangle 153 double chartNum = (rectangle.width - LEFT_PADDING - RIGHT_PADDING) / chartWidth; 154 int mY = (int) (rectangle.getY() + rectangle.height / 2 + bounds.getHeight() / 2); 155 if (chartNum >= str.length()) { 156 graphics.drawString(str, (int) (rectangle.getX() + (rectangle.width - bounds.getWidth()) / 2), mY); 157 } else if (chartNum >= LEN + 1) { 158 graphics.drawString(str.substring(0, (int) chartNum - LEN) + "...", (int) (rectangle.getX() + LEFT_PADDING), 159 mY); 160 } else if (chartNum > 1 && chartNum < LEN) { // If only one character can be displayed 161 graphics.drawString(str.substring(0, 1), (int) (rectangle.getX() + LEFT_PADDING), mY); 162 } else { 163 graphics.drawString("", (int) (rectangle.getX() + LEFT_PADDING), mY); 164 } 165 } 166 167 /** 168 * Draw strings centered vertically and horizontally, 169 * using this method when graphics are passed in from a single component. 170 * 171 * @param graphics graphics 172 * @param str str 173 * @param rectangle rectangle 174 */ drawStringVHCenter(Graphics2D graphics, String str, final Rectangle rectangle)175 public static void drawStringVHCenter(Graphics2D graphics, String str, final Rectangle rectangle) { 176 if (rectangle.width < 5 || rectangle.height < 5) { 177 return; 178 } 179 Rectangle2D bounds = graphics.getFontMetrics(graphics.getFont()).getStringBounds(str, graphics); 180 double chartWidth = bounds.getWidth() / str.length(); // The width of each character 181 // How many characters can be displayed in the rectangle 182 double chartNum = (rectangle.width - LEFT_PADDING - RIGHT_PADDING) / chartWidth; 183 int mY = (int) (rectangle.height / 2 + bounds.getHeight() / 2); 184 if (chartNum >= str.length()) { 185 graphics.drawString(str, (int) (rectangle.getX() + (rectangle.width - bounds.getWidth()) / 2), mY); 186 } else if (chartNum >= LEN + 1) { 187 graphics.drawString(str.substring(0, (int) chartNum - LEN) + "...", (int) (rectangle.getX() + LEFT_PADDING), 188 mY); 189 } else if (chartNum > 1 && chartNum < LEN) { // If only one character can be displayed 190 graphics.drawString(str.substring(0, 1), (int) (rectangle.getX() + LEFT_PADDING), mY); 191 } else { 192 graphics.drawString("", (int) (rectangle.getX() + LEFT_PADDING), mY); 193 } 194 } 195 196 /** 197 * Draw strings middle height vertically and horizontally, 198 * using this method when graphics are passed in from a single component. 199 * 200 * @param g2 g2 201 * @param str str 202 * @param rect rect 203 */ drawStringMiddleHeight(Graphics2D g2, String str, final Rectangle rect)204 public static void drawStringMiddleHeight(Graphics2D g2, String str, final Rectangle rect) { 205 Rectangle2D bounds = g2.getFontMetrics().getStringBounds(str, g2); 206 double chartWidth = bounds.getWidth() / str.length(); 207 double chartNum = (rect.width - LEFT_PADDING - RIGHT_PADDING) / chartWidth; 208 if (chartNum >= str.length()) { 209 g2.drawString(str, Utils.getX(rect), 210 (float) (Utils.getY(rect) + rect.height / 2 + bounds.getHeight() / 2 - 3)); 211 } else { 212 if (chartNum >= LEN + 1) { 213 g2.drawString(str.substring(0, (int) chartNum - LEN) + "...", Utils.getX(rect), 214 (float) (Utils.getY(rect) + rect.height / 2 + bounds.getHeight() / 2 - 3)); 215 } else if (chartNum > 1 && chartNum < LEN) { 216 g2.drawString(str.substring(0, 1), Utils.getX(rect), 217 (float) (Utils.getY(rect) + rect.height / 2 + bounds.getHeight() / 2 - 3)); 218 } else { 219 int yValue = Utils.getY(rect); 220 } 221 } 222 } 223 } 224