OSDN Git Service

TextView strict layout.
[fukui-no-namari/dialektos.git] / src / text_element_char_size_cache.cxx
index b255966..6e1798f 100644 (file)
@@ -20,6 +20,9 @@
 
 #include "text_element_char_size_cache.hxx"
 
+#include <pangomm/glyphstring.h>
+#include <boost/foreach.hpp>
+
 
 namespace dialektos {
 
@@ -33,44 +36,42 @@ inline gunichar ucs4_to_ucs2(const gunichar ucs4) {
 } // anonymous namespace
 
 
-CharSizeCache::CharSizeCache() { initialize(); }
+CharSizeCache::ArrayClass::ArrayClass() : array_() {
+  BOOST_FOREACH(int& i, array_) i = -1;
+}
 
-void CharSizeCache::initialize() {
-  for (size_t i = 0; i != ArrayType::size(); ++i) {
-    char_width_cache[i] = -1;
-    char_height_cache[i] = -1;
-    bold_char_width_cache[i] = -1;
-    bold_char_height_cache[i] = -1;
-  }
+
+CharSizeCache::CharSizeCache() {}
+
+CharSizeCache::ArrayClass& CharSizeCache::get_map(int font_id) {
+  return map_[font_id];
 }
 
-void CharSizeCache::get_char_size(gunichar uch,
-    Glib::RefPtr<Pango::Layout>& layout,
-    double& width, double& height, bool bold) {
+double CharSizeCache::get_char_width(
+    gunichar uch, const Pango::Item& item, ArrayClass& cache) {
 
   const gunichar ucs2 = ucs4_to_ucs2(uch);
 
-  int w = get_width_cache_array(bold)[ucs2];
-  int h = get_height_cache_array(bold)[ucs2];
-
-  if (w == -1 || h == -1) {
-    Glib::ustring ch(1, ucs2);
-    layout->set_text(ch);
-    layout->get_size(w, h);
-    get_width_cache_array(bold)[ucs2] = w;
-    get_height_cache_array(bold)[ucs2] = h;
+  int width = cache.array_[ucs2];
+  if (width == -1) {
+    const Glib::ustring text(1, uch);
+    Pango::GlyphString glyphs = item.shape(text);
+    width = glyphs.get_width();
+    cache.array_[ucs2] = width;
   }
-
-  width = double(w) / Pango::SCALE;
-  height = double(h) / Pango::SCALE;
+  return double(width) / Pango::SCALE;
 }
 
-void CharSizeCache::get_char_size_from_cache(gunichar uch,
-    double& width, double& height, bool bold) const {
+double CharSizeCache::get_char_width(
+    gunichar uch, const Pango::Item& item, int font_id) {
+  return get_char_width(uch, item, get_map(font_id));
+}
 
-  const gunichar ucs2 = ucs4_to_ucs2(uch);
-  width = double(get_width_cache_array(bold)[ucs2]) / Pango::SCALE;
-  height = double(get_height_cache_array(bold)[ucs2]) / Pango::SCALE;
+double CharSizeCache::get_char_width(gunichar uch, const Pango::Item& item) {
+  Pango::Analysis analysis = item.get_analysis();
+  Glib::RefPtr<Pango::Font> font = analysis.get_font();
+  const int font_id = reinterpret_cast<int>(font->gobj());
+  return get_char_width(uch, item, get_map(font_id));
 }