2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Helge Norberg, helge.norberg@svt.se
22 #include "freetype_library.h"
24 #include <boost/thread/tss.hpp>
26 namespace caspar { namespace core { namespace text {
28 spl::shared_ptr<std::remove_pointer<FT_Library>::type> get_lib_for_thread()
30 typedef std::remove_pointer<FT_Library>::type non_ptr_type;
31 static boost::thread_specific_ptr<spl::shared_ptr<non_ptr_type>> libs;
33 auto result = libs.get();
39 if (FT_Init_FreeType(&raw_lib))
40 CASPAR_THROW_EXCEPTION(freetype_exception() << msg_info("Failed to initialize freetype"));
42 auto lib = spl::shared_ptr<non_ptr_type>(raw_lib, FT_Done_FreeType);
43 result = new spl::shared_ptr<non_ptr_type>(std::move(lib));
51 spl::shared_ptr<std::remove_pointer<FT_Face>::type> get_new_face(
52 const std::string& font_file, const std::string& font_name)
54 if (font_file.empty())
55 CASPAR_THROW_EXCEPTION(expected_freetype_exception() << msg_info("Failed to find font file for \"" + font_name + "\""));
57 auto lib = get_lib_for_thread();
59 if (FT_New_Face(lib.get(), u8(font_file).c_str(), 0, &face))
60 CASPAR_THROW_EXCEPTION(freetype_exception() << msg_info("Failed to load font file \"" + font_file + "\""));
62 return spl::shared_ptr<std::remove_pointer<FT_Face>::type>(face, [lib](FT_Face p)