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 23 #ifdef SK_BUILD_FOR_WIN 24 extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&); 25 #endif 26 27 static const char gText[] = 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 SampleView { 36 public: 37 TextBoxView() { 38 #ifdef SK_BUILD_FOR_WIN 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(); 49 #endif 50 } 51 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 } 62 63 virtual void onDrawContent(SkCanvas* canvas) { 64 SkScalar margin = 20; 65 SkTextBox tbox; 66 tbox.setMode(SkTextBox::kLineBreak_Mode); 67 tbox.setBox(margin, margin, 68 this->width() - margin, this->height() - margin); 69 tbox.setSpacing(SkIntToScalar(3)/3, 0); 70 71 SkPaint paint; 72 paint.setAntiAlias(true); 73 paint.setLCDRenderText(true); 74 tbox.setText(gText, strlen(gText), paint); 75 76 for (int i = 9; i < 24; i += 2) { 77 paint.setTextSize(SkIntToScalar(i)); 78 tbox.draw(canvas); 79 canvas->translate(0, tbox.getTextHeight() + paint.getFontSpacing()); 80 } 81 } 82 83 private: 84 typedef SkView INHERITED; 85 }; 86 87 ////////////////////////////////////////////////////////////////////////////// 88 89 static SkView* MyFactory() { return new TextBoxView; } 90 static SkViewRegister reg(MyFactory); 91 92