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 <unx/freetypetextrender.hxx>
22 #include <unotools/configmgr.hxx>
23 #include <vcl/settings.hxx>
24 #include <vcl/sysdata.hxx>
25 #include <vcl/svapp.hxx>
26 #include <vcl/fontcharmap.hxx>
27 #include <sal/log.hxx>
29 #include <unx/genpspgraphics.h>
30 #include <unx/geninst.h>
31 #include <unx/glyphcache.hxx>
32 #include <unx/fc_fontoptions.hxx>
33 #include <unx/freetype_glyphcache.hxx>
34 #include <PhysicalFontFace.hxx>
35 #include <impfontmetricdata.hxx>
37 #include <sallayout.hxx>
39 FreeTypeTextRenderImpl::FreeTypeTextRenderImpl()
40 : mnTextColor(Color(0x00, 0x00, 0x00)) //black
44 FreeTypeTextRenderImpl::~FreeTypeTextRenderImpl()
49 void FreeTypeTextRenderImpl::SetFont(LogicalFontInstance
*pEntry
, int nFallbackLevel
)
51 // release all no longer needed font resources
52 for( int i
= nFallbackLevel
; i
< MAX_FALLBACK
; ++i
)
54 // old server side font is no longer referenced
55 mpFreetypeFont
[i
] = nullptr;
58 // return early if there is no new font
62 FreetypeFontInstance
* pFreetypeFont
= static_cast<FreetypeFontInstance
*>(pEntry
);
63 mpFreetypeFont
[ nFallbackLevel
] = pFreetypeFont
;
65 // ignore fonts with e.g. corrupted font files
66 if (!mpFreetypeFont
[nFallbackLevel
]->GetFreetypeFont().TestFont())
67 mpFreetypeFont
[nFallbackLevel
] = nullptr;
70 FontCharMapRef
FreeTypeTextRenderImpl::GetFontCharMap() const
72 if (!mpFreetypeFont
[0])
74 return mpFreetypeFont
[0]->GetFreetypeFont().GetFontCharMap();
77 bool FreeTypeTextRenderImpl::GetFontCapabilities(vcl::FontCapabilities
&rGetImplFontCapabilities
) const
79 if (!mpFreetypeFont
[0])
81 return mpFreetypeFont
[0]->GetFreetypeFont().GetFontCapabilities(rGetImplFontCapabilities
);
86 FreeTypeTextRenderImpl::SetTextColor( Color nColor
)
88 if( mnTextColor
!= nColor
)
94 bool FreeTypeTextRenderImpl::AddTempDevFont( PhysicalFontCollection
* pFontCollection
,
95 const OUString
& rFileURL
,
96 const OUString
& rFontName
)
98 return GenPspGraphics::AddTempDevFontHelper(pFontCollection
, rFileURL
, rFontName
);
101 void FreeTypeTextRenderImpl::ClearDevFontCache()
103 FreetypeManager::get().ClearFontCache();
104 psp::PrintFontManager::clearFontOptionsCache();
107 void FreeTypeTextRenderImpl::GetDevFontList( PhysicalFontCollection
* pFontCollection
)
109 // prepare the FreetypeManager using psprint's font infos
110 FreetypeManager
& rFreetypeManager
= FreetypeManager::get();
112 psp::PrintFontManager
& rMgr
= psp::PrintFontManager::get();
113 ::std::vector
< psp::fontID
> aList
;
114 psp::FastPrintFontInfo aInfo
;
115 rMgr
.getFontList( aList
);
116 for (auto const& elem
: aList
)
118 if( !rMgr
.getFontFastInfo( elem
, aInfo
) )
121 // normalize face number to the FreetypeManager
122 int nFaceNum
= rMgr
.getFontFaceNumber( aInfo
.m_nID
);
123 int nVariantNum
= rMgr
.getFontFaceVariation( aInfo
.m_nID
);
125 // inform FreetypeManager about this font provided by the PsPrint subsystem
126 FontAttributes aDFA
= GenPspGraphics::Info2FontAttributes( aInfo
);
127 aDFA
.IncreaseQualityBy( 4096 );
128 const OString
& rFileName
= rMgr
.getFontFileSysPath( aInfo
.m_nID
);
129 rFreetypeManager
.AddFontFile(rFileName
, nFaceNum
, nVariantNum
, aInfo
.m_nID
, aDFA
);
132 // announce glyphcache fonts
133 rFreetypeManager
.AnnounceFonts(pFontCollection
);
135 // register platform specific font substitutions if available
136 if (!utl::ConfigManager::IsFuzzing())
137 SalGenericInstance::RegisterFontSubstitutors( pFontCollection
);
140 void FreeTypeTextRenderImpl::GetFontMetric( ImplFontMetricDataRef
& rxFontMetric
, int nFallbackLevel
)
142 if( nFallbackLevel
>= MAX_FALLBACK
)
145 if (mpFreetypeFont
[nFallbackLevel
])
146 mpFreetypeFont
[nFallbackLevel
]->GetFreetypeFont().GetFontMetric(rxFontMetric
);
149 std::unique_ptr
<GenericSalLayout
> FreeTypeTextRenderImpl::GetTextLayout(int nFallbackLevel
)
151 assert(mpFreetypeFont
[nFallbackLevel
]);
152 if (!mpFreetypeFont
[nFallbackLevel
])
154 return std::make_unique
<GenericSalLayout
>(*mpFreetypeFont
[nFallbackLevel
]);
157 bool FreeTypeTextRenderImpl::CreateFontSubset(
158 const OUString
& rToFile
,
159 const PhysicalFontFace
* pFont
,
160 const sal_GlyphId
* pGlyphIds
,
161 const sal_uInt8
* pEncoding
,
164 FontSubsetInfo
& rInfo
167 // in this context the pFont->GetFontId() is a valid PSP
168 // font since they are the only ones left after the PDF
169 // export has filtered its list of subsettable fonts (for
170 // which this method was created). The correct way would
171 // be to have the FreetypeManager search for the PhysicalFontFace pFont
172 psp::fontID aFont
= pFont
->GetFontId();
174 psp::PrintFontManager
& rMgr
= psp::PrintFontManager::get();
175 bool bSuccess
= rMgr
.createFontSubset( rInfo
,
185 const void* FreeTypeTextRenderImpl::GetEmbedFontData(const PhysicalFontFace
* pFont
, tools::Long
* pDataLen
)
187 // in this context the pFont->GetFontId() is a valid PSP
188 // font since they are the only ones left after the PDF
189 // export has filtered its list of subsettable fonts (for
190 // which this method was created). The correct way would
191 // be to have the FreetypeManager search for the PhysicalFontFace pFont
192 psp::fontID aFont
= pFont
->GetFontId();
193 return GenPspGraphics::DoGetEmbedFontData(aFont
, pDataLen
);
196 void FreeTypeTextRenderImpl::FreeEmbedFontData( const void* pData
, tools::Long nLen
)
198 GenPspGraphics::DoFreeEmbedFontData( pData
, nLen
);
201 void FreeTypeTextRenderImpl::GetGlyphWidths( const PhysicalFontFace
* pFont
,
203 std::vector
< sal_Int32
>& rWidths
,
204 Ucs2UIntMap
& rUnicodeEnc
)
206 // in this context the pFont->GetFontId() is a valid PSP
207 // font since they are the only ones left after the PDF
208 // export has filtered its list of subsettable fonts (for
209 // which this method was created). The correct way would
210 // be to have the FreetypeManager search for the PhysicalFontFace pFont
211 psp::fontID aFont
= pFont
->GetFontId();
212 GenPspGraphics::DoGetGlyphWidths( aFont
, bVertical
, rWidths
, rUnicodeEnc
);
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */