OpenClonk
C4Markup.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
5  * Copyright (c) 2011-2016, The OpenClonk Team and contributors
6  *
7  * Distributed under the terms of the ISC license; see accompanying file
8  * "COPYING" for details.
9  *
10  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11  * See accompanying file "TRADEMARK" for details.
12  *
13  * To redistribute this file separately, substitute the full license texts
14  * for the above references.
15  */
16 // markup tags for fonts
17 
18 #ifndef INC_STDMARKUP
19 #define INC_STDMARKUP
20 
21 // one markup tag
23 {
24 public:
25  C4MarkupTag *pPrev{nullptr}, *pNext{nullptr};
26 
27  C4MarkupTag() = default; // ctor
28  virtual ~C4MarkupTag() = default; // dtor
29 
30  virtual void Apply(C4BltTransform &rBltTrf, bool fDoClr, DWORD &dwClr)=0; // assign markup
31  virtual const char *TagName() const = 0; // get character string for this tag
32  virtual std::string OpeningTag() const; // get opening tag, i.e. "<{TagName()}>"
33  virtual std::string ClosingTag() const; // get closing tag, i.e. "</{TagName()}>"
34 };
35 
36 // markup tag for italic text
38 {
39 public:
40  C4MarkupTagItalic() : C4MarkupTag() { } // ctor
41 
42  void Apply(C4BltTransform &rBltTrf, bool fDoClr, DWORD &dwClr) override; // assign markup
43  const char *TagName() const override { return "i"; }
44 };
45 
46 // markup tag for colored text
48 {
49 private:
50  DWORD dwClr; // color
51 public:
52  C4MarkupTagColor(DWORD dwClr) : C4MarkupTag(), dwClr(dwClr) { } // ctor
53 
54  void Apply(C4BltTransform &rBltTrf, bool fDoClr, DWORD &dwClr) override; // assign markup
55  const char *TagName() const override { return "c"; }
56  std::string OpeningTag() const override;
57 };
58 
59 // markup rendering functionality for text
60 class C4Markup
61 {
62 private:
63  C4MarkupTag *pTags, *pLast; // tag list; double linked
64  bool fDoClr; // set if color changes should be made (not in text shadow!)
65 
66  void Push(C4MarkupTag *pTag)
67  { if ((pTag->pPrev=pLast)) pLast->pNext=pTag; else pTags=pTag; pLast=pTag; }
68  C4MarkupTag *Pop()
69  { C4MarkupTag *pL=pLast; if (!pL) return nullptr; if ((pLast=pL->pPrev)) pLast->pNext=nullptr; else pTags=nullptr; return pL; }
70 public:
71  C4Markup(bool fDoClr) { pTags=pLast=nullptr; this->fDoClr=fDoClr; }; // ctor
72  ~C4Markup() // dtor
73  { C4MarkupTag *pTag=pTags,*pNext; while (pTag) { pNext=pTag->pNext; delete pTag; pTag=pNext; } }
74 
75  bool Read(const char **ppText, bool fSkip=false); // get markup from text
76  bool SkipTags(const char **ppText); // extract markup from text; return whether end is reached
77  void Apply(C4BltTransform &rBltTrf, DWORD &dwClr) // assign markup to vertices
78  { for (C4MarkupTag *pTag=pTags; pTag; pTag=pTag->pNext) pTag->Apply(rBltTrf, fDoClr, dwClr); }
79  bool Clean() { return !pTags; } // empty?
80 
81  // The following two functions are for splitting a markup string.
82  std::string ClosingTags() const; // get all closing tags at the current location
83  std::string OpeningTags() const; // get all opening tags at the current location
84 
85  static bool StripMarkup(char *szText); // strip any markup codes from given text buffer
86  static bool StripMarkup(StdStrBuf *sText); // strip any markup codes from given text buffer
87 };
88 #endif // INC_STDMARKUP
~C4Markup()
Definition: C4Markup.h:72
virtual ~C4MarkupTag()=default
C4MarkupTag * pPrev
Definition: C4Markup.h:25
virtual std::string OpeningTag() const
Definition: C4Markup.cpp:24
C4MarkupTag * pNext
Definition: C4Markup.h:25
virtual void Apply(C4BltTransform &rBltTrf, bool fDoClr, DWORD &dwClr)=0
const char * TagName() const override
Definition: C4Markup.h:43
C4MarkupTagColor(DWORD dwClr)
Definition: C4Markup.h:52
bool Clean()
Definition: C4Markup.h:79
C4Markup(bool fDoClr)
Definition: C4Markup.h:71
virtual std::string ClosingTag() const
Definition: C4Markup.cpp:29
C4MarkupTag()=default
void Apply(C4BltTransform &rBltTrf, DWORD &dwClr)
Definition: C4Markup.h:77
uint32_t DWORD
virtual const char * TagName() const =0
const char * TagName() const override
Definition: C4Markup.h:55