MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / getStringWidthOneLine

Function getStringWidthOneLine

src/OpenLoco/src/Graphics/TextRenderer.cpp:1727–1793  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1725 }
1726
1727 static std::tuple<uint16_t, const char*, Font> getStringWidthOneLine(const char* ptr, Font font)
1728 {
1729 uint16_t lineWidth = 0;
1730 for (; *ptr != '\0'; ++ptr)
1731 {
1732 const auto chr = static_cast<uint8_t>(*ptr);
1733 if (chr >= ControlCodes::noArgBegin && chr < ControlCodes::noArgEnd)
1734 {
1735 switch (chr)
1736 {
1737 case ControlCodes::newline:
1738 {
1739 return std::make_tuple(lineWidth, ptr, font);
1740 }
1741 case ControlCodes::Font::small:
1742 font = Font::small;
1743 break;
1744 case ControlCodes::Font::large:
1745 font = Font::large;
1746 break;
1747 case ControlCodes::Font::bold:
1748 font = Font::medium_bold;
1749 break;
1750 case ControlCodes::Font::regular:
1751 font = Font::medium_normal;
1752 break;
1753 }
1754 }
1755 else if (chr >= ControlCodes::oneArgBegin && chr < ControlCodes::oneArgEnd)
1756 {
1757 switch (chr)
1758 {
1759 case ControlCodes::moveX:
1760 lineWidth = static_cast<uint8_t>(ptr[1]);
1761 break;
1762 }
1763 ptr += 1;
1764 }
1765 else if (chr >= ControlCodes::twoArgBegin && chr < ControlCodes::twoArgEnd)
1766 {
1767 ptr += 2;
1768 }
1769 else if (chr >= ControlCodes::fourArgBegin && chr < ControlCodes::fourArgEnd)
1770 {
1771 switch (chr)
1772 {
1773 case ControlCodes::inlineSpriteStr:
1774 {
1775 uint32_t image = *reinterpret_cast<const uint32_t*>(ptr);
1776 ImageId imageId{ image & 0x7FFFF };
1777 auto* el = getG1Element(imageId.getIndex());
1778 if (el != nullptr)
1779 {
1780 lineWidth += el->width;
1781 }
1782 break;
1783 }
1784 }

Callers 1

getStringWidthNewLinedFunction · 0.85

Calls 3

getG1ElementFunction · 0.85
getCharacterWidthFunction · 0.85
getIndexMethod · 0.45

Tested by

no test coverage detected