blob: 3b67af41bbc977eefde91472baa2520386fb28d9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 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 */
Ben Wagnerb2c4ea62018-08-08 11:36:17 -04007#include "Sample.h"
Mike Reed193d3a02018-02-08 15:45:51 -05008
reed@android.com0bb6d062010-05-17 14:50:04 +00009#include "SkBlurMaskFilter.h"
10#include "SkCanvas.h"
Mike Reed193d3a02018-02-08 15:45:51 -050011#include "SkColorFilter.h"
12#include "SkColorPriv.h"
reed@google.com021b4052011-09-28 15:24:00 +000013#include "SkColorShader.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000014#include "SkGradientShader.h"
15#include "SkGraphics.h"
Mike Reed193d3a02018-02-08 15:45:51 -050016#include "SkOSFile.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000017#include "SkPath.h"
18#include "SkRandom.h"
19#include "SkRegion.h"
20#include "SkShader.h"
Mike Reed193d3a02018-02-08 15:45:51 -050021#include "SkShaper.h"
22#include "SkStream.h"
23#include "SkTextBlob.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000024#include "SkTime.h"
25#include "SkTypeface.h"
Hal Canaryea60b952018-08-21 11:45:46 -040026#include "SkUTF.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000027
reed@google.com021b4052011-09-28 15:24:00 +000028extern void skia_set_text_gamma(float blackGamma, float whiteGamma);
29
bungeman@google.comb49d9892012-08-16 17:35:58 +000030#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
reed@google.comb6524272011-03-01 15:18:14 +000031extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
reed@google.comd6f5a812011-03-02 14:34:11 +000032#endif
reed@google.comb6524272011-03-01 15:18:14 +000033
reed@google.comd6f5a812011-03-02 14:34:11 +000034static const char gText[] =
rmistry@google.comae933ce2012-08-23 18:19:56 +000035 "When in the Course of human events it becomes necessary for one people "
36 "to dissolve the political bands which have connected them with another "
37 "and to assume among the powers of the earth, the separate and equal "
38 "station to which the Laws of Nature and of Nature's God entitle them, "
39 "a decent respect to the opinions of mankind requires that they should "
40 "declare the causes which impel them to the separation.";
reed@android.com0bb6d062010-05-17 14:50:04 +000041
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040042class TextBoxView : public Sample {
reed@google.comd6f5a812011-03-02 14:34:11 +000043public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000044 TextBoxView() {
bungeman@google.comb49d9892012-08-16 17:35:58 +000045#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
rmistry@google.comae933ce2012-08-23 18:19:56 +000046 LOGFONT lf;
47 sk_bzero(&lf, sizeof(lf));
48 lf.lfHeight = 9;
49 SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
50 lf.lfHeight = 12;
51 SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
52 // we assert that different sizes should not affect which face we get
53 SkASSERT(tf0 == tf1);
54 tf0->unref();
55 tf1->unref();
reed@google.comd6f5a812011-03-02 14:34:11 +000056#endif
reed@google.com021b4052011-09-28 15:24:00 +000057 }
reed@google.comd6f5a812011-03-02 14:34:11 +000058
reed@android.com0bb6d062010-05-17 14:50:04 +000059protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040060 bool onQuery(Sample::Event* evt) override {
61 if (Sample::TitleQ(*evt)) {
62 Sample::TitleR(evt, "TextBox");
reed@android.com0bb6d062010-05-17 14:50:04 +000063 return true;
64 }
65 return this->INHERITED::onQuery(evt);
66 }
reed@google.comd6f5a812011-03-02 14:34:11 +000067
reed@google.com021b4052011-09-28 15:24:00 +000068 void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
69 SkAutoCanvasRestore acr(canvas, true);
70
71 canvas->clipRect(SkRect::MakeWH(w, h));
72 canvas->drawColor(bg);
Mike Reed193d3a02018-02-08 15:45:51 -050073
74 SkShaper shaper(nullptr);
75
rmistry@google.comae933ce2012-08-23 18:19:56 +000076 SkScalar margin = 20;
reed@android.com0bb6d062010-05-17 14:50:04 +000077
rmistry@google.comae933ce2012-08-23 18:19:56 +000078 SkPaint paint;
reed@google.com021b4052011-09-28 15:24:00 +000079 paint.setColor(fg);
reed@android.com0bb6d062010-05-17 14:50:04 +000080
rmistry@google.comae933ce2012-08-23 18:19:56 +000081 for (int i = 9; i < 24; i += 2) {
Florin Malita950243d2019-01-11 11:08:35 -050082 SkTextBlobBuilderRunHandler builder;
Hal Canary4484b8f2019-01-08 14:00:08 -050083 SkFont font(nullptr, SkIntToScalar(i));
84 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
85
Hal Canary2a1848d2018-11-26 17:23:24 -050086 SkPoint end = shaper.shape(&builder, font, gText, strlen(gText), true,
Mike Reed193d3a02018-02-08 15:45:51 -050087 { margin, margin }, w - margin);
Florin Malita9867f612018-12-12 10:54:49 -050088 canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint);
Mike Reed193d3a02018-02-08 15:45:51 -050089
90 canvas->translate(0, end.y());
rmistry@google.comae933ce2012-08-23 18:19:56 +000091 }
reed@android.com0bb6d062010-05-17 14:50:04 +000092 }
reed@google.comd6f5a812011-03-02 14:34:11 +000093
Mike Reed193d3a02018-02-08 15:45:51 -050094 void onDrawContent(SkCanvas* canvas) override {
reed@google.com021b4052011-09-28 15:24:00 +000095 SkScalar width = this->width() / 3;
96 drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
97 canvas->translate(width, 0);
98 drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
99 canvas->translate(width, 0);
100 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
101 canvas->translate(0, this->height()/2);
102 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
103 }
104
reed@android.com0bb6d062010-05-17 14:50:04 +0000105private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400106 typedef Sample INHERITED;
reed@android.com0bb6d062010-05-17 14:50:04 +0000107};
108
109//////////////////////////////////////////////////////////////////////////////
110
Ben Wagnerb2c4ea62018-08-08 11:36:17 -0400111DEF_SAMPLE( return new TextBoxView(); )