blob: c5fe4cfcd2280ca1072379402754974ebed34486 [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"
8#include "SkView.h"
9#include "SkBlurMaskFilter.h"
10#include "SkCanvas.h"
reed@google.com021b4052011-09-28 15:24:00 +000011#include "SkColorShader.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000012#include "SkGradientShader.h"
13#include "SkGraphics.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000014#include "SkPath.h"
15#include "SkRandom.h"
16#include "SkRegion.h"
17#include "SkShader.h"
18#include "SkUtils.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000019#include "SkColorPriv.h"
20#include "SkColorFilter.h"
21#include "SkTime.h"
22#include "SkTypeface.h"
23#include "SkTextBox.h"
24#include "SkOSFile.h"
25#include "SkStream.h"
reed@android.com0bb6d062010-05-17 14:50:04 +000026
reed@google.com021b4052011-09-28 15:24:00 +000027extern void skia_set_text_gamma(float blackGamma, float whiteGamma);
28
bungeman@google.comb49d9892012-08-16 17:35:58 +000029#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
reed@google.comb6524272011-03-01 15:18:14 +000030extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
reed@google.comd6f5a812011-03-02 14:34:11 +000031#endif
reed@google.comb6524272011-03-01 15:18:14 +000032
reed@google.comd6f5a812011-03-02 14:34:11 +000033static const char gText[] =
rmistry@google.comae933ce2012-08-23 18:19:56 +000034 "When in the Course of human events it becomes necessary for one people "
35 "to dissolve the political bands which have connected them with another "
36 "and to assume among the powers of the earth, the separate and equal "
37 "station to which the Laws of Nature and of Nature's God entitle them, "
38 "a decent respect to the opinions of mankind requires that they should "
39 "declare the causes which impel them to the separation.";
reed@android.com0bb6d062010-05-17 14:50:04 +000040
reed@google.com17fb3872011-05-04 14:31:07 +000041class TextBoxView : public SampleView {
reed@google.comd6f5a812011-03-02 14:34:11 +000042public:
rmistry@google.comae933ce2012-08-23 18:19:56 +000043 TextBoxView() {
bungeman@google.comb49d9892012-08-16 17:35:58 +000044#if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
rmistry@google.comae933ce2012-08-23 18:19:56 +000045 LOGFONT lf;
46 sk_bzero(&lf, sizeof(lf));
47 lf.lfHeight = 9;
48 SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
49 lf.lfHeight = 12;
50 SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
51 // we assert that different sizes should not affect which face we get
52 SkASSERT(tf0 == tf1);
53 tf0->unref();
54 tf1->unref();
reed@google.comd6f5a812011-03-02 14:34:11 +000055#endif
reed@google.com021b4052011-09-28 15:24:00 +000056 }
reed@google.comd6f5a812011-03-02 14:34:11 +000057
reed@android.com0bb6d062010-05-17 14:50:04 +000058protected:
59 // overrides from SkEventSink
60 virtual bool onQuery(SkEvent* evt) {
61 if (SampleCode::TitleQ(*evt)) {
tfarina@chromium.org45369a32012-09-30 11:30:01 +000062 SampleCode::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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000073 SkScalar margin = 20;
reed@android.com0bb6d062010-05-17 14:50:04 +000074 SkTextBox tbox;
rmistry@google.comae933ce2012-08-23 18:19:56 +000075 tbox.setMode(SkTextBox::kLineBreak_Mode);
76 tbox.setBox(margin, margin,
77 w - margin, h - margin);
78 tbox.setSpacing(SkIntToScalar(3)/3, 0);
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);
rmistry@google.comae933ce2012-08-23 18:19:56 +000084 tbox.setText(gText, strlen(gText), paint);
reed@android.com0bb6d062010-05-17 14:50:04 +000085
rmistry@google.comae933ce2012-08-23 18:19:56 +000086 for (int i = 9; i < 24; i += 2) {
87 paint.setTextSize(SkIntToScalar(i));
88 tbox.draw(canvas);
89 canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing());
90 }
reed@android.com0bb6d062010-05-17 14:50:04 +000091 }
reed@google.comd6f5a812011-03-02 14:34:11 +000092
reed@google.com021b4052011-09-28 15:24:00 +000093 virtual void onDrawContent(SkCanvas* canvas) {
94 SkScalar width = this->width() / 3;
95 drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
96 canvas->translate(width, 0);
97 drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
98 canvas->translate(width, 0);
99 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
100 canvas->translate(0, this->height()/2);
101 drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
102 }
103
reed@android.com0bb6d062010-05-17 14:50:04 +0000104private:
reed@google.com4ee14332011-09-08 13:37:07 +0000105 typedef SampleView INHERITED;
reed@android.com0bb6d062010-05-17 14:50:04 +0000106};
107
108//////////////////////////////////////////////////////////////////////////////
109
110static SkView* MyFactory() { return new TextBoxView; }
111static SkViewRegister reg(MyFactory);