| OLD | NEW | 
|    1  |    1  | 
|    2 /* |    2 /* | 
|    3  * Copyright 2006 The Android Open Source Project |    3  * Copyright 2006 The Android Open Source Project | 
|    4  * |    4  * | 
|    5  * Use of this source code is governed by a BSD-style license that can be |    5  * Use of this source code is governed by a BSD-style license that can be | 
|    6  * found in the LICENSE file. |    6  * found in the LICENSE file. | 
|    7  */ |    7  */ | 
|    8  |    8  | 
|    9  |    9  | 
|   10 #include "SkParse.h" |   10 #include "SkParse.h" | 
| (...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  416             } |  416             } | 
|  417             ++sixMatchPtr; |  417             ++sixMatchPtr; | 
|  418         } |  418         } | 
|  419         int sixMask = *sixMatchPtr & ~0x80000000; |  419         int sixMask = *sixMatchPtr & ~0x80000000; | 
|  420         int midMask = gColorNames[mid] & ~0x80000000; |  420         int midMask = gColorNames[mid] & ~0x80000000; | 
|  421         if (sixMask > midMask) { |  421         if (sixMask > midMask) { | 
|  422             lo = mid + 2;   // skip color |  422             lo = mid + 2;   // skip color | 
|  423             while ((int) gColorNames[lo] >= 0) |  423             while ((int) gColorNames[lo] >= 0) | 
|  424                 ++lo; |  424                 ++lo; | 
|  425         } else if (hi == mid) |  425         } else if (hi == mid) | 
|  426             return NULL; |  426             return nullptr; | 
|  427         else |  427         else | 
|  428             hi = mid; |  428             hi = mid; | 
|  429     } |  429     } | 
|  430     return NULL; |  430     return nullptr; | 
|  431 } |  431 } | 
|  432  |  432  | 
|  433 // !!! move to char utilities |  433 // !!! move to char utilities | 
|  434 //static int count_separators(const char* str, const char* sep) { |  434 //static int count_separators(const char* str, const char* sep) { | 
|  435 //  char c; |  435 //  char c; | 
|  436 //  int separators = 0; |  436 //  int separators = 0; | 
|  437 //  while ((c = *str++) != '\0') { |  437 //  while ((c = *str++) != '\0') { | 
|  438 //      if (strchr(sep, c) == NULL) |  438 //      if (strchr(sep, c) == nullptr) | 
|  439 //          continue; |  439 //          continue; | 
|  440 //      do { |  440 //      do { | 
|  441 //          if ((c = *str++) == '\0') |  441 //          if ((c = *str++) == '\0') | 
|  442 //              goto goHome; |  442 //              goto goHome; | 
|  443 //      } while (strchr(sep, c) != NULL); |  443 //      } while (strchr(sep, c) != nullptr); | 
|  444 //      separators++; |  444 //      separators++; | 
|  445 //  } |  445 //  } | 
|  446 //goHome: |  446 //goHome: | 
|  447 //  return separators; |  447 //  return separators; | 
|  448 //} |  448 //} | 
|  449  |  449  | 
|  450 static inline unsigned nib2byte(unsigned n) |  450 static inline unsigned nib2byte(unsigned n) | 
|  451 { |  451 { | 
|  452     SkASSERT((n & ~0xF) == 0); |  452     SkASSERT((n & ~0xF) == 0); | 
|  453     return (n << 4) | n; |  453     return (n << 4) | n; | 
|  454 } |  454 } | 
|  455  |  455  | 
|  456 const char* SkParse::FindColor(const char* value, SkColor* colorPtr) { |  456 const char* SkParse::FindColor(const char* value, SkColor* colorPtr) { | 
|  457     unsigned int oldAlpha = SkColorGetA(*colorPtr); |  457     unsigned int oldAlpha = SkColorGetA(*colorPtr); | 
|  458     if (value[0] == '#') { |  458     if (value[0] == '#') { | 
|  459         uint32_t    hex; |  459         uint32_t    hex; | 
|  460         const char* end = SkParse::FindHex(value + 1, &hex); |  460         const char* end = SkParse::FindHex(value + 1, &hex); | 
|  461 //      SkASSERT(end); |  461 //      SkASSERT(end); | 
|  462         if (end == NULL) |  462         if (end == nullptr) | 
|  463             return end; |  463             return end; | 
|  464         size_t len = end - value - 1; |  464         size_t len = end - value - 1; | 
|  465         if (len == 3 || len == 4) { |  465         if (len == 3 || len == 4) { | 
|  466             unsigned a = len == 4 ? nib2byte(hex >> 12) : oldAlpha; |  466             unsigned a = len == 4 ? nib2byte(hex >> 12) : oldAlpha; | 
|  467             unsigned r = nib2byte((hex >> 8) & 0xF); |  467             unsigned r = nib2byte((hex >> 8) & 0xF); | 
|  468             unsigned g = nib2byte((hex >> 4) & 0xF); |  468             unsigned g = nib2byte((hex >> 4) & 0xF); | 
|  469             unsigned b = nib2byte(hex & 0xF); |  469             unsigned b = nib2byte(hex & 0xF); | 
|  470             *colorPtr = SkColorSetARGB(a, r, g, b); |  470             *colorPtr = SkColorSetARGB(a, r, g, b); | 
|  471             return end; |  471             return end; | 
|  472         } else if (len == 6 || len == 8) { |  472         } else if (len == 6 || len == 8) { | 
|  473             if (len == 6) |  473             if (len == 6) | 
|  474                 hex |= oldAlpha << 24; |  474                 hex |= oldAlpha << 24; | 
|  475             *colorPtr = hex; |  475             *colorPtr = hex; | 
|  476             return end; |  476             return end; | 
|  477         } else { |  477         } else { | 
|  478 //          SkASSERT(0); |  478 //          SkASSERT(0); | 
|  479             return NULL; |  479             return nullptr; | 
|  480         } |  480         } | 
|  481 //  } else if (strchr(value, ',')) { |  481 //  } else if (strchr(value, ',')) { | 
|  482 //      SkScalar array[4]; |  482 //      SkScalar array[4]; | 
|  483 //      int count = count_separators(value, ",") + 1; // !!! count commas, add 1 |  483 //      int count = count_separators(value, ",") + 1; // !!! count commas, add 1 | 
|  484 //      SkASSERT(count == 3 || count == 4); |  484 //      SkASSERT(count == 3 || count == 4); | 
|  485 //      array[0] = SK_Scalar1 * 255; |  485 //      array[0] = SK_Scalar1 * 255; | 
|  486 //      const char* end = SkParse::FindScalars(value, &array[4 - count], count); |  486 //      const char* end = SkParse::FindScalars(value, &array[4 - count], count); | 
|  487 //      if (end == NULL) |  487 //      if (end == nullptr) | 
|  488 //          return NULL; |  488 //          return nullptr; | 
|  489         // !!! range check for errors? |  489         // !!! range check for errors? | 
|  490 //      *colorPtr = SkColorSetARGB(SkScalarRoundToInt(array[0]), SkScalarRoundTo
     Int(array[1]), |  490 //      *colorPtr = SkColorSetARGB(SkScalarRoundToInt(array[0]), SkScalarRoundTo
     Int(array[1]), | 
|  491 //          SkScalarRoundToInt(array[2]), SkScalarRoundToInt(array[3])); |  491 //          SkScalarRoundToInt(array[2]), SkScalarRoundToInt(array[3])); | 
|  492 //      return end; |  492 //      return end; | 
|  493     } else |  493     } else | 
|  494         return FindNamedColor(value, strlen(value), colorPtr); |  494         return FindNamedColor(value, strlen(value), colorPtr); | 
|  495 } |  495 } | 
|  496  |  496  | 
|  497 #ifdef SK_SUPPORT_UNITTEST |  497 #ifdef SK_SUPPORT_UNITTEST | 
|  498 void SkParse::TestColor() { |  498 void SkParse::TestColor() { | 
|  499     if (false) |  499     if (false) | 
|  500         CreateTable();  // regenerates data table in the output window |  500         CreateTable();  // regenerates data table in the output window | 
|  501     SkColor result; |  501     SkColor result; | 
|  502     int index; |  502     int index; | 
|  503     for (index = 0; index < colorNamesSize; index++) { |  503     for (index = 0; index < colorNamesSize; index++) { | 
|  504         result = SK_ColorBLACK; |  504         result = SK_ColorBLACK; | 
|  505         SkNameRGB nameRGB = colorNames[index]; |  505         SkNameRGB nameRGB = colorNames[index]; | 
|  506         SkASSERT(FindColor(nameRGB.name, &result) != NULL); |  506         SkASSERT(FindColor(nameRGB.name, &result) != nullptr); | 
|  507         SkASSERT(result == (SkColor) (nameRGB.rgb | 0xFF000000)); |  507         SkASSERT(result == (SkColor) (nameRGB.rgb | 0xFF000000)); | 
|  508     } |  508     } | 
|  509     for (index = 0; index < colorNamesSize; index++) { |  509     for (index = 0; index < colorNamesSize; index++) { | 
|  510         result = SK_ColorBLACK; |  510         result = SK_ColorBLACK; | 
|  511         SkNameRGB nameRGB = colorNames[index]; |  511         SkNameRGB nameRGB = colorNames[index]; | 
|  512         char bad[24]; |  512         char bad[24]; | 
|  513         size_t len = strlen(nameRGB.name); |  513         size_t len = strlen(nameRGB.name); | 
|  514         memcpy(bad, nameRGB.name, len); |  514         memcpy(bad, nameRGB.name, len); | 
|  515         bad[len - 1] -= 1; |  515         bad[len - 1] -= 1; | 
|  516         SkASSERT(FindColor(bad, &result) == NULL); |  516         SkASSERT(FindColor(bad, &result) == nullptr); | 
|  517         bad[len - 1] += 2; |  517         bad[len - 1] += 2; | 
|  518         SkASSERT(FindColor(bad, &result) == NULL); |  518         SkASSERT(FindColor(bad, &result) == nullptr); | 
|  519     } |  519     } | 
|  520     result = SK_ColorBLACK; |  520     result = SK_ColorBLACK; | 
|  521     SkASSERT(FindColor("lightGrey", &result)); |  521     SkASSERT(FindColor("lightGrey", &result)); | 
|  522     SkASSERT(result == 0xffd3d3d3); |  522     SkASSERT(result == 0xffd3d3d3); | 
|  523 //  SkASSERT(FindColor("12,34,56,78", &result)); |  523 //  SkASSERT(FindColor("12,34,56,78", &result)); | 
|  524 //  SkASSERT(result == ((12 << 24) | (34 << 16) | (56 << 8) | (78 << 0))); |  524 //  SkASSERT(result == ((12 << 24) | (34 << 16) | (56 << 8) | (78 << 0))); | 
|  525     result = SK_ColorBLACK; |  525     result = SK_ColorBLACK; | 
|  526     SkASSERT(FindColor("#ABCdef", &result)); |  526     SkASSERT(FindColor("#ABCdef", &result)); | 
|  527     SkASSERT(result == 0XFFABCdef); |  527     SkASSERT(result == 0XFFABCdef); | 
|  528     SkASSERT(FindColor("#12ABCdef", &result)); |  528     SkASSERT(FindColor("#12ABCdef", &result)); | 
|  529     SkASSERT(result == 0X12ABCdef); |  529     SkASSERT(result == 0X12ABCdef); | 
|  530     result = SK_ColorBLACK; |  530     result = SK_ColorBLACK; | 
|  531     SkASSERT(FindColor("#123", &result)); |  531     SkASSERT(FindColor("#123", &result)); | 
|  532     SkASSERT(result == 0Xff112233); |  532     SkASSERT(result == 0Xff112233); | 
|  533     SkASSERT(FindColor("#abcd", &result)); |  533     SkASSERT(FindColor("#abcd", &result)); | 
|  534     SkASSERT(result == 0Xaabbccdd); |  534     SkASSERT(result == 0Xaabbccdd); | 
|  535     result = SK_ColorBLACK; |  535     result = SK_ColorBLACK; | 
|  536 //  SkASSERT(FindColor("71,162,253", &result)); |  536 //  SkASSERT(FindColor("71,162,253", &result)); | 
|  537 //  SkASSERT(result == ((0xFF << 24) | (71 << 16) | (162 << 8) | (253 << 0))); |  537 //  SkASSERT(result == ((0xFF << 24) | (71 << 16) | (162 << 8) | (253 << 0))); | 
|  538 } |  538 } | 
|  539 #endif |  539 #endif | 
| OLD | NEW |