LCOV - code coverage report
Current view: top level - source/graphics - TextRenderer.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 15 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 2 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 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_TEXTRENDERER
      19             : #define INCLUDED_TEXTRENDERER
      20             : 
      21             : #include "graphics/Color.h"
      22             : #include "maths/Rect.h"
      23             : #include "maths/Vector2D.h"
      24             : #include "ps/CStrIntern.h"
      25             : #include "ps/memory/AllocatorAdapter.h"
      26             : #include "ps/memory/LinearAllocator.h"
      27             : 
      28             : #include <cstddef>
      29             : #include <list>
      30             : #include <memory>
      31             : #include <string>
      32             : 
      33             : class CFont;
      34             : namespace Renderer::Backend { class IDeviceCommandContext; }
      35             : namespace Renderer::Backend { class IShaderProgram; }
      36             : 
      37           0 : class CTextRenderer
      38             : {
      39             : public:
      40             :     CTextRenderer();
      41             : 
      42             :     /**
      43             :      * Reset the text transform to the default, with (0,0) in the top-left corner.
      44             :      */
      45             :     void ResetTranslate(const CVector2D& translate = CVector2D{});
      46             : 
      47             :     const CVector2D& GetTranslate() const { return m_Translate; }
      48             :     void Translate(float x, float y);
      49             : 
      50             :     /**
      51             :      * Set clipping rectangle, in pre-transform coordinates (i.e. text is clipped against
      52             :      * this rect based purely on the x,y values passed into Put()). Text fully outside the
      53             :      * clipping rectangle may not be rendered. Should be used in conjunction with SetScissors
      54             :      * for precise clipping - this is just an optimisation.
      55             :      */
      56             :     void SetClippingRect(const CRect& rect);
      57             : 
      58             :     /**
      59             :      * Set the color for subsequent print calls.
      60             :      */
      61             :     void SetCurrentColor(const CColor& color);
      62             : 
      63             :     /**
      64             :      * Set the font for subsequent print calls.
      65             :      */
      66             :     void SetCurrentFont(CStrIntern font, CStrIntern locale);
      67             : 
      68             :     /**
      69             :      * Print formatted text at (0,0) under the current transform,
      70             :      * and advance the transform by the width of the text.
      71             :      */
      72             :     void PrintfAdvance(const wchar_t* fmt, ...);
      73             : 
      74             :     /**
      75             :      * Print formatted text at (x,y) under the current transform.
      76             :      * Does not alter the current transform.
      77             :      */
      78             :     void PrintfAt(float x, float y, const wchar_t* fmt, ...);
      79             : 
      80             :     /**
      81             :      * Print text at (0,0) under the current transform,
      82             :      * and advance the transform by the width of the text.
      83             :      */
      84             :     void PutAdvance(const wchar_t* buf);
      85             : 
      86             :     /**
      87             :      * Print text at (x,y) under the current transform.
      88             :      * Does not alter the current transform.
      89             :      */
      90             :     void Put(float x, float y, const wchar_t* buf);
      91             : 
      92             :     /**
      93             :      * Print text at (x,y) under the current transform.
      94             :      * Does not alter the current transform.
      95             :      * @p buf must be a UTF-8 string.
      96             :      */
      97             :     void Put(float x, float y, const char* buf);
      98             : 
      99             :     /**
     100             :      * Print text at (x,y) under the current transform.
     101             :      * Does not alter the current transform.
     102             :      * @p buf must remain valid until Render() is called.
     103             :      * (This should be used to minimise memory copies when possible.)
     104             :      */
     105             :     void Put(float x, float y, const std::wstring* buf);
     106             : 
     107             :     /**
     108             :      * Render all of the previously printed text calls.
     109             :      */
     110             :     void Render(
     111             :         Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
     112             :         Renderer::Backend::IShaderProgram* shader,
     113             :         const CVector2D& transformScale, const CVector2D& translation,
     114             :         const bool debugFontBox, const CColor& debugBoxColor);
     115             : 
     116             : private:
     117             :     friend struct SBatchCompare;
     118             : 
     119             :     /**
     120             :      * A string (optionally owned by this object, or else pointing to an
     121             :      * externally-owned string) with a position.
     122             :      */
     123             :     struct SBatchRun
     124             :     {
     125             :     private:
     126             :         SBatchRun& operator=(const SBatchRun&);
     127             :     public:
     128           0 :         SBatchRun()
     129           0 :             : text(NULL), owned(false)
     130             :         {
     131             :         }
     132             : 
     133           0 :         SBatchRun(const SBatchRun& str)
     134           0 :             : x(str.x), y(str.y), owned(str.owned)
     135             :         {
     136           0 :             if (owned)
     137           0 :                 text = new std::wstring(*str.text);
     138             :             else
     139           0 :                 text = str.text;
     140           0 :         }
     141             : 
     142           0 :         ~SBatchRun()
     143             :         {
     144           0 :             if (owned)
     145           0 :                 delete text;
     146           0 :         }
     147             : 
     148             :         float x, y;
     149             : 
     150             :         const std::wstring* text;
     151             :         bool owned;
     152             :     };
     153             : 
     154             :     /**
     155             :      * A list of SBatchRuns, with a single font/color/transform,
     156             :      * to be rendered in a single GL call.
     157             :      */
     158           0 :     struct SBatch
     159             :     {
     160             :         size_t chars; // sum of runs[i].text->size()
     161             :         CVector2D translate;
     162             :         CColor color;
     163             :         CFont* font;
     164             :         using SBatchRunList = std::list<SBatchRun, PS::Memory::AllocatorAdapter<SBatchRun, PS::Memory::ScopedLinearAllocator>>;
     165             :         SBatchRunList runs;
     166             : 
     167           0 :         SBatch(PS::Memory::ScopedLinearAllocator& allocator) : runs{allocator} {}
     168             :     };
     169             : 
     170             :     void PutString(float x, float y, const std::wstring* buf, bool owned);
     171             : 
     172             :     PS::Memory::ScopedLinearAllocator m_ScopedLinearAllocator;
     173             : 
     174             :     CVector2D m_Translate;
     175             :     CRect m_Clipping;
     176             : 
     177             :     CColor m_Color;
     178             :     CStrIntern m_FontName;
     179             :     CFont* m_Font{};
     180             : 
     181             :     bool m_Dirty = true;
     182             : 
     183             :     using SBatchList = std::list<SBatch, PS::Memory::AllocatorAdapter<SBatch, PS::Memory::ScopedLinearAllocator>>;
     184             :     SBatchList m_Batches;
     185             : };
     186             : 
     187             : #endif // INCLUDED_TEXTRENDERER

Generated by: LCOV version 1.16