-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
GUI: Change in the BDF fonts scaling code #4867
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
NischayDiwan
commented
Apr 3, 2023
- The primary change is to fix or improve the scaling of BDF fonts
- Now uses the same approach as in MacFonts scaling
- Bugs - there are still some problems when testing in capital letters
- Note: the change in graphics.cpp for the testbed engine now calls the scale font function.
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.
This code is not correct. You are generating dstGray but then do not use it at all.
graphics/fonts/bdf.cpp
Outdated
| } | ||
|
|
||
| #if DRAWDEBUG | ||
| bool dododo11; |
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.
Why is this sitting in a common space and not in the method?
Also, if you want to keep it, please give it a meaningful name
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 have copied this as it is from the MacFont Code, it basically allows the debug to run in the Magnify gray function as well.
I will remove that.
graphics/fonts/bdf.cpp
Outdated
| data.defaultBox.xOffset = (int)(roundf((float)src->_data.defaultBox.xOffset * scale)); | ||
| data.defaultBox.yOffset = (int)(roundf((float)src->_data.defaultBox.yOffset * scale)); | ||
| data.ascent = (int)roundf(((float)src->_data.ascent * scale)); | ||
| data.maxAdvance = (int)(((float)src->_data.maxAdvance * scale)); |
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.
Are you sure you do not want to round but use the floor()?
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.
Oh, I missed that, will fix it.
graphics/fonts/bdf.cpp
Outdated
| if (src->_data.bitmaps[i]) { | ||
| const int bytes = ((box.width + 7) / 8) * box.height; // Dimensions have been already corrected | ||
| bitmaps[i] = new byte[bytes]; | ||
| // int hs = srcBox.height; |
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.
Why is this commented out and not removed?
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 I will fix this.
graphics/fonts/bdf.cpp
Outdated
| } | ||
| #endif | ||
| if (*grayPtr > grayLevel) | ||
| *dst = 1; |
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.
Could you please refactor this magnification method, stick it into Common::Font (base method) and then reuse it in both MacFont and here?
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 don't understand, what to change here please clarify
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.
Refactor the magnification of a srcSurf via dstGray into a separate reusable method.
graphics/fonts/bdf.cpp
Outdated
| debugN("\n"); | ||
| #endif | ||
| } | ||
| const int bytes = dstPitch*h; |
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.
This does not conform to our code formatting conventions.
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 will fix that.
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.
Too many strange decisions. And still not following our code formatting guidelines
graphics/fonts/bdf.cpp
Outdated
|
|
||
| #if DRAWDEBUG | ||
| int ccc = 'a'; | ||
| dododo11 = i == ccc; |
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.
Why is it still here?
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 fixed that
graphics/font.cpp
Outdated
| grayScaleMap = (int *)malloc(grayScaleMapSize * sizeof(int)); | ||
| } | ||
|
|
||
| Surface *Font::scaleSingleGlyph(int width, int height, int grayLevel, int chr, int xOffset, int srcheight, int srcwidth, float scale){ |
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.
Bad code formatting
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 the function line, please check I have changed it
graphics/font.cpp
Outdated
| return scaleSurface; | ||
| } | ||
|
|
||
| void Font::deinitializeScaling(){ |
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.
Bad code formatting
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 same fixed it
graphics/fonts/macfont.cpp
Outdated
| PixelFormat::createFormatCLUT8()); | ||
| int dstGraySize = newSize * 20 * newSize; | ||
| int *dstGray = (int *)malloc(dstGraySize * sizeof(int)); | ||
| BdfFont* srcCopy = new BdfFont(src->_data, DisposeAfterUse::YES); |
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.
Nad code formatting. And WHY BdfFont????
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 removed that, check the new commit
graphics/fonts/macfont.cpp
Outdated
| srcCopy->initializeScaling(src->getFontSize(), newSize); | ||
|
|
||
| Graphics::Surface tmpSurf; | ||
| // srcSurf.create(MAX(src->getFontSize() * 2, newSize * 2), MAX(src->getFontSize() * 2, newSize * 2), |
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.
Why is this still commented out instead of being removed?
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.
fixed that
| // int dstGraySize = newSize * 20 * newSize; | ||
| // int *dstGray = (int *)malloc(dstGraySize * sizeof(int)); | ||
|
|
||
| tmpSurf.create(MAX(src->getFontSize() * 2, newSize * 2), MAX(src->getFontSize() * 2 + 2, newSize * 2 + 2), |
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.
Why is this still here?
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.
fixed the comments, tmpSurf is not used in any of those 3 functions, but outside though so I have kept it as it is
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.
Then please move its creation closer to the usage.
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, I have fixed that but It has to be declare in the outside the for loop.
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.
More changes are still needed.
graphics/font.h
Outdated
| class Font { | ||
| public: | ||
| Font() {} | ||
| Font() {}; |
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.
What is the intent of this change?
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 added the semicolon earlier by mistake, it was not in original files
graphics/font.h
Outdated
| /** | ||
| * Initializes the scalesurface for scaling and the grayScaleMap for the given @p srcSize and @p newSize. | ||
| * | ||
| * @param srcSize |
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.
Here you have to describe the parameters, not just enumerate them
graphics/font.h
Outdated
| * | ||
| * @param width The width of the bouding box. | ||
| * @param height The height of the bounding box. | ||
| * @param grayLevel The gray level. |
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.
What is "gray level"? you need to describe it here.
graphics/font.h
Outdated
| /** | ||
| * Scales the single gylph at @p chr the given the @p scale and @p resGray and returns the scaled surface with updated @p resGray | ||
| * | ||
| * @param width The width of the bouding box. |
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.
bounding box of what?
graphics/fonts/bdf.cpp
Outdated
|
|
||
| #if DRAWDEBUG | ||
| if (i == ccc) { | ||
| warning("BdfFont::scaleFont(): %d %d", data.height,box.height); |
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.
Do you need this as a warning? And if you do, the code formatting is off.
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 that is not required, removed it now
graphics/fonts/bdf.cpp
Outdated
| return nullptr; | ||
| } | ||
|
|
||
| BdfFont *srcCopy = new BdfFont(src->_data, DisposeAfterUse::YES); |
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.
Why are you creating a whole copy of the entire font? Please, do not do it.
graphics/fonts/bdf.cpp
Outdated
| } | ||
|
|
||
| BdfFont *srcCopy = new BdfFont(src->_data, DisposeAfterUse::YES); | ||
| srcCopy->initializeScaling(src->getFontSize(), newSize); |
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.
initializeScaling() should be called for this copy of the object, not creating another one pointlessly.
| // int dstGraySize = newSize * 20 * newSize; | ||
| // int *dstGray = (int *)malloc(dstGraySize * sizeof(int)); | ||
|
|
||
| tmpSurf.create(MAX(src->getFontSize() * 2, newSize * 2), MAX(src->getFontSize() * 2 + 2, newSize * 2 + 2), |
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.
Then please move its creation closer to the usage.
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.
Still, a lot of problems, starting from basics that are code formatting, ending with memory leaks, possible memory override and in general, oddities.
As we discussed on Discord, I recommend allocating dstGray() in the caller code, e.g. in the scaleFont() calls and pass it to the scaleSingleGlyph(). This way you just avoid any modifications to the class state. Instead, you decided to go your own way, but the implementation is very problematic. I outlined it in the comments.
engines/testbed/graphics.cpp
Outdated
| for (int i = origFont->getFontHeight(); i <= 20; i++) { | ||
|
|
||
| //const Graphics::BdfFont *font = Graphics::BdfFont::scaleFont(origFont, i); | ||
| // const Graphics::BdfFont *font = Graphics::BdfFont::scaleFont(origFont, i); |
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.
What is the purpose of this change?
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.
fixed this
graphics/fonts/bdf.cpp
Outdated
| : _data(data), _dispose(dispose) { | ||
| } | ||
|
|
||
| // BdfFont::BdfFont(DisposeAfterUse::Flag dispose) |
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.
What is the purpose of this code?
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.
removed the comments
graphics/fonts/bdf.cpp
Outdated
| } | ||
|
|
||
| void BdfFont::assignFontData(const BdfFontData &data) { | ||
| _data = data; |
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.
And what is happening with the previous thing which was in _data?
graphics/fonts/bdf.cpp
Outdated
|
|
||
| float scale = (float)newSize / (float)src->getFontSize(); | ||
|
|
||
| BdfFont *scaledFont = new BdfFont(src->_data, DisposeAfterUse::YES); |
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.
Why are you passing src->_data here when later you just overwrite it? Please, avoid it.
graphics/fonts/bdf.cpp
Outdated
| return new BdfFont(data, DisposeAfterUse::YES); | ||
| } | ||
|
|
||
| void BdfFont::assignFontData(const BdfFontData &data) { |
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.
What about the _dispose flag? Also, in accordance with other similar methods in other classes, it is better name it setData()
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 initialize the Font using the constructor, and this method only changes the data. I will rename it to setData. _dispose flag is handled by the constructor used at the beginning
graphics/fonts/bdf.h
Outdated
| class BdfFont : public Font { | ||
| public: | ||
| BdfFont(const BdfFontData &data, DisposeAfterUse::Flag dispose); | ||
| // BdfFont(DisposeAfterUse::Flag dispose); |
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.
Why is it here?
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.
removed this
graphics/fonts/bdf.cpp
Outdated
| const int bytes = dstPitch * box.height; | ||
| #if DRAWDEBUG | ||
| if (i == ccc) { | ||
| int *dstGray = *dstGrayPointer; |
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.
What is the purpose of this declaration which immediately goes out of scope?
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.
dstGray is being used in the debug part of code, so needed declaration. It was getting its value from dstGrayPointer defined above, now changed it and removed dstGrayPointer, as it was redundant.
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.
If you want to declare it, then you declare it incorrectly. Try to compile with DRAWDEBUG on. It will not even compile!
graphics/fonts/bdf.cpp
Outdated
| const int bytes = ((box.width + 7) / 8) * box.height; // Dimensions have been already corrected | ||
| int *dstGrayPointer = 0; | ||
| Graphics::Surface *srcSurf = scaledFont->scaleSingleGlyph(box.width, box.height, grayLevel, i + src->_data.firstCharacter, | ||
| box.xOffset, src->_data.height, srcBox.width, scale, &dstGrayPointer); |
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.
Okay, now you allocate dstGrayPointer in scaleSingleGlyph(). Where do you free this 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.
it is happening in the deconstructor
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.
sorry I missunderstood, I have replace it to dstGra, which is required for debuging
2c4dff0 to
9672dad
Compare
graphics/fonts/bdf.cpp
Outdated
| byte b = 0; | ||
|
|
||
| for (int x = 0; x < box.width; x++) { | ||
| for (int x = 0; x < box.width; x++,srcd++) { |
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.
This does not conform to our code formatting guidelines.
- The refactoring code for MacFont that magnifies the font on a surface and then uses a grayscalemap to refactor the scaling, is pushed back into a resuable method in the base font class. - surface magnigy funtion transfer and scaleSingleGlyph method add to the font class.
- The primary change is to fix or improve the scaling of BDF fonts - Now uses the same approach as in MacFonts scaling that is creating another surface and refactor it with a grayscalemap - Bugs - there are still some problems when testing in capital letters
|
Thank you! |