OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4Markup.cpp
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 #include "C4Include.h"
19 #include "lib/C4Markup.h"
21 
22 void C4MarkupTagItalic::Apply(C4BltTransform &rBltTrf, bool fDoClr, DWORD &dwClr)
23 {
24  // do sheering
25  rBltTrf.mat[1]-=0.3f;
26 }
27 
28 void C4MarkupTagColor::Apply(C4BltTransform &rBltTrf, bool fDoClr, DWORD &dwClr)
29 {
30  // set color
31  if (fDoClr) dwClr = this->dwClr;
32 }
33 
34 bool C4Markup::Read(const char **ppText, bool fSkip)
35 {
36  char Tag[50]; C4MarkupTag *pNewTag=nullptr; int iTagLen,iParLen;
37  // get tag
38  if (!SCopyEnclosed(*ppText, '<', '>', Tag, 49)) return false;
39  iTagLen=SLen(Tag);
40  // split tag to name and pars
41  char *szPars=nullptr; int iSPos;
42  if ((iSPos=SCharPos(' ', Tag))>-1)
43  {
44  Tag[iSPos]=0;
45  szPars=Tag+iSPos+1;
46  }
47  // closing tag?
48  if (Tag[0]=='/')
49  {
50  // no parameters
51  if (szPars) return false;
52  if (!fSkip)
53  {
54  // is this the tag to be closed?
55  if (!pLast) return false;
56  if (!SEqual(pLast->TagName(), Tag+1)) return false;
57  // close it
58  delete Pop();
59  }
60  }
61  // italic
62  else if (SEqual(Tag, "i"))
63  {
64  // no parameters
65  if (szPars) return false;
66  // create italic tag
67  if (!fSkip) pNewTag=new C4MarkupTagItalic();
68  }
69  // colored
70  else if (SEqual(Tag, "c"))
71  {
72  // no parameters?
73  if (!szPars) return false;
74  if ((iParLen=SLen(szPars))>8) return false;
75  if (!fSkip)
76  {
77  // get color value by parameter
78  DWORD dwClr=0;
79  for (int i=0; i<iParLen; ++i)
80  {
81  BYTE b;
82  if (szPars[i]>='0' && szPars[i]<='9') b=szPars[i]-'0';
83  else if (szPars[i]>='a' && szPars[i]<='f') b=szPars[i]-'a'+10;
84  else return false;
85  dwClr|=(b<<((iParLen-i-1)*4));
86  }
87  // adjust alpha if not given
88  if (iParLen<=6) dwClr|=0xff000000;
89  // create color tag
90  pNewTag=new C4MarkupTagColor(dwClr);
91  }
92  }
93  // unknown tag
94  else return false;
95  // add created tag
96  if (pNewTag) Push(pNewTag);
97  // advance past tag
98  *ppText+=iTagLen+2;
99  // success
100  return true;
101 }
102 
103 
104 
105 bool C4Markup::SkipTags(const char **ppText)
106 {
107  // read tags as long as found
108  while (**ppText=='<') if (!Read(ppText, true)) break;
109  // return whether end is reached
110  return !**ppText;
111 }
112 
113 bool C4Markup::StripMarkup(char *szText)
114 {
115  // skip any tags and inline-images
116  C4Markup mkup(false);
117  const char *szRead = szText, *szPos2;
118  do
119  {
120  mkup.SkipTags(&szRead);
121  if (szRead[0] == '{' && szRead[1] == '{' && szRead[2] != '{') // skip at {{{, because {{{id}} should be parsed as { {{id}} }.
122  {
123  if ((szPos2 = SSearch(szRead+2, "}}")))
124  // valid {{blub}}-tag
125  szRead = szPos2;
126  else
127  // invalid {{-tag
128  szRead += 2;
129  }
130  else if (szRead[0] == '}' && szRead[1] == '}')
131  // invalid }}-tag
132  szRead += 2;
133  }
134  while ((*szText++ = *szRead++));
135  return szText != szRead;
136 }
137 
139 {
140  // strip any markup codes from given text buffer
141  char *buf = sText->GrabPointer();
142  if (!buf) return false;
143  bool fSuccess = StripMarkup(buf);
144  sText->Take(buf);
145  return fSuccess;
146 }
char * GrabPointer()
Definition: StdBuf.h:459
#define b
const char * SSearch(const char *szString, const char *szIndex)
Definition: Standard.cpp:363
bool SCopyEnclosed(const char *szSource, char cOpen, char cClose, char *sTarget, int iSize)
Definition: Standard.cpp:523
uint8_t BYTE
virtual const char * TagName()=0
size_t SLen(const char *sptr)
Definition: Standard.h:74
bool SEqual(const char *szStr1, const char *szStr2)
Definition: Standard.h:93
bool SkipTags(const char **ppText)
Definition: C4Markup.cpp:105
void Take(char *pnData)
Definition: StdBuf.h:457
bool Read(const char **ppText, bool fSkip=false)
Definition: C4Markup.cpp:34
void Apply(C4BltTransform &rBltTrf, bool fDoClr, DWORD &dwClr) override
Definition: C4Markup.cpp:28
int SCharPos(char cTarget, const char *szInStr, int iIndex)
Definition: Standard.cpp:233
uint32_t DWORD
void Apply(C4BltTransform &rBltTrf, bool fDoClr, DWORD &dwClr) override
Definition: C4Markup.cpp:22
static bool StripMarkup(char *szText)
Definition: C4Markup.cpp:113