STLL  0.0
Simple Text Layouting Library
layouter.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_H
23 #define STLL_LAYOUTER_H
24 
29 #include "layouterFont.h"
30 
31 #include "color.h"
32 
33 #include <boost/icl/split_interval_map.hpp>
34 #include <boost/icl/closed_interval.hpp>
35 
36 #include <string>
37 #include <vector>
38 #include <memory>
39 
40 #include <stdint.h>
41 
45 namespace STLL {
46 
50 {
51 public:
52  enum
53  {
57  } command;
58 
61  int32_t x;
62  int32_t y;
63 
67 
69  std::shared_ptr<FontFace_c> font;
70 
73  uint32_t w;
74  uint32_t h;
75 
79 
81  uint16_t blurr;
82 
83  std::string imageURL;
84 
87  CommandData_c(std::shared_ptr<FontFace_c> f, glyphIndex_t i, int32_t x_, int32_t y_, Color_c c_, uint16_t rad) :
88  command(CMD_GLYPH), x(x_), y(y_), glyphIndex(i), font(f), w(0), h(0), c(c_), blurr(rad) {}
89 
92  CommandData_c(const std::string & i, int32_t x_, int32_t y_, uint32_t w_, uint32_t h_) :
93  command(CMD_IMAGE), x(x_), y(y_), glyphIndex(0), w(w_), h(h_), blurr(0), imageURL(i) {}
94 
97  CommandData_c(int32_t x_, int32_t y_, uint32_t w_, uint32_t h_, Color_c c_, uint16_t rad) :
98  command(CMD_RECT), x(x_), y(y_), glyphIndex(0), w(w_), h(h_), c(c_), blurr(rad) {}
99 };
100 
106 {
107  private:
108  // position where you can add additional text after this ...
109  uint32_t height;
110  // left and right edge
111  int32_t left, right;
112  // vertical position of the very first baseline in this layout
113  int32_t firstBaseline;
114  // the drawing commands that make up this layout
115  std::vector<CommandData_c> data;
116 
117  public:
118 
121  const std::vector<CommandData_c> getData(void) const { return data; }
122 
125  {
126  public:
127  int x, y, w, h;
128  };
129 
133  {
134  public:
136  std::string url;
138  std::vector<Rectangle_c> areas;
139  };
140 
142  std::vector<LinkInformation_c> links;
143 
151  template <class... Args>
152  void addCommand(Args&&... args)
153  {
154  data.emplace_back(std::forward<Args>(args)...);
155  }
156 
163  void addCommand(const CommandData_c & c)
164  {
165  data.push_back(c);
166  }
167 
175  template <class... Args>
176  void addCommandStart(Args&&... args)
177  {
178  data.emplace(data.begin(), std::forward<Args>(args)...);
179  }
180 
188  {
189  data.insert(data.begin(), d);
190  }
191 
202  void append(const TextLayout_c & l, int dx = 0, int dy = 0);
203 
207  {
208  data.swap(l.data);
209  height = l.height;
210  left = l.left;
211  right = l.right;
212  firstBaseline = l.firstBaseline;
213  swap(links, l.links);
214  }
215 
218  void operator=(const TextLayout_c & l)
219  {
220  data = l.data;
221  height = l.height;
222  left = l.left;
223  right = l.right;
224  firstBaseline = l.firstBaseline;
225  links = l.links;
226  }
227 
228  ~TextLayout_c(void) { }
229 
232  TextLayout_c(void);
233 
236  TextLayout_c(const TextLayout_c & src);
237 
240  TextLayout_c(TextLayout_c && src);
241 
247  void shift(int32_t dx, int32_t dy);
248 
252  uint32_t getHeight(void) const { return height; }
253 
256  void setHeight(uint32_t h) { height = h; }
257 
260  int32_t getLeft(void) const { return left; }
261 
264  void setLeft(int32_t l) { left = l; }
265 
268  int32_t getRight(void) const { return right; }
269 
272  void setRight(int32_t r) { right = r; }
273 
276  void setFirstBaseline(int32_t r) { firstBaseline = r; }
277 
280  int32_t getFirstBaseline(void) const { return firstBaseline; }
281 };
282 
286 {
287  public:
288 
292 
296 
299  std::string lang;
300 
303  static const uint8_t FL_UNDERLINE = 1;
304 
307  uint8_t flags;
308 
311  class Shadow_c
312  {
313  public:
314 
318 
322  int8_t dx;
323 
327  int8_t dy;
328 
331  int16_t blurr;
332 
335  bool operator==(const struct Shadow_c & rhs) const
336  {
337  return c == rhs.c && dx == rhs.dx && dy == rhs.dy && blurr == rhs.blurr;
338  }
339  };
340 
345  std::vector<Shadow_c> shadows;
346 
359  std::shared_ptr<TextLayout_c> inlay;
360 
365  int32_t baseline_shift;
366 
373  size_t link;
374 
377  CodepointAttributes_c(void) : lang(""), flags(0), inlay(0), baseline_shift(0), link(0) { }
378 
381  bool operator==(const CodepointAttributes_c & rhs) const
382  {
383  return c == rhs.c && font == rhs.font && lang == rhs.lang
384  && flags == rhs.flags && shadows.size() && rhs.shadows.size()
385  && std::equal(shadows.begin(), shadows.end(), rhs.shadows.begin())
386  && inlay == rhs.inlay && baseline_shift == rhs.baseline_shift
387  && link == rhs.link;
388  }
389 
396  {
397  // when someone tries to overwrite the attributes, we take over the new ones
398  c = rhs.c;
399  font = rhs.font;
400  lang = rhs.lang;
401  flags = rhs.flags;
402  shadows = rhs.shadows;
403  inlay = rhs.inlay;
404  baseline_shift = rhs.baseline_shift;
405  link = rhs.link;
406 
407  return *this;
408  }
409 };
410 
411 
418 {
419  private:
420  boost::icl::interval_map<size_t, CodepointAttributes_c> attr;
421 
422  public:
427 
432  {
433  attr += std::make_pair(boost::icl::interval<size_t>::closed(0, SIZE_MAX), a);
434  }
435 
440  void set(size_t i, CodepointAttributes_c a)
441  {
442  attr += std::make_pair(boost::icl::interval<size_t>::closed(i, i), a);
443  }
444 
450  void set(size_t start, size_t end, CodepointAttributes_c a)
451  {
452  attr += std::make_pair(boost::icl::interval<size_t>::closed(start, end), a);
453  }
454 
459  const CodepointAttributes_c & get(size_t i) const
460  {
461  return attr.find(i)->second;
462  }
463 
464  bool hasAttribute(size_t i) const
465  {
466  return attr.find(i) != attr.end();
467  }
468 };
469 
478 class Shape_c
479 {
480  public:
486  virtual int32_t getLeft(int32_t top, int32_t bottom) const = 0;
487 
493  virtual int32_t getRight(int32_t top, int32_t bottom) const = 0;
494 
500  virtual int32_t getLeft2(int32_t top, int32_t bottom) const = 0;
501 
507  virtual int32_t getRight2(int32_t top, int32_t bottom) const = 0;
508 };
509 
513 class RectangleShape_c : public Shape_c
514 {
515  private:
516  int32_t w;
517 
518  public:
522  RectangleShape_c(int32_t width) : w(width) { }
523 
524  virtual int32_t getLeft(int32_t /*top*/, int32_t /*bottom*/) const { return 0; }
525  virtual int32_t getLeft2(int32_t /*top*/, int32_t /*bottom*/) const { return 0; }
526  virtual int32_t getRight(int32_t /*top*/, int32_t /*bottom*/) const { return w; }
527  virtual int32_t getRight2(int32_t /*top*/, int32_t /*bottom*/) const { return w; }
528 };
529 
533 {
534  public:
535 
536  enum {
542  } align = ALG_LEFT;
543 
544  int32_t indent = 0;
545 
546  bool ltr = true;
547 
556 
558  std::vector<std::string> links;
559 
566  bool optimizeLinebreaks = true;
567 
574  bool hyphenate = true;
575 };
576 
577 
594 TextLayout_c layoutParagraph(const std::u32string & txt32, const AttributeIndex_c & attr,
595  const Shape_c & shape, const LayoutProperties_c & prop, int32_t ystart = 0);
596 
597 }
598 
599 #endif
int8_t dy
offset of the shadow in 1/64th pixels relative to the position of the normal glyph ...
Definition: layouter.h:327
CommandData_c(std::shared_ptr< FontFace_c > f, glyphIndex_t i, int32_t x_, int32_t y_, Color_c c_, uint16_t rad)
constructor to create an glyph command
Definition: layouter.h:87
virtual int32_t getLeft(int32_t top, int32_t bottom) const =0
get the left edge
int8_t dx
offset of the shadow in 1/64th pixels relative to the position of the normal glyph ...
Definition: layouter.h:322
one element of the shadow
Definition: layouter.h:311
void setFirstBaseline(int32_t r)
set the y position of the first baseline
Definition: layouter.h:276
std::vector< std::string > links
link URLs for the links used in the CodepointAttribute_c
Definition: layouter.h:558
int32_t getLeft(void) const
get the left edge of the layout in 1/64th pixels
Definition: layouter.h:260
Font_c font
font of the letter
Definition: layouter.h:295
a little class representing an RGBA colour value, an a value of 255 is assumed to be opaque ...
Definition: color.h:36
bool operator==(const struct Shadow_c &rhs) const
comparison operator for one shadow
Definition: layouter.h:335
collection of codepointAttributes for the layouter.
Definition: layouter.h:417
int32_t x
x position
Definition: layouter.h:61
AttributeIndex_c(void)
create an empty index
Definition: layouter.h:426
uint32_t glyphIndex_t
type used for all glyph indices. Right now there is no font with more than 2^16 fonts, so 2^32 should be on the safe side. Also HarfBuzz also uses only 2^32 codepoints.
Definition: layouterFont.h:53
AttributeIndex_c(const CodepointAttributes_c &a)
create an index where all entries have the same attribute
Definition: layouter.h:431
contains all the FontFaces_c of one FontRessource_c
Definition: layouterFont.h:232
CommandData_c(int32_t x_, int32_t y_, uint32_t w_, uint32_t h_, Color_c c_, uint16_t rad)
constructor to create an rectangle command
Definition: layouter.h:97
bool hasAttribute(size_t i) const
Definition: layouter.h:464
void setRight(int32_t r)
get the right edge in 1/64th pixels
Definition: layouter.h:272
static const uint8_t FL_UNDERLINE
bit for the flags variable, if set the glyph will be underlined
Definition: layouter.h:303
const std::vector< CommandData_c > getData(void) const
get the command vector
Definition: layouter.h:121
bool ltr
is the base direction of the text left to right?
Definition: layouter.h:546
Color_c c
colour of the glyph or the rectangle
Definition: layouter.h:78
base class to define the shape to layout text into
Definition: layouter.h:478
Color_c c
colour of the letter
Definition: layouter.h:291
std::vector< Shadow_c > shadows
the shadows to draw behind the real glyph. The shadows are drawn in the order within this vector and ...
Definition: layouter.h:345
Color_c c
the colour to use for this shadow element
Definition: layouter.h:317
void addCommandStart(Args &&...args)
add a single drawing command to the start of the command list
Definition: layouter.h:176
void addCommand(const CommandData_c &c)
add a single drawing command to the end of the command list
Definition: layouter.h:163
virtual int32_t getLeft2(int32_t, int32_t) const
get the left edge
Definition: layouter.h:525
std::shared_ptr< TextLayout_c > inlay
sometimes you want to place something different instead of a glyph (e.g. an image). This can be done with an inlay.
Definition: layouter.h:359
uint8_t flags
some flags for the glyph, use FL_xxx constants to change the value
Definition: layouter.h:307
void shift(int32_t dx, int32_t dy)
shift all the commands within the layout by the given amount
RectangleShape_c(int32_t width)
construct the shape such that the area goes from 0 to width on the x-axis
Definition: layouter.h:522
glyphIndex_t glyphIndex
which glyph to draw
Definition: layouter.h:66
int16_t blurr
radius of the shadow in 1/64th pixel size
Definition: layouter.h:331
int w
Definition: layouter.h:127
TextLayout_c layoutParagraph(const std::u32string &txt32, const AttributeIndex_c &attr, const Shape_c &shape, const LayoutProperties_c &prop, int32_t ystart=0)
layout left adjusted
Definition: layouter.h:537
void operator=(TextLayout_c &&l)
move assignment
Definition: layouter.h:206
enum STLL::LayoutProperties_c::@1 align
alignment that the text is supposed to have
layout justified and the last line right adjusted
Definition: layouter.h:541
int32_t y
y position
Definition: layouter.h:62
int x
Definition: layouter.h:127
std::shared_ptr< FontFace_c > font
which front to take the glyph from
Definition: layouter.h:69
void setLeft(int32_t l)
set the left edge in 1/64th pixels
Definition: layouter.h:264
int32_t getRight(void) const
get the right edge of the layout in 1/64th pixels
Definition: layouter.h:268
CodepointAttributes_c(void)
create an empty attribute, no font, no language, no flags, no inlay, no baseline shift ...
Definition: layouter.h:377
draw a rectangle
Definition: layouter.h:55
int32_t baseline_shift
do you want to move the baseline of this character or inlay relative to the baseline of the line...
Definition: layouter.h:365
std::string imageURL
URL of image to draw.
Definition: layouter.h:83
a little structure to hold information for one rectangle
Definition: layouter.h:124
void set(size_t i, CodepointAttributes_c a)
set attributes for a single indices
Definition: layouter.h:440
virtual int32_t getRight2(int32_t top, int32_t bottom) const =0
get the right edge
this structure contains all attributes that a single glyph can get assigned
Definition: layouter.h:285
~TextLayout_c(void)
Definition: layouter.h:228
The namespace for the library. Every function and class is within this namespace. ...
Definition: color.h:31
TextLayout_c(void)
create empty layout
uint32_t w
width of block
Definition: layouter.h:73
bool hyphenate
automatically find hyphenation points
Definition: layouter.h:574
Font_c underlineFont
The font that defines the underline parameters.
Definition: layouter.h:555
virtual int32_t getRight(int32_t, int32_t) const
get the right edge
Definition: layouter.h:526
virtual int32_t getRight(int32_t top, int32_t bottom) const =0
get the right edge
virtual int32_t getRight2(int32_t, int32_t) const
get the right edge
Definition: layouter.h:527
encapsulates a finished layout.
Definition: layouter.h:105
this structure contains information for the layouter how to layout the text
Definition: layouter.h:532
concrete implementation of the shape that will allow layouting inside a rectangular with a certain wi...
Definition: layouter.h:513
void operator=(const TextLayout_c &l)
copy assignment
Definition: layouter.h:218
CommandData_c(const std::string &i, int32_t x_, int32_t y_, uint32_t w_, uint32_t h_)
constructor to create an image command
Definition: layouter.h:92
std::vector< LinkInformation_c > links
information for all links TODO this interface is bad
Definition: layouter.h:142
uint16_t blurr
blurr radius to use for this command
Definition: layouter.h:81
this file contains just a little helper class for a 4-value colour
void setHeight(uint32_t h)
set the height in 1/64th pixels
Definition: layouter.h:256
a FreeType wrapper
void addCommandStart(const CommandData_c &d)
add a single drawing command to the start of the command list
Definition: layouter.h:187
void addCommand(Args &&...args)
add a single drawing command to the end of the command list
Definition: layouter.h:152
layout right adjusted
Definition: layouter.h:538
size_t link
which link does this letter belong to
Definition: layouter.h:373
void append(const TextLayout_c &l, int dx=0, int dy=0)
append a layout to this layout, which means that the drawing commands of the 2nd layout are copied in...
enum STLL::CommandData_c::@0 command
specifies what to draw
uint32_t getHeight(void) const
the height of the layout. This is supposed to be the vertical space that this layout takes up in 1/64...
Definition: layouter.h:252
bool optimizeLinebreaks
Choose between fast and optimizing linebreak algorithm.
Definition: layouter.h:566
virtual int32_t getLeft2(int32_t top, int32_t bottom) const =0
get the left edge
int y
Definition: layouter.h:127
This structure encapsulates a drawing command.
Definition: layouter.h:49
layout justified and the last line left adjusted
Definition: layouter.h:540
bool operator==(const CodepointAttributes_c &rhs) const
comparison operator
Definition: layouter.h:381
uint32_t h
height of block
Definition: layouter.h:74
void set(size_t start, size_t end, CodepointAttributes_c a)
set attributes for a range of indices
Definition: layouter.h:450
virtual int32_t getLeft(int32_t, int32_t) const
get the left edge
Definition: layouter.h:524
int32_t indent
indentation of the first line in pixel
Definition: layouter.h:544
CodepointAttributes_c operator+=(const CodepointAttributes_c &rhs)
this operator is required for the interval container within the attributeIndex_c class, do not use it
Definition: layouter.h:395
layout centred
Definition: layouter.h:539
std::string lang
current language at this letter
Definition: layouter.h:299
int32_t getFirstBaseline(void) const
get the y position of the first baseline
Definition: layouter.h:280
draw an image
Definition: layouter.h:56
int h
Definition: layouter.h:127
draw a glyph from a font
Definition: layouter.h:54