tagging release
[dasher.git] / Src / Gtk2 / PangoCache.cpp
blobf8b71d6956e1cebe26958a8d68ae2573e3bf7298
1 #include "../Common/Common.h"
3 #include "PangoCache.h"
4 #include <iostream>
6 CPangoCache::CPangoCache(const std::string &strFontName) {
7 font = pango_font_description_from_string(strFontName.c_str());
10 void CPangoCache::ChangeFont(const std::string &strFontName) {
11 pango_font_description_free(font);
12 font = pango_font_description_from_string(strFontName.c_str());
14 // FIXME - probably need to free the pango layouts, but I can't find a function to do this.
16 oPangoCache.clear();
19 #if WITH_CAIRO
20 PangoLayout *CPangoCache::GetLayout(cairo_t *cr, std::string sDisplayText, int iSize) {
21 #else
22 PangoLayout *CPangoCache::GetLayout(GtkWidget *pCanvas, std::string sDisplayText, int iSize) {
23 #endif
25 // Calculate the name of the pango layout in the cache - this
26 // includes the display text and the size.
28 char buffer[128]; // FIXME - what if we exceed this?
30 snprintf(buffer, 128, "%d_%s", iSize, sDisplayText.c_str());
31 // // std::stringstream sCacheName;
32 // //sCacheName << iSize << "_" << sDisplayText;
34 std::string sCacheName(buffer);
36 // TODO: This is blatantly not going to work for unicode
37 // int iCacheIndex(iSize + 256 * sDisplayText[0]);
39 // If we haven't got a cached pango layout for this string/size yet,
40 // create a new one
42 std::map<std::string, PangoLayout *>::iterator it(oPangoCache.find(sCacheName));
44 if(it != oPangoCache.end())
45 return it->second;
46 else {
48 #if WITH_CAIRO
49 PangoLayout *pNewPangoLayout(pango_cairo_create_layout(cr));
50 #else
51 PangoLayout *pNewPangoLayout(gtk_widget_create_pango_layout(pCanvas, ""));
52 #endif
54 pango_font_description_set_size(font, iSize * PANGO_SCALE);
55 pango_layout_set_font_description(pNewPangoLayout, font);
56 pango_layout_set_text(pNewPangoLayout, sDisplayText.c_str(), -1);
58 oPangoCache[sCacheName] = pNewPangoLayout;
60 return pNewPangoLayout;
62 return 0;