blob: aedeaf6ce86b6fc3e5c46b4a373dd9134767f530 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com0bb6d062010-05-17 14:50:04 +00008#include "SampleCode.h"
9#include "SkView.h"
10#include "SkBlurMaskFilter.h"
11#include "SkCanvas.h"
reed@google.com021b4052011-09-28 15:24:00 +000012#include "SkColorShader.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000013#include "SkGradientShader.h"
14#include "SkGraphics.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000015#include "SkPath.h"
16#include "SkRandom.h"
17#include "SkRegion.h"
18#include "SkShader.h"
19#include "SkUtils.h"
20#include "SkXfermode.h"
21#include "SkColorPriv.h"
22#include "SkColorFilter.h"
23#include "SkTime.h"
24#include "SkTypeface.h"
25#include "SkTextBox.h"
26#include "SkOSFile.h"
27#include "SkStream.h"
28#include "SkKey.h"
29
reed@google.com021b4052011-09-28 15:24:00 +000030extern void skia_set_text_gamma(float blackGamma, float whiteGamma);
31
bungeman@google.comb49d9892012-08-16 17:35:58 +000032#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
reed@google.comb6524272011-03-01 15:18:14 +000033extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
reed@google.comd6f5a812011-03-02 14:34:11 +000034#endif
reed@google.comb6524272011-03-01 15:18:14 +000035
reed@google.comd6f5a812011-03-02 14:34:11 +000036static const char gText[] =
rmistry@google.comae933ce2012-08-23 18:19:56 +000037 "When in the Course of human events it becomes necessary for one people "
38 "to dissolve the political bands which have connected them with another "
39 "and to assume among the powers of the earth, the separate and equal "
40 "station to which the Laws of Nature and of Nature's God entitle them, "
41 "a decent respect to the opinions of mankind requires that they should "
42 "declare the causes which impel them to the separation.";
reed@android.com0bb6d062010-05-17 14:50:04 +000043
reed@google.com17fb3872011-05-04 14:31:07 +000044class TextBoxView : public SampleView {
reed@google.comd6f5a812011-03-02 14:34:11 +000045public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000046 TextBoxView() {
bungeman@google.comb49d9892012-08-16 17:35:58 +000047#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
rmistry@google.comae933ce2012-08-23 18:19:56 +000048 LOGFONT lf;
49 sk_bzero(&lf, sizeof(lf));
50 lf.lfHeight = 9;
51 SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
52 lf.lfHeight = 12;
53 SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
54 // we assert that different sizes should not affect which face we get
55 SkASSERT(tf0 == tf1);
56 tf0->unref();
57 tf1->unref();
reed@google.comd6f5a812011-03-02 14:34:11 +000058#endif
reed@google.com021b4052011-09-28 15:24:00 +000059 }
reed@google.comd6f5a812011-03-02 14:34:11 +000060
reed@android.com0bb6d062010-05-17 14:50:04 +000061protected:
62 // overrides from SkEventSink
63 virtual bool onQuery(SkEvent* evt) {
64 if (SampleCode::TitleQ(*evt)) {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000065 SampleCode::TitleR(evt, "TextBox");
reed@android.com0bb6d062010-05-17 14:50:04 +000066 return true;
67 }
68 return this->INHERITED::onQuery(evt);
69 }
reed@google.comd6f5a812011-03-02 14:34:11 +000070
reed@google.com021b4052011-09-28 15:24:00 +000071 void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) {
72 SkAutoCanvasRestore acr(canvas, true);
73
74 canvas->clipRect(SkRect::MakeWH(w, h));
75 canvas->drawColor(bg);
rmistry@google.comae933ce2012-08-23 18:19:56 +000076 SkScalar margin = 20;
reed@android.com0bb6d062010-05-17 14:50:04 +000077 SkTextBox tbox;
rmistry@google.comae933ce2012-08-23 18:19:56 +000078 tbox.setMode(SkTextBox::kLineBreak_Mode);
79 tbox.setBox(margin, margin,
80 w - margin, h - margin);
81 tbox.setSpacing(SkIntToScalar(3)/3, 0);
reed@android.com0bb6d062010-05-17 14:50:04 +000082
rmistry@google.comae933ce2012-08-23 18:19:56 +000083 SkPaint paint;
84 paint.setAntiAlias(true);
reed@google.com261b8e22011-04-14 17:53:24 +000085 paint.setLCDRenderText(true);
reed@google.com021b4052011-09-28 15:24:00 +000086 paint.setColor(fg);
rmistry@google.comae933ce2012-08-23 18:19:56 +000087 tbox.setText(gText, strlen(gText), paint);
reed@android.com0bb6d062010-05-17 14:50:04 +000088
rmistry@google.comae933ce2012-08-23 18:19:56 +000089 for (int i = 9; i < 24; i += 2) {
90 paint.setTextSize(SkIntToScalar(i));
91 tbox.draw(canvas);
92 canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing());
93 }
reed@android.com0bb6d062010-05-17 14:50:04 +000094 }
reed@google.comd6f5a812011-03-02 14:34:11 +000095
reed@google.com021b4052011-09-28 15:24:00 +000096 virtual void onDrawContent(SkCanvas* canvas) {
97 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);