| 662 | } |
| 663 | |
| 664 | bool AppFrame::RenderText(ResFont* p, wchar_t* strBuf, fcyRect rect, fcyVec2 scale, ResFont::FontAlignHorizontal halign, ResFont::FontAlignVertical valign, bool bWordBreak)LNOEXCEPT |
| 665 | { |
| 666 | if (m_GraphType != GraphicsType::Graph2D) |
| 667 | { |
| 668 | LERROR("RenderText: 只有2D渲染器可以执行该方法"); |
| 669 | return false; |
| 670 | } |
| 671 | |
| 672 | f2dFontProvider* pFontProvider = p->GetFontProvider(); |
| 673 | |
| 674 | // 准备渲染字体 |
| 675 | m_FontRenderer->SetFontProvider(pFontProvider); |
| 676 | m_FontRenderer->SetScale(scale); |
| 677 | #ifdef LSHOWFONTBASELINE |
| 678 | FontBaseLineDebugHelper tDebugger(m_Graph2D, m_GRenderer, rect); |
| 679 | m_FontRenderer->SetListener(&tDebugger); |
| 680 | #endif |
| 681 | |
| 682 | // 设置混合和颜色 |
| 683 | updateGraph2DBlendMode(p->GetBlendMode()); |
| 684 | m_FontRenderer->SetColor(p->GetBlendColor()); |
| 685 | |
| 686 | // 第一次遍历计算要渲染多少行 |
| 687 | const wchar_t* pText = strBuf; |
| 688 | int iLineCount = 1; |
| 689 | float fLineWidth = 0.f; |
| 690 | while (*pText) |
| 691 | { |
| 692 | bool bNewLine = false; |
| 693 | if (*pText == L'\n') |
| 694 | bNewLine = true; |
| 695 | else |
| 696 | { |
| 697 | f2dGlyphInfo tGlyphInfo; |
| 698 | if (FCYOK(pFontProvider->QueryGlyph(m_Graph2D, *pText, &tGlyphInfo))) |
| 699 | { |
| 700 | float adv = tGlyphInfo.Advance.x * scale.x; |
| 701 | if (bWordBreak && fLineWidth + adv > rect.GetWidth()) // 截断模式 |
| 702 | { |
| 703 | if (pText == strBuf || *(pText - 1) == L'\n') |
| 704 | { |
| 705 | ++pText; // 防止一个字符都不渲染导致死循环 |
| 706 | if (*pText == L'\0') |
| 707 | break; |
| 708 | } |
| 709 | bNewLine = true; |
| 710 | } |
| 711 | else |
| 712 | fLineWidth += adv; |
| 713 | } |
| 714 | } |
| 715 | if (bNewLine) |
| 716 | { |
| 717 | ++iLineCount; |
| 718 | fLineWidth = 0.f; |
| 719 | } |
| 720 | if (*pText != L'\0') |
| 721 | ++pText; |
nothing calls this directly
no test coverage detected