blob: 058fc4e3f90b41fe40dcaa0915f7d0d047232e50 [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"
12#include "SkGradientShader.h"
13#include "SkGraphics.h"
14#include "SkImageDecoder.h"
15#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.comd6f5a812011-03-02 14:34:11 +000030#ifdef SK_BUILD_FOR_WIN
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[] =
reed@android.com0bb6d062010-05-17 14:50:04 +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.";
41
reed@google.com17fb3872011-05-04 14:31:07 +000042class TextBoxView : public SampleView {
reed@google.comd6f5a812011-03-02 14:34:11 +000043public:
reed@android.com0bb6d062010-05-17 14:50:04 +000044 TextBoxView() {
reed@google.comd6f5a812011-03-02 14:34:11 +000045#ifdef SK_BUILD_FOR_WIN
reed@google.comb6524272011-03-01 15:18:14 +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@android.com0bb6d062010-05-17 14:50:04 +000057 }
reed@google.comd6f5a812011-03-02 14:34:11 +000058
reed@android.com0bb6d062010-05-17 14:50:04 +000059protected:
60 // overrides from SkEventSink
61 virtual bool onQuery(SkEvent* evt) {
62 if (SampleCode::TitleQ(*evt)) {
63 SkString str("TextBox");
64 SampleCode::TitleR(evt, str.c_str());
65 return true;
66 }
67 return this->INHERITED::onQuery(evt);
68 }
reed@google.comd6f5a812011-03-02 14:34:11 +000069
reed@google.com17fb3872011-05-04 14:31:07 +000070 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com0bb6d062010-05-17 14:50:04 +000071 SkScalar margin = 20;
72 SkTextBox tbox;
73 tbox.setMode(SkTextBox::kLineBreak_Mode);
74 tbox.setBox(margin, margin,
75 this->width() - margin, this->height() - margin);
76 tbox.setSpacing(SkIntToScalar(3)/3, 0);
77
78 SkPaint paint;
79 paint.setAntiAlias(true);
reed@google.com261b8e22011-04-14 17:53:24 +000080 paint.setLCDRenderText(true);
reed@google.comb6524272011-03-01 15:18:14 +000081 tbox.setText(gText, strlen(gText), paint);
reed@android.com0bb6d062010-05-17 14:50:04 +000082
reed@google.comb6524272011-03-01 15:18:14 +000083 for (int i = 9; i < 24; i += 2) {
84 paint.setTextSize(SkIntToScalar(i));
85 tbox.draw(canvas);
86 canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing());
reed@android.com0bb6d062010-05-17 14:50:04 +000087 }
reed@android.com0bb6d062010-05-17 14:50:04 +000088 }
reed@google.comd6f5a812011-03-02 14:34:11 +000089
reed@android.com0bb6d062010-05-17 14:50:04 +000090private:
reed@android.com0bb6d062010-05-17 14:50:04 +000091 typedef SkView INHERITED;
92};
93
94//////////////////////////////////////////////////////////////////////////////
95
96static SkView* MyFactory() { return new TextBoxView; }
97static SkViewRegister reg(MyFactory);
98