• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (C) 2019 The Android Open Source Project
2//
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
15export function cropText(str: string, charWidth: number, rectWidth: number) {
16  const maxTextWidth = rectWidth - 4;
17  let displayText = '';
18  const nameLength = str.length * charWidth;
19  if (nameLength < maxTextWidth) {
20    displayText = str;
21  } else {
22    // -3 for the 3 ellipsis.
23    const displayedChars = Math.floor(maxTextWidth / charWidth) - 3;
24    if (displayedChars > 3) {
25      displayText = str.substring(0, displayedChars) + '...';
26    }
27  }
28  return displayText;
29}
30
31export function drawDoubleHeadedArrow(
32    ctx: CanvasRenderingContext2D,
33    x: number,
34    y: number,
35    length: number,
36    showArrowHeads: boolean,
37    width = 2,
38    color = 'black') {
39  ctx.beginPath();
40  ctx.lineWidth = width;
41  ctx.lineCap = 'round';
42  ctx.strokeStyle = color;
43  ctx.moveTo(x, y);
44  ctx.lineTo(x + length, y);
45  ctx.stroke();
46  ctx.closePath();
47  // Arrowheads on the each end of the line.
48  if (showArrowHeads) {
49    ctx.beginPath();
50    ctx.moveTo(x + length - 8, y - 4);
51    ctx.lineTo(x + length, y);
52    ctx.lineTo(x + length - 8, y + 4);
53    ctx.stroke();
54    ctx.closePath();
55    ctx.beginPath();
56    ctx.moveTo(x + 8, y - 4);
57    ctx.lineTo(x, y);
58    ctx.lineTo(x + 8, y + 4);
59    ctx.stroke();
60    ctx.closePath();
61  }
62}