ftp_render_test.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2018 Intel Corporation
  6. #include "../test_precomp.hpp"
  7. #ifdef HAVE_FREETYPE
  8. #include <random>
  9. #include <opencv2/core/utils/configuration.private.hpp>
  10. #include "backends/render/ft_render.hpp"
  11. namespace opencv_test
  12. {
  13. static std::string getFontPath()
  14. {
  15. static std::string path = cv::utils::getConfigurationParameterString("OPENCV_TEST_FREETYPE_FONT_PATH",
  16. "/usr/share/fonts/truetype/wqy/wqy-microhei.ttc");
  17. return path;
  18. }
  19. inline void RunTest(const std::string& font,
  20. size_t num_iters,
  21. size_t lower_char_code,
  22. size_t upper_char_code)
  23. {
  24. cv::gapi::wip::draw::FTTextRender ftpr(font);
  25. std::mt19937 gen{std::random_device()()};
  26. std::uniform_int_distribution<int> dist(lower_char_code, upper_char_code);
  27. std::uniform_int_distribution<int> dist_size(2, 200);
  28. for (size_t i = 0; i < num_iters; ++i)
  29. {
  30. size_t text_size = dist_size(gen);
  31. std::wstring text;
  32. for (size_t j = 0; j < text_size; ++j)
  33. {
  34. wchar_t c = dist(gen);
  35. text += c;
  36. }
  37. int fh = dist_size(gen);
  38. int baseline = 0;
  39. cv::Size size;
  40. ASSERT_NO_THROW(size = ftpr.getTextSize(text, fh, &baseline));
  41. cv::Mat bmp(size, CV_8UC1, cv::Scalar::all(0));
  42. cv::Point org(0, bmp.rows - baseline);
  43. ASSERT_NO_THROW(ftpr.putText(bmp, text, org, fh));
  44. }
  45. }
  46. TEST(FTTextRenderTest, Smoke_Test_Ascii)
  47. {
  48. RunTest(getFontPath(), 2000, 32, 126);
  49. }
  50. TEST(FTTextRenderTest, Smoke_Test_Unicode)
  51. {
  52. RunTest(getFontPath(), 2000, 20320, 30000);
  53. }
  54. } // namespace opencv_test
  55. #endif // HAVE_FREETYPE