FontHostStreamTest.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "include/core/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkGraphics.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPoint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkStream.h"
  16. #include "include/core/SkTypeface.h"
  17. #include "include/core/SkTypes.h"
  18. #include "src/core/SkFontDescriptor.h"
  19. #include "src/core/SkFontPriv.h"
  20. #include "tests/Test.h"
  21. static const SkColor bgColor = SK_ColorWHITE;
  22. static void create(SkBitmap* bm, SkIRect bound) {
  23. bm->allocN32Pixels(bound.width(), bound.height());
  24. }
  25. static void drawBG(SkCanvas* canvas) {
  26. canvas->drawColor(bgColor);
  27. }
  28. /** Assumes that the ref draw was completely inside ref canvas --
  29. implies that everything outside is "bgColor".
  30. Checks that all overlap is the same and that all non-overlap on the
  31. ref is "bgColor".
  32. */
  33. static bool compare(const SkBitmap& ref, const SkIRect& iref,
  34. const SkBitmap& test, const SkIRect& itest)
  35. {
  36. const int xOff = itest.fLeft - iref.fLeft;
  37. const int yOff = itest.fTop - iref.fTop;
  38. for (int y = 0; y < test.height(); ++y) {
  39. for (int x = 0; x < test.width(); ++x) {
  40. SkColor testColor = test.getColor(x, y);
  41. int refX = x + xOff;
  42. int refY = y + yOff;
  43. SkColor refColor;
  44. if (refX >= 0 && refX < ref.width() &&
  45. refY >= 0 && refY < ref.height())
  46. {
  47. refColor = ref.getColor(refX, refY);
  48. } else {
  49. refColor = bgColor;
  50. }
  51. if (refColor != testColor) {
  52. return false;
  53. }
  54. }
  55. }
  56. return true;
  57. }
  58. DEF_TEST(FontHostStream, reporter) {
  59. {
  60. SkPaint paint;
  61. paint.setColor(SK_ColorGRAY);
  62. SkFont font(SkTypeface::MakeFromName("Georgia", SkFontStyle()), 30);
  63. font.setEdging(SkFont::Edging::kAlias);
  64. SkIRect origRect = SkIRect::MakeWH(64, 64);
  65. SkBitmap origBitmap;
  66. create(&origBitmap, origRect);
  67. SkCanvas origCanvas(origBitmap);
  68. SkIRect streamRect = SkIRect::MakeWH(64, 64);
  69. SkBitmap streamBitmap;
  70. create(&streamBitmap, streamRect);
  71. SkCanvas streamCanvas(streamBitmap);
  72. SkPoint point = SkPoint::Make(24, 32);
  73. // Test: origTypeface and streamTypeface from orig data draw the same
  74. drawBG(&origCanvas);
  75. origCanvas.drawString("A", point.fX, point.fY, font, paint);
  76. sk_sp<SkTypeface> typeface = font.refTypefaceOrDefault();
  77. int ttcIndex;
  78. std::unique_ptr<SkStreamAsset> fontData = typeface->openStream(&ttcIndex);
  79. if (!fontData) {
  80. // We're using a SkTypeface that can't give us a stream.
  81. // This happens with portable or system fonts. End the test now.
  82. return;
  83. }
  84. sk_sp<SkTypeface> streamTypeface(SkTypeface::MakeFromStream(std::move(fontData)));
  85. SkFontDescriptor desc;
  86. bool isLocalStream = false;
  87. streamTypeface->getFontDescriptor(&desc, &isLocalStream);
  88. REPORTER_ASSERT(reporter, isLocalStream);
  89. font.setTypeface(streamTypeface);
  90. drawBG(&streamCanvas);
  91. streamCanvas.drawString("A", point.fX, point.fY, font, paint);
  92. REPORTER_ASSERT(reporter,
  93. compare(origBitmap, origRect, streamBitmap, streamRect));
  94. }
  95. //Make sure the typeface is deleted and removed.
  96. SkGraphics::PurgeFontCache();
  97. }