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