1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 2 /* 3 * This file is part of the LibreOffice project. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 * 9 * This file incorporates work covered by the following license notice: 10 * 11 * Licensed to the Apache Software Foundation (ASF) under one or more 12 * contributor license agreements. See the NOTICE file distributed 13 * with this work for additional information regarding copyright 14 * ownership. The ASF licenses this file to you under the Apache 15 * License, Version 2.0 (the "License"); you may not use this file 16 * except in compliance with the License. You may obtain a copy of 17 * the License at http://www.apache.org/licenses/LICENSE-2.0 . 18 */ 19 20 #ifndef INCLUDED_VCL_INC_QUARTZ_SALGDI_H 21 #define INCLUDED_VCL_INC_QUARTZ_SALGDI_H 22 23 #include <vector> 24 25 #include <basegfx/polygon/b2dpolypolygon.hxx> 26 #include <tools/long.hxx> 27 28 #include <premac.h> 29 #ifdef MACOSX 30 #include <ApplicationServices/ApplicationServices.h> 31 #include <osx/osxvcltypes.h> 32 #include <osx/salframe.h> 33 #else 34 #include <CoreGraphics/CoreGraphics.h> 35 #include <CoreText/CoreText.h> 36 #endif 37 #include <postmac.h> 38 39 #include <vcl/fontcapabilities.hxx> 40 #include <vcl/metric.hxx> 41 42 43 #include <fontinstance.hxx> 44 #include <impfontmetricdata.hxx> 45 #include <PhysicalFontFace.hxx> 46 #include <salgdi.hxx> 47 48 #include <quartz/salgdicommon.hxx> 49 #include <unordered_map> 50 #include <hb-ot.h> 51 52 #include <quartz/CGHelpers.hxx> 53 54 class AquaSalFrame; 55 class FontAttributes; 56 class XorEmulation; 57 58 // CoreText-specific physically available font face 59 class CoreTextFontFace : public PhysicalFontFace 60 { 61 public: 62 CoreTextFontFace( const FontAttributes&, sal_IntPtr nFontID ); 63 virtual ~CoreTextFontFace() override; 64 65 sal_IntPtr GetFontId() const override; 66 67 int GetFontTable( uint32_t nTagCode, unsigned char* ) const; 68 int GetFontTable( const char pTagName[5], unsigned char* ) const; 69 70 FontCharMapRef GetFontCharMap() const override; 71 bool GetFontCapabilities(vcl::FontCapabilities&) const override; 72 bool HasChar( sal_uInt32 cChar ) const; 73 74 rtl::Reference<LogicalFontInstance> CreateFontInstance(const FontSelectPattern&) const override; 75 76 private: 77 const sal_IntPtr mnFontId; 78 mutable FontCharMapRef mxCharMap; 79 mutable vcl::FontCapabilities maFontCapabilities; 80 mutable bool mbFontCapabilitiesRead; 81 }; 82 83 class CoreTextStyle final : public LogicalFontInstance 84 { 85 friend rtl::Reference<LogicalFontInstance> CoreTextFontFace::CreateFontInstance(const FontSelectPattern&) const; 86 87 public: 88 ~CoreTextStyle() override; 89 90 void GetFontMetric( ImplFontMetricDataRef const & ); 91 bool GetGlyphOutline(sal_GlyphId, basegfx::B2DPolyPolygon&, bool) const override; 92 GetStyleDict(void)93 CFMutableDictionaryRef GetStyleDict( void ) const { return mpStyleDict; } 94 95 /// <1.0: font is squeezed, >1.0 font is stretched, else 1.0 96 float mfFontStretch; 97 /// text rotation in radian 98 float mfFontRotation; 99 /// faux bold - true, if font doesn't have proper bold variants 100 bool mbFauxBold; 101 102 private: 103 explicit CoreTextStyle(const PhysicalFontFace&, const FontSelectPattern&); 104 105 hb_font_t* ImplInitHbFont() override; 106 bool ImplGetGlyphBoundRect(sal_GlyphId, tools::Rectangle&, bool) const override; 107 108 /// CoreText text style object 109 CFMutableDictionaryRef mpStyleDict; 110 }; 111 112 // TODO: move into cross-platform headers 113 114 class SystemFontList 115 { 116 public: 117 SystemFontList( void ); 118 ~SystemFontList( void ); 119 120 bool Init( void ); 121 void AddFont( CoreTextFontFace* ); 122 123 void AnnounceFonts( PhysicalFontCollection& ) const; 124 CoreTextFontFace* GetFontDataFromId( sal_IntPtr nFontId ) const; 125 126 private: 127 CTFontCollectionRef mpCTFontCollection; 128 CFArrayRef mpCTFontArray; 129 130 std::unordered_map<sal_IntPtr, rtl::Reference<CoreTextFontFace>> maFontContainer; 131 }; 132 133 namespace sal::aqua 134 { 135 float getWindowScaling(); 136 } 137 138 struct AquaSharedAttributes 139 { 140 /// path representing current clip region 141 CGMutablePathRef mxClipPath; 142 143 /// Drawing colors 144 /// pen color RGBA 145 RGBAColor maLineColor; 146 147 /// brush color RGBA 148 RGBAColor maFillColor; 149 150 // Graphics types 151 #ifdef MACOSX 152 AquaSalFrame* mpFrame; 153 /// is this a window graphics 154 bool mbWindow; 155 #else // IOS 156 // mirror AquaSalVirtualDevice::mbForeignContext for SvpSalGraphics objects related to such 157 bool mbForeignContext; 158 #endif 159 /// is this a printer graphics 160 bool mbPrinter; 161 /// is this a virtual device graphics 162 bool mbVirDev; 163 164 CGLayerHolder maLayer; // Quartz graphics layer 165 CGContextHolder maContextHolder; // Quartz drawing context 166 CGContextHolder maBGContextHolder; // Quartz drawing context for CGLayer 167 CGContextHolder maCSContextHolder; // Quartz drawing context considering the color space 168 int mnWidth; 169 int mnHeight; 170 int mnXorMode; // 0: off 1: on 2: invert only 171 int mnBitmapDepth; // zero unless bitmap 172 173 std::unique_ptr<XorEmulation> mpXorEmulation; 174 AquaSharedAttributesAquaSharedAttributes175 AquaSharedAttributes() 176 : mxClipPath(nullptr) 177 , maLineColor(COL_WHITE) 178 , maFillColor(COL_BLACK) 179 #ifdef MACOSX 180 , mpFrame(nullptr) 181 , mbWindow(false) 182 #else 183 , mbForeignContext(false) 184 #endif 185 , mbPrinter(false) 186 , mbVirDev(false) 187 , mnWidth(0) 188 , mnHeight(0) 189 , mnXorMode(0) 190 , mnBitmapDepth(0) 191 {} 192 unsetClipPathAquaSharedAttributes193 void unsetClipPath() 194 { 195 if (mxClipPath) 196 { 197 CGPathRelease(mxClipPath); 198 mxClipPath = nullptr; 199 } 200 } 201 unsetStateAquaSharedAttributes202 void unsetState() 203 { 204 unsetClipPath(); 205 } 206 207 bool checkContext(); 208 void setState(); 209 isPenVisibleAquaSharedAttributes210 bool isPenVisible() const 211 { 212 return maLineColor.IsVisible(); 213 } isBrushVisibleAquaSharedAttributes214 bool isBrushVisible() const 215 { 216 return maFillColor.IsVisible(); 217 } 218 refreshRectAquaSharedAttributes219 void refreshRect(float lX, float lY, float lWidth, float lHeight) 220 { 221 #ifdef MACOSX 222 if (!mbWindow) // view only on Window graphics 223 return; 224 225 if (mpFrame) 226 { 227 // update a little more around the designated rectangle 228 // this helps with antialiased rendering 229 // Rounding down x and width can accumulate a rounding error of up to 2 230 // The decrementing of x, the rounding error and the antialiasing border 231 // require that the width and the height need to be increased by four 232 const tools::Rectangle aVclRect( 233 Point(tools::Long(lX - 1), tools::Long(lY - 1)), 234 Size(tools::Long(lWidth + 4), tools::Long(lHeight + 4))); 235 236 mpFrame->maInvalidRect.Union(aVclRect); 237 } 238 #else 239 (void) lX; 240 (void) lY; 241 (void) lWidth; 242 (void) lHeight; 243 return; 244 #endif 245 } 246 247 // apply the XOR mask to the target context if active and dirty applyXorContextAquaSharedAttributes248 void applyXorContext() 249 { 250 if (!mpXorEmulation) 251 return; 252 if (mpXorEmulation->UpdateTarget()) 253 { 254 refreshRect(0, 0, mnWidth, mnHeight); // TODO: refresh minimal changerect 255 } 256 } 257 258 // differences between VCL, Quartz and kHiThemeOrientation coordinate systems 259 // make some graphics seem to be vertically-mirrored from a VCL perspective isFlippedAquaSharedAttributes260 bool isFlipped() const 261 { 262 #ifdef MACOSX 263 return mbWindow; 264 #else 265 return false; 266 #endif 267 } 268 }; 269 270 class AquaGraphicsBackend final : public SalGraphicsImpl 271 { 272 private: 273 AquaSharedAttributes& mrShared; 274 275 void drawPixelImpl( tools::Long nX, tools::Long nY, const RGBAColor& rColor); // helper to draw single pixels 276 277 #ifdef MACOSX refreshRect(const NSRect & rRect)278 void refreshRect(const NSRect& rRect) 279 { 280 mrShared.refreshRect(rRect.origin.x, rRect.origin.y, rRect.size.width, rRect.size.height); 281 } 282 #else refreshRect(const CGRect &)283 void refreshRect(const CGRect& /*rRect*/) 284 {} 285 #endif 286 287 void pattern50Fill(); 288 289 #ifdef MACOSX 290 void copyScaledArea(tools::Long nDestX, tools::Long nDestY, tools::Long nSrcX, tools::Long nSrcY, 291 tools::Long nSrcWidth, tools::Long nSrcHeight, AquaSharedAttributes* pSrcShared); 292 #endif 293 294 public: 295 AquaGraphicsBackend(AquaSharedAttributes & rShared); 296 ~AquaGraphicsBackend() override; 297 298 void Init() override; 299 300 void freeResources() override; 301 getRenderBackendName()302 OUString getRenderBackendName() const override 303 { 304 return "aqua"; 305 } 306 307 bool setClipRegion(vcl::Region const& rRegion) override; 308 void ResetClipRegion() override; 309 310 sal_uInt16 GetBitCount() const override; 311 312 tools::Long GetGraphicsWidth() const override; 313 314 void SetLineColor() override; 315 void SetLineColor(Color nColor) override; 316 void SetFillColor() override; 317 void SetFillColor(Color nColor) override; 318 void SetXORMode(bool bSet, bool bInvertOnly) override; 319 void SetROPLineColor(SalROPColor nROPColor) override; 320 void SetROPFillColor(SalROPColor nROPColor) override; 321 322 void drawPixel(tools::Long nX, tools::Long nY) override; 323 void drawPixel(tools::Long nX, tools::Long nY, Color nColor) override; 324 325 void drawLine(tools::Long nX1, tools::Long nY1, tools::Long nX2, tools::Long nY2) override; 326 void drawRect(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight) override; 327 void drawPolyLine(sal_uInt32 nPoints, const Point* pPointArray) override; 328 void drawPolygon(sal_uInt32 nPoints, const Point* pPointArray) override; 329 void drawPolyPolygon(sal_uInt32 nPoly, const sal_uInt32* pPoints, 330 const Point** pPointArray) override; 331 332 bool drawPolyPolygon(const basegfx::B2DHomMatrix& rObjectToDevice, 333 const basegfx::B2DPolyPolygon&, double fTransparency) override; 334 335 bool drawPolyLine(const basegfx::B2DHomMatrix& rObjectToDevice, const basegfx::B2DPolygon&, 336 double fTransparency, double fLineWidth, const std::vector<double>* pStroke, 337 basegfx::B2DLineJoin, css::drawing::LineCap, double fMiterMinimumAngle, 338 bool bPixelSnapHairline) override; 339 340 bool drawPolyLineBezier(sal_uInt32 nPoints, const Point* pPointArray, 341 const PolyFlags* pFlagArray) override; 342 343 bool drawPolygonBezier(sal_uInt32 nPoints, const Point* pPointArray, 344 const PolyFlags* pFlagArray) override; 345 346 bool drawPolyPolygonBezier(sal_uInt32 nPoly, const sal_uInt32* pPoints, 347 const Point* const* pPointArray, 348 const PolyFlags* const* pFlagArray) override; 349 350 void copyArea(tools::Long nDestX, tools::Long nDestY, tools::Long nSrcX, tools::Long nSrcY, 351 tools::Long nSrcWidth, tools::Long nSrcHeight, bool bWindowInvalidate) override; 352 353 void copyBits(const SalTwoRect& rPosAry, SalGraphics* pSrcGraphics) override; 354 355 void drawBitmap(const SalTwoRect& rPosAry, const SalBitmap& rSalBitmap) override; 356 357 void drawBitmap(const SalTwoRect& rPosAry, const SalBitmap& rSalBitmap, 358 const SalBitmap& rMaskBitmap) override; 359 360 void drawMask(const SalTwoRect& rPosAry, const SalBitmap& rSalBitmap, 361 Color nMaskColor) override; 362 363 std::shared_ptr<SalBitmap> getBitmap(tools::Long nX, tools::Long nY, tools::Long nWidth, 364 tools::Long nHeight) override; 365 366 Color getPixel(tools::Long nX, tools::Long nY) override; 367 368 void invert(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, 369 SalInvert nFlags) override; 370 371 void invert(sal_uInt32 nPoints, const Point* pPtAry, SalInvert nFlags) override; 372 373 bool drawEPS(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, 374 void* pPtr, sal_uInt32 nSize) override; 375 376 bool blendBitmap(const SalTwoRect&, const SalBitmap& rBitmap) override; 377 378 bool blendAlphaBitmap(const SalTwoRect&, const SalBitmap& rSrcBitmap, 379 const SalBitmap& rMaskBitmap, const SalBitmap& rAlphaBitmap) override; 380 381 bool drawAlphaBitmap(const SalTwoRect&, const SalBitmap& rSourceBitmap, 382 const SalBitmap& rAlphaBitmap) override; 383 384 bool drawTransformedBitmap(const basegfx::B2DPoint& rNull, const basegfx::B2DPoint& rX, 385 const basegfx::B2DPoint& rY, const SalBitmap& rSourceBitmap, 386 const SalBitmap* pAlphaBitmap, double fAlpha) override; 387 388 bool hasFastDrawTransformedBitmap() const override; 389 390 bool drawAlphaRect(tools::Long nX, tools::Long nY, tools::Long nWidth, tools::Long nHeight, 391 sal_uInt8 nTransparency) override; 392 393 bool drawGradient(const tools::PolyPolygon& rPolygon, const Gradient& rGradient) override; 394 bool implDrawGradient(basegfx::B2DPolyPolygon const& rPolyPolygon, 395 SalGradient const& rGradient) override; 396 397 bool supportsOperation(OutDevSupportType eType) const override; 398 }; 399 400 class AquaSalGraphics : public SalGraphicsAutoDelegateToImpl 401 { 402 AquaSharedAttributes maShared; 403 std::unique_ptr<AquaGraphicsBackend> mpBackend; 404 405 /// device resolution of this graphics 406 sal_Int32 mnRealDPIX; 407 sal_Int32 mnRealDPIY; 408 409 // Device Font settings 410 rtl::Reference<CoreTextStyle> mpTextStyle[MAX_FALLBACK]; 411 RGBAColor maTextColor; 412 /// allows text to be rendered without antialiasing 413 bool mbNonAntialiasedText; 414 415 public: 416 AquaSalGraphics(); 417 virtual ~AquaSalGraphics() override; 418 419 void SetVirDevGraphics(CGLayerHolder const &rLayer, CGContextRef, int nBitDepth = 0); 420 #ifdef MACOSX 421 void initResolution( NSWindow* ); 422 void copyResolution( AquaSalGraphics& ); 423 void updateResolution(); 424 425 void SetWindowGraphics( AquaSalFrame* pFrame ); IsWindowGraphics()426 bool IsWindowGraphics() const { return maShared.mbWindow; } 427 void SetPrinterGraphics(CGContextRef, sal_Int32 nRealDPIX, sal_Int32 nRealDPIY); getGraphicsFrame()428 AquaSalFrame* getGraphicsFrame() const { return maShared.mpFrame; } setGraphicsFrame(AquaSalFrame * pFrame)429 void setGraphicsFrame( AquaSalFrame* pFrame ) { maShared.mpFrame = pFrame; } 430 #endif 431 432 #ifdef MACOSX 433 void UpdateWindow( NSRect& ); // delivered in NSView coordinates RefreshRect(const NSRect & rRect)434 void RefreshRect(const NSRect& rRect) 435 { 436 maShared.refreshRect(rRect.origin.x, rRect.origin.y, rRect.size.width, rRect.size.height); 437 } 438 #else RefreshRect(const CGRect &)439 void RefreshRect( const CGRect& ) {} 440 #endif 441 442 void UnsetState(); 443 // InvalidateContext does an UnsetState and sets mrContext to 0 444 void InvalidateContext(); 445 getAquaGraphicsBackend()446 AquaGraphicsBackend* getAquaGraphicsBackend() const 447 { 448 return mpBackend.get(); 449 } 450 451 virtual SalGraphicsImpl* GetImpl() const override; 452 453 #ifdef MACOSX 454 455 protected: 456 457 // native widget rendering methods that require mirroring 458 459 virtual bool isNativeControlSupported( ControlType nType, ControlPart nPart ) override; 460 461 virtual bool hitTestNativeControl( ControlType nType, ControlPart nPart, const tools::Rectangle& rControlRegion, 462 const Point& aPos, bool& rIsInside ) override; 463 virtual bool drawNativeControl( ControlType nType, ControlPart nPart, const tools::Rectangle& rControlRegion, 464 ControlState nState, const ImplControlValue& aValue, 465 const OUString& aCaption, const Color& rBackgroundColor ) override; 466 virtual bool getNativeControlRegion( ControlType nType, ControlPart nPart, const tools::Rectangle& rControlRegion, ControlState nState, 467 const ImplControlValue& aValue, const OUString& aCaption, 468 tools::Rectangle &rNativeBoundingRegion, tools::Rectangle &rNativeContentRegion ) override; 469 #endif 470 471 public: 472 // get device resolution 473 virtual void GetResolution( sal_Int32& rDPIX, sal_Int32& rDPIY ) override; 474 // set the text color to a specific color 475 virtual void SetTextColor( Color nColor ) override; 476 // set the font 477 virtual void SetFont( LogicalFontInstance*, int nFallbackLevel ) override; 478 // get the current font's metrics 479 virtual void GetFontMetric( ImplFontMetricDataRef&, int nFallbackLevel ) override; 480 // get the repertoire of the current font 481 virtual FontCharMapRef GetFontCharMap() const override; 482 virtual bool GetFontCapabilities(vcl::FontCapabilities &rFontCapabilities) const override; 483 // graphics must fill supplied font list 484 virtual void GetDevFontList( PhysicalFontCollection* ) override; 485 // graphics must drop any cached font info 486 virtual void ClearDevFontCache() override; 487 virtual bool AddTempDevFont( PhysicalFontCollection*, const OUString& rFileURL, const OUString& rFontName ) override; 488 // CreateFontSubset: a method to get a subset of glyhps of a font 489 // inside a new valid font file 490 // returns TRUE if creation of subset was successful 491 // parameters: rToFile: contains an osl file URL to write the subset to 492 // pFont: describes from which font to create a subset 493 // pGlyphIDs: the glyph ids to be extracted 494 // pEncoding: the character code corresponding to each glyph 495 // pWidths: the advance widths of the corresponding glyphs (in PS font units) 496 // nGlyphs: the number of glyphs 497 // rInfo: additional outgoing information 498 // implementation note: encoding 0 with glyph id 0 should be added implicitly 499 // as "undefined character" 500 virtual bool CreateFontSubset( const OUString& rToFile, 501 const PhysicalFontFace* pFont, 502 const sal_GlyphId* pGlyphIds, 503 const sal_uInt8* pEncoding, 504 sal_Int32* pWidths, 505 int nGlyphs, 506 FontSubsetInfo& rInfo // out parameter 507 ) override; 508 509 // GetEmbedFontData: gets the font data for a font marked 510 // embeddable by GetDevFontList or NULL in case of error 511 // parameters: pFont: describes the font in question 512 // pDataLen: out parameter, contains the byte length of the returned buffer 513 virtual const void* GetEmbedFontData(const PhysicalFontFace*, tools::Long* pDataLen) 514 override; 515 // frees the font data again 516 virtual void FreeEmbedFontData( const void* pData, tools::Long nDataLen ) override; 517 518 virtual void GetGlyphWidths( const PhysicalFontFace*, 519 bool bVertical, 520 std::vector< sal_Int32 >& rWidths, 521 Ucs2UIntMap& rUnicodeEnc ) override; 522 523 virtual std::unique_ptr<GenericSalLayout> 524 GetTextLayout(int nFallbackLevel) override; 525 virtual void DrawTextLayout( const GenericSalLayout& ) override; 526 527 virtual SystemGraphicsData 528 GetGraphicsData() const override; 529 530 private: 531 UInt32 getState( ControlState nState ); 532 UInt32 getTrackState( ControlState nState ); 533 static bool GetRawFontData( const PhysicalFontFace* pFontData, 534 std::vector<unsigned char>& rBuffer, 535 bool* pJustCFF ); 536 }; 537 538 539 #endif // INCLUDED_VCL_INC_QUARTZ_SALGDI_H 540 541 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 542