xref: /aoo41x/main/vcl/inc/aqua/salgdi.h (revision 5aaf853b)
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(
190         const ::basegfx::B2DPolygon&,
191         double fTransparency,
192         const ::basegfx::B2DVector& rLineWidths,
193         basegfx::B2DLineJoin,
194         com::sun::star::drawing::LineCap eLineCap);
195 
196     // CopyArea --> No RasterOp, but ClipRegion
197     virtual void		copyArea( long nDestX, long nDestY, long nSrcX, long nSrcY, long nSrcWidth,
198                                   long nSrcHeight, sal_uInt16 nFlags );
199 
200     // CopyBits and DrawBitmap --> RasterOp and ClipRegion
201     // CopyBits() --> pSrcGraphics == NULL, then CopyBits on same Graphics
202     virtual void		copyBits( const SalTwoRect* pPosAry, SalGraphics* pSrcGraphics );
203     virtual void		drawBitmap( const SalTwoRect* pPosAry, const SalBitmap& rSalBitmap );
204     virtual void		drawBitmap( const SalTwoRect* pPosAry,
205                                     const SalBitmap& rSalBitmap,
206                                     SalColor nTransparentColor );
207     virtual void		drawBitmap( const SalTwoRect* pPosAry,
208                                     const SalBitmap& rSalBitmap,
209                                     const SalBitmap& rTransparentBitmap );
210     virtual void		drawMask( const SalTwoRect* pPosAry,
211                                   const SalBitmap& rSalBitmap,
212                                   SalColor nMaskColor );
213 
214     virtual SalBitmap*	getBitmap( long nX, long nY, long nWidth, long nHeight );
215     virtual SalColor	getPixel( long nX, long nY );
216 
217     // invert --> ClipRegion (only Windows or VirDevs)
218     virtual void		invert( long nX, long nY, long nWidth, long nHeight, SalInvert nFlags);
219     virtual void		invert( sal_uLong nPoints, const SalPoint* pPtAry, SalInvert nFlags );
220 
221     virtual sal_Bool		drawEPS( long nX, long nY, long nWidth, long nHeight, void* pPtr, sal_uLong nSize );
222 
223     virtual bool 			drawAlphaBitmap( const SalTwoRect&,
224                                              const SalBitmap& rSourceBitmap,
225                                              const SalBitmap& rAlphaBitmap );
226 
227     virtual bool		    drawAlphaRect( long nX, long nY, long nWidth,
228                                            long nHeight, sal_uInt8 nTransparency );
229 
230     CGPoint*                makeCGptArray(sal_uLong nPoints, const SalPoint*  pPtAry);
231     // native widget rendering methods that require mirroring
232     virtual sal_Bool        hitTestNativeControl( ControlType nType, ControlPart nPart, const Rectangle& rControlRegion,
233                                               const Point& aPos, sal_Bool& rIsInside );
234     virtual sal_Bool        drawNativeControl( ControlType nType, ControlPart nPart, const Rectangle& rControlRegion,
235                                            ControlState nState, const ImplControlValue& aValue,
236                                            const rtl::OUString& aCaption );
237     virtual sal_Bool        drawNativeControlText( ControlType nType, ControlPart nPart, const Rectangle& rControlRegion,
238                                                ControlState nState, const ImplControlValue& aValue,
239                                                const rtl::OUString& aCaption );
240     virtual sal_Bool        getNativeControlRegion( ControlType nType, ControlPart nPart, const Rectangle& rControlRegion, ControlState nState,
241                                                 const ImplControlValue& aValue, const rtl::OUString& aCaption,
242                                                 Rectangle &rNativeBoundingRegion, Rectangle &rNativeContentRegion );
243 
244     // get device resolution
245     virtual void			GetResolution( long& rDPIX, long& rDPIY );
246     // get the depth of the device
247     virtual sal_uInt16			GetBitCount();
248     // get the width of the device
249     virtual long			GetGraphicsWidth() const;
250 
251     // set the clip region to empty
252     virtual void			ResetClipRegion();
253 
254     // set the line color to transparent (= don't draw lines)
255     virtual void			SetLineColor();
256     // set the line color to a specific color
257     virtual void			SetLineColor( SalColor nSalColor );
258     // set the fill color to transparent (= don't fill)
259     virtual void			SetFillColor();
260     // set the fill color to a specific color, shapes will be
261     // filled accordingly
262     virtual void          	SetFillColor( SalColor nSalColor );
263     // enable/disable XOR drawing
264     virtual void			SetXORMode( bool bSet, bool bInvertOnly );
265     // set line color for raster operations
266     virtual void			SetROPLineColor( SalROPColor nROPColor );
267     // set fill color for raster operations
268     virtual void			SetROPFillColor( SalROPColor nROPColor );
269     // set the text color to a specific color
270     virtual void			SetTextColor( SalColor nSalColor );
271     // set the font
272     virtual sal_uInt16         SetFont( ImplFontSelectData*, int nFallbackLevel );
273     // get the current font's etrics
274     virtual void			GetFontMetric( ImplFontMetricData*, int nFallbackLevel );
275     // get kernign pairs of the current font
276     // return only PairCount if (pKernPairs == NULL)
277     virtual sal_uLong			GetKernPairs( sal_uLong nPairs, ImplKernPairData* pKernPairs );
278     // get the repertoire of the current font
279     virtual const ImplFontCharMap* GetImplFontCharMap() const;
280     // graphics must fill supplied font list
281     virtual void			GetDevFontList( ImplDevFontList* );
282     // graphics should call ImplAddDevFontSubstitute on supplied
283     // OutputDevice for all its device specific preferred font substitutions
284     virtual void			GetDevFontSubstList( OutputDevice* );
285     virtual bool			AddTempDevFont( ImplDevFontList*, const String& rFileURL, const String& rFontName );
286     // CreateFontSubset: a method to get a subset of glyhps of a font
287     // inside a new valid font file
288     // returns TRUE if creation of subset was successfull
289     // parameters: rToFile: contains a osl file URL to write the subset to
290     //             pFont: describes from which font to create a subset
291     //             pGlyphIDs: the glyph ids to be extracted
292     //             pEncoding: the character code corresponding to each glyph
293     //             pWidths: the advance widths of the correspoding glyphs (in PS font units)
294     //             nGlyphs: the number of glyphs
295     //             rInfo: additional outgoing information
296     // implementation note: encoding 0 with glyph id 0 should be added implicitly
297     // as "undefined character"
298     virtual sal_Bool			CreateFontSubset( const rtl::OUString& rToFile,
299                                               const ImplFontData* pFont,
300                                               long* pGlyphIDs,
301                                               sal_uInt8* pEncoding,
302                                               sal_Int32* pWidths,
303                                               int nGlyphs,
304                                               FontSubsetInfo& rInfo // out parameter
305                                               );
306 
307     // GetFontEncodingVector: a method to get the encoding map Unicode
308 	// to font encoded character; this is only used for type1 fonts and
309     // may return NULL in case of unknown encoding vector
310     // if ppNonEncoded is set and non encoded characters (that is type1
311     // glyphs with only a name) exist it is set to the corresponding
312     // map for non encoded glyphs; the encoding vector contains -1
313     // as encoding for these cases
314     virtual const Ucs2SIntMap* GetFontEncodingVector( const ImplFontData*, const Ucs2OStrMap** ppNonEncoded );
315 
316     // GetEmbedFontData: gets the font data for a font marked
317     // embeddable by GetDevFontList or NULL in case of error
318     // parameters: pFont: describes the font in question
319     //             pWidths: the widths of all glyphs from char code 0 to 255
320     //                      pWidths MUST support at least 256 members;
321     //             rInfo: additional outgoing information
322     //             pDataLen: out parameter, contains the byte length of the returned buffer
323     virtual const void*	GetEmbedFontData( const ImplFontData*,
324                                           const sal_Ucs* pUnicodes,
325                                           sal_Int32* pWidths,
326                                           FontSubsetInfo& rInfo,
327                                           long* pDataLen );
328     // frees the font data again
329     virtual void			FreeEmbedFontData( const void* pData, long nDataLen );
330 
331     virtual void            GetGlyphWidths( const ImplFontData*,
332                                             bool bVertical,
333                                             Int32Vector& rWidths,
334                                             Ucs2UIntMap& rUnicodeEnc );
335 
336     virtual sal_Bool                    GetGlyphBoundRect( long nIndex, Rectangle& );
337     virtual sal_Bool                    GetGlyphOutline( long nIndex, basegfx::B2DPolyPolygon& );
338 
339     virtual SalLayout*              GetTextLayout( ImplLayoutArgs&, int nFallbackLevel );
340     virtual void					 DrawServerFontLayout( const ServerFontLayout& );
341     virtual bool                    supportsOperation( OutDevSupportType ) const;
342 
343     // Query the platform layer for control support
344     virtual sal_Bool IsNativeControlSupported( ControlType nType, ControlPart nPart );
345 
346     virtual SystemGraphicsData    GetGraphicsData() const;
347     virtual SystemFontData        GetSysFontData( int /* nFallbacklevel */ ) const;
348 
349 private:
350     // differences between VCL, Quartz and kHiThemeOrientation coordinate systems
351     // make some graphics seem to be vertically-mirrored from a VCL perspective
352     bool IsFlipped() const { return mbWindow; }
353 
354 	void ApplyXorContext();
355     void Pattern50Fill();
356     UInt32 getState( ControlState nState );
357     UInt32 getTrackState( ControlState nState );
358 };
359 
360 class XorEmulation
361 {
362 public:
363 					XorEmulation();
364 	/*final*/		~XorEmulation();
365 
366 	void			SetTarget( int nWidth, int nHeight, int nBitmapDepth, CGContextRef, CGLayerRef );
367 	bool			UpdateTarget();
368 	void			Enable()			{ mbIsEnabled = true; }
369 	void			Disable()			{ mbIsEnabled = false; }
370 	bool 			IsEnabled() const	{ return mbIsEnabled; }
371 	CGContextRef	GetTargetContext() const { return mxTargetContext; }
372 	CGContextRef	GetMaskContext() const { return (mbIsEnabled ? mxMaskContext : NULL); }
373 
374 private:
375 	CGLayerRef		mxTargetLayer;
376 	CGContextRef	mxTargetContext;
377 	CGContextRef	mxMaskContext;
378 	CGContextRef	mxTempContext;
379 	sal_uLong*			mpMaskBuffer;
380 	sal_uLong*			mpTempBuffer;
381 	int				mnBufferLongs;
382 	bool			mbIsEnabled;
383 };
384 
385 
386 // --- some trivial inlines
387 
388 inline void AquaSalGraphics::RefreshRect( const CGRect& rRect )
389 {
390 	RefreshRect( rRect.origin.x, rRect.origin.y, rRect.size.width, rRect.size.height );
391 }
392 
393 inline void AquaSalGraphics::RefreshRect( const NSRect& rRect )
394 {
395 	RefreshRect( rRect.origin.x, rRect.origin.y, rRect.size.width, rRect.size.height );
396 }
397 
398 inline RGBAColor::RGBAColor( SalColor nSalColor )
399 :	mfRed( SALCOLOR_RED(nSalColor) * (1.0/255))
400 ,	mfGreen( SALCOLOR_GREEN(nSalColor) * (1.0/255))
401 ,	mfBlue( SALCOLOR_BLUE(nSalColor) * (1.0/255))
402 ,	mfAlpha( 1.0 )	// opaque
403 {}
404 
405 inline RGBAColor::RGBAColor( float fRed, float fGreen, float fBlue, float fAlpha )
406 :	mfRed( fRed )
407 ,	mfGreen( fGreen )
408 ,	mfBlue( fBlue )
409 ,	mfAlpha( fAlpha )
410 {}
411 
412 #endif // _SV_SALGDI_H
413