epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkBlurMaskFilter.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkGraphics.h" |
| 14 | #include "SkImageDecoder.h" |
| 15 | #include "SkPath.h" |
| 16 | #include "SkRandom.h" |
| 17 | #include "SkRegion.h" |
| 18 | #include "SkShader.h" |
| 19 | #include "SkUtils.h" |
| 20 | #include "SkXfermode.h" |
| 21 | #include "SkColorPriv.h" |
| 22 | #include "SkColorFilter.h" |
| 23 | #include "SkTime.h" |
| 24 | #include "SkTypeface.h" |
| 25 | #include "SkTextBox.h" |
| 26 | #include "SkOSFile.h" |
| 27 | #include "SkStream.h" |
| 28 | #include "SkKey.h" |
| 29 | |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 30 | #ifdef SK_BUILD_FOR_WIN |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 31 | extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&); |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 32 | #endif |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 33 | |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 34 | static const char gText[] = |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 35 | "When in the Course of human events it becomes necessary for one people " |
| 36 | "to dissolve the political bands which have connected them with another " |
| 37 | "and to assume among the powers of the earth, the separate and equal " |
| 38 | "station to which the Laws of Nature and of Nature's God entitle them, " |
| 39 | "a decent respect to the opinions of mankind requires that they should " |
| 40 | "declare the causes which impel them to the separation."; |
| 41 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 42 | class TextBoxView : public SampleView { |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 43 | public: |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 44 | TextBoxView() { |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 45 | #ifdef SK_BUILD_FOR_WIN |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 46 | LOGFONT lf; |
| 47 | sk_bzero(&lf, sizeof(lf)); |
| 48 | lf.lfHeight = 9; |
| 49 | SkTypeface* tf0 = SkCreateTypefaceFromLOGFONT(lf); |
| 50 | lf.lfHeight = 12; |
| 51 | SkTypeface* tf1 = SkCreateTypefaceFromLOGFONT(lf); |
| 52 | // we assert that different sizes should not affect which face we get |
| 53 | SkASSERT(tf0 == tf1); |
| 54 | tf0->unref(); |
| 55 | tf1->unref(); |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 56 | #endif |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 57 | } |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 58 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 59 | protected: |
| 60 | // overrides from SkEventSink |
| 61 | virtual bool onQuery(SkEvent* evt) { |
| 62 | if (SampleCode::TitleQ(*evt)) { |
| 63 | SkString str("TextBox"); |
| 64 | SampleCode::TitleR(evt, str.c_str()); |
| 65 | return true; |
| 66 | } |
| 67 | return this->INHERITED::onQuery(evt); |
| 68 | } |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 69 | |
reed@google.com | 17fb387 | 2011-05-04 14:31:07 +0000 | [diff] [blame] | 70 | virtual void onDrawContent(SkCanvas* canvas) { |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 71 | SkScalar margin = 20; |
| 72 | SkTextBox tbox; |
| 73 | tbox.setMode(SkTextBox::kLineBreak_Mode); |
| 74 | tbox.setBox(margin, margin, |
| 75 | this->width() - margin, this->height() - margin); |
| 76 | tbox.setSpacing(SkIntToScalar(3)/3, 0); |
| 77 | |
| 78 | SkPaint paint; |
| 79 | paint.setAntiAlias(true); |
reed@google.com | 261b8e2 | 2011-04-14 17:53:24 +0000 | [diff] [blame] | 80 | paint.setLCDRenderText(true); |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 81 | tbox.setText(gText, strlen(gText), paint); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 82 | |
reed@google.com | b652427 | 2011-03-01 15:18:14 +0000 | [diff] [blame] | 83 | for (int i = 9; i < 24; i += 2) { |
| 84 | paint.setTextSize(SkIntToScalar(i)); |
| 85 | tbox.draw(canvas); |
| 86 | canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing()); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 87 | } |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 88 | } |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 89 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 90 | private: |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 91 | typedef SkView INHERITED; |
| 92 | }; |
| 93 | |
| 94 | ////////////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | static SkView* MyFactory() { return new TextBoxView; } |
| 97 | static SkViewRegister reg(MyFactory); |
| 98 | |