1f3754a84289680782b2c7caac30fda18207a5c23Chris Craik/*
2f3754a84289680782b2c7caac30fda18207a5c23Chris Craik * Copyright (C) 2016 The Android Open Source Project
3f3754a84289680782b2c7caac30fda18207a5c23Chris Craik *
4f3754a84289680782b2c7caac30fda18207a5c23Chris Craik * Licensed under the Apache License, Version 2.0 (the "License");
5f3754a84289680782b2c7caac30fda18207a5c23Chris Craik * you may not use this file except in compliance with the License.
6f3754a84289680782b2c7caac30fda18207a5c23Chris Craik * You may obtain a copy of the License at
7f3754a84289680782b2c7caac30fda18207a5c23Chris Craik *
8f3754a84289680782b2c7caac30fda18207a5c23Chris Craik *      http://www.apache.org/licenses/LICENSE-2.0
9f3754a84289680782b2c7caac30fda18207a5c23Chris Craik *
10f3754a84289680782b2c7caac30fda18207a5c23Chris Craik * Unless required by applicable law or agreed to in writing, software
11f3754a84289680782b2c7caac30fda18207a5c23Chris Craik * distributed under the License is distributed on an "AS IS" BASIS,
12f3754a84289680782b2c7caac30fda18207a5c23Chris Craik * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f3754a84289680782b2c7caac30fda18207a5c23Chris Craik * See the License for the specific language governing permissions and
14f3754a84289680782b2c7caac30fda18207a5c23Chris Craik * limitations under the License.
15f3754a84289680782b2c7caac30fda18207a5c23Chris Craik */
16f3754a84289680782b2c7caac30fda18207a5c23Chris Craik
17f3754a84289680782b2c7caac30fda18207a5c23Chris Craik#include <gtest/gtest.h>
18f3754a84289680782b2c7caac30fda18207a5c23Chris Craik
19f3754a84289680782b2c7caac30fda18207a5c23Chris Craik#include "GammaFontRenderer.h"
20f3754a84289680782b2c7caac30fda18207a5c23Chris Craik#include "tests/common/TestUtils.h"
21f3754a84289680782b2c7caac30fda18207a5c23Chris Craik
22f3754a84289680782b2c7caac30fda18207a5c23Chris Craikusing namespace android::uirenderer;
23f3754a84289680782b2c7caac30fda18207a5c23Chris Craik
24f3754a84289680782b2c7caac30fda18207a5c23Chris Craikstatic bool isZero(uint8_t* data, int size) {
25f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    for (int i = 0; i < size; i++) {
26f3754a84289680782b2c7caac30fda18207a5c23Chris Craik        if (data[i]) return false;
27f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    }
28f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    return true;
29f3754a84289680782b2c7caac30fda18207a5c23Chris Craik}
30f3754a84289680782b2c7caac30fda18207a5c23Chris Craik
31f3754a84289680782b2c7caac30fda18207a5c23Chris CraikRENDERTHREAD_TEST(FontRenderer, renderDropShadow) {
32f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    SkPaint paint;
33f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    paint.setTextSize(10);
34f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
35f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    GammaFontRenderer gammaFontRenderer;
36f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    FontRenderer& fontRenderer = gammaFontRenderer.getFontRenderer();
37f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    fontRenderer.setFont(&paint, SkMatrix::I());
38f3754a84289680782b2c7caac30fda18207a5c23Chris Craik
39f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    std::vector<glyph_t> glyphs;
40f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    std::vector<float> positions;
41f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    float totalAdvance;
42f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    Rect bounds;
43f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    TestUtils::layoutTextUnscaled(paint, "This is a test",
44f3754a84289680782b2c7caac30fda18207a5c23Chris Craik            &glyphs, &positions, &totalAdvance, &bounds);
45f3754a84289680782b2c7caac30fda18207a5c23Chris Craik
46f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    for (int radius : {28, 20, 2}) {
47f3754a84289680782b2c7caac30fda18207a5c23Chris Craik        auto result = fontRenderer.renderDropShadow(&paint, glyphs.data(), glyphs.size(),
48f3754a84289680782b2c7caac30fda18207a5c23Chris Craik                radius, positions.data());
49f3754a84289680782b2c7caac30fda18207a5c23Chris Craik        ASSERT_NE(nullptr, result.image);
50f3754a84289680782b2c7caac30fda18207a5c23Chris Craik        EXPECT_FALSE(isZero(result.image, result.width * result.height));
51f3754a84289680782b2c7caac30fda18207a5c23Chris Craik        EXPECT_LE(bounds.getWidth() + radius * 2, (int) result.width);
52f3754a84289680782b2c7caac30fda18207a5c23Chris Craik        EXPECT_LE(bounds.getHeight() + radius * 2, (int) result.height);
53f3754a84289680782b2c7caac30fda18207a5c23Chris Craik        delete result.image;
54f3754a84289680782b2c7caac30fda18207a5c23Chris Craik    }
55f3754a84289680782b2c7caac30fda18207a5c23Chris Craik}
56