mekhontsev / imgui_md Goto Github PK
View Code? Open in Web Editor NEWMarkdown renderer for Dear ImGui using MD4C parser
License: MIT License
Markdown renderer for Dear ImGui using MD4C parser
License: MIT License
Hello
Thanks for sharing your wonderful library. I have added a couple of features buy I won't make a pull request because the custom renderer I'm using make it incompatible with the standard font handling and also I realized too late that I have my text editor configured to change tabs to spaces.
However I'll post here the relevant code fragments in case you find them useful.
Here's how it looks:
And now the code.
For numeric character references:
#include <charconv>
// Adapted from RapidJSON (MIT licence)
static std::string UTF8Encoder(unsigned int aCodePoint)
{
std::string res="";
if (aCodePoint <= 0x79)
{
res.push_back(static_cast<char>(aCodePoint & 0xFF));
}
else if (aCodePoint <= 0x7FF)
{
res.push_back(static_cast<char>(0xC0 | ((aCodePoint >> 6) & 0xFF)));
res.push_back(static_cast<char>(0x80 | ((aCodePoint & 0x3F))));
}
else if (aCodePoint <= 0xFFFF)
{
res.push_back(static_cast<char>(0xE0 | ((aCodePoint >> 12) & 0xFF)));
res.push_back(static_cast<char>(0x80 | ((aCodePoint >> 6) & 0x3F)));
res.push_back(static_cast<char>(0x80 | (aCodePoint & 0x3F)));
}
else
{
//ASSERT(codepoint <= 0x10FFFF);
res.push_back(static_cast<char>(0xF0 | ((aCodePoint >> 18) & 0xFF)));
res.push_back(static_cast<char>(0x80 | ((aCodePoint >> 12) & 0x3F)));
res.push_back(static_cast<char>(0x80 | ((aCodePoint >> 6) & 0x3F)));
res.push_back(static_cast<char>(0x80 | (aCodePoint & 0x3F)));
}
return res;
}
bool imgui_md::render_entity(const char* str, const char* str_end)
{
const size_t sz = str_end - str;
if (strncmp(str, " ", sz) == 0) {
ImGui::TextUnformatted(""); ImGui::SameLine();
return true;
}
if(sz<4)
{
return false;
}
if (strncmp(str, "&#", 2) == 0 )
{
int base,offset;
if(*(str+2)=='x' || *(str+2)=='X')
{
base=16;
offset=3;
}
else
{
base=10;
offset=2;
}
int code_point;
auto [ptr, ec] = std::from_chars(str+offset, str_end, code_point,base);
if (ec == std::errc())
{
if(code_point>= 0xD800 && code_point<=0xDBFF)
{
// TODO: Handle UTF-16 surrogate pair
return false;
}
std::string utf8_char=UTF8Encoder(code_point);
ImGui::TextUnformatted(utf8_char.c_str(),utf8_char.c_str()+utf8_char.size()); ImGui::SameLine(0.0f,0.0f);
return true;
}
}
return false;
}
For task list:
I have added a member variable to personalize indent.
float m_indent_size=40.0f;
And now the BLOCK_LI function becomes:
void imgui_md::BLOCK_LI(const MD_BLOCK_LI_DETAIL* aDetailBlockLI, bool e)
{
if (e) {
ImGui::NewLine();
list_info& nfo = m_list_stack.back();
if (nfo.is_ol) {
ImGui::Text("%d%c", nfo.cur_ol++, nfo.delim);
ImGui::SameLine();
} else {
if (nfo.delim == '*') {
float cx = ImGui::GetCursorPosX();
cx -= ImGui::GetStyle().FramePadding.x * 2;
ImGui::SetCursorPosX(cx);
ImGui::Bullet();
} else {
ImGui::Text("%c", nfo.delim);
ImGui::SameLine();
}
}
ImGui::Indent(m_indent_size);
if(aDetailBlockLI->is_task)
{
bool v=(aDetailBlockLI->task_mark=='x' || aDetailBlockLI->task_mark=='X');
ImGui::Checkbox("##task",&v);
ImGui::SameLine(0.0f,m_indent_size*0.25f);
}
} else {
ImGui::Unindent(m_indent_size);
}
}
Hope you find it useful.
I'm happy to announce the first public release of Dear ImGui Bundle. Dear ImGui Bundle is a bundle for Dear ImGui, including many powerful libraries from its ecosystem. It enables to easily create ImGui applications in C++ and Python, under Windows, macOS, Linux, emscripten. It is aimed at application developers, researchers, and beginner developers who want to quickly get started.
imgui_md is a included in the widgets provided with it, and it is a central part, since it enables to display all the doc and code snippets. It is a joy to use, in C++ and Python. I used imgui_md, to which I added a lot of customizations.
@mekhontsev : sorry to hijack the issues list for this, and feel to close this issue; as this is just for your information. I informed @ocornut yesterday, and I plan to communicate about it in the ImGui gallery in the next hours.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.