File: TextFormatterXLP.m

package info (click to toggle)
helpviewer.app 0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,940 kB
  • sloc: objc: 2,416; makefile: 12
file content (593 lines) | stat: -rw-r--r-- 18,997 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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
    This file is part of HelpViewer (http://www.roard.com/helpviewer)
    Copyright (C) 2003      Nicolas Roard (nicolas@roard.com)
                  2020-2021 Riccardo Mottola <rm@gnu.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.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the
    Free Software Foundation, Inc.  
    51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

#include "TextFormatterXLP.h"

#define HAVING(str) ([[elementName lowercaseString] isEqualToString: str])

@implementation TextFormatterXLP

- (id) init
{
  if ((self = [super init]))
    {
      _firstSection = [[Section alloc] initWithHeader: @"document"];
      _listCounter = [[NSMutableArray alloc] init];
      legends = nil;
      _currentSection = _firstSection;
      _document = NO;
      Bundle = nil;
      content = nil;
    }
  return self;
}

- (void) startElement: (NSString*) elementName attributes: (NSMutableDictionary*) elementAttributes {
    if (_pre)
    {
    	// verbatim mode
	id tag = [[NSString alloc] initWithFormat: @"<%@", elementName];
	id str = [[NSMutableAttributedString alloc] initWithString: tag];

	[tag release];
	
	NSEnumerator *enumerator = [elementAttributes keyEnumerator];
	id key;

	while ((key = [enumerator nextObject])) 
	{
		id strelem = [[NSString alloc] initWithFormat: @" %@=%@",
			key, [elementAttributes objectForKey: key]];
		id astrelem = [[NSMutableAttributedString alloc] initWithString: strelem];
		[str appendAttributedString: astrelem];
		[astrelem release];
		[strelem release];
	}
	id strend = [[NSMutableAttributedString alloc] initWithString: @">"];

	[_currentContent appendAttributedString: str];
	[_currentContent appendAttributedString: strend];

	[str release];
	[strend release];
    }
    else
    {
    	if HAVING (@"b")
    	{
		_bold = YES;
    	}
	else if HAVING (@"i")
	{
		_italic = YES;
	}
	else if HAVING (@"sc")
	{
		_smallcaps = YES;
	}
	else if HAVING (@"code")
	{
		_code = YES;
	}
	else if HAVING (@"pre")
	{
		_pre = YES;
		_code = YES;
	}
	else if HAVING (@"url")
	{
		_url = YES;
	}
	else if HAVING (@"br")
	{
		id str = [[NSMutableAttributedString alloc] initWithString: @"\n"];
		if ((_legendfig) || (_note) || (_caution) || (_listing) || (_information))
		{
			[_string appendAttributedString: str];
		}
		else
		{
			[_currentContent appendAttributedString: str];
		}
		[str release];
	}
        else if HAVING (@"ol") { _ol = YES; _listLevel ++;
            [_listCounter addObject: [NSDecimalNumber one]];
        }
        else if HAVING (@"ul") { _ul = YES; _listLevel ++;
            [_listCounter addObject: [NSDecimalNumber one]];
        }
        else if HAVING (@"li") {
            NSMutableString* add = [NSMutableString string];
            NSMutableParagraphStyle* paragraphStyle = [NSMutableParagraphStyle
            new];
            //defaultParagraphStyle];
            NSMutableAttributedString* AS;
            int i = 0;

            [add appendString: @"\n"];
            //NSLog (@"on a li, listlevel : %d", _listLevel);

            if (_listLevel > 0)
            {
                for (i = 0; i< _listLevel; i++)
                {
                    [add appendString: @"  "];
                }
                if (_ol)
                {
                    NSDecimalNumber* counter = [_listCounter lastObject];
                    [add appendString: [counter stringValue]];
                    counter = [counter decimalNumberByAdding: [NSDecimalNumber one]];
                    [_listCounter replaceObjectAtIndex: [_listCounter count] -1 withObject: counter];
                    [add appendString: @". "];
                }
                else if (_ul)
                {
                    [add appendString: @" "];
                }
            }
            _li = YES;
            //printf ("on ajoute la chaine : <%s>\n", [add cString]);
            AS = [[NSMutableAttributedString alloc] initWithString: add];
            NSMutableAttributedString* space = [[NSMutableAttributedString alloc] initWithString: @" "];
	    if (_listLevel > 0) [self addImage: [NSImage imageNamed: @"Point2.tiff"] onString: AS];
	    [AS appendAttributedString: space];
	    [space release];
            //[paragraphStyle setAlignment: NSLeftTextAlignment];
            [paragraphStyle setHeadIndent: 16.0];
            //[paragraphStyle setTailIndent: 100.0];
            [AS addAttribute: NSParagraphStyleAttributeName
                value: paragraphStyle
                range: NSMakeRange(0,[AS length])];
	    [paragraphStyle release];
	    [_currentContent appendAttributedString: AS];
            RELEASE (AS);
        }
        else if HAVING (@"img") {
            NSString* src = nil;
            if ([elementAttributes objectForKey: @"src"] != nil)
            {
                src = [NSString stringWithString: [elementAttributes objectForKey: @"src"]];
                //NSLog (@"src : %@", src);
                [self addImage: [Bundle pathForResource: [src stringByDeletingPathExtension] ofType: [src pathExtension]]];
            }
        }
        else if HAVING (@"legendfig") {
            _legendfig = YES; 
            RELEASE (legends);
            legends = [[NSMutableArray alloc] init];

            RELEASE (imgSource);
            if ([elementAttributes objectForKey: @"src"] != nil)
            {
                imgSource = [[NSString alloc] initWithString: [elementAttributes objectForKey: @"src"]];
                //NSLog (@"src : %@", imgSource);
            }
        }
        else if HAVING (@"legend") {
            int x, y;
            x = y = 0;
            _preString = _string;
            _string = [[NSMutableAttributedString alloc] init];
         
            if ([elementAttributes objectForKey: @"x"] != nil)
            {
                x = [[elementAttributes objectForKey: @"x"] intValue];
            }

            if ([elementAttributes objectForKey: @"y"] != nil)
            {
                y = [[elementAttributes objectForKey: @"y"] intValue];
            }

            legendX = x;
            legendY = y;
        }
	else if HAVING (@"note") {
		_note = YES;
		_preString = _string;
		_string = [[NSMutableAttributedString alloc] init];
	}
	else if HAVING (@"listing") {
		_listing = YES;
		_preString = _string;
		_string = [[NSMutableAttributedString alloc] init];
	}
	else if HAVING (@"caution") {
		_caution = YES;
		_preString = _string;
		_string = [[NSMutableAttributedString alloc] init];
	}
	else if HAVING (@"information") {
		_information = YES;
		_preString = _string;
		_string = [[NSMutableAttributedString alloc] init];
	}
    }
}

- (void) endElement: (NSString*) elementName {

	if HAVING (@"pre")
	{
		_pre = NO;
		_code = NO;
	}
	if (_pre)
	{
		id tag = [[NSString alloc] initWithFormat: @"</%@>", elementName];
		id str = [[NSMutableAttributedString alloc] initWithString: tag];
		[_currentContent appendAttributedString: str];
		[str release];
		[tag release];
	}
	else 
	{
		if HAVING (@"b")
		{
			_bold = NO;
		}
		else if HAVING (@"i")
		{
			_italic = NO;
		}
		else if HAVING (@"sc")
		{
			_smallcaps = NO;
		}
		else if HAVING (@"code")
		{
			_code = NO;
		}
		else if HAVING (@"url")
		{
			_url = NO;
		}
		else if HAVING (@"ol") {
		    _ol = NO;
		    _listLevel --;
		    [_listCounter removeObjectAtIndex: [_listCounter count] -1];
		}
		else if HAVING (@"ul") {
		    _ul = NO;
		    _listLevel --;
		}
		else if HAVING (@"li") _li = NO;
		else if HAVING (@"legendfig") {
		    _legendfig = NO;
		    [self addLegendFig: imgSource withLegends: legends];
		}
		else if HAVING (@"legend") {
		    Legend* current = [Legend legendWithString: _string andPoint: NSMakePoint (legendX, legendY)];
		    RELEASE (_string);
		    _string = _preString;
		    [legends addObject: current];
		}
		else if HAVING (@"note") {
			[self addNote: _string 
				withImage: [NSImage imageNamed: @"note.png"]
				withColor: [NSColor colorWithCalibratedRed: 0.81 green: 0.84 blue: 0.88 alpha:1.0]];
			RELEASE (_string);
			_string = _preString;
			_note = NO;
		}
		else if HAVING (@"listing") {
			[self addNote: _string 
				withImage: [NSImage imageNamed: @"listing.png"]
				withColor: [NSColor colorWithCalibratedRed: 0.88 green: 0.88 blue: 0.88 alpha:1.0]];
			RELEASE (_string);
			_string = _preString;
			_listing = NO;
		}
		else if HAVING (@"caution") {
			[self addNote: _string 
				withImage: [NSImage imageNamed: @"caution.png"]
				withColor: [NSColor colorWithCalibratedRed: 0.88 green: 0.78 blue: 0.78 alpha:1.0]];
			RELEASE (_string);
			_string = _preString;
			_caution = NO;
		}
		else if HAVING (@"information") {
			[self addNote: _string 
				withImage: [NSImage imageNamed: @"information.png"]
				withColor: [NSColor colorWithCalibratedRed: 0.79 green: 0.89 blue: 0.78 alpha:1.0]];
			RELEASE (_string);
			_string = _preString;
			_information = NO;
		}
	}
}

- (void) characters: (NSString*) name {
    NSMutableDictionary* attr = [NSMutableDictionary dictionaryWithCapacity: 2];
    NSFont* font = nil;
    NSFontTraitMask FontMask = 0;
    int FontSize = 12;

        if (_bold) FontMask = NSBoldFontMask | FontMask;
	if (_italic) FontMask = NSItalicFontMask | FontMask;
	if (_smallcaps) FontMask = NSSmallCapsFontMask | FontMask;
	if (_code || _url) font = [NSFont userFixedPitchFontOfSize: FontSize];
	else font = [NSFont userFontOfSize: FontSize];

        font = [[NSFontManager sharedFontManager]
                    convertFont: font
                    toHaveTrait: FontMask];

        //[attr setObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
        [attr setObject: font forKey: NSFontAttributeName];
        
	id str = [[NSMutableAttributedString alloc] initWithString: name attributes: attr];

	if ((_legendfig) || (_note) || (_caution) || (_listing) || (_information))
	{
		[_string appendAttributedString: str];
	}
	else
	{
		[_currentContent appendAttributedString: str];
	}

	[str release];
}

- (void) setBundle: (NSBundle*) bundle {
	ASSIGN (Bundle, bundle);
}

- (NSMutableAttributedString*) renderHeader: (NSString*) header withLevel: (int) level
{
    NSMutableDictionary* attr = [NSMutableDictionary dictionaryWithCapacity: 2];
    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    NSFont* font;
    NSFontTraitMask FontMask = 0;

    [paragraphStyle setAlignment: NSLeftTextAlignment];

    CGFloat fontSize = 12.0;
    int ruleHeight = 0;

    switch (level)
    {
	case 0: 
		fontSize = 24; 
		FontMask = NSBoldFontMask | FontMask;
		ruleHeight = 3;
		break;
	case 1: 
		fontSize = 20; 
		FontMask = NSBoldFontMask | FontMask;
		ruleHeight = 1;
		break;
	case 2: 
		fontSize = 16; 
		FontMask = NSBoldFontMask | FontMask;
		ruleHeight = 1;
		break;
	default:
		fontSize = 12;
		FontMask = NSBoldFontMask | FontMask;
    }

    font = [[NSFontManager sharedFontManager]
                convertFont: [NSFont userFontOfSize: fontSize]
                toHaveTrait: FontMask];

    //[attr setObject: paragraphStyle forKey: NSParagraphStyleAttributeName];
    [attr setObject: font forKey: NSFontAttributeName];

    [paragraphStyle release];

    NSMutableAttributedString* ret = [[NSMutableAttributedString alloc] init];
    NSMutableAttributedString* astring = [[NSMutableAttributedString alloc] initWithString: @"\n"];
    if (level) {
    	[ret appendAttributedString: astring];
    	[ret appendAttributedString: astring];
    }
    NSMutableAttributedString* ahead = [[NSMutableAttributedString alloc] initWithString: header attributes: attr];
    [ret appendAttributedString: ahead];
    [ahead release];
    if (ruleHeight) 
    {	
	[self addRuleTo: ret withHeight: ruleHeight];
    }
    [ret appendAttributedString: astring];
    [astring release];

    return AUTORELEASE(ret);
}

- (void) addRuleTo: (NSMutableAttributedString*) string withHeight: (CGFloat) height {
    if (height> 0)
    {
	    NSTextAttachment* BR = [[NSTextAttachment alloc] init];
    	    NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc] init];

	    BRCell* attachCell = [BRCell sharedBRCell];
	    [BR setAttachmentCell: attachCell];

	    NSMutableAttributedString* attStr = [[NSMutableAttributedString alloc] init];
	    [attStr appendAttributedString: [NSAttributedString attributedStringWithAttachment: BR]];
    	    
	    NSFont* font = [[NSFontManager sharedFontManager]
                convertFont: [NSFont userFontOfSize: 1]
                toHaveTrait: 0];

	
    	    //[attStr addAttribute: NSFontAttributeName value: font range: NSMakeRange (0, [attSStr length])];
	    [paragraphStyle setMaximumLineHeight: (CGFloat) height];
    	    [attStr addAttribute: NSParagraphStyleAttributeName value: paragraphStyle range: NSMakeRange (0, [attStr length])];
	    [paragraphStyle release];
    	    //[attStr addAttribute: NSBackgroundColorAttributeName value: [NSColor redColor] range: NSMakeRange (0, [attStr length])];
	    [attStr addAttribute: @"BRCellHeight" value: [NSNumber numberWithInt: height] range: NSMakeRange (0,[attStr length])];
    	    NSMutableAttributedString* astring = [[NSMutableAttributedString alloc] initWithString: @"\n"];

	    [string appendAttributedString: astring];
	    [string appendAttributedString: attStr];
	    [attStr release];
	    //[string appendAttributedString: astring];
    	    [astring release];
	    RELEASE (BR);
    }
}

