3268: Fix ScaleFontSize with too long strings r=Jupeyy a=def-

Thanks to PopCorn181 for report
![screenshot_2020-11-08_00-03-19](https://user-images.githubusercontent.com/2335377/98453118-d1c1ae00-2155-11eb-97cf-cd5a455c00d3.png)

<!-- What is the motivation for the changes of this pull request -->

## Checklist

- [x] Tested the change ingame
- [x] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [x] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [x] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: def <dennis@felsin9.de>
This commit is contained in:
bors[bot] 2020-11-07 23:07:38 +00:00 committed by GitHub
commit 848bbf543d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 12 deletions

View file

@ -897,7 +897,7 @@ public:
//the outlined texture is always the same size as the current
float UVScale = 1.0f / pFont->m_CurTextureDimensions[0];
const char *pCurrent = (char *)pText;
const char *pCurrent = pText;
const char *pEnd = pCurrent + Length;
if((m_RenderFlags & TEXT_RENDER_FLAG_NO_PIXEL_ALIGMENT) != 0)
@ -941,7 +941,7 @@ public:
const char *pBatchEnd = pEnd;
if(pCursor->m_LineWidth > 0 && !(pCursor->m_Flags & TEXTFLAG_STOP_AT_END))
{
int Wlen = minimum(WordLength((char *)pCurrent), (int)(pEnd - pCurrent));
int Wlen = minimum(WordLength(pCurrent), (int)(pEnd - pCurrent));
CTextCursor Compare = *pCursor;
Compare.m_X = DrawX;
Compare.m_Y = DrawY;

View file

@ -2962,9 +2962,9 @@ void CEditor::DoMapEditor(CUIRect View)
//UI()->ClipDisable();
}
float CEditor::ScaleFontSize(char *aBuf, float FontSize, int Width)
float CEditor::ScaleFontSize(char *pText, int TextSize, float FontSize, int Width)
{
while(TextRender()->TextWidth(0, FontSize, aBuf, -1, -1.0f) > Width)
while(TextRender()->TextWidth(0, FontSize, pText, -1, -1.0f) > Width)
{
if(FontSize > 6.0f)
{
@ -2972,8 +2972,8 @@ float CEditor::ScaleFontSize(char *aBuf, float FontSize, int Width)
}
else
{
aBuf[str_length(aBuf) - 4] = '\0';
str_append(aBuf, "...", sizeof(aBuf));
pText[str_length(pText) - 4] = '\0';
str_append(pText, "...", TextSize);
}
}
return FontSize;
@ -3140,7 +3140,7 @@ int CEditor::DoProperties(CUIRect *pToolBox, CProperty *pProps, int *pIDs, int *
else
str_format(aBuf, sizeof(aBuf), "%s", m_Map.m_lImages[pProps[i].m_Value]->m_aName);
float FontSize = ScaleFontSize(aBuf, 10.0f, Shifter.w);
float FontSize = ScaleFontSize(aBuf, sizeof(aBuf), 10.0f, Shifter.w);
if(DoButton_Ex(&pIDs[i], aBuf, 0, &Shifter, 0, 0, CUI::CORNER_ALL, FontSize))
PopupSelectImageInvoke(pProps[i].m_Value, UI()->MouseX(), UI()->MouseY());
@ -3194,7 +3194,7 @@ int CEditor::DoProperties(CUIRect *pToolBox, CProperty *pProps, int *pIDs, int *
else
str_format(aBuf, sizeof(aBuf), "%s", m_Map.m_lSounds[pProps[i].m_Value]->m_aName);
float FontSize = ScaleFontSize(aBuf, 10.0f, Shifter.w);
float FontSize = ScaleFontSize(aBuf, sizeof(aBuf), 10.0f, Shifter.w);
if(DoButton_Ex(&pIDs[i], aBuf, 0, &Shifter, 0, 0, CUI::CORNER_ALL, FontSize))
PopupSelectSoundInvoke(pProps[i].m_Value, UI()->MouseX(), UI()->MouseY());
@ -3213,7 +3213,7 @@ int CEditor::DoProperties(CUIRect *pToolBox, CProperty *pProps, int *pIDs, int *
else
str_format(aBuf, sizeof(aBuf), "%s", m_Map.m_lImages[pProps[i].m_Min]->m_AutoMapper.GetConfigName(pProps[i].m_Value));
float FontSize = ScaleFontSize(aBuf, 10.0f, Shifter.w);
float FontSize = ScaleFontSize(aBuf, sizeof(aBuf), 10.0f, Shifter.w);
if(DoButton_Ex(&pIDs[i], aBuf, 0, &Shifter, 0, 0, CUI::CORNER_ALL, FontSize))
PopupSelectConfigAutoMapInvoke(pProps[i].m_Value, UI()->MouseX(), UI()->MouseY());
@ -3240,7 +3240,7 @@ int CEditor::DoProperties(CUIRect *pToolBox, CProperty *pProps, int *pIDs, int *
else
str_format(aBuf, sizeof(aBuf), "%d", CurValue);
float FontSize = ScaleFontSize(aBuf, 10.0f, Shifter.w);
float FontSize = ScaleFontSize(aBuf, sizeof(aBuf), 10.0f, Shifter.w);
RenderTools()->DrawUIRect(&Shifter, Color, 0, 5.0f);
UI()->DoLabel(&Shifter, aBuf, FontSize, 0, -1);
@ -4785,7 +4785,7 @@ void CEditor::RenderStatusbar(CUIRect View)
else
str_copy(aBuf, m_pTooltip, sizeof(aBuf));
float FontSize = ScaleFontSize(aBuf, 10.0f, View.w);
float FontSize = ScaleFontSize(aBuf, sizeof(aBuf), 10.0f, View.w);
UI()->DoLabel(&View, aBuf, FontSize, -1, View.w);
}
}

View file

@ -799,7 +799,7 @@ public:
bool IsQuadSelected(int Index);
int FindSelectedQuadIndex(int Index);
float ScaleFontSize(char *aBuf, float FontSize, int Width);
float ScaleFontSize(char *pText, int TextSize, float FontSize, int Width);
int DoProperties(CUIRect *pToolbox, CProperty *pProps, int *pIDs, int *pNewVal, ColorRGBA Color = ColorRGBA(1, 1, 1, 0.5f));
int m_Mode;