File: textrenderer.cpp

package info (click to toggle)
asc 2.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 75,080 kB
  • ctags: 24,943
  • sloc: cpp: 155,023; sh: 8,829; ansic: 6,890; makefile: 650; perl: 138
file content (529 lines) | stat: -rw-r--r-- 14,796 bytes parent folder | download | duplicates (2)
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/***************************************************************************
                          paradialog.cpp  -  description
                             -------------------
    begin                : Thu Feb 21 2002
    copyright            : (C) 2002 by Martin Bickel
    email                : bickel@asc-hq.org
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/



#include <boost/regex.hpp>
#include <pglabel.h>
#include <pgimage.h>

#include "../global.h"

#include "textrenderer.h"
#include "../graphics/surface.h"
#include "../iconrepository.h"
#include "../dialogs/fileselector.h"

void TextRenderer :: TextAttributes :: assign ( PG_Widget* w )
{

   static PG_ThemeWidget* theme = NULL;
   if ( !theme )
      theme = new PG_ThemeWidget( NULL );

   w->SetFontSize( fontsize );
   if ( textcolor > 0 )
      w->SetFontColor ( textcolor );
   else
      w->SetFontColor ( theme->GetFontColor() );
}

TextRenderer :: TextRenderer (PG_Widget *parent, const PG_Rect &r ) : PG_ScrollWidget( parent, r, "ScrollWidget" ), lastWidget(NULL)
{
   SetTransparency(255);
   SetLineSize( scrollsize );
};

TextRenderer :: TextRenderer (PG_Widget *parent, const PG_Rect &r, const std::string& text, const std::string &style) : PG_ScrollWidget( parent, r, style ), lastWidget(NULL)
{
   SetTransparency(255);
   SetText( text );
};


int TextRenderer :: arrangeLine( int y, const Widgets& line, int lineHeight, int indent )
{
   int x = indent;
   for ( Widgets::const_iterator i = line.begin(); i != line.end(); ++i ) {
      (*i)->MoveWidget( x, y + lineHeight- (*i)->Height(), false );
      x += (*i)->Width();
      if ( attributes.find(*i ) != attributes.end() ) {
         x += attributes[*i].spaceAfter;
         if ( attributes[*i].absolutePosition >= 0 )
            x = attributes[*i].absolutePosition;
      }
   }
   return x;
}

int TextRenderer :: AreaWidth()
{
   return max( int(GetListWidth()), Width() - 18 );
}

void TextRenderer :: layout()
{
   int maxx = 0;
   int x = 0;
   int y = 0;
   int lineHeight = 0;
   Widgets currentLine;

   bool breakNow = false;

   int firstLineIndent = 0;
   int furtherLineIndent = 0;
   int indentation = 0;
   int vspace = 0;

   for ( Widgets::iterator i = widgets.begin(); i != widgets.end(); ++i ) {
      if ( (x + (*i)->Width() >= AreaWidth()-2 && x > 0) || breakNow ) {
         maxx = max( arrangeLine( y, currentLine, lineHeight, indentation ), maxx);
         
         y += lineHeight + vspace;

         if ( breakNow )
            indentation = firstLineIndent;
         else
            indentation = furtherLineIndent;

         x = indentation;
         
         lineHeight = 0;
         currentLine.clear();
         breakNow = false;
         vspace = 0;
      }

      currentLine.push_back ( *i );

      if ( lineHeight < (*i)->Height() )
         lineHeight = (*i)->Height();

      x += (*i)->Width();
      Attributes::iterator at = attributes.find(*i );
      if ( at != attributes.end()) {
         x += at->second.spaceAfter;
         if ( at->second.linebreak )
            breakNow = true;
         
         if ( at->second.vspace )
            vspace  = at->second.vspace;

         if ( at->second.firstLineIndent >= 0 )
            firstLineIndent = at->second.firstLineIndent;

         if ( at->second.furtherLineIndent >= 0 )
            furtherLineIndent = at->second.furtherLineIndent;
         
         if ( at->second.absolutePosition >= 0 )
            x = at->second.absolutePosition;
         
      }

   }
   maxx = max(arrangeLine( y, currentLine, lineHeight, indentation ), maxx );

   //! this is a hack to get the scrollbars updated
   new PG_Widget( this, PG_Rect( maxx, y + lineHeight, 1, 1 ));
}

void TextRenderer :: addWidget( Widgets w )
{
   for ( Widgets::iterator i = w.begin(); i != w.end(); ++i )
      addWidget( *i );
}

void TextRenderer :: addWidget( PG_Widget* w )
{
   if ( w ) {
      widgets.push_back( w );
      lastWidget = w;
   }
}

void TextRenderer :: addSpace( int space )
{
   if ( lastWidget )
      attributes[lastWidget].spaceAfter += space * 5;
}

void TextRenderer :: addLinebreak( int pixel, int lines )
{
   if ( lastWidget ) {
      if ( attributes[lastWidget].linebreak ) {
         attributes[lastWidget].vspace += pixel + lines * (textAttributes.fontsize+3);
      } else {
         attributes[lastWidget].linebreak = true;
         attributes[lastWidget].vspace += pixel + (lines-1) * (textAttributes.fontsize+3);
      }
   }
}

void TextRenderer :: addIndentation( int firstLine, int furtherLines )
{
   if ( !lastWidget ) 
      addWidget( new PG_Widget( this, PG_Rect(0,0,0,1)));
   
   if ( firstLine >= 0 )
      attributes[lastWidget].firstLineIndent = firstLine;

   if ( furtherLines >= 0 )
      attributes[lastWidget].furtherLineIndent = furtherLines;

}

void TextRenderer :: addAbsPosition( int pos )
{
   if ( !lastWidget ) 
      addWidget( new PG_Widget( this, PG_Rect(0,0,0,1)));
   
   if ( pos >= 0 )
      attributes[lastWidget].absolutePosition = pos;
}


ASCString TextRenderer :: substr( const ASCString& text, ASCString::const_iterator begin, ASCString::const_iterator end )
{
   return text.substr( begin-text.begin(), end-begin+1);
}

ASCString::const_iterator TextRenderer :: token_command ( const ASCString& text, ASCString::const_iterator start )
{
   ASCString::const_iterator end = start;
   while( end+1 != text.end() && (*end != '#' || end == start ) && !isSpace(*end))
      ++end;

   addWidget( eval_command( substr( text, start, end )));

   return end+1;
}


ASCString::const_iterator TextRenderer :: token ( const ASCString& text, ASCString::const_iterator start )
{
   ASCString::const_iterator end = start;

   while( end+1 != text.end() && !isBreaker(*end) && !isSpace(*end) && *end != '#' )
      ++end;

   if ( isSpace( *end )) {
      addWidget( render ( substr( text, start, end-1)));

      int hspace = 0;
      int vspace = 0;

      while ( end != text.end() && isSpace( *end)) {
         if ( *end == ' ' )
            hspace += 1;
         else
            if ( *end == '\n' ) {
               hspace = 0;
               vspace += 1;
            }

         ++end;
      }
      if ( hspace )
         addSpace( hspace );

      if ( vspace )
         addLinebreak( 0, vspace );

      return end;
   } else {
      if ( *end == '#' ) {
         addWidget( render( substr( text, start, end-1)));
         return end;
      } else {
         addWidget( render( substr( text, start, end)));
         return end+1;
      }
   }
}

void TextRenderer :: parse( const ASCString& text )
{
   textAttributes.fontsize = GetFontSize();
   textAttributes.textcolor = GetFontColor();
   
   ASCString::const_iterator pos = text.begin();

   // skip spaces at beginning to text
   while ( pos != text.end() && isSpace(*pos) )
      ++pos;

   if ( pos == text.end() )
      return;

   while ( pos != text.end() )
      if ( *pos == '#' )
         pos = token_command ( text, pos );
      else
         pos = token ( text, pos );

}

PG_Widget* TextRenderer :: parsingError( const ASCString& errorMessage )
{
   PG_Widget* w = new PG_Label( this );
   textAttributes.assign( w );
   w->SetText( errorMessage );
   w->SetSizeByText();
   w->SetFontColor( 0xff0000 );
   return w;
}

typedef Loki::SingletonHolder< deallocating_vector<TextRenderer::TagRenderer*> > RenderingAddons;

bool TextRenderer::registerTagRenderer ( TextRenderer::TagRenderer* renderer )
{
   RenderingAddons::Instance().push_back( renderer );
   return true;
}




TextRenderer::Widgets TextRenderer :: eval_command( const ASCString& token )
{
   Widgets widgets;

   assert ( token[0] == '#' );
   boost::smatch what;

   static boost::regex size( "#fontsize=(\\d+)#");
   if( boost::regex_match( token, what, size)) {
      ASCString s;
      s.assign( what[1].first, what[1].second );
      textAttributes.fontsize = strtol(s.c_str(), NULL, 0 );
      return widgets;
   }

   static boost::regex image( "#image=(\\S+)#");
   if( boost::regex_match( token, what, image)) {
      ASCString s;
      s.assign( what[1].first, what[1].second );
      Surface& surf = IconRepository::getIcon(s);
      widgets.push_back( new PG_Image( this, PG_Point(0,0), surf.getBaseSurface(), false ));
      return widgets;
   }

   static boost::regex color( "#fontcolor=((0x[a-fA-F\\d]+)|\\d+)#");
   if( boost::regex_match( token, what, color)) {
      ASCString s;
      s.assign( what[1].first, what[1].second );
      textAttributes.textcolor = strtol(s.c_str(), NULL, 0 );
      return widgets;
   }

   static boost::regex defcolor( "#fontcolor=default#");
   if( boost::regex_match( token, what, defcolor)) {
      textAttributes.textcolor = GetFontColor();
      return widgets;
   }

   static boost::regex legacycolor( "#color(\\d+)#");
   if( boost::regex_match( token, what, legacycolor)) {
      ASCString s;
      s.assign( what[1].first, what[1].second );
      int index = strtol(s.c_str(), NULL, 0 );
      if ( index > 0 )
         textAttributes.textcolor = (pal[index][0] << 18) + (pal[index][1] << 10) + (pal[index][2] << 2);
      else
         textAttributes.textcolor = GetFontColor();
      return widgets;
   }

   static boost::regex crtp( "#crtp=?(\\d+)#");
   if( boost::regex_match( token, what, crtp)) {
      ASCString s;
      s.assign( what[1].first, what[1].second );
      addLinebreak( strtol(s.c_str(), NULL, 0 ), 0);
      return widgets;
   }

   static boost::regex crt( "#crt#");
   if( boost::regex_match( token, what, crt)) {
      addLinebreak( 0, 1 );
      return widgets;
   }

   static boost::regex firstindent( "#eeinzug(\\d+)#");
   if( boost::regex_match( token, what, firstindent)) {
      ASCString s;
      s.assign( what[1].first, what[1].second );
      addIndentation( strtol(s.c_str(), NULL, 0 ), -1 );
      return widgets;
   }

   static boost::regex furtherindent( "#aeinzug(\\d+)#");
   if( boost::regex_match( token, what, furtherindent)) {
      ASCString s;
      s.assign( what[1].first, what[1].second );
      addIndentation( -1, strtol(s.c_str(), NULL, 0 ) );
      return widgets;
   }

   static boost::regex indent( "#indent=(\\d+)\\,(\\d+)#");
   if( boost::regex_match( token, what, indent)) {
      ASCString s1;
      s1.assign( what[1].first, what[1].second );
      ASCString s2;
      s2.assign( what[2].first, what[2].second );
      addIndentation( strtol(s1.c_str(), NULL, 0 ), strtol(s2.c_str(), NULL, 0 ) );
      return widgets;
   }

   static boost::regex abspos( "#pos(\\d+)#");
   if( boost::regex_match( token, what, abspos)) {
      ASCString s1;
      s1.assign( what[1].first, what[1].second );
      addAbsPosition( strtol(s1.c_str(), NULL, 0 ) );
      return widgets;
   }


   static boost::regex legacyfont1( "#font0*[1|0]#");
   if( boost::regex_match( token, what, legacyfont1)) {
      textAttributes.fontsize = 12;
      return widgets;
   }

   static boost::regex legacyfont2( "#font0*2#");
   if( boost::regex_match( token, what, legacyfont2)) {
      textAttributes.fontsize = 20;
      return widgets;
   }


   for ( deallocating_vector<TextRenderer::TagRenderer*>::iterator i = RenderingAddons::Instance().begin(); i != RenderingAddons::Instance().end(); ++i ) {
      Widgets w;
      if ( (*i)->renderWidget( token, w, this ))
         return w;
   }

   widgets.push_back ( parsingError ( "unknown token: " + token ) );
   return widgets;
}



      
PG_Widget* TextRenderer :: render( const ASCString& token )
{     
   PG_Widget* w = new PG_Label( this );
   textAttributes.assign( w );
   w->SetText( token );
   w->SetSizeByText();
   w->SizeWidget( w->Width(), textAttributes.fontsize*4/3, false );
   return w;
};

void TextRenderer::clear()
{
   BulkGraphicUpdates bgu( this );
   for ( Widgets::iterator i = widgets.begin(); i != widgets.end(); ++i )
      delete *i;
     
   widgets.clear();
   attributes.clear();
   bgu.release();
}


void TextRenderer::SetText( const string& text )
{
   clear();
   parse(text);
   layout();
   my_text = text;
   Update();
}

void TextRenderer :: saveText( bool stripFormatting )
{
   ASCString name = selectFile( "*.txt", false );
   if ( !name.empty() ) {
      tn_file_buf_stream s( name, tnstream::writing );
      if ( stripFormatting ) {
         static boost::regex tags( "#[^#]+#");
         s.writeString( boost::regex_replace(my_text, tags, ""), true );
      } else {
         s.writeString( my_text, true );
      }
   }
}

bool TextRenderer :: eventKeyDown(const SDL_KeyboardEvent* key)
{
   int mod = SDL_GetModState() & ~(KMOD_NUM | KMOD_CAPS | KMOD_MODE);
   if ( (mod & KMOD_CTRL) &&( key->keysym.sym == SDLK_s )) {
      saveText( mod & KMOD_SHIFT );
      return true;
   }

   int keyStateNum;
   Uint8* keyStates = SDL_GetKeyState ( &keyStateNum );

   if ( (key->keysym.sym == SDLK_UP  && keyStates[SDLK_UP] ) || ( key->keysym.sym == SDLK_KP8  && keyStates[SDLK_KP8] )) {
      ScrollTo( GetScrollPosX (), GetScrollPosY () - scrollsize );
      return true;
   }
   if ( (key->keysym.sym == SDLK_DOWN  && keyStates[SDLK_DOWN]) || (key->keysym.sym == SDLK_KP2  && keyStates[SDLK_KP2] )) {
      ScrollTo( GetScrollPosX (), GetScrollPosY () + scrollsize );
      return true;
   }

   if ( key->keysym.sym == SDLK_PAGEUP  && keyStates[SDLK_PAGEUP] ) {
      ScrollTo( GetScrollPosX (), GetScrollPosY() - (Height() - 10) );
      return true;
   }
   if ( key->keysym.sym == SDLK_PAGEDOWN  && keyStates[SDLK_PAGEDOWN] ) {
      ScrollTo( GetScrollPosX (), GetScrollPosY () + (Height() - 10) );
      return true;
   }


   return false;
}



ViewFormattedText :: ViewFormattedText( const ASCString& title, const ASCString& text, const PG_Rect& pos) : ASC_PG_Dialog( NULL, pos, title )
{
   TextRenderer* pr = new TextRenderer( this, PG_Rect( 10, 40, Width() - 20, Height()-50));
   pr->SetText( text );
};

bool ViewFormattedText :: eventKeyDown(const SDL_KeyboardEvent* key)
{
   switch ( key->keysym.sym ) {
      case SDLK_ESCAPE:
      case SDLK_RETURN:
      case SDLK_KP_ENTER:
      case SDLK_SPACE:
         QuitModal();
         return true;
      default:
         return false;
   }
   return false;
}