TestUtils.cpp revision d7448e65e243754f31890baef29dff187dc2e5e5
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& paint, float x, float y) { 41 // drawing text requires GlyphID TextEncoding (which JNI layer would have done) 42 LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding, 43 "must use glyph encoding"); 44 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 45 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); 46 47 float totalAdvance = 0; 48 std::vector<glyph_t> glyphs; 49 std::vector<float> positions; 50 Rect bounds; 51 while (*text != '\0') { 52 SkUnichar unichar = SkUTF8_NextUnichar(&text); 53 glyph_t glyph = autoCache.getCache()->unicharToGlyph(unichar); 54 autoCache.getCache()->unicharToGlyph(unichar); 55 56 // push glyph and its relative position 57 glyphs.push_back(glyph); 58 positions.push_back(totalAdvance); 59 positions.push_back(0); 60 61 // compute bounds 62 SkGlyph skGlyph = autoCache.getCache()->getUnicharMetrics(unichar); 63 Rect glyphBounds(skGlyph.fWidth, skGlyph.fHeight); 64 glyphBounds.translate(totalAdvance + skGlyph.fLeft, skGlyph.fTop); 65 bounds.unionWith(glyphBounds); 66 67 // advance next character 68 SkScalar skWidth; 69 paint.getTextWidths(&glyph, sizeof(glyph), &skWidth, NULL); 70 totalAdvance += skWidth; 71 } 72 73 // apply alignment via x parameter (which JNI layer would have done) 74 if (paint.getTextAlign() == SkPaint::kCenter_Align) { 75 x -= totalAdvance / 2; 76 } else if (paint.getTextAlign() == SkPaint::kRight_Align) { 77 x -= totalAdvance; 78 } 79 80 bounds.translate(x, y); 81 82 // Force left alignment, since alignment offset is already baked in 83 SkPaint alignPaintCopy(paint); 84 alignPaintCopy.setTextAlign(SkPaint::kLeft_Align); 85 canvas->drawText(glyphs.data(), positions.data(), glyphs.size(), alignPaintCopy, x, y, 86 bounds.left, bounds.top, bounds.right, bounds.bottom, totalAdvance); 87} 88 89void TestUtils::drawTextToCanvas(TestCanvas* canvas, const char* text, 90 const SkPaint& paint, const SkPath& path) { 91 // drawing text requires GlyphID TextEncoding (which JNI layer would have done) 92 LOG_ALWAYS_FATAL_IF(paint.getTextEncoding() != SkPaint::kGlyphID_TextEncoding, 93 "must use glyph encoding"); 94 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry); 95 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I()); 96 97 std::vector<glyph_t> glyphs; 98 while (*text != '\0') { 99 SkUnichar unichar = SkUTF8_NextUnichar(&text); 100 glyphs.push_back(autoCache.getCache()->unicharToGlyph(unichar)); 101 } 102 canvas->drawTextOnPath(glyphs.data(), glyphs.size(), path, 0, 0, paint); 103} 104 105} /* namespace uirenderer */ 106} /* namespace android */ 107