stroketext.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2014 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 "gm/gm.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkFontTypes.h"
  13. #include "include/core/SkImageInfo.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkPathEffect.h"
  16. #include "include/core/SkPoint.h"
  17. #include "include/core/SkScalar.h"
  18. #include "include/core/SkTextBlob.h"
  19. #include "include/core/SkTypeface.h"
  20. #include "include/core/SkTypes.h"
  21. #include "include/effects/SkDashPathEffect.h"
  22. #include "tools/ToolUtils.h"
  23. static void test_nulldev(SkCanvas* canvas) {
  24. SkBitmap bm;
  25. bm.setInfo(SkImageInfo::MakeN32Premul(30, 30));
  26. // notice: no pixels mom! be sure we don't crash
  27. // https://code.google.com/p/chromium/issues/detail?id=352616
  28. SkCanvas c(bm);
  29. SkBitmap src;
  30. src.allocN32Pixels(10, 10);
  31. src.eraseColor(SK_ColorRED);
  32. // ensure we don't crash
  33. c.writePixels(src, 0, 0);
  34. }
  35. static void draw_text_stroked(SkCanvas* canvas, const SkPaint& paint, const SkFont& font,
  36. SkScalar strokeWidth) {
  37. SkPaint p(paint);
  38. SkPoint loc = { 20, 435 };
  39. if (strokeWidth > 0) {
  40. p.setStyle(SkPaint::kFill_Style);
  41. canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
  42. canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
  43. }
  44. p.setColor(SK_ColorRED);
  45. p.setStyle(SkPaint::kStroke_Style);
  46. p.setStrokeWidth(strokeWidth);
  47. canvas->drawSimpleText("P", 1, SkTextEncoding::kUTF8, loc.fX, loc.fY - 225, font, p);
  48. canvas->drawTextBlob(SkTextBlob::MakeFromPosText("P", 1, &loc, font), 0, 0, p);
  49. }
  50. static void draw_text_set(SkCanvas* canvas, const SkPaint& paint, const SkFont& font) {
  51. SkAutoCanvasRestore acr(canvas, true);
  52. draw_text_stroked(canvas, paint, font, 10);
  53. canvas->translate(200, 0);
  54. draw_text_stroked(canvas, paint, font, 0);
  55. const SkScalar intervals[] = { 20, 10, 5, 10 };
  56. const SkScalar phase = 0;
  57. canvas->translate(200, 0);
  58. SkPaint p(paint);
  59. p.setPathEffect(SkDashPathEffect::Make(intervals, SK_ARRAY_COUNT(intervals), phase));
  60. draw_text_stroked(canvas, p, font, 10);
  61. }
  62. namespace {
  63. enum {
  64. kBelowThreshold_TextSize = 255,
  65. kAboveThreshold_TextSize = 257
  66. };
  67. }
  68. DEF_SIMPLE_GM(stroketext, canvas, 1200, 480) {
  69. if (true) { test_nulldev(canvas); }
  70. SkPaint paint;
  71. paint.setAntiAlias(true);
  72. SkFont font(ToolUtils::create_portable_typeface(), kBelowThreshold_TextSize);
  73. draw_text_set(canvas, paint, font);
  74. canvas->translate(600, 0);
  75. font.setSize(kAboveThreshold_TextSize);
  76. draw_text_set(canvas, paint, font);
  77. }