| 1 | #pragma once |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | #include <stdint.h> |
| 229 | |
| 230 | typedef int ImGuiMarkdownFormatFlags; |
| 231 | |
| 232 | enum ImGuiMarkdownFormatFlags_ |
| 233 | { |
| 234 | ImGuiMarkdownFormatFlags_None = 0, |
| 235 | ImGuiMarkdownFormatFlags_DiscardExtraNewLines = 1 << 0, |
| 236 | ImGuiMarkdownFormatFlags_NoNewLineBeforeHeading = 1 << 1, |
| 237 | ImGuiMarkdownFormatFlags_SeparatorDoesNotAdvance = 1 << 2, |
| 238 | ImGuiMarkdownFormatFlags_GithubStyle = ImGuiMarkdownFormatFlags_DiscardExtraNewLines | ImGuiMarkdownFormatFlags_NoNewLineBeforeHeading | ImGuiMarkdownFormatFlags_SeparatorDoesNotAdvance, |
| 239 | ImGuiMarkdownFormatFlags_CommonMarkAll = ImGuiMarkdownFormatFlags_DiscardExtraNewLines | ImGuiMarkdownFormatFlags_NoNewLineBeforeHeading | ImGuiMarkdownFormatFlags_SeparatorDoesNotAdvance, |
| 240 | }; |
| 241 | |
| 242 | namespace ImGui |
| 243 | { |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | struct Link; |
| 249 | struct MarkdownConfig; |
| 250 | |
| 251 | struct MarkdownLinkCallbackData |
| 252 | { |
| 253 | const char* text; |
| 254 | int textLength; |
| 255 | const char* link; |
| 256 | int linkLength; |
| 257 | void* userData; |
| 258 | bool isImage; |
| 259 | }; |
| 260 | |
| 261 | struct MarkdownTooltipCallbackData |
| 262 | { |
| 263 | MarkdownLinkCallbackData linkData; |
| 264 | const char* linkIcon; |
| 265 | }; |
| 266 | |
| 267 | struct MarkdownImageData |
| 268 | { |
| 269 | bool isValid = false; |
| 270 | bool useLinkCallback = false; |
| 271 | ImTextureID user_texture_id = {}; |
| 272 | ImVec2 size = ImVec2( 100.0f, 100.0f ); |
| 273 | ImVec2 uv0 = ImVec2( 0, 0 ); |
| 274 | ImVec2 uv1 = ImVec2( 1, 1 ); |
| 275 | ImVec4 tint_col = ImVec4( 1, 1, 1, 1 ); |
| 276 | ImVec4 border_col = ImVec4( 0, 0, 0, 0 ); |
| 277 | ImVec4 bg_col = ImVec4( 0, 0, 0, 0 ); |
| 278 | }; |
| 279 | |
| 280 | enum class MarkdownFormatType |
| 281 | { |
| 282 | NORMAL_TEXT, |
| 283 | HEADING, |
| 284 | UNORDERED_LIST, |
| 285 | LINK, |
| 286 | EMPHASIS, |
| 287 | }; |
| 288 | |
| 289 | struct MarkdownFormatInfo |
| 290 | { |
| 291 | MarkdownFormatType type = MarkdownFormatType::NORMAL_TEXT; |
| 292 | int32_t level = 0; |
| 293 | bool itemHovered = false; |
| 294 | const MarkdownConfig* config = NULL; |
| 295 | const char* text = NULL; |
| 296 | int32_t textLength = 0; |
| 297 | }; |
| 298 | |
| 299 | typedef void MarkdownLinkCallback( MarkdownLinkCallbackData data ); |
| 300 | typedef void MarkdownTooltipCallback( MarkdownTooltipCallbackData data ); |
| 301 | |
| 302 | inline void defaultMarkdownTooltipCallback( MarkdownTooltipCallbackData data_ ) |
| 303 | { |
| 304 | if( data_.linkData.isImage ) |
| 305 | { |
| 306 | ImGui::SetTooltip( "%.*s", data_.linkData.linkLength, data_.linkData.link ); |
| 307 | } |
| 308 | else |
| 309 | { |
| 310 | ImGui::SetTooltip( "%s Open in browser\n%.*s", data_.linkIcon, data_.linkData.linkLength, data_.linkData.link ); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | typedef MarkdownImageData MarkdownImageCallback( MarkdownLinkCallbackData data ); |
| 315 | typedef void MarkdownFormalCallback( const MarkdownFormatInfo& markdownFormatInfo_, bool start_ ); |
| 316 | |
| 317 | inline void defaultMarkdownFormatCallback( const MarkdownFormatInfo& markdownFormatInfo_, bool start_ ); |
| 318 | |
| 319 | struct MarkdownHeadingFormat |
| 320 | { |
| 321 | ImFont* font; |
| 322 | bool separator; |
| 323 | #ifdef IMGUI_HAS_TEXTURES |
| 324 | float fontSize = 0.0f; |
| 325 | #endif |
| 326 | }; |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | struct MarkdownConfig |
| 333 | { |
| 334 | static const int NUMHEADINGS = 3; |
| 335 | |
| 336 | MarkdownLinkCallback* linkCallback = NULL; |
| 337 | MarkdownTooltipCallback* tooltipCallback = NULL; |
| 338 | MarkdownImageCallback* imageCallback = NULL; |
| 339 | const char* linkIcon = ""; |
| 340 | MarkdownHeadingFormat headingFormats[ NUMHEADINGS ] = { { NULL, true }, { NULL, true }, { NULL, true } }; |
| 341 | void* userData = NULL; |
| 342 | MarkdownFormalCallback* formatCallback = defaultMarkdownFormatCallback; |
| 343 | ImGuiMarkdownFormatFlags formatFlags = ImGuiMarkdownFormatFlags_None; |
| 344 | }; |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | inline void Markdown( const char* markdown_, size_t markdownLength_, const MarkdownConfig& mdConfig_ ); |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | struct TextRegion; |
| 357 | struct Line; |
| 358 | inline void UnderLine( ImColor col_ ); |
| 359 | inline void RenderLine( const char* markdown_, Line& line_, TextRegion& textRegion_, const MarkdownConfig& mdConfig_ ); |
| 360 | |
| 361 | struct TextRegion |
| 362 | { |
| 363 | TextRegion() : indentX( 0.0f ) |
| 364 | { |
| 365 | } |
| 366 | ~TextRegion() |
| 367 | { |
| 368 | ResetIndent(); |
| 369 | } |
| 370 | |
| 371 | void RenderTextWrapped( const char* text_, const char* text_end_, bool bIndentToHere_ = false ); |
| 372 | |
| 373 | void RenderListTextWrapped( const char* text_, const char* text_end_ ) |
| 374 | { |
| 375 | ImGui::Bullet(); |
| 376 | ImGui::SameLine(); |
| 377 | RenderTextWrapped( text_, text_end_, true ); |
| 378 | } |
| 379 | |
| 380 | bool RenderLinkText( const char* text_, const char* text_end_, const Link& link_, |
| 381 | const char* markdown_, const MarkdownConfig& mdConfig_, const char** linkHoverStart_ ); |
| 382 | |
| 383 | void RenderLinkTextWrapped( const char* text_, const char* text_end_, const Link& link_, |
| 384 | const char* markdown_, const MarkdownConfig& mdConfig_, const char** linkHoverStart_, bool bIndentToHere_ = false ); |
| 385 | |
| 386 | void ResetIndent() |
| 387 | { |
| 388 | if( indentX > 0.0f ) |
| 389 | { |
| 390 | ImGui::Unindent( indentX ); |
| 391 | } |
| 392 | indentX = 0.0f; |
| 393 | } |
| 394 | |
| 395 | private: |
| 396 | float indentX; |
| 397 | }; |
| 398 | |
| 399 | |
| 400 | struct Line { |
| 401 | bool isHeading = false; |
| 402 | bool isEmphasis = false; |
| 403 | bool isUnorderedListStart = false; |
| 404 | bool isLeadingSpace = true; |
| 405 | int leadSpaceCount = 0; |
| 406 | int headingCount = 0; |
| 407 | int emphasisCount = 0; |
| 408 | int lineStart = 0; |
| 409 | int lineEnd = 0; |
| 410 | int lastRenderPosition = 0; |
| 411 | }; |
| 412 | |
| 413 | struct TextBlock { |
| 414 | int start = 0; |
| 415 | int stop = 0; |
| 416 | int size() const |
| 417 | { |
| 418 | return stop - start; |
| 419 | } |
| 420 | }; |
| 421 | |
| 422 | struct Link { |
| 423 | enum LinkState { |
| 424 | NO_LINK, |
| 425 | HAS_SQUARE_BRACKET_OPEN, |
| 426 | HAS_SQUARE_BRACKETS, |
| 427 | HAS_SQUARE_BRACKETS_ROUND_BRACKET_OPEN, |
| 428 | }; |
| 429 | LinkState state = NO_LINK; |
| 430 | TextBlock text; |
| 431 | TextBlock url; |
| 432 | bool isImage = false; |
| 433 | int num_brackets_open = 0; |
| 434 | }; |
| 435 | |
| 436 | struct Emphasis { |
| 437 | enum EmphasisState { |
| 438 | NONE, |
| 439 | LEFT, |
| 440 | MIDDLE, |
| 441 | RIGHT, |
| 442 | }; |
| 443 | EmphasisState state = NONE; |
| 444 | TextBlock text; |
| 445 | char sym; |
| 446 | }; |
| 447 | |
| 448 | inline void UnderLine( ImColor col_ ) |
| 449 | { |
| 450 | ImVec2 min = ImGui::GetItemRectMin(); |
| 451 | ImVec2 max = ImGui::GetItemRectMax(); |
| 452 | min.y = max.y; |
| 453 | ImGui::GetWindowDrawList()->AddLine( min, max, col_, 1.0f ); |
| 454 | } |
| 455 | |
| 456 | inline void RenderLine( const char* markdown_, Line& line_, TextRegion& textRegion_, const MarkdownConfig& mdConfig_ ) |
| 457 | { |
| 458 | |
| 459 | int indentStart = 0; |
| 460 | if( line_.isUnorderedListStart ) |
| 461 | { |
| 462 | indentStart = 1; |
| 463 | } |
| 464 | for( int j = indentStart; j < line_.leadSpaceCount / 2; ++j ) |
| 465 | { |
| 466 | ImGui::Indent(); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | MarkdownFormatInfo formatInfo; |
| 471 | formatInfo.config = &mdConfig_; |
| 472 | int textStart = line_.lastRenderPosition + 1; |
| 473 | int textSize = line_.lineEnd - textStart; |
| 474 | if( line_.isUnorderedListStart ) |
| 475 | { |
| 476 | formatInfo.type = MarkdownFormatType::UNORDERED_LIST; |
| 477 | mdConfig_.formatCallback( formatInfo, true ); |
| 478 | const char* text = markdown_ + textStart + 1; |
| 479 | textRegion_.RenderListTextWrapped( text, text + textSize - 1 ); |
| 480 | } |
| 481 | else if( line_.isHeading ) |
| 482 | { |
| 483 | formatInfo.level = line_.headingCount; |
| 484 | formatInfo.type = MarkdownFormatType::HEADING; |
| 485 | const char* text = markdown_ + textStart + 1; |
| 486 | formatInfo.text = text; |
| 487 | formatInfo.textLength = textSize - 1; |
| 488 | mdConfig_.formatCallback( formatInfo, true ); |
| 489 | textRegion_.RenderTextWrapped( text, text + textSize - 1 ); |
| 490 | } |
| 491 | else if( line_.isEmphasis ) |
| 492 | { |
| 493 | formatInfo.level = line_.emphasisCount; |
| 494 | formatInfo.type = MarkdownFormatType::EMPHASIS; |
| 495 | mdConfig_.formatCallback(formatInfo, true); |
| 496 | const char* text = markdown_ + textStart; |
| 497 | textRegion_.RenderTextWrapped(text, text + textSize); |
| 498 | } |
| 499 | else |
| 500 | { |
| 501 | formatInfo.type = MarkdownFormatType::NORMAL_TEXT; |
| 502 | mdConfig_.formatCallback( formatInfo, true ); |
| 503 | const char* text = markdown_ + textStart; |
| 504 | textRegion_.RenderTextWrapped( text, text + textSize ); |
| 505 | } |
| 506 | mdConfig_.formatCallback( formatInfo, false ); |
| 507 | |
| 508 | |
| 509 | for( int j = indentStart; j < line_.leadSpaceCount / 2; ++j ) |
| 510 | { |
| 511 | ImGui::Unindent(); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | |
| 516 | inline void Markdown( const char* markdown_, size_t markdownLength_, const MarkdownConfig& mdConfig_ ) |
| 517 | { |
| 518 | static const char* s_linkHoverStart = NULL; |
| 519 | static ImGuiID s_linkHoverID = 0; |
| 520 | const char* linkHoverStart = NULL; |
| 521 | ImGuiID linkHoverID = ImGui::GetID("MDLHS"); |
| 522 | if( linkHoverID == s_linkHoverID ) |
| 523 | { |
| 524 | linkHoverStart = s_linkHoverStart; |
| 525 | } |
| 526 | |
| 527 | ImGuiStyle& style = ImGui::GetStyle(); |
| 528 | Line line; |
| 529 | Line prevLine; |
| 530 | Link link; |
| 531 | Emphasis em; |
| 532 | TextRegion textRegion; |
| 533 | int concurrentEmptyNewlines = 0; |
| 534 | bool appliedExtraNewline = false; |
| 535 | |
| 536 | char c = 0; |
| 537 | for( int i=0; i < (int)markdownLength_; ++i ) |
| 538 | { |
| 539 | c = markdown_[i]; |
| 540 | if( c == 0 ) { break; } |
| 541 | |
| 542 | |
| 543 | if( line.isLeadingSpace ) |
| 544 | { |
| 545 | if ( (mdConfig_.formatFlags & ImGuiMarkdownFormatFlags_DiscardExtraNewLines) ) |
| 546 | { |
| 547 | if ( c == '\n' ) |
| 548 | { |
| 549 | concurrentEmptyNewlines++; |
| 550 | line.lineStart += 1; |
| 551 | continue; |
| 552 | } |
| 553 | else if ( ( c == '\r' ) && ( (int)markdownLength_ > i + 1 ) && ( markdown_[i + 1] == '\n' ) ) |
| 554 | { |
| 555 | concurrentEmptyNewlines++; |
| 556 | line.lineStart += 2; |
| 557 | i += 1; |
| 558 | continue; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | if( c == ' ' ) |
| 563 | { |
| 564 | ++line.leadSpaceCount; |
| 565 | continue; |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | line.isLeadingSpace = false; |
| 570 | line.lastRenderPosition = i - 1; |
| 571 | if(( c == '*' ) && ( line.leadSpaceCount >= 2 )) |
| 572 | { |
| 573 | if( ( (int)markdownLength_ > i + 1 ) && ( markdown_[ i + 1 ] == ' ' ) ) |
| 574 | { |
| 575 | line.isUnorderedListStart = true; |
| 576 | ++i; |
| 577 | ++line.lastRenderPosition; |
| 578 | } |
| 579 | |
| 580 | } |
| 581 | else if( c == '#' ) |
| 582 | { |
| 583 | line.headingCount++; |
| 584 | bool bContinueChecking = true; |
| 585 | int j = i; |
| 586 | while( ++j < (int)markdownLength_ && bContinueChecking ) |
| 587 | { |
| 588 | c = markdown_[j]; |
| 589 | switch( c ) |
| 590 | { |
| 591 | case '#': |
| 592 | line.headingCount++; |
| 593 | break; |
| 594 | case ' ': |
| 595 | line.lastRenderPosition = j - 1; |
| 596 | i = j; |
| 597 | line.isHeading = true; |
| 598 | bContinueChecking = false; |
| 599 | break; |
| 600 | default: |
| 601 | line.isHeading = false; |
| 602 | bContinueChecking = false; |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | if( line.isHeading ) |
| 607 | { |
| 608 | |
| 609 | em = Emphasis(); |
| 610 | continue; |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if ( (mdConfig_.formatFlags & ImGuiMarkdownFormatFlags_DiscardExtraNewLines) ) |
| 617 | { |
| 618 | |
| 619 | |
| 620 | if (!appliedExtraNewline && !prevLine.isHeading && concurrentEmptyNewlines >= 1) { |
| 621 | ImGui::NewLine(); |
| 622 | appliedExtraNewline = true; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | |
| 627 | switch( link.state ) |
| 628 | { |
| 629 | case Link::NO_LINK: |
| 630 | if( c == '[' && !line.isHeading ) |
| 631 | { |
| 632 | link.state = Link::HAS_SQUARE_BRACKET_OPEN; |
| 633 | link.text.start = i + 1; |
| 634 | if( i > 0 && markdown_[i - 1] == '!' ) |
| 635 | { |
| 636 | link.isImage = true; |
| 637 | } |
| 638 | } |
| 639 | break; |
| 640 | case Link::HAS_SQUARE_BRACKET_OPEN: |
| 641 | if( c == ']' ) |
| 642 | { |
| 643 | link.state = Link::HAS_SQUARE_BRACKETS; |
| 644 | link.text.stop = i; |
| 645 | } |
| 646 | break; |
| 647 | case Link::HAS_SQUARE_BRACKETS: |
| 648 | if( c == '(' ) |
| 649 | { |
| 650 | link.state = Link::HAS_SQUARE_BRACKETS_ROUND_BRACKET_OPEN; |
| 651 | link.url.start = i + 1; |
| 652 | link.num_brackets_open = 1; |
| 653 | } |
| 654 | break; |
| 655 | case Link::HAS_SQUARE_BRACKETS_ROUND_BRACKET_OPEN: |
| 656 | if( c == '(' ) |
| 657 | { |
| 658 | ++link.num_brackets_open; |
| 659 | } |
| 660 | else if( c == ')' ) |
| 661 | { |
| 662 | --link.num_brackets_open; |
| 663 | } |
| 664 | if( link.num_brackets_open == 0 ) |
| 665 | { |
| 666 | |
| 667 | em = Emphasis(); |
| 668 | |
| 669 | line.lineEnd = link.text.start - ( link.isImage ? 2 : 1 ); |
| 670 | RenderLine( markdown_, line, textRegion, mdConfig_ ); |
| 671 | line.leadSpaceCount = 0; |
| 672 | link.url.stop = i; |
| 673 | line.isUnorderedListStart = false; |
| 674 | ImGui::SameLine( 0.0f, 0.0f ); |
| 675 | if( link.isImage ) |
| 676 | { |
| 677 | bool drawnImage = false; |
| 678 | bool useLinkCallback = false; |
| 679 | if( mdConfig_.imageCallback ) |
| 680 | { |
| 681 | MarkdownImageData imageData = mdConfig_.imageCallback( { markdown_ + link.text.start, link.text.size(), markdown_ + link.url.start, link.url.size(), mdConfig_.userData, true } ); |
| 682 | useLinkCallback = imageData.useLinkCallback; |
| 683 | if( imageData.isValid ) |
| 684 | { |
| 685 | #if IMGUI_VERSION_NUM < 19185 |
| 686 | if( imageData.bg_col.w > 0.0f ) |
| 687 | { |
| 688 | ImVec2 p = ImGui::GetCursorScreenPos(); |
| 689 | ImGui::GetWindowDrawList()->AddRectFilled( p, ImVec2( p.x + imageData.size.x, p.y + imageData.size.y ), ImGui::GetColorU32( imageData.bg_col )); |
| 690 | } |
| 691 | ImGui::Image( imageData.user_texture_id, imageData.size, imageData.uv0, imageData.uv1, imageData.tint_col, imageData.border_col ); |
| 692 | #else |
| 693 | ImGui::PushStyleColor( ImGuiCol_Border, imageData.border_col ); |
| 694 | ImGui::ImageWithBg( imageData.user_texture_id, imageData.size, imageData.uv0, imageData.uv1, imageData.bg_col, imageData.tint_col ); |
| 695 | ImGui::PopStyleColor(); |
| 696 | #endif |
| 697 | drawnImage = true; |
| 698 | } |
| 699 | } |
| 700 | if( !drawnImage ) |
| 701 | { |
| 702 | ImGui::Text( "( Image %.*s not loaded )", link.url.size(), markdown_ + link.url.start ); |
| 703 | } |
| 704 | if( ImGui::IsItemHovered() ) |
| 705 | { |
| 706 | if( ImGui::IsMouseReleased( 0 ) && mdConfig_.linkCallback && useLinkCallback ) |
| 707 | { |
| 708 | mdConfig_.linkCallback( { markdown_ + link.text.start, link.text.size(), markdown_ + link.url.start, link.url.size(), mdConfig_.userData, true } ); |
| 709 | } |
| 710 | if( link.text.size() > 0 && mdConfig_.tooltipCallback ) |
| 711 | { |
| 712 | mdConfig_.tooltipCallback( { { markdown_ + link.text.start, link.text.size(), markdown_ + link.url.start, link.url.size(), mdConfig_.userData, true }, mdConfig_.linkIcon } ); |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | else |
| 717 | { |
| 718 | textRegion.RenderLinkTextWrapped( markdown_ + link.text.start, markdown_ + link.text.start + link.text.size(), link, markdown_, mdConfig_, &linkHoverStart, false ); |
| 719 | } |
| 720 | ImGui::SameLine( 0.0f, 0.0f ); |
| 721 | |
| 722 | link = Link(); |
| 723 | line.lastRenderPosition = i; |
| 724 | break; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | |
| 729 | switch( em.state ) |
| 730 | { |
| 731 | case Emphasis::NONE: |
| 732 | if( link.state == Link::NO_LINK && !line.isHeading ) |
| 733 | { |
| 734 | int next = i + 1; |
| 735 | int prev = i - 1; |
| 736 | if( ( c == '*' || c == '_' ) |
| 737 | && ( i == line.lineStart |
| 738 | || markdown_[ prev ] == ' ' |
| 739 | || markdown_[ prev ] == '\t' ) |
| 740 | && (int)markdownLength_ > next |
| 741 | && markdown_[ next ] != ' ' |
| 742 | && markdown_[ next ] != '\n' |
| 743 | && markdown_[ next ] != '\t' ) |
| 744 | { |
| 745 | em.state = Emphasis::LEFT; |
| 746 | em.sym = c; |
| 747 | em.text.start = i; |
| 748 | line.emphasisCount = 1; |
| 749 | continue; |
| 750 | } |
| 751 | } |
| 752 | break; |
| 753 | case Emphasis::LEFT: |
| 754 | if( em.sym == c ) |
| 755 | { |
| 756 | ++line.emphasisCount; |
| 757 | continue; |
| 758 | } |
| 759 | else |
| 760 | { |
| 761 | em.text.start = i; |
| 762 | em.state = Emphasis::MIDDLE; |
| 763 | } |
| 764 | break; |
| 765 | case Emphasis::MIDDLE: |
| 766 | if( em.sym == c ) |
| 767 | { |
| 768 | em.state = Emphasis::RIGHT; |
| 769 | em.text.stop = i; |
| 770 | |
| 771 | } |
| 772 | else |
| 773 | { |
| 774 | break; |
| 775 | } |
| 776 | #if __cplusplus >= 201703L |
| 777 | [[fallthrough]]; |
| 778 | #endif |
| 779 | case Emphasis::RIGHT: |
| 780 | if( em.sym == c ) |
| 781 | { |
| 782 | if( line.emphasisCount < 3 && ( i - em.text.stop + 1 == line.emphasisCount ) ) |
| 783 | { |
| 784 | |
| 785 | int lineEnd = em.text.start - line.emphasisCount; |
| 786 | if( lineEnd > line.lineStart ) |
| 787 | { |
| 788 | line.lineEnd = lineEnd; |
| 789 | RenderLine( markdown_, line, textRegion, mdConfig_ ); |
| 790 | ImGui::SameLine( 0.0f, 0.0f ); |
| 791 | line.isUnorderedListStart = false; |
| 792 | line.leadSpaceCount = 0; |
| 793 | } |
| 794 | line.isEmphasis = true; |
| 795 | line.lastRenderPosition = em.text.start - 1; |
| 796 | line.lineStart = em.text.start; |
| 797 | line.lineEnd = em.text.stop; |
| 798 | RenderLine( markdown_, line, textRegion, mdConfig_ ); |
| 799 | ImGui::SameLine( 0.0f, 0.0f ); |
| 800 | line.isEmphasis = false; |
| 801 | line.lastRenderPosition = i; |
| 802 | em = Emphasis(); |
| 803 | } |
| 804 | continue; |
| 805 | } |
| 806 | else |
| 807 | { |
| 808 | em.state = Emphasis::NONE; |
| 809 | |
| 810 | int start = em.text.start - line.emphasisCount; |
| 811 | if( start < line.lineStart ) |
| 812 | { |
| 813 | line.lineEnd = line.lineStart; |
| 814 | line.lineStart = start; |
| 815 | line.lastRenderPosition = start - 1; |
| 816 | RenderLine( markdown_, line, textRegion, mdConfig_ ); |
| 817 | line.lineStart = line.lineEnd; |
| 818 | line.lastRenderPosition = line.lineStart - 1; |
| 819 | } |
| 820 | } |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | |
| 825 | if( c == '\n' ) |
| 826 | { |
| 827 | |
| 828 | line.lineEnd = i; |
| 829 | if( em.state == Emphasis::MIDDLE && line.emphasisCount >=3 && |
| 830 | ( line.lineStart + line.emphasisCount ) == i ) |
| 831 | { |
| 832 | ImGui::Separator(); |
| 833 | } |
| 834 | else |
| 835 | { |
| 836 | |
| 837 | RenderLine( markdown_, line, textRegion, mdConfig_ ); |
| 838 | } |
| 839 | |
| 840 | |
| 841 | prevLine = line; |
| 842 | line = Line(); |
| 843 | em = Emphasis(); |
| 844 | |
| 845 | line.lineStart = i + 1; |
| 846 | line.lastRenderPosition = i; |
| 847 | |
| 848 | textRegion.ResetIndent(); |
| 849 | |
| 850 | |
| 851 | link = Link(); |
| 852 | |
| 853 | concurrentEmptyNewlines = 0; |
| 854 | appliedExtraNewline = false; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | if( em.state == Emphasis::LEFT && line.emphasisCount >= 3 ) |
| 859 | { |
| 860 | ImGui::Separator(); |
| 861 | } |
| 862 | else |
| 863 | { |
| 864 | |
| 865 | if( markdownLength_ && line.lineStart < (int)markdownLength_ && markdown_[ line.lineStart ] != 0 ) |
| 866 | { |
| 867 | |
| 868 | line.lineEnd = (int)markdownLength_; |
| 869 | if( 0 == markdown_[ line.lineEnd - 1 ] ) |
| 870 | { |
| 871 | --line.lineEnd; |
| 872 | } |
| 873 | RenderLine( markdown_, line, textRegion, mdConfig_ ); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | if( NULL != linkHoverStart || linkHoverID == s_linkHoverID ) |
| 878 | { |
| 879 | s_linkHoverStart = linkHoverStart; |
| 880 | s_linkHoverID = linkHoverID; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | inline bool TextRegion::RenderLinkText( const char* text_, const char* text_end_, const Link& link_, |
| 885 | const char* markdown_, const MarkdownConfig& mdConfig_, const char** linkHoverStart_ ) |
| 886 | { |
| 887 | MarkdownFormatInfo formatInfo; |
| 888 | formatInfo.config = &mdConfig_; |
| 889 | formatInfo.type = MarkdownFormatType::LINK; |
| 890 | mdConfig_.formatCallback( formatInfo, true ); |
| 891 | ImGui::PushTextWrapPos( -1.0f ); |
| 892 | ImGui::TextUnformatted( text_, text_end_ ); |
| 893 | ImGui::PopTextWrapPos(); |
| 894 | |
| 895 | bool bThisItemHovered = ImGui::IsItemHovered(); |
| 896 | if(bThisItemHovered) |
| 897 | { |
| 898 | *linkHoverStart_ = markdown_ + link_.text.start; |
| 899 | } |
| 900 | bool bHovered = bThisItemHovered || ( *linkHoverStart_ == ( markdown_ + link_.text.start ) ); |
| 901 | |
| 902 | formatInfo.itemHovered = bHovered; |
| 903 | mdConfig_.formatCallback( formatInfo, false ); |
| 904 | |
| 905 | if(bHovered) |
| 906 | { |
| 907 | if( ImGui::IsMouseReleased( 0 ) && mdConfig_.linkCallback ) |
| 908 | { |
| 909 | mdConfig_.linkCallback( { markdown_ + link_.text.start, link_.text.size(), markdown_ + link_.url.start, link_.url.size(), mdConfig_.userData, false } ); |
| 910 | } |
| 911 | if( mdConfig_.tooltipCallback ) |
| 912 | { |
| 913 | mdConfig_.tooltipCallback( { { markdown_ + link_.text.start, link_.text.size(), markdown_ + link_.url.start, link_.url.size(), mdConfig_.userData, false }, mdConfig_.linkIcon } ); |
| 914 | } |
| 915 | } |
| 916 | return bThisItemHovered; |
| 917 | } |
| 918 | |
| 919 | |
| 920 | inline bool IsCharInsideWord( char c_ ) |
| 921 | { |
| 922 | return c_ != ' ' && c_ != '.' && c_ != ',' && c_ != ';' && c_ != '!' && c_ != '?' && c_ != '\"'; |
| 923 | } |
| 924 | |
| 925 | |
| 926 | |
| 927 | inline void TextRegion::RenderTextWrapped( const char* text_, const char* text_end_, bool bIndentToHere_ ) |
| 928 | { |
| 929 | #if IMGUI_VERSION_NUM >= 19197 |
| 930 | float fontSize = ImGui::GetFontSize(); |
| 931 | #else |
| 932 | float scale = ImGui::GetIO().FontGlobalScale; |
| 933 | #endif |
| 934 | float widthLeft = GetContentRegionAvail().x; |
| 935 | const char* endLine = text_; |
| 936 | if( widthLeft > 0.0f ) |
| 937 | { |
| 938 | #if IMGUI_VERSION_NUM >= 19197 |
| 939 | endLine = ImGui::GetFont()->CalcWordWrapPosition( fontSize, text_, text_end_, widthLeft ); |
| 940 | #else |
| 941 | endLine = ImGui::GetFont()->CalcWordWrapPositionA( scale, text_, text_end_, widthLeft ); |
| 942 | #endif |
| 943 | } |
| 944 | |
| 945 | if( endLine > text_ && endLine < text_end_ ) |
| 946 | { |
| 947 | if( IsCharInsideWord( *endLine ) ) |
| 948 | { |
| 949 | |
| 950 | float widthNextLine = widthLeft + GetCursorScreenPos().x - GetWindowPos().x; |
| 951 | #if IMGUI_VERSION_NUM >= 19197 |
| 952 | const char* endNextLine = ImGui::GetFont()->CalcWordWrapPosition( fontSize, text_, text_end_, widthNextLine ); |
| 953 | #else |
| 954 | const char* endNextLine = ImGui::GetFont()->CalcWordWrapPositionA( scale, text_, text_end_, widthNextLine ); |
| 955 | #endif |
| 956 | if( endNextLine == text_end_ || ( endNextLine <= text_end_ && !IsCharInsideWord( *endNextLine ) ) ) |
| 957 | { |
| 958 | |
| 959 | endLine = text_; |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | ImGui::TextUnformatted( text_, endLine ); |
| 964 | if( bIndentToHere_ ) |
| 965 | { |
| 966 | float indentNeeded = GetContentRegionAvail().x - widthLeft; |
| 967 | if( indentNeeded ) |
| 968 | { |
| 969 | ImGui::Indent( indentNeeded ); |
| 970 | indentX += indentNeeded; |
| 971 | } |
| 972 | } |
| 973 | widthLeft = GetContentRegionAvail().x; |
| 974 | while( endLine < text_end_ ) |
| 975 | { |
| 976 | text_ = endLine; |
| 977 | if( *text_ == ' ' ) { ++text_; } |
| 978 | #if IMGUI_VERSION_NUM >= 19197 |
| 979 | endLine = ImGui::GetFont()->CalcWordWrapPosition( fontSize, text_, text_end_, widthLeft ); |
| 980 | #else |
| 981 | endLine = ImGui::GetFont()->CalcWordWrapPositionA( scale, text_, text_end_, widthLeft ); |
| 982 | #endif |
| 983 | if( text_ == endLine ) |
| 984 | { |
| 985 | endLine++; |
| 986 | } |
| 987 | ImGui::TextUnformatted( text_, endLine ); |
| 988 | } |
| 989 | } |
| 990 | |
| 991 | inline void TextRegion::RenderLinkTextWrapped( const char* text_, const char* text_end_, const Link& link_, |
| 992 | const char* markdown_, const MarkdownConfig& mdConfig_, const char** linkHoverStart_, bool bIndentToHere_ ) |
| 993 | { |
| 994 | #if IMGUI_VERSION_NUM >= 19197 |
| 995 | float fontSize = ImGui::GetFontSize(); |
| 996 | #else |
| 997 | float scale = ImGui::GetIO().FontGlobalScale; |
| 998 | #endif |
| 999 | float widthLeft = GetContentRegionAvail().x; |
| 1000 | const char* endLine = text_; |
| 1001 | if( widthLeft > 0.0f ) |
| 1002 | { |
| 1003 | #if IMGUI_VERSION_NUM >= 19197 |
| 1004 | endLine = ImGui::GetFont()->CalcWordWrapPosition( fontSize, text_, text_end_, widthLeft ); |
| 1005 | #else |
| 1006 | endLine = ImGui::GetFont()->CalcWordWrapPositionA( scale, text_, text_end_, widthLeft ); |
| 1007 | #endif |
| 1008 | } |
| 1009 | |
| 1010 | if( endLine > text_ && endLine < text_end_ ) |
| 1011 | { |
| 1012 | if( IsCharInsideWord( *endLine ) ) |
| 1013 | { |
| 1014 | |
| 1015 | float widthNextLine = widthLeft + GetCursorScreenPos().x - GetWindowPos().x; |
| 1016 | #if IMGUI_VERSION_NUM >= 19197 |
| 1017 | const char* endNextLine = ImGui::GetFont()->CalcWordWrapPosition( fontSize, text_, text_end_, widthNextLine ); |
| 1018 | #else |
| 1019 | const char* endNextLine = ImGui::GetFont()->CalcWordWrapPositionA( scale, text_, text_end_, widthNextLine ); |
| 1020 | #endif |
| 1021 | if( endNextLine == text_end_ || ( endNextLine <= text_end_ && !IsCharInsideWord( *endNextLine ) ) ) |
| 1022 | { |
| 1023 | |
| 1024 | endLine = text_; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | bool bHovered = RenderLinkText( text_, endLine, link_, markdown_, mdConfig_, linkHoverStart_ ); |
| 1029 | if( bIndentToHere_ ) |
| 1030 | { |
| 1031 | float indentNeeded = GetContentRegionAvail().x - widthLeft; |
| 1032 | if( indentNeeded ) |
| 1033 | { |
| 1034 | ImGui::Indent( indentNeeded ); |
| 1035 | indentX += indentNeeded; |
| 1036 | } |
| 1037 | } |
| 1038 | widthLeft = GetContentRegionAvail().x; |
| 1039 | while( endLine < text_end_ ) |
| 1040 | { |
| 1041 | text_ = endLine; |
| 1042 | if( *text_ == ' ' ) { ++text_; } |
| 1043 | #if IMGUI_VERSION_NUM >= 19197 |
| 1044 | endLine = ImGui::GetFont()->CalcWordWrapPosition( fontSize, text_, text_end_, widthLeft ); |
| 1045 | #else |
| 1046 | endLine = ImGui::GetFont()->CalcWordWrapPositionA( scale, text_, text_end_, widthLeft ); |
| 1047 | #endif |
| 1048 | if( text_ == endLine ) |
| 1049 | { |
| 1050 | endLine++; |
| 1051 | } |
| 1052 | bool bThisLineHovered = RenderLinkText( text_, endLine, link_, markdown_, mdConfig_, linkHoverStart_ ); |
| 1053 | bHovered = bHovered || bThisLineHovered; |
| 1054 | } |
| 1055 | if( !bHovered && *linkHoverStart_ == markdown_ + link_.text.start ) |
| 1056 | { |
| 1057 | *linkHoverStart_ = NULL; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | |
| 1062 | inline void defaultMarkdownFormatCallback( const MarkdownFormatInfo& markdownFormatInfo_, bool start_ ) |
| 1063 | { |
| 1064 | switch( markdownFormatInfo_.type ) |
| 1065 | { |
| 1066 | case MarkdownFormatType::NORMAL_TEXT: |
| 1067 | break; |
| 1068 | case MarkdownFormatType::EMPHASIS: |
| 1069 | { |
| 1070 | MarkdownHeadingFormat fmt; |
| 1071 | |
| 1072 | |
| 1073 | if( markdownFormatInfo_.level == 1 ) |
| 1074 | { |
| 1075 | |
| 1076 | if( start_ ) |
| 1077 | { |
| 1078 | ImGui::PushStyleColor( ImGuiCol_Text, ImGui::GetStyle().Colors[ ImGuiCol_TextDisabled ] ); |
| 1079 | } |
| 1080 | else |
| 1081 | { |
| 1082 | ImGui::PopStyleColor(); |
| 1083 | } |
| 1084 | } |
| 1085 | else |
| 1086 | { |
| 1087 | |
| 1088 | fmt = markdownFormatInfo_.config->headingFormats[ MarkdownConfig::NUMHEADINGS - 1 ]; |
| 1089 | if( start_ ) |
| 1090 | { |
| 1091 | if( fmt.font ) |
| 1092 | { |
| 1093 | #ifdef IMGUI_HAS_TEXTURES |
| 1094 | ImGui::PushFont( fmt.font, 0.0f ); |
| 1095 | #else |
| 1096 | ImGui::PushFont( fmt.font ); |
| 1097 | #endif |
| 1098 | } |
| 1099 | } |
| 1100 | else |
| 1101 | { |
| 1102 | if( fmt.font ) |
| 1103 | { |
| 1104 | ImGui::PopFont(); |
| 1105 | } |
| 1106 | } |
| 1107 | } |
| 1108 | break; |
| 1109 | } |
| 1110 | case MarkdownFormatType::HEADING: |
| 1111 | { |
| 1112 | MarkdownHeadingFormat fmt; |
| 1113 | if( markdownFormatInfo_.level > MarkdownConfig::NUMHEADINGS ) |
| 1114 | { |
| 1115 | fmt = markdownFormatInfo_.config->headingFormats[ MarkdownConfig::NUMHEADINGS - 1 ]; |
| 1116 | } |
| 1117 | else |
| 1118 | { |
| 1119 | fmt = markdownFormatInfo_.config->headingFormats[ markdownFormatInfo_.level - 1 ]; |
| 1120 | } |
| 1121 | if (start_) |
| 1122 | { |
| 1123 | if ( 0 == ( markdownFormatInfo_.config->formatFlags & ImGuiMarkdownFormatFlags_NoNewLineBeforeHeading ) ) |
| 1124 | { |
| 1125 | ImGui::NewLine(); |
| 1126 | } |
| 1127 | if (fmt.font) |
| 1128 | { |
| 1129 | #ifdef IMGUI_HAS_TEXTURES |
| 1130 | ImGui::PushFont(fmt.font, fmt.fontSize > 0.0f ? fmt.fontSize : fmt.font->LegacySize); |
| 1131 | #else |
| 1132 | ImGui::PushFont(fmt.font); |
| 1133 | #endif |
| 1134 | } |
| 1135 | } |
| 1136 | else |
| 1137 | { |
| 1138 | if (fmt.separator) |
| 1139 | { |
| 1140 | |
| 1141 | ImVec2 cursor = ImGui::GetCursorPos(); |
| 1142 | ImGui::Separator(); |
| 1143 | if ( (markdownFormatInfo_.config->formatFlags & ImGuiMarkdownFormatFlags_SeparatorDoesNotAdvance) ) { |
| 1144 | ImGui::SetCursorPos(cursor); |
| 1145 | } |
| 1146 | } |
| 1147 | if (fmt.font) |
| 1148 | { |
| 1149 | ImGui::PopFont(); |
| 1150 | } |
| 1151 | ImGui::NewLine(); |
| 1152 | } |
| 1153 | break; |
| 1154 | } |
| 1155 | case MarkdownFormatType::UNORDERED_LIST: |
| 1156 | break; |
| 1157 | case MarkdownFormatType::LINK: |
| 1158 | if( start_ ) |
| 1159 | { |
| 1160 | ImGui::PushStyleColor( ImGuiCol_Text, ImGui::GetStyle().Colors[ ImGuiCol_ButtonHovered ] ); |
| 1161 | } |
| 1162 | else |
| 1163 | { |
| 1164 | ImGui::PopStyleColor(); |
| 1165 | if( markdownFormatInfo_.itemHovered ) |
| 1166 | { |
| 1167 | ImGui::UnderLine( ImGui::GetStyle().Colors[ ImGuiCol_ButtonHovered ] ); |
| 1168 | } |
| 1169 | else |
| 1170 | { |
| 1171 | ImGui::UnderLine( ImGui::GetStyle().Colors[ ImGuiCol_Button ] ); |
| 1172 | } |
| 1173 | } |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | } |