17ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org/*
27ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org * Copyright 2014 Google Inc.
37ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org *
47ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
57ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org * found in the LICENSE file.
67ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org */
77ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
87ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org#include "SkFont.h"
97ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org#include "SkPaint.h"
107ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org#include "SkTypeface.h"
117ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org#include "Test.h"
127ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
137ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.orgstatic bool is_use_nonlinear_metrics(const SkPaint& paint) {
147ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    return !paint.isSubpixelText() && !paint.isLinearText();
157ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org}
167ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
177ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.orgstatic bool is_enable_auto_hints(const SkPaint& paint) {
187ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    return paint.isAutohinted();
197ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org}
207ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
217ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.orgstatic bool is_enable_bytecode_hints(const SkPaint& paint) {
227ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    return paint.getHinting() >= SkPaint::kFull_Hinting;
237ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org}
247ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
257ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.orgstatic void test_cachedfont(skiatest::Reporter* reporter, const SkPaint& paint) {
267ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    SkAutoTUnref<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
277ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
2849270921860ce1cab9bf825e6038fa854943cc28reed@google.com    // Currently SkFont resolves null into the default, so only test if paint's is not null
2949270921860ce1cab9bf825e6038fa854943cc28reed@google.com    if (paint.getTypeface()) {
3049270921860ce1cab9bf825e6038fa854943cc28reed@google.com        REPORTER_ASSERT(reporter, font->getTypeface() == paint.getTypeface());
3149270921860ce1cab9bf825e6038fa854943cc28reed@google.com    }
327ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    REPORTER_ASSERT(reporter, font->getSize() == paint.getTextSize());
337ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    REPORTER_ASSERT(reporter, font->getScaleX() == paint.getTextScaleX());
347ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    REPORTER_ASSERT(reporter, font->getSkewX() == paint.getTextSkewX());
357ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
367ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    REPORTER_ASSERT(reporter, font->isVertical() == paint.isVerticalText());
377ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    REPORTER_ASSERT(reporter, font->isEmbolden() == paint.isFakeBoldText());
387ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
397ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    REPORTER_ASSERT(reporter, font->isUseNonLinearMetrics() == is_use_nonlinear_metrics(paint));
407ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    REPORTER_ASSERT(reporter, font->isEnableAutoHints() == is_enable_auto_hints(paint));
417ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    REPORTER_ASSERT(reporter, font->isEnableByteCodeHints() == is_enable_bytecode_hints(paint));
427ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org}
437ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
447ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.orgstatic void test_cachedfont(skiatest::Reporter* reporter) {
457ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    static const char* const faces[] = {
467ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        NULL,   // default font
477ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
487ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        "Courier New", "Verdana", "monospace",
497ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    };
507ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
517ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    static const struct {
527ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        SkPaint::Hinting    hinting;
537ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        unsigned            flags;
547ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    } settings[] = {
557ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SkPaint::kNo_Hinting,     0                               },
567ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SkPaint::kNo_Hinting,     SkPaint::kLinearText_Flag       },
577ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SkPaint::kNo_Hinting,     SkPaint::kSubpixelText_Flag     },
587ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SkPaint::kSlight_Hinting, 0                               },
597ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag       },
607ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag     },
617ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SkPaint::kNormal_Hinting, 0                               },
627ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag       },
637ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag     },
647ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    };
657ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
667ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    static const struct {
677ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        SkScalar    fScaleX;
687ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        SkScalar    fSkewX;
697ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    } gScaleRec[] = {
707ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SK_Scalar1, 0 },
717ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SK_Scalar1/2, 0 },
727ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        // these two exercise obliquing (skew)
737ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SK_Scalar1, -SK_Scalar1/4 },
747ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        { SK_Scalar1/2, -SK_Scalar1/4 },
757ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    };
767ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
777ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    SkPaint paint;
787ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    char txt[] = "long.text.with.lots.of.dots.";
797ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
807ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
817ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        SkAutoTUnref<SkTypeface> face(SkTypeface::CreateFromName(faces[i], SkTypeface::kNormal));
827ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        paint.setTypeface(face);
837ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
847ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        for (size_t j = 0; j  < SK_ARRAY_COUNT(settings); j++) {
857ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org            paint.setHinting(settings[j].hinting);
867ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org            paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
877ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org            paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
887ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
897ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org            for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
907ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                paint.setTextScaleX(gScaleRec[k].fScaleX);
917ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                paint.setTextSkewX(gScaleRec[k].fSkewX);
927ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
937ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                test_cachedfont(reporter, paint);
947ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
957ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                SkRect bounds;
967ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
977ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                // For no hinting and light hinting this should take the
987ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                // optimized generateAdvance path.
997ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                SkScalar width1 = paint.measureText(txt, strlen(txt));
1007ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
1017ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                // Requesting the bounds forces a generateMetrics call.
1027ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                SkScalar width2 = paint.measureText(txt, strlen(txt), &bounds);
1037ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
1047ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                REPORTER_ASSERT(reporter, width1 == width2);
1057ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
1067ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                SkAutoTUnref<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
1077ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                SkScalar font_width1 = font->measureText(txt, strlen(txt), kUTF8_SkTextEncoding);
1087ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                // measureText not yet implemented...
1097ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org                REPORTER_ASSERT(reporter, font_width1 == -1);
1107ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org//                REPORTER_ASSERT(reporter, width1 == font_width1);
1117ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org            }
1127ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org        }
1137ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    }
1147ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org}
1157ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
1167ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.orgDEF_TEST(FontObj, reporter) {
1177ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org    test_cachedfont(reporter);
1187ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org}
1197ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org
1207ae034d651f3536457b9654e3a36a91716a18948commit-bot@chromium.org// need tests for SkStrSearch
121