Giter Club home page Giter Club logo

imgui_md's Issues

Added support for numeric character references and task lists

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:

image

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, "&nbsp;", 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.

Annoucing Dear ImGui Bundle including imgui_md and many others; with support for C++, Python and emscripten

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.

how to render utf-8

hello, i wanto use this lib like : ImGuiMd::RenderUnindented("中文");
but don't work

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.