File: lazfreetypefpimagedrawer.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (511 lines) | stat: -rw-r--r-- 13,674 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
{
 *****************************************************************************
  This file is part of LazUtils.

  See the file COPYING.modifiedLGPL.txt, included in this distribution,
  for details about the license.
 *****************************************************************************
}
unit LazFreeTypeFPImageDrawer;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, EasyLazFreeType, FPimage;

type
  TLazIntfImageGetPixelAtProc = procedure(p: pointer; out Color: TFPColor);
  TLazIntfImageSetPixelAtProc = procedure(p: pointer; const Color: TFPColor);

  { TFPImageFreeTypeDrawer }

  TFPImageFreeTypeDrawer = class(TFreeTypeDrawer)
  private
    FColor: TFPColor;
    FCurX,FCurY: integer;
  protected
    FImage: TFPCustomImage;
    procedure MoveToPixel(x,y: integer); virtual;
    function GetCurrentColor: TFPColor; virtual;
    procedure SetCurrentColorAndMoveRight(const AColor: TFPColor); virtual;
    procedure MoveRight; virtual;
    function GetClipRect: TRect; virtual;
  protected
    procedure RenderDirectly(x, y, tx: integer; data: pointer);
    procedure RenderDirectlyClearType(x, y, tx: integer; data: pointer);
    procedure InternalMergeColorOver(var merge: TFPColor; const c: TFPColor; calpha: word); inline;
    procedure MergeColorOver(var merge: TFPColor; const c: TFPColor); inline;
    procedure MergeColorOver(var merge: TFPColor; const c: TFPColor; ApplyOpacity: byte); inline;
    procedure DrawPixelAndMoveRight(const c: TFPColor);
    procedure DrawPixelAndMoveRight(const c: TFPColor; applyOpacity: byte);
    procedure ClearTypePixelAndMoveRight(Cr,Cg,Cb: byte; const Color: TFPColor);
    procedure UnclippedDrawPixel(x,y: integer; const c: TFPColor);
  public
    ClearTypeRGBOrder: boolean;
    constructor Create(AImage: TFPCustomImage); virtual;
    procedure DrawPixel(x,y: integer; const c: TFPColor);
    procedure ClearTypePixel(x,y: integer; Cr,Cg,Cb: byte; const Color: TFPColor);
    procedure SetVertLine(x,y1,y2: integer; const c: TFPColor);
    procedure DrawVertLine(x,y1,y2: integer; const c: TFPColor);
    procedure SetHorizLine(x1,y,x2: integer; const c: TFPColor);
    procedure DrawHorizLine(x1,y,x2: integer; const c: TFPColor);
    procedure FillPixels(const c: TFPColor);
    procedure DrawText(AText: string; AFont: TFreeTypeRenderableFont; x,y: single; AColor: TFPColor); override;
    destructor Destroy; override;
    property Image: TFPCustomImage read FImage;
  end;

  { TFPMemoryImageWithScanline }

  TFPMemoryImageWithScanline = class(TFPMemoryImage)
  protected
    function GetScanline(y: integer): PFPColor;
    procedure SetUsePalette ({%H-}Value:boolean);override;
  public
    property ScanLine[y: integer]: PFPColor read GetScanline;
  end;

  { TFPImageWithScanlineFreeTypeDrawer }

  TFPImageWithScanlineFreeTypeDrawer= class(TFPImageFreeTypeDrawer)
  protected
    FCurrentColor: PFPColor;
    procedure MoveToPixel(x,y: integer); override;
    function GetCurrentColor: TFPColor; override;
    procedure SetCurrentColorAndMoveRight(const AColor: TFPColor); override;
    procedure MoveRight; override;
    function GetClipRect: TRect; override;
  public
    constructor Create(AImage: TFPCustomImage); override; //requires TFPMemoryImageWithScanline
  end;

implementation

{ TFPImageFreeTypeDrawer }

procedure TFPImageFreeTypeDrawer.MergeColorOver(var merge: TFPColor; const c: TFPColor);
begin
  InternalMergeColorOver(merge,c,c.alpha);
end;

procedure TFPImageFreeTypeDrawer.MergeColorOver(var merge: TFPColor;
  const c: TFPColor; ApplyOpacity: byte);
var
  calpha: longword;
begin
  calpha := c.alpha*applyOpacity div 255;
  InternalMergeColorOver(merge,c,calpha);
end;

procedure TFPImageFreeTypeDrawer.UnclippedDrawPixel(x, y: integer; const c: TFPColor);
var
  merge: TFPColor;
