LCOV - code coverage report
Current view: top level - source/gui - CGUIText.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 8 13 61.5 %
Date: 2026-12-03 01:19:39 Functions: 4 6 66.7 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #ifndef INCLUDED_GUITEXT
      19             : #define INCLUDED_GUITEXT
      20             : 
      21             : #include "gui/CGUISprite.h"
      22             : #include "gui/SettingTypes/CGUIColor.h"
      23             : #include "lib/code_annotation.h"
      24             : #include "maths/Rect.h"
      25             : #include "maths/Size2D.h"
      26             : #include "maths/Vector2D.h"
      27             : #include "ps/CStr.h"
      28             : #include "ps/CStrIntern.h"
      29             : 
      30             : #include <array>
      31             : #include <list>
      32             : #include <vector>
      33             : 
      34             : class CCanvas2D;
      35             : class CGUI;
      36             : class CGUIString;
      37             : class IGUIObject;
      38             : enum class EAlign;
      39             : struct SGenerateTextImage;
      40             : 
      41             : using SGenerateTextImages = std::array<std::vector<SGenerateTextImage>, 2>;
      42             : 
      43             : /**
      44             :  * An CGUIText object is a parsed string, divided into
      45             :  * text-rendering components. Each component, being a
      46             :  * call to the Renderer. For instance, if you by tags
      47             :  * change the color, then the GUI will have to make
      48             :  * individual calls saying it want that color on the
      49             :  * text.
      50             :  *
      51             :  * For instance:
      52             :  * "Hello [b]there[/b] bunny!"
      53             :  *
      54             :  * That without word-wrapping would mean 3 components.
      55             :  * i.e. 3 calls to CRenderer. One drawing "Hello",
      56             :  * one drawing "there" in bold, and one drawing "bunny!".
      57             :  */
      58         145 : class CGUIText
      59             : {
      60             : public:
      61             :     /**
      62             :      * A sprite call to the CRenderer
      63             :      */
      64           0 :     struct SSpriteCall
      65             :     {
      66             :         // The CGUISpriteInstance makes this uncopyable to avoid invalidating its draw cache
      67             :         NONCOPYABLE(SSpriteCall);
      68           0 :         MOVABLE(SSpriteCall);
      69             : 
      70           0 :         SSpriteCall() {}
      71             : 
      72             :         /**
      73             :          * Size and position of sprite
      74             :          */
      75             :         CRect m_Area;
      76             : 
      77             :         /**
      78             :          * Sprite from global GUI sprite database.
      79             :          */
      80             :         CGUISpriteInstance m_Sprite;
      81             :     };
      82             : 
      83             :     /**
      84             :      * A text call to the CRenderer
      85             :      */
      86             :     struct STextCall
      87             :     {
      88             :         NONCOPYABLE(STextCall);
      89        2416 :         MOVABLE(STextCall);
      90             : 
      91        1668 :         STextCall() :
      92        1668 :             m_UseCustomColor(false),
      93        1668 :             m_Bold(false), m_Italic(false), m_Underlined(false),
      94        1668 :             m_pSpriteCall(nullptr) {}
      95             : 
      96             :         /**
      97             :          * Position
      98             :          */
      99             :         CVector2D m_Pos;
     100             : 
     101             :         /**
     102             :          * Size
     103             :          */
     104             :         CSize2D m_Size;
     105             : 
     106             :         /**
     107             :          * The string that is suppose to be rendered.
     108             :          */
     109             :         CStrW m_String;
     110             : 
     111             :         /**
     112             :          * Use custom color? If true then m_Color is used,
     113             :          * else the color inputted will be used.
     114             :          */
     115             :         bool m_UseCustomColor;
     116             : 
     117             :         /**
     118             :          * Color setup
     119             :          */
     120             :         CGUIColor m_Color;
     121             : 
     122             :         /**
     123             :          * Font name
     124             :          */
     125             :         CStrIntern m_Font;
     126             : 
     127             :         /**
     128             :          * BCP-47 locale tag (e.g. `"en"`, `"ja"`, `"zh-hans"`) that tells the
     129             :          * FontManager which per-locale font to use for this portion of text.
     130             :          *
     131             :          * If the string is non-empty, the renderer tries to substitute a font
     132             :          * registered for that locale; otherwise it falls back to the game-wide
     133             :          * UI font so legacy captions behave exactly as before.
     134             :          *
     135             :          * The value is filled in by the `[locale='xx']…[/locale]` inline-markup
     136             :          * parser and read during glyph layout.
     137             :          */
     138             :         CStrIntern m_FontLocale;
     139             : 
     140             :         /**
     141             :          * Settings
     142             :          */
     143             :         bool m_Bold, m_Italic, m_Underlined;
     144             : 
     145             :         /**
     146             :          * Tooltip text
     147             :          */
     148             :         CStrW m_Tooltip;
     149             : 
     150             :         /**
     151             :          * *IF* an icon, then this is not nullptr.
     152             :          */
     153             :         std::list<SSpriteCall>::pointer m_pSpriteCall;
     154             :     };
     155             : 
     156             :     // The SSpriteCall CGUISpriteInstance makes this uncopyable to avoid invalidating its draw cache.
     157             :     // Also take advantage of exchanging the containers directly with move semantics.
     158             :     NONCOPYABLE(CGUIText);
     159          21 :     MOVABLE(CGUIText);
     160             : 
     161             :     /**
     162             :      * Generates empty text.
     163             :      */
     164           5 :     CGUIText() = default;
     165             : 
     166             :     /**
     167             :      * Generate a CGUIText object from the inputted string.
     168             :      * The function will break down the string and its
     169             :      * tags to calculate exactly which rendering queries
     170             :      * will be sent to the Renderer. Also, horizontal alignment
     171             :      * is taken into acount in this method but NOT vertical alignment.
     172             :      *
     173             :      * @param string Text to generate CGUIText object from.
     174             :      * @param font Default font, notice both Default color and default font
     175             :      *        can be changed by tags.
     176             :      * @param width Width, 0 if no word-wrapping.
     177             :      * @param bufferZone Space between text and edge, and space between text and images.
     178             :      * @param align Horizontal alignment (left / center / right).
     179             :      * @param pObject Optional parameter for error output. Used *only* if error parsing fails,
     180             :      *        and we need to be able to output which object the error occurred in to aid the user.
     181             :      */
     182             :     CGUIText(const CGUI& pGUI, const CGUIString& string, const CStrW& fontW, const float width, const float bufferZone, const EAlign align, const IGUIObject* pObject);
     183             : 
     184             :     /**
     185             :      * Draw this CGUIText object
     186             :      */
     187             :     void Draw(CGUI& pGUI, CCanvas2D& canvas, const CGUIColor& DefaultColor, const CVector2D& pos, CRect clipping) const;
     188             : 
     189           0 :     const CSize2D& GetSize() const { return m_Size; }
     190             : 
     191             :     const std::list<SSpriteCall>& GetSpriteCalls() const { return m_SpriteCalls; }
     192             : 
     193           0 :     const std::vector<STextCall>& GetTextCalls() const { return m_TextCalls; }
     194             : 
     195             :     // Helper functions of the constructor
     196             :     bool ProcessLine(
     197             :         const CGUI& pGUI,
     198             :         const CGUIString& string,
     199             :         const CStrIntern& font,
     200             :         const IGUIObject* pObject,
     201             :         const SGenerateTextImages& images,
     202             :         const EAlign align,
     203             :         const float prelimLineHeight,
     204             :         const float width,
     205             :         const float bufferZone,
     206             :         bool& firstLine,
     207             :         float& y,
     208             :         int& i,
     209             :         int& from);
     210             : 
     211             :     void SetupSpriteCalls(
     212             :         const CGUI& pGUI,
     213             :         const std::array<std::vector<CStr>, 2>& feedbackImages,
     214             :         const float y,
     215             :         const float width,
     216             :         const float bufferZone,
     217             :         const int i,
     218             :         const int posLastImage,
     219             :         SGenerateTextImages& images);
     220             : 
     221             :     float GetLineOffset(
     222             :         const EAlign align,
     223             :         const float widthRangeFrom,
     224             :         const float widthRangeTo,
     225             :         const CSize2D& lineSize) const;
     226             : 
     227             :     void ComputeLineRange(
     228             :         const SGenerateTextImages& images,
     229             :         const float y,
     230             :         const float width,
     231             :         const float prelimLineHeight,
     232             :         float& widthRangeFrom,
     233             :         float& widthRangeTo) const;
     234             : 
     235             :     void ComputeLineSize(
     236             :         const CGUI& pGUI,
     237             :         const CGUIString& string,
     238             :         const CStrIntern& font,
     239             :         const bool firstLine,
     240             :         const float width,
     241             :         const float widthRangeFrom,
     242             :         const float widthRangeTo,
     243             :         const int i,
     244             :         const int tempFrom,
     245             :         CSize2D& lineSize) const;
     246             : 
     247             :     bool AssembleCalls(
     248             :         const CGUI& pGUI,
     249             :         const CGUIString& string,
     250             :         const CStrIntern& font,
     251             :         const IGUIObject* pObject,
     252             :         const bool firstLine,
     253             :         const float width,
     254             :         const float widthRangeTo,
     255             :         const float dx,
     256             :         const float y,
     257             :         const int tempFrom,
     258             :         const int i,
     259             :         int& from);
     260             : 
     261             :     /**
     262             :      * List of TextCalls, for instance "Hello", "there!"
     263             :      */
     264             :     std::vector<STextCall> m_TextCalls;
     265             : 
     266             :     /**
     267             :      * List of sprites, or "icons" that should be rendered
     268             :      * along with the text.
     269             :      */
     270             :     std::list<SSpriteCall> m_SpriteCalls; // list for consistent mem addresses
     271             :                                           // so that we can point to elements.
     272             :     /**
     273             :      * Width and height of the whole output, used when setting up
     274             :      * scrollbars and such.
     275             :      */
     276             :     CSize2D m_Size;
     277             : };
     278             : 
     279             : struct SGenerateTextImage
     280             : {
     281             :     // The image's starting location in Y
     282             :     float m_YFrom;
     283             : 
     284             :     // The image's end location in Y
     285             :     float m_YTo;
     286             : 
     287             :     // The image width in other words
     288             :     float m_Indentation;
     289             : 
     290             :     void SetupSpriteCall(
     291             :         const bool left, CGUIText::SSpriteCall& spriteCall, const float width, const float y,
     292             :         const CSize2D& size, const CStr& textureName, const float bufferZone);
     293             : };
     294             : 
     295             : #endif // INCLUDED_GUITEXT

Generated by: LCOV version 1.16