blob: 9511914ab651a053a376729c34733591e48922c3 [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.comb6524272011-03-01 15:18:14 +000023extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
24
reed@android.com0bb6d062010-05-17 14:50:04 +000025static const char gText[] =
26 "When in the Course of human events it becomes necessary for one people "
27 "to dissolve the political bands which have connected them with another "
28 "and to assume among the powers of the earth, the separate and equal "
29 "station to which the Laws of Nature and of Nature's God entitle them, "
30 "a decent respect to the opinions of mankind requires that they should "
31 "declare the causes which impel them to the separation.";
32
33class TextBoxView : public SkView {
34public:
35 TextBoxView() {
reed@google.comb6524272011-03-01 15:18:14 +000036 LOGFONT lf;
37 sk_bzero(&lf, sizeof(lf));
38 lf.lfHeight = 9;
39 SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf);
40 lf.lfHeight = 12;
41 SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf);
42 // we assert that different sizes should not affect which face we get
43 SkASSERT(tf0 == tf1);
44 tf0->unref();
45 tf1->unref();
reed@android.com0bb6d062010-05-17 14:50:04 +000046 }
47
48protected:
49 // overrides from SkEventSink
50 virtual bool onQuery(SkEvent* evt) {
51 if (SampleCode::TitleQ(*evt)) {
52 SkString str("TextBox");
53 SampleCode::TitleR(evt, str.c_str());
54 return true;
55 }
56 return this->INHERITED::onQuery(evt);
57 }
58
59 void drawBG(SkCanvas* canvas) {
60 canvas->drawColor(SK_ColorWHITE);
61 }
62
63 virtual void onDraw(SkCanvas* canvas) {
64 this->drawBG(canvas);
65
66 SkScalar margin = 20;
67 SkTextBox tbox;
68 tbox.setMode(SkTextBox::kLineBreak_Mode);
69 tbox.setBox(margin, margin,
70 this->width() - margin, this->height() - margin);
71 tbox.setSpacing(SkIntToScalar(3)/3, 0);
72
73 SkPaint paint;
74 paint.setAntiAlias(true);
reed@google.comb6524272011-03-01 15:18:14 +000075 tbox.setText(gText, strlen(gText), paint);
reed@android.com0bb6d062010-05-17 14:50:04 +000076
reed@google.comb6524272011-03-01 15:18:14 +000077 for (int i = 9; i < 24; i += 2) {
78 paint.setTextSize(SkIntToScalar(i));
79 tbox.draw(canvas);
80 canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing());
reed@android.com0bb6d062010-05-17 14:50:04 +000081 }
reed@android.com0bb6d062010-05-17 14:50:04 +000082 }
83
84private:
reed@android.com0bb6d062010-05-17 14:50:04 +000085 typedef SkView INHERITED;
86};
87
88//////////////////////////////////////////////////////////////////////////////
89
90static SkView* MyFactory() { return new TextBoxView; }
91static SkViewRegister reg(MyFactory);
92