| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
namespace android { |
| 20 |
namespace NinePatchUtils { |
| 21 |
|
| 22 |
static inline void SetLatticeDivs(SkCanvas::Lattice* lattice, const Res_png_9patch& chunk, |
| 23 |
int width, int height) { |
| 24 |
lattice->fXCount = chunk.numXDivs; |
| 25 |
lattice->fYCount = chunk.numYDivs; |
| 26 |
lattice->fXDivs = chunk.getXDivs(); |
| 27 |
lattice->fYDivs = chunk.getYDivs(); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
if (lattice->fXCount > 0 && width == lattice->fXDivs[lattice->fXCount - 1]) { |
| 32 |
lattice->fXCount--; |
| 33 |
} |
| 34 |
if (lattice->fYCount > 0 && height == lattice->fYDivs[lattice->fYCount - 1]) { |
| 35 |
lattice->fYCount--; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
static inline int NumDistinctRects(const SkCanvas::Lattice& lattice) { |
| 40 |
int xRects; |
| 41 |
if (lattice.fXCount > 0) { |
| 42 |
xRects = (0 == lattice.fXDivs[0]) ? lattice.fXCount : lattice.fXCount + 1; |
| 43 |
} else { |
| 44 |
xRects = 1; |
| 45 |
} |
| 46 |
|
| 47 |
int yRects; |
| 48 |
if (lattice.fYCount > 0) { |
| 49 |
yRects = (0 == lattice.fYDivs[0]) ? lattice.fYCount : lattice.fYCount + 1; |
| 50 |
} else { |
| 51 |
yRects = 1; |
| 52 |
} |
| 53 |
return xRects * yRects; |
| 54 |
} |
| 55 |
|
| 56 |
static inline void SetLatticeFlags(SkCanvas::Lattice* lattice, SkCanvas::Lattice::RectType* flags, |
| 57 |
int numFlags, const Res_png_9patch& chunk, SkColor* colors) { |
| 58 |
lattice->fRectTypes = flags; |
| 59 |
lattice->fColors = colors; |
| 60 |
sk_bzero(flags, numFlags * sizeof(SkCanvas::Lattice::RectType)); |
| 61 |
sk_bzero(colors, numFlags * sizeof(SkColor)); |
| 62 |
|
| 63 |
bool needPadRow = lattice->fYCount > 0 && 0 == lattice->fYDivs[0]; |
| 64 |
bool needPadCol = lattice->fXCount > 0 && 0 == lattice->fXDivs[0]; |
| 65 |
|
| 66 |
int yCount = lattice->fYCount; |
| 67 |
if (needPadRow) { |
| 68 |
|
| 69 |
flags += lattice->fXCount + 1; |
| 70 |
colors += lattice->fXCount + 1; |
| 71 |
yCount--; |
| 72 |
} |
| 73 |
|
| 74 |
int i = 0; |
| 75 |
bool setFlags = false; |
| 76 |
for (int y = 0; y < yCount + 1; y++) { |
| 77 |
for (int x = 0; x < lattice->fXCount + 1; x++) { |
| 78 |
if (0 == x && needPadCol) { |
| 79 |
|
| 80 |
flags++; |
| 81 |
colors++; |
| 82 |
continue; |
| 83 |
} |
| 84 |
|
| 85 |
uint32_t currentColor = chunk.getColors()[i++]; |
| 86 |
if (Res_png_9patch::TRANSPARENT_COLOR == currentColor) { |
| 87 |
*flags = SkCanvas::Lattice::kTransparent; |
| 88 |
setFlags = true; |
| 89 |
} else if (Res_png_9patch::NO_COLOR != currentColor) { |
| 90 |
*flags = SkCanvas::Lattice::kFixedColor; |
| 91 |
*colors = currentColor; |
| 92 |
setFlags = true; |
| 93 |
} |
| 94 |
|
| 95 |
flags++; |
| 96 |
colors++; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
if (!setFlags) { |
| 101 |
lattice->fRectTypes = nullptr; |
| 102 |
lattice->fColors = nullptr; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
} |
| 107 |
} |