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 | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 23 | #ifdef SK_BUILD_FOR_WIN |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 24 | extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&); |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 25 | #endif |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 26 | |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 27 | static const char gText[] = |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 28 | "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 | |
| 35 | class TextBoxView : public SkView { |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 36 | public: |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 37 | TextBoxView() { |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 38 | #ifdef SK_BUILD_FOR_WIN |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 39 | 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.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 49 | #endif |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 50 | } |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 51 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 52 | protected: |
| 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.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 62 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 63 | void drawBG(SkCanvas* canvas) { |
| 64 | canvas->drawColor(SK_ColorWHITE); |
| 65 | } |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 66 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 67 | 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.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 79 | tbox.setText(gText, strlen(gText), paint); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 80 | |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 81 | 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.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 85 | } |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 86 | } |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame^] | 87 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 88 | private: |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 89 | typedef SkView INHERITED; |
| 90 | }; |
| 91 | |
| 92 | ////////////////////////////////////////////////////////////////////////////// |
| 93 | |
| 94 | static SkView* MyFactory() { return new TextBoxView; } |
| 95 | static SkViewRegister reg(MyFactory); |
| 96 | |