- (NSMutableAttributedString*) renderText: (NSMutableAttributedString*) text
{
	RELEASE (_currentContent);
	_currentContent = [[NSMutableAttributedString alloc] init];
	//[Parser parserWithHandler: self withData:
	[[GSHTMLParser parserWithSAXHandler: self withData: 
		[[text string] dataUsingEncoding: NSUTF8StringEncoding
		allowLossyConversion: YES]]
		parse];
	return _currentContent;
}

- (void) setTextView: (NSTextView*) textview {
    textView = textview;
}

- (void) addImage: (NSImage*) img onString: (NSMutableAttributedString*) astring {
        NSTextAttachmentCell* attachCell = [[NSTextAttachmentCell alloc] initImageCell: img];

        NSTextAttachment* tAttachment = [[NSTextAttachment alloc] init];
        [tAttachment setAttachmentCell: attachCell];

        NSMutableAttributedString* t = [[NSMutableAttributedString alloc] init];
	[t appendAttributedString: [NSMutableAttributedString attributedStringWithAttachment: tAttachment]];
        //[t setAlignment: NSCenterTextAlignment range: NSMakeRange (0, [t length])];

    	[astring appendAttributedString: t];
	[t release];
        RELEASE (attachCell);
        RELEASE (tAttachment);
}


