Skip to content

Commit a0a059c

Browse files
committed
Removed all calls to ImFont::CalcTextSizeA() and replaced with ImGui::CalcTextSize(). Fixes #851.
1 parent 5e4b595 commit a0a059c

6 files changed

Lines changed: 49 additions & 34 deletions

File tree

src/ngscopeclient/FilterGraphEditor.cpp

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -739,9 +739,8 @@ void FilterGraphEditor::DoNodeForGroupInputs(shared_ptr<FilterGraphGroup> group)
739739
auto gpos = ax::NodeEditor::GetNodePosition(gid);
740740

741741
//Figure out how big the port text is
742-
auto textfont = ImGui::GetFont();
743742
float oportmax = 1;
744-
float iportmax = textfont->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, 0, "").x;
743+
float iportmax = ImGui::CalcTextSize("").x;
745744
vector<string> onames;
746745
for(auto it : group->m_hierInputMap)
747746
{
@@ -759,7 +758,7 @@ void FilterGraphEditor::DoNodeForGroupInputs(shared_ptr<FilterGraphGroup> group)
759758
auto name = sinkname + "";
760759
onames.push_back(name);
761760
oportmax = max(oportmax,
762-
textfont->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, 0, name.c_str()).x +
761+
ImGui::CalcTextSize(name.c_str()).x +
763762
ImGui::GetFontSize() * 2);
764763
}
765764
float nodewidth = oportmax + iportmax + 1*ImGui::GetStyle().ItemSpacing.x;
@@ -848,17 +847,16 @@ void FilterGraphEditor::DoNodeForGroupOutputs(shared_ptr<FilterGraphGroup> group
848847
auto gsz = ax::NodeEditor::GetNodeSize(gid);
849848

850849
//Figure out how big the port text is
851-
auto textfont = ImGui::GetFont();
852850
float oportmax = 1;
853-
float iportmax = textfont->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, 0, "").x;
851+
float iportmax = ImGui::CalcTextSize("").x;
854852
vector<string> onames;
855853
for(auto it : group->m_hierOutputMap)
856854
{
857855
auto stream = it.first;
858856

859857
auto name = stream.GetName() + "";
860858
onames.push_back(name);
861-
oportmax = max(oportmax, textfont->CalcTextSizeA(ImGui::GetFontSize(), FLT_MAX, 0, name.c_str()).x);
859+
oportmax = max(oportmax, ImGui::CalcTextSize(name.c_str()).x);
862860
}
863861
float nodewidth = oportmax + iportmax + 3*ImGui::GetStyle().ItemSpacing.x;
864862

@@ -1886,7 +1884,7 @@ void FilterGraphEditor::DoNodeForTrigger(Trigger* trig)
18861884
headerText = trig->GetScope()->m_nickname + ": " + headerText;
18871885

18881886
//Figure out how big the header text is and reserve space for it
1889-
auto headerSize = headerfont.first->CalcTextSizeA(headerfontsize, FLT_MAX, 0, headerText.c_str());
1887+
auto headerSize = CalcTextSizeForFont(headerfont, headerText.c_str());
18901888
float nodewidth = max(15*tsize, headerSize.x);
18911889
ImGui::Dummy(ImVec2(nodewidth, headerheight));
18921890

