TestUtils.cpp revision a1717271caac5e8ea3808c331d4141ac01a42134
1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#include "TestUtils.h" 18 19namespace android { 20namespace uirenderer { 21 22SkColor TestUtils::interpolateColor(float fraction, SkColor start, SkColor end) { 23 int startA = (start >> 24) & 0xff; 24 int startR = (start >> 16) & 0xff; 25 int startG = (start >> 8) & 0xff; 26 int startB = start & 0xff; 27 28 int endA = (end >> 24) & 0xff; 29 int endR = (end >> 16) & 0xff; 30 int endG = (end >> 8) & 0xff; 31 int endB = end & 0xff; 32 33 return (int)((startA + (int)(fraction * (endA - startA))) << 24) 34 | (int)((startR + (int)(fraction * (endR - startR))) << 16) 35 | (int)((startG + (int)(fraction * (endG - startG))) << 8) 36 | (int)((startB + (int)(fraction * (endB - startB)))); 37} 38 39void TestUtils::drawTextToCanvas(TestCanvas* canvas, const char* text, 40 const SkPaint& inPaint, float x, float y) { 41 // copy to force TextEncoding (which JNI layer would have done) 42 SkPaint paint(inPaint); 43 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); 44 45 SkMatrix identity; 46 identity.setIdentity(); 47 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 48 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &identity); 49 50 float totalAdvance = 0; 51 std::vector<glyph_t> glyphs; 52 std::vector<float> positions; 53 Rect bounds; 54 while (*text != '\0') { 55 SkUnichar unichar = SkUTF8_NextUnichar(&text); 56 glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar); 57 autoCache.getCache()->unicharToGlyph(unichar); 58 59 // push glyph and its relative position 60 glyphs.push_back(glyph); 61 positions.push_back(totalAdvance); 62 positions.push_back(0); 63 64 // compute bounds 65 SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar); 66 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight); 67 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop); 68 bounds.unionWith(glyphBounds); 69 70 // advance next character 71 SkScalar skWidth; 72 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL); 73 totalAdvance += skWidth; 74 } 75 bounds.translate(x, y); 76 canvas->drawText(glyphs.data(), positions.data(), glyphs.size(), paint, x, y, 77 bounds.left, bounds.top, bounds.right, bounds.bottom, totalAdvance); 78} 79 80} /* namespace uirenderer */ 81} /* namespace android */ 82