LibreOffice Module vcl (master)  1
fontinstance.hxx
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 #pragma once
21 
22 #include <sal/config.h>
23 
25 #include <o3tl/hash_combine.hxx>
26 #include <rtl/ref.hxx>
28 #include <tools/gen.hxx>
29 #include <tools/fontenum.hxx>
30 #include <tools/degree.hxx>
31 
33 #include "impfontmetricdata.hxx"
34 #include "glyphid.hxx"
35 
36 #include <optional>
37 #include <unordered_map>
38 #include <memory>
39 
40 #include <hb.h>
41 
42 class ConvertChar;
43 class ImplFontCache;
44 
45 // extend std namespace to add custom hash needed for LogicalFontInstance
46 
47 namespace std
48 {
49  template <> struct hash< pair< sal_UCS4, FontWeight > >
50  {
51  size_t operator()(const pair< sal_UCS4, FontWeight >& rData) const
52  {
53  std::size_t seed = 0;
54  o3tl::hash_combine(seed, rData.first);
55  o3tl::hash_combine(seed, rData.second);
56  return seed;
57  }
58  };
59 }
60 
61 // TODO: allow sharing of metrics for related fonts
62 
64 {
65  // just declaring the factory function doesn't work AKA
66  // friend LogicalFontInstance* PhysicalFontFace::CreateFontInstance(const FontSelectPattern&) const;
68  friend class ImplFontCache;
69 
70 public: // TODO: make data members private
71  virtual ~LogicalFontInstance() override;
72 
73  ImplFontMetricDataRef mxFontMetric; // Font attributes
74  const ConvertChar* mpConversion; // used e.g. for StarBats->StarSymbol
75 
77  Degree10 mnOwnOrientation; // text angle if lower layers don't rotate text themselves
78  Degree10 mnOrientation; // text angle in 3600 system
79  bool mbInit; // true if maFontMetric member is valid
80 
81  void AddFallbackForUnicode(sal_UCS4 cChar, FontWeight eWeight, const OUString& rFontName,
82  bool bEmbolden, const ItalicMatrix& rMatrix);
83  bool GetFallbackForUnicode(sal_UCS4 cInChar, FontWeight eInWeight,
84  OUString* pOutFontName, bool* pOutEmbolden, ItalicMatrix* pOutItalicMatrix) const;
85  void IgnoreFallbackForUnicode( sal_UCS4, FontWeight eWeight, std::u16string_view rFontName );
86 
87  inline hb_font_t* GetHbFont();
88  bool IsGraphiteFont();
89  // NeedOffsetCorrection: Return if the font need offset correction in TTB direction.
90  // nYOffset is the original offset. It is used to check if the correction is necessary.
91  bool NeedOffsetCorrection(sal_Int32 nYOffset);
92  void SetAverageWidthFactor(double nFactor) { m_nAveWidthFactor = std::abs(nFactor); }
93  double GetAverageWidthFactor() const { return m_nAveWidthFactor; }
94  const vcl::font::FontSelectPattern& GetFontSelectPattern() const { return m_aFontSelData; }
95 
96  const vcl::font::PhysicalFontFace* GetFontFace() const { return m_pFontFace.get(); }
97  vcl::font::PhysicalFontFace* GetFontFace() { return m_pFontFace.get(); }
98  const ImplFontCache* GetFontCache() const { return mpFontCache; }
99 
100  bool GetGlyphBoundRect(sal_GlyphId, tools::Rectangle&, bool) const;
101  virtual bool GetGlyphOutline(sal_GlyphId, basegfx::B2DPolyPolygon&, bool) const = 0;
102 
103  int GetKashidaWidth() const;
104 
105  void GetScale(double* nXScale, double* nYScale) const;
106  static inline void DecodeOpenTypeTag(const uint32_t nTableTag, char* pTagName);
107 
108 protected:
110 
111  virtual bool ImplGetGlyphBoundRect(sal_GlyphId, tools::Rectangle&, bool) const = 0;
112 
113  // Takes ownership of pHbFace.
114  static hb_font_t* InitHbFont(hb_face_t* pHbFace);
115  virtual hb_font_t* ImplInitHbFont() { assert(false); return hb_font_get_empty(); }
116 
117 private:
118  struct MapEntry
119  {
120  OUString sFontName;
121  bool bEmbolden;
123  };
124  // cache of Unicode characters and replacement font names and attributes
125  // TODO: a fallback map can be shared with many other ImplFontEntries
126  // TODO: at least the ones which just differ in orientation, stretching or height
127  typedef ::std::unordered_map< ::std::pair<sal_UCS4,FontWeight>, MapEntry > UnicodeFallbackList;
128  UnicodeFallbackList maUnicodeFallbackList;
131  hb_font_t* m_pHbFont;
134  std::optional<bool> m_xbIsGraphiteFont;
135 
136  enum class FontFamilyEnum { Unclassified, DFKaiSB };
137 
138  // The value is initialized and used in NeedOffsetCorrection().
139  std::optional<FontFamilyEnum> m_xeFontFamilyEnum;
140 };
141 
143 {
144  if (!m_pHbFont)
146  return m_pHbFont;
147 }
148 
149 inline void LogicalFontInstance::DecodeOpenTypeTag(const uint32_t nTableTag, char* pTagName)
150 {
151  pTagName[0] = static_cast<char>(nTableTag >> 24);
152  pTagName[1] = static_cast<char>(nTableTag >> 16);
153  pTagName[2] = static_cast<char>(nTableTag >> 8);
154  pTagName[3] = static_cast<char>(nTableTag);
155  pTagName[4] = 0;
156 }
157 
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::optional< bool > m_xbIsGraphiteFont
tools::Long mnLineHeight
ImplFontCache * mpFontCache
sal_uInt32 sal_UCS4
Definition: vclenum.hxx:193
size_t operator()(const pair< sal_UCS4, FontWeight > &rData) const
void SetAverageWidthFactor(double nFactor)
const ConvertChar * mpConversion
vcl::font::PhysicalFontFace * GetFontFace()
long Long
::std::unordered_map< ::std::pair< sal_UCS4, FontWeight >, MapEntry > UnicodeFallbackList
std::optional< FontFamilyEnum > m_xeFontFamilyEnum
hb_font_t * GetHbFont()
static void DecodeOpenTypeTag(const uint32_t nTableTag, char *pTagName)
ImplFontMetricDataRef mxFontMetric
double GetAverageWidthFactor() const
sal_uInt16 sal_GlyphId
Definition: glyphid.hxx:24
const vcl::font::FontSelectPattern & GetFontSelectPattern() const
const vcl::font::FontSelectPattern m_aFontSelData
std::enable_if_t<(sizeof(N)==4)> hash_combine(N &nSeed, T const *pValue, size_t nCount)
abstract base class for physical font faces
#define VCL_PLUGIN_PUBLIC
Definition: dllapi.h:40
rtl::Reference< vcl::font::PhysicalFontFace > m_pFontFace
FontWeight
const ImplFontCache * GetFontCache() const
virtual hb_font_t * ImplInitHbFont()
UnicodeFallbackList maUnicodeFallbackList
const vcl::font::PhysicalFontFace * GetFontFace() const