2 * Copyright (C) 2014-2016 Paul Davis <paul@linuxaudiosystems.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <pangomm/layout.h>
23 #include "gtkmm2ext/emscale.h"
27 using namespace Gtkmm2ext;
29 std::map<std::string,EmScale> EmScale::_emscales;
31 EmScale::EmScale (const Pango::FontDescription& fd)
33 , _char_pixel_width (-1)
34 , _char_pixel_height (-1)
35 , _char_avg_pixel_width (-1)
40 EmScale::recalc_char_pixel_geometry ()
42 if (_char_pixel_height > 0 && _char_pixel_width > 0) {
46 Glib::RefPtr<Pango::Context> pc = Glib::wrap (gdk_pango_context_get_for_screen (gdk_screen_get_default()));
47 Glib::RefPtr<Pango::Layout> layout = Pango::Layout::create (pc);
49 layout->set_font_description (_font);
52 std::string x = _("ABCDEFGHIJLKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
54 layout->get_pixel_size (w, h);
55 _char_pixel_height = std::max(4, h);
56 // number of actual chars in the string (not bytes)
57 // Glib to the rescue.
59 _char_avg_pixel_width = w / (float)gx.size();
60 _char_pixel_width = std::max(4, (int) ceil (_char_avg_pixel_width));
64 EmScale::by_font (const Pango::FontDescription& fd)
66 std::map<std::string,EmScale>::iterator i = _emscales.find (fd.to_string());
68 if (i == _emscales.end()) {
69 i = _emscales.insert (std::make_pair (fd.to_string(), EmScale (fd))).first;