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 #include <sal/config.h>
22 #include <unx/freetypetextrender.hxx>
24 #include <unotools/configmgr.hxx>
25 #include <vcl/settings.hxx>
26 #include <vcl/sysdata.hxx>
27 #include <vcl/svapp.hxx>
28 #include <vcl/fontcharmap.hxx>
29 #include <sal/log.hxx>
31 #include <unx/fontmanager.hxx>
32 #include <unx/geninst.h>
33 #include <unx/glyphcache.hxx>
34 #include <unx/fc_fontoptions.hxx>
35 #include <unx/freetype_glyphcache.hxx>
36 #include <font/PhysicalFontFace.hxx>
37 #include <font/FontMetricData.hxx>
39 #include <sallayout.hxx>
41 FreeTypeTextRenderImpl::FreeTypeTextRenderImpl()
42 : mnTextColor(Color(0x00, 0x00, 0x00)) //black
46 FreeTypeTextRenderImpl::~FreeTypeTextRenderImpl()
51 void FreeTypeTextRenderImpl::SetFont(LogicalFontInstance
*pEntry
, int nFallbackLevel
)
53 // release all no longer needed font resources
54 for( int i
= nFallbackLevel
; i
< MAX_FALLBACK
; ++i
)
56 // old server side font is no longer referenced
57 mpFreetypeFont
[i
] = nullptr;
60 // return early if there is no new font
64 FreetypeFontInstance
* pFreetypeFont
= static_cast<FreetypeFontInstance
*>(pEntry
);
65 mpFreetypeFont
[ nFallbackLevel
] = pFreetypeFont
;
67 // ignore fonts with e.g. corrupted font files
68 if (!mpFreetypeFont
[nFallbackLevel
]->GetFreetypeFont().TestFont())
69 mpFreetypeFont
[nFallbackLevel
] = nullptr;
72 FontCharMapRef
FreeTypeTextRenderImpl::GetFontCharMap() const
74 if (!mpFreetypeFont
[0])
76 return mpFreetypeFont
[0]->GetFontFace()->GetFontCharMap();
79 bool FreeTypeTextRenderImpl::GetFontCapabilities(vcl::FontCapabilities
&rGetImplFontCapabilities
) const
81 if (!mpFreetypeFont
[0])
83 return mpFreetypeFont
[0]->GetFontFace()->GetFontCapabilities(rGetImplFontCapabilities
);
88 FreeTypeTextRenderImpl::SetTextColor( Color nColor
)
90 if( mnTextColor
!= nColor
)
96 bool FreeTypeTextRenderImpl::AddTempDevFont(vcl::font::PhysicalFontCollection
* pFontCollection
,
97 const OUString
& rFileURL
, const OUString
& rFontName
)
99 // inform PSP font manager
100 psp::PrintFontManager
& rMgr
= psp::PrintFontManager::get();
101 std::vector
<psp::fontID
> aFontIds
= rMgr
.addFontFile(rFileURL
);
102 if (aFontIds
.empty())
105 FreetypeManager
& rFreetypeManager
= FreetypeManager::get();
106 for (auto const& nFontId
: aFontIds
)
109 auto const* pFont
= rMgr
.getFont(nFontId
);
113 // inform glyph cache of new font
114 FontAttributes aDFA
= pFont
->m_aFontAttributes
;
115 aDFA
.IncreaseQualityBy(5800);
116 if (!rFontName
.isEmpty())
117 aDFA
.SetFamilyName(rFontName
);
119 int nFaceNum
= rMgr
.getFontFaceNumber(nFontId
);
120 int nVariantNum
= rMgr
.getFontFaceVariation(nFontId
);
122 const OString aFileName
= rMgr
.getFontFileSysPath(nFontId
);
123 rFreetypeManager
.AddFontFile(aFileName
, nFaceNum
, nVariantNum
, nFontId
, aDFA
);
126 // announce new font to device's font list
127 rFreetypeManager
.AnnounceFonts(pFontCollection
);
131 void FreeTypeTextRenderImpl::ClearDevFontCache()
133 FreetypeManager::get().ClearFontCache();
136 void FreeTypeTextRenderImpl::GetDevFontList(vcl::font::PhysicalFontCollection
* pFontCollection
)
138 // prepare the FreetypeManager using psprint's font infos
139 FreetypeManager
& rFreetypeManager
= FreetypeManager::get();
141 psp::PrintFontManager
& rMgr
= psp::PrintFontManager::get();
142 ::std::vector
<psp::fontID
> aList
;
143 rMgr
.getFontList(aList
);
144 for (auto const& nFontId
: aList
)
146 auto const* pFont
= rMgr
.getFont(nFontId
);
150 // normalize face number to the FreetypeManager
151 int nFaceNum
= rMgr
.getFontFaceNumber(nFontId
);
152 int nVariantNum
= rMgr
.getFontFaceVariation(nFontId
);
154 // inform FreetypeManager about this font provided by the PsPrint subsystem
155 FontAttributes aDFA
= pFont
->m_aFontAttributes
;
156 aDFA
.IncreaseQualityBy(4096);
157 const OString aFileName
= rMgr
.getFontFileSysPath(nFontId
);
158 rFreetypeManager
.AddFontFile(aFileName
, nFaceNum
, nVariantNum
, nFontId
, aDFA
);
161 // announce glyphcache fonts
162 rFreetypeManager
.AnnounceFonts(pFontCollection
);
164 // register platform specific font substitutions if available
165 SalGenericInstance::RegisterFontSubstitutors(pFontCollection
);
168 void FreeTypeTextRenderImpl::GetFontMetric( FontMetricDataRef
& rxFontMetric
, int nFallbackLevel
)
170 if( nFallbackLevel
>= MAX_FALLBACK
)
173 if (mpFreetypeFont
[nFallbackLevel
])
174 mpFreetypeFont
[nFallbackLevel
]->GetFreetypeFont().GetFontMetric(rxFontMetric
);
177 std::unique_ptr
<GenericSalLayout
> FreeTypeTextRenderImpl::GetTextLayout(int nFallbackLevel
)
179 assert(mpFreetypeFont
[nFallbackLevel
]);
180 if (!mpFreetypeFont
[nFallbackLevel
])
182 return std::make_unique
<GenericSalLayout
>(*mpFreetypeFont
[nFallbackLevel
]);
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */