bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / font / fontinstance.cxx
blob40e334bd6d5c0a8f2d39ea666519cc54a1b9b5c2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <hb-ot.h>
22 #include <hb-graphite2.h>
24 #include <fontinstance.hxx>
25 #include <impfontcache.hxx>
27 #include <PhysicalFontFace.hxx>
29 // extend std namespace to add custom hash needed for LogicalFontInstance
31 namespace std
33 template <> struct hash< pair< sal_UCS4, FontWeight > >
35 size_t operator()(const pair< sal_UCS4, FontWeight >& rData) const
37 std::size_t seed = 0;
38 boost::hash_combine(seed, rData.first);
39 boost::hash_combine(seed, rData.second);
40 return seed;
46 LogicalFontInstance::LogicalFontInstance(const PhysicalFontFace& rFontFace, const FontSelectPattern& rFontSelData )
47 : mxFontMetric( new ImplFontMetricData( rFontSelData ))
48 , mpConversion( nullptr )
49 , mnLineHeight( 0 )
50 , mnOwnOrientation( 0 )
51 , mnOrientation( 0 )
52 , mbInit( false )
53 , mpFontCache( nullptr )
54 , m_aFontSelData(rFontSelData)
55 , m_pHbFont(nullptr)
56 , m_nAveWidthFactor(1.0f)
57 , m_pFontFace(&const_cast<PhysicalFontFace&>(rFontFace))
61 LogicalFontInstance::~LogicalFontInstance()
63 mpUnicodeFallbackList.reset();
64 mpFontCache = nullptr;
65 mxFontMetric = nullptr;
67 if (m_pHbFont)
68 hb_font_destroy(m_pHbFont);
71 hb_font_t* LogicalFontInstance::InitHbFont(hb_face_t* pHbFace)
73 assert(pHbFace);
74 hb_font_t* pHbFont = hb_font_create(pHbFace);
75 unsigned int nUPEM = hb_face_get_upem(pHbFace);
76 hb_font_set_scale(pHbFont, nUPEM, nUPEM);
77 hb_ot_font_set_funcs(pHbFont);
78 // hb_font_t keeps a reference to hb_face_t, so destroy this one.
79 hb_face_destroy(pHbFace);
80 return pHbFont;
83 int LogicalFontInstance::GetKashidaWidth()
85 hb_font_t* pHbFont = GetHbFont();
86 hb_position_t nWidth = 0;
87 hb_codepoint_t nIndex = 0;
89 if (hb_font_get_glyph(pHbFont, 0x0640, 0, &nIndex))
91 double nXScale = 0;
92 GetScale(&nXScale, nullptr);
93 nWidth = hb_font_get_glyph_h_advance(pHbFont, nIndex) * nXScale;
96 return nWidth;
99 void LogicalFontInstance::GetScale(double* nXScale, double* nYScale)
101 hb_face_t* pHbFace = hb_font_get_face(GetHbFont());
102 unsigned int nUPEM = hb_face_get_upem(pHbFace);
104 double nHeight(m_aFontSelData.mnHeight);
106 // On Windows, mnWidth is relative to average char width not font height,
107 // and we need to keep it that way for GDI to correctly scale the glyphs.
108 // Here we compensate for this so that HarfBuzz gives us the correct glyph
109 // positions.
110 double nWidth(m_aFontSelData.mnWidth ? m_aFontSelData.mnWidth * m_nAveWidthFactor : nHeight);
112 if (nYScale)
113 *nYScale = nHeight / nUPEM;
115 if (nXScale)
116 *nXScale = nWidth / nUPEM;
119 void LogicalFontInstance::AddFallbackForUnicode( sal_UCS4 cChar, FontWeight eWeight, const OUString& rFontName )
121 if( !mpUnicodeFallbackList )
122 mpUnicodeFallbackList.reset(new UnicodeFallbackList);
123 (*mpUnicodeFallbackList)[ std::pair< sal_UCS4, FontWeight >(cChar,eWeight) ] = rFontName;
126 bool LogicalFontInstance::GetFallbackForUnicode( sal_UCS4 cChar, FontWeight eWeight, OUString* pFontName ) const
128 if( !mpUnicodeFallbackList )
129 return false;
131 UnicodeFallbackList::const_iterator it = mpUnicodeFallbackList->find( std::pair< sal_UCS4, FontWeight >(cChar,eWeight) );
132 if( it == mpUnicodeFallbackList->end() )
133 return false;
135 *pFontName = (*it).second;
136 return true;
139 void LogicalFontInstance::IgnoreFallbackForUnicode( sal_UCS4 cChar, FontWeight eWeight, const OUString& rFontName )
141 UnicodeFallbackList::iterator it = mpUnicodeFallbackList->find( std::pair< sal_UCS4,FontWeight >(cChar,eWeight) );
142 if( it == mpUnicodeFallbackList->end() )
143 return;
144 if( (*it).second == rFontName )
145 mpUnicodeFallbackList->erase( it );
148 bool LogicalFontInstance::GetGlyphBoundRect(sal_GlyphId nID, tools::Rectangle &rRect, bool bVertical) const
150 if (mpFontCache && mpFontCache->GetCachedGlyphBoundRect(this, nID, rRect))
151 return true;
153 bool res = ImplGetGlyphBoundRect(nID, rRect, bVertical);
154 if (mpFontCache && res)
155 mpFontCache->CacheGlyphBoundRect(this, nID, rRect);
156 return res;
159 bool LogicalFontInstance::IsGraphiteFont()
161 if (!m_xbIsGraphiteFont)
163 m_xbIsGraphiteFont = hb_graphite2_face_get_gr_face(hb_font_get_face(GetHbFont())) != nullptr;
165 return *m_xbIsGraphiteFont;
168 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */