11 #include <config_folders.h>
12 #include <config_eot.h>
14 #include <osl/file.hxx>
15 #include <rtl/bootstrap.hxx>
19 #include <com/sun/star/io/XInputStream.hpp>
32 #include <libeot/libeot.h>
42 osl::Directory dir( path );
43 if( dir.reset() == osl::Directory::E_None )
47 osl::DirectoryItem item;
48 if( dir.getNextItem( item ) != osl::Directory::E_None )
50 osl::FileStatus
status( osl_FileStatus_Mask_FileURL );
51 if( item.getFileStatus( status ) == osl::File::E_None )
52 osl::File::remove( status.getFileURL());
59 OUString path =
"${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE(
"bootstrap")
"::UserInstallation}";
60 rtl::Bootstrap::expandMacros( path );
61 path +=
"/user/temp/embeddedfonts/";
67 const char* extra, std::vector< unsigned char >
const & key,
bool eot )
70 osl::File file( fileUrl );
71 switch( file.open( osl_File_OpenFlag_Create | osl_File_OpenFlag_Write ))
73 case osl::File::E_None:
75 case osl::File::E_EXIST:
78 SAL_WARN(
"vcl.fonts",
"Cannot open file for temporary font" );
82 std::vector< char > fontData;
83 fontData.reserve( 1000000 );
86 uno::Sequence< sal_Int8 > buffer;
87 sal_uInt64 read = stream->readBytes( buffer, 1024 );
88 for( sal_uInt64
pos = 0;
89 pos < read && keyPos < key.size();
91 buffer[
pos ] ^= key[ keyPos++ ];
93 if( !eot && read > 0 )
95 sal_uInt64 writtenTotal = 0;
96 while( writtenTotal < read )
99 file.write( buffer.getConstArray(), read, written );
100 writtenTotal += written;
103 fontData.insert( fontData.end(), buffer.getConstArray(), buffer.getConstArray() + read );
107 bool sufficientFontRights(
false);
111 unsigned uncompressedFontSize = 0;
112 unsigned char *nakedPointerToUncompressedFont =
nullptr;
113 libeot::EOTMetadata eotMetadata;
114 libeot::EOTError uncompressError =
115 libeot::EOT2ttf_buffer( reinterpret_cast<unsigned char *>(fontData.data()), fontData.size(), &eotMetadata, &nakedPointerToUncompressedFont, &uncompressedFontSize );
116 std::shared_ptr<unsigned char> uncompressedFont( nakedPointerToUncompressedFont, libeot::EOTfreeBuffer );
117 if( uncompressError != libeot::EOT_SUCCESS )
119 SAL_WARN(
"vcl.fonts",
"Failed to uncompress font" );
120 osl::File::remove( fileUrl );
123 sal_uInt64 writtenTotal = 0;
124 while( writtenTotal < uncompressedFontSize )
127 if( file.write( uncompressedFont.get() + writtenTotal, uncompressedFontSize - writtenTotal, written ) != osl::File::E_None )
129 SAL_WARN(
"vcl.fonts",
"Error writing temporary font file" );
130 osl::File::remove( fileUrl );
133 writtenTotal += written;
135 sufficientFontRights = libeot::EOTcanLegallyEdit( &eotMetadata );
136 libeot::EOTfreeMetadata( &eotMetadata );
140 if( file.close() != osl::File::E_None )
142 SAL_WARN(
"vcl.fonts",
"Writing temporary font file failed" );
143 osl::File::remove( fileUrl );
148 sufficientFontRights = sufficientTTFRights(fontData.data(), fontData.size(), FontRights::EditingAllowed);
150 if( !sufficientFontRights )
155 SAL_INFO(
"vcl.fonts",
"Ignoring embedded font that is not usable for editing" );
156 osl::File::remove( fileUrl );
159 m_aAccumulatedFonts.emplace_back(std::make_pair(fontName, fileUrl));
165 struct UpdateFontsGuard
181 if (m_aAccumulatedFonts.empty())
183 UpdateFontsGuard aUpdateFontsGuard;
184 for (
const auto& rEntry : m_aAccumulatedFonts)
186 m_aAccumulatedFonts.clear();
191 OUString path =
"${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE(
"bootstrap")
"::UserInstallation}";
192 rtl::Bootstrap::expandMacros( path );
193 path +=
"/user/temp/embeddedfonts/fromdocs/";
194 osl::Directory::createPath( path );
195 OUString filename = fontName;
196 static int uniqueCounter = 0;
197 if( strcmp( extra,
"?" ) == 0 )
198 filename += OUString::number( uniqueCounter++ );
200 filename += OStringToOUString( extra, RTL_TEXTENCODING_ASCII_US );
202 return path + filename;
228 case FontRights::ViewingAllowed:
230 return ( copyright & 0x02 ) != 0x02;
231 case FontRights::EditingAllowed:
233 return copyright == 0 || ( copyright & 0x08 );
242 OUString path =
"${$BRAND_BASE_DIR/" LIBO_ETC_FOLDER
"/" SAL_CONFIGFILE(
"bootstrap")
"::UserInstallation}";
243 rtl::Bootstrap::expandMacros( path );
244 path +=
"/user/temp/embeddedfonts/fromsystem/";
245 osl::Directory::createPath( path );
246 OUString filename = OUString::Concat(familyName) +
"_" + OUString::number( family ) +
"_" + OUString::number( italic )
247 +
"_" + OUString::number( weight ) +
"_" + OUString::number( pitch )
249 OUString url = path + filename;
250 if( osl::File( url ).open( osl_File_OpenFlag_Read ) == osl::File::E_None )
262 i < fontInfo->Count();
290 if( selected !=
nullptr )
295 if( sufficientTTFRights(
data, size, rights ))
297 osl::File file( url );
298 if( file.open( osl_File_OpenFlag_Write | osl_File_OpenFlag_Create ) == osl::File::E_None )
300 sal_uInt64 written = 0;
301 sal_uInt64 totalSize = size;
303 while( written < totalSize && !error)
305 sal_uInt64 nowWritten;
306 switch( file.write( static_cast< const char* >(
data ) + written, size - written, nowWritten ))
308 case osl::File::E_None:
309 written += nowWritten;
311 case osl::File::E_AGAIN:
312 case osl::File::E_INTR:
321 osl::File::remove( url );
329 return ok ? url :
"";
exports com.sun.star.frame. status
static void clearDir(const OUString &path)
void activateFonts()
Adds the accumulated fonts to the list of known fonts.
void CloseTTFont(TrueTypeFont *ttf)
TrueTypeFont destructor.
bool addEmbeddedFont(const css::uno::Reference< css::io::XInputStream > &stream, const OUString &fontName, const char *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...
FontRights
Specification of what kind of operation is allowed when embedding a font.
virtual const void * GetEmbedFontData(const PhysicalFontFace *pFont, tools::Long *pDataLen)=0
static OutputDevice * GetDefaultDevice()
Get the default "device" (in this case the default window).
virtual void FreeEmbedFontData(const void *pData, tools::Long nDataLen)=0
FontItalic GetItalic() const
void GetTTGlobalFontInfo(TrueTypeFont *ttf, TTGlobalFontInfo *info)
Returns global font information about the TrueType font.
bool AddTempDevFont(const OUString &rFileURL, const OUString &rFontName)
sal_uInt32 typeFlags
type flags (copyright bits)
abstract base class for physical font faces
FontFamily GetFamilyType() const
#define SAL_CONFIGFILE(name)
static SAL_DLLPRIVATE void ImplRefreshAllFontData(bool bNewFontLists)
static void activateFont(const OUString &fontName, const OUString &fileUrl)
Adds the given font to the list of known fonts.
FontWeight GetWeight() const
static void clearTemporaryFontFiles()
Removes all temporary fonts in the path used by fileUrlForTemporaryFont().
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
static OUString fileUrlForTemporaryFont(const OUString &fontName, const char *extra)
Returns a URL for a file where to store contents of a given temporary font.
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.
std::unique_ptr< ImplDeviceFontList > GetDeviceFontList() const
exports com.sun.star.chart2. data
#define SAL_INFO(area, stream)
SalGraphics const * GetGraphics() const
Get the graphic context that the output device uses to draw on.
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...
#define SAL_WARN(area, stream)
void(* f)(TrueTypeTable *)
FontPitch GetPitch() const
static SAL_DLLPRIVATE void ImplClearAllFontData(bool bNewFontLists)
const OUString & GetFamilyName() const
Return value of GetTTGlobalFontInfo()
virtual void GetDevFontList(PhysicalFontCollection *)=0
SFErrCodes OpenTTFontBuffer(const void *pBuffer, sal_uInt32 nLen, sal_uInt32 facenum, TrueTypeFont **ttf, const FontCharMapRef xCharMap)
TrueTypeFont constructor.