FontMgrFontConfigTest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "include/core/SkCanvas.h"
  8. #include "include/core/SkFont.h"
  9. #include "include/core/SkFontMgr.h"
  10. #include "include/core/SkTypeface.h"
  11. #include "include/ports/SkFontMgr_fontconfig.h"
  12. #include "tests/Test.h"
  13. #include "tools/Resources.h"
  14. #include <fontconfig/fontconfig.h>
  15. static bool bitmap_compare(const SkBitmap& ref, const SkBitmap& test) {
  16. for (int y = 0; y < test.height(); ++y) {
  17. for (int x = 0; x < test.width(); ++x) {
  18. SkColor testColor = test.getColor(x, y);
  19. SkColor refColor = ref.getColor(x, y);
  20. if (refColor != testColor) {
  21. return false;
  22. }
  23. }
  24. }
  25. return true;
  26. }
  27. DEF_TEST(FontMgrFontConfig, reporter) {
  28. FcConfig* config = FcConfigCreate();
  29. // FontConfig may modify the passed path (make absolute or other).
  30. FcConfigSetSysRoot(config, reinterpret_cast<const FcChar8*>(GetResourcePath("").c_str()));
  31. // FontConfig will lexically compare paths against its version of the sysroot.
  32. SkString distortablePath(reinterpret_cast<const char*>(FcConfigGetSysRoot(config)));
  33. distortablePath += "/fonts/Distortable.ttf";
  34. FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(distortablePath.c_str()));
  35. FcConfigBuildFonts(config);
  36. sk_sp<SkFontMgr> fontMgr(SkFontMgr_New_FontConfig(config));
  37. sk_sp<SkTypeface> typeface(fontMgr->legacyMakeTypeface("Distortable", SkFontStyle()));
  38. if (!typeface) {
  39. ERRORF(reporter, "Could not find typeface. FcVersion: %d", FcGetVersion());
  40. return;
  41. }
  42. SkBitmap bitmapStream;
  43. bitmapStream.allocN32Pixels(64, 64);
  44. SkCanvas canvasStream(bitmapStream);
  45. canvasStream.drawColor(SK_ColorWHITE);
  46. SkBitmap bitmapClone;
  47. bitmapClone.allocN32Pixels(64, 64);
  48. SkCanvas canvasClone(bitmapClone);
  49. canvasStream.drawColor(SK_ColorWHITE);
  50. SkPaint paint;
  51. paint.setColor(SK_ColorGRAY);
  52. constexpr float kTextSize = 20;
  53. std::unique_ptr<SkStreamAsset> distortableStream(
  54. GetResourceAsStream("fonts/Distortable.ttf"));
  55. if (!distortableStream) {
  56. return;
  57. }
  58. SkPoint point = SkPoint::Make(20.0f, 20.0f);
  59. SkFourByteTag tag = SkSetFourByteTag('w', 'g', 'h', 't');
  60. for (int i = 0; i < 10; ++i) {
  61. SkScalar styleValue =
  62. SkDoubleToScalar(0.5 + i * ((2.0 - 0.5) / 10));
  63. SkFontArguments::VariationPosition::Coordinate
  64. coordinates[] = {{tag, styleValue}};
  65. SkFontArguments::VariationPosition
  66. position = {coordinates, SK_ARRAY_COUNT(coordinates)};
  67. SkFont fontStream(
  68. fontMgr->makeFromStream(distortableStream->duplicate(),
  69. SkFontArguments().setVariationDesignPosition(position)),
  70. kTextSize);
  71. fontStream.setEdging(SkFont::Edging::kSubpixelAntiAlias);
  72. SkFont fontClone(
  73. typeface->makeClone(SkFontArguments().setVariationDesignPosition(position)), kTextSize);
  74. fontClone.setEdging(SkFont::Edging::kSubpixelAntiAlias);
  75. constexpr char text[] = "abc";
  76. canvasStream.drawColor(SK_ColorWHITE);
  77. canvasStream.drawString(text, point.fX, point.fY, fontStream, paint);
  78. canvasClone.drawColor(SK_ColorWHITE);
  79. canvasClone.drawString(text, point.fX, point.fY, fontClone, paint);
  80. bool success = bitmap_compare(bitmapStream, bitmapClone);
  81. REPORTER_ASSERT(reporter, success);
  82. }
  83. }