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