GR32_Backends.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. unit GR32_Backends;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is Backend Extension for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Andre Beckedorf - metaException
  26. * [email protected]
  27. *
  28. * Portions created by the Initial Developer are Copyright (C) 2007-2009
  29. * the Initial Developer. All Rights Reserved.
  30. *
  31. * Contributor(s):
  32. *
  33. * ***** END LICENSE BLOCK ***** *)
  34. interface
  35. {$I GR32.inc}
  36. uses
  37. {$IFDEF FPC}
  38. LCLIntf, LCLType, Types, Controls, Graphics,
  39. {$ELSE}
  40. Windows, Messages, Controls, Graphics,
  41. {$ENDIF}
  42. Classes, SysUtils, GR32, GR32_Containers, GR32_Image, GR32_Paths;
  43. type
  44. EBackend = class(Exception);
  45. ITextSupport = interface(IUnknown)
  46. ['{225997CC-958A-423E-8B60-9EDE0D3B53B5}']
  47. procedure Textout(X, Y: Integer; const Text: String); overload;
  48. procedure Textout(X, Y: Integer; const ClipRect: TRect; const Text: String); overload;
  49. procedure Textout(var DstRect: TRect; const Flags: Cardinal; const Text: String); overload;
  50. function TextExtent(const Text: String): TSize;
  51. end;
  52. IFontSupport = interface(IUnknown)
  53. ['{67C73044-1EFF-4FDE-AEA2-56BFADA50A48}']
  54. function GetOnFontChange: TNotifyEvent;
  55. procedure SetOnFontChange(Handler: TNotifyEvent);
  56. function GetFont: TFont;
  57. procedure SetFont(const Font: TFont);
  58. procedure UpdateFont;
  59. property Font: TFont read GetFont write SetFont;
  60. property OnFontChange: TNotifyEvent read GetOnFontChange write SetOnFontChange;
  61. end;
  62. ITextToPathSupport = interface(IUnknown)
  63. ['{6C4037E4-FF4D-4EE2-9C20-B9DB9C64B42D}']
  64. procedure TextToPath(Path: TCustomPath; const X, Y: TFloat; const Text: string); overload;
  65. procedure TextToPath(Path: TCustomPath; const DstRect: TFloatRect; const Text: string; Flags: Cardinal); overload;
  66. function MeasureText(const DstRect: TFloatRect; const Text: string; Flags: Cardinal): TFloatRect;
  67. end;
  68. ICanvasSupport = interface(IUnknown)
  69. ['{5ACFEEC7-0123-4AD8-8AE6-145718438E01}']
  70. function GetCanvasChange: TNotifyEvent;
  71. procedure SetCanvasChange(Handler: TNotifyEvent);
  72. function GetCanvas: TCanvas;
  73. procedure DeleteCanvas;
  74. function CanvasAllocated: Boolean;
  75. property Canvas: TCanvas read GetCanvas;
  76. property OnCanvasChange: TNotifyEvent read GetCanvasChange write SetCanvasChange;
  77. end;
  78. IInteroperabilitySupport = interface(IUnknown)
  79. ['{B86229E8-228F-4FDA-A47D-2E9BD9F78D1E}']
  80. function CopyFrom(Graphic: TGraphic): Boolean; overload;
  81. end;
  82. IDeviceContextSupport = interface(IUnknown)
  83. ['{DD1109DA-4019-4A5C-A450-3631A73CF288}']
  84. function GetHandle: HDC;
  85. procedure Draw(const DstRect, SrcRect: TRect; hSrc: HDC);
  86. procedure DrawTo(hDst: HDC; DstX, DstY: Integer); overload;
  87. procedure DrawTo(hDst: HDC; const DstRect, SrcRect: TRect); overload;
  88. property Handle: HDC read GetHandle;
  89. end;
  90. IBitmapContextSupport = interface(IUnknown)
  91. ['{DF0F9475-BA13-4C6B-81C3-D138624C4D08}']
  92. function GetBitmapInfo: TBitmapInfo;
  93. function GetBitmapHandle: THandle;
  94. property BitmapInfo: TBitmapInfo read GetBitmapInfo;
  95. property BitmapHandle: THandle read GetBitmapHandle;
  96. end;
  97. IPaintSupport = interface(IUnknown)
  98. ['{CE64DBEE-C4A9-4E8E-ABCA-1B1FD6F45924}']
  99. procedure ImageNeeded;
  100. procedure CheckPixmap;
  101. procedure DoPaint(ABuffer: TBitmap32; AInvalidRects: TRectList; ACanvas: TCanvas; APaintBox: TCustomPaintBox32);
  102. end;
  103. TRequireOperatorMode = (romAnd, romOr);
  104. // Helper functions to temporarily switch the back-end depending on the required interfaces
  105. procedure RequireBackendSupport(TargetBitmap: TCustomBitmap32;
  106. RequiredInterfaces: array of TGUID;
  107. Mode: TRequireOperatorMode; UseOptimizedDestructiveSwitchMethod: Boolean;
  108. out ReleasedBackend: TCustomBackend);
  109. procedure RestoreBackend(TargetBitmap: TCustomBitmap32; const SavedBackend: TCustomBackend);
  110. resourcestring
  111. RCStrCannotAllocateDIBHandle = 'Can''t allocate the DIB handle';
  112. RCStrCannotCreateCompatibleDC = 'Can''t create compatible DC';
  113. RCStrCannotSelectAnObjectIntoDC = 'Can''t select an object into DC';
  114. implementation
  115. procedure RequireBackendSupport(TargetBitmap: TCustomBitmap32;
  116. RequiredInterfaces: array of TGUID;
  117. Mode: TRequireOperatorMode; UseOptimizedDestructiveSwitchMethod: Boolean;
  118. out ReleasedBackend: TCustomBackend);
  119. var
  120. I: Integer;
  121. Supported: Boolean;
  122. begin
  123. Supported := False;
  124. for I := Low(RequiredInterfaces) to High(RequiredInterfaces) do
  125. begin
  126. Supported := Supports(TargetBitmap.Backend, RequiredInterfaces[I]);
  127. if ((Mode = romAnd) and not Supported) or
  128. ((Mode = romOr) and Supported) then
  129. Break;
  130. end;
  131. if not Supported then
  132. begin
  133. if UseOptimizedDestructiveSwitchMethod then
  134. TargetBitmap.SetSize(0, 0); // Reset size so we avoid the buffer copy during back-end switch
  135. ReleasedBackend := TargetBitmap.ReleaseBackend;
  136. // TODO: Try to find a back-end that supports the required interfaces
  137. // instead of resorting to the default platform back-end class...
  138. TargetBitmap.Backend := TargetBitmap.GetPlatformBackendClass.Create;
  139. end
  140. else
  141. ReleasedBackend := nil;
  142. end;
  143. procedure RestoreBackend(TargetBitmap: TCustomBitmap32; const SavedBackend: TCustomBackend);
  144. begin
  145. if Assigned(SavedBackend) then
  146. TargetBitmap.Backend := SavedBackend;
  147. end;
  148. end.