- (void) addImage: (NSString*) pathname {

    NSString* file;
    NSTextAttachment* textAttachment;

    NSLog (@"addImage : %@", pathname);

    if ([path length] == 0)
        file = pathname;
    else
        file = [NSString stringWithFormat: @"%@/%@", path, pathname];

    if ([[NSFileManager defaultManager] fileExistsAtPath: file])
    {
        //NSLog (@"on lit l'image: >%@<", file);

        NSImage* img = [[NSImage alloc] initWithContentsOfFile: file];
        NSFileWrapper* wrapper = [[NSFileWrapper alloc] initWithPath: file];
        NSTextAttachmentCell* attachCell = [[NSTextAttachmentCell alloc] initImageCell: img];

        [wrapper setIcon: [NSImage imageNamed: @"Search.tiff"]];

        textAttachment = [[NSTextAttachment alloc] initWithFileWrapper: wrapper];
        [textAttachment setAttachmentCell: attachCell];

        NSMutableAttributedString* t = [[NSMutableAttributedString alloc] init];
	[t appendAttributedString: [NSMutableAttributedString attributedStringWithAttachment: textAttachment]];
        //[t setAlignment: NSCenterTextAlignment range: NSMakeRange (0, [t length])];

    	[_currentContent appendAttributedString: t];
	[t release];
        RELEASE (img);
        RELEASE (wrapper);
        RELEASE (attachCell);
        RELEASE (textAttachment);
    }
    else
    {
        NSLog (@"l'image >%@< n'existe pas !", file);
    }
}

