Home | History | Annotate | Download | only in gm
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 /* Tests text vertical text rendering with different fonts and centering.
     11  */
     12 
     13 #include "gm.h"
     14 #include "SkCanvas.h"
     15 #include "SkTypeface.h"
     16 
     17 namespace skiagm {
     18 
     19 class VertText2GM : public GM {
     20 public:
     21     VertText2GM()
     22         : fProp(nullptr)
     23         , fMono(nullptr) {
     24     }
     25 
     26     virtual ~VertText2GM() {
     27         SkSafeUnref(fProp);
     28         SkSafeUnref(fMono);
     29     }
     30 
     31 protected:
     32     void onOnceBeforeDraw() override {
     33         const int pointSize = 24;
     34         textHeight = SkIntToScalar(pointSize);
     35         fProp = SkTypeface::CreateFromName(sk_tool_utils::platform_font_name("sans-serif"),
     36                 SkTypeface::kNormal);
     37         fMono = SkTypeface::CreateFromName(sk_tool_utils::platform_font_name("monospace"),
     38                 SkTypeface::kNormal);
     39     }
     40 
     41     SkString onShortName() override {
     42         SkString name("verttext2");
     43         name.append(sk_tool_utils::major_platform_os_name());
     44         return name;
     45     }
     46 
     47     SkISize onISize() override { return SkISize::Make(640, 480); }
     48 
     49     void onDraw(SkCanvas* canvas) override {
     50         for (int i = 0; i < 3; ++i) {
     51             SkPaint paint;
     52             paint.setColor(SK_ColorRED);
     53             paint.setAntiAlias(true);
     54             y = textHeight;
     55             canvas->drawLine(0, SkIntToScalar(10),
     56                     SkIntToScalar(110), SkIntToScalar(10), paint);
     57             canvas->drawLine(0, SkIntToScalar(240),
     58                     SkIntToScalar(110), SkIntToScalar(240), paint);
     59             canvas->drawLine(0, SkIntToScalar(470),
     60                     SkIntToScalar(110), SkIntToScalar(470), paint);
     61             drawText(canvas, SkString("Proportional / Top Aligned"),
     62                      fProp,  SkPaint::kLeft_Align);
     63             drawText(canvas, SkString("<   Proportional / Centered   >"),
     64                      fProp,  SkPaint::kCenter_Align);
     65             drawText(canvas, SkString("Monospaced / Top Aligned"),
     66                      fMono, SkPaint::kLeft_Align);
     67             drawText(canvas, SkString("<    Monospaced / Centered    >"),
     68                      fMono, SkPaint::kCenter_Align);
     69             canvas->rotate(SkIntToScalar(-15));
     70             canvas->translate(textHeight * 4, SkIntToScalar(50));
     71             if (i > 0) {
     72                 canvas->translate(0, SkIntToScalar(50));
     73             }
     74         }
     75     }
     76 
     77     void drawText(SkCanvas* canvas, const SkString& string,
     78                   SkTypeface* family, SkPaint::Align alignment) {
     79         SkPaint paint;
     80         paint.setColor(SK_ColorBLACK);
     81         paint.setAntiAlias(true);
     82         paint.setVerticalText(true);
     83         paint.setTextAlign(alignment);
     84         paint.setTypeface(family);
     85         paint.setTextSize(textHeight);
     86 
     87         canvas->drawText(string.c_str(), string.size(), y,
     88                 SkIntToScalar(alignment == SkPaint::kLeft_Align ? 10 : 240),
     89                 paint);
     90         y += textHeight;
     91     }
     92 
     93 private:
     94     typedef GM INHERITED;
     95     SkScalar y, textHeight;
     96     SkTypeface* fProp;
     97     SkTypeface* fMono;
     98 };
     99 
    100 ///////////////////////////////////////////////////////////////////////////////
    101 
    102 static GM* MyFactory(void*) { return new VertText2GM; }
    103 static GMRegistry reg(MyFactory);
    104 
    105 }
    106