~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to src/libdisplay/TextRendererPangoFT2.cpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <string>
 
2
using namespace std;
 
3
 
 
4
#include "Options.h"
 
5
 
 
6
#include "DisplayBase.h"
 
7
#include "TextRendererPangoFT2.h"
 
8
 
 
9
TextRendererPangoFT2::TextRendererPangoFT2(DisplayBase *display) 
 
10
    : TextRenderer(display), direction_(PANGO_DIRECTION_LTR)
 
11
{
 
12
    g_type_init();
 
13
 
 
14
    fontMap_ = pango_ft2_font_map_new();
 
15
    int dpiX = 100;
 
16
    int dpiY = 100;
 
17
    pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fontMap_), 
 
18
                                      dpiX, dpiY);
 
19
 
 
20
    context_ = pango_ft2_font_map_create_context(PANGO_FT2_FONT_MAP(fontMap_));
 
21
 
 
22
    pango_context_set_language(context_,
 
23
                               pango_language_from_string ("en_US"));
 
24
    pango_context_set_base_dir(context_, direction_);
 
25
 
 
26
    fontDescription_ = pango_font_description_new();
 
27
 
 
28
    Options *options = Options::getInstance();
 
29
    Font(options->Font());
 
30
    FontSize(options->FontSize());
 
31
 
 
32
    layout_ = pango_layout_new(context_);
 
33
    pango_layout_set_width(layout_, -1); // Don't wrap lines
 
34
}
 
35
 
 
36
TextRendererPangoFT2::~TextRendererPangoFT2()
 
37
{
 
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();
 
43
}
 
44
 
 
45
void
 
46
TextRendererPangoFT2::Font(const string &font)
 
47
{
 
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);
 
55
 
 
56
}
 
57
 
 
58
void
 
59
TextRendererPangoFT2::FontSize(const int size)
 
60
{
 
61
    pango_font_description_set_size(fontDescription_, size * PANGO_SCALE);
 
62
}
 
63
 
 
64
int
 
65
TextRendererPangoFT2::FontHeight() const
 
66
{
 
67
    int returnVal = 0;
 
68
 
 
69
    PangoRectangle rect;
 
70
    pango_layout_get_extents(layout_, NULL, &rect);
 
71
 
 
72
    returnVal = static_cast<int> (1.5 * PANGO_PIXELS(pango_font_description_get_size(fontDescription_)));
 
73
 
 
74
    return(returnVal);
 
75
}
 
76
 
 
77
void
 
78
TextRendererPangoFT2::DrawText(const int x, const int y, 
 
79
                               const unsigned char color[3])
 
80
{
 
81
    FT_Bitmap bitmap;
 
82
 
 
83
    int textWidth, textHeight;
 
84
    TextBox(textWidth, textHeight);
 
85
 
 
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;
 
94
    
 
95
    pango_ft2_render_layout(&bitmap, layout_, 0, 0);
 
96
 
 
97
    for (int j = 0; j < bitmap.rows; j++)
 
98
    {
 
99
        int istart = j * bitmap.width;
 
100
        for (int k = 0; k < bitmap.width; k++)
 
101
        {
 
102
            if (bitmap.buffer[istart + k])
 
103
            {
 
104
                const double opacity = bitmap.buffer[istart + k]/255.0;
 
105
                display_->setPixel(x + k, 
 
106
                                   y + j - textHeight, 
 
107
                                   color, opacity);
 
108
            }
 
109
        }
 
110
    }
 
111
 
 
112
    delete [] buffer;
 
113
}
 
114
 
 
115
void
 
116
TextRendererPangoFT2::SetText(const std::string &text)
 
117
{
 
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_);
 
121
}
 
122
 
 
123
void
 
124
TextRendererPangoFT2::FreeText()
 
125
{
 
126
}
 
127
 
 
128
void
 
129
TextRendererPangoFT2::TextBox(int &textWidth, int &textHeight)
 
130
{
 
131
    PangoRectangle rect;
 
132
    pango_layout_get_extents(layout_, NULL, &rect);
 
133
    textWidth = PANGO_PIXELS(rect.width);
 
134
    textHeight = PANGO_PIXELS(rect.height);
 
135
}