TestUtils.cpp revision 8160f20b0aca8c6595d4b385d673f59b6bcd16a4
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
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
76    // apply alignment via x parameter (which JNI layer would have done)
77    if (paint.getTextAlign() == SkPaint::kCenter_Align) {
78        x -= totalAdvance / 2;
79    } else if (paint.getTextAlign() == SkPaint::kRight_Align) {
80        x -= totalAdvance;
81    }
82
83    bounds.translate(x, y);
84
85    // Force left alignment, since alignment offset is already baked in
86    SkPaint alignPaintCopy(paint);
87    alignPaintCopy.setTextAlign(SkPaint::kLeft_Align);
88    canvas->drawText(glyphs.data(), positions.data(), glyphs.size(), alignPaintCopy, x, y,
89                bounds.left, bounds.top, bounds.right, bounds.bottom, totalAdvance);
90}
91
92} /* namespace uirenderer */
93} /* namespace android */
94