blob: f2b5e2c33ba89bd5714c0fffe27996d59ff13906 [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
35class TextBoxView : public SkView {
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@android.com0bb6d062010-05-17 14:50:04 +000063 void drawBG(SkCanvas* canvas) {
64 canvas->drawColor(SK_ColorWHITE);
65 }
reed@google.comd6f5a812011-03-02 14:34:11 +000066
reed@android.com0bb6d062010-05-17 14:50:04 +000067 virtual void onDraw(SkCanvas* canvas) {
68 this->drawBG(canvas);
69
70 SkScalar margin = 20;
71 SkTextBox tbox;
72 tbox.setMode(SkTextBox::kLineBreak_Mode);
73 tbox.setBox(margin, margin,
74 this->width() - margin, this->height() - margin);
75 tbox.setSpacing(SkIntToScalar(3)/3, 0);
76
77 SkPaint paint;
78 paint.setAntiAlias(true);
reed@google.comb6524272011-03-01 15:18:14 +000079 tbox.setText(gText, strlen(gText), paint);
reed@android.com0bb6d062010-05-17 14:50:04 +000080
reed@google.comb6524272011-03-01 15:18:14 +000081 for (int i = 9; i < 24; i += 2) {
82 paint.setTextSize(SkIntToScalar(i));
83 tbox.draw(canvas);
84 canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing());
reed@android.com0bb6d062010-05-17 14:50:04 +000085 }
reed@android.com0bb6d062010-05-17 14:50:04 +000086 }
reed@google.comd6f5a812011-03-02 14:34:11 +000087
reed@android.com0bb6d062010-05-17 14:50:04 +000088private:
reed@android.com0bb6d062010-05-17 14:50:04 +000089 typedef SkView INHERITED;
90};
91
92//////////////////////////////////////////////////////////////////////////////
93
94static SkView* MyFactory() { return new TextBoxView; }
95static SkViewRegister reg(MyFactory);
96