-
Notifications
You must be signed in to change notification settings - Fork 5.9k
freetype: Add function to load font from buffer #3586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
e4d776b to
b2bb3d6
Compare
| @param idx face_index to select a font faces in a single file. | ||
| */ | ||
|
|
||
| CV_WRAP virtual void loadFontData(uchar* pBuf, size_t bufSize, int idx) = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To fix compile error for pyhton-binding error, it maybe better to use char* instead of uchar*.
| mIsFaceAvailable = false; | ||
| CV_Assert( !FT_New_Face( mLibrary, fontFileName.c_str(), static_cast<FT_Long>(idx), &(mFace) ) ); | ||
| FT_Open_Args args{ FT_OPEN_MEMORY, (FT_Byte*)pBuf, static_cast<FT_Long>(bufSize), nullptr, nullptr, nullptr, 0, nullptr }; | ||
| CV_Assert( !FT_Open_Face(mLibrary, &args, idx, &mFace) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two functions reference different Freetype APIs. It would be better to have one or the other in common.
- Idea1) To use
FT_Open_Face()for file and memory.- both functrions construct
FT_Open_Argsfor file and memory. - new helper function receives
FT_Open_Argsandidx.
- both functrions construct
- Idea2) To use
FT_New_Face()for file, and to useFT_New_Memory_Face()for memory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs for FT_New_Face and docs for FT_New_Memory_Face say explicitly to use FT_Open_Face for both loading from file or buffer. My PR was specific to adding more functionality, so i didnt want to change old code in the same PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your comment. It is OK. I think there is no problem.
In this pull-request, the FreeType function to be called was switched with an additional commit, so I confirmed the intention. thank you very much !!
| } | ||
|
|
||
| void FreeType2Impl::loadFontData(uchar* pBuf, size_t bufSize, int idx) | ||
| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think to check pBuf and bufSize is needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes but i do not want to assert.
i think both loading functions should return a result indicating if the loading was successful.
loading from file is an inherently failable i/o operation, so asserting there is very bad practice imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason why the loadFontData() function has no return value is because there is no way to recover if this function fails. If this function returns some errors, I believe it is difficult/hard to do anything. Is it better to continue with missing drawing ?
And If an inappropriate/invalid argument is set, it would be better to use CV_Assert() to indicate that there is a problem with the user program implementation/calling.
- If
pBuf == nullptrand/orbufSize == 0are invalid input. (We can omit to checkbufSize.) - In other case, if
pBuf[0...bufSize]are invalid/broken font data, we expectsFace_Open_args()will be return with failed. It will be captured withCV_Assert(). - In other case if
pBug[]buffer is smaller thanbufSize, we cannot do anything. Only what we can do is user-application handle it correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could recover by trying to load another font. A lot of cpp programs (including all of mine) have exceptions disabled, by asserting you remove their option to handle it in a way they want to. If a user wants to, he can still assert the return value
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I think it is difficult to use another/alternative fonts.
- Cases where header data is corrupted can be caught at
loadFontData()stage. This is a case where clearly incorrect data such as image data is specified. - Cases where glyph data is corrupted cannot be caught until the
putText()stage, which reads and renders the glyph data.(When glyph data ofAis broken, ifAis not contained strings forputText(), there are no problem.)
With your design, that means if putText() failed, we should re-call loadFontData() with another font and retry to call putText(). I think it is too complex.
- Exception handling is a prerequisite for using OpenCV. Following is OpenCV Introduction Error Handling Section section.
OpenCV uses exceptions to signal critical errors. When the input data has a correct format and belongs to the specified value range, but the algorithm cannot succeed for some reason.
|
I believe to add some test is required to merge it. ( Opencv-extra contains font data. ) |
|
@razaqq Friendly reminder. |
|
@Kumataro I already added simple test for the new method. |
|
Thank you for your comment. I created a patch on my own repository based on razaqq's repository and created a pull request ( #3593 ). However, maybe I was able to commit directly to razaqq's repository? |
There currently is no way to load a freetype font from a memory buffer (for example embedded resources etc). This small PR adds a simple function adding that functionality.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.