- (void) addNote: (NSMutableAttributedString*) string withImage: (NSImage*) img withColor: (NSColor*) color
{
    NSTextAttachment* textAttach = [[NSTextAttachment alloc] init];
    NoteCell* attachCell = [[NoteCell alloc] initWithTextView: textView];

    [attachCell setImage: img];
    [attachCell setColor: color];
    [attachCell setText: string];
    [attachCell resizeWithTextView: textView];

    [[NSNotificationCenter defaultCenter] addObserver: attachCell
					     selector: @selector (resize:)
						 name: @"NSViewFrameDidChangeNotification"
					       object: textView];

    [textAttach setAttachmentCell: attachCell];
    NSMutableAttributedString* attStr = [[NSMutableAttributedString alloc] init];
    [attStr appendAttributedString: [NSMutableAttributedString attributedStringWithAttachment: textAttach]];
    //[attStr setAlignment: NSCenterTextAlignment range: NSMakeRange (0, [attStr length])];
    NSMutableAttributedString* astring = [[NSMutableAttributedString alloc] initWithString: @"\n"];

    [_currentContent appendAttributedString: astring];
    [astring release];

    [_currentContent appendAttributedString: attStr];
    [attStr release];
    RELEASE (textAttach);
    RELEASE (attachCell);
}

