reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 1 | #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.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame^] | 23 | extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&); |
| 24 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 25 | static 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 | |
| 33 | class TextBoxView : public SkView { |
| 34 | public: |
| 35 | TextBoxView() { |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame^] | 36 | 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.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | protected: |
| 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.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame^] | 75 | tbox.setText(gText, strlen(gText), paint); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 76 | |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame^] | 77 | 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.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 81 | } |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | private: |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 85 | typedef SkView INHERITED; |
| 86 | }; |
| 87 | |
| 88 | ////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | static SkView* MyFactory() { return new TextBoxView; } |
| 91 | static SkViewRegister reg(MyFactory); |
| 92 | |