blob: 6cef85203352b67ae86a19fa32ac6aaba245d7f6 [file] [log] [blame]
John Reck16c9d6a2015-11-17 15:51:08 -08001/*
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
Chris Craika1717272015-11-19 13:02:43 -080039void TestUtils::drawTextToCanvas(TestCanvas* canvas, const char* text,
Chris Craik42a54072015-11-24 11:41:54 -080040 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");
Chris Craika1717272015-11-19 13:02:43 -080044
Chris Craik42a54072015-11-24 11:41:54 -080045 SkMatrix identity;
46 identity.setIdentity();
47 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
48 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &identity);
Chris Craika1717272015-11-19 13:02:43 -080049
Chris Craik42a54072015-11-24 11:41:54 -080050 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);
Chris Craika1717272015-11-19 13:02:43 -080058
Chris Craik42a54072015-11-24 11:41:54 -080059 // push glyph and its relative position
60 glyphs.push_back(glyph);
61 positions.push_back(totalAdvance);
62 positions.push_back(0);
Chris Craika1717272015-11-19 13:02:43 -080063
Chris Craik42a54072015-11-24 11:41:54 -080064 // 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);
Chris Craika1717272015-11-19 13:02:43 -080069
Chris Craik42a54072015-11-24 11:41:54 -080070 // 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);
Chris Craika1717272015-11-19 13:02:43 -080090}
91
John Reck16c9d6a2015-11-17 15:51:08 -080092} /* namespace uirenderer */
93} /* namespace android */