File: trans_curve1_ft.dpr

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 (450 lines) | stat: -rw-r--r-- 9,311 bytes parent folder | download | duplicates (6)
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
//
// AggPas 2.4 RM3 Demo application
// Note: Press F1 key on run to see more info about this demo
//
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
//
program
 trans_curve1_ft ;

{DEFINE AGG_GRAY8 }
{$DEFINE AGG_BGR24 }
{DEFINE AGG_RGB24 }
{DEFINE AGG_BGRA32 }
{DEFINE AGG_RGBA32 }
{DEFINE AGG_ARGB32 }
{DEFINE AGG_ABGR32 }
{DEFINE AGG_RGB565 }
{DEFINE AGG_RGB555 }

uses
 agg_basics ,
 agg_platform_support ,

 agg_ctrl ,
 agg_cbox_ctrl ,
 agg_slider_ctrl ,

 agg_rasterizer_scanline_aa ,
 agg_scanline ,
 agg_scanline_p ,

 agg_rendering_buffer ,
 agg_renderer_base ,
 agg_renderer_scanline ,
 agg_render_scanlines ,

 agg_conv_curve ,
 agg_conv_transform ,
 agg_conv_bspline ,
 agg_conv_segmentator ,
 agg_conv_stroke ,
 agg_font_freetype ,
 agg_font_cache_manager ,
 agg_trans_single_path ,
 interactive_polygon_

{$I pixel_formats.inc }
{$I agg_mode.inc }

const
 flip_y = true;

 text_ : PChar =
  'Anti-Grain Geometry is designed as a set of loosely coupled ' +
  'algorithms and class templates united with a common idea, ' +
  'so that all the components can be easily combined. Also, ' +
  'the template based design allows you to replace any part of ' +
  'the library without the necessity to modify a single byte in ' +
  'the existing code. ';

type
 the_application = object(platform_support )
   m_feng : font_engine_freetype_int16;
   m_fman : font_cache_manager;
   m_poly : interactive_polygon;

   m_num_points       : slider_ctrl;
   m_close            ,
   m_preserve_x_scale ,
   m_fixed_len        ,
   m_animate          : cbox_ctrl;

   m_dx ,
   m_dy : array[0..5 ] of double;

   m_prev_animate : boolean;

   constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
   destructor  Destruct;

   procedure on_init; virtual;
   procedure on_draw; virtual;

   procedure on_mouse_move       (x ,y : int; flags : unsigned ); virtual;
   procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;
   procedure on_mouse_button_up  (x ,y : int; flags : unsigned ); virtual;

   procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;
   procedure on_ctrl_change; virtual;

   procedure move_point(x ,y ,dx ,dy : double_ptr );

   procedure on_idle; virtual;

  end;

{ CONSTRUCT }
constructor the_application.Construct;
begin
 inherited Construct(format_ ,flip_y_ );

 m_feng.Construct;
 m_fman.Construct(@m_feng );
 m_poly.Construct(6 ,5.0 );

 m_num_points.Construct      (5.0 ,5.0  ,340.0 ,12.0 ,not flip_y_ );
 m_close.Construct           (350 ,5.0  ,'Close' ,not flip_y_ );
 m_preserve_x_scale.Construct(460 ,5.0  ,'Preserve X scale' ,not flip_y_ );
 m_fixed_len.Construct       (350 ,25.0 ,'Fixed Length' ,not flip_y_ );
 m_animate.Construct         (460 ,25.0 ,'Animate' ,not flip_y_ );

 m_prev_animate:=false;

 add_ctrl(@m_close );
 add_ctrl(@m_preserve_x_scale );
 add_ctrl(@m_fixed_len );
 add_ctrl(@m_animate );

 m_preserve_x_scale.status_(true );
 m_fixed_len.status_       (true );

 m_num_points.range_(10.0 ,400.0 );
 m_num_points.value_(200.0 );
 m_num_points.label_('Number of intermediate Points = %.3f' );

 add_ctrl(@m_num_points );

end;

{ DESTRUCT }
destructor the_application.Destruct;
begin
 inherited Destruct;

 m_poly.Destruct;

 m_num_points.Destruct;
 m_close.Destruct;
 m_preserve_x_scale.Destruct;
 m_fixed_len.Destruct;
 m_animate.Destruct;

 m_feng.Destruct;
 m_fman.Destruct;

end;

{ ON_INIT }
procedure the_application.on_init;
begin
 m_poly.xn_ptr(0 )^:=50;
 m_poly.yn_ptr(0 )^:=50;
 m_poly.xn_ptr(1 )^:=150 + 20;
 m_poly.yn_ptr(1 )^:=150 - 20;
 m_poly.xn_ptr(2 )^:=250 - 20;
 m_poly.yn_ptr(2 )^:=250 + 20;
 m_poly.xn_ptr(3 )^:=350 + 20;
 m_poly.yn_ptr(3 )^:=350 - 20;
 m_poly.xn_ptr(4 )^:=450 - 20;
 m_poly.yn_ptr(4 )^:=450 + 20;
 m_poly.xn_ptr(5 )^:=550;
 m_poly.yn_ptr(5 )^:=550;

end;

{ ON_DRAW }
procedure the_application.on_draw;
var
 pixf : pixel_formats;
 rgba : aggclr;

 rb : renderer_base;
 r  : renderer_scanline_aa_solid;
 sl : scanline_p8;

 ras : rasterizer_scanline_aa;

 path    : simple_polygon_vertex_source;
 bspline : conv_bspline;
 tcurve  : trans_single_path;
 fcurves : conv_curve;
 fsegm   : conv_segmentator;
 ftrans  : conv_transform;

 x ,y : double;

 p : int8u_ptr;

 glyph : glyph_cache_ptr;

 stroke : conv_stroke;

begin
// Initialize structures
 pixfmt(pixf ,rbuf_window );

 rb.Construct(@pixf );
 r.Construct (@rb );

 rgba.ConstrDbl(1 ,1 ,1 );
 rb.clear      (@rgba );

 sl.Construct;
 ras.Construct;

 m_poly.close_(m_close._status );

// Render the text
 path.Construct   (m_poly.polygon ,m_poly.num_points ,false ,m_close._status );
 bspline.Construct(@path );

 bspline.interpolation_step_(1.0 / m_num_points._value );

 tcurve.Construct;
 tcurve.add_path         (@bspline );
 tcurve.preserve_x_scale_(m_preserve_x_scale._status );

 if m_fixed_len._status then
  tcurve.base_length_(1120 );

 fcurves.Construct(m_fman.path_adaptor );
 fsegm.Construct  (@fcurves );
 ftrans.Construct (@fsegm ,@tcurve );

 fsegm.approximation_scale_  (3.0 );
 fcurves.approximation_scale_(2.0 );

 if m_feng.load_font('timesi.ttf' ,0 ,glyph_ren_outline ) then
  begin
   x:=0.0;
   y:=3.0;
   p:=@text_[0 ];

   m_feng.hinting_(false );
   m_feng.height_ (40.0 );

   while p^ <> 0 do
    begin
     glyph:=m_fman.glyph(p^ );

     if glyph <> NIL then
      begin
       if x > tcurve.total_length then
        break;

       m_fman.add_kerning           (@x ,@y );
       m_fman.init_embedded_adaptors(glyph ,x ,y );

       if glyph.data_type = glyph_data_outline then
        begin
         ras.reset;
         ras.add_path(@ftrans );

         rgba.ConstrInt(0 ,0 ,0 );
         r.color_      (@rgba );

         render_scanlines(@ras ,@sl ,@r );

        end;

      // increment pen position
       x:=x + glyph.advance_x;
       y:=y + glyph.advance_y;

      end;

     inc(ptrcomp(p ) ,sizeof(int8u ) );

    end;

  end
 else
  message_(
   'Please copy file timesi.ttf to the current directory'#13 +
   'or download it from http://www.antigrain.com/timesi.zip' );

// Render the path curve
 stroke.Construct(@bspline );
 stroke.width_   (2.0 );

 rgba.ConstrInt(170 ,50 ,20 ,100 );
 r.color_      (@rgba );

 ras.add_path    (@stroke );
 render_scanlines(@ras ,@sl ,@r );

// Render the "poly" tool
 rgba.ConstrDbl(0 ,0.3 ,0.5 ,0.3 );
 r.color_      (@rgba );

 ras.add_path    (@m_poly );
 render_scanlines(@ras ,@sl ,@r );

// Render the controls
 render_ctrl(@ras ,@sl ,@r ,@m_close );
 render_ctrl(@ras ,@sl ,@r ,@m_preserve_x_scale );
 render_ctrl(@ras ,@sl ,@r ,@m_fixed_len );
 render_ctrl(@ras ,@sl ,@r ,@m_animate );
 render_ctrl(@ras ,@sl ,@r ,@m_num_points );

// Free AGG resources
 sl.Destruct;
 ras.Destruct;

 bspline.Destruct;
 tcurve.Destruct;
 fcurves.Destruct;
 fsegm.Destruct;

 stroke.Destruct;

end;

{ ON_MOUSE_MOVE }
procedure the_application.on_mouse_move;
begin
 if flags and mouse_left <> 0 then
  if m_poly.on_mouse_move(x ,y ) then
   force_redraw;

 if flags and mouse_left = 0 then
  on_mouse_button_up(x ,y ,flags );

end;

{ ON_MOUSE_BUTTON_DOWN }
procedure the_application.on_mouse_button_down;
begin
 if flags and mouse_left <> 0 then
  if m_poly.on_mouse_button_down(x ,y ) then
   force_redraw;

end;

{ ON_MOUSE_BUTTON_UP }
procedure the_application.on_mouse_button_up;
begin
 if m_poly.on_mouse_button_up(x ,y ) then
  force_redraw;

end;

{ ON_KEY }
procedure the_application.on_key;
begin
 if key = key_f1 then
  message_(
   'This is a "kinda-cool-stuff" demo that performs non-linear transformations and '#13 +
   'draws vector text along a curve. Note that it''s not just calculating of the glyph '#13 +
   'angles and positions, they are transformed as if they were elastic. The curve is '#13 +
   'calculated as a bicubic spline. The option "Preserve X scale" makes the converter '#13 +
   'distribute all the points uniformly along the curve. If it''s unchechked, the scale '#13 +
   'will be proportional to the distance between the control points.' +
   #13#13'Note: F2 key saves current "screenshot" file in this demo''s directory.  ' );

end;

{ ON_CTRL_CHANGE }
procedure the_application.on_ctrl_change;
var
 i : int;

begin
 if m_animate._status <> m_prev_animate then
  begin
   if m_animate._status then
    begin
     on_init;

     for i:=0 to 5 do
      begin
       m_dx[i ]:=((Random($7fff ) mod 1000 ) - 500 ) * 0.01;
       m_dy[i ]:=((Random($7fff ) mod 1000 ) - 500 ) * 0.01;

      end;

     wait_mode_(false );

    end
   else
    wait_mode_(true );

   m_prev_animate:=m_animate._status;

  end;

end;

{ MOVE_POINT }
procedure the_application.move_point;
begin
 if x^ < 0.0 then
  begin
   x^ :=0.0;
   dx^:=-dx^;

  end;

 if x^ > _width then
  begin
   x^ :=_width;
   dx^:=-dx^;

  end;

 if y^ < 0.0 then
  begin
   y^ :=0.0;
   dy^:=-dy^;

  end;

 if y^ > _height then
  begin
   y^ :=_height;
   dy^:=-dy^;

  end;

 x^:=x^ + dx^;
 y^:=y^ + dy^;

end;

{ ON_IDLE }
procedure the_application.on_idle;
var
 i : int;

begin
 for i:=0 to 5 do
  move_point(m_poly.xn_ptr(i ) ,m_poly.yn_ptr(i ) ,@m_dx[i ] ,@m_dy[i ] );

 force_redraw;

end;

VAR
 app : the_application;

BEGIN
 app.Construct(pix_format ,flip_y );
 app.caption_ ('AGG Example. Non-linear "Along-A-Curve" Transformer - FreeType (F1-Help)' );

 if app.init(600 ,600 ,window_resize ) then
  app.run;

 app.Destruct;

END.