blob: 3c1e9eb878d40116fa3692c2392c06dfb6c8b8df [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 */
reed@android.com0bb6d062010-05-17 14:50:04 +00007#include "SampleCode.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"
Mike Reed193d3a02018-02-08 15:45:51 -050026#include "SkUtils.h"
27#include "SkView.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000028
reed@google.com021b4052011-09-28 15:24:00 +000029extern void skia_set_text_gamma(float blackGamma, float whiteGamma);
30
bungeman@google.comb49d9892012-08-16 17:35:58 +000031#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
reed@google.comb6524272011-03-01 15:18:14 +000032extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
reed@google.comd6f5a812011-03-02 14:34:11 +000033#endif
reed@google.comb6524272011-03-01 15:18:14 +000034
reed@google.comd6f5a812011-03-02 14:34:11 +000035static const char gText[] =
rmistry@google.comae933ce2012-08-23 18:19:56 +000036 "When in the Course of human events it becomes necessary for one people "
37 "to dissolve the political bands which have connected them with another "
38 "and to assume among the powers of the earth, the separate and equal "
39 "station to which the Laws of Nature and of Nature's God entitle them, "
40 "a decent respect to the opinions of mankind requires that they should "
41 "declare the causes which impel them to the separation.";
reed@android.com0bb6d062010-05-17 14:50:04 +000042
reed@google.com17fb3872011-05-04 14:31:07 +000043class TextBoxView : public SampleView {
reed@google.comd6f5a812011-03-02 14:34:11 +000044public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000045 TextBoxView() {
bungeman@google.comb49d9892012-08-16 17:35:58 +000046#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
rmistry@google.comae933ce2012-08-23 18:19:56 +000047 LOGFONT lf;
48 sk_bzero(&lf, sizeof(lf));
49 lf.lfHeight = 9;
50 SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
51 lf.lfHeight = 12;
52 SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
53 // we assert that different sizes should not affect which face we get
54 SkASSERT(tf0 == tf1);
55 tf0->unref();
56 tf1->unref();
reed@google.comd6f5a812011-03-02 14:34:11 +000057#endif
reed@google.com021b4052011-09-28 15:24:00 +000058 }
reed@google.comd6f5a812011-03-02 14:34:11 +000059
reed@android.com0bb6d062010-05-17 14:50:04 +000060protected:
61 // overrides from SkEventSink
Mike Reed193d3a02018-02-08 15:45:51 -050062 bool onQuery(SkEvent* evt) override {
reed@android.com0bb6d062010-05-17 14:50:04 +000063 if (SampleCode::TitleQ(*evt)) {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000064 SampleCode::TitleR(evt, "TextBox");
reed@android.com0bb6d062010-05-17 14:50:04 +000065 return true;
66 }
67 return this->INHERITED::onQuery(evt);
68 }
reed@google.comd6f5a812011-03-02 14:34:11 +000069
reed@google.com021b4052011-09-28 15:24:00 +000070 void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
71 SkAutoCanvasRestore acr(canvas, true);
72
73 canvas->clipRect(SkRect::MakeWH(w, h));
74 canvas->drawColor(bg);
Mike Reed193d3a02018-02-08 15:45:51 -050075
76 SkShaper shaper(nullptr);
77
rmistry@google.comae933ce2012-08-23 18:19:56 +000078 SkScalar margin = 20;
reed@android.com0bb6d062010-05-17 14:50:04 +000079
rmistry@google.comae933ce2012-08-23 18:19:56 +000080 SkPaint paint;
81 paint.setAntiAlias(true);
reed@google.com261b8e22011-04-14 17:53:24 +000082 paint.setLCDRenderText(true);
reed@google.com021b4052011-09-28 15:24:00 +000083 paint.setColor(fg);
reed@android.com0bb6d062010-05-17 14:50:04 +000084
rmistry@google.comae933ce2012-08-23 18:19:56 +000085 for (int i = 9; i < 24; i += 2) {
Mike Reed193d3a02018-02-08 15:45:51 -050086 SkTextBlobBuilder builder;
rmistry@google.comae933ce2012-08-23 18:19:56 +000087 paint.setTextSize(SkIntToScalar(i));
Mike Reed193d3a02018-02-08 15:45:51 -050088 SkPoint end = shaper.shape(&builder, paint, gText, strlen(gText), true,
89 { margin, margin }, w - margin);
90 canvas->drawTextBlob(builder.make(), 0, 0, paint);
91
92 canvas->translate(0, end.y());
rmistry@google.comae933ce2012-08-23 18:19:56 +000093 }
reed@android.com0bb6d062010-05-17 14:50:04 +000094 }
reed@google.comd6f5a812011-03-02 14:34:11 +000095
Mike Reed193d3a02018-02-08 15:45:51 -050096 void onDrawContent(SkCanvas* canvas) override {
reed@google.com021b4052011-09-28 15:24:00 +000097 SkScalar width = this->width() / 3;
98 drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
99 canvas->translate(width, 0);
100 drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
101 canvas->translate(width, 0);
102 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
103 canvas->translate(0, this->height()/2);
104 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
105 }
106
reed@android.com0bb6d062010-05-17 14:50:04 +0000107private:
reed@google.com4ee14332011-09-08 13:37:07 +0000108 typedef SampleView INHERITED;
reed@android.com0bb6d062010-05-17 14:50:04 +0000109};
110
111//////////////////////////////////////////////////////////////////////////////
112
113static SkView* MyFactory() { return new TextBoxView; }
114static SkViewRegister reg(MyFactory);