begin
  if c.alpha = 0 then exit;
  MoveToPixel(x,y);
  if c.alpha = $ffff then
    SetCurrentColorAndMoveRight(c)
  else
  begin
    merge := GetCurrentColor;
    MergeColorOver(merge,c);
    SetCurrentColorAndMoveRight(merge);
  end;
end;

procedure TFPImageFreeTypeDrawer.DrawPixelAndMoveRight(const c: TFPColor; applyOpacity: byte);
var
  merge: TFPColor;
  calpha: longword;
begin
  calpha := c.alpha*applyOpacity div 255;
  if calpha = 0 then
  begin
    MoveRight;
    exit;
  end;
  if calpha = $ffff then
    SetCurrentColorAndMoveRight(c)
  else
  begin
    merge := GetCurrentColor;
    InternalMergeColorOver(merge,c,calpha);
    SetCurrentColorAndMoveRight(merge);
  end;
end;

procedure TFPImageFreeTypeDrawer.DrawPixelAndMoveRight(const c: TFPColor);
var
  merge: TFPColor;
begin
  if (c.alpha = 0) then
  begin
    MoveRight;
    exit;
  end;
  if (c.alpha = $ffff) then
    SetCurrentColorAndMoveRight(c)
  else
  begin
    merge := GetCurrentColor;
    InternalMergeColorOver(merge,c,c.alpha);
    SetCurrentColorAndMoveRight(merge);
  end;
end;

procedure TFPImageFreeTypeDrawer.SetVertLine(x, y1, y2: integer;
  const c: TFPColor);
var y: integer;
begin
  with GetClipRect do
  begin
    if (x < Left) or (x >= Right) then exit;
    if (y1 > y2) then
    begin
      y := y1;
      y1:= y2;
      y2 := y;
    end;
    if y1 < Top then y1 := Top;
    if y2 >= Bottom then y2 := Bottom-1;
  end;
  for y := y1 to y2 do
  begin
    MoveToPixel(x,y1);
    SetCurrentColorAndMoveRight(c);
  end;
end;

procedure TFPImageFreeTypeDrawer.DrawVertLine(x, y1, y2: integer; const c: TFPColor
  );
var y: integer;
begin
  with GetClipRect do
  begin
    if (x < Left) or (x >= Right) then exit;
    if (y1 > y2) then
    begin
      y := y1;
      y1:= y2;
      y2 := y;
    end;
    if y1 < Top then y1 := Top;
    if y2 >= Bottom then y2 := Bottom-1;
  end;
  for y := y1 to y2 do
    UnclippedDrawPixel(x,y, c);
end;

procedure TFPImageFreeTypeDrawer.SetHorizLine(x1, y, x2: integer; const c: TFPColor);
var i: integer;
begin
  with GetClipRect do
  begin
    if (y < Top) or (y >= Bottom) then exit;
    if (x1 > x2) then
    begin
      i := x1;
      x1:= x2;
      x2 := i;
    end;
    if x1 < Left then x1 := Left;
    if x2 >= Right then x2 := Right-1;
  end;
  MoveToPixel(x1,y);
  i := x2-x1+1;
  while i > 0 do
  begin
    SetCurrentColorAndMoveRight(c);
    dec(i);
  end;
end;

procedure TFPImageFreeTypeDrawer.DrawHorizLine(x1, y, x2: integer;
  const c: TFPColor);
var i: integer;
begin
  with GetClipRect do
  begin
    if (y < Top) or (y >= Bottom) then exit;
    if (x1 > x2) then
    begin
      i := x1;
      x1:= x2;
      x2 := i;
    end;
    if x1 < Left then x1 := Left;
    if x2 >= Right then x2 := Right-1;
  end;
  MoveToPixel(x1,y);
  i := x2-x1+1;
  while i > 0 do
  begin
    DrawPixelAndMoveRight(c);
    dec(i);
  end;
end;

procedure TFPImageFreeTypeDrawer.FillPixels(const c: TFPColor);
var yb: integer;
begin
  with GetClipRect do
  begin
    for yb := Top to Bottom-1 do
      SetHorizLine(Left,yb,Right-1,c);
  end;
end;

procedure TFPImageFreeTypeDrawer.ClearTypePixel(x, y: integer; Cr, Cg, Cb: byte; const Color: TFPColor);
begin
  with GetClipRect do
    if (x < Left) or (y < Top) or (x >= Right) or (y >= Bottom) then exit;

  MoveToPixel(x,y);
  ClearTypePixelAndMoveRight(Cr,Cg,Cb,Color);
