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.distributed.util; 17 18 import ohos.devtools.datasources.utils.profilerlog.ProfilerLogManager; 19 import ohos.devtools.views.distributed.component.DistributedTracePanel; 20 import ohos.devtools.views.trace.util.Utils; 21 import org.apache.log4j.LogManager; 22 import org.apache.log4j.Logger; 23 24 import java.awt.AlphaComposite; 25 import java.awt.Graphics; 26 import java.awt.Graphics2D; 27 import java.awt.Rectangle; 28 import java.awt.geom.Rectangle2D; 29 30 import static ohos.devtools.views.distributed.component.DistributedTracePanel.endNS; 31 import static ohos.devtools.views.distributed.component.DistributedTracePanel.startNS; 32 33 /** 34 * Common util 35 * 36 * @since 2021/5/13 16:40 37 */ 38 public class DistributedCommon { 39 private static final Logger LOGGER = LogManager.getLogger(DistributedCommon.class); 40 private static final int LEN = 3; 41 private static final int LEFT_PADDING = 2; 42 private static final int RIGHT_PADDING = 2; 43 44 /** 45 * Get the string Rect 46 * 47 * @param str str 48 * @param graphics graphics 49 * @return int 50 */ getStringRect(Graphics graphics, String str)51 public static Rectangle2D getStringRect(Graphics graphics, String str) { 52 return graphics.getFontMetrics(graphics.getFont()).getStringBounds(str, graphics); 53 } 54 55 /** 56 * x coordinate to time (unit ns) 57 * 58 * @param coordX coordX 59 * @param rect rect 60 * @return time 61 */ x2ns(final int coordX, Rectangle rect)62 public static long x2ns(final int coordX, Rectangle rect) { 63 return x2ns(coordX, rect, DistributedTracePanel.getDURATION()); 64 } 65 66 /** 67 * x coordinate to time (unit ns) 68 * 69 * @param coordX coordX 70 * @param rect rect 71 * @param duration duration 72 * @return long time 73 */ x2ns(final int coordX, Rectangle rect, long duration)74 public static long x2ns(final int coordX, Rectangle rect, long duration) { 75 return (long) ((coordX - rect.getX()) * duration / (rect.getWidth() - Utils.getX(rect))); 76 } 77 78 /** 79 * time to x coordinate 80 * 81 * @param ns ns 82 * @param rect rect 83 * @return x coordinate 84 */ ns2x(long ns, Rectangle rect)85 public static double ns2x(long ns, Rectangle rect) { 86 return ns2x(ns, rect, DistributedTracePanel.getDURATION()); 87 } 88 89 /** 90 * time to x coordinate 91 * 92 * @param ns ns 93 * @param rect rect 94 * @param duration duration 95 * @return x coordinate 96 */ ns2x(long ns, Rectangle rect, long duration)97 public static double ns2x(long ns, Rectangle rect, long duration) { 98 if (endNS == 0) { 99 endNS = duration; 100 } 101 double xSize = (ns - startNS) * rect.getWidth() / (endNS - startNS); 102 if (xSize < 0) { 103 xSize = 0; 104 } 105 if (xSize > rect.getWidth()) { 106 xSize = rect.getWidth(); 107 } 108 return xSize; 109 } 110 111 /** 112 * time to x coordinate by duration 113 * 114 * @param ns ns 115 * @param rect rect 116 * @param duration duration 117 * @return x coordinate 118 */ nsToXByDur(long ns, Rectangle rect, long duration)119 public static double nsToXByDur(long ns, Rectangle rect, long duration) { 120 double xSize = ns * rect.getWidth() / duration; 121 if (xSize < 0) { 122 xSize = 0; 123 } 124 if (xSize > rect.getWidth()) { 125 xSize = rect.getWidth(); 126 } 127 return xSize; 128 } 129 130 /** 131 * set alpha 132 * 133 * @param g2 g2 134 * @param alpha alpha 135 */ setAlpha(Graphics2D g2, float alpha)136 public static void setAlpha(Graphics2D g2, float alpha) { 137 g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); 138 } 139 140 /** 141 * draw String in the Center 142 * 143 * @param graphics graphics 144 * @param str str 145 * @param rectangle rectangle 146 */ drawStringCenter(Graphics2D graphics, String str, final Rectangle rectangle)147 public static void drawStringCenter(Graphics2D graphics, String str, final Rectangle rectangle) { 148 Rectangle2D minBound = 149 graphics.getFontMetrics(graphics.getFont()).getStringBounds("m...", graphics); // show string min rect 150 if (rectangle.width < minBound.getWidth() || rectangle.height < minBound.getHeight()) { 151 return; 152 } 153 Rectangle2D bounds = graphics.getFontMetrics(graphics.getFont()).getStringBounds(str, graphics); 154 double chartWidth = bounds.getWidth() / str.length(); // The width of each character 155 156 // How many characters can be displayed in the rectangle 157 double chartNum = (rectangle.width - LEFT_PADDING - RIGHT_PADDING) / chartWidth; 158 int mY = (int) (rectangle.getY() + rectangle.height / 2 + bounds.getHeight() / 2); 159 if (chartNum >= str.length()) { 160 graphics.drawString(str, (int) (rectangle.getX() + (rectangle.width - bounds.getWidth()) / 2), mY); 161 } else if (chartNum >= LEN + 1) { 162 graphics.drawString(str.substring(0, (int) chartNum - LEN) + "...", (int) (rectangle.getX() + LEFT_PADDING), 163 mY); 164 } else if (chartNum > 1 && chartNum < LEN) { // If only one character can be displayed 165 graphics.drawString(str.substring(0, 1), (int) (rectangle.getX() + LEFT_PADDING), mY); 166 } else { 167 graphics.drawString("", (int) (rectangle.getX() + LEFT_PADDING), mY); 168 } 169 } 170 171 /** 172 * Draw strings centered vertically and horizontally, 173 * using this method when graphics are passed in from a single component. 174 * 175 * @param graphics graphics 176 * @param str str 177 * @param rectangle rectangle 178 */ drawStringVHCenter(Graphics2D graphics, String str, final Rectangle rectangle)179 public static void drawStringVHCenter(Graphics2D graphics, String str, final Rectangle rectangle) { 180 if (rectangle.width < 5 || rectangle.height < 5) { 181 return; 182 } 183 Rectangle2D bounds = graphics.getFontMetrics(graphics.getFont()).getStringBounds(str, graphics); 184 double chartWidth = bounds.getWidth() / str.length(); // The width of each character 185 186 // How many characters can be displayed in the rectangle 187 double chartNum = (rectangle.width - LEFT_PADDING - RIGHT_PADDING) / chartWidth; 188 int mY = (int) (rectangle.height / 2 + bounds.getHeight() / 2); 189 if (chartNum >= str.length()) { 190 graphics.drawString(str, (int) (rectangle.getX() + (rectangle.width - bounds.getWidth()) / 2), mY); 191 } else if (chartNum >= LEN + 1) { 192 graphics.drawString(str.substring(0, (int) chartNum - LEN) + "...", (int) (rectangle.getX() + LEFT_PADDING), 193 mY); 194 } else if (chartNum > 1 && chartNum < LEN) { // If only one character can be displayed 195 graphics.drawString(str.substring(0, 1), (int) (rectangle.getX() + LEFT_PADDING), mY); 196 } else { 197 graphics.drawString("", (int) (rectangle.getX() + LEFT_PADDING), mY); 198 } 199 } 200 201 /** 202 * Draw strings middle height vertically and horizontally, 203 * using this method when graphics are passed in from a single component. 204 * 205 * @param g2 g2 206 * @param str str 207 * @param rect rect 208 */ drawStringMiddleHeight(Graphics2D g2, String str, final Rectangle rect)209 public static void drawStringMiddleHeight(Graphics2D g2, String str, final Rectangle rect) { 210 Rectangle2D bounds = g2.getFontMetrics().getStringBounds(str, g2); 211 double chartWidth = bounds.getWidth() / str.length(); 212 double chartNum = (rect.width - LEFT_PADDING - RIGHT_PADDING) / chartWidth; 213 if (chartNum >= str.length()) { 214 g2.drawString(str, Utils.getX(rect), 215 (float) (Utils.getY(rect) + rect.height / 2 + bounds.getHeight() / 2 - 3)); 216 } else if (chartNum >= LEN + 1) { 217 g2.drawString(str.substring(0, (int) chartNum - LEN) + "...", Utils.getX(rect), 218 (float) (Utils.getY(rect) + rect.height / 2 + bounds.getHeight() / 2 - 3)); 219 } else if (chartNum > 1 && chartNum < LEN) { 220 g2.drawString(str.substring(0, 1), Utils.getX(rect), 221 (float) (Utils.getY(rect) + rect.height / 2 + bounds.getHeight() / 2 - 3)); 222 } else { 223 if (ProfilerLogManager.isDebugEnabled()) { 224 LOGGER.debug("drawStringMiddleHeight not in display area"); 225 } 226 } 227 } 228 } 229