blob: 37a6be09361388b324bcc8bd9d777b965a66ad81 [file] [log] [blame]
reed@android.com0bb6d062010-05-17 14:50:04 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkBlurMaskFilter.h"
4#include "SkCanvas.h"
5#include "SkGradientShader.h"
6#include "SkGraphics.h"
7#include "SkImageDecoder.h"
8#include "SkPath.h"
9#include "SkRandom.h"
10#include "SkRegion.h"
11#include "SkShader.h"
12#include "SkUtils.h"
13#include "SkXfermode.h"
14#include "SkColorPriv.h"
15#include "SkColorFilter.h"
16#include "SkTime.h"
17#include "SkTypeface.h"
18#include "SkTextBox.h"
19#include "SkOSFile.h"
20#include "SkStream.h"
21#include "SkKey.h"
22
reed@google.comd6f5a812011-03-02 14:34:11 +000023#ifdef SK_BUILD_FOR_WIN
reed@google.comb6524272011-03-01 15:18:14 +000024extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
reed@google.comd6f5a812011-03-02 14:34:11 +000025#endif
reed@google.comb6524272011-03-01 15:18:14 +000026
reed@google.comd6f5a812011-03-02 14:34:11 +000027static const char gText[] =
reed@android.com0bb6d062010-05-17 14:50:04 +000028 "When in the Course of human events it becomes necessary for one people "
29 "to dissolve the political bands which have connected them with another "
30 "and to assume among the powers of the earth, the separate and equal "
31 "station to which the Laws of Nature and of Nature's God entitle them, "
32 "a decent respect to the opinions of mankind requires that they should "
33 "declare the causes which impel them to the separation.";
34
reed@google.com17fb3872011-05-04 14:31:07 +000035class TextBoxView : public SampleView {
reed@google.comd6f5a812011-03-02 14:34:11 +000036public:
reed@android.com0bb6d062010-05-17 14:50:04 +000037 TextBoxView() {
reed@google.comd6f5a812011-03-02 14:34:11 +000038#ifdef SK_BUILD_FOR_WIN
reed@google.comb6524272011-03-01 15:18:14 +000039 LOGFONT lf;
40 sk_bzero(&lf, sizeof(lf));
41 lf.lfHeight = 9;
42 SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
43 lf.lfHeight = 12;
44 SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
45 // we assert that different sizes should not affect which face we get
46 SkASSERT(tf0 == tf1);
47 tf0->unref();
48 tf1->unref();
reed@google.comd6f5a812011-03-02 14:34:11 +000049#endif
reed@android.com0bb6d062010-05-17 14:50:04 +000050 }
reed@google.comd6f5a812011-03-02 14:34:11 +000051
reed@android.com0bb6d062010-05-17 14:50:04 +000052protected:
53 // overrides from SkEventSink
54 virtual bool onQuery(SkEvent* evt) {
55 if (SampleCode::TitleQ(*evt)) {
56 SkString str("TextBox");
57 SampleCode::TitleR(evt, str.c_str());
58 return true;
59 }
60 return this->INHERITED::onQuery(evt);
61 }
reed@google.comd6f5a812011-03-02 14:34:11 +000062
reed@google.com17fb3872011-05-04 14:31:07 +000063 virtual void onDrawContent(SkCanvas* canvas) {
reed@android.com0bb6d062010-05-17 14:50:04 +000064 SkScalar margin = 20;
65 SkTextBox tbox;
66 tbox.setMode(SkTextBox::kLineBreak_Mode);
67 tbox.setBox(margin, margin,
68 this->width() - margin, this->height() - margin);
69 tbox.setSpacing(SkIntToScalar(3)/3, 0);
70
71 SkPaint paint;
72 paint.setAntiAlias(true);
reed@google.com261b8e22011-04-14 17:53:24 +000073 paint.setLCDRenderText(true);
reed@google.comb6524272011-03-01 15:18:14 +000074 tbox.setText(gText, strlen(gText), paint);
reed@android.com0bb6d062010-05-17 14:50:04 +000075
reed@google.comb6524272011-03-01 15:18:14 +000076 for (int i = 9; i < 24; i += 2) {
77 paint.setTextSize(SkIntToScalar(i));
78 tbox.draw(canvas);
79 canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing());
reed@android.com0bb6d062010-05-17 14:50:04 +000080 }
reed@android.com0bb6d062010-05-17 14:50:04 +000081 }
reed@google.comd6f5a812011-03-02 14:34:11 +000082
reed@android.com0bb6d062010-05-17 14:50:04 +000083private:
reed@android.com0bb6d062010-05-17 14:50:04 +000084 typedef SkView INHERITED;
85};
86
87//////////////////////////////////////////////////////////////////////////////
88
89static SkView* MyFactory() { return new TextBoxView; }
90static SkViewRegister reg(MyFactory);
91