LibreOffice Module vcl (master) 1
LogicalFontInstance.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <hb-ot.h>
23#include <hb-graphite2.h>
24
27#include <impfontcache.hxx>
28
30 const vcl::font::FontSelectPattern& rFontSelData)
31 : mxFontMetric(new ImplFontMetricData(rFontSelData))
32 , mpConversion(nullptr)
33 , mnLineHeight(0)
34 , mnOwnOrientation(0)
35 , mnOrientation(0)
36 , mbInit(false)
37 , mpFontCache(nullptr)
38 , m_aFontSelData(rFontSelData)
39 , m_pHbFont(nullptr)
40 , m_nAveWidthFactor(1.0f)
41 , m_pFontFace(&const_cast<vcl::font::PhysicalFontFace&>(rFontFace))
42{
43}
44
46{
48 mpFontCache = nullptr;
49 mxFontMetric = nullptr;
50
51 if (m_pHbFont)
52 hb_font_destroy(m_pHbFont);
53
55 hb_font_destroy(m_pHbFontUntransformed);
56
57#if HB_VERSION_ATLEAST(4, 0, 0)
58 if (m_pHbDrawFuncs)
59 hb_draw_funcs_destroy(m_pHbDrawFuncs);
60#endif
61}
62
64{
65 auto pFace = GetFontFace();
66 hb_face_t* pHbFace = pFace->GetHbFace();
67 assert(pHbFace);
68 auto nUPEM = pFace->UnitsPerEm();
69
70 hb_font_t* pHbFont = hb_font_create(pHbFace);
71 hb_font_set_scale(pHbFont, nUPEM, nUPEM);
72 hb_ot_font_set_funcs(pHbFont);
73
74 auto aVariations = pFace->GetVariations(*this);
75 if (!aVariations.empty())
76 hb_font_set_variations(pHbFont, aVariations.data(), aVariations.size());
77
78#if HB_VERSION_ATLEAST(3, 3, 0)
79 // If we are applying artificial italic, instruct HarfBuzz to do the same
80 // so that mark positioning is also transformed.
82 hb_font_set_synthetic_slant(pHbFont, ARTIFICIAL_ITALIC_SKEW);
83#endif
84
85 ImplInitHbFont(pHbFont);
86
87 return pHbFont;
88}
89
91{
92 auto* pHbFont = const_cast<LogicalFontInstance*>(this)->GetHbFont();
93
94#if HB_VERSION_ATLEAST(3, 3, 0)
95 if (NeedsArtificialItalic()) // || NeedsArtificialBold()
96 {
98 {
99 m_pHbFontUntransformed = hb_font_create_sub_font(pHbFont);
100 // Unset slant set on parent font.
101 // Does not actually work: https://github.com/harfbuzz/harfbuzz/issues/3890
102 hb_font_set_synthetic_slant(m_pHbFontUntransformed, 0);
103 }
105 }
106#endif
107 return pHbFont;
108}
109
111{
112 sal_GlyphId nGlyph = GetGlyphIndex(0x0640);
113 if (nGlyph)
114 return std::ceil(GetGlyphWidth(nGlyph));
115 return 0;
116}
117
118void LogicalFontInstance::GetScale(double* nXScale, double* nYScale) const
119{
120 double nUPEM = GetFontFace()->UnitsPerEm();
121 double nHeight(m_aFontSelData.mnHeight);
122
123 // On Windows, mnWidth is relative to average char width not font height,
124 // and we need to keep it that way for GDI to correctly scale the glyphs.
125 // Here we compensate for this so that HarfBuzz gives us the correct glyph
126 // positions.
128
129 if (nYScale)
130 *nYScale = nHeight / nUPEM;
131
132 if (nXScale)
133 *nXScale = nWidth / nUPEM;
134}
135
137 const OUString& rFontName, bool bEmbolden,
138 const ItalicMatrix& rMatrix)
139{
140 MapEntry& rEntry = maUnicodeFallbackList[std::pair<sal_UCS4, FontWeight>(cChar, eWeight)];
141 rEntry.sFontName = rFontName;
142 rEntry.bEmbolden = bEmbolden;
143 rEntry.aItalicMatrix = rMatrix;
144}
145
147 OUString* pFontName, bool* pEmbolden,
148 ItalicMatrix* pMatrix) const
149{
150 UnicodeFallbackList::const_iterator it
151 = maUnicodeFallbackList.find(std::pair<sal_UCS4, FontWeight>(cChar, eWeight));
152 if (it == maUnicodeFallbackList.end())
153 return false;
154
155 const MapEntry& rEntry = (*it).second;
156 *pFontName = rEntry.sFontName;
157 *pEmbolden = rEntry.bEmbolden;
158 *pMatrix = rEntry.aItalicMatrix;
159 return true;
160}
161
163 std::u16string_view rFontName)
164{
165 UnicodeFallbackList::iterator it
166 = maUnicodeFallbackList.find(std::pair<sal_UCS4, FontWeight>(cChar, eWeight));
167 if (it == maUnicodeFallbackList.end())
168 return;
169 const MapEntry& rEntry = (*it).second;
170 if (rEntry.sFontName == rFontName)
171 maUnicodeFallbackList.erase(it);
172}
173
175 bool bVertical) const
176{
177 if (mpFontCache && mpFontCache->GetCachedGlyphBoundRect(this, nID, rRect))
178 return true;
179
180 bool res = ImplGetGlyphBoundRect(nID, rRect, bVertical);
181 if (mpFontCache && res)
182 mpFontCache->CacheGlyphBoundRect(this, nID, rRect);
183 return res;
184}
185
186sal_GlyphId LogicalFontInstance::GetGlyphIndex(uint32_t nUnicode, uint32_t nVariationSelector) const
187{
188 auto* pHbFont = const_cast<LogicalFontInstance*>(this)->GetHbFont();
189 sal_GlyphId nGlyph = 0;
190 if (hb_font_get_glyph(pHbFont, nUnicode, nVariationSelector, &nGlyph))
191 return nGlyph;
192 return 0;
193}
194
195double LogicalFontInstance::GetGlyphWidth(sal_GlyphId nGlyph, bool bVertical, bool bScale) const
196{
197 auto* pHbFont = const_cast<LogicalFontInstance*>(this)->GetHbFont();
198 int nWidth;
199 if (bVertical)
200 nWidth = hb_font_get_glyph_v_advance(pHbFont, nGlyph);
201 else
202 nWidth = hb_font_get_glyph_h_advance(pHbFont, nGlyph);
203
204 if (!bScale)
205 return nWidth;
206
207 double nScale = 0;
208 GetScale(&nScale, nullptr);
209 return double(nWidth * nScale);
210}
211
213{
215 {
217 = hb_graphite2_face_get_gr_face(hb_font_get_face(GetHbFont())) != nullptr;
218 }
219 return *m_xbIsGraphiteFont;
220}
221
223{
225 {
227
228 // DFKai-SB (ukai.ttf) is a built-in font under traditional Chinese
229 // Windows. It has wrong extent values in glyf table. The problem results
230 // in wrong positioning of glyphs in vertical writing.
231 // Check https://github.com/harfbuzz/harfbuzz/issues/3521 for reference.
234 }
235
236 bool bRet = true;
237
238 switch (*m_xeFontFamilyEnum)
239 {
241 // -839: optimization for one third of ukai.ttf
242 if (nYOffset == -839)
243 bRet = false;
244 break;
245 default:
246 bRet = false;
247 }
248
249 return bRet;
250}
251
253{
254 return m_aFontSelData.GetWeight() > WEIGHT_MEDIUM && m_pFontFace->GetWeight() <= WEIGHT_MEDIUM;
255}
256
258{
259 return m_aFontSelData.GetItalic() != ITALIC_NONE && m_pFontFace->GetItalic() == ITALIC_NONE;
260}
261
262#if HB_VERSION_ATLEAST(4, 0, 0)
263namespace
264{
265void move_to_func(hb_draw_funcs_t*, void* /*pDrawData*/, hb_draw_state_t*, float to_x, float to_y,
266 void* pUserData)
267{
268 auto pPoly = static_cast<basegfx::B2DPolygon*>(pUserData);
269 pPoly->append(basegfx::B2DPoint(to_x, -to_y));
270}
271
272void line_to_func(hb_draw_funcs_t*, void* /*pDrawData*/, hb_draw_state_t*, float to_x, float to_y,
273 void* pUserData)
274{
275 auto pPoly = static_cast<basegfx::B2DPolygon*>(pUserData);
276 pPoly->append(basegfx::B2DPoint(to_x, -to_y));
277}
278
279void cubic_to_func(hb_draw_funcs_t*, void* /*pDrawData*/, hb_draw_state_t*, float control1_x,
280 float control1_y, float control2_x, float control2_y, float to_x, float to_y,
281 void* pUserData)
282{
283 auto pPoly = static_cast<basegfx::B2DPolygon*>(pUserData);
284 pPoly->appendBezierSegment(basegfx::B2DPoint(control1_x, -control1_y),
285 basegfx::B2DPoint(control2_x, -control2_y),
286 basegfx::B2DPoint(to_x, -to_y));
287}
288
289void close_path_func(hb_draw_funcs_t*, void* pDrawData, hb_draw_state_t*, void* pUserData)
290{
291 auto pPolyPoly = static_cast<basegfx::B2DPolyPolygon*>(pDrawData);
292 auto pPoly = static_cast<basegfx::B2DPolygon*>(pUserData);
293 pPolyPoly->append(*pPoly);
294 pPoly->clear();
295}
296}
297#endif
298
300 basegfx::B2DPolyPolygon& rPolyPoly) const
301{
302#if HB_VERSION_ATLEAST(4, 0, 0)
303 if (!m_pHbDrawFuncs)
304 {
305 m_pHbDrawFuncs = hb_draw_funcs_create();
306 auto pUserData = const_cast<basegfx::B2DPolygon*>(&m_aDrawPolygon);
307 hb_draw_funcs_set_move_to_func(m_pHbDrawFuncs, move_to_func, pUserData, nullptr);
308 hb_draw_funcs_set_line_to_func(m_pHbDrawFuncs, line_to_func, pUserData, nullptr);
309 hb_draw_funcs_set_cubic_to_func(m_pHbDrawFuncs, cubic_to_func, pUserData, nullptr);
310 // B2DPolyPolygon does not support quadratic curves, HarfBuzz will
311 // convert them to cubic curves for us if we don’t set a callback
312 // function.
313 //hb_draw_funcs_set_quadratic_to_func(m_pHbDrawFuncs, quadratic_to_func, pUserData, nullptr);
314 hb_draw_funcs_set_close_path_func(m_pHbDrawFuncs, close_path_func, pUserData, nullptr);
315 }
316
317 hb_font_get_glyph_shape(GetHbFontUntransformed(), nGlyph, m_pHbDrawFuncs, &rPolyPoly);
318 return true;
319#else
320 return false;
321#endif
322}
323
324/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr float ARTIFICIAL_ITALIC_SKEW
FontItalic GetItalic() const
FontWeight GetWeight() const
bool GetCachedGlyphBoundRect(const LogicalFontInstance *, sal_GlyphId, tools::Rectangle &)
Definition: fontcache.cxx:251
void CacheGlyphBoundRect(const LogicalFontInstance *, sal_GlyphId, tools::Rectangle &)
Definition: fontcache.cxx:268
std::optional< bool > m_xbIsGraphiteFont
void GetScale(double *nXScale, double *nYScale) const
const vcl::font::PhysicalFontFace * GetFontFace() const
LogicalFontInstance(const vcl::font::PhysicalFontFace &, const vcl::font::FontSelectPattern &)
void AddFallbackForUnicode(sal_UCS4 cChar, FontWeight eWeight, const OUString &rFontName, bool bEmbolden, const ItalicMatrix &rMatrix)
double GetGlyphWidth(sal_GlyphId, bool=false, bool=true) const
bool GetGlyphBoundRect(sal_GlyphId, tools::Rectangle &, bool) const
ImplFontMetricDataRef mxFontMetric
const vcl::font::FontSelectPattern m_aFontSelData
virtual void ImplInitHbFont(hb_font_t *)
bool GetFallbackForUnicode(sal_UCS4 cInChar, FontWeight eInWeight, OUString *pOutFontName, bool *pOutEmbolden, ItalicMatrix *pOutItalicMatrix) const
bool NeedsArtificialItalic() const
virtual bool ImplGetGlyphBoundRect(sal_GlyphId, tools::Rectangle &, bool) const =0
rtl::Reference< vcl::font::PhysicalFontFace > m_pFontFace
std::optional< FontFamilyEnum > m_xeFontFamilyEnum
bool NeedOffsetCorrection(sal_Int32 nYOffset)
void IgnoreFallbackForUnicode(sal_UCS4, FontWeight eWeight, std::u16string_view rFontName)
bool GetGlyphOutlineUntransformed(sal_GlyphId, basegfx::B2DPolyPolygon &) const
UnicodeFallbackList maUnicodeFallbackList
virtual ~LogicalFontInstance() override
hb_font_t * GetHbFontUntransformed() const
sal_GlyphId GetGlyphIndex(uint32_t, uint32_t=0) const
void append(const basegfx::B2DPoint &rPoint, sal_uInt32 nCount)
void appendBezierSegment(const basegfx::B2DPoint &rNextControlPoint, const basegfx::B2DPoint &rPrevControlPoint, const basegfx::B2DPoint &rPoint)
abstract base class for physical font faces
virtual OUString GetName() const override
ITALIC_NONE
WEIGHT_MEDIUM
uint32_t sal_GlyphId
Definition: glyphid.hxx:24
FontWeight
sal_uInt32 sal_UCS4
Definition: vclenum.hxx:172