File: vector_icon_utils.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (140 lines) | stat: -rw-r--r-- 3,608 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/vector_icon_utils.h"

#include <ostream>

#include "base/check_op.h"
#include "base/no_destructor.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"
#include "ui/gfx/vector_icon_types.h"

namespace gfx {

namespace {
const char kCanvasDimensions[] = "CANVAS_DIMENSIONS";
}  // namespace

int GetCommandArgumentCount(CommandType command) {
  switch (command) {
    case STROKE:
    case H_LINE_TO:
    case R_H_LINE_TO:
    case V_LINE_TO:
    case R_V_LINE_TO:
    case CANVAS_DIMENSIONS:
    case PATH_COLOR_ALPHA:
      return 1;

    case MOVE_TO:
    case R_MOVE_TO:
    case LINE_TO:
    case R_LINE_TO:
    case QUADRATIC_TO_SHORTHAND:
    case R_QUADRATIC_TO_SHORTHAND:
      return 2;

    case CIRCLE:
      return 3;

    case PATH_COLOR_ARGB:
    case CUBIC_TO_SHORTHAND:
    case CLIP:
    case QUADRATIC_TO:
    case R_QUADRATIC_TO:
    case OVAL:
      return 4;

    case ROUND_RECT:
      return 5;

    case CUBIC_TO:
    case R_CUBIC_TO:
      return 6;

    case ARC_TO:
    case R_ARC_TO:
      return 7;

    case FILL_RULE_NONZERO:
    case NEW_PATH:
    case PATH_MODE_CLEAR:
    case CAP_SQUARE:
    case CLOSE:
    case DISABLE_AA:
    case FLIPS_IN_RTL:
      return 0;
  }

  NOTREACHED();
}

int GetDefaultSizeOfVectorIcon(const VectorIcon& icon) {
  if (icon.is_empty())
    return -1;
  const VectorIconRep& default_rep = icon.reps.back();
  DCHECK_EQ(default_rep.path[0].command, CANVAS_DIMENSIONS)
      << " " << icon.name
      << " has no size in its icon definition, and it seems unlikely you want "
         "to display at the default of 48dip. Please specify a size in "
         "CreateVectorIcon().";
  return default_rep.path[1].arg;
}

#define DECLARE_VECTOR_COMMAND(x) {#x, gfx::x},

PathElement ParsePathElement(std::string_view s) {
  static base::NoDestructor<absl::flat_hash_map<std::string_view, CommandType>>
      kCommandMap({DECLARE_VECTOR_COMMANDS});

  // Attempt to parse as a command.
  auto it = kCommandMap->find(s);
  if (it != kCommandMap->end()) {
    return PathElement(it->second);
  }

  // Attempt to parse as hex.
  if (base::StartsWith(s, "0x")) {
    int int_value = 0;
    CHECK(base::HexStringToInt(s, &int_value));
    return PathElement(SkIntToScalar(int_value));
  }

  // Finally, parse as double.
  double value = 0.0;
  if (base::EndsWith(s, "f")) {
    auto trimmed = s.substr(0, s.length() - 1);
    CHECK(base::StringToDouble(trimmed, &value));
  } else {
    CHECK(base::StringToDouble(s, &value));
  }

  return PathElement(SkDoubleToScalar(value));
}

void ParsePathElements(std::string_view s,
                       std::vector<std::vector<PathElement>>& path_elements) {
  auto lines = base::SplitString(s, "\n", base::TRIM_WHITESPACE,
                                 base::SPLIT_WANT_NONEMPTY);
  for (const auto& line : lines) {
    if (base::StartsWith(line, "//")) {
      continue;
    }
    auto elements = base::SplitString(line, ", ", base::TRIM_WHITESPACE,
                                      base::SPLIT_WANT_NONEMPTY);
    for (const auto& element : elements) {
      if (element == kCanvasDimensions || path_elements.empty()) {
        path_elements.emplace_back();
      }
      path_elements.back().push_back(ParsePathElement(element));
    }
  }
}

}  // namespace gfx