blob: 4c8d23dd82ea0a5d5c6e5174b85401748990f8e9 [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 Craik42a54072015-11-24 11:41:54 -080044 SkSurfaceProps surfaceProps(0, kUnknown_SkPixelGeometry);
Chris Craikd7448e62015-12-15 10:34:36 -080045 SkAutoGlyphCacheNoGamma autoCache(paint, &surfaceProps, &SkMatrix::I());
Chris Craika1717272015-11-19 13:02:43 -080046
Chris Craik42a54072015-11-24 11:41:54 -080047 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);
Chris Craika1717272015-11-19 13:02:43 -080055
Chris Craik42a54072015-11-24 11:41:54 -080056 // push glyph and its relative position
57 glyphs.push_back(glyph);
58 positions.push_back(totalAdvance);
59 positions.push_back(0);
Chris Craika1717272015-11-19 13:02:43 -080060
Chris Craik42a54072015-11-24 11:41:54 -080061 // 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);
Chris Craika1717272015-11-19 13:02:43 -080066
Chris Craik42a54072015-11-24 11:41:54 -080067 // 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);
Chris Craika1717272015-11-19 13:02:43 -080087}
88
Chris Craikd7448e62015-12-15 10:34:36 -080089void 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
John Reck16c9d6a2015-11-17 15:51:08 -0800105} /* namespace uirenderer */
106} /* namespace android */