genode/repos/os/include/nitpicker_gfx/text_painter.h
Norman Feske eba9c15746 Follow practices suggested by "Effective C++"
The patch adjust the code of the base, base-<kernel>, and os repository.
To adapt existing components to fix violations of the best practices
suggested by "Effective C++" as reported by the -Weffc++ compiler
argument. The changes follow the patterns outlined below:

* A class with virtual functions can no longer publicly inherit base
  classed without a vtable. The inherited object may either be moved
  to a member variable, or inherited privately. The latter would be
  used for classes that inherit 'List::Element' or 'Avl_node'. In order
  to enable the 'List' and 'Avl_tree' to access the meta data, the
  'List' must become a friend.

* Instead of adding a virtual destructor to abstract base classes,
  we inherit the new 'Interface' class, which contains a virtual
  destructor. This way, single-line abstract base classes can stay
  as compact as they are now. The 'Interface' utility resides in
  base/include/util/interface.h.

* With the new warnings enabled, all member variables must be explicitly
  initialized. Basic types may be initialized with '='. All other types
  are initialized with braces '{ ... }' or as class initializers. If
  basic types and non-basic types appear in a row, it is nice to only
  use the brace syntax (also for basic types) and align the braces.

* If a class contains pointers as members, it must now also provide a
  copy constructor and assignment operator. In the most cases, one
  would make them private, effectively disallowing the objects to be
  copied. Unfortunately, this warning cannot be fixed be inheriting
  our existing 'Noncopyable' class (the compiler fails to detect that
  the inheriting class cannot be copied and still gives the error).
  For now, we have to manually add declarations for both the copy
  constructor and assignment operator as private class members. Those
  declarations should be prepended with a comment like this:

        /*
         * Noncopyable
         */
        Thread(Thread const &);
        Thread &operator = (Thread const &);

  In the future, we should revisit these places and try to replace
  the pointers with references. In the presence of at least one
  reference member, the compiler would no longer implicitly generate
  a copy constructor. So we could remove the manual declaration.

Issue #465
2018-01-17 12:14:35 +01:00

134 lines
3.3 KiB
C++

/*
* \brief Functor for drawing text into a surface
* \author Norman Feske
* \date 2006-08-04
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _INCLUDE__NITPICKER_GFX__TEXT_PAINTER_H_
#define _INCLUDE__NITPICKER_GFX__TEXT_PAINTER_H_
#include <base/stdint.h>
#include <os/surface.h>
struct Text_painter
{
class Font
{
private:
typedef Genode::int32_t int32_t;
typedef Genode::size_t size_t;
public:
unsigned char const *img; /* font image */
int const img_w, img_h; /* size of font image */
int32_t const *otab; /* offset table */
int32_t const *wtab; /* width table */
/**
* Construct font from a TFF data block
*/
Font(const char *tff)
:
img((unsigned char *)(tff + 2056)),
img_w(*((int32_t *)(tff + 2048))),
img_h(*((int32_t *)(tff + 2052))),
otab((int32_t *)(tff)),
wtab((int32_t *)(tff + 1024))
{ }
/**
* Calculate width of string when printed with the font
*/
int str_w(const char *sstr, size_t len = ~0UL) const
{
const unsigned char *str = (const unsigned char *)sstr;
int res = 0;
for (; str && *str && len; len--, str++) res += wtab[*str];
return res;
}
/**
* Calculate height of string when printed with the font
*/
int str_h(const char *, size_t = ~0UL) const { return img_h; }
};
typedef Genode::Surface_base::Point Point;
typedef Genode::Surface_base::Area Area;
typedef Genode::Surface_base::Rect Rect;
template <typename PT>
static inline void paint(Genode::Surface<PT> &surface,
Point p,
Font const &font,
Genode::Color color,
char const *sstr)
{
unsigned char const *str = (unsigned char const *)sstr;
int x = p.x(), y = p.y();
unsigned char const *src = font.img;
int d, h = font.img_h;
/* check top clipping */
if ((d = surface.clip().y1() - y) > 0) {
src += d*font.img_w;
y += d;
h -= d;
}
/* check bottom clipping */
if ((d = y + h -1 - surface.clip().y2()) > 0)
h -= d;
if (h < 1) return;
/* skip hidden glyphs */
for ( ; *str && (x + font.wtab[*str] < surface.clip().x1()); )
x += font.wtab[*str++];
int const x_start = x;
PT *dst = surface.addr() + y*surface.size().w();
PT const pix(color.r, color.g, color.b);
int const alpha = color.a;
/* draw glyphs */
for ( ; *str && (x <= surface.clip().x2()); str++) {
int const w = font.wtab[*str];
int const start = Genode::max(0, surface.clip().x1() - x);
int const end = Genode::min(w - 1, surface.clip().x2() - x);
PT *d = dst + x;
unsigned char const *s = src + font.otab[*str];
for (int j = 0; j < h; j++, s += font.img_w, d += surface.size().w())
for (int i = start; i <= end; i++)
if (s[i])
d[i] = (s[i] == 255 && alpha == 255)
? pix : PT::mix(d[i], pix, (alpha*s[i]) >> 8);
x += w;
}
surface.flush_pixels(Rect(Point(x_start, y), Area(x - x_start + 1, h)));
}
};
#endif /* _INCLUDE__NITPICKER_GFX__TEXT_PAINTER_H_ */