141868fe5625fc3bd70daa3f461c881b5db6a9265bungeman/*
241868fe5625fc3bd70daa3f461c881b5db6a9265bungeman * Copyright 2015 Google Inc.
341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman *
441868fe5625fc3bd70daa3f461c881b5db6a9265bungeman * Use of this source code is governed by a BSD-style license that can be
541868fe5625fc3bd70daa3f461c881b5db6a9265bungeman * found in the LICENSE file.
641868fe5625fc3bd70daa3f461c881b5db6a9265bungeman */
741868fe5625fc3bd70daa3f461c881b5db6a9265bungeman#include "gm.h"
841868fe5625fc3bd70daa3f461c881b5db6a9265bungeman#include "Resources.h"
941868fe5625fc3bd70daa3f461c881b5db6a9265bungeman#include "SkFixed.h"
1041868fe5625fc3bd70daa3f461c881b5db6a9265bungeman#include "SkFontDescriptor.h"
1141868fe5625fc3bd70daa3f461c881b5db6a9265bungeman#include "SkFontMgr.h"
1241868fe5625fc3bd70daa3f461c881b5db6a9265bungeman#include "SkTypeface.h"
1341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
1441868fe5625fc3bd70daa3f461c881b5db6a9265bungemannamespace skiagm {
1541868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
1641868fe5625fc3bd70daa3f461c881b5db6a9265bungemanclass FontScalerDistortableGM : public GM {
1741868fe5625fc3bd70daa3f461c881b5db6a9265bungemanpublic:
1841868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    FontScalerDistortableGM() {
1941868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        this->setBGColor(0xFFFFFFFF);
2041868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    }
2141868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
2241868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    virtual ~FontScalerDistortableGM() { }
2341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
2441868fe5625fc3bd70daa3f461c881b5db6a9265bungemanprotected:
2541868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
2641868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    SkString onShortName() override {
2741868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        return SkString("fontscalerdistortable");
2841868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    }
2941868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
3041868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    SkISize onISize() override {
3141868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        return SkISize::Make(550, 700);
3241868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    }
3341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
3441868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    static void rotate_about(SkCanvas* canvas, SkScalar degrees, SkScalar px, SkScalar py) {
3541868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        canvas->translate(px, py);
3641868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        canvas->rotate(degrees);
3741868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        canvas->translate(-px, -py);
3841868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    }
3941868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
4041868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    void onDraw(SkCanvas* canvas) override {
4141868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        SkPaint paint;
4241868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        paint.setAntiAlias(true);
4341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        paint.setLCDRenderText(true);
44f6c7107d0385cc2b556802354b93b7dcff61570dbungeman        SkAutoTUnref<SkFontMgr> fontMgr(SkFontMgr::RefDefault());
4541868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
4641868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        SkAutoTDelete<SkStreamAsset> distortable(GetResourceAsStream("/fonts/Distortable.ttf"));
4741868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        if (!distortable) {
4841868fe5625fc3bd70daa3f461c881b5db6a9265bungeman            return;
4941868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        }
5041868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        const char* text = "abc";
5141868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        const size_t textLen = strlen(text);
5241868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
5341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        for (int j = 0; j < 2; ++j) {
5441868fe5625fc3bd70daa3f461c881b5db6a9265bungeman            for (int i = 0; i < 5; ++i) {
5541868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                SkScalar x = SkIntToScalar(10);
5641868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                SkScalar y = SkIntToScalar(20);
5741868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
58f6c7107d0385cc2b556802354b93b7dcff61570dbungeman                SkFourByteTag tag = SkSetFourByteTag('w','g','h','t');
59f6c7107d0385cc2b556802354b93b7dcff61570dbungeman                SkScalar styleValue = SkDoubleToScalar(0.5 + (5*j + i) * ((2.0 - 0.5) / (2 * 5)));
60f6c7107d0385cc2b556802354b93b7dcff61570dbungeman                SkFontMgr::FontParameters::Axis axes[] = { { tag, styleValue } };
61f6c7107d0385cc2b556802354b93b7dcff61570dbungeman                SkAutoTUnref<SkTypeface> typeface(fontMgr->createFromStream(
62f6c7107d0385cc2b556802354b93b7dcff61570dbungeman                    distortable->duplicate(), SkFontMgr::FontParameters().setAxes(axes, 1)));
6341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                paint.setTypeface(typeface);
6441868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
6541868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                SkAutoCanvasRestore acr(canvas, true);
6641868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                canvas->translate(SkIntToScalar(30 + i * 100), SkIntToScalar(20));
6741868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                rotate_about(canvas, SkIntToScalar(i * 5), x, y * 10);
6841868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
6941868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                {
7041868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                    SkPaint p;
7141868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                    p.setAntiAlias(true);
7241868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                    SkRect r;
7341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                    r.set(x - SkIntToScalar(3), SkIntToScalar(15),
7441868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                          x - SkIntToScalar(1), SkIntToScalar(280));
7541868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                    canvas->drawRect(r, p);
7641868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                }
7741868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
7841868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                for (int ps = 6; ps <= 22; ps++) {
7941868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                    paint.setTextSize(SkIntToScalar(ps));
8041868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                    canvas->drawText(text, textLen, x, y, paint);
8196fcdcc219d2a0d3579719b84b28bede76efba64halcanary                    y += paint.getFontMetrics(nullptr);
8241868fe5625fc3bd70daa3f461c881b5db6a9265bungeman                }
8341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman            }
8441868fe5625fc3bd70daa3f461c881b5db6a9265bungeman            canvas->translate(0, SkIntToScalar(360));
8541868fe5625fc3bd70daa3f461c881b5db6a9265bungeman            paint.setSubpixelText(true);
8641868fe5625fc3bd70daa3f461c881b5db6a9265bungeman        }
8741868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    }
8841868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
8941868fe5625fc3bd70daa3f461c881b5db6a9265bungemanprivate:
9041868fe5625fc3bd70daa3f461c881b5db6a9265bungeman    typedef GM INHERITED;
9141868fe5625fc3bd70daa3f461c881b5db6a9265bungeman};
9241868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
9341868fe5625fc3bd70daa3f461c881b5db6a9265bungeman//////////////////////////////////////////////////////////////////////////////
9441868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
9541868fe5625fc3bd70daa3f461c881b5db6a9265bungemanstatic GM* MyFactory(void*) { return new FontScalerDistortableGM; }
9641868fe5625fc3bd70daa3f461c881b5db6a9265bungemanstatic GMRegistry reg(MyFactory);
9741868fe5625fc3bd70daa3f461c881b5db6a9265bungeman
9841868fe5625fc3bd70daa3f461c881b5db6a9265bungeman}
99