@@ -1995,7 +1993,7 @@ void FilterGraphEditor::DoNodeForChannel(
19951993
headerText = inst->m_nickname + ": " + headerText;
19961994

19971995
//Figure out how big the header text is
1998-
auto headerSize = headerfont.first->CalcTextSizeA(headerfontsize, FLT_MAX, 0, headerText.c_str());
1996+
auto headerSize = CalcTextSizeForFont(headerfont, headerText.c_str());
19991997

20001998
//Format block type early, even though it's not drawn until later
20011999
//so that we know how much space to allocate
@@ -2021,7 +2019,7 @@ void FilterGraphEditor::DoNodeForChannel(
20212019
blocktype = "Hardware input";
20222020
}
20232021
ImVec2 iconsize(ImGui::GetFontSize() * 6, ImGui::GetFontSize() * 3);
2024-
auto captionsize = textfont.first->CalcTextSizeA(textfontsize, FLT_MAX, 0, blocktype.c_str());
2022+
auto captionsize = CalcTextSizeForFont(textfont, blocktype.c_str());
20252023

20262024
//Reserve space for the center icon and node type caption
20272025
float iconwidth = max(iconsize.x, captionsize.x);
@@ -2035,13 +2033,13 @@ void FilterGraphEditor::DoNodeForChannel(
20352033
{
20362034
auto name = string("") + channel->GetInputName(i);
20372035
inames.push_back(name);
2038-
iportmax = max(iportmax, textfont.first->CalcTextSizeA(textfontsize, FLT_MAX, 0, name.c_str()).x);
2036+
iportmax = max(iportmax, CalcTextSizeForFont(textfont, name.c_str()).x);
20392037
}
20402038
for(size_t i=0; i<channel->GetStreamCount(); i++)
20412039
{
20422040
auto name = channel->GetStreamName(i) + "";
20432041
onames.push_back(name);
2044-
oportmax = max(oportmax, textfont.first->CalcTextSizeA(textfontsize, FLT_MAX, 0, name.c_str()).x);
2042+
oportmax = max(oportmax, CalcTextSizeForFont(textfont, name.c_str()).x);
20452043
}
20462044
float colswidth = iportmax + oportmax + iconwidth;
20472045
float nodewidth = max(colswidth, headerSize.x) + 3*ImGui::GetStyle().ItemSpacing.x;
@@ -2161,7 +2159,7 @@ void FilterGraphEditor::DoNodeForChannel(
21612159
if(runtime > 0)
21622160
{
21632161
auto runtimeText = fs.PrettyPrint(runtime, 3);
2164-
auto runtimeSize = headerfont.first->CalcTextSizeA(headerfontsize, FLT_MAX, 0, runtimeText.c_str());
2162+
auto runtimeSize = CalcTextSizeForFont(headerfont, runtimeText.c_str());
21652163

21662164
auto timebgColor = ColorFromString("#404040");
21672165
auto timeTextColor = ColorFromString("#ffffff");

src/ngscopeclient/FontManager.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,18 @@ void FontManager::AddFontDescriptions(PreferenceCategory& cat, set<FontDescripti
136136
}
137137
}
138138
}
139+
140+
/**
141+
@brief Wrapper around ImGui::CalcTextSize that uses an explicit font
142+
*/
143+
ImVec2 CalcTextSizeForFont(
144+
FontWithSize font,
145+
const char* text,
146+
bool hide_text_after_double_hash,
147+
float wrap_width)
148+
{
149+
ImGui::PushFont(font.first, font.second);
150+
ImVec2 size = ImGui::CalcTextSize(text, nullptr, hide_text_after_double_hash, wrap_width);
151+
ImGui::PopFont();
152+
return size;
153+
}

src/ngscopeclient/FontManager.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,10 @@ class FontManager
6666
std::map<FontDescription, ImFont*> m_fonts;
6767
};
6868

69+
ImVec2 CalcTextSizeForFont(
70+
FontWithSize font,
71+
const char* text,
72+
bool hide_text_after_double_hash = false,
73+
float wrap_width = -1.0f);
74+
6975
#endif

src/ngscopeclient/ProtocolAnalyzerDialog.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ void ProtocolAnalyzerDialog::DoImageColumn(Packet* pack, vector<RowData>& rows,
566566
*/
567567
void ProtocolAnalyzerDialog::DoDataColumn(Packet* pack, FontWithSize dataFont, vector<RowData>& rows, size_t nrow)
568568
{
569+
ImGui::PushFont(dataFont.first, dataFont.second);
570+
569571
//When drawing the first cell, figure out dimensions for subsequent stuff
570572
if(m_firstDataBlockOfFrame)
571573
{
@@ -575,7 +577,7 @@ void ProtocolAnalyzerDialog::DoDataColumn(Packet* pack, FontWithSize dataFont, v
575577
//Figure out how many characters of text we can fit in the data region
576578
//This assumes data font is fixed width, may break if user chooses variable width.
577579
//But hex dumps with variable width will look horrible anyway so that's probably not a problem?
578-
auto fontwidth = dataFont.first->CalcTextSizeA(dataFont.second, FLT_MAX, -1, "W").x;
580+
auto fontwidth = ImGui::CalcTextSize("W").x;
579581
size_t charsPerLine = floor(xsize / fontwidth);
580582

581583
//TODO: use 2-nibble address if packet has <256 bytes of data
@@ -604,7 +606,10 @@ void ProtocolAnalyzerDialog::DoDataColumn(Packet* pack, FontWithSize dataFont, v
604606
}
605607

606608
if(m_bytesPerLine <= 0)
609+
{
610+
ImGui::PopFont();
607611
return;
612+
}
608613
}
609614

610615
string firstLine;
@@ -615,7 +620,6 @@ void ProtocolAnalyzerDialog::DoDataColumn(Packet* pack, FontWithSize dataFont, v
615620
string lineAscii;
616621

617622
//Create the tree node early - before we've even rendered any data - so we know the open / closed state
618-
ImGui::PushFont(dataFont.first, dataFont.second);
619623
bool open = false;
620624
if(!bytes.empty())
621625
{

src/ngscopeclient/WaveformArea.cpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ void WaveformArea::RenderYAxisCursors(ImVec2 pos, ImVec2 size, float yAxisWidth)
821821
//Text
822822
//Anchor bottom left at the cursor
823823
auto str = string("Y1: ") + m_yAxisUnit.PrettyPrint(m_yAxisCursorPositions[0]);
824-
auto tsize = font.first->CalcTextSizeA(font.second, FLT_MAX, 0.0, str.c_str());
824+
auto tsize = CalcTextSizeForFont(font, str.c_str());
825825
float padding = 2;
826826
float wrounding = 2;
827827
float textTop = ypos0 - (3*padding + tsize.y);
@@ -849,7 +849,7 @@ void WaveformArea::RenderYAxisCursors(ImVec2 pos, ImVec2 size, float yAxisWidth)
849849
"ΔY = " + m_yAxisUnit.PrettyPrint(delta);
850850

851851
//Text
852-
tsize = font.first->CalcTextSizeA(font.second, FLT_MAX, 0.0, str.c_str());
852+
tsize = CalcTextSizeForFont(font, str.c_str());
853853
textTop = ypos1 - (3*padding + tsize.y);
854854
textLeft = plotRight - (2*padding + tsize.x);
855855
list->AddRectFilled(
@@ -869,7 +869,7 @@ void WaveformArea::RenderYAxisCursors(ImVec2 pos, ImVec2 size, float yAxisWidth)
869869
else if(m_dragState == DRAG_STATE_Y_CURSOR1)
870870
m_dragState = DRAG_STATE_NONE;
871871

872-
//TODO: text for value readouts, in-band power, etc
872+
//TODO: text for value readouts, in-band power, etc?
873873
}
874874
ImGui::EndChild();
875875

@@ -1450,7 +1450,7 @@ void WaveformArea::RenderSpectrumPeaks(ImDrawList* list, shared_ptr<DisplayedCha
14501450
"X = " + stream.GetXAxisUnits().PrettyPrint(label.m_peakXpos) + "\n" +
14511451
"Y = " + stream.GetYAxisUnits().PrettyPrint(label.m_peakYpos) + "\n" +
14521452
"FWHM = " + stream.GetXAxisUnits().PrettyPrint(label.m_fwhm);
1453-
auto textSizePixels = font.first->CalcTextSizeA(font.second, FLT_MAX, 0, str.c_str());
1453+
auto textSizePixels = CalcTextSizeForFont(font, str.c_str());
14541454

14551455
//Create rectangle for box around centroid
14561456
float padding = 2;
@@ -1798,7 +1798,7 @@ void WaveformArea::RenderComplexSignal(
17981798
if(available_width > 15)
17991799
{
18001800
auto font = m_parent->GetFontPref("Appearance.Decodes.protocol_font");
1801-
auto textsize = font.first->CalcTextSizeA(font.second, FLT_MAX, 0, str.c_str());
1801+
auto textsize = CalcTextSizeForFont(font, str.c_str());
18021802

18031803
//Minimum width (if outline ends up being smaller than this, just fill)
18041804
float min_width = 40;
@@ -1874,7 +1874,7 @@ void WaveformArea::RenderComplexSignal(
18741874
else
18751875
str_render = "..." + str.substr(str.length() - len - 1);
18761876

1877-
textsize = font.first->CalcTextSizeA(font.second, FLT_MAX, 0, str_render.c_str());
1877+
textsize = CalcTextSizeForFont(font, str_render.c_str());
18781878
if(textsize.x < available_width)
18791879
{
18801880
//Re-center text in available space
@@ -2702,7 +2702,7 @@ void WaveformArea::RenderYAxis(ImVec2 size, map<float, float>& gridmap, float vb
27022702
float vhi = YPositionToYAxisUnits(it.second + 0.5);
27032703
auto label = m_yAxisUnit.PrettyPrintRange(vlo, vhi, vbot, vtop);
27042704

2705-
auto tsize = font.first->CalcTextSizeA(font.second, FLT_MAX, 0, label.c_str());
2705+
auto tsize = CalcTextSizeForFont(font, label.c_str());
27062706
float y = it.second - tsize.y/2;
27072707
if(y > ybot)
27082708
continue;
@@ -2994,11 +2994,7 @@ void WaveformArea::CheckForScaleMismatch(ImVec2 start, ImVec2 size)
29942994

29952995
//Draw background for text
29962996
float wrapWidth = 40 * fontHeight;
2997-
auto textsize = font.first->CalcTextSizeA(
2998-
fontHeight,
2999-
FLT_MAX,
3000-
wrapWidth,
3001-
str.c_str());
2997+
auto textsize = CalcTextSizeForFont(font, str.c_str(), false, wrapWidth);
30022998
float padding = 5;
30032999
float wrounding = 2;
30043000
list->AddRectFilled(
@@ -3623,11 +3619,7 @@ void WaveformArea::DrawDropRangeMismatchMessage(
36233619

36243620
//Draw background for text
36253621
float wrapWidth = 40 * fontHeight;
3626-
auto textsize = font.first->CalcTextSizeA(
3627-
fontHeight,
3628-
FLT_MAX,
3629-
wrapWidth,
3630-
str.c_str());
3622+
auto textsize = CalcTextSizeForFont(font, str.c_str(), false, wrapWidth);
36313623
float padding = 5;
36323624
float wrounding = 2;
36333625
list->AddRectFilled(

src/ngscopeclient/WaveformGroup.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ void WaveformGroup::RenderMarkers(ImVec2 pos, ImVec2 size)
523523
//Text
524524
//Anchor bottom right at the cursor
525525
auto str = m.m_name + ": " + m_xAxisUnit.PrettyPrint(m.m_offset);
526-
auto tsize = font.first->CalcTextSizeA(font.second, FLT_MAX, 0.0, str.c_str());
526+
auto tsize = CalcTextSizeForFont(font, str.c_str());
527527
float padding = 2;
528528
float wrounding = 2;
529529
float textTop = pos.y + m_timelineHeight - (padding + tsize.y);
@@ -683,7 +683,7 @@ void WaveformGroup::RenderXAxisCursors(ImVec2 pos, ImVec2 size)
683683
//Text
684684
//Anchor bottom right at the cursor
685685
auto str = string("X1: ") + m_xAxisUnit.PrettyPrint(m_xAxisCursorPositions[0]);
686-
auto tsize = font.first->CalcTextSizeA(font.second, FLT_MAX, 0.0, str.c_str());
686+
auto tsize = CalcTextSizeForFont(font, str.c_str());
687687
float padding = 2;
688688
float wrounding = 2;
689689
float textTop = pos.y + m_timelineHeight - (padding + tsize.y);
@@ -714,7 +714,7 @@ void WaveformGroup::RenderXAxisCursors(ImVec2 pos, ImVec2 size)
714714
str += string(" (") + hz.PrettyPrint(FS_PER_SECOND / delta) + ")";
715715

716716
//Text
717-
tsize = font.first->CalcTextSizeA(font.second, FLT_MAX, 0.0, str.c_str());
717+
tsize = CalcTextSizeForFont(font, str.c_str());
718718
textTop = pos.y + m_timelineHeight - (padding + tsize.y);
719719
list->AddRectFilled(
720720
ImVec2(xpos1 + 1, textTop - padding ),

0 commit comments

Comments
 (0)