end;

procedure TFPImageFreeTypeDrawer.ClearTypePixelAndMoveRight(Cr, Cg, Cb: byte;
  const Color: TFPColor);
var merge,mergeClearType: TFPColor;
    acc: longword;
    keep,dont_keep: word;
begin
  Cr := Cr*(color.alpha+1) shr 16;
  Cg := Cg*(color.alpha+1) shr 16;
  Cb := Cb*(color.alpha+1) shr 16;
  acc := Cr+Cg+Cb;
  if acc = 0 then exit;

  merge := GetCurrentColor;
  mergeClearType.red := (merge.red * (not byte(Cr)) +
                color.red * Cr + 128) div 255;
  mergeClearType.green := (merge.green * (not byte(Cg)) +
                color.green * Cg + 128) div 255;
  mergeClearType.blue := (merge.blue * (not byte(Cb)) +
                color.blue * Cb + 128) div 255;
  mergeClearType.alpha := merge.alpha;

  if (mergeClearType.alpha = $ffff) then
    SetCurrentColorAndMoveRight(mergeClearType)
  else
  begin
    if Cg <> 0 then
      MergeColorOver(merge,color,Cg);
    dont_keep := mergeClearType.alpha shr 1;
    if dont_keep > 0 then
    begin
      keep := 32767 - dont_keep;
      merge.red := (merge.red * keep + mergeClearType.red * dont_keep) div 32767;
      merge.green := (merge.green * keep + mergeClearType.green * dont_keep) div 32767;
      merge.blue := (merge.blue * keep + mergeClearType.blue * dont_keep) div 32767;
      merge.alpha := mergeClearType.alpha + ((not mergeClearType.alpha)*merge.alpha div 65535);
    end;
    SetCurrentColorAndMoveRight(merge);
  end;
end;

procedure TFPImageFreeTypeDrawer.MoveToPixel(x, y: integer);
begin
  FCurX := x;
  FCurY := y;
end;

function TFPImageFreeTypeDrawer.GetCurrentColor: TFPColor;
begin
  result := FImage.Colors[FCurX,FCurY];
end;

procedure TFPImageFreeTypeDrawer.SetCurrentColorAndMoveRight(
  const AColor: TFPColor);
begin
  FImage.Colors[FCurX,FCurY] := AColor;
  Inc(FCurX);
end;

procedure TFPImageFreeTypeDrawer.MoveRight;
begin
  inc(FCurX);
end;

function TFPImageFreeTypeDrawer.GetClipRect: TRect;
begin
  result := rect(0,0,FImage.Width,FImage.Height);
end;

procedure TFPImageFreeTypeDrawer.RenderDirectly( x,y,tx: integer;
                          data: pointer );
var psrc: pbyte;
    c: TFPColor;
    tempValue: byte;
begin
  //ensure rendering in bounds
  with GetClipRect do
    if (y < Top) or (y >= Bottom) or (x < Left) or (x > Right-tx) then exit;

  c := FColor;
  psrc := pbyte(data);

  MoveToPixel(x,y);
  inc(psrc,tx);
  while tx > 0 do
  begin
    tempValue := (psrc-tx)^;
    if tempValue <> 0 then
      DrawPixelAndMoveRight(c,tempValue)
    else
      MoveRight;
    dec(tx);
  end;
end;

procedure TFPImageFreeTypeDrawer.RenderDirectlyClearType(x, y, tx: integer; data: pointer);
var xb: integer;
    psrc: pbyte;
    Cr,Cg,Cb: byte;
