xref: /trunk/main/oox/inc/oox/helper/graphichelper.hxx (revision a893be29)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef OOX_HELPER_GRAPHICHELPER_HXX
25 #define OOX_HELPER_GRAPHICHELPER_HXX
26 
27 #include <deque>
28 #include <map>
29 #include <rtl/ustring.hxx>
30 #include <com/sun/star/awt/DeviceInfo.hpp>
31 #include <com/sun/star/uno/Reference.hxx>
32 #include "oox/helper/binarystreambase.hxx"
33 #include "oox/helper/storagebase.hxx"
34 
35 namespace com { namespace sun { namespace star {
36     namespace awt { struct Point; }
37     namespace awt { struct Size; }
38     namespace awt { class XUnitConversion; }
39     namespace io { class XInputStream; }
40     namespace frame { class XFrame; }
41     namespace graphic { class XGraphic; }
42     namespace graphic { class XGraphicObject; }
43     namespace graphic { class XGraphicProvider; }
44     namespace uno { class XComponentContext; }
45 } } }
46 
47 namespace oox {
48 
49 // ============================================================================
50 
51 /** Provides helper functions for colors, device measurement conversion,
52     graphics, and graphic objects handling.
53 
54     All createGraphicObject() and importGraphicObject() functions create
55     persistent graphic objects internally and store them in an internal
56     container to prevent their early destruction. This makes it possible to use
57     the returned URL of the graphic object in any way (e.g. insert it into a
58     property map) without needing to store it immediately at an object that
59     resolves the graphic object from the passed URL and thus prevents it from
60     being destroyed.
61  */
62 class GraphicHelper
63 {
64 public:
65     explicit            GraphicHelper(
66                             const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext >& rxContext,
67                             const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XFrame >& rxTargetFrame,
68                             const StorageRef& rxStorage );
69     virtual             ~GraphicHelper();
70 
71     // System colors and predefined colors ------------------------------------
72 
73     /** Returns a system color specified by the passed XML token identifier. */
74     sal_Int32           getSystemColor( sal_Int32 nToken, sal_Int32 nDefaultRgb = API_RGB_TRANSPARENT ) const;
75     /** Derived classes may implement to resolve a scheme color from the passed XML token identifier. */
76     virtual sal_Int32   getSchemeColor( sal_Int32 nToken ) const;
77     /** Derived classes may implement to resolve a palette index to an RGB color. */
78     virtual sal_Int32   getPaletteColor( sal_Int32 nPaletteIdx ) const;
79 
80     // Device info and device dependent unit conversion -----------------------
81 
82     /** Returns information about the output device. */
83     const ::com::sun::star::awt::DeviceInfo& getDeviceInfo() const;
84 
85     /** Converts the passed value from horizontal screen pixels to 1/100 mm. */
86     sal_Int32           convertScreenPixelXToHmm( double fPixelX ) const;
87     /** Converts the passed value from vertical screen pixels to 1/100 mm. */
88     sal_Int32           convertScreenPixelYToHmm( double fPixelY ) const;
89     /** Converts the passed point from screen pixels to 1/100 mm. */
90     ::com::sun::star::awt::Point convertScreenPixelToHmm( const ::com::sun::star::awt::Point& rPixel ) const;
91     /** Converts the passed size from screen pixels to 1/100 mm. */
92     ::com::sun::star::awt::Size convertScreenPixelToHmm( const ::com::sun::star::awt::Size& rPixel ) const;
93 
94     /** Converts the passed value from 1/100 mm to horizontal screen pixels. */
95     double              convertHmmToScreenPixelX( sal_Int32 nHmmX ) const;
96     /** Converts the passed value from 1/100 mm to vertical screen pixels. */
97     double              convertHmmToScreenPixelY( sal_Int32 nHmmY ) const;
98     /** Converts the passed point from 1/100 mm to screen pixels. */
99     ::com::sun::star::awt::Point convertHmmToScreenPixel( const ::com::sun::star::awt::Point& rHmm ) const;
100     /** Converts the passed size from 1/100 mm to screen pixels. */
101     ::com::sun::star::awt::Size convertHmmToScreenPixel( const ::com::sun::star::awt::Size& rHmm ) const;
102 
103     /** Converts the passed point from AppFont units to 1/100 mm. */
104     ::com::sun::star::awt::Point convertAppFontToHmm( const ::com::sun::star::awt::Point& rAppFont ) const;
105     /** Converts the passed point from AppFont units to 1/100 mm. */
106     ::com::sun::star::awt::Size convertAppFontToHmm( const ::com::sun::star::awt::Size& rAppFont ) const;
107 
108     /** Converts the passed point from 1/100 mm to AppFont units. */
109     ::com::sun::star::awt::Point convertHmmToAppFont( const ::com::sun::star::awt::Point& rHmm ) const;
110     /** Converts the passed size from 1/100 mm to AppFont units. */
111     ::com::sun::star::awt::Size convertHmmToAppFont( const ::com::sun::star::awt::Size& rHmm ) const;
112 
113     // Graphics and graphic objects  ------------------------------------------
114 
115     /** Imports a graphic from the passed input stream. */
116     ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic >
117                         importGraphic(
118                             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStrm ) const;
119 
120     /** Imports a graphic from the passed binary memory block. */
121     ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic >
122                         importGraphic( const StreamDataSequence& rGraphicData ) const;
123 
124     /** Imports a graphic from the storage stream with the passed path and name. */
125     ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic >
126                         importEmbeddedGraphic( const ::rtl::OUString& rStreamName ) const;
127 
128     /** Creates a persistent graphic object from the passed graphic.
129         @return  The URL of the created and internally cached graphic object. */
130     ::rtl::OUString     createGraphicObject(
131                             const ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic >& rxGraphic ) const;
132 
133     /** Creates a persistent graphic object from the passed input stream.
134         @return  The URL of the created and internally cached graphic object. */
135     ::rtl::OUString     importGraphicObject(
136                             const ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream >& rxInStrm ) const;
137 
138     /** Creates a persistent graphic object from the passed binary memory block.
139         @return  The URL of the created and internally cached graphic object. */
140     ::rtl::OUString     importGraphicObject( const StreamDataSequence& rGraphicData ) const;
141 
142     /** Imports a graphic object from the storage stream with the passed path and name.
143         @return  The URL of the created and internally cached graphic object. */
144     ::rtl::OUString     importEmbeddedGraphicObject( const ::rtl::OUString& rStreamName ) const;
145 
146 	/** calculates the orignal size of a graphic which is necessary to be able to calculate cropping values
147 		@return	The original Graphic size in 100thmm */
148 	::com::sun::star::awt::Size getOriginalSize( const ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic >& rxGraphic ) const;
149 
150     // ------------------------------------------------------------------------
151 private:
152     typedef ::std::map< sal_Int32, sal_Int32 > SystemPalette;
153     typedef ::std::deque< ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphicObject > > GraphicObjectDeque;
154     typedef ::std::map< ::rtl::OUString, ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphic > > EmbeddedGraphicMap;
155 
156     ::com::sun::star::uno::Reference< ::com::sun::star::uno::XComponentContext > mxContext;
157     ::com::sun::star::uno::Reference< ::com::sun::star::graphic::XGraphicProvider > mxGraphicProvider;
158     ::com::sun::star::uno::Reference< ::com::sun::star::awt::XUnitConversion > mxUnitConversion;
159     ::com::sun::star::awt::DeviceInfo maDeviceInfo; /// Current output device info.
160     SystemPalette       maSystemPalette;            /// Maps system colors (XML tokens) to RGB color values.
161     StorageRef          mxStorage;                  /// Storage containing embedded graphics.
162     mutable GraphicObjectDeque maGraphicObjects;    /// Caches all created graphic objects to keep them alive.
163     mutable EmbeddedGraphicMap maEmbeddedGraphics;  /// Maps all embedded graphics by their storage path.
164     const ::rtl::OUString maGraphicObjScheme;       /// The URL scheme name for graphic objects.
165     double              mfPixelPerHmmX;             /// Number of screen pixels per 1/100 mm in X direction.
166     double              mfPixelPerHmmY;             /// Number of screen pixels per 1/100 mm in Y direction.
167 };
168 
169 // ============================================================================
170 
171 } // namespace oox
172 
173 #endif
174