OSDN Git Service

Change idx sub dir's time stamp when saving idx.
[fukui-no-namari/dialektos.git] / src / text_element_char_size_cache.cxx
1 /*
2  * Copyright (C) 2009 by Aiwota Programmer
3  * aiwotaprog@tetteke.tk
4  *
5  * This file is part of Dialektos.
6  *
7  * Dialektos is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * Dialektos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Dialektos.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "text_element_char_size_cache.hxx"
22
23
24 namespace dialektos {
25
26 namespace text_element {
27
28
29 namespace {
30 inline gunichar ucs4_to_ucs2(const gunichar ucs4) {
31   return (ucs4 >> 16) ? '?' : (ucs4 & 0x0000ffff);
32 }
33 } // anonymous namespace
34
35
36 CharSizeCache::CharSizeCache() { initialize(); }
37
38 void CharSizeCache::initialize() {
39   for (size_t i = 0; i != ArrayType::size(); ++i) {
40     char_width_cache[i] = -1;
41     char_height_cache[i] = -1;
42     bold_char_width_cache[i] = -1;
43     bold_char_height_cache[i] = -1;
44   }
45 }
46
47 void CharSizeCache::get_char_size(gunichar uch,
48     Glib::RefPtr<Pango::Layout>& layout,
49     double& width, double& height, bool bold) {
50
51   const gunichar ucs2 = ucs4_to_ucs2(uch);
52
53   int w = get_width_cache_array(bold)[ucs2];
54   int h = get_height_cache_array(bold)[ucs2];
55
56   if (w == -1 || h == -1) {
57     Glib::ustring ch(1, ucs2);
58     layout->set_text(ch);
59     layout->get_size(w, h);
60     get_width_cache_array(bold)[ucs2] = w;
61     get_height_cache_array(bold)[ucs2] = h;
62   }
63
64   width = double(w) / Pango::SCALE;
65   height = double(h) / Pango::SCALE;
66 }
67
68 void CharSizeCache::get_char_size_from_cache(gunichar uch,
69     double& width, double& height, bool bold) const {
70
71   const gunichar ucs2 = ucs4_to_ucs2(uch);
72   width = double(get_width_cache_array(bold)[ucs2]) / Pango::SCALE;
73   height = double(get_height_cache_array(bold)[ucs2]) / Pango::SCALE;
74 }
75
76
77 } // namespace text_element
78
79 } // namespace dialektos