STLL  0.0
Simple Text Layouting Library
layouterCSS.h
Go to the documentation of this file.
1 /*
2  * STLL Simple Text Layouting Library
3  *
4  * STLL is the legal property of its developers, whose
5  * names are listed in the COPYRIGHT file, which is included
6  * within the source distribution.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1
11  * of the License, or (at your option) any later version.
12 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */
22 #ifndef STLL_LAYOUTER_CSS_H
23 #define STLL_LAYOUTER_CSS_H
24 
29 #include "layouterFont.h"
30 
31 #include "internal/xmllibraries.h"
32 #include "internal/layouterCSS_internal.h"
33 
34 #include <string>
35 #include <memory>
36 #include <vector>
37 #include <map>
38 
39 namespace STLL {
40 
43 class XhtmlException_c : public std::runtime_error
44 {
45  public:
46  explicit XhtmlException_c(const std::string & what_arg) : std::runtime_error(what_arg) {}
47 
48 };
49 
57 {
58  typedef struct
59  {
60  std::string selector;
61  std::string attribute;
62  std::string value;
63  } rule;
64 
65  public:
66 
74  TextStyleSheet_c(std::shared_ptr<FontCache_c> c = 0);
75 
91  void addFont(const std::string & family, const FontResource_c & res,
92  const std::string & style = "normal",
93  const std::string & variant = "normal",
94  const std::string & weight = "normal",
95  const std::string & stretch = "normal");
96 
102  std::shared_ptr<FontFamily_c> findFamily(const std::string & family) const
103  {
104  auto i = families.find(family);
105 
106  if (i != families.end())
107  return families.find(family)->second;
108  else
109  return 0;
110  }
111 
122  void addRule(const std::string sel, const std::string attr, const std::string val);
123 
127  {
128  useOptimizingLayouter = on;
129  }
130 
132  bool getUseOptimizingLayouter(void) const { return useOptimizingLayouter; }
133 
136  void setHyphenate(bool on)
137  {
138  hyphenate = on;
139  }
140 
142  bool getHyphenate(void) const { return hyphenate; }
143 
159  template <class X>
160  const std::string & getValue(X node, const std::string & attribute, const std::string & def = "") const
161  {
162  // go through all rules, check only the ones that give a value to the requested attribute
163  // evaluate rule by priority (look at the CSS priority rules
164  // choose the highest priority
165 
166  while (!internal::xml_isEmpty(node))
167  {
168  uint16_t prio = 0;
169  size_t bestI;
170 
171  for (size_t i = 0; i < rules.size(); i++)
172  {
173  if ( rules[i].attribute == attribute
174  && internal::ruleFits(rules[i].selector, node)
175  && internal::rulePrio(rules[i].selector) > prio
176  )
177  {
178  prio = internal::rulePrio(rules[i].selector);
179  bestI = i;
180  }
181  }
182 
183  if (prio)
184  return rules[bestI].value;
185 
186  if (!internal::isInheriting(attribute))
187  {
188  if (def.empty())
189  {
190  return internal::getDefault(attribute);
191  }
192  else
193  {
194  return def;
195  }
196  }
197 
198  node = internal::xml_getParent(node);
199  }
200 
201  return internal::getDefault(attribute);
202  }
203 
204  private:
205  std::vector<rule> rules;
206  std::map<std::string, std::shared_ptr<FontFamily_c> > families;
207  std::shared_ptr<FontCache_c> cache;
208  bool useOptimizingLayouter = true;
209  bool hyphenate = true;
210 };
211 
212 }
213 
214 #endif
This class represents a font resource.
Definition: layouterFont.h:87
bool getUseOptimizingLayouter(void) const
get status of optimizing layouter
Definition: layouterCSS.h:132
this class encapsulates information for how to format a text, just like the style sheets in html are ...
Definition: layouterCSS.h:56
void addRule(const std::string sel, const std::string attr, const std::string val)
add a rule to the stylesheet
void setUseOptimizingLayouter(bool on)
enable or disable usage of the optimizing layouter. See LayoutProperties_c for details ...
Definition: layouterCSS.h:126
std::shared_ptr< FontFamily_c > findFamily(const std::string &family) const
Get a font family from the CSS.
Definition: layouterCSS.h:102
void addFont(const std::string &family, const FontResource_c &res, const std::string &style="normal", const std::string &variant="normal", const std::string &weight="normal", const std::string &stretch="normal")
Add a font to a family.
bool getHyphenate(void) const
get status of hyphenation setting
Definition: layouterCSS.h:142
The namespace for the library. Every function and class is within this namespace. ...
Definition: color.h:31
XhtmlException_c(const std::string &what_arg)
Definition: layouterCSS.h:46
TextStyleSheet_c(std::shared_ptr< FontCache_c > c=0)
create an empty style sheet only with default rules
a FreeType wrapper
const std::string & getValue(X node, const std::string &attribute, const std::string &def="") const
get the value for an attribute for a given xml-node
Definition: layouterCSS.h:160
exception thrown on XHTML and CSS problems
Definition: layouterCSS.h:43
void setHyphenate(bool on)
enable or disable hyphenation. See LayoutProperties_c for details
Definition: layouterCSS.h:136