1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
20 #ifndef INCLUDED_VCL_GLYPHITEMCACHE_HXX
21 #define INCLUDED_VCL_GLYPHITEMCACHE_HXX
23 #include <sal/types.h>
24 #include <vcl/dllapi.h>
26 #include <o3tl/lru_map.hxx>
27 #include <o3tl/hash_combine.hxx>
28 #include <vcl/glyphitem.hxx>
29 #include <vcl/metric.hxx>
30 #include <vcl/outdev.hxx>
31 #include <vcl/vclptr.hxx>
32 #include <tools/gen.hxx>
37 A cache for SalLayoutGlyphs objects.
39 Allows caching for OutputDevice::DrawText() and similar calls. Pass the text and the output device
40 for the call to OutputDevice::ImplLayout(). Items are cached per output device and its font.
41 If something more changes, call clear().
43 class VCL_DLLPUBLIC SalLayoutGlyphsCache final
46 // NOTE: The lifetime of the returned value is guaranteed only until the next call
47 // to any function in this class.
48 const SalLayoutGlyphs
* GetLayoutGlyphs(VclPtr
<const OutputDevice
> outputDevice
,
50 const vcl::text::TextLayoutCache
* layoutCache
= nullptr)
52 return GetLayoutGlyphs(outputDevice
, text
, 0, text
.getLength(), 0, layoutCache
);
54 const SalLayoutGlyphs
* GetLayoutGlyphs(VclPtr
<const OutputDevice
> outputDevice
,
55 const OUString
& text
, sal_Int32 nIndex
, sal_Int32 nLen
,
56 tools::Long nLogicWidth
= 0,
57 const vcl::text::TextLayoutCache
* layoutCache
= nullptr);
60 /// Normally, we cannot cache glyphs when doing font fallback, because the font fallbacks
61 /// can cache during the lifetime of the cache, and they are not included in the cache key.
62 /// But during some processes, we can turn this on, as long as we remember to turn it off
64 void SetCacheGlyphsWhenDoingFallbackFonts(bool bOK
);
66 static SalLayoutGlyphsCache
* self();
67 SalLayoutGlyphsCache(int size
) // needs to be public for vcl::DeleteOnDeinit
73 struct CachedGlyphsKey
78 tools::Long logicWidth
;
79 FontMetric fontMetric
;
84 bool disabledLigatures
; // because of fixed pitch
85 bool artificialItalic
;
87 vcl::text::ComplexTextLayoutFlags layoutMode
;
88 LanguageType digitLanguage
;
90 CachedGlyphsKey(const VclPtr
<const OutputDevice
>& dev
, OUString t
, sal_Int32 i
, sal_Int32 l
,
92 bool operator==(const CachedGlyphsKey
& other
) const;
94 struct CachedGlyphsHash
96 size_t operator()(const CachedGlyphsKey
& key
) const { return key
.hashValue
; }
100 size_t operator()(const SalLayoutGlyphs
&) const;
102 typedef o3tl::lru_map
<CachedGlyphsKey
, SalLayoutGlyphs
, CachedGlyphsHash
,
103 std::equal_to
<CachedGlyphsKey
>, GlyphsCost
>
105 GlyphsCache mCachedGlyphs
;
106 // Last temporary glyphs returned (pointer is returned, so the object needs to be kept somewhere).
107 std::optional
<CachedGlyphsKey
> mLastTemporaryKey
;
108 SalLayoutGlyphs mLastTemporaryGlyphs
;
109 // If set, info about the last call which wanted a substring of the full text.
110 std::optional
<CachedGlyphsKey
> mLastSubstringKey
;
111 bool mbCacheGlyphsWhenDoingFallbackFonts
= false;
113 SalLayoutGlyphsCache(const SalLayoutGlyphsCache
&) = delete;
114 SalLayoutGlyphsCache
& operator=(const SalLayoutGlyphsCache
&) = delete;
117 #endif // INCLUDED_VCL_GLYPHITEMCACHE_HXX
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */