Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright 2014 Google Inc.
      3  *
      4  * Use of this source code is governed by a BSD-style license that can be
      5  * found in the LICENSE file.
      6  */
      7 
      8 #include "SkFont.h"
      9 #include "SkPaint.h"
     10 #include "SkTypeface.h"
     11 #include "Test.h"
     12 
     13 static bool is_use_nonlinear_metrics(const SkPaint& paint) {
     14     return !paint.isSubpixelText() && !paint.isLinearText();
     15 }
     16 
     17 static bool is_enable_auto_hints(const SkPaint& paint) {
     18     return paint.isAutohinted();
     19 }
     20 
     21 static bool is_enable_bytecode_hints(const SkPaint& paint) {
     22     return paint.getHinting() >= SkPaint::kFull_Hinting;
     23 }
     24 
     25 static void test_cachedfont(skiatest::Reporter* reporter, const SkPaint& paint) {
     26     sk_sp<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
     27 
     28     // Currently SkFont resolves null into the default, so only test if paint's is not null
     29     if (paint.getTypeface()) {
     30         REPORTER_ASSERT(reporter, font->getTypeface() == paint.getTypeface());
     31     }
     32     REPORTER_ASSERT(reporter, font->getSize() == paint.getTextSize());
     33     REPORTER_ASSERT(reporter, font->getScaleX() == paint.getTextScaleX());
     34     REPORTER_ASSERT(reporter, font->getSkewX() == paint.getTextSkewX());
     35 
     36     REPORTER_ASSERT(reporter, font->isVertical() == paint.isVerticalText());
     37     REPORTER_ASSERT(reporter, font->isEmbolden() == paint.isFakeBoldText());
     38 
     39     REPORTER_ASSERT(reporter, font->isUseNonLinearMetrics() == is_use_nonlinear_metrics(paint));
     40     REPORTER_ASSERT(reporter, font->isEnableAutoHints() == is_enable_auto_hints(paint));
     41     REPORTER_ASSERT(reporter, font->isEnableByteCodeHints() == is_enable_bytecode_hints(paint));
     42 }
     43 
     44 static void test_cachedfont(skiatest::Reporter* reporter) {
     45     static const char* const faces[] = {
     46         nullptr,   // default font
     47         "Arial", "Times", "Times New Roman", "Helvetica", "Courier",
     48         "Courier New", "Verdana", "monospace",
     49     };
     50 
     51     static const struct {
     52         SkPaint::Hinting    hinting;
     53         unsigned            flags;
     54     } settings[] = {
     55         { SkPaint::kNo_Hinting,     0                               },
     56         { SkPaint::kNo_Hinting,     SkPaint::kLinearText_Flag       },
     57         { SkPaint::kNo_Hinting,     SkPaint::kSubpixelText_Flag     },
     58         { SkPaint::kSlight_Hinting, 0                               },
     59         { SkPaint::kSlight_Hinting, SkPaint::kLinearText_Flag       },
     60         { SkPaint::kSlight_Hinting, SkPaint::kSubpixelText_Flag     },
     61         { SkPaint::kNormal_Hinting, 0                               },
     62         { SkPaint::kNormal_Hinting, SkPaint::kLinearText_Flag       },
     63         { SkPaint::kNormal_Hinting, SkPaint::kSubpixelText_Flag     },
     64     };
     65 
     66     static const struct {
     67         SkScalar    fScaleX;
     68         SkScalar    fSkewX;
     69     } gScaleRec[] = {
     70         { SK_Scalar1, 0 },
     71         { SK_Scalar1/2, 0 },
     72         // these two exercise obliquing (skew)
     73         { SK_Scalar1, -SK_Scalar1/4 },
     74         { SK_Scalar1/2, -SK_Scalar1/4 },
     75     };
     76 
     77     SkPaint paint;
     78     char txt[] = "long.text.with.lots.of.dots.";
     79 
     80     for (size_t i = 0; i < SK_ARRAY_COUNT(faces); i++) {
     81         paint.setTypeface(SkTypeface::MakeFromName(faces[i], SkFontStyle()));
     82 
     83         for (size_t j = 0; j  < SK_ARRAY_COUNT(settings); j++) {
     84             paint.setHinting(settings[j].hinting);
     85             paint.setLinearText((settings[j].flags & SkPaint::kLinearText_Flag) != 0);
     86             paint.setSubpixelText((settings[j].flags & SkPaint::kSubpixelText_Flag) != 0);
     87 
     88             for (size_t k = 0; k < SK_ARRAY_COUNT(gScaleRec); ++k) {
     89                 paint.setTextScaleX(gScaleRec[k].fScaleX);
     90                 paint.setTextSkewX(gScaleRec[k].fSkewX);
     91 
     92                 test_cachedfont(reporter, paint);
     93 
     94                 SkRect bounds;
     95 
     96                 // For no hinting and light hinting this should take the
     97                 // optimized generateAdvance path.
     98                 SkScalar width1 = paint.measureText(txt, strlen(txt));
     99 
    100                 // Requesting the bounds forces a generateMetrics call.
    101                 SkScalar width2 = paint.measureText(txt, strlen(txt), &bounds);
    102 
    103                 REPORTER_ASSERT(reporter, width1 == width2);
    104 
    105                 sk_sp<SkFont> font(SkFont::Testing_CreateFromPaint(paint));
    106                 SkScalar font_width1 = font->measureText(txt, strlen(txt), kUTF8_SkTextEncoding);
    107                 // measureText not yet implemented...
    108                 REPORTER_ASSERT(reporter, font_width1 == -1);
    109 //                REPORTER_ASSERT(reporter, width1 == font_width1);
    110             }
    111         }
    112     }
    113 }
    114 
    115 DEF_TEST(FontObj, reporter) {
    116     test_cachedfont(reporter);
    117 }
    118 
    119 // need tests for SkStrSearch
    120