1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #ifndef INCLUDED_VCL_EMBEDDEDFONTSHELPER_HXX
11 #define INCLUDED_VCL_EMBEDDEDFONTSHELPER_HXX
12 
13 #include <vcl/dllapi.h>
14 
15 #include <rtl/ustring.hxx>
16 #include <tools/fontenum.hxx>
17 #include <tools/long.hxx>
18 
19 #include <string_view>
20 #include <vector>
21 
22 namespace com::sun::star::io { class XInputStream; }
23 namespace com::sun::star::uno { template <typename > class Reference; }
24 
25 /**
26  Helper functions for handling embedded fonts in documents.
27 
28 */
29 class VCL_DLLPUBLIC EmbeddedFontsHelper
30 {
31 private:
32     std::vector<std::pair<OUString, OUString>> m_aAccumulatedFonts;
33 
34     /**
35       Adds the given font to the list of known fonts. The font is used only until application
36       exit.
37 
38       @param fontName name of the font (e.g. 'Times New Roman')
39       @param fileUrl URL of the font file
40     */
41     static void activateFont( const OUString& fontName, const OUString& fileUrl );
42 
43 public:
44     /// Specification of what kind of operation is allowed when embedding a font
45     enum class FontRights
46     {
47         ViewingAllowed, ///< Font may be embedded for viewing the document (but not editing)
48         EditingAllowed ///< Font may be embedded for editing document (implies viewing)
49     };
50 
51     /**
52       Returns URL for a font file for the given font, or empty if it does not exist.
53     */
54     static OUString fontFileUrl(
55         std::u16string_view familyName, FontFamily family, FontItalic italic,
56         FontWeight weight, FontPitch pitch, FontRights rights );
57 
58     /**
59       Reads a font from the input stream, saves it to a temporary font file and adds it to the list of
60       fonts that activateFonts will activate.
61       @param stream stream of font data
62       @param fontName name of the font (e.g. 'Times New Roman')
63       @param extra additional text to use for name (e.g. to distinguish regular from bold, italic,...), "?" for unique
64       @param key key to xor the data with, from the start until the key's length (not repeated)
65       @param eot whether the data is compressed in Embedded OpenType format
66     */
67     bool addEmbeddedFont( const css::uno::Reference< css::io::XInputStream >& stream,
68         const OUString& fontName, const char* extra,
69         std::vector< unsigned char > const & key, bool eot = false);
70 
71     /**
72       Returns a URL for a file where to store contents of a given temporary font.
73       The file may or not may not exist yet, and will be cleaned up automatically as appropriate.
74       Use activateTemporaryFont() to actually enable usage of the font.
75 
76       @param fontName name of the font (e.g. 'Times New Roman')
77       @param extra additional text to use for name (e.g. to distinguish regular from bold, italic,...), "?" for unique
78     */
79     static OUString fileUrlForTemporaryFont( const OUString& fontName, const char* extra );
80 
81     /**
82       Adds the accumulated fonts to the list of known fonts. The fonts are used only until application
83       exit.
84     */
85     void activateFonts();
86 
87     /**
88       Returns if the restrictions specified in the font (if present) allow embedding
89       the font for a particular purpose.
90       @param data font data
91       @param size size of the font data
92       @param rights type of operation to be allowed for the font
93     */
94     static bool sufficientTTFRights( const void* data, tools::Long size, FontRights rights );
95 
96     /**
97       Removes all temporary fonts in the path used by fileUrlForTemporaryFont().
98       @internal
99     */
100     static void clearTemporaryFontFiles();
101 
~EmbeddedFontsHelper()102     ~EmbeddedFontsHelper() COVERITY_NOEXCEPT_FALSE
103     {
104         activateFonts();
105     }
106 };
107 
108 #endif
109 
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
111