]> git.sesse.net Git - casparcg/blob - core/producer/text/utils/freetype_library.cpp
[text_producer] Made it easier to see what fonts are missing when loading a PSD.
[casparcg] / core / producer / text / utils / freetype_library.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Helge Norberg, helge.norberg@svt.se
20 */
21
22 #include "freetype_library.h"
23
24 #include <boost/thread/tss.hpp>
25
26 namespace caspar { namespace core { namespace text {
27
28 spl::shared_ptr<std::remove_pointer<FT_Library>::type> get_lib_for_thread()
29 {
30         typedef std::remove_pointer<FT_Library>::type non_ptr_type;
31         static boost::thread_specific_ptr<spl::shared_ptr<non_ptr_type>> libs;
32
33         auto result = libs.get();
34
35         if (!result)
36         {
37                 FT_Library raw_lib;
38
39                 if (FT_Init_FreeType(&raw_lib))
40                         CASPAR_THROW_EXCEPTION(freetype_exception() << msg_info("Failed to initialize freetype"));
41
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));
44
45                 libs.reset(result);
46         }
47
48         return *result;
49 }
50
51 spl::shared_ptr<std::remove_pointer<FT_Face>::type> get_new_face(
52                 const std::string& font_file, const std::string& font_name)
53 {
54         if (font_file.empty())
55                 CASPAR_THROW_EXCEPTION(expected_freetype_exception() << msg_info("Failed to find font file for \"" + font_name + "\""));
56
57         auto lib = get_lib_for_thread();
58         FT_Face face;
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 + "\""));
61
62         return spl::shared_ptr<std::remove_pointer<FT_Face>::type>(face, [lib](FT_Face p)
63         {
64                 FT_Done_Face(p);
65         });
66 }
67
68 }}}