LibreOffice Module vcl (master)  1
winlayout.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 #ifndef INCLUDED_VCL_INC_WIN_WINLAYOUT_HXX
21 #define INCLUDED_VCL_INC_WIN_WINLAYOUT_HXX
22 
23 #include <rtl/ustring.hxx>
24 
25 #include <sallayout.hxx>
26 #include <svsys.h>
27 #include <win/salgdi.h>
28 
30 
31 class WinFontInstance;
32 
33 namespace
34 {
35 // Extra space at the top and bottom of the glyph in total = tmHeight / GLYPH_SPACE_RATIO;
36 const int GLYPH_SPACE_RATIO = 8;
37 // Border size at the top of the glyph = tmHeight / GLYPH_OFFSET_RATIO;
38 const int GLYPH_OFFSET_RATIO = GLYPH_SPACE_RATIO * 2;
39 }
40 
42 {
47  int mnHeight;
48  bool mbVertical;
49 
50  int getExtraSpace() const
51  {
52  return std::max(mnHeight / GLYPH_SPACE_RATIO, 4);
53  }
54 
55  int getExtraOffset() const
56  {
57  return std::max(mnHeight / GLYPH_OFFSET_RATIO, 2);
58  }
59 };
60 
61 class OpenGLGlyphCache;
62 
64 {
66  : maPackedTextureAtlas(2048, 2048)
67  {}
68 
70  std::unordered_set<OpenGLGlyphCache*> maOpenGLGlyphCaches;
71 
72  static GlobalOpenGLGlyphCache * get();
73 };
74 
76 {
77 private:
78  std::unordered_map<int, OpenGLGlyphDrawElement> maOpenGLTextureCache;
79 
80 public:
82  {
84  }
85 
87  {
89  }
90 
91  static bool ReserveTextureSpace(OpenGLGlyphDrawElement& rElement, int nWidth, int nHeight)
92  {
93  GlobalOpenGLGlyphCache* pGlobalOpenGLGlyphCache = GlobalOpenGLGlyphCache::get();
94  rElement.maTexture = pGlobalOpenGLGlyphCache->maPackedTextureAtlas.Reserve(nWidth, nHeight);
95  if (!rElement.maTexture)
96  return false;
97  std::vector<GLuint> aTextureIDs = pGlobalOpenGLGlyphCache->maPackedTextureAtlas.ReduceTextureNumber(8);
98  if (!aTextureIDs.empty())
99  {
100  for (auto& pOpenGLGlyphCache: pGlobalOpenGLGlyphCache->maOpenGLGlyphCaches)
101  {
102  pOpenGLGlyphCache->RemoveTextures(aTextureIDs);
103  }
104  }
105  return true;
106  }
107 
108  void RemoveTextures(std::vector<GLuint>& rTextureIDs)
109  {
110  auto it = maOpenGLTextureCache.begin();
111 
112  while (it != maOpenGLTextureCache.end())
113  {
114  GLuint nTextureID = it->second.maTexture.Id();
115 
116  if (std::find(rTextureIDs.begin(), rTextureIDs.end(), nTextureID) != rTextureIDs.end())
117  {
118  it = maOpenGLTextureCache.erase(it);
119  }
120  else
121  {
122  ++it;
123  }
124  }
125  }
126 
127  void PutDrawElementInCache(const OpenGLGlyphDrawElement& rElement, int nGlyphIndex)
128  {
129  assert(!IsGlyphCached(nGlyphIndex));
130  maOpenGLTextureCache[nGlyphIndex] = rElement;
131  }
132 
134  {
135  assert(IsGlyphCached(nGlyphIndex));
136  return maOpenGLTextureCache[nGlyphIndex];
137  }
138 
139  bool IsGlyphCached(int nGlyphIndex) const
140  {
141  return maOpenGLTextureCache.find(nGlyphIndex) != maOpenGLTextureCache.end();
142  }
143 };
144 
145 // win32 specific logical font instance
147 {
149 
150 public:
151  ~WinFontInstance() override;
152 
153  bool hasHScale() const;
154  float getHScale() const;
155 
158 
159  HFONT GetHFONT() const { return m_hFont; }
160  float GetScale() const { return m_fScale; }
161 
162  // Prevent deletion of the HFONT in the WinFontInstance destructor
163  // Used for the ScopedFont handling
164  void SetHFONT(HFONT hFont) { m_hFont = hFont; }
165 
166  const WinFontFace * GetFontFace() const { return static_cast<const WinFontFace *>(LogicalFontInstance::GetFontFace()); }
167 
168  bool CacheGlyphToAtlas(HDC hDC, HFONT hFont, int nGlyphIndex, SalGraphics& rGraphics, const GenericSalLayout& rLayout);
170 
171  bool GetGlyphOutline(sal_GlyphId, basegfx::B2DPolyPolygon&, bool) const override;
172 
173 private:
174  explicit WinFontInstance(const WinFontFace&, const FontSelectPattern&);
175 
176  hb_font_t* ImplInitHbFont() override;
177  bool ImplGetGlyphBoundRect(sal_GlyphId, tools::Rectangle&, bool) const override;
178 
180  HFONT m_hFont;
181  float m_fScale;
183 };
184 
186 {
187 protected:
188  explicit TextOutRenderer() = default;
189  TextOutRenderer(const TextOutRenderer &) = delete;
190  TextOutRenderer & operator = (const TextOutRenderer &) = delete;
191 
192 public:
193  static TextOutRenderer & get(bool bUseDWrite);
194 
195  virtual ~TextOutRenderer() = default;
196 
197  virtual bool operator ()(GenericSalLayout const &rLayout,
198  SalGraphics &rGraphics,
199  HDC hDC) = 0;
200 };
201 
203 {
204  ExTextOutRenderer(const ExTextOutRenderer &) = delete;
205  ExTextOutRenderer & operator = (const ExTextOutRenderer &) = delete;
206 
207 public:
208  explicit ExTextOutRenderer() = default;
209 
210  bool operator ()(GenericSalLayout const &rLayout,
211  SalGraphics &rGraphics,
212  HDC hDC) override;
213 };
214 
215 #endif // INCLUDED_VCL_INC_WIN_WINLAYOUT_HXX
216 
217 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool CacheGlyphToAtlas(HDC hDC, HFONT hFont, int nGlyphIndex, SalGraphics &rGraphics, const GenericSalLayout &rLayout)
void SetHFONT(HFONT hFont)
Definition: winlayout.hxx:164
int getExtraOffset() const
Definition: winlayout.hxx:55
virtual ~TextOutRenderer()=default
WinSalGraphics * GetGraphics() const
Definition: winlayout.hxx:157
bool ImplGetGlyphBoundRect(sal_GlyphId, tools::Rectangle &, bool) const override
Pack textures into one texture atlas.
std::unordered_set< OpenGLGlyphCache * > maOpenGLGlyphCaches
Definition: winlayout.hxx:70
bool hasHScale() const
TextOutRenderer & operator=(const TextOutRenderer &)=delete
sal_uInt16 sal_GlyphId
Definition: glyphitem.hxx:26
PackedTextureAtlasManager maPackedTextureAtlas
Definition: winlayout.hxx:69
TextOutRenderer()=default
virtual bool operator()(GenericSalLayout const &rLayout, SalGraphics &rGraphics, HDC hDC)=0
bool GetGlyphOutline(sal_GlyphId, basegfx::B2DPolyPolygon &, bool) const override
~WinFontInstance() override
bool IsGlyphCached(int nGlyphIndex) const
Definition: winlayout.hxx:139
void RemoveTextures(std::vector< GLuint > &rTextureIDs)
Definition: winlayout.hxx:108
tools::Rectangle maLocation
Definition: winlayout.hxx:43
OpenGLGlyphCache maOpenGLGlyphCache
Definition: winlayout.hxx:182
int getExtraSpace() const
Definition: winlayout.hxx:50
virtual rtl::Reference< LogicalFontInstance > CreateFontInstance(const FontSelectPattern &) const override
bool operator()(GenericSalLayout const &rLayout, SalGraphics &rGraphics, HDC hDC) override
void PutDrawElementInCache(const OpenGLGlyphDrawElement &rElement, int nGlyphIndex)
Definition: winlayout.hxx:127
void SetGraphics(WinSalGraphics *)
OpenGLGlyphDrawElement & GetDrawElement(int nGlyphIndex)
Definition: winlayout.hxx:133
WinFontInstance(const WinFontFace &, const FontSelectPattern &)
float getHScale() const
const WinFontFace * GetFontFace() const
Definition: winlayout.hxx:166
OpenGLTexture Reserve(int nWidth, int nHeight)
ExTextOutRenderer()=default
OpenGLTexture maTexture
Definition: winlayout.hxx:45
ExTextOutRenderer & operator=(const ExTextOutRenderer &)=delete
OpenGLGlyphCache & GetOpenGLGlyphCache()
Definition: winlayout.hxx:169
WinSalGraphics * m_pGraphics
Definition: winlayout.hxx:179
const PhysicalFontFace * GetFontFace() const
std::unordered_map< int, OpenGLGlyphDrawElement > maOpenGLTextureCache
Definition: winlayout.hxx:78
HFONT GetHFONT() const
Definition: winlayout.hxx:159
hb_font_t * ImplInitHbFont() override
static bool ReserveTextureSpace(OpenGLGlyphDrawElement &rElement, int nWidth, int nHeight)
Definition: winlayout.hxx:91
std::vector< GLuint > ReduceTextureNumber(int nMaxNumberOfTextures)
float GetScale() const
Definition: winlayout.hxx:160
static GlobalOpenGLGlyphCache * get()