MCPcopy Create free account
hub / github.com/diasurgical/DevilutionX / DrawString

Function DrawString

Source/engine/render/text_render.cpp:739–784  ·  view source on GitHub ↗

* @todo replace Rectangle with cropped Surface */

Source from the content-addressed store, hash-verified

737 * @todo replace Rectangle with cropped Surface
738 */
739uint32_t DrawString(const Surface &out, string_view text, const Rectangle &rect, TextRenderOptions opts)
740{
741 const GameFontTables size = GetFontSizeFromUiFlags(opts.flags);
742 const text_color color = GetColorFromFlags(opts.flags);
743
744 int charactersInLine = 0;
745 int lineWidth = 0;
746 if (HasAnyOf(opts.flags, (UiFlags::AlignCenter | UiFlags::AlignRight | UiFlags::KerningFitSpacing)))
747 lineWidth = GetLineWidth(text, size, opts.spacing, &charactersInLine);
748
749 Point characterPosition { GetLineStartX(opts.flags, rect, lineWidth), rect.position.y };
750 const int initialX = characterPosition.x;
751
752 const int rightMargin = rect.position.x + rect.size.width;
753 const int bottomMargin = rect.size.height != 0 ? std::min(rect.position.y + rect.size.height + BaseLineOffset[size], out.h()) : out.h();
754
755 if (opts.lineHeight == -1)
756 opts.lineHeight = GetLineHeight(text, size);
757
758 if (HasAnyOf(opts.flags, UiFlags::VerticalCenter)) {
759 const int textHeight = static_cast<int>((c_count(text, '\n') + 1) * opts.lineHeight);
760 characterPosition.y += std::max(0, (rect.size.height - textHeight) / 2);
761 }
762
763 characterPosition.y += BaseLineOffset[size];
764
765 const bool outlined = HasAnyOf(opts.flags, UiFlags::Outlined);
766
767 const Surface clippedOut = ClipSurface(out, rect);
768
769 // Only draw the PentaCursor if the cursor is not at the end.
770 if (HasAnyOf(opts.flags, UiFlags::PentaCursor) && static_cast<size_t>(opts.cursorPosition) == text.size()) {
771 opts.cursorPosition = -1;
772 }
773
774 const uint32_t bytesDrawn = DoDrawString(clippedOut, text, rect, characterPosition,
775 lineWidth, charactersInLine, rightMargin, bottomMargin, size, color, outlined, opts);
776
777 if (HasAnyOf(opts.flags, UiFlags::PentaCursor)) {
778 const ClxSprite sprite = (*pSPentSpn2Cels)[PentSpn2Spin()];
779 MaybeWrap(characterPosition, sprite.width(), rightMargin, initialX, opts.lineHeight);
780 ClxDraw(clippedOut, characterPosition + Displacement { 0, opts.lineHeight - BaseLineOffset[size] }, sprite);
781 }
782
783 return bytesDrawn;
784}
785
786void DrawStringWithColors(const Surface &out, string_view fmt, DrawStringFormatArg *args, std::size_t argsLen, const Rectangle &rect, TextRenderOptions opts)
787{

Callers

nothing calls this directly

Calls 15

GetFontSizeFromUiFlagsFunction · 0.85
GetColorFromFlagsFunction · 0.85
HasAnyOfFunction · 0.85
GetLineWidthFunction · 0.85
GetLineStartXFunction · 0.85
GetLineHeightFunction · 0.85
c_countFunction · 0.85
ClipSurfaceFunction · 0.85
DoDrawStringFunction · 0.85
PentSpn2SpinFunction · 0.85
MaybeWrapFunction · 0.85
ClxDrawFunction · 0.85

Tested by

no test coverage detected