begin
  //ClearType position in third of pixels horizontally (multiple of 3)
  x := x div 3;
  tx := tx div 3;
  //ensure rendering in bounds
  with GetClipRect do
    if (y < Top) or (y >= Bottom) or (x < Left) or (x > Right-tx) then exit;
  if tx=0 then exit;

  psrc := pbyte(data);
  Cr := (psrc^ + psrc^ + (psrc+1)^) div 3;
  Cg := (psrc^+ (psrc+1)^ + (psrc+2)^) div 3;
  if tx > 1 then
    Cb := ((psrc+1)^ + (psrc+2)^ + (psrc+3)^) div 3
  else
    Cb := ((psrc+1)^ + (psrc+2)^ + (psrc+2)^) div 3;

  MoveToPixel(x,y);
  if Cr+Cg+Cb <> 0 then
    ClearTypePixelAndMoveRight(Cr,Cg,Cb, FColor)
  else
    MoveRight;
  inc(psrc,3);
  for xb := 1 to tx-2 do
  begin
    Cr := ((psrc-1)^+ psrc^ + (psrc+1)^) div 3;
    Cg := (psrc^+ (psrc+1)^ + (psrc+2)^) div 3;
    Cb := ((psrc+1)^ + (psrc+2)^ + (psrc+3)^) div 3;
    if Cr+Cg+Cb <> 0 then
      ClearTypePixelAndMoveRight(Cr,Cg,Cb, FColor)
    else
      MoveRight;
    inc(psrc,3);
  end;
  if tx > 1 then
  begin
    Cr := ((psrc-1)^+ psrc^ + (psrc+1)^) div 3;
    Cg := (psrc^+ (psrc+1)^ + (psrc+2)^) div 3;
    Cb := ((psrc+1)^ + (psrc+2)^ + (psrc+2)^) div 3;
    if Cr+Cg+Cb <> 0 then
      ClearTypePixelAndMoveRight(Cr,Cg,Cb, FColor);
  end;
end;

procedure TFPImageFreeTypeDrawer.InternalMergeColorOver(var merge: TFPColor;
  const c: TFPColor; calpha: word);
var
  a1f, a2f, a12, a12m: cardinal;
begin
  if calpha = 0 then exit;
  a12  := 65534 - ((not merge.alpha) * (not calpha) shr 16);
  a12m := a12 shr 1;

  a1f := merge.alpha * (not calpha) shr 16;
  a2f := calpha - (calpha shr 15);

  merge.red := (merge.red * a1f + c.red * a2f + a12m) div a12;
  merge.green := (merge.green * a1f + c.green * a2f + a12m) div a12;
  merge.blue := (merge.blue * a1f + c.blue * a2f + a12m) div a12;
  merge.alpha := a12 + (a12 shr 15);
end;

constructor TFPImageFreeTypeDrawer.Create(AImage: TFPCustomImage);
begin
  ClearTypeRGBOrder:= true;
  FImage := AImage;
end;

procedure TFPImageFreeTypeDrawer.DrawPixel(x, y: integer; const c: TFPColor
  );
begin
  with GetClipRect do
    if (x < Left) or (y < Top) or (x >= Right) or (y >= Bottom) then exit;
  UnclippedDrawPixel(x,y,c);
end;

procedure TFPImageFreeTypeDrawer.DrawText(AText: string; AFont: TFreeTypeRenderableFont; x, y: single;
  AColor: TFPColor);
begin
  FColor := AColor;
  if AFont.ClearType then
    AFont.RenderText(AText, x, y, GetClipRect, @RenderDirectlyClearType)
  else
    AFont.RenderText(AText, x, y, GetClipRect, @RenderDirectly);
end;

destructor TFPImageFreeTypeDrawer.Destroy;
begin
  inherited Destroy;
end;

{ TFPImageWithScanlineFreeTypeDrawer }

procedure TFPImageWithScanlineFreeTypeDrawer.MoveToPixel(x, y: integer);
begin
  FCurrentColor:= TFPMemoryImageWithScanline(FImage).ScanLine[y]+x;
end;

function TFPImageWithScanlineFreeTypeDrawer.GetCurrentColor: TFPColor;
begin
  result := FCurrentColor^;
end;

procedure TFPImageWithScanlineFreeTypeDrawer.SetCurrentColorAndMoveRight(
  const AColor: TFPColor);
begin
  FCurrentColor^ := AColor;
  inc(FCurrentColor);
end;

procedure TFPImageWithScanlineFreeTypeDrawer.MoveRight;
begin
  inc(FCurrentColor);
end;

function TFPImageWithScanlineFreeTypeDrawer.GetClipRect: TRect;
begin
  result := rect(0,0,FImage.Width,FImage.Height);
end;

constructor TFPImageWithScanlineFreeTypeDrawer.Create(AImage: TFPCustomImage);
begin
  inherited Create(AImage);
  if not (AImage is TFPMemoryImageWithScanline) then
    raise Exception.Create('Scanline not available');
end;

{ TFPMemoryImageWithScanline }

function TFPMemoryImageWithScanline.GetScanline(y: integer): PFPColor;
begin
  if (y < 0) or (y >= Height) then
    raise ERangeError.Create('Scanline out of bounds');
  result := PFPColor(FData)+(y*Width);
end;

procedure TFPMemoryImageWithScanline.SetUsePalette(Value: boolean);
begin
  if Value then
    raise Exception.Create('Palette not supported with scanlines');
end;

end.