13#include <config_folders.h>
14#include <config_eot.h>
16#include <osl/file.hxx>
17#include <rtl/bootstrap.hxx>
21#include <com/sun/star/io/XInputStream.hpp>
34#include <libeot/libeot.h>
44 osl::Directory dir( path );
45 if( dir.reset() == osl::Directory::E_None )
49 osl::DirectoryItem item;
50 if( dir.getNextItem( item ) != osl::Directory::E_None )
52 osl::FileStatus status( osl_FileStatus_Mask_FileURL );
53 if( item.getFileStatus( status ) == osl::File::E_None )
54 osl::File::remove( status.getFileURL());
61 OUString path =
"${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE(
"bootstrap")
"::UserInstallation}";
62 rtl::Bootstrap::expandMacros( path );
63 path +=
"/user/temp/embeddedfonts/";
69 std::u16string_view extra, std::vector< unsigned char >
const & key,
bool eot )
72 osl::File file( fileUrl );
73 switch( file.open( osl_File_OpenFlag_Create | osl_File_OpenFlag_Write ))
75 case osl::File::E_None:
77 case osl::File::E_EXIST:
80 SAL_WARN(
"vcl.fonts",
"Cannot open file for temporary font" );
84 std::vector< char > fontData;
85 fontData.reserve( 1000000 );
88 uno::Sequence< sal_Int8 > buffer;
89 sal_uInt64 read =
stream->readBytes( buffer, 1024 );
90 auto bufferRange = asNonConstRange(buffer);
91 for( sal_uInt64
pos = 0;
92 pos < read && keyPos < key.size();
94 bufferRange[
pos ] ^= key[ keyPos++ ];
96 if( !eot && read > 0 )
98 sal_uInt64 writtenTotal = 0;
99 while( writtenTotal < read )
102 file.write( buffer.getConstArray(), read, written );
103 writtenTotal += written;
106 fontData.insert( fontData.end(), buffer.getConstArray(), buffer.getConstArray() + read );
110 bool sufficientFontRights(
false);
114 unsigned uncompressedFontSize = 0;
115 unsigned char *nakedPointerToUncompressedFont =
nullptr;
116 libeot::EOTMetadata eotMetadata;
117 libeot::EOTError uncompressError =
118 libeot::EOT2ttf_buffer(
reinterpret_cast<unsigned char *
>(fontData.data()), fontData.size(), &eotMetadata, &nakedPointerToUncompressedFont, &uncompressedFontSize );
119 std::shared_ptr<unsigned char> uncompressedFont( nakedPointerToUncompressedFont, libeot::EOTfreeBuffer );
120 if( uncompressError != libeot::EOT_SUCCESS )
122 SAL_WARN(
"vcl.fonts",
"Failed to uncompress font" );
123 osl::File::remove( fileUrl );
126 sal_uInt64 writtenTotal = 0;
127 while( writtenTotal < uncompressedFontSize )
130 if( file.write( uncompressedFont.get() + writtenTotal, uncompressedFontSize - writtenTotal, written ) != osl::File::E_None )
132 SAL_WARN(
"vcl.fonts",
"Error writing temporary font file" );
133 osl::File::remove( fileUrl );
136 writtenTotal += written;
138 sufficientFontRights = libeot::EOTcanLegallyEdit( &eotMetadata );
139 libeot::EOTfreeMetadata( &eotMetadata );
143 if( file.close() != osl::File::E_None )
145 SAL_WARN(
"vcl.fonts",
"Writing temporary font file failed" );
146 osl::File::remove( fileUrl );
153 if( !sufficientFontRights )
158 SAL_INFO(
"vcl.fonts",
"Ignoring embedded font that is not usable for editing" );
159 osl::File::remove( fileUrl );
168 struct UpdateFontsGuard
186 UpdateFontsGuard aUpdateFontsGuard;
194 OUString path =
"${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE(
"bootstrap")
"::UserInstallation}";
195 rtl::Bootstrap::expandMacros( path );
196 path +=
"/user/temp/embeddedfonts/fromdocs/";
197 osl::Directory::createPath( path );
198 OUString filename = fontName;
199 static int uniqueCounter = 0;
201 filename += OUString::number( uniqueCounter++ );
205 return path + filename;
233 return ( copyright & 0x02 ) != 0x02;
236 return copyright == 0 || ( copyright & 0x08 );
245 OUString path =
"${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE(
"bootstrap")
"::UserInstallation}";
246 rtl::Bootstrap::expandMacros( path );
247 path +=
"/user/temp/embeddedfonts/fromsystem/";
248 osl::Directory::createPath( path );
249 OUString filename = OUString::Concat(familyName) +
"_" + OUString::number( family ) +
"_" + OUString::number(
italic )
250 +
"_" + OUString::number( weight ) +
"_" + OUString::number( pitch )
252 OUString url = path + filename;
253 if( osl::File( url ).open( osl_File_OpenFlag_Read ) == osl::File::E_None )
262 std::unique_ptr< vcl::font::PhysicalFontFaceCollection > fontInfo(
fonts.GetFontFaceCollection());
270 typedef std::vector<vcl::font::PhysicalFontFace*>
FontList;
274 i < fontInfo->Count();
301 familyNameFonts.push_back(f);
307 FontList fontsToAdd = (selected ?
FontList(1, selected) : std::move(familyNameFonts));
312 filename = OUString::Concat(familyName) +
"_" + OUString::number(f->GetFamilyType()) +
"_" +
313 OUString::number(f->GetItalic()) +
"_" + OUString::number(f->GetWeight()) +
"_" +
314 OUString::number(f->GetPitch()) +
".ttf";
315 url = path + filename;
316 if (osl::File(url).open(osl_File_OpenFlag_Read) == osl::File::E_None)
322 auto aFontData(f->GetRawFontData(0));
323 if (!aFontData.empty())
325 auto data = aFontData.data();
326 auto size = aFontData.size();
329 osl::File file( url );
330 if( file.open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create ) == osl::File::E_None )
332 sal_uInt64 written = 0;
333 sal_uInt64 totalSize =
size;
335 while( written < totalSize && !error)
337 sal_uInt64 nowWritten;
338 switch( file.write( data + written,
size - written, nowWritten ))
340 case osl::File::E_None:
341 written += nowWritten;
343 case osl::File::E_AGAIN:
344 case osl::File::E_INTR:
353 osl::File::remove( url );
360 return ok ? url :
"";
static OutputDevice * GetDefaultDevice()
Get the default "device" (in this case the default window).
std::vector< std::pair< OUString, OUString > > m_aAccumulatedFonts
static OUString fileUrlForTemporaryFont(const OUString &fontName, std::u16string_view extra)
Returns a URL for a file where to store contents of a given temporary font.
bool addEmbeddedFont(const css::uno::Reference< css::io::XInputStream > &stream, const OUString &fontName, std::u16string_view extra, std::vector< unsigned char > const &key, bool eot=false)
Reads a font from the input stream, saves it to a temporary font file and adds it to the list of font...
void activateFonts()
Adds the accumulated fonts to the list of known fonts.
static OUString fontFileUrl(std::u16string_view familyName, FontFamily family, FontItalic italic, FontWeight weight, FontPitch pitch, FontRights rights)
Returns URL for a font file for the given font, or empty if it does not exist.
static bool sufficientTTFRights(const void *data, tools::Long size, FontRights rights)
Returns if the restrictions specified in the font (if present) allow embedding the font for a particu...
static void clearTemporaryFontFiles()
Removes all temporary fonts in the path used by fileUrlForTemporaryFont().
static void activateFont(const OUString &fontName, const OUString &fileUrl)
Adds the given font to the list of known fonts.
FontRights
Specification of what kind of operation is allowed when embedding a font.
@ ViewingAllowed
Font may be embedded for viewing the document (but not editing)
@ EditingAllowed
Font may be embedded for editing document (implies viewing)
FontFamily GetFamilyType() const
FontItalic GetItalic() const
FontWeight GetWeight() const
const OUString & GetFamilyName() const
FontPitch GetPitch() const
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
SalGraphics const * GetGraphics() const
Get the graphic context that the output device uses to draw on.
static void ImplClearAllFontData(bool bNewFontLists)
bool AddTempDevFont(const OUString &rFileURL, const OUString &rFontName)
static void ImplRefreshAllFontData(bool bNewFontLists)
virtual void GetDevFontList(vcl::font::PhysicalFontCollection *)=0
abstract base class for physical font faces
#define SAL_CONFIGFILE(name)
Reference< XOutputStream > stream
static void clearDir(const OUString &path)
void GetTTGlobalFontInfo(AbstractTrueTypeFont *ttf, TTGlobalFontInfo *info)
Returns global font information about the TrueType font.
SFErrCodes OpenTTFontBuffer(const void *pBuffer, sal_uInt32 nLen, sal_uInt32 facenum, TrueTypeFont **ttf, const FontCharMapRef xCharMap)
TrueTypeFont constructor.
void CloseTTFont(TrueTypeFont *ttf)
TrueTypeFont destructor.
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
Return value of GetTTGlobalFontInfo()
sal_uInt32 typeFlags
type flags (copyright bits)