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 | */ |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 7 | #include "Sample.h" |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 8 | |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 9 | #include "SkBlurMaskFilter.h" |
| 10 | #include "SkCanvas.h" |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
| 12 | #include "SkColorPriv.h" |
reed@google.com | 021b405 | 2011-09-28 15:24:00 +0000 | [diff] [blame] | 13 | #include "SkColorShader.h" |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 14 | #include "SkGradientShader.h" |
| 15 | #include "SkGraphics.h" |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 16 | #include "SkOSFile.h" |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 17 | #include "SkPath.h" |
| 18 | #include "SkRandom.h" |
| 19 | #include "SkRegion.h" |
| 20 | #include "SkShader.h" |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 21 | #include "SkShaper.h" |
| 22 | #include "SkStream.h" |
| 23 | #include "SkTextBlob.h" |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 24 | #include "SkTime.h" |
| 25 | #include "SkTypeface.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame] | 26 | #include "SkUTF.h" |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 27 | |
reed@google.com | 021b405 | 2011-09-28 15:24:00 +0000 | [diff] [blame] | 28 | extern void skia_set_text_gamma(float blackGamma, float whiteGamma); |
| 29 | |
bungeman@google.com | b49d989 | 2012-08-16 17:35:58 +0000 | [diff] [blame] | 30 | #if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI) |
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[] = |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +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."; |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 41 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 42 | class TextBoxView : public Sample { |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 43 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 44 | TextBoxView() { |
bungeman@google.com | b49d989 | 2012-08-16 17:35:58 +0000 | [diff] [blame] | 45 | #if defined(SK_BUILD_FOR_WIN) && defined(SK_FONTHOST_WIN_GDI) |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +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@google.com | 021b405 | 2011-09-28 15:24:00 +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: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 60 | bool onQuery(Sample::Event* evt) override { |
| 61 | if (Sample::TitleQ(*evt)) { |
| 62 | Sample::TitleR(evt, "TextBox"); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 63 | return true; |
| 64 | } |
| 65 | return this->INHERITED::onQuery(evt); |
| 66 | } |
reed@google.com | d6f5a81 | 2011-03-02 14:34:11 +0000 | [diff] [blame] | 67 | |
reed@google.com | 021b405 | 2011-09-28 15:24:00 +0000 | [diff] [blame] | 68 | void drawTest(SkCanvas* canvas, SkScalar w, SkScalar h, SkColor fg, SkColor bg) { |
| 69 | SkAutoCanvasRestore acr(canvas, true); |
| 70 | |
| 71 | canvas->clipRect(SkRect::MakeWH(w, h)); |
| 72 | canvas->drawColor(bg); |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 73 | |
| 74 | SkShaper shaper(nullptr); |
| 75 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 76 | SkScalar margin = 20; |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 77 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 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 | 021b405 | 2011-09-28 15:24:00 +0000 | [diff] [blame] | 81 | paint.setColor(fg); |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 82 | |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 83 | for (int i = 9; i < 24; i += 2) { |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 84 | SkTextBlobBuilderLineHandler builder; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 85 | paint.setTextSize(SkIntToScalar(i)); |
Hal Canary | 2a1848d | 2018-11-26 17:23:24 -0500 | [diff] [blame] | 86 | SkFont font = SkFont::LEGACY_ExtractFromPaint(paint); |
| 87 | SkPoint end = shaper.shape(&builder, font, gText, strlen(gText), true, |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 88 | { margin, margin }, w - margin); |
Florin Malita | 9867f61 | 2018-12-12 10:54:49 -0500 | [diff] [blame] | 89 | canvas->drawTextBlob(builder.makeBlob(), 0, 0, paint); |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 90 | |
| 91 | canvas->translate(0, end.y()); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 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 | |
Mike Reed | 193d3a0 | 2018-02-08 15:45:51 -0500 | [diff] [blame] | 95 | void onDrawContent(SkCanvas* canvas) override { |
reed@google.com | 021b405 | 2011-09-28 15:24:00 +0000 | [diff] [blame] | 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: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 107 | typedef Sample INHERITED; |
reed@android.com | 0bb6d06 | 2010-05-17 14:50:04 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | ////////////////////////////////////////////////////////////////////////////// |
| 111 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 112 | DEF_SAMPLE( return new TextBoxView(); ) |