| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "embedders/openglui/common/canvas_state.h" | 5 #include "embedders/openglui/common/canvas_state.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "embedders/openglui/common/support.h" | 10 #include "embedders/openglui/common/support.h" |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 return baseline; | 133 return baseline; |
| 134 } | 134 } |
| 135 | 135 |
| 136 const char* CanvasState::setTextDirection(const char* direction) { | 136 const char* CanvasState::setTextDirection(const char* direction) { |
| 137 // TODO(gram): Add support for this if Skia does. | 137 // TODO(gram): Add support for this if Skia does. |
| 138 return direction; | 138 return direction; |
| 139 } | 139 } |
| 140 | 140 |
| 141 void CanvasState::setGlobalCompositeOperation(const char* op) { | 141 void CanvasState::setGlobalCompositeOperation(const char* op) { |
| 142 SkXfermode::Mode mode; | 142 SkXfermode::Mode mode; |
| 143 if (strcmp(op, "source-atop") == 0) { | 143 static const struct CompositOpToXfermodeMode { |
| 144 mode = SkXfermode::kSrcATop_Mode; | 144 const char* mCompositOp; |
| 145 } else if (strcmp(op, "source-in") == 0) { | 145 uint8_t m_xfermodeMode; |
| 146 mode = SkXfermode::kSrcIn_Mode; | 146 } gMapCompositOpsToXfermodeModes[] = { |
| 147 } else if (strcmp(op, "source-out") == 0) { | 147 { "clear", SkXfermode::kClear_Mode }, |
| 148 mode = SkXfermode::kSrcOut_Mode; | 148 { "copy", SkXfermode::kSrc_Mode }, |
| 149 } else if (strcmp(op, "source-over") == 0) { | 149 { "source-over", SkXfermode::kSrcOver_Mode }, |
| 150 mode = SkXfermode::kSrcOver_Mode; // Default. | 150 { "source-in", SkXfermode::kSrcIn_Mode }, |
| 151 } else if (strcmp(op, "destination-atop") == 0) { | 151 { "source-out", SkXfermode::kSrcOut_Mode }, |
| 152 mode = SkXfermode::kDstATop_Mode; | 152 { "source-atop", SkXfermode::kSrcATop_Mode }, |
| 153 } else if (strcmp(op, "destination-in") == 0) { | 153 { "destination-over", SkXfermode::kDstOver_Mode }, |
| 154 mode = SkXfermode::kDstIn_Mode; | 154 { "destination-in", SkXfermode::kDstIn_Mode }, |
| 155 } else if (strcmp(op, "destination-out") == 0) { | 155 { "destination-out", SkXfermode::kDstOut_Mode }, |
| 156 mode = SkXfermode::kDstOut_Mode; | 156 { "destination-atop", SkXfermode::kDstATop_Mode }, |
| 157 } else if (strcmp(op, "destination-over") == 0) { | 157 { "xor", SkXfermode::kXor_Mode }, |
| 158 mode = SkXfermode::kDstOver_Mode; | 158 { "darker", SkXfermode::kDarken_Mode }, |
| 159 } else if (strcmp(op, "lighter") == 0) { | 159 { "lighter", SkXfermode::kPlus_Mode } |
| 160 mode = SkXfermode::kLighten_Mode; | 160 }; |
| 161 } else if (strcmp(op, "darker") == 0) { | 161 for (unsigned i = 0; |
| 162 mode = SkXfermode::kDarken_Mode; | 162 i < SK_ARRAY_COUNT(gMapCompositOpsToXfermodeModes); |
| 163 } else if (strcmp(op, "xor") == 0) { | 163 i++) { |
| 164 mode = SkXfermode::kXor_Mode; | 164 if (strcmp(op, gMapCompositOpsToXfermodeModes[i].mCompositOp) == 0) { |
| 165 } else if (strcmp(op, "copy") == 0) { | 165 mode = (SkXfermode::Mode)gMapCompositOpsToXfermodeModes[i].m_xfermodeMode; |
| 166 mode = SkXfermode::kSrc_Mode; | 166 paint_.setXfermodeMode(mode); |
| 167 return; |
| 168 } |
| 167 } | 169 } |
| 168 SkXfermode* m = SkXfermode::Create(mode); | 170 LOGE("Unknown CompositeOperator %s\n", op); |
| 169 // It seems we don't need unref() here. Including it causes | 171 paint_.setXfermodeMode(SkXfermode::kSrcOver_Mode); // fall-back |
| 170 // a crash. Maybe Skia has a preallocated long-lived set of | |
| 171 // instances. | |
| 172 paint_.setXfermode(m); | |
| 173 } | 172 } |
| 174 | 173 |
| 175 void CanvasState::Arc(float x, float y, float radius, | 174 void CanvasState::Arc(float x, float y, float radius, |
| 176 float startAngle, float endAngle, | 175 float startAngle, float endAngle, |
| 177 bool antiClockwise) { | 176 bool antiClockwise) { |
| 178 SkRect rect; | 177 SkRect rect; |
| 179 rect.set(x - radius, y - radius, x + radius, y + radius); | 178 rect.set(x - radius, y - radius, x + radius, y + radius); |
| 180 bool doCircle = false; | 179 bool doCircle = false; |
| 181 | 180 |
| 182 static float twoPi = 2 * M_PI; | 181 static float twoPi = 2 * M_PI; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 208 int hexDigit(char c) { | 207 int hexDigit(char c) { |
| 209 if (c >= '0' && c <= '9') return c - '0'; | 208 if (c >= '0' && c <= '9') return c - '0'; |
| 210 if (c <= 'Z') return c - 'A' + 10; | 209 if (c <= 'Z') return c - 'A' + 10; |
| 211 return c - 'a' + 10; | 210 return c - 'a' + 10; |
| 212 } | 211 } |
| 213 | 212 |
| 214 // Color parser. | 213 // Color parser. |
| 215 // See http://www.w3.org/TR/CSS21/syndata.html#color-units. | 214 // See http://www.w3.org/TR/CSS21/syndata.html#color-units. |
| 216 // There is also another format: hsl(240,100%,100%) (and hsla) | 215 // There is also another format: hsl(240,100%,100%) (and hsla) |
| 217 // TODO(gram): We probably eventually want to use a table rather | 216 // TODO(gram): We probably eventually want to use a table rather |
| 218 // than a big if statement. | 217 // than a big if statement; see setGlobalCompositeOperation for |
| 218 // an example. |
| 219 ColorRGBA CanvasState::GetColor(const char* color) { | 219 ColorRGBA CanvasState::GetColor(const char* color) { |
| 220 if (color[0] == '#') { | 220 if (color[0] == '#') { |
| 221 int r, g, b; | 221 int r = 0, g = 0, b = 0; |
| 222 if (strlen(color) == 7) { | 222 if (strlen(color) == 7) { |
| 223 r = hexDigit(color[1]) * 16 + hexDigit(color[2]); | 223 r = hexDigit(color[1]) * 16 + hexDigit(color[2]); |
| 224 g = hexDigit(color[3]) * 16 + hexDigit(color[4]); | 224 g = hexDigit(color[3]) * 16 + hexDigit(color[4]); |
| 225 b = hexDigit(color[5]) * 16 + hexDigit(color[6]); | 225 b = hexDigit(color[5]) * 16 + hexDigit(color[6]); |
| 226 } else if (strlen(color) == 4) { | 226 } else if (strlen(color) == 4) { |
| 227 r = hexDigit(color[1]) * 16 + hexDigit(color[1]); | 227 r = hexDigit(color[1]) * 16 + hexDigit(color[1]); |
| 228 g = hexDigit(color[2]) * 16 + hexDigit(color[2]); | 228 g = hexDigit(color[2]) * 16 + hexDigit(color[2]); |
| 229 b = hexDigit(color[3]) * 16 + hexDigit(color[3]); | 229 b = hexDigit(color[3]) * 16 + hexDigit(color[3]); |
| 230 } | 230 } |
| 231 return ColorRGBA(r, g, b); | 231 return ColorRGBA(r, g, b); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 if (sw < 0) sw = bm.width(); | 290 if (sw < 0) sw = bm.width(); |
| 291 if (dw < 0) dw = bm.width(); | 291 if (dw < 0) dw = bm.width(); |
| 292 if (sh < 0) sh = bm.height(); | 292 if (sh < 0) sh = bm.height(); |
| 293 if (dh < 0) dh = bm.height(); | 293 if (dh < 0) dh = bm.height(); |
| 294 SkIRect src = SkIRect::MakeXYWH(sx, sy, sw, sh); | 294 SkIRect src = SkIRect::MakeXYWH(sx, sy, sw, sh); |
| 295 SkRect dst = SkRect::MakeXYWH(dx, dy, dw, dh); | 295 SkRect dst = SkRect::MakeXYWH(dx, dy, dw, dh); |
| 296 LOGI("DrawImage(_,%d,%d,%d,%d,%d,%d,%d,%d)", sx, sy, sw, sh, dx, dy, dw, dh); | 296 LOGI("DrawImage(_,%d,%d,%d,%d,%d,%d,%d,%d)", sx, sy, sw, sh, dx, dy, dw, dh); |
| 297 canvas_->drawBitmapRect(bm, &src, dst); | 297 canvas_->drawBitmapRect(bm, &src, dst); |
| 298 } | 298 } |
| 299 | 299 |
| OLD | NEW |