6
#include "DisplayBase.h"
7
#include "TextRendererPangoFT2.h"
9
TextRendererPangoFT2::TextRendererPangoFT2(DisplayBase *display)
10
: TextRenderer(display), direction_(PANGO_DIRECTION_LTR)
14
fontMap_ = pango_ft2_font_map_new();
17
pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fontMap_),
20
context_ = pango_ft2_font_map_create_context(PANGO_FT2_FONT_MAP(fontMap_));
22
pango_context_set_language(context_,
23
pango_language_from_string ("en_US"));
24
pango_context_set_base_dir(context_, direction_);
26
fontDescription_ = pango_font_description_new();
28
Options *options = Options::getInstance();
29
Font(options->Font());
30
FontSize(options->FontSize());
32
layout_ = pango_layout_new(context_);
33
pango_layout_set_width(layout_, -1); // Don't wrap lines
36
TextRendererPangoFT2::~TextRendererPangoFT2()
38
g_object_unref(layout_);
39
pango_font_description_free(fontDescription_);
40
g_object_unref(context_);
41
g_object_unref(fontMap_);
42
pango_ft2_shutdown_display();
46
TextRendererPangoFT2::Font(const string &font)
48
pango_font_description_set_family(fontDescription_, font.c_str());
49
pango_font_description_set_style(fontDescription_, PANGO_STYLE_NORMAL);
50
pango_font_description_set_variant(fontDescription_,
51
PANGO_VARIANT_NORMAL);
52
pango_font_description_set_weight(fontDescription_, PANGO_WEIGHT_NORMAL);
53
pango_font_description_set_stretch(fontDescription_,
54
PANGO_STRETCH_NORMAL);
59
TextRendererPangoFT2::FontSize(const int size)
61
pango_font_description_set_size(fontDescription_, size * PANGO_SCALE);
65
TextRendererPangoFT2::FontHeight() const
70
pango_layout_get_extents(layout_, NULL, &rect);
72
returnVal = static_cast<int> (1.5 * PANGO_PIXELS(pango_font_description_get_size(fontDescription_)));
78
TextRendererPangoFT2::DrawText(const int x, const int y,
79
const unsigned char color[3])
83
int textWidth, textHeight;
84
TextBox(textWidth, textHeight);
86
unsigned char *buffer = new unsigned char[textWidth * textHeight];
87
memset(buffer, 0, textWidth * textHeight);
88
bitmap.rows = textHeight;
89
bitmap.width = textWidth;
90
bitmap.pitch = bitmap.width;
91
bitmap.buffer = buffer;
92
bitmap.num_grays = 256;
93
bitmap.pixel_mode = ft_pixel_mode_grays;
95
pango_ft2_render_layout(&bitmap, layout_, 0, 0);
97
for (int j = 0; j < bitmap.rows; j++)
99
int istart = j * bitmap.width;
100
for (int k = 0; k < bitmap.width; k++)
102
if (bitmap.buffer[istart + k])
104
const double opacity = bitmap.buffer[istart + k]/255.0;
105
display_->setPixel(x + k,
116
TextRendererPangoFT2::SetText(const std::string &text)
118
pango_layout_set_text(layout_, text.c_str(), text.size());
119
pango_layout_set_alignment(layout_, PANGO_ALIGN_LEFT);
120
pango_layout_set_font_description(layout_, fontDescription_);
124
TextRendererPangoFT2::FreeText()
129
TextRendererPangoFT2::TextBox(int &textWidth, int &textHeight)
132
pango_layout_get_extents(layout_, NULL, &rect);
133
textWidth = PANGO_PIXELS(rect.width);
134
textHeight = PANGO_PIXELS(rect.height);