cvxFont.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #ifndef OPENCVUNICODE_CVXFONT_H
  2. #define OPENCVUNICODE_CVXFONT_H
  3. #include <ft2build.h>
  4. #include FT_FREETYPE_H
  5. #include <opencv2/opencv.hpp>
  6. #include <typeinfo>
  7. #include <codecvt>
  8. #include <string>
  9. #include <locale>
  10. namespace cvx {
  11. struct FontProperty {
  12. int fontSize; // font size (pixel)
  13. double spaceRatio; // ratio of distance when meet a space, base on font size
  14. double fontRatio; // ratio of distance between each character, base on font size
  15. double fontRotateAngle; // rotate angle
  16. double fontDiaphaneity; // merge ratio
  17. bool fontIsUnderline; // underline
  18. bool fontIsVertical; // put text in vertical
  19. };
  20. class CvxFont
  21. {
  22. public:
  23. explicit CvxFont(const cv::String& fontType);
  24. virtual ~CvxFont();
  25. void setFontSize(int fontSize);
  26. void setSpaceRatio(const double spaceRatio) { m_font->spaceRatio = spaceRatio; }
  27. void setFontRatio(const double fontRatio) { m_font->fontRatio = fontRatio; }
  28. void setRotateAngle(const double angle) { m_font->fontRotateAngle = angle; }
  29. void setUnderline(const bool isUnderline) { m_font->fontIsUnderline = isUnderline; }
  30. void setDiaphaneity(const double diaphaneity) { m_font->fontDiaphaneity = diaphaneity; }
  31. void setVertical(const bool vertical) { m_font->fontIsVertical = vertical; }
  32. [[nodiscard]] int getFontSize() const { return m_font->fontSize; }
  33. [[nodiscard]] double getSpaceRatio() const { return m_font->spaceRatio; }
  34. [[nodiscard]] double getFontRatio() const { return m_font->fontRatio; }
  35. [[nodiscard]] double getAngle() const { return m_font->fontRotateAngle; }
  36. [[nodiscard]] bool getUnderline() const { return m_font->fontIsUnderline; }
  37. [[nodiscard]] double getDiaphaneity() const { return m_font->fontDiaphaneity; }
  38. [[nodiscard]] bool getVertical() const { return m_font->fontIsVertical; }
  39. private:
  40. void initFont();
  41. void rotateFont(double angle);
  42. void putTextStr(cv::Mat& img, const cv::String& text, cv::Point pos, const cv::Scalar& color);
  43. void putWChar(cv::Mat& img, uint32_t wc, cv::Point& pos, const cv::Scalar& color);
  44. friend void putText(cv::Mat&, const std::string&, cv::Point, cvx::CvxFont&, int, const cv::Scalar&);
  45. FT_Library m_library{}; // font library
  46. FT_Face m_face{}; // font type
  47. FT_Matrix m_matrix{};
  48. FT_Vector m_pen{};
  49. FT_Error m_error;
  50. FontProperty* m_font;
  51. long m_maxDiffHeight{ 0 };
  52. };
  53. void putText(cv::Mat& img, const std::string& text, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color);
  54. void putSymbols(cv::Mat& img, std::vector<uint32_t>& symbols, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color);
  55. void putOneSymbol(cv::Mat& img, uint32_t symbol, cv::Point pos, cvx::CvxFont& fontFace, int fontSize, const cv::Scalar& color);
  56. }
  57. #endif //OPENCVUNICODE_CVXFONT_H