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

Function clipString

src/OpenLoco/src/Graphics/TextRenderer.cpp:1542–1634  ·  view source on GitHub ↗

0x004957C4

Source from the content-addressed store, hash-verified

1540
1541 // 0x004957C4
1542 static int16_t clipString(Font font, int16_t width, char* string)
1543 {
1544 if (width < 6)
1545 {
1546 *string = '\0';
1547 return 0;
1548 }
1549
1550 // If width of the full string is less than allowed width then we don't need to clip
1551 auto clippedWidth = getStringWidth(font, string);
1552 if (clippedWidth <= width)
1553 {
1554 return clippedWidth;
1555 }
1556
1557 // Append each character 1 by 1 with an ellipsis on the end until width is exceeded
1558 std::string bestString;
1559 std::string curString;
1560
1561 for (const auto* chr = string; *chr != '\0'; ++chr)
1562 {
1563 curString.push_back(*chr);
1564 switch (static_cast<uint8_t>(*chr))
1565 {
1566 case ControlCodes::moveX:
1567 curString.push_back(*++chr);
1568 break;
1569
1570 case ControlCodes::adjustPalette:
1571 case 3:
1572 case 4:
1573 curString.push_back(*++chr);
1574 break;
1575
1576 case ControlCodes::newline:
1577 case ControlCodes::newlineSmaller:
1578 case ControlCodes::Font::small:
1579 case ControlCodes::Font::large:
1580 case ControlCodes::Font::bold:
1581 case ControlCodes::Font::regular:
1582 case ControlCodes::Font::outline:
1583 case ControlCodes::Font::outlineOff:
1584 case ControlCodes::windowColour1:
1585 case ControlCodes::windowColour2:
1586 case ControlCodes::windowColour3:
1587 case ControlCodes::windowColour4:
1588 break;
1589
1590 case ControlCodes::inlineSpriteStr:
1591 curString.push_back(*++chr);
1592 curString.push_back(*++chr);
1593 curString.push_back(*++chr);
1594 curString.push_back(*++chr);
1595 break;
1596
1597 case ControlCodes::newlineXY:
1598 curString.push_back(*++chr);
1599 curString.push_back(*++chr);

Callers 3

drawStringLeftClippedFunction · 0.85
drawStringCentredClippedFunction · 0.85
clipStringMethod · 0.85

Calls 4

getStringWidthFunction · 0.85
locoStrcpyFunction · 0.85
push_backMethod · 0.80
appendMethod · 0.80

Tested by

no test coverage detected