Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ui/gfx/vector_icons.h" | |
| 6 | |
| 7 #include "ui/gfx/canvas.h" | |
| 8 | |
| 9 namespace gfx { | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 const SkScalar reference_size = 48.0; | |
| 14 | |
| 15 enum CommandType { | |
| 16 MOVE_TO, | |
| 17 R_MOVE_TO, | |
| 18 LINE_TO, | |
| 19 R_LINE_TO, | |
| 20 H_LINE_TO, | |
| 21 R_H_LINE_TO, | |
| 22 V_LINE_TO, | |
| 23 R_V_LINE_TO, | |
| 24 R_CUBIC_TO, | |
| 25 CIRCLE, | |
| 26 CLOSE, | |
| 27 END | |
| 28 }; | |
| 29 | |
| 30 struct PathElement { | |
| 31 PathElement(CommandType type) { this->type = type; } | |
| 32 PathElement(SkScalar arg) { this->arg = arg; } | |
| 33 | |
| 34 union { | |
| 35 CommandType type; | |
| 36 SkScalar arg; | |
| 37 }; | |
| 38 }; | |
| 39 | |
| 40 const PathElement* GetPathForVectorIcon(VectorIconId id) { | |
| 41 switch (id) { | |
| 42 case CHECK_CIRCLE: { | |
| 43 static PathElement path[] = { | |
|
sky
2015/07/01 16:19:52
Style guide says: " and function static variables,
Evan Stade
2015/07/01 21:52:58
"[...] or arrays/structs of POD."
sky
2015/07/01 22:00:55
I'm pretty sure once you add a constructor that ru
Evan Stade
2015/07/02 00:08:05
It's hard to interpret from the style guide, but i
Evan Stade
2015/07/02 00:58:24
I take it back, MSVC doesn't support constexpr yet
| |
| 44 CIRCLE, 24, 24, 20, | |
| 45 MOVE_TO, 20, 34, | |
| 46 LINE_TO, 10, 24, | |
| 47 R_LINE_TO, 2.83, -2.83, | |
| 48 LINE_TO, 20, 28.34, | |
| 49 R_LINE_TO, 15.17, -15.17, | |
| 50 LINE_TO, 38, 16, | |
| 51 LINE_TO, 20, 34, | |
| 52 END | |
| 53 }; | |
| 54 return path; | |
| 55 } | |
| 56 | |
| 57 case PHOTO_CAMERA: { | |
| 58 static PathElement path[] = { | |
| 59 CIRCLE, 24, 24, 6.4, | |
| 60 MOVE_TO, 18, 4, | |
| 61 R_LINE_TO, -3.66, 4, | |
| 62 H_LINE_TO, 8, | |
| 63 R_CUBIC_TO, -2.21, 0, -4, 1.79, -4, 4, | |
| 64 R_V_LINE_TO, 24, | |
| 65 R_CUBIC_TO, 0, 2.21, 1.79, 4, 4, 4, | |
| 66 R_H_LINE_TO, 32, | |
| 67 R_CUBIC_TO, 2.21, 0, 4, -1.79, 4, -4, | |
| 68 V_LINE_TO, 12, | |
| 69 R_CUBIC_TO, 0, -2.21, -1.79, -4, -4, -4, | |
| 70 R_H_LINE_TO, -6.34, | |
| 71 LINE_TO, 30, 4, | |
| 72 H_LINE_TO, 18, | |
| 73 CLOSE, | |
| 74 CIRCLE, 24, 24, 10, | |
| 75 END | |
| 76 }; | |
| 77 return path; | |
| 78 } | |
| 79 | |
| 80 case VECTOR_ICON_NONE: | |
| 81 NOTREACHED(); | |
| 82 return nullptr; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 } // namespace | |
| 87 | |
| 88 void PaintVectorIcon(Canvas* canvas, VectorIconId id, size_t dp_size, | |
| 89 SkColor color) { | |
| 90 if (id == VECTOR_ICON_NONE) { | |
| 91 NOTREACHED(); | |
| 92 return; | |
| 93 } | |
| 94 | |
| 95 const PathElement* path_elements = GetPathForVectorIcon(id); | |
| 96 SkPath path; | |
| 97 path.setFillType(SkPath::kEvenOdd_FillType); | |
| 98 SkScalar scale = dp_size / reference_size; | |
| 99 | |
| 100 for (size_t i = 0; path_elements[i].type != END;) { | |
| 101 switch (path_elements[i++].type) { | |
| 102 case CIRCLE: { | |
| 103 SkScalar x = scale * path_elements[i++].arg; | |
| 104 SkScalar y = scale * path_elements[i++].arg; | |
| 105 SkScalar r = scale * path_elements[i++].arg; | |
| 106 path.addCircle(x, y, r); | |
| 107 break; | |
| 108 } | |
| 109 | |
| 110 case MOVE_TO: { | |
| 111 SkScalar x = scale * path_elements[i++].arg; | |
| 112 SkScalar y = scale * path_elements[i++].arg; | |
| 113 path.moveTo(x, y); | |
| 114 break; | |
| 115 } | |
| 116 | |
| 117 case R_MOVE_TO: { | |
| 118 SkScalar x = scale * path_elements[i++].arg; | |
| 119 SkScalar y = scale * path_elements[i++].arg; | |
| 120 path.rMoveTo(x, y); | |
| 121 break; | |
| 122 } | |
| 123 | |
| 124 case LINE_TO: { | |
| 125 SkScalar x = scale * path_elements[i++].arg; | |
| 126 SkScalar y = scale * path_elements[i++].arg; | |
| 127 path.lineTo(x, y); | |
| 128 break; | |
| 129 } | |
| 130 | |
| 131 case R_LINE_TO: { | |
| 132 SkScalar x = scale * path_elements[i++].arg; | |
| 133 SkScalar y = scale * path_elements[i++].arg; | |
| 134 path.rLineTo(x, y); | |
| 135 break; | |
| 136 } | |
| 137 | |
| 138 case H_LINE_TO: { | |
| 139 SkPoint last_point; | |
| 140 path.getLastPt(&last_point); | |
| 141 SkScalar x = scale * path_elements[i++].arg; | |
| 142 path.lineTo(x, last_point.fY); | |
| 143 break; | |
| 144 } | |
| 145 | |
| 146 case V_LINE_TO: { | |
| 147 SkPoint last_point; | |
| 148 path.getLastPt(&last_point); | |
| 149 SkScalar y = scale * path_elements[i++].arg; | |
| 150 path.lineTo(last_point.fX, y); | |
| 151 break; | |
| 152 } | |
| 153 | |
| 154 case R_H_LINE_TO: { | |
| 155 SkScalar x = scale * path_elements[i++].arg; | |
| 156 path.rLineTo(x, 0); | |
| 157 break; | |
| 158 } | |
| 159 | |
| 160 case R_V_LINE_TO: { | |
| 161 SkScalar y = scale * path_elements[i++].arg; | |
| 162 path.rLineTo(0, y); | |
| 163 break; | |
| 164 } | |
| 165 | |
| 166 case R_CUBIC_TO: { | |
| 167 SkScalar x1 = scale * path_elements[i++].arg; | |
| 168 SkScalar y1 = scale * path_elements[i++].arg; | |
| 169 SkScalar x2 = scale * path_elements[i++].arg; | |
| 170 SkScalar y2 = scale * path_elements[i++].arg; | |
| 171 SkScalar x3 = scale * path_elements[i++].arg; | |
| 172 SkScalar y3 = scale * path_elements[i++].arg; | |
| 173 path.rCubicTo(x1, y1, x2, y2, x3, y3); | |
| 174 break; | |
| 175 } | |
| 176 | |
| 177 case CLOSE: { | |
| 178 path.close(); | |
| 179 break; | |
| 180 } | |
| 181 | |
| 182 case END: | |
| 183 NOTREACHED(); | |
| 184 break; | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 SkPaint paint; | |
| 189 paint.setStyle(SkPaint::kFill_Style); | |
| 190 paint.setAntiAlias(true); | |
| 191 paint.setColor(color); | |
| 192 canvas->DrawPath(path, paint); | |
| 193 } | |
| 194 | |
| 195 } // namespace gfx | |
| OLD | NEW |