- (void) addLegendFig: (NSString*) imgpath withLegends: (NSArray*) plegends
{
    NSImage* img = [[NSImage alloc] initWithContentsOfFile: 
    	[Bundle pathForResource: [imgpath stringByDeletingPathExtension] 
	ofType: [imgpath pathExtension]]];

    //NSLog (@"addLegendFig: %@ legends : %@", imgpath, plegends);

    NSTextAttachment* textAttach = [[NSTextAttachment alloc] init];
    FigureCell* attachCell = [[FigureCell alloc] initWithSize:
        NSMakeSize ([textView bounds].size.width, [img size].height)];

    [[NSNotificationCenter defaultCenter] addObserver: attachCell
        selector: @selector (resize:)
        name: @"NSViewFrameDidChangeNotification"
        object: textView];

    [attachCell setImage: img];
    [attachCell setLegends: plegends];
    [attachCell resizeWithTextView: textView];
    [textAttach setAttachmentCell: attachCell];

    NSMutableAttributedString* attStr = [[NSMutableAttributedString alloc] init];
    [attStr appendAttributedString: [NSMutableAttributedString attributedStringWithAttachment: textAttach]];
    [attStr setAlignment: NSCenterTextAlignment range: NSMakeRange (0, [attStr length])];

    [_currentContent appendAttributedString: attStr];
    [attStr release];
    RELEASE (textAttach);
    RELEASE (attachCell);
    RELEASE (img);
}



@end