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