• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.fragment.ruler;
17 
18 import ohos.devtools.views.trace.fragment.graph.AbstractGraph;
19 
20 import java.awt.Graphics;
21 import java.awt.Rectangle;
22 import java.awt.geom.Rectangle2D;
23 
24 /**
25  * all of data node graph node
26  *
27  * @since 2021/4/26 14:33
28  */
29 public abstract class AbstractNode {
30     /**
31      * Left padding
32      */
33     static final int LEFT_PADDING = 5;
34 
35     /**
36      * Right padding
37      */
38     static final int RIGHT_PADDING = 5;
39 
40     /**
41      * Draw string
42      *
43      * @param graphics graphics
44      * @param rectangle rectangle
45      * @param str str
46      * @param placement placement
47      */
drawString(final Graphics graphics, final Rectangle rectangle, final String str, final AbstractGraph.Placement placement)48     public void drawString(final Graphics graphics, final Rectangle rectangle, final String str,
49         final AbstractGraph.Placement placement) {
50         if (str == null || rectangle.width < 5) {
51             return;
52         }
53         final int len = 3;
54         if (placement == Placement.CENTER) {
55             Rectangle2D bounds = graphics.getFontMetrics(graphics.getFont()).getStringBounds(str, graphics);
56             double chartWidth = bounds.getWidth() / str.length(); // The width of each character
57             // How many characters can be displayed in the rectangle
58             double chartNum = (rectangle.getWidth() - LEFT_PADDING - RIGHT_PADDING) / chartWidth;
59             int mY = (int) (rectangle.getY() + bounds.getHeight());
60             // If the width is enough to display, the display text is in the middle of the rectangle
61             if (chartNum >= str.length()) {
62                 graphics.drawString(str, (int) (rectangle.getX() + (rectangle.width - bounds.getWidth()) / 2), mY);
63             } else if (chartNum >= len + 1) {
64                 // If the width is not enough to display, cut out the part that can be displayed with an ellipsis behind
65                 graphics
66                     .drawString(str.substring(0, (int) chartNum - len) + "...", (int) (rectangle.getX() + LEFT_PADDING),
67                         mY);
68             } else if (chartNum > 1 && chartNum < len) { // If only one character can be displayed
69                 graphics.drawString(str.substring(0, 1), (int) (rectangle.getX() + LEFT_PADDING), mY);
70             } else {
71                 graphics.drawString("", (int) (rectangle.getX() + LEFT_PADDING), mY);
72             }
73         }
74         if (placement == Placement.CENTER_LINE) {
75             Rectangle2D bounds = graphics.getFontMetrics(graphics.getFont()).getStringBounds(str, graphics);
76             double chartWidth = bounds.getWidth() / str.length(); // The width of each character
77             // How many characters can be displayed in the rectangle
78             double chartNum = (rectangle.width - LEFT_PADDING - RIGHT_PADDING) / chartWidth;
79             int mY = (int) (rectangle.getY() + rectangle.height / 2 + bounds.getHeight() / 2);
80             if (chartNum >= str.length()) {
81                 // If the width is enough to display, the display text is in the middle of the rectangle
82                 graphics.drawString(str, (int) (rectangle.getX() + (rectangle.width - bounds.getWidth()) / 2), mY);
83             } else if (chartNum >= len + 1) {
84                 graphics
85                     .drawString(str.substring(0, (int) chartNum - len) + "...", (int) (rectangle.getX() + LEFT_PADDING),
86                         mY);
87             } else if (chartNum > 1 && chartNum < len) { // If only one character can be displayed
88                 graphics.drawString(str.substring(0, 1), (int) (rectangle.getX() + LEFT_PADDING), mY);
89             } else {
90                 graphics.drawString("", (int) (rectangle.getX() + LEFT_PADDING), mY);
91             }
92         }
93     }
94 
95     /**
96      * Direction Enum
97      */
98     public enum Placement {
99         /**
100          * center
101          */
102         CENTER,
103         /**
104          * Multi-line
105          */
106         MULTILINE,
107         /**
108          * Middle row
109          */
110         CENTER_LINE
111     }
112 }
113