@@ -101,12 +101,16 @@ void main()
101101
102102std::string startupFile;
103103
104- Zep::NVec2f GetDisplayScale ()
104+ Zep::NVec2f GetPixelScale ()
105105{
106106 float ddpi = 0 .0f ;
107107 float hdpi = 0 .0f ;
108108 float vdpi = 0 .0f ;
109- auto res = SDL_GetDisplayDPI (0 , &ddpi, &hdpi, &vdpi);
109+
110+ auto window = SDL_GL_GetCurrentWindow ();
111+ auto index = window ? SDL_GetWindowDisplayIndex (window) : 0 ;
112+
113+ auto res = SDL_GetDisplayDPI (index, &ddpi, &hdpi, &vdpi);
110114 if (res == 0 && hdpi != 0 )
111115 {
112116 return Zep::NVec2f (hdpi, vdpi) / 96 .0f ;
@@ -153,15 +157,44 @@ bool ReadCommandLine(int argc, char** argv, int& exitCode)
153157}
154158
155159// A helper struct to init the editor and handle callbacks
156- struct ZepContainer : public IZepComponent , public IZepReplProvider
160+ struct ZepContainerImGui : public IZepComponent , public IZepReplProvider
157161{
158- ZepContainer (const std::string& startupFilePath, const std::string& configPath)
159- : spEditor(std::make_unique<ZepEditor_ImGui>(configPath))
162+ ZepContainerImGui (const std::string& startupFilePath, const std::string& configPath)
163+ : spEditor(std::make_unique<ZepEditor_ImGui>(configPath, GetPixelScale() ))
160164 {
161165 chibi_init (scheme, SDL_GetBasePath ());
162166
167+ // ZepEditor_ImGui will have created the fonts for us; but we need to build
168+ // the font atlas
169+ auto fontPath = std::string (SDL_GetBasePath ()) + " Cousine-Regular.ttf" ;
170+ auto & display = static_cast <ZepDisplay_ImGui&>(spEditor->GetDisplay ());
171+
172+ int fontPixelHeight = (int )dpi_pixel_height_from_point_size (DemoFontPtSize, GetPixelScale ().y );
173+
174+ auto & io = ImGui::GetIO ();
175+ ImVector<ImWchar> ranges;
176+ ImFontGlyphRangesBuilder builder;
177+ builder.AddRanges (io.Fonts ->GetGlyphRangesDefault ()); // Add one of the default ranges
178+ builder.AddRanges (io.Fonts ->GetGlyphRangesCyrillic ()); // Add one of the default ranges
179+ builder.AddRanges (greek_range);
180+ builder.BuildRanges (&ranges); // Build the final result (ordered ranges with all the unique characters submitted)
181+
182+ ImFontConfig cfg;
183+ cfg.OversampleH = 4 ;
184+ cfg.OversampleV = 4 ;
185+
186+ auto pImFont = ImGui::GetIO ().Fonts ->AddFontFromFileTTF (fontPath.c_str (), float (fontPixelHeight), &cfg, ranges.Data );
187+
188+ display.SetFont (ZepTextType::UI, std::make_shared<ZepFont_ImGui>(display, pImFont, fontPixelHeight));
189+ display.SetFont (ZepTextType::Text, std::make_shared<ZepFont_ImGui>(display, pImFont, fontPixelHeight));
190+ display.SetFont (ZepTextType::Heading1, std::make_shared<ZepFont_ImGui>(display, pImFont, int (fontPixelHeight * 1.75 )));
191+ display.SetFont (ZepTextType::Heading2, std::make_shared<ZepFont_ImGui>(display, pImFont, int (fontPixelHeight * 1.5 )));
192+ display.SetFont (ZepTextType::Heading3, std::make_shared<ZepFont_ImGui>(display, pImFont, int (fontPixelHeight * 1.25 )));
193+
194+ unsigned int flags = 0 ; // ImGuiFreeType::NoHinting;
195+ ImGuiFreeType::BuildFontAtlas (ImGui::GetIO ().Fonts , flags);
196+
163197 spEditor->RegisterCallback (this );
164- spEditor->SetPixelScale (GetDisplayScale ());
165198
166199 ZepMode_Orca::Register (*spEditor);
167200
@@ -198,7 +231,7 @@ struct ZepContainer : public IZepComponent, public IZepReplProvider
198231#endif
199232 }
200233
201- ZepContainer ()
234+ ZepContainerImGui ()
202235 {
203236 spEditor->UnRegisterCallback (this );
204237 }
@@ -230,7 +263,7 @@ struct ZepContainer : public IZepComponent, public IZepReplProvider
230263 if (range.first >= range.second )
231264 return " <No Expression>" ;
232265
233- const auto & text = buffer.GetGapBuffer ();
266+ const auto & text = buffer.GetWorkingBuffer ();
234267 auto eval = std::string (text.begin () + range.first .Index (), text.begin () + range.second .Index ());
235268
236269 // Flash the evaluated expression
@@ -324,7 +357,7 @@ struct ZepContainer : public IZepComponent, public IZepReplProvider
324357 if (pSyntax->GetSyntaxAt (spTipMsg->location ).foreground == ThemeColor::Identifier)
325358 {
326359 auto spMarker = std::make_shared<RangeMarker>(*spTipMsg->pBuffer );
327- spMarker->description = " This is an identifier" ;
360+ spMarker->SetDescription ( " This is an identifier" ) ;
328361 spMarker->SetHighlightColor (ThemeColor::Identifier);
329362 spMarker->SetTextColor (ThemeColor::Text);
330363 spTipMsg->spMarker = spMarker;
@@ -333,7 +366,7 @@ struct ZepContainer : public IZepComponent, public IZepReplProvider
333366 else if (pSyntax->GetSyntaxAt (spTipMsg->location ).foreground == ThemeColor::Keyword)
334367 {
335368 auto spMarker = std::make_shared<RangeMarker>(*spTipMsg->pBuffer );
336- spMarker->description = " This is a keyword" ;
369+ spMarker->SetDescription ( " This is a keyword" ) ;
337370 spMarker->SetHighlightColor (ThemeColor::Keyword);
338371 spMarker->SetTextColor (ThemeColor::Text);
339372 spTipMsg->spMarker = spMarker;
@@ -436,37 +469,17 @@ int main(int argc, char** argv)
436469 ImGui_ImplSDL2_InitForOpenGL (window, gl_context);
437470 ImGui_ImplOpenGL3_Init (glsl_version);
438471
472+ // ** Zep specific code, before Initializing font map
473+ ZepContainerImGui zep (startupFile, SDL_GetBasePath ());
474+
439475 // Setup style
440476 ImGui::StyleColorsDark ();
441477
442- ImVector<ImWchar> ranges;
443- ImFontGlyphRangesBuilder builder;
444- builder.AddRanges (io.Fonts ->GetGlyphRangesDefault ()); // Add one of the default ranges
445- builder.AddRanges (io.Fonts ->GetGlyphRangesCyrillic ()); // Add one of the default ranges
446- // builder.AddRanges(io.Fonts->GetGlyphRangesThai()); // Add one of the default ranges
447- ImWchar greek_range[] = { 0x300 , 0x52F , 0x1f00 , 0x1fff , 0 , 0 };
448- builder.AddRanges (greek_range);
449- builder.BuildRanges (&ranges); // Build the final result (ordered ranges with all the unique characters submitted)
450-
451- ImFontConfig cfg;
452- cfg.OversampleH = 4 ;
453- cfg.OversampleV = 4 ;
454-
455- float fontPixelHeight = dpi_pixel_height_from_point_size (DemoFontPtSize, GetDisplayScale ().y );
456- io.Fonts ->AddFontFromFileTTF ((std::string (SDL_GetBasePath ()) + " Cousine-Regular.ttf" ).c_str (), fontPixelHeight, &cfg, ranges.Data );
457-
458- ZLOG (INFO, " DPI Scale: " << MUtils::NVec2f (GetDisplayScale ().x , GetDisplayScale ().y ));
459- ZLOG (INFO, " Font Pixel Size: " << fontPixelHeight);
460-
461- unsigned int flags = 0 ; // ImGuiFreeType::NoHinting;
462- ImGuiFreeType::BuildFontAtlas (io.Fonts , flags);
478+ ZLOG (INFO, " DPI Scale: " << MUtils::NVec2f (GetPixelScale ().x , GetPixelScale ().y ));
463479
464480 bool show_demo_window = false ;
465481 ImVec4 clear_color = ImVec4 (0 .45f , 0 .55f , 0 .60f , 1 .00f );
466482
467- // ** Zep specific code
468- ZepContainer zep (startupFile, SDL_GetBasePath ());
469-
470483 MUtils::TimeProvider::Instance ().StartThread ();
471484
472485 // Main loop
0 commit comments