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