1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 oriented thread wrappers for different thread implementations. Copyright (c) 2000-2006 Jori Liesenborgs (jori.liesenborgs@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "jmutex.h" JMutex::JMutex() { initialized = false; } JMutex::~JMutex() { if (initialized) pthread_mutex_destroy(&mutex); } int JMutex::Init() { if (initialized/* Minetest Copyright (C) 2010-2014 sapier <sapier at gmx dot net> This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #pragma once #include <map> #include "util/basic_macros.h" #include "irrlichttypes.h" #include <IGUIFont.h> #include <IGUISkin.h> #include <IGUIEnvironment.h> #include "settings.h" #include "threading/mutex_auto_lock.h" #define FONT_SIZE_UNSPECIFIED 0xFFFFFFFF enum FontMode : u8 { FM_Standard = 0, FM_Mono, _FM_Fallback, // do not use directly FM_MaxMode, FM_Unspecified }; struct FontSpec { FontSpec(unsigned int font_size, FontMode mode, bool bold, bool italic) : size(font_size), mode(mode), bold(bold), italic(italic) {} u16 getHash() const { return (mode << 2) | (static_cast<u8>(bold) << 1) | static_cast<u8>(italic); } unsigned int size; FontMode mode; bool bold; bool italic; }; class FontEngine { public: FontEngine(gui::IGUIEnvironment* env); ~FontEngine(); // Get best possible font specified by FontSpec irr::gui::IGUIFont *getFont(FontSpec spec); irr::gui::IGUIFont *getFont(unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified) { FontSpec spec(font_size, mode, m_default_bold, m_default_italic); return getFont(spec); } /** get text height for a specific font */ unsigned int getTextHeight(const FontSpec &spec); /** get text width if a text for a specific font */ unsigned int getTextHeight( unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified) { FontSpec spec(font_size, mode, m_default_bold, m_default_italic); return getTextHeight(spec); } unsigned int getTextWidth(const std::wstring &text, const FontSpec &spec); /** get text width if a text for a specific font */ unsigned int getTextWidth(const std::wstring& text, unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified) { FontSpec spec(font_size, mode, m_default_bold, m_default_italic); return getTextWidth(text, spec); } unsigned int getTextWidth(const std::string &text, const FontSpec &spec) { return getTextWidth(utf8_to_wide(text), spec); } unsigned int getTextWidth(const std::string& text, unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified) { FontSpec spec(font_size, mode, m_default_bold, m_default_italic); return getTextWidth(utf8_to_wide(text), spec); } /** get line height for a specific font (including empty room between lines) */ unsigned int getLineHeight(const FontSpec &spec); unsigned int getLineHeight(unsigned int font_size=FONT_SIZE_UNSPECIFIED, FontMode mode=FM_Unspecified) { FontSpec spec(font_size, mode, m_default_bold, m_default_italic); return getLineHeight(spec); } /** get default font size */ unsigned int getDefaultFontSize(); /** get font size for a specific mode */ unsigned int getFontSize(FontMode mode); /** update internal parameters from settings */ void readSettings(); private: irr::gui::IGUIFont *getFont(FontSpec spec, bool may_fail); /** update content of font cache in case of a setting change made it invalid */ void updateFontCache(); /** initialize a new TTF font */ gui::IGUIFont *initFont(const FontSpec &spec); /** update current minetest skin with font changes */ void updateSkin(); /** clean cache */ void cleanCache(); /** pointer to irrlicht gui environment */ gui::IGUIEnvironment* m_env = nullptr; /** mutex used to protect font init and cache */ std::recursive_mutex m_font_mutex; /** internal storage for caching fonts of different size */ std::map<unsigned int, irr::gui::IGUIFont*> m_font_cache[FM_MaxMode << 2]; /** default font size to use */ unsigned int m_default_size[FM_MaxMode]; /** default bold and italic */ bool m_default_bold = false; bool m_default_italic = false; /** default font engine mode (fixed) */ static const FontMode m_currentMode = FM_Standard; DISABLE_CLASS_COPY(FontEngine); }; /** interface to access main font engine*/ extern FontEngine* g_fontengine; |