Home | History | Annotate | Download | only in samplecode
      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  */
      7 #include "SampleCode.h"
      8 
      9 #include "SkBlurMaskFilter.h"
     10 #include "SkCanvas.h"
     11 #include "SkColorFilter.h"
     12 #include "SkColorPriv.h"
     13 #include "SkColorShader.h"
     14 #include "SkGradientShader.h"
     15 #include "SkGraphics.h"
     16 #include "SkOSFile.h"
     17 #include "SkPath.h"
     18 #include "SkRandom.h"
     19 #include "SkRegion.h"
     20 #include "SkShader.h"
     21 #include "SkShaper.h"
     22 #include "SkStream.h"
     23 #include "SkTextBlob.h"
     24 #include "SkTime.h"
     25 #include "SkTypeface.h"
     26 #include "SkUtils.h"
     27 #include "SkView.h"
     28 
     29 extern void skia_set_text_gamma(float blackGamma, float whiteGamma);
     30 
     31 #if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
     32 extern SkTypeface* SkCreateTypefaceFromLOGFONT(const LOGFONT&);
     33 #endif
     34 
     35 static const char gText[] =
     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.";
     42 
     43 class TextBoxView : public SampleView {
     44 public:
     45     TextBoxView() {
     46 #if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI)
     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();
     57 #endif
     58     }
     59 
     60 protected:
     61     // overrides from SkEventSink
     62     bool onQuery(SkEvent* evt) override {
     63         if (SampleCode::TitleQ(*evt)) {
     64             SampleCode::TitleR(evt, "TextBox");
     65             return true;
     66         }
     67         return this->INHERITED::onQuery(evt);
     68     }
     69 
     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);
     75 
     76         SkShaper shaper(nullptr);
     77 
     78         SkScalar margin = 20;
     79 
     80         SkPaint paint;
     81         paint.setAntiAlias(true);
     82         paint.setLCDRenderText(true);
     83         paint.setColor(fg);
     84 
     85         for (int i = 9; i < 24; i += 2) {
     86             SkTextBlobBuilder builder;
     87             paint.setTextSize(SkIntToScalar(i));
     88             SkPoint end = shaper.shape(&builder, paint, gText, strlen(gText), true,
     89                                        { margin, margin }, w - margin);
     90             canvas->drawTextBlob(builder.make(), 0, 0, paint);
     91 
     92             canvas->translate(0, end.y());
     93         }
     94     }
     95 
     96     void onDrawContent(SkCanvas* canvas) override {
     97         SkScalar width = this->width() / 3;
     98         drawTest(canvas, width, this->height(), SK_ColorBLACK, SK_ColorWHITE);
     99         canvas->translate(width, 0);
    100         drawTest(canvas, width, this->height(), SK_ColorWHITE, SK_ColorBLACK);
    101         canvas->translate(width, 0);
    102         drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorWHITE);
    103         canvas->translate(0, this->height()/2);
    104         drawTest(canvas, width, this->height()/2, SK_ColorGRAY, SK_ColorBLACK);
    105     }
    106 
    107 private:
    108     typedef SampleView INHERITED;
    109 };
    110 
    111 //////////////////////////////////////////////////////////////////////////////
    112 
    113 static SkView* MyFactory() { return new TextBoxView; }
    114 static SkViewRegister reg(MyFactory);
    115