~s-cecilio/lomse/master

« back to all changes in this revision

Viewing changes to src/render/lomse_agg_drawer.cpp

  • Committer: cecilios
  • Date: 2010-11-14 17:47:31 UTC
  • Revision ID: git-v1:1fa3764c8c4d338b95b1a537b1e78271170c0025
latest new code. demo_1 tested on linux and win32

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//-------------------------------------------------------------------------------------
 
2
//  This file is part of the Lomse library.
 
3
//  Copyright (c) 2010 Lomse project
 
4
//
 
5
//  Lomse is free software; you can redistribute it and/or modify it under the
 
6
//  terms of the GNU General Public License as published by the Free Software Foundation,
 
7
//  either version 3 of the License, or (at your option) any later version.
 
8
//
 
9
//  Lomse is distributed in the hope that it will be useful, but WITHOUT ANY
 
10
//  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 
11
//  PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 
12
//
 
13
//  You should have received a copy of the GNU General Public License along
 
14
//  with Lomse; if not, see <http://www.gnu.org/licenses/>.
 
15
//  
 
16
//  For any comment, suggestion or feature request, please contact the manager of
 
17
//  the project at cecilios@users.sourceforge.net
 
18
//
 
19
//  Credits:
 
20
//  -------------------------
 
21
//  This file is based on Anti-Grain Geometry version 2.4 examples' code.
 
22
//  Anti-Grain Geometry (AGG) is copyright (C) 2002-2005 Maxim Shemanarev 
 
23
//  (http://www.antigrain.com). AGG 2.4 is distributed under BSD license.
 
24
//
 
25
//-------------------------------------------------------------------------------------
 
26
 
 
27
#include "lomse_agg_drawer.h"
 
28
 
 
29
#include "lomse_exceptions.h"
 
30
#include "lomse_screen_renderer.h"      //PathAttributes
 
31
//#include "lomse_gm_basic.h"
 
32
 
 
33
//using namespace std;
 
34
 
 
35
namespace lomse
 
36
{
 
37
 
 
38
 
 
39
//---------------------------------------------------------------------------------------
 
40
AggDrawer::AggDrawer(path_storage& storage, attr_storage& attr_storage)
 
41
    : Drawer()
 
42
    , m_storage(storage)
 
43
    , m_attr_storage(attr_storage)
 
44
{
 
45
}
 
46
 
 
47
//---------------------------------------------------------------------------------------
 
48
AggDrawer::~AggDrawer()
 
49
{
 
50
}
 
51
 
 
52
////---------------------------------------------------------------------------------------
 
53
//void AggDrawer::remove_all()
 
54
//{
 
55
//    m_storage.remove_all();
 
56
//    m_attr_storage.remove_all();
 
57
//    m_attr_stack.remove_all();
 
58
//    m_transform.reset();
 
59
//}
 
60
 
 
61
//---------------------------------------------------------------------------------------
 
62
void AggDrawer::begin_path()
 
63
{
 
64
    push_attr();
 
65
    unsigned idx = m_storage.start_new_path();
 
66
    m_attr_storage.add(PathAttributes(cur_attr(), idx));
 
67
}
 
68
 
 
69
//---------------------------------------------------------------------------------------
 
70
void AggDrawer::end_path()
 
71
{
 
72
    if(m_attr_storage.size() == 0) 
 
73
    {
 
74
        throw exception("end_path : The path was not begun");
 
75
    }
 
76
    PathAttributes attr = cur_attr();
 
77
    unsigned idx = m_attr_storage[m_attr_storage.size() - 1].index;
 
78
    attr.index = idx;
 
79
    m_attr_storage[m_attr_storage.size() - 1] = attr;
 
80
    pop_attr();
 
81
}
 
82
 
 
83
//---------------------------------------------------------------------------------------
 
84
void AggDrawer::move_to(double x, double y)
 
85
{
 
86
    m_storage.move_to(x, y);
 
87
}
 
88
 
 
89
//---------------------------------------------------------------------------------------
 
90
void AggDrawer::move_to_rel(double x, double y)
 
91
{
 
92
    m_storage.rel_to_abs(&x, &y);
 
93
    m_storage.move_to(x, y);
 
94
}
 
95
 
 
96
//---------------------------------------------------------------------------------------
 
97
void AggDrawer::line_to(double x,  double y)
 
98
{
 
99
    m_storage.line_to(x, y);
 
100
}
 
101
 
 
102
//---------------------------------------------------------------------------------------
 
103
void AggDrawer::line_to_rel(double x,  double y)
 
104
{
 
105
    m_storage.rel_to_abs(&x, &y);
 
106
    m_storage.line_to(x, y);
 
107
}
 
108
 
 
109
//---------------------------------------------------------------------------------------
 
110
void AggDrawer::hline_to(double x)
 
111
{
 
112
    double x2 = 0.0;
 
113
    double y2 = 0.0;
 
114
    if(m_storage.total_vertices())
 
115
    {
 
116
        m_storage.vertex(m_storage.total_vertices() - 1, &x2, &y2);
 
117
        m_storage.line_to(x, y2);
 
118
    }
 
119
}
 
120
 
 
121
//---------------------------------------------------------------------------------------
 
122
void AggDrawer::hline_to_rel(double x)
 
123
{
 
124
    double x2 = 0.0;
 
125
    double y2 = 0.0;
 
126
    if(m_storage.total_vertices())
 
127
    {
 
128
        m_storage.vertex(m_storage.total_vertices() - 1, &x2, &y2);
 
129
        x += x2;
 
130
        m_storage.line_to(x, y2);
 
131
    }
 
132
}
 
133
 
 
134
//---------------------------------------------------------------------------------------
 
135
void AggDrawer::vline_to(double y)
 
136
{
 
137
    double x2 = 0.0;
 
138
    double y2 = 0.0;
 
139
    if(m_storage.total_vertices())
 
140
    {
 
141
        m_storage.vertex(m_storage.total_vertices() - 1, &x2, &y2);
 
142
        m_storage.line_to(x2, y);
 
143
    }
 
144
}
 
145
 
 
146
//---------------------------------------------------------------------------------------
 
147
void AggDrawer::vline_to_rel(double y)
 
148
{
 
149
    double x2 = 0.0;
 
150
    double y2 = 0.0;
 
151
    if(m_storage.total_vertices())
 
152
    {
 
153
        m_storage.vertex(m_storage.total_vertices() - 1, &x2, &y2);
 
154
        y += y2;
 
155
        m_storage.line_to(x2, y);
 
156
    }
 
157
}
 
158
 
 
159
//---------------------------------------------------------------------------------------
 
160
void AggDrawer::cubic_bezier(double x1, double y1, double x,  double y)
 
161
{
 
162
    m_storage.curve3(x1, y1, x, y);
 
163
}
 
164
 
 
165
//---------------------------------------------------------------------------------------
 
166
void AggDrawer::cubic_bezier_rel(double x1, double y1, double x,  double y)
 
167
{
 
168
    m_storage.rel_to_abs(&x1, &y1);
 
169
    m_storage.rel_to_abs(&x,  &y);
 
170
    m_storage.curve3(x1, y1, x, y);
 
171
}
 
172
 
 
173
//---------------------------------------------------------------------------------------
 
174
void AggDrawer::cubic_bezier(double x, double y)
 
175
{
 
176
    m_storage.curve3(x, y);
 
177
}
 
178
 
 
179
//---------------------------------------------------------------------------------------
 
180
void AggDrawer::cubic_bezier_rel(double x, double y)
 
181
{
 
182
    m_storage.curve3_rel(x, y);
 
183
}
 
184
 
 
185
//---------------------------------------------------------------------------------------
 
186
void AggDrawer::quadratic_bezier(double x1, double y1, double x2, double y2, 
 
187
                                 double x,  double y)
 
188
{
 
189
    m_storage.curve4(x1, y1, x2, y2, x, y);
 
190
}
 
191
//---------------------------------------------------------------------------------------
 
192
void AggDrawer::quadratic_bezier_rel(double x1, double y1, double x2, double y2, 
 
193
                                     double x,  double y)
 
194
{
 
195
    m_storage.rel_to_abs(&x1, &y1);
 
196
    m_storage.rel_to_abs(&x2, &y2);
 
197
    m_storage.rel_to_abs(&x,  &y);
 
198
    m_storage.curve4(x1, y1, x2, y2, x, y);
 
199
}
 
200
 
 
201
//---------------------------------------------------------------------------------------
 
202
void AggDrawer::quadratic_bezier(double x2, double y2, double x,  double y)
 
203
{
 
204
    m_storage.curve4(x2, y2, x, y);
 
205
}
 
206
 
 
207
//---------------------------------------------------------------------------------------
 
208
void AggDrawer::quadratic_bezier_rel(double x2, double y2, double x,  double y)
 
209
{
 
210
    m_storage.curve4_rel(x2, y2, x, y);
 
211
}
 
212
 
 
213
//---------------------------------------------------------------------------------------
 
214
void AggDrawer::close_subpath()
 
215
{
 
216
    m_storage.end_poly(path_flags_close);
 
217
}
 
218
 
 
219
//---------------------------------------------------------------------------------------
 
220
PathAttributes& AggDrawer::cur_attr()
 
221
{
 
222
    if(m_attr_stack.size() == 0)
 
223
    {
 
224
        throw exception("cur_attr : Attribute stack is empty");
 
225
    }
 
226
    return m_attr_stack[m_attr_stack.size() - 1];
 
227
}
 
228
 
 
229
//---------------------------------------------------------------------------------------
 
230
void AggDrawer::push_attr()
 
231
{
 
232
    m_attr_stack.add(m_attr_stack.size() ? 
 
233
                        m_attr_stack[m_attr_stack.size() - 1] :
 
234
                        PathAttributes());
 
235
}
 
236
 
 
237
//---------------------------------------------------------------------------------------
 
238
void AggDrawer::pop_attr()
 
239
{
 
240
    if(m_attr_stack.size() == 0)
 
241
    {
 
242
        throw exception("pop_attr : Attribute stack is empty");
 
243
    }
 
244
    m_attr_stack.remove_last();
 
245
}
 
246
 
 
247
//---------------------------------------------------------------------------------------
 
248
void AggDrawer::fill(const rgba8& f)
 
249
{
 
250
    PathAttributes& attr = cur_attr();
 
251
    attr.fill_color = f;
 
252
    attr.fill_flag = true;
 
253
}
 
254
 
 
255
//---------------------------------------------------------------------------------------
 
256
void AggDrawer::stroke(const rgba8& s)
 
257
{
 
258
    PathAttributes& attr = cur_attr();
 
259
    attr.stroke_color = s;
 
260
    attr.stroke_flag = true;
 
261
}
 
262
 
 
263
//---------------------------------------------------------------------------------------
 
264
void AggDrawer::even_odd(bool flag)
 
265
{
 
266
    cur_attr().even_odd_flag = flag;
 
267
}
 
268
 
 
269
//---------------------------------------------------------------------------------------
 
270
void AggDrawer::stroke_width(double w)
 
271
{
 
272
    cur_attr().stroke_width = w;
 
273
}
 
274
 
 
275
//---------------------------------------------------------------------------------------
 
276
void AggDrawer::fill_none()
 
277
{
 
278
    cur_attr().fill_flag = false;
 
279
}
 
280
 
 
281
//---------------------------------------------------------------------------------------
 
282
void AggDrawer::stroke_none()
 
283
{
 
284
    cur_attr().stroke_flag = false;
 
285
}
 
286
 
 
287
//---------------------------------------------------------------------------------------
 
288
void AggDrawer::fill_opacity(double op)
 
289
{
 
290
    cur_attr().fill_color.opacity(op);
 
291
}
 
292
 
 
293
//---------------------------------------------------------------------------------------
 
294
void AggDrawer::stroke_opacity(double op)
 
295
{
 
296
    cur_attr().stroke_color.opacity(op);
 
297
}
 
298
 
 
299
//---------------------------------------------------------------------------------------
 
300
void AggDrawer::line_join(line_join_e join)
 
301
{
 
302
    cur_attr().line_join = join;
 
303
}
 
304
 
 
305
//---------------------------------------------------------------------------------------
 
306
void AggDrawer::line_cap(line_cap_e cap)
 
307
{
 
308
    cur_attr().line_cap = cap;
 
309
}
 
310
 
 
311
//---------------------------------------------------------------------------------------
 
312
void AggDrawer::miter_limit(double ml)
 
313
{
 
314
    cur_attr().miter_limit = ml;
 
315
}
 
316
 
 
317
//---------------------------------------------------------------------------------------
 
318
trans_affine& AggDrawer::transform()
 
319
{
 
320
    return cur_attr().transform;
 
321
}
 
322
 
 
323
//---------------------------------------------------------------------------------------
 
324
void AggDrawer::add_path(VertexSource& vs,  unsigned path_id, bool solid_path)
 
325
{
 
326
    m_storage.concat_path(vs, path_id);
 
327
}
 
328
 
 
329
 
 
330
 
 
331
 
 
332
////---------------------------------------------------------------------------------------
 
333
//void AggDrawer::flip_text(bool flip)
 
334
//{
 
335
//    m_fontEngine.flip_y(flip);
 
336
//}
 
337
//
 
338
////---------------------------------------------------------------------------------------
 
339
//void AggDrawer::font(const char* fontName, double height, bool bold, bool italic,
 
340
//                     FontCacheType ch, double angle)
 
341
//{
 
342
    //m_textAngle = angle;
 
343
    //m_fontHeight = height;
 
344
    //m_fontCacheType = ch;
 
345
 
 
346
    //m_fontEngine.load_font(fontName, 0,
 
347
    //                       (ch == VectorFontCache) ?
 
348
    //                            agg::glyph_ren_outline :
 
349
    //                            agg::glyph_ren_agg_gray8);
 
350
    //m_fontEngine.hinting(m_textHints);
 
351
    //m_fontEngine.height((ch == VectorFontCache) ? height : worldToScreen(height));
 
352
//}
 
353
 
 
354
////---------------------------------------------------------------------------------------
 
355
//double AggDrawer::font_height() const
 
356
//{
 
357
//   return m_fontHeight;
 
358
//}
 
359
//
 
360
////---------------------------------------------------------------------------------------
 
361
//void AggDrawer::text_alignment(TextAlignment alignX, TextAlignment alignY)
 
362
//{
 
363
//   m_textAlignX = alignX;
 
364
//   m_textAlignY = alignY;
 
365
//}
 
366
//
 
367
////---------------------------------------------------------------------------------------
 
368
//double AggDrawer::text_width(const char* str)
 
369
//{
 
370
//    double x = 0;
 
371
//    double y = 0;
 
372
//    bool first = true;
 
373
//    while(*str)
 
374
//    {
 
375
//        const agg::glyph_cache* glyph = m_fontCacheManager.glyph(*str);
 
376
//        if(glyph)
 
377
//        {
 
378
//            if(!first) m_fontCacheManager.add_kerning(&x, &y);
 
379
//            x += glyph->advance_x;
 
380
//            y += glyph->advance_y;
 
381
//            first = false;
 
382
//        }
 
383
//        ++str;
 
384
//    }
 
385
//    return (m_fontCacheType == VectorFontCache) ? x : screenToWorld(x);
 
386
//}
 
387
//
 
388
////---------------------------------------------------------------------------------------
 
389
//bool AggDrawer::text_hints() const
 
390
//{
 
391
//   return m_textHints;
 
392
//}
 
393
//
 
394
////---------------------------------------------------------------------------------------
 
395
//void AggDrawer::text_hints(bool hints)
 
396
//{
 
397
//   m_textHints = hints;
 
398
//}
 
399
 
 
400
//---------------------------------------------------------------------------------------
 
401
void AggDrawer::text(double x, double y, const char* str, bool roundOff,
 
402
                     double ddx, double ddy)
 
403
{
 
404
//    double dx = 0.0;
 
405
//    double dy = 0.0;
 
406
//
 
407
//    switch(m_textAlignX)
 
408
//    {
 
409
//        case AlignCenter:  dx = -textWidth(str) * 0.5; break;
 
410
//        case AlignRight:   dx = -textWidth(str);       break;
 
411
//        default: break;
 
412
//    }
 
413
//
 
414
//
 
415
//    double asc = font_height();
 
416
//    const agg::glyph_cache* glyph = m_fontCacheManager.glyph('H');
 
417
//    if(glyph)
 
418
//    {
 
419
//        asc = glyph->bounds.y2 - glyph->bounds.y1;
 
420
//    }
 
421
//
 
422
//    if(m_fontCacheType == RasterFontCache)
 
423
//    {
 
424
//        asc = screenToWorld(asc);
 
425
//    }
 
426
//
 
427
//    switch(m_textAlignY)
 
428
//    {
 
429
//        case AlignCenter:  dy = -asc * 0.5; break;
 
430
//        case AlignTop:     dy = -asc;       break;
 
431
//        default: break;
 
432
//    }
 
433
//
 
434
//    if(m_fontEngine.flip_y()) dy = -dy;
 
435
//
 
436
//    agg::trans_affine  mtx;
 
437
//
 
438
//    double start_x = x + dx;
 
439
//    double start_y = y + dy;
 
440
//
 
441
//    if (roundOff)
 
442
//    {
 
443
//        start_x = int(start_x);
 
444
//        start_y = int(start_y);
 
445
//    }
 
446
//    start_x += ddx;
 
447
//    start_y += ddy;
 
448
//
 
449
//    mtx *= agg::trans_affine_translation(-x, -y);
 
450
//    mtx *= agg::trans_affine_rotation(m_textAngle);
 
451
//    mtx *= agg::trans_affine_translation(x, y);
 
452
//
 
453
//    agg::conv_transform<FontCacheManager::path_adaptor_type> tr(m_fontCacheManager.path_adaptor(), mtx);
 
454
//
 
455
//    if(m_fontCacheType == RasterFontCache)
 
456
//    {
 
457
//        worldToScreen(start_x, start_y);
 
458
//    }
 
459
//
 
460
//    int i;
 
461
//    for (i = 0; str[i]; i++)
 
462
//    {
 
463
//        glyph = m_fontCacheManager.glyph(str[i]);
 
464
//        if(glyph)
 
465
//        {
 
466
//            if(i) m_fontCacheManager.add_kerning(&start_x, &start_y);
 
467
//            m_fontCacheManager.init_embedded_adaptors(glyph, start_x, start_y);
 
468
//
 
469
//            if(glyph->data_type == agg::glyph_data_outline)
 
470
//            {
 
471
//                m_path.remove_all();
 
472
//                //m_path.add_path(tr, 0, false);
 
473
//                              m_path.concat_path(tr,0); // JME
 
474
//                drawPath();
 
475
//            }
 
476
//
 
477
//            if(glyph->data_type == agg::glyph_data_gray8)
 
478
//            {
 
479
//                render(m_fontCacheManager.gray8_adaptor(),
 
480
//                       m_fontCacheManager.gray8_scanline());
 
481
//            }
 
482
//            start_x += glyph->advance_x;
 
483
//            start_y += glyph->advance_y;
 
484
//        }
 
485
//    }
 
486
}
 
487
 
 
488
 
 
489
 
 
490
}  //namespace lomse