xref: /aoo41x/main/vcl/inc/aqua/salgdi.h (revision 24f6443d)
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 _SV_SALGDI_H
25 #define _SV_SALGDI_H
26 
27 #include "basegfx/polygon/b2dpolypolygon.hxx"
28 
29 #include "premac.h"
30 #include <ApplicationServices/ApplicationServices.h>
31 #include "postmac.h"
32 
33 #include "aqua/aquavcltypes.h"
34 
35 #include "outfont.hxx"
36 #include "salgdi.hxx"
37 
38 #include <vector>
39 
40 class AquaSalFrame;
41 class AquaSalBitmap;
42 class ImplDevFontAttributes;
43 
44 class CGRect;
45 
46 // mac specific physically available font face
47 class ImplMacFontData : public ImplFontData
48 {
49 public:
50 	ImplMacFontData( const ImplDevFontAttributes&, ATSUFontID );
51 
52     virtual ~ImplMacFontData();
53 
54     virtual ImplFontData*   Clone() const;
55     virtual ImplFontEntry*  CreateFontInstance( ImplFontSelectData& ) const;
56 	virtual sal_IntPtr      GetFontId() const;
57 
58     const ImplFontCharMap*	GetImplFontCharMap() const;
59 	bool					HasChar( sal_uInt32 cChar ) const;
60 
61 	void					ReadOs2Table() const;
62 	void					ReadMacCmapEncoding() const;
63 	bool					HasCJKSupport() const;
64 
65 private:
66     const ATSUFontID			mnFontId;
67 	mutable const ImplFontCharMap*	mpCharMap;
68 	mutable bool				mbOs2Read;		 // true if OS2-table related info is valid
69 	mutable bool				mbHasOs2Table;
70 	mutable bool				mbCmapEncodingRead; // true if cmap encoding of Mac font is read
71 	mutable bool				mbHasCJKSupport; // #i78970# CJK fonts need extra leading
72 };
73 
74 // abstracting quartz color instead of having to use an CGFloat[] array
75 class RGBAColor
76 {
77 public:
78 	RGBAColor( SalColor );
79 	RGBAColor( float fRed, float fGreen, float fBlue, float fAlpha ); //NOTUSEDYET
80 	const float* AsArray() const	{ return &mfRed; }
81 	bool IsVisible() const			{ return (mfAlpha > 0); }
82 	void SetAlpha( float fAlpha )	{ mfAlpha = fAlpha; }
83 private:
84 	float mfRed, mfGreen, mfBlue, mfAlpha;
85 };
86 
87 // -------------------
88 // - AquaSalGraphics -
89 // -------------------
90 class AquaSalGraphics : public SalGraphics
91 {
92     friend class ATSLayout;
93 protected:
94     AquaSalFrame*                           mpFrame;
95 	CGLayerRef								mxLayer;	// Quartz graphics layer
96 	CGContextRef		                    mrContext;	// Quartz drawing context
97 	class XorEmulation*						mpXorEmulation;
98     int                                     mnXorMode; // 0: off 1: on 2: invert only
99 	int										mnWidth;
100 	int										mnHeight;
101 	int										mnBitmapDepth;	// zero unless bitmap
102     /// device resolution of this graphics
103     long                                    mnRealDPIX;
104 	long                                    mnRealDPIY;
105 	/// some graphics implementations (e.g. AquaSalInfoPrinter) scale
106 	/// everything down by a factor (see SetupPrinterGraphics for details)
107 	/// so we have to compensate for it with the inverse factor
108     double                                  mfFakeDPIScale;
109 
110     /// path representing current clip region
111     CGMutablePathRef                        mxClipPath;
112 
113     /// Drawing colors
114     /// pen color RGBA
115     RGBAColor                               maLineColor;
116     /// brush color RGBA
117     RGBAColor                               maFillColor;
118 
119     // Device Font settings
120  	const ImplMacFontData*                  mpMacFontData;
121     /// ATSU style object which carries all font attributes
122     ATSUStyle			                    maATSUStyle;
123     /// text rotation as ATSU angle
124     Fixed                                   mnATSUIRotation;
125     /// workaround to prevent ATSU overflows for huge font sizes
126     float                                   mfFontScale;
127     /// <1.0: font is squeezed, >1.0 font is stretched, else 1.0
128     float                                   mfFontStretch;
129     /// allows text to be rendered without antialiasing
130     bool                                    mbNonAntialiasedText;
131 
132 	// Graphics types
133 
134     /// is this a printer graphics
135 	bool                                    mbPrinter;
136     /// is this a virtual device graphics
137 	bool                                    mbVirDev;
138     /// is this a window graphics
139 	bool                                    mbWindow;
140 
141 public:
142     AquaSalGraphics();
143     virtual ~AquaSalGraphics();
144 
145     bool                IsPenVisible() const	{ return maLineColor.IsVisible(); }
146     bool                IsBrushVisible() const	{ return maFillColor.IsVisible(); }
147 
148     void                SetWindowGraphics( AquaSalFrame* pFrame );
149     void                SetPrinterGraphics( CGContextRef, long nRealDPIX, long nRealDPIY, double fFakeScale );
150     void                SetVirDevGraphics( CGLayerRef, CGContextRef, int nBitDepth = 0 );
151 
152     void                initResolution( NSWindow* );
153     void                copyResolution( AquaSalGraphics& );
154     void                updateResolution();
155 
156     bool                IsWindowGraphics()      const   { return mbWindow; }
157     bool                IsPrinterGraphics()     const   { return mbPrinter; }
158     bool                IsVirDevGraphics()      const   { return mbVirDev; }
159     AquaSalFrame*       getGraphicsFrame() const { return mpFrame; }
160     void                setGraphicsFrame( AquaSalFrame* pFrame ) { mpFrame = pFrame; }
161 
162     void                ImplDrawPixel( long nX, long nY, const RGBAColor& ); // helper to draw single pixels
163 
164     bool                CheckContext();
165     void                UpdateWindow( NSRect& ); // delivered in NSView coordinates
166 	void				RefreshRect( const CGRect& );
167 	void				RefreshRect( const NSRect& );
168 	void				RefreshRect(float lX, float lY, float lWidth, float lHeight);
169 
170     void                SetState();
171     void                UnsetState();
172     // InvalidateContext does an UnsetState and sets mrContext to 0
173     void                InvalidateContext();
174 
175     virtual bool        setClipRegion( const Region& );
176 
177     // draw --> LineColor and FillColor and RasterOp and ClipRegion
178     virtual void		drawPixel( long nX, long nY );
179     virtual void		drawPixel( long nX, long nY, SalColor nSalColor );
180     virtual void		drawLine( long nX1, long nY1, long nX2, long nY2 );
181     virtual void		drawRect( long nX, long nY, long nWidth, long nHeight );
182     virtual void		drawPolyLine( sal_uLong nPoints, const SalPoint* pPtAry );
183     virtual void		drawPolygon( sal_uLong nPoints, const SalPoint* pPtAry );
184     virtual void		drawPolyPolygon( sal_uLong nPoly, const sal_uLong* pPoints, PCONSTSALPOINT* pPtAry );
185     virtual bool        drawPolyPolygon( const ::basegfx::B2DPolyPolygon&, double fTransparency );
186     virtual sal_Bool	drawPolyLineBezier( sal_uLong nPoints, const SalPoint* pPtAry, const sal_uInt8* pFlgAry );
187     virtual sal_Bool	drawPolygonBezier( sal_uLong nPoints, const SalPoint* pPtAry, const sal_uInt8* pFlgAry );
188     virtual sal_Bool	drawPolyPolygonBezier( sal_uLong nPoly, const sal_uLong* pPoints, const SalPoint* const* pPtAry, const sal_uInt8* const* pFlgAry );
189     virtual bool        drawPolyLine( const ::basegfx::B2DPolygon&, double fTransparency, const ::basegfx::B2DVector& rLineWidths, basegfx::B2DLineJoin );
190 
191     // CopyArea --> No RasterOp, but ClipRegion
192     virtual void		copyArea( long nDestX, long nDestY, long nSrcX, long nSrcY, long nSrcWidth,
193                                   long nSrcHeight, sal_uInt16 nFlags );
194 
195     // CopyBits and DrawBitmap --> RasterOp and ClipRegion
196     // CopyBits() --> pSrcGraphics == NULL, then CopyBits on same Graphics
197     virtual void		copyBits( const SalTwoRect* pPosAry, SalGraphics* pSrcGraphics );
198     virtual void		drawBitmap( const SalTwoRect* pPosAry, const SalBitmap& rSalBitmap );
199     virtual void		drawBitmap( const SalTwoRect* pPosAry,
200                                     const SalBitmap& rSalBitmap,
201                                     SalColor nTransparentColor );
202     virtual void		drawBitmap( const SalTwoRect* pPosAry,
203                                     const SalBitmap& rSalBitmap,
204                                     const SalBitmap& rTransparentBitmap );
205     virtual void		drawMask( const SalTwoRect* pPosAry,
206                                   const SalBitmap& rSalBitmap,
207                                   SalColor nMaskColor );
208 
209     virtual SalBitmap*	getBitmap( long nX, long nY, long nWidth, long nHeight );
210     virtual SalColor	getPixel( long nX, long nY );
211 
212     // invert --> ClipRegion (only Windows or VirDevs)
213     virtual void		invert( long nX, long nY, long nWidth, long nHeight, SalInvert nFlags);
214     virtual void		invert( sal_uLong nPoints, const SalPoint* pPtAry, SalInvert nFlags );
215 
216     virtual sal_Bool		drawEPS( long nX, long nY, long nWidth, long nHeight, void* pPtr, sal_uLong nSize );
217 
218     virtual bool 			drawAlphaBitmap( const SalTwoRect&,
219                                              const SalBitmap& rSourceBitmap,
220                                              const SalBitmap& rAlphaBitmap );
221 
222     virtual bool		    drawAlphaRect( long nX, long nY, long nWidth,
223                                            long nHeight, sal_uInt8 nTransparency );
224 
225     CGPoint*                makeCGptArray(sal_uLong nPoints, const SalPoint*  pPtAry);
226     // native widget rendering methods that require mirroring
227     virtual sal_Bool        hitTestNativeControl( ControlType nType, ControlPart nPart, const Rectangle& rControlRegion,
228                                               const Point& aPos, sal_Bool& rIsInside );
229     virtual sal_Bool        drawNativeControl( ControlType nType, ControlPart nPart, const Rectangle& rControlRegion,
230                                            ControlState nState, const ImplControlValue& aValue,
231                                            const rtl::OUString& aCaption );
232     virtual sal_Bool        drawNativeControlText( ControlType nType, ControlPart nPart, const Rectangle& rControlRegion,
233                                                ControlState nState, const ImplControlValue& aValue,
234                                                const rtl::OUString& aCaption );
235     virtual sal_Bool        getNativeControlRegion( ControlType nType, ControlPart nPart, const Rectangle& rControlRegion, ControlState nState,
236                                                 const ImplControlValue& aValue, const rtl::OUString& aCaption,
237                                                 Rectangle &rNativeBoundingRegion, Rectangle &rNativeContentRegion );
238 
239     // get device resolution
240     virtual void			GetResolution( long& rDPIX, long& rDPIY );
241     // get the depth of the device
242     virtual sal_uInt16			GetBitCount();
243     // get the width of the device
244     virtual long			GetGraphicsWidth() const;
245 
246     // set the clip region to empty
247     virtual void			ResetClipRegion();
248 
249     // set the line color to transparent (= don't draw lines)
250     virtual void			SetLineColor();
251     // set the line color to a specific color
252     virtual void			SetLineColor( SalColor nSalColor );
253     // set the fill color to transparent (= don't fill)
254     virtual void			SetFillColor();
255     // set the fill color to a specific color, shapes will be
256     // filled accordingly
257     virtual void          	SetFillColor( SalColor nSalColor );
258     // enable/disable XOR drawing
259     virtual void			SetXORMode( bool bSet, bool bInvertOnly );
260     // set line color for raster operations
261     virtual void			SetROPLineColor( SalROPColor nROPColor );
262     // set fill color for raster operations
263     virtual void			SetROPFillColor( SalROPColor nROPColor );
264     // set the text color to a specific color
265     virtual void			SetTextColor( SalColor nSalColor );
266     // set the font
267     virtual sal_uInt16         SetFont( ImplFontSelectData*, int nFallbackLevel );
268     // get the current font's etrics
269     virtual void			GetFontMetric( ImplFontMetricData*, int nFallbackLevel );
270     // get kernign pairs of the current font
271     // return only PairCount if (pKernPairs == NULL)
272     virtual sal_uLong			GetKernPairs( sal_uLong nPairs, ImplKernPairData* pKernPairs );
273     // get the repertoire of the current font
274     virtual const ImplFontCharMap* GetImplFontCharMap() const;
275     // graphics must fill supplied font list
276     virtual void			GetDevFontList( ImplDevFontList* );
277     // graphics should call ImplAddDevFontSubstitute on supplied
278     // OutputDevice for all its device specific preferred font substitutions
279     virtual void			GetDevFontSubstList( OutputDevice* );
280     virtual bool			AddTempDevFont( ImplDevFontList*, const String& rFileURL, const String& rFontName );
281     // CreateFontSubset: a method to get a subset of glyhps of a font
282     // inside a new valid font file
283     // returns TRUE if creation of subset was successfull
284     // parameters: rToFile: contains a osl file URL to write the subset to
285     //             pFont: describes from which font to create a subset
286     //             pGlyphIDs: the glyph ids to be extracted
287     //             pEncoding: the character code corresponding to each glyph
288     //             pWidths: the advance widths of the correspoding glyphs (in PS font units)
289     //             nGlyphs: the number of glyphs
290     //             rInfo: additional outgoing information
291     // implementation note: encoding 0 with glyph id 0 should be added implicitly
292     // as "undefined character"
293     virtual sal_Bool			CreateFontSubset( const rtl::OUString& rToFile,
294                                               const ImplFontData* pFont,
295                                               long* pGlyphIDs,
296                                               sal_uInt8* pEncoding,
297                                               sal_Int32* pWidths,
298                                               int nGlyphs,
299                                               FontSubsetInfo& rInfo // out parameter
300                                               );
301 
302     // GetFontEncodingVector: a method to get the encoding map Unicode
303 	// to font encoded character; this is only used for type1 fonts and
304     // may return NULL in case of unknown encoding vector
305     // if ppNonEncoded is set and non encoded characters (that is type1
306     // glyphs with only a name) exist it is set to the corresponding
307     // map for non encoded glyphs; the encoding vector contains -1
308     // as encoding for these cases
309     virtual const Ucs2SIntMap* GetFontEncodingVector( const ImplFontData*, const Ucs2OStrMap** ppNonEncoded );
310 
311     // GetEmbedFontData: gets the font data for a font marked
312     // embeddable by GetDevFontList or NULL in case of error
313     // parameters: pFont: describes the font in question
314     //             pWidths: the widths of all glyphs from char code 0 to 255
315     //                      pWidths MUST support at least 256 members;
316     //             rInfo: additional outgoing information
317     //             pDataLen: out parameter, contains the byte length of the returned buffer
318     virtual const void*	GetEmbedFontData( const ImplFontData*,
319                                           const sal_Ucs* pUnicodes,
320                                           sal_Int32* pWidths,
321                                           FontSubsetInfo& rInfo,
322                                           long* pDataLen );
323     // frees the font data again
324     virtual void			FreeEmbedFontData( const void* pData, long nDataLen );
325 
326     virtual void            GetGlyphWidths( const ImplFontData*,
327                                             bool bVertical,
328                                             Int32Vector& rWidths,
329                                             Ucs2UIntMap& rUnicodeEnc );
330 
331     virtual sal_Bool                    GetGlyphBoundRect( long nIndex, Rectangle& );
332     virtual sal_Bool                    GetGlyphOutline( long nIndex, basegfx::B2DPolyPolygon& );
333 
334     virtual SalLayout*              GetTextLayout( ImplLayoutArgs&, int nFallbackLevel );
335     virtual void					 DrawServerFontLayout( const ServerFontLayout& );
336     virtual bool                    supportsOperation( OutDevSupportType ) const;
337 
338     // Query the platform layer for control support
339     virtual sal_Bool IsNativeControlSupported( ControlType nType, ControlPart nPart );
340 
341     virtual SystemGraphicsData    GetGraphicsData() const;
342     virtual SystemFontData        GetSysFontData( int /* nFallbacklevel */ ) const;
343 
344 private:
345     // differences between VCL, Quartz and kHiThemeOrientation coordinate systems
346     // make some graphics seem to be vertically-mirrored from a VCL perspective
347     bool IsFlipped() const { return mbWindow; }
348 
349 	void ApplyXorContext();
350     void Pattern50Fill();
351     UInt32 getState( ControlState nState );
352     UInt32 getTrackState( ControlState nState );
353 };
354 
355 class XorEmulation
356 {
357 public:
358 					XorEmulation();
359 	/*final*/		~XorEmulation();
360 
361 	void			SetTarget( int nWidth, int nHeight, int nBitmapDepth, CGContextRef, CGLayerRef );
362 	bool			UpdateTarget();
363 	void			Enable()			{ mbIsEnabled = true; }
364 	void			Disable()			{ mbIsEnabled = false; }
365 	bool 			IsEnabled() const	{ return mbIsEnabled; }
366 	CGContextRef	GetTargetContext() const { return mxTargetContext; }
367 	CGContextRef	GetMaskContext() const { return (mbIsEnabled ? mxMaskContext : NULL); }
368 
369 private:
370 	CGLayerRef		mxTargetLayer;
371 	CGContextRef	mxTargetContext;
372 	CGContextRef	mxMaskContext;
373 	CGContextRef	mxTempContext;
374 	sal_uLong*			mpMaskBuffer;
375 	sal_uLong*			mpTempBuffer;
376 	int				mnBufferLongs;
377 	bool			mbIsEnabled;
378 };
379 
380 
381 // --- some trivial inlines
382 
383 inline void AquaSalGraphics::RefreshRect( const CGRect& rRect )
384 {
385 	RefreshRect( rRect.origin.x, rRect.origin.y, rRect.size.width, rRect.size.height );
386 }
387 
388 inline void AquaSalGraphics::RefreshRect( const NSRect& rRect )
389 {
390 	RefreshRect( rRect.origin.x, rRect.origin.y, rRect.size.width, rRect.size.height );
391 }
392 
393 inline RGBAColor::RGBAColor( SalColor nSalColor )
394 :	mfRed( SALCOLOR_RED(nSalColor) * (1.0/255))
395 ,	mfGreen( SALCOLOR_GREEN(nSalColor) * (1.0/255))
396 ,	mfBlue( SALCOLOR_BLUE(nSalColor) * (1.0/255))
397 ,	mfAlpha( 1.0 )	// opaque
398 {}
399 
400 inline RGBAColor::RGBAColor( float fRed, float fGreen, float fBlue, float fAlpha )
401 :	mfRed( fRed )
402 ,	mfGreen( fGreen )
403 ,	mfBlue( fBlue )
404 ,	mfAlpha( fAlpha )
405 {}
406 
407 #endif // _SV_SALGDI_H
408