1 | unit BGRAfpGUIBitmap;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | SysUtils, Classes, BGRAGraphics, BGRABitmapTypes, BGRADefaultBitmap,
|
---|
9 | BGRAFreeType, EasyLazFreeType, LazFreeTypeFontCollection,
|
---|
10 | BGRACanvas;
|
---|
11 |
|
---|
12 | type
|
---|
13 |
|
---|
14 | { TBGRAfpGUIBitmap }
|
---|
15 |
|
---|
16 | TBGRAfpGUIBitmap = class(TBGRADefaultBitmap)
|
---|
17 | private
|
---|
18 | FPseudoCanvas: TBGRACanvas;
|
---|
19 | function GetPseudoCanvas: TBGRACanvas;
|
---|
20 | function GetBitmapTransparent: boolean;
|
---|
21 | procedure SetBitmapTransparent(AValue: boolean);
|
---|
22 | protected
|
---|
23 | procedure RebuildBitmap; override;
|
---|
24 | function CreateDefaultFontRenderer: TBGRACustomFontRenderer; override;
|
---|
25 | function LoadFromRawImage(ARawImage: TRawImage; DefaultOpacity: byte;
|
---|
26 | AlwaysReplaceAlpha: boolean=False; {%H-}RaiseErrorOnInvalidPixelFormat: boolean
|
---|
27 | =True): boolean; override;
|
---|
28 | procedure Init; override;
|
---|
29 | procedure FreeData; override;
|
---|
30 | procedure ReallocData; override;
|
---|
31 | procedure FreeBitmap; override;
|
---|
32 | procedure NotAvailable;
|
---|
33 | public
|
---|
34 | destructor Destroy; override;
|
---|
35 | class procedure AddFreeTypeFontFolder(ADirectory: string; AUTF8: boolean = false);
|
---|
36 | class procedure AddFreeTypeFontFile(AFilename: string; AUTF8: boolean = false);
|
---|
37 | procedure Draw(ACanvas: TCanvas; x, y: integer; {%H-}Opaque: boolean=True); override;
|
---|
38 | procedure Draw(ACanvas: TCanvas; Rect: TRect; {%H-}Opaque: boolean=True); override;
|
---|
39 | procedure Draw(ACanvas: TGUICanvas; x, y: integer; {%H-}Opaque: boolean=True); overload;
|
---|
40 | procedure Draw(ACanvas: TGUICanvas; Rect: TRect; {%H-}Opaque: boolean=True); overload;
|
---|
41 | procedure GetImageFromCanvas({%H-}CanvasSource: TCanvas; {%H-}x, {%H-}y: integer); override; //not available
|
---|
42 | procedure DataDrawTransparent(ACanvas: TCanvas; Rect: TRect; AData: Pointer;
|
---|
43 | ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer); override;
|
---|
44 | procedure DataDrawOpaque(ACanvas: TCanvas; Rect: TRect; AData: Pointer;
|
---|
45 | ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer); override;
|
---|
46 | procedure TakeScreenshot({%H-}ARect: TRect); override; //not available
|
---|
47 | procedure TakeScreenshotOfPrimaryMonitor; override; //not available
|
---|
48 | procedure LoadFromDevice({%H-}DC: HDC); override; //not available
|
---|
49 | procedure LoadFromDevice({%H-}DC: HDC; {%H-}ARect: TRect); override; //not available
|
---|
50 | property BitmapTransparent: boolean read GetBitmapTransparent write SetBitmapTransparent;
|
---|
51 | property Canvas: TBGRACanvas read GetPseudoCanvas;
|
---|
52 | end;
|
---|
53 |
|
---|
54 | implementation
|
---|
55 |
|
---|
56 | { TBGRAfpGUIBitmap }
|
---|
57 |
|
---|
58 | function TBGRAfpGUIBitmap.GetBitmapTransparent: boolean;
|
---|
59 | begin
|
---|
60 | result := FBitmap.Transparent;
|
---|
61 | end;
|
---|
62 |
|
---|
63 | function TBGRAfpGUIBitmap.GetPseudoCanvas: TBGRACanvas;
|
---|
64 | begin
|
---|
65 | if FPseudoCanvas = nil then
|
---|
66 | begin
|
---|
67 | FPseudoCanvas := TBGRACanvas.Create(self);
|
---|
68 | FPseudoCanvas.AntialiasingMode := amOff;
|
---|
69 | end;
|
---|
70 | result := FPseudoCanvas;
|
---|
71 | end;
|
---|
72 |
|
---|
73 | procedure TBGRAfpGUIBitmap.SetBitmapTransparent(AValue: boolean);
|
---|
74 | begin
|
---|
75 | if FBitmap.Transparent <> AValue then
|
---|
76 | begin
|
---|
77 | FBitmap.Transparent:= AValue;
|
---|
78 | InvalidateBitmap;
|
---|
79 | end;
|
---|
80 | end;
|
---|
81 |
|
---|
82 | procedure TBGRAfpGUIBitmap.RebuildBitmap;
|
---|
83 | var pmask, pmaskline: PByte;
|
---|
84 | pdata: PBGRAPixel;
|
---|
85 | raw: TRawImage;
|
---|
86 | x,y,bit,masklinesize,curmaskbyte: NativeUInt;
|
---|
87 | begin
|
---|
88 | if FBitmap.Transparent then
|
---|
89 | begin
|
---|
90 | raw := FBitmap.RawImage;
|
---|
91 | masklinesize := ((Width+31) div 32)*4;
|
---|
92 | pmaskline := FBitmap.RawImage.MaskData;
|
---|
93 | pdata := raw.ImageData;
|
---|
94 | for y := 0 to Height-1 do
|
---|
95 | begin
|
---|
96 | pmask:= pmaskline;
|
---|
97 | bit := $80;
|
---|
98 | curmaskbyte := 0;
|
---|
99 | for x := Width-1 downto 0 do
|
---|
100 | begin
|
---|
101 | if pdata^.alpha >= $80 then
|
---|
102 | curmaskbyte := curmaskbyte or bit;
|
---|
103 | bit := bit shr 1;
|
---|
104 | if bit = 0 then
|
---|
105 | begin
|
---|
106 | bit := $80;
|
---|
107 | pmask^ := curmaskbyte;
|
---|
108 | inc(pmask);
|
---|
109 | curmaskbyte := 0;
|
---|
110 | end;
|
---|
111 | inc(pdata);
|
---|
112 | end;
|
---|
113 | if bit <> $80 then
|
---|
114 | pmask^ := curmaskbyte;
|
---|
115 | inc(pmaskline, masklinesize);
|
---|
116 | end;
|
---|
117 | end;
|
---|
118 | FBitmap.RawImage.UpdateImage;
|
---|
119 | end;
|
---|
120 |
|
---|
121 | function TBGRAfpGUIBitmap.CreateDefaultFontRenderer: TBGRACustomFontRenderer;
|
---|
122 | begin
|
---|
123 | result := TBGRAFreeTypeFontRenderer.Create;
|
---|
124 | end;
|
---|
125 |
|
---|
126 | function TBGRAfpGUIBitmap.LoadFromRawImage(ARawImage: TRawImage;
|
---|
127 | DefaultOpacity: byte; AlwaysReplaceAlpha: boolean;
|
---|
128 | RaiseErrorOnInvalidPixelFormat: boolean): boolean;
|
---|
129 | var
|
---|
130 | lineSize: integer;
|
---|
131 | y: Integer;
|
---|
132 | begin
|
---|
133 | if (ARawImage.Width <> Width) or
|
---|
134 | (ARawImage.Height <> Height) then
|
---|
135 | raise Exception.Create('Bitmap size is inconsistant');
|
---|
136 |
|
---|
137 | lineSize := Width*sizeof(TBGRAPixel);
|
---|
138 | for y := 0 to Height-1 do
|
---|
139 | move(ARawImage.ScanLine[y]^, ScanLine[y]^, lineSize);
|
---|
140 | if AlwaysReplaceAlpha then AlphaFill(DefaultOpacity);
|
---|
141 | result := true;
|
---|
142 | end;
|
---|
143 |
|
---|
144 | procedure TBGRAfpGUIBitmap.Init;
|
---|
145 | begin
|
---|
146 | inherited Init;
|
---|
147 | FBitmap := TBitmap.Create;
|
---|
148 | FontAntialias:= true;
|
---|
149 | end;
|
---|
150 |
|
---|
151 | procedure TBGRAfpGUIBitmap.ReallocData;
|
---|
152 | begin
|
---|
153 | FBitmap.Width := Width;
|
---|
154 | FBitmap.Height:= Height;
|
---|
155 | FData := FBitmap.RawImage.ImageData;
|
---|
156 | InvalidateBitmap;
|
---|
157 | FScanPtr := nil;
|
---|
158 | end;
|
---|
159 |
|
---|
160 | procedure TBGRAfpGUIBitmap.FreeData;
|
---|
161 | begin
|
---|
162 | //nothing
|
---|
163 | end;
|
---|
164 |
|
---|
165 | procedure TBGRAfpGUIBitmap.FreeBitmap;
|
---|
166 | begin
|
---|
167 | //nothing
|
---|
168 | end;
|
---|
169 |
|
---|
170 | procedure TBGRAfpGUIBitmap.NotAvailable;
|
---|
171 | begin
|
---|
172 | raise exception.Create('Function not available with fpGUI');
|
---|
173 | end;
|
---|
174 |
|
---|
175 | destructor TBGRAfpGUIBitmap.Destroy;
|
---|
176 | begin
|
---|
177 | FreeAndNil(FBitmap);
|
---|
178 | FreeAndNil(FPseudoCanvas);
|
---|
179 | inherited Destroy;
|
---|
180 | end;
|
---|
181 |
|
---|
182 | class procedure TBGRAfpGUIBitmap.AddFreeTypeFontFolder(ADirectory: string; AUTF8: boolean);
|
---|
183 | begin
|
---|
184 | if AUTF8 then ADirectory:= Utf8ToAnsi(ADirectory);
|
---|
185 | EasyLazFreeType.FontCollection.AddFolder(ADirectory);
|
---|
186 | end;
|
---|
187 |
|
---|
188 | class procedure TBGRAfpGUIBitmap.AddFreeTypeFontFile(AFilename: string; AUTF8: boolean);
|
---|
189 | begin
|
---|
190 | if AUTF8 then AFilename:= Utf8ToAnsi(AFilename);
|
---|
191 | EasyLazFreeType.FontCollection.AddFile(AFilename);
|
---|
192 | end;
|
---|
193 |
|
---|
194 | procedure TBGRAfpGUIBitmap.Draw(ACanvas: TCanvas; x, y: integer; Opaque: boolean);
|
---|
195 | begin
|
---|
196 | Draw(ACanvas.GUICanvas, x, y, Opaque);
|
---|
197 | end;
|
---|
198 |
|
---|
199 | procedure TBGRAfpGUIBitmap.Draw(ACanvas: TCanvas; Rect: TRect; Opaque: boolean);
|
---|
200 | begin
|
---|
201 | Draw(ACanvas.GUICanvas, Rect, Opaque);
|
---|
202 | end;
|
---|
203 |
|
---|
204 | procedure TBGRAfpGUIBitmap.Draw(ACanvas: TGUICanvas; x, y: integer;
|
---|
205 | Opaque: boolean);
|
---|
206 | begin
|
---|
207 | BitmapTransparent := not Opaque;
|
---|
208 | ACanvas.DrawImage(x,y, Bitmap.RawImage);
|
---|
209 | end;
|
---|
210 |
|
---|
211 | procedure TBGRAfpGUIBitmap.Draw(ACanvas: TGUICanvas; Rect: TRect;
|
---|
212 | Opaque: boolean);
|
---|
213 | begin
|
---|
214 | BitmapTransparent := not Opaque;
|
---|
215 | ACanvas.StretchDraw(rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top, FBitmap.RawImage);
|
---|
216 | end;
|
---|
217 |
|
---|
218 | procedure TBGRAfpGUIBitmap.GetImageFromCanvas(CanvasSource: TCanvas; x,
|
---|
219 | y: integer);
|
---|
220 | begin
|
---|
221 | NotAvailable;
|
---|
222 | end;
|
---|
223 |
|
---|
224 | procedure TBGRAfpGUIBitmap.DataDrawTransparent(ACanvas: TCanvas; Rect: TRect;
|
---|
225 | AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
|
---|
226 | var temp: TBGRAfpGUIBitmap;
|
---|
227 | begin
|
---|
228 | temp := TBGRAfpGUIBitmap.Create(AWidth,AHeight);
|
---|
229 | move(AData^, temp.Data^, temp.NbPixels*sizeof(TBGRAPixel));
|
---|
230 | if ALineOrder <> temp.LineOrder then temp.VerticalFlip;
|
---|
231 | temp.Draw(ACanvas, Rect, False);
|
---|
232 | temp.Free;
|
---|
233 | end;
|
---|
234 |
|
---|
235 | procedure TBGRAfpGUIBitmap.DataDrawOpaque(ACanvas: TCanvas; Rect: TRect;
|
---|
236 | AData: Pointer; ALineOrder: TRawImageLineOrder; AWidth, AHeight: integer);
|
---|
237 | var temp: TBGRAfpGUIBitmap;
|
---|
238 | begin
|
---|
239 | temp := TBGRAfpGUIBitmap.Create(AWidth,AHeight);
|
---|
240 | move(AData^, temp.Data^, temp.NbPixels*sizeof(TBGRAPixel));
|
---|
241 | if ALineOrder <> temp.LineOrder then temp.VerticalFlip;
|
---|
242 | temp.Draw(ACanvas, Rect, True);
|
---|
243 | temp.Free;
|
---|
244 | end;
|
---|
245 |
|
---|
246 | procedure TBGRAfpGUIBitmap.TakeScreenshot(ARect: TRect);
|
---|
247 | begin
|
---|
248 | NotAvailable;
|
---|
249 | end;
|
---|
250 |
|
---|
251 | procedure TBGRAfpGUIBitmap.TakeScreenshotOfPrimaryMonitor;
|
---|
252 | begin
|
---|
253 | NotAvailable;
|
---|
254 | end;
|
---|
255 |
|
---|
256 | procedure TBGRAfpGUIBitmap.LoadFromDevice(DC: HDC);
|
---|
257 | begin
|
---|
258 | NotAvailable;
|
---|
259 | end;
|
---|
260 |
|
---|
261 | procedure TBGRAfpGUIBitmap.LoadFromDevice(DC: HDC; ARect: TRect);
|
---|
262 | begin
|
---|
263 | NotAvailable;
|
---|
264 | end;
|
---|
265 |
|
---|
266 | end.
|
---|
267 |
|
---|