2 * Copyright (C) 2009 by Aiwota Programmer
3 * aiwotaprog@tetteke.tk
5 * This file is part of Dialektos.
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.
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.
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/>.
21 #include "text_element_char_size_cache.hxx"
26 namespace text_element {
30 inline gunichar ucs4_to_ucs2(const gunichar ucs4) {
31 return (ucs4 >> 16) ? '?' : (ucs4 & 0x0000ffff);
33 } // anonymous namespace
36 CharSizeCache::CharSizeCache() { initialize(); }
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;
47 void CharSizeCache::get_char_size(gunichar uch,
48 Glib::RefPtr<Pango::Layout>& layout,
49 double& width, double& height, bool bold) {
51 const gunichar ucs2 = ucs4_to_ucs2(uch);
53 int w = get_width_cache_array(bold)[ucs2];
54 int h = get_height_cache_array(bold)[ucs2];
56 if (w == -1 || h == -1) {
57 Glib::ustring ch(1, ucs2);
59 layout->get_size(w, h);
60 get_width_cache_array(bold)[ucs2] = w;
61 get_height_cache_array(bold)[ucs2] = h;
64 width = double(w) / Pango::SCALE;
65 height = double(h) / Pango::SCALE;
68 void CharSizeCache::get_char_size_from_cache(gunichar uch,
69 double& width, double& height, bool bold) const {
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;
77 } // namespace text_element
79 } // namespace dialektos