1 | unit BGRAGraphics;
|
---|
2 | {=== Types imported from Graphics ===}
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | {$I bgrabitmap.inc}
|
---|
8 |
|
---|
9 | {$IFDEF BGRABITMAP_USE_LCL}
|
---|
10 | uses Graphics, GraphType, FPImage;
|
---|
11 |
|
---|
12 | type
|
---|
13 | PColor = Graphics.PColor;
|
---|
14 | TColor = Graphics.TColor;
|
---|
15 | TAntialiasingMode = Graphics.TAntialiasingMode;
|
---|
16 | TGradientDirection = Graphics.TGradientDirection;
|
---|
17 | TPenEndCap = Graphics.TPenEndCap;
|
---|
18 | TPenJoinStyle = Graphics.TPenJoinStyle;
|
---|
19 | TPenStyle = Graphics.TPenStyle;
|
---|
20 |
|
---|
21 | const
|
---|
22 | amDontCare = Graphics.amDontCare;
|
---|
23 | amOn = Graphics.amOn;
|
---|
24 | amOff = Graphics.amOff;
|
---|
25 |
|
---|
26 | gdVertical = Graphics.gdVertical;
|
---|
27 | gdHorizontal = Graphics.gdHorizontal;
|
---|
28 |
|
---|
29 | pecRound = Graphics.pecRound;
|
---|
30 | pecSquare = Graphics.pecSquare;
|
---|
31 | pecFlat = Graphics.pecFlat;
|
---|
32 |
|
---|
33 | pjsRound = Graphics.pjsRound;
|
---|
34 | pjsBevel = Graphics.pjsBevel;
|
---|
35 | pjsMiter = Graphics.pjsMiter;
|
---|
36 |
|
---|
37 | psSolid = Graphics.psSolid;
|
---|
38 | psDash = Graphics.psDash;
|
---|
39 | psDot = Graphics.psDot;
|
---|
40 | psDashDot = Graphics.psDashDot;
|
---|
41 | psDashDotDot = Graphics.psDashDotDot;
|
---|
42 | psClear = Graphics.psClear;
|
---|
43 | psInsideframe = Graphics.psInsideframe;
|
---|
44 | psPattern = Graphics.psPattern;
|
---|
45 |
|
---|
46 | tmAuto = Graphics.tmAuto;
|
---|
47 | tmFixed = Graphics.tmFixed;
|
---|
48 |
|
---|
49 | type
|
---|
50 | TPen = Graphics.TPen;
|
---|
51 | TTextLayout = Graphics.TTextLayout;
|
---|
52 | TTextStyle = Graphics.TTextStyle;
|
---|
53 |
|
---|
54 | TFillStyle = Graphics.TFillStyle;
|
---|
55 | TFillMode = Graphics.TFillMode;
|
---|
56 | TBrushStyle = Graphics.TBrushStyle;
|
---|
57 |
|
---|
58 | const
|
---|
59 | tlTop = Graphics.tlTop;
|
---|
60 | tlCenter = Graphics.tlCenter;
|
---|
61 | tlBottom = Graphics.tlBottom;
|
---|
62 |
|
---|
63 | fsSurface = GraphType.fsSurface;
|
---|
64 | fsBorder = GraphType.fsBorder;
|
---|
65 |
|
---|
66 | fmAlternate = Graphics.fmAlternate;
|
---|
67 | fmWinding = Graphics.fmWinding;
|
---|
68 |
|
---|
69 | bsSolid = Graphics.bsSolid;
|
---|
70 | bsClear = Graphics.bsClear;
|
---|
71 | bsHorizontal = Graphics.bsHorizontal;
|
---|
72 | bsVertical = Graphics.bsVertical;
|
---|
73 | bsFDiagonal = Graphics.bsFDiagonal;
|
---|
74 | bsBDiagonal = Graphics.bsBDiagonal;
|
---|
75 | bsCross = Graphics.bsCross;
|
---|
76 | bsDiagCross = Graphics.bsDiagCross;
|
---|
77 |
|
---|
78 | type
|
---|
79 | TBrush = Graphics.TBrush;
|
---|
80 | TCanvas = Graphics.TCanvas;
|
---|
81 | TGraphic = Graphics.TGraphic;
|
---|
82 | TRawImage = GraphType.TRawImage;
|
---|
83 | TBitmap = Graphics.TBitmap;
|
---|
84 |
|
---|
85 | TRasterImage = Graphics.TRasterImage;
|
---|
86 |
|
---|
87 | TFontStyle = Graphics.TFontStyle;
|
---|
88 | TFontStyles = Graphics.TFontStyles;
|
---|
89 | TFontQuality = Graphics.TFontQuality;
|
---|
90 |
|
---|
91 | type
|
---|
92 | TFont = Graphics.TFont;
|
---|
93 |
|
---|
94 | const
|
---|
95 | fsBold = Graphics.fsBold;
|
---|
96 | fsItalic = Graphics.fsItalic;
|
---|
97 | fsStrikeOut = Graphics.fsStrikeOut;
|
---|
98 | fsUnderline = Graphics.fsUnderline;
|
---|
99 |
|
---|
100 | fqDefault = Graphics.fqDefault;
|
---|
101 | fqDraft = Graphics.fqDraft;
|
---|
102 | fqProof = Graphics.fqProof;
|
---|
103 | fqNonAntialiased = Graphics.fqNonAntialiased;
|
---|
104 | fqAntialiased = Graphics.fqAntialiased;
|
---|
105 | fqCleartype = Graphics.fqCleartype;
|
---|
106 | fqCleartypeNatural = Graphics.fqCleartypeNatural;
|
---|
107 |
|
---|
108 | clNone = Graphics.clNone;
|
---|
109 |
|
---|
110 | clBlack = Graphics.clBlack;
|
---|
111 | clMaroon = Graphics.clMaroon;
|
---|
112 | clGreen = Graphics.clGreen;
|
---|
113 | clOlive = Graphics.clOlive;
|
---|
114 | clNavy = Graphics.clNavy;
|
---|
115 | clPurple = Graphics.clPurple;
|
---|
116 | clTeal = Graphics.clTeal;
|
---|
117 | clGray = Graphics.clGray;
|
---|
118 | clSilver = Graphics.clSilver;
|
---|
119 | clRed = Graphics.clRed;
|
---|
120 | clLime = Graphics.clLime;
|
---|
121 | clYellow = Graphics.clYellow;
|
---|
122 | clBlue = Graphics.clBlue;
|
---|
123 | clFuchsia = Graphics.clFuchsia;
|
---|
124 | clAqua = Graphics.clAqua;
|
---|
125 | clLtGray = Graphics.clLtGray; // clSilver alias
|
---|
126 | clDkGray = Graphics.clDkGray; // clGray alias
|
---|
127 | clWhite = Graphics.clWhite;
|
---|
128 |
|
---|
129 | function FPColorToTColor(const FPColor: TFPColor): TColor; inline;
|
---|
130 | function TColorToFPColor(const c: TColor): TFPColor; inline;
|
---|
131 | function ColorToRGB(c: TColor): TColor; inline;
|
---|
132 | function RGBToColor(R, G, B: Byte): TColor; inline;
|
---|
133 | procedure RedGreenBlue(rgb: TColor; out Red, Green, Blue: Byte); inline;// does not work on system color
|
---|
134 | function clRgbBtnHighlight: TColor;
|
---|
135 | function clRgbBtnShadow: TColor;
|
---|
136 |
|
---|
137 | implementation
|
---|
138 |
|
---|
139 | function FPColorToTColor(const FPColor: TFPColor): TColor;
|
---|
140 | begin
|
---|
141 | result := Graphics.FPColorToTColor(FPColor);
|
---|
142 | end;
|
---|
143 |
|
---|
144 | function TColorToFPColor(const c: TColor): TFPColor;
|
---|
145 | begin
|
---|
146 | result := Graphics.TColorToFPColor(c);
|
---|
147 | end;
|
---|
148 |
|
---|
149 | function ColorToRGB(c: TColor): TColor;
|
---|
150 | begin
|
---|
151 | result := Graphics.ColorToRGB(c);
|
---|
152 | end;
|
---|
153 |
|
---|
154 | function RGBToColor(R, G, B: Byte): TColor;
|
---|
155 | begin
|
---|
156 | result := Graphics.RGBToColor(R, G, B);
|
---|
157 | end;
|
---|
158 |
|
---|
159 | procedure RedGreenBlue(rgb: TColor; out Red, Green, Blue: Byte);
|
---|
160 | begin
|
---|
161 | Graphics.RedGreenBlue(rgb, Red, Green, Blue);
|
---|
162 | end;
|
---|
163 |
|
---|
164 | function clRgbBtnHighlight: TColor;
|
---|
165 | begin
|
---|
166 | result := Graphics.ColorToRGB(clBtnHighlight);
|
---|
167 | end;
|
---|
168 |
|
---|
169 | function clRgbBtnShadow: TColor;
|
---|
170 | begin
|
---|
171 | result := Graphics.ColorToRGB(clBtnShadow);
|
---|
172 | end;
|
---|
173 |
|
---|
174 | {$ELSE}
|
---|
175 |
|
---|
176 | uses
|
---|
177 | Classes, FPCanvas, FPImage
|
---|
178 | {$DEFINE INCLUDE_USES}
|
---|
179 | {$IFDEF BGRABITMAP_USE_FPGUI}
|
---|
180 | {$i bgrafpgui.inc}
|
---|
181 | {$ELSE}
|
---|
182 | {$i bgranogui.inc}
|
---|
183 | {$ENDIF}
|
---|
184 | ;
|
---|
185 |
|
---|
186 | {$DEFINE INCLUDE_INTERFACE}
|
---|
187 | {$IFDEF BGRABITMAP_USE_FPGUI}
|
---|
188 | {$i bgrafpgui.inc}
|
---|
189 | {$ELSE}
|
---|
190 | {$i bgranogui.inc}
|
---|
191 | {$ENDIF}
|
---|
192 |
|
---|
193 | type
|
---|
194 | {* Pointer to a ''TColor'' value }
|
---|
195 | PColor = ^TColor;
|
---|
196 | {* Contains a color stored as RGB. The red/green/blue values
|
---|
197 | range from 0 to 255. The formula to get the color value is:
|
---|
198 | * ''color'' = ''red'' + (''green'' '''shl''' 8) + (''blue'' '''shl''' 16)
|
---|
199 | *except with fpGUI where it is:
|
---|
200 | * ''color'' = (''red'' '''shl''' 16) + (''green'' '''shl''' 8) + ''blue'' }{import
|
---|
201 | TColor = Int32;
|
---|
202 | }
|
---|
203 | {** Converts a ''TFPColor'' into a ''TColor'' value }
|
---|
204 | function FPColorToTColor(const FPColor: TFPColor): TColor;
|
---|
205 | {** Converts a ''TColor'' into a ''TFPColor'' value }
|
---|
206 | function TColorToFPColor(const c: TColor): TFPColor;
|
---|
207 |
|
---|
208 | type
|
---|
209 | {* Direction of change in a gradient }
|
---|
210 | TGradientDirection = (
|
---|
211 | {** Color changes vertically }
|
---|
212 | gdVertical,
|
---|
213 | {** Color changes horizontally }
|
---|
214 | gdHorizontal);
|
---|
215 |
|
---|
216 | {* Antialiasing mode for a Canvas }
|
---|
217 | TAntialiasingMode = (
|
---|
218 | {** It does not matter if there is antialiasing or not }
|
---|
219 | amDontCare,
|
---|
220 | {** Antialiasing is required (BGRACanvas provide it) }
|
---|
221 | amOn,
|
---|
222 | {** Antialiasing is disabled }
|
---|
223 | amOff);
|
---|
224 |
|
---|
225 | {* How to draw the end of line }
|
---|
226 | TPenEndCap = TFPPenEndCap;
|
---|
227 |
|
---|
228 | const
|
---|
229 | {** Draw a half-disk at the end of the line. The diameter of the disk is
|
---|
230 | equal to the pen width. }
|
---|
231 | pecRound = FPCanvas.pecRound;
|
---|
232 | {** Draw a half-square. The size of the square is equal to the pen width.
|
---|
233 | This is visually equivalent to extend the line of half the pen width }
|
---|
234 | pecSquare = FPCanvas.pecSquare;
|
---|
235 | {** The line ends exactly at the end point }
|
---|
236 | pecFlat = FPCanvas.pecFlat;
|
---|
237 |
|
---|
238 | type
|
---|
239 | {* How to join segments. This makes sense only for geometric pens (that
|
---|
240 | have a certain width) }
|
---|
241 | TPenJoinStyle = TFPPenJoinStyle;
|
---|
242 |
|
---|
243 | const
|
---|
244 | {** Segments are joined by filling the gap with an arc }
|
---|
245 | pjsRound = FPCanvas.pjsRound;
|
---|
246 | {** Segments are joind by filling the gap with an intermediary segment }
|
---|
247 | pjsBevel = FPCanvas.pjsBevel;
|
---|
248 | {** Segments are joined by extending them up to their intersection.
|
---|
249 | There is a miter limit so that if the intersection is too far,
|
---|
250 | an intermediary segment is used }
|
---|
251 | pjsMiter = FPCanvas.pjsMiter;
|
---|
252 |
|
---|
253 | type
|
---|
254 | {* Style to use for the pen. The unit for the pattern is the width of the
|
---|
255 | line }
|
---|
256 | TPenStyle = TFPPenStyle;
|
---|
257 |
|
---|
258 | const
|
---|
259 | {** Pen is continuous }
|
---|
260 | psSolid = FPCanvas.psSolid;
|
---|
261 | {** Pen is dashed. The dash have a length of 3 unit and the gaps of 1 unit }
|
---|
262 | psDash = FPCanvas.psDash;
|
---|
263 | {** Pen is dotted. The dots have a length of 1 unit and the gaps of 1 unit }
|
---|
264 | psDot = FPCanvas.psDot;
|
---|
265 | {** Pattern is a dash of length 3 followed by a dot of length 1, separated by a gap of length 1 }
|
---|
266 | psDashDot = FPCanvas.psDashDot;
|
---|
267 | {** Dash of length 3, and two dots of length 1 }
|
---|
268 | psDashDotDot = FPCanvas.psDashDotDot;
|
---|
269 | {** Pen is not drawn }
|
---|
270 | psClear = FPCanvas.psClear;
|
---|
271 | {** Not used. Provided for compatibility }
|
---|
272 | psInsideframe = FPCanvas.psInsideframe;
|
---|
273 | {** Custom pattern used }
|
---|
274 | psPattern = FPCanvas.psPattern;
|
---|
275 |
|
---|
276 | type
|
---|
277 | TTransparentMode = (
|
---|
278 | tmAuto,
|
---|
279 | tmFixed
|
---|
280 | );
|
---|
281 |
|
---|
282 | { TPen }
|
---|
283 | {* A class containing a pen }
|
---|
284 | TPen = class(TFPCustomPen)
|
---|
285 | private
|
---|
286 | FEndCap: TPenEndCap;
|
---|
287 | FJoinStyle: TPenJoinStyle;
|
---|
288 | function GetColor: TColor;
|
---|
289 | procedure SetColor(AValue: TColor);
|
---|
290 | public
|
---|
291 | constructor Create; override;
|
---|
292 | {** Color of the pen }
|
---|
293 | property Color: TColor read GetColor write SetColor;
|
---|
294 | {** End cap of the pen: how to draw the ends of the lines }
|
---|
295 | property EndCap;
|
---|
296 | {** Join style: how to join the segments of a polyline }
|
---|
297 | property JoinStyle;
|
---|
298 | {** Pen style: solid, dash, dot... }{inherited
|
---|
299 | property Style : TPenStyle read write;
|
---|
300 | }{** Pen width in pixels }{inherited
|
---|
301 | property Width : Integer read write;
|
---|
302 | }
|
---|
303 | end;
|
---|
304 |
|
---|
305 | type
|
---|
306 | {* Vertical position of a text }
|
---|
307 | TTextLayout = (tlTop, tlCenter, tlBottom);
|
---|
308 | {* Styles to describe how a text is drawn in a rectangle }
|
---|
309 | TTextStyle = packed record
|
---|
310 | {** Horizontal alignment }
|
---|
311 | Alignment : TAlignment;
|
---|
312 |
|
---|
313 | {** Vertical alignment }
|
---|
314 | Layout : TTextLayout;
|
---|
315 |
|
---|
316 | {** If WordBreak is false then process #13, #10 as
|
---|
317 | standard chars and perform no Line breaking }
|
---|
318 | SingleLine: boolean;
|
---|
319 |
|
---|
320 | {** Clip Text to passed Rectangle }
|
---|
321 | Clipping : boolean;
|
---|
322 |
|
---|
323 | {** Replace #9 by apropriate amount of spaces (default is usually 8) }
|
---|
324 | ExpandTabs: boolean;
|
---|
325 |
|
---|
326 | {** Process first single '&' per line as an underscore and draw '&&' as '&' }
|
---|
327 | ShowPrefix: boolean;
|
---|
328 |
|
---|
329 | {** If line of text is too long too fit between left and right boundaries
|
---|
330 | try to break into multiple lines between words. See also ''EndEllipsis'' }
|
---|
331 | Wordbreak : boolean;
|
---|
332 |
|
---|
333 | {** Fills background with current brush }
|
---|
334 | Opaque : boolean;
|
---|
335 |
|
---|
336 | {** Use the system font instead of canvas font }
|
---|
337 | SystemFont: Boolean;
|
---|
338 |
|
---|
339 | {** For RightToLeft text reading (Text Direction) }
|
---|
340 | RightToLeft: Boolean;
|
---|
341 |
|
---|
342 | {** If line of text is too long to fit between left and right boundaries
|
---|
343 | truncates the text and adds "...". If Wordbreak is set as well,
|
---|
344 | Workbreak will dominate }
|
---|
345 | EndEllipsis: Boolean;
|
---|
346 | end;
|
---|
347 |
|
---|
348 | {* Option for floodfill (used in BGRACanvas) }
|
---|
349 | TFillStyle =
|
---|
350 | (
|
---|
351 | {** Fill up to the color (it fills all except the specified color) }
|
---|
352 | fsSurface,
|
---|
353 | {** Fill the specified color (it fills only connected pixels of this color) }
|
---|
354 | fsBorder
|
---|
355 | );
|
---|
356 | {* How to handle polygons that intersect with themselves and
|
---|
357 | overlapping polygons }
|
---|
358 | TFillMode = (
|
---|
359 | {** Each time a boundary is found, it enters or exit the filling zone }
|
---|
360 | fmAlternate,
|
---|
361 | {** Adds or subtract 1 depending on the order of the points of the
|
---|
362 | polygons (clockwise or counter clockwise) and fill when the
|
---|
363 | result is non-zero. So, to draw a hole, you must specify the points
|
---|
364 | of the hole in the opposite order }
|
---|
365 | fmWinding);
|
---|
366 |
|
---|
367 | {* Pattern when filling with a brush. It is used in BGRACanvas but can
|
---|
368 | also be created with TBGRABitmap.CreateBrushTexture function }
|
---|
369 | TBrushStyle = TFPBrushStyle;
|
---|
370 |
|
---|
371 | const
|
---|
372 | {** Fill with the current color }
|
---|
373 | bsSolid = FPCanvas.bsSolid;
|
---|
374 | {** Does not fill at all }
|
---|
375 | bsClear = FPCanvas.bsClear;
|
---|
376 | {** Draw horizontal lines }
|
---|
377 | bsHorizontal = FPCanvas.bsHorizontal;
|
---|
378 | {** Draw vertical lines }
|
---|
379 | bsVertical = FPCanvas.bsVertical;
|
---|
380 | {** Draw diagonal lines from top-left to bottom-right }
|
---|
381 | bsFDiagonal = FPCanvas.bsFDiagonal;
|
---|
382 | {** Draw diagonal lines from bottom-left to top-right }
|
---|
383 | bsBDiagonal = FPCanvas.bsBDiagonal;
|
---|
384 | {** Draw both horizontal and vertical lines }
|
---|
385 | bsCross = FPCanvas.bsCross;
|
---|
386 | {** Draw both diagonal lines }
|
---|
387 | bsDiagCross = FPCanvas.bsDiagCross;
|
---|
388 |
|
---|
389 | type
|
---|
390 |
|
---|
391 | { TBrush }
|
---|
392 | {* A class describing a brush }
|
---|
393 | TBrush = class(TFPCustomBrush)
|
---|
394 | private
|
---|
395 | function GetColor: TColor;
|
---|
396 | procedure SetColor(AValue: TColor);
|
---|
397 | public
|
---|
398 | constructor Create; override;
|
---|
399 | {** Color of the brush }
|
---|
400 | property Color: TColor read GetColor write SetColor;
|
---|
401 | {** Style of the brush: solid, diagonal lines, horizontal lines... }{inherited
|
---|
402 | property Style : TBrushStyle read write;
|
---|
403 | }
|
---|
404 | end;
|
---|
405 |
|
---|
406 | type
|
---|
407 | TGraphic = class;
|
---|
408 |
|
---|
409 | { TCanvas }
|
---|
410 | {* A surface on which to draw }
|
---|
411 | TCanvas = class
|
---|
412 | protected
|
---|
413 | FCanvas: TGUICanvas;
|
---|
414 | public
|
---|
415 | constructor Create(ACanvas: TGUICanvas);
|
---|
416 | {** Draw an image with top-left corner at (''x'',''y'') }
|
---|
417 | procedure Draw(x,y: integer; AImage: TGraphic);
|
---|
418 | {** Draw and stretch an image within the rectangle ''ARect'' }
|
---|
419 | procedure StretchDraw(ARect: TRect; AImage: TGraphic);
|
---|
420 | property GUICanvas: TGUICanvas read FCanvas;
|
---|
421 | end;
|
---|
422 |
|
---|
423 | { TGraphic }
|
---|
424 | {* A class containing any element that can be drawn within rectangular bounds }
|
---|
425 | TGraphic = class(TPersistent)
|
---|
426 | protected
|
---|
427 | procedure Draw(ACanvas: TCanvas; const Rect: TRect); virtual; abstract;
|
---|
428 | function GetEmpty: Boolean; virtual; abstract;
|
---|
429 | function GetHeight: Integer; virtual; abstract;
|
---|
430 | function GetWidth: Integer; virtual; abstract;
|
---|
431 | function GetTransparent: Boolean; virtual; abstract;
|
---|
432 | procedure SetTransparent(Value: Boolean); virtual; abstract;
|
---|
433 | procedure SetHeight(Value: Integer); virtual; abstract;
|
---|
434 | procedure SetWidth(Value: Integer); virtual; abstract;
|
---|
435 | function GetMimeType: string; virtual;
|
---|
436 | public
|
---|
437 | constructor Create; virtual;
|
---|
438 | {** Load the content from a given file }
|
---|
439 | procedure LoadFromFile({%H-}const Filename: string); virtual;
|
---|
440 | {** Load the content from a given stream }
|
---|
441 | procedure LoadFromStream(Stream: TStream); virtual; abstract;
|
---|
442 | {** Saves the content to a file }
|
---|
443 | procedure SaveToFile({%H-}const Filename: string); virtual;
|
---|
444 | {** Saves the content into a given stream }
|
---|
445 | procedure SaveToStream(Stream: TStream); virtual; abstract;
|
---|
446 | {** Returns the list of possible file extensions }
|
---|
447 | class function GetFileExtensions: string; virtual;
|
---|
448 | {** Clears the content }
|
---|
449 | procedure Clear; virtual;
|
---|
450 | public
|
---|
451 | {** Returns if the content is completely empty }
|
---|
452 | property Empty: Boolean read GetEmpty;
|
---|
453 | {** Returns the height of the bounding rectangle }
|
---|
454 | property Height: Integer read GetHeight write SetHeight;
|
---|
455 | {** Returns the width of the bounding rectangle }
|
---|
456 | property Width: Integer read GetWidth write SetWidth;
|
---|
457 | {** Gets or sets if it is drawn with transparency }
|
---|
458 | property Transparent: Boolean read GetTransparent write SetTransparent;
|
---|
459 | end;
|
---|
460 |
|
---|
461 | { TBitmap }
|
---|
462 | {* Contains a bitmap }
|
---|
463 | TBitmap = class(TGraphic)
|
---|
464 | private
|
---|
465 | FHeight: integer;
|
---|
466 | FWidth: integer;
|
---|
467 | FInDraw: boolean;
|
---|
468 | FTransparent: boolean;
|
---|
469 | FTransparentColor: TColor;
|
---|
470 | FTransparentMode: TTransparentMode;
|
---|
471 | function GetCanvas: TCanvas;
|
---|
472 | function GetRawImage: TRawImage;
|
---|
473 | procedure SetTransparentColor(AValue: TColor);
|
---|
474 | procedure SetTransparentMode(AValue: TTransparentMode);
|
---|
475 | protected
|
---|
476 | FRawImage: TRawImage;
|
---|
477 | procedure Draw(ACanvas: TCanvas; const Rect: TRect); override;
|
---|
478 | procedure Changed(Sender: TObject); virtual;
|
---|
479 | function GetHeight: Integer; override;
|
---|
480 | function GetWidth: Integer; override;
|
---|
481 | procedure SetHeight(Value: Integer); override;
|
---|
482 | procedure SetWidth(Value: Integer); override;
|
---|
483 | function GetEmpty: Boolean; override;
|
---|
484 | function GetTransparent: Boolean; override;
|
---|
485 | procedure SetTransparent({%H-}Value: Boolean); override;
|
---|
486 | function GetMimeType: string; override;
|
---|
487 | public
|
---|
488 | constructor Create; override;
|
---|
489 | destructor Destroy; override;
|
---|
490 | procedure LoadFromStream({%H-}Stream: TStream); override;
|
---|
491 | procedure SaveToStream({%H-}Stream: TStream); override;
|
---|
492 | {** Width of the bitmap in pixels }
|
---|
493 | property Width: integer read GetWidth write SetWidth;
|
---|
494 | {** Height of the bitmap in pixels }
|
---|
495 | property Height: integer read GetHeight write SetHeight;
|
---|
496 | property RawImage: TRawImage read GetRawImage;
|
---|
497 | property Canvas: TCanvas read GetCanvas;
|
---|
498 | property TransparentColor: TColor read FTransparentColor
|
---|
499 | write SetTransparentColor default clDefault;
|
---|
500 | property TransparentMode: TTransparentMode read FTransparentMode
|
---|
501 | write SetTransparentMode default tmAuto;
|
---|
502 | end;
|
---|
503 |
|
---|
504 | TRasterImage = TBitmap;
|
---|
505 |
|
---|
506 | {* Available font styles }
|
---|
507 | TFontStyle = (
|
---|
508 | {** Font is bold }
|
---|
509 | fsBold,
|
---|
510 | {** Font is italic }
|
---|
511 | fsItalic,
|
---|
512 | {** An horizontal line is drawn in the middle of the text }
|
---|
513 | fsStrikeOut,
|
---|
514 | {** Text is underlined }
|
---|
515 | fsUnderline);
|
---|
516 | {** A combination of font styles }
|
---|
517 | TFontStyles = set of TFontStyle;
|
---|
518 | {* Quality to use when font is rendered by the system }
|
---|
519 | TFontQuality = (fqDefault, fqDraft, fqProof, fqNonAntialiased, fqAntialiased, fqCleartype, fqCleartypeNatural);
|
---|
520 |
|
---|
521 | { TFont }
|
---|
522 | {* Contains the description of a font }
|
---|
523 | TFont = class(TFPCustomFont)
|
---|
524 | private
|
---|
525 | FPixelsPerInch, FHeight: Integer;
|
---|
526 | FQuality: TFontQuality;
|
---|
527 | FStyle: TFontStyles;
|
---|
528 | function GetColor: TColor;
|
---|
529 | function GetHeight: Integer;
|
---|
530 | function GetSize: Integer;
|
---|
531 | function GetStyle: TFontStyles;
|
---|
532 | procedure SetColor(AValue: TColor);
|
---|
533 | procedure SetHeight(AValue: Integer);
|
---|
534 | procedure SetQuality(AValue: TFontQuality);
|
---|
535 | procedure SetStyle(AValue: TFontStyles);
|
---|
536 | protected
|
---|
537 | procedure SetSize(AValue: Integer); override;
|
---|
538 | public
|
---|
539 | constructor Create; override;
|
---|
540 | {** Pixels per inches }
|
---|
541 | property PixelsPerInch: Integer read FPixelsPerInch write FPixelsPerInch;
|
---|
542 | {** Color of the font }
|
---|
543 | property Color: TColor read GetColor write SetColor;
|
---|
544 | {** Height of the font in pixels. When the number is negative, it indicates a size in pixels }
|
---|
545 | property Height: Integer read GetHeight write SetHeight;
|
---|
546 | {** Size of the font in inches. When the number is negative, it indicates a height in inches }
|
---|
547 | property Size: Integer read GetSize write SetSize;
|
---|
548 | {** Quality of the font rendering }
|
---|
549 | property Quality: TFontQuality read FQuality write SetQuality;
|
---|
550 | {** Style to apply to the text }
|
---|
551 | property Style: TFontStyles read GetStyle write SetStyle;
|
---|
552 | end;
|
---|
553 |
|
---|
554 | {* Multiply and divide the number allowing big intermediate number and rounding the result }
|
---|
555 | function MulDiv(nNumber, nNumerator, nDenominator: Integer): Integer;
|
---|
556 | {* Round the number using math convention }
|
---|
557 | function MathRound(AValue: ValReal): Int64; inline;
|
---|
558 |
|
---|
559 | implementation
|
---|
560 |
|
---|
561 | uses sysutils, BGRAUTF8;
|
---|
562 |
|
---|
563 | {$DEFINE INCLUDE_IMPLEMENTATION}
|
---|
564 | {$IFDEF BGRABITMAP_USE_FPGUI}
|
---|
565 | {$i bgrafpgui.inc}
|
---|
566 | {$ELSE}
|
---|
567 | {$i bgranogui.inc}
|
---|
568 | {$ENDIF}
|
---|
569 |
|
---|
570 | function MathRound(AValue: ValReal): Int64; inline;
|
---|
571 | begin
|
---|
572 | if AValue >= 0 then
|
---|
573 | Result := Trunc(AValue + 0.5)
|
---|
574 | else
|
---|
575 | Result := Trunc(AValue - 0.5);
|
---|
576 | end;
|
---|
577 |
|
---|
578 | function MulDiv(nNumber, nNumerator, nDenominator: Integer): Integer;
|
---|
579 | begin
|
---|
580 | if nDenominator = 0 then
|
---|
581 | Result := -1
|
---|
582 | else
|
---|
583 | Result := MathRound(int64(nNumber) * int64(nNumerator) / nDenominator);
|
---|
584 | end;
|
---|
585 |
|
---|
586 | function FPColorToTColor(const FPColor: TFPColor): TColor;
|
---|
587 | begin
|
---|
588 | {$IFDEF BGRABITMAP_USE_FPGUI}
|
---|
589 | Result:=((FPColor.Blue shr 8) and $ff)
|
---|
590 | or (FPColor.Green and $ff00)
|
---|
591 | or ((FPColor.Red shl 8) and $ff0000);
|
---|
592 | {$ELSE}
|
---|
593 | Result:=((FPColor.Red shr 8) and $ff)
|
---|
594 | or (FPColor.Green and $ff00)
|
---|
595 | or ((FPColor.Blue shl 8) and $ff0000);
|
---|
596 | {$ENDIF}
|
---|
597 | end;
|
---|
598 |
|
---|
599 | function TColorToFPColor(const c: TColor): TFPColor;
|
---|
600 | begin
|
---|
601 | {$IFDEF BGRABITMAP_USE_FPGUI}
|
---|
602 | Result.Blue:=(c and $ff);
|
---|
603 | Result.Blue:=Result.Blue+(Result.Blue shl 8);
|
---|
604 | Result.Green:=(c and $ff00);
|
---|
605 | Result.Green:=Result.Green+(Result.Green shr 8);
|
---|
606 | Result.Red:=(c and $ff0000) shr 8;
|
---|
607 | Result.Red:=Result.Red+(Result.Red shr 8);
|
---|
608 | {$ELSE}
|
---|
609 | Result.Red:=(c and $ff);
|
---|
610 | Result.Red:=Result.Red+(Result.Red shl 8);
|
---|
611 | Result.Green:=(c and $ff00);
|
---|
612 | Result.Green:=Result.Green+(Result.Green shr 8);
|
---|
613 | Result.Blue:=(c and $ff0000) shr 8;
|
---|
614 | Result.Blue:=Result.Blue+(Result.Blue shr 8);
|
---|
615 | {$ENDIF}
|
---|
616 | Result.Alpha:=FPImage.alphaOpaque;
|
---|
617 | end;
|
---|
618 |
|
---|
619 | { TGraphic }
|
---|
620 |
|
---|
621 | function TGraphic.GetMimeType: string;
|
---|
622 | begin
|
---|
623 | result := '';
|
---|
624 | end;
|
---|
625 |
|
---|
626 | constructor TGraphic.Create;
|
---|
627 | begin
|
---|
628 | //nothing
|
---|
629 | end;
|
---|
630 |
|
---|
631 | procedure TGraphic.LoadFromFile(const Filename: string);
|
---|
632 | var
|
---|
633 | Stream: TStream;
|
---|
634 | begin
|
---|
635 | Stream := TFileStreamUTF8.Create(Filename, fmOpenRead or fmShareDenyWrite);
|
---|
636 | try
|
---|
637 | LoadFromStream(Stream);
|
---|
638 | finally
|
---|
639 | Stream.Free;
|
---|
640 | end;
|
---|
641 | end;
|
---|
642 |
|
---|
643 | procedure TGraphic.SaveToFile(const Filename: string);
|
---|
644 | var
|
---|
645 | Stream: TStream;
|
---|
646 | begin
|
---|
647 | Stream := TFileStreamUTF8.Create(Filename, fmCreate);
|
---|
648 | try
|
---|
649 | SaveToStream(Stream);
|
---|
650 | finally
|
---|
651 | Stream.Free;
|
---|
652 | end;
|
---|
653 | end;
|
---|
654 |
|
---|
655 | class function TGraphic.GetFileExtensions: string;
|
---|
656 | begin
|
---|
657 | result := '';
|
---|
658 | end;
|
---|
659 |
|
---|
660 | procedure TGraphic.Clear;
|
---|
661 | begin
|
---|
662 | //nothing
|
---|
663 | end;
|
---|
664 |
|
---|
665 | { TCanvas }
|
---|
666 |
|
---|
667 | constructor TCanvas.Create(ACanvas: TGUICanvas);
|
---|
668 | begin
|
---|
669 | FCanvas := ACanvas;
|
---|
670 | end;
|
---|
671 |
|
---|
672 | procedure TCanvas.Draw(x, y: integer; AImage: TGraphic);
|
---|
673 | begin
|
---|
674 | if AImage is TBitmap then
|
---|
675 | FCanvas.DrawImage(x,y, TBitmap(AImage).RawImage)
|
---|
676 | else
|
---|
677 | AImage.Draw(self, rect(x,y,x+AImage.Width,y+AImage.Height));
|
---|
678 | end;
|
---|
679 |
|
---|
680 | procedure TCanvas.StretchDraw(ARect: TRect; AImage: TGraphic);
|
---|
681 | begin
|
---|
682 | if AImage is TBitmap then
|
---|
683 | FCanvas.StretchDraw(ARect.Left,ARect.Top,ARect.Right-ARect.Left,ARect.Bottom-ARect.Top, TBitmap(AImage).RawImage)
|
---|
684 | else
|
---|
685 | AImage.Draw(self, ARect);
|
---|
686 | end;
|
---|
687 |
|
---|
688 | { TPen }
|
---|
689 |
|
---|
690 | procedure TPen.SetColor(AValue: TColor);
|
---|
691 | begin
|
---|
692 | FPColor := TColorToFPColor(AValue);
|
---|
693 | end;
|
---|
694 |
|
---|
695 | function TPen.GetColor: TColor;
|
---|
696 | begin
|
---|
697 | result := FPColorToTColor(FPColor);
|
---|
698 | end;
|
---|
699 |
|
---|
700 | constructor TPen.Create;
|
---|
701 | begin
|
---|
702 | inherited Create;
|
---|
703 | Mode := pmCopy;
|
---|
704 | Style := psSolid;
|
---|
705 | Width := 1;
|
---|
706 | FPColor := colBlack;
|
---|
707 | FEndCap:= pecRound;
|
---|
708 | FJoinStyle:= pjsRound;
|
---|
709 | end;
|
---|
710 |
|
---|
711 | { TBrush }
|
---|
712 |
|
---|
713 | function TBrush.GetColor: TColor;
|
---|
714 | begin
|
---|
715 | result := FPColorToTColor(FPColor);
|
---|
716 | end;
|
---|
717 |
|
---|
718 | procedure TBrush.SetColor(AValue: TColor);
|
---|
719 | begin
|
---|
720 | FPColor := TColorToFPColor(AValue);
|
---|
721 | end;
|
---|
722 |
|
---|
723 | constructor TBrush.Create;
|
---|
724 | begin
|
---|
725 | inherited Create;
|
---|
726 | FPColor := colWhite;
|
---|
727 | end;
|
---|
728 |
|
---|
729 | { TFont }
|
---|
730 |
|
---|
731 | function TFont.GetColor: TColor;
|
---|
732 | begin
|
---|
733 | result := FPColorToTColor(FPColor);
|
---|
734 | end;
|
---|
735 |
|
---|
736 | function TFont.GetHeight: Integer;
|
---|
737 | begin
|
---|
738 | result := FHeight;
|
---|
739 | end;
|
---|
740 |
|
---|
741 | function TFont.GetSize: Integer;
|
---|
742 | begin
|
---|
743 | Result := inherited Size;
|
---|
744 | end;
|
---|
745 |
|
---|
746 | function TFont.GetStyle: TFontStyles;
|
---|
747 | begin
|
---|
748 | result := FStyle;
|
---|
749 | end;
|
---|
750 |
|
---|
751 | procedure TFont.SetColor(AValue: TColor);
|
---|
752 | begin
|
---|
753 | FPColor := TColorToFPColor(AValue);
|
---|
754 | end;
|
---|
755 |
|
---|
756 | procedure TFont.SetHeight(AValue: Integer);
|
---|
757 | begin
|
---|
758 | if Height <> AValue then
|
---|
759 | begin
|
---|
760 | FHeight := AValue;
|
---|
761 | inherited SetSize(-MulDiv(AValue, 72, FPixelsPerInch));
|
---|
762 | end;
|
---|
763 | end;
|
---|
764 |
|
---|
765 | procedure TFont.SetQuality(AValue: TFontQuality);
|
---|
766 | begin
|
---|
767 | if FQuality=AValue then Exit;
|
---|
768 | FQuality:=AValue;
|
---|
769 | end;
|
---|
770 |
|
---|
771 | procedure TFont.SetSize(AValue: Integer);
|
---|
772 | begin
|
---|
773 | if Size <> AValue then
|
---|
774 | begin
|
---|
775 | inherited SetSize(AValue);
|
---|
776 | FHeight := -MulDiv(AValue, FPixelsPerInch, 72);
|
---|
777 | end;
|
---|
778 | end;
|
---|
779 |
|
---|
780 | procedure TFont.SetStyle(AValue: TFontStyles);
|
---|
781 | begin
|
---|
782 | if FStyle <> AValue then
|
---|
783 | begin
|
---|
784 | FStyle := AValue;
|
---|
785 | inherited SetFlags(5, fsBold in FStyle);
|
---|
786 | inherited SetFlags(6, fsItalic in FStyle);
|
---|
787 | inherited SetFlags(7, fsUnderline in FStyle);
|
---|
788 | inherited SetFlags(8, fsStrikeOut in FStyle);
|
---|
789 | end;
|
---|
790 | end;
|
---|
791 |
|
---|
792 | constructor TFont.Create;
|
---|
793 | begin
|
---|
794 | FPixelsPerInch := GetScreenDPIY;
|
---|
795 | FQuality := fqDefault;
|
---|
796 | FPColor := colBlack;
|
---|
797 | end;
|
---|
798 |
|
---|
799 | { TBitmap }
|
---|
800 |
|
---|
801 | procedure TBitmap.SetWidth(Value: Integer);
|
---|
802 | begin
|
---|
803 | if FWidth=Value then Exit;
|
---|
804 | FWidth:=Value;
|
---|
805 | end;
|
---|
806 |
|
---|
807 | function TBitmap.GetEmpty: Boolean;
|
---|
808 | begin
|
---|
809 | result := (Width = 0) or (Height = 0);
|
---|
810 | end;
|
---|
811 |
|
---|
812 | function TBitmap.GetTransparent: Boolean;
|
---|
813 | begin
|
---|
814 | result := FTransparent;
|
---|
815 | end;
|
---|
816 |
|
---|
817 | procedure TBitmap.SetTransparent(Value: Boolean);
|
---|
818 | begin
|
---|
819 | if Value = FTransparent then exit;
|
---|
820 | FTransparent:= Value;
|
---|
821 | end;
|
---|
822 |
|
---|
823 | procedure TBitmap.SetTransparentColor(AValue: TColor);
|
---|
824 | begin
|
---|
825 | if FTransparentColor = AValue then exit;
|
---|
826 | FTransparentColor := AValue;
|
---|
827 |
|
---|
828 | if AValue = clDefault
|
---|
829 | then FTransparentMode := tmAuto
|
---|
830 | else FTransparentMode := tmFixed;
|
---|
831 | end;
|
---|
832 |
|
---|
833 | procedure TBitmap.SetTransparentMode(AValue: TTransparentMode);
|
---|
834 | begin
|
---|
835 | if AValue = TransparentMode then exit;
|
---|
836 | FTransparentMode := AValue;
|
---|
837 |
|
---|
838 | if AValue = tmAuto
|
---|
839 | then TransparentColor := clDefault
|
---|
840 | end;
|
---|
841 |
|
---|
842 | function TBitmap.GetMimeType: string;
|
---|
843 | begin
|
---|
844 | Result:= 'image/bmp';
|
---|
845 | end;
|
---|
846 |
|
---|
847 | procedure TBitmap.Changed(Sender: TObject);
|
---|
848 | begin
|
---|
849 | //nothing
|
---|
850 | end;
|
---|
851 |
|
---|
852 | procedure TBitmap.LoadFromStream(Stream: TStream);
|
---|
853 | begin
|
---|
854 | raise exception.Create('Not implemented');
|
---|
855 | end;
|
---|
856 |
|
---|
857 | procedure TBitmap.SaveToStream(Stream: TStream);
|
---|
858 | begin
|
---|
859 | raise exception.Create('Not implemented');
|
---|
860 | end;
|
---|
861 |
|
---|
862 | procedure TBitmap.SetHeight(Value: Integer);
|
---|
863 | begin
|
---|
864 | if FHeight=Value then Exit;
|
---|
865 | FHeight:=Value;
|
---|
866 | end;
|
---|
867 |
|
---|
868 | function TBitmap.GetRawImage: TRawImage;
|
---|
869 | begin
|
---|
870 | FRawImage.BGRASetSizeAndTransparency(FWidth, FHeight, FTransparent);
|
---|
871 | result := FRawImage;
|
---|
872 | end;
|
---|
873 |
|
---|
874 | procedure TBitmap.Draw(ACanvas: TCanvas; const Rect: TRect);
|
---|
875 | begin
|
---|
876 | if FInDraw then exit;
|
---|
877 | FInDraw := true;
|
---|
878 | ACanvas.StretchDraw(Rect, self);
|
---|
879 | FInDraw := false;
|
---|
880 | end;
|
---|
881 |
|
---|
882 | function TBitmap.GetHeight: Integer;
|
---|
883 | begin
|
---|
884 | result := FHeight;
|
---|
885 | end;
|
---|
886 |
|
---|
887 | function TBitmap.GetWidth: Integer;
|
---|
888 | begin
|
---|
889 | result := FWidth;
|
---|
890 | end;
|
---|
891 |
|
---|
892 | function TBitmap.GetCanvas: TCanvas;
|
---|
893 | begin
|
---|
894 | result := nil;
|
---|
895 | raise exception.Create('Canvas not available');
|
---|
896 | end;
|
---|
897 |
|
---|
898 | constructor TBitmap.Create;
|
---|
899 | begin
|
---|
900 | FRawImage := TRawImage.Create;
|
---|
901 | FTransparent:= false;
|
---|
902 | end;
|
---|
903 |
|
---|
904 | destructor TBitmap.Destroy;
|
---|
905 | begin
|
---|
906 | FRawImage.Free;
|
---|
907 | inherited Destroy;
|
---|
908 | end;
|
---|
909 |
|
---|
910 | {$ENDIF}
|
---|
911 |
|
---|
912 | end.
|
---|
913 |
|
---|