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