FontSupport::FontMatch FontSupport::matchFont(const WFont& f) const { for (MatchCache::iterator i = cache_.begin(); i != cache_.end(); ++i) { if (i->font == f) { cache_.splice(cache_.begin(), cache_, i); // implement LRU return FontMatch(i->match, i->desc); } } PANGO_LOCK; enabledFontFormats = enabledFontFormats_; PangoFontDescription *desc = createFontDescription(f); PangoFont *match = pango_font_map_load_font(pangoFontMap, context_, desc); pango_context_set_font_description(context_, desc); // for layoutText() if (cache_.back().match) { g_object_unref(cache_.back().match); pango_font_description_free(cache_.back().desc); } cache_.pop_back(); cache_.push_front(Matched(f, match, desc)); return FontMatch(match, desc); }
GList *FontSupport::layoutText(const WFont& font, const std::string& utf8, std::vector<PangoGlyphString *>& glyphs, int& width) { PANGO_LOCK; PangoFontDescription *desc = createFontDescription(font); pango_context_set_font_description(context_, desc); pango_font_description_free(desc); PangoAttrList *attrs = pango_attr_list_new(); GList *items = pango_itemize(context_, utf8.c_str(), 0, utf8.length(), attrs, NULL); width = 0; for (GList *elem = items; elem; elem = elem->next) { PangoItem *item = (PangoItem *)elem->data; PangoAnalysis *analysis = &item->analysis; PangoGlyphString *gl = pango_glyph_string_new(); pango_shape(utf8.c_str() + item->offset, item->length, analysis, gl); glyphs.push_back(gl); if (device_) { currentFont_ = analysis->font; WTextItem textItem = device_->measureText(WString::fromUTF8(utf8.substr(item->offset, item->length)), -1, false); width += pangoUnitsFromDouble(textItem.width()); currentFont_ = 0; } else width += pango_glyph_string_get_width(gl); } pango_attr_list_unref(attrs); return items; }
FontSupport::FontMatch FontSupport::matchFont(const WFont& f) const { if (wtFont_ == f) return FontMatch(matchFont_); wtFont_ = f; PANGO_LOCK; PangoFontDescription *desc = createFontDescription(f); if (matchFont_) g_object_unref(matchFont_); matchFont_ = pango_font_map_load_font(pangoFontMap, context_, desc); pango_font_description_free(desc); return FontMatch(matchFont_); }