Menu

[r2034]: / trunk / Src / Hiliter.UAttrs.pas  Maximize  Restore  History

Download this file

612 lines (561 with data), 20.9 kB

  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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
{
* Hiliter.UAttrs.pas
*
* Implements classes that define attributes of syntax highlighters.
*
* $Rev$
* $Date$
*
* ***** BEGIN LICENSE BLOCK *****
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The Original Code is Hiliter.UAttrs.pas, formerly UHiliteAttrs.pas
*
* The Initial Developer of the Original Code is Peter Johnson
* (http://www.delphidabbler.com/).
*
* Portions created by the Initial Developer are Copyright (C) 2005-2010 Peter
* Johnson. All Rights Reserved.
*
* Contributor(s)
* NONE
*
* ***** END LICENSE BLOCK *****
}
unit Hiliter.UAttrs;
interface
{
NOTES:
Syntax highlighter attribute objects are output-neutral objects that store the
attributes of various syntax highlighters. These objects are used by the
objects that format source code to determine the font and colour information
to use.
The objects define a font name and size used for the whole output. Each
different highlight element (e.g. reserved words, comments etc) can have its
own font colour and style (italic, bold, underline).
Therefore the highlighter object records a single font name and size along
with an array of objects that define the various highlight elements.
}
uses
// Project
Hiliter.UGlobals, UBaseObjects;
type
{
THiliteAttrsFactory:
Factory class used to create various highlight attribute objects.
}
THiliteAttrsFactory = class(TNoConstructObject)
public
class function CreateNulAttrs: IHiliteAttrs;
{Creates a nul highlighter object: one that provides no additional
formatting information other than default font and size.
@return Highlighter instance.
}
class function CreateDefaultAttrs: IHiliteAttrs;
{Creates a highlighter object that uses program's default highlighting
style.
@return Highlighter instance.
}
class function CreateUserAttrs: IHiliteAttrs;
{Creates a highlighter object that uses highlighting style defined by
user.
@return Highlighter instance.
}
class function CreatePrintAttrs(const Attrs: IHiliteAttrs;
const UseColour: Boolean): IHiliteAttrs;
{Creates a copy of a highlighter suitable for printing. Ensures font is
Courier New and removes colours if mono printing required.
@param Attrs [in] Highlighter attributes to be converted for printing.
If nul then the nul highlighter is used.
@param UseColour [in] Flag indicating whether colour required. When
False all colour information is removed.
@return New instance of highlighter adapted for printing.
}
class function CreateDisplayAttrs: IHiliteAttrs;
{Creates a highlighter object to use to render source code in the main
display. Based on user defined highlighter but with program's default mono
font.
@return Highlighter instance.
}
class function CreatePredefinedAttrs(
const Style: TPredefinedHiliteStyle): IHiliteAttrs;
{Creates a predefined highlighter object.
@return Highlighter instance.
}
end;
implementation
uses
// Delphi
Generics.Collections, SysUtils, Graphics,
// Project
IntfCommon, UExceptions, UFontHelper, UPreferences;
type
{
THiliteAttrs:
Object that stores display attributes that are used in a syntax highlighter.
Supports assignment.
}
THiliteAttrs = class(TInterfacedObject,
IHiliteAttrs, // defines highlight attributes and methods
IAssignable // defines object assignment
)
strict private
var
fElemAttrs: TList<IHiliteElemAttrs>; // List of element attributes
fFontSize: Integer; // Size of font in points
fFontName: string; // Name of font
const
cDefFontName = 'Courier New'; // Default font name
cDefFontSize = 9; // Default font size
protected // do not make strict
{ IHiliteAttrs methods }
function GetFontName: string;
{Gets name of font to use for all output.
@return Name of font.
}
procedure SetFontName(const AFontName: string);
{Sets name of font to use for all output.
@param AFontName [in] Required font name.
}
function GetFontSize: Integer;
{Gets size of font to use for all output.
@return Size of font in points.
}
procedure SetFontSize(const AFontSize: Integer);
{Sets size of font to use for all output.
@param AFontSize [in] Required font size in points.
}
procedure ResetDefaultFont;
{Resets font name and size to default value.
}
function GetElement(const Elem: THiliteElement): IHiliteElemAttrs;
{Gets the attributes of a highlighter element.
@param Elem [in] Required element.
@return Highlight attributes for element.
}
{ IAssignable method }
procedure Assign(const Src: IInterface);
{Assigns properties of a given object to this object.
@param Src [in] Object whose properties are to be copied. If Src is nil
object is reset to default values.
@except EBug raised if Src is incompatible with this object.
}
public
constructor Create;
{Object constructor. Sets up and intialises object.
}
destructor Destroy; override;
{Object destructor. Tears down object.
}
end;
{
THiliteElemAttrs:
Object that stores attributes applicable to various elements used in syntax
highlighting. Supports assignment.
}
THiliteElemAttrs = class(TInterfacedObject,
IHiliteElemAttrs, // defines element highlight attributes and methods
IAssignable // defines object assignment
)
strict private
var fForeColor: TColor; // Foreground (text) colour
var fFontStyle: TFontStyles; // Font styles
protected // do not make strict
{ IHiliteElemAttrs methods }
function IsNul: Boolean;
{Checks whether element's attributes are "nul" - i.e. all properties have
default values. Used to determine whether to output formatting information
for an element.
@return True if element's attributes are nul.
}
function GetFontStyle: TFontStyles;
{Gets the font style to use for element.
@return Set of font styles.
}
procedure SetFontStyle(const AFontStyle: TFontStyles);
{Sets font style to use for element.
@param AFontStyle [in] Required set of font styles.
}
function GetForeColor: TColor;
{Gets the foreground colour (i.e. text colour) to use for element.
@return Text colour.
}
procedure SetForeColor(const AColor: TColor);
{Sets foreground colour (i.e. text colour) to use for element.
@param AColor [in] Required colour.
}
{ IAssignable methods }
procedure Assign(const Src: IInterface);
{Assigns properties of a given object to this object.
@param Src [in] Object whose properties are to be copied. If Src is nil
object is reset to default values.
@except EBug raised if Src is incompatible with this object.
}
public
constructor Create;
{Object constructor. Sets up and initialises object.
}
end;
{ THiliteAttrs }
procedure THiliteAttrs.Assign(const Src: IInterface);
{Assigns properties of a given object to this object.
@param Src [in] Object whose properties are to be copied. If Src is nil
object is reset to default values.
@except EBug raised if Src is incompatible with this object.
}
var
Elem: THiliteElement; // loops thru all highlight elements
begin
if Assigned(Src) then
begin
if not Supports(Src, IHiliteAttrs) then
raise EBug.Create(
ClassName + '.Assign: Src does not support IHiliteAttrs'
);
// Src is assigned: copy its properties
with Src as IHiliteAttrs do
begin
Self.SetFontName(FontName);
Self.SetFontSize(FontSize);
for Elem := Low(THiliteElement) to High(THiliteElement) do
(Self.GetElement(Elem) as IAssignable).Assign(Elements[Elem]);
end;
end
else
begin
// Src is nil: set defaults
Self.SetFontName(cDefFontName);
Self.SetFontSize(cDefFontSize);
// Get each element attribute to sets its own default by assigning nil
for Elem := Low(THiliteElement) to High(THiliteElement) do
(Self.GetElement(Elem) as IAssignable).Assign(nil);
end;
end;
constructor THiliteAttrs.Create;
{Object constructor. Sets up and intialises object.
}
var
Elem: THiliteElement; // loops thru all highlight elements
begin
inherited;
// Set default font values
fFontName := cDefFontName;
fFontSize := cDefFontSize;
// Create list that holds an nul object for each highlight element
// Low(THiliteElement) is at index 0 in list
fElemAttrs := TList<IHiliteElemAttrs>.Create;
for Elem := Low(THiliteElement) to High(THiliteElement) do
fElemAttrs.Add(THiliteElemAttrs.Create);
end;
destructor THiliteAttrs.Destroy;
{Object destructor. Tears down object.
}
begin
fElemAttrs.Free; // releases each object in list
inherited;
end;
function THiliteAttrs.GetElement(
const Elem: THiliteElement): IHiliteElemAttrs;
{Gets the attributes of a highlighter element.
@param Elem [in] Required element.
@return Highlight attributes for element.
}
begin
// Note: Low(THiliteElement) is at index 0 in list. Following code does *not*
// assume that Ord(Low(THiliteElement)) = 0.
Result := fElemAttrs[Ord(Elem) - Ord(Low(THiliteElement))];
end;
function THiliteAttrs.GetFontName: string;
{Gets name of font to use for all output.
@return Name of font.
}
begin
Result := fFontName;
end;
function THiliteAttrs.GetFontSize: Integer;
{Gets size of font to use for all output.
@return Size of font in points.
}
begin
Result := fFontSize;
end;
procedure THiliteAttrs.ResetDefaultFont;
{Resets font name and size to default value.
}
begin
SetFontName(cDefFontName);
SetFontSize(cDefFontSize);
end;
procedure THiliteAttrs.SetFontName(const AFontName: string);
{Sets name of font to use for all output.
@param AFontName [in] Required font name.
}
begin
fFontName := AFontName;
end;
procedure THiliteAttrs.SetFontSize(const AFontSize: Integer);
{Sets size of font to use for all output.
@param AFontSize [in] Required font size in points.
}
begin
fFontSize := AFontSize;
end;
{ THiliteElemAttrs }
procedure THiliteElemAttrs.Assign(const Src: IInterface);
{Assigns properties of a given object to this object.
@param Src [in] Object whose properties are to be copied. If Src is nil
object is reset to default values.
@except EBug raised if Src is incompatible with this object.
}
begin
if Assigned(Src) then
begin
if not Supports(Src, IHiliteElemAttrs) then
raise EBug.Create(
ClassName + '.Assign: Src does not support IHiliteElemAttrs'
);
// Src is assigned: copy its properties
with Src as IHiliteElemAttrs do
begin
Self.SetForeColor(ForeColor);
Self.SetFontStyle(FontStyle);
end;
end
else
begin
// Src is nil: reset properties to default (nul) values
Self.SetForeColor(clNone);
Self.SetFontStyle([]);
end;
end;
constructor THiliteElemAttrs.Create;
{Object constructor. Sets up and initialises object.
}
begin
inherited;
// Intialise properties to default (nul) values
fForeColor := clNone;
fFontStyle := [];
end;
function THiliteElemAttrs.GetFontStyle: TFontStyles;
{Gets the font style to use for element.
@return Set of font styles.
}
begin
Result := fFontStyle;
end;
function THiliteElemAttrs.GetForeColor: TColor;
{Gets the foreground colour (i.e. text colour) to use for element.
@return Text colour.
}
begin
Result := fForeColor;
end;
function THiliteElemAttrs.IsNul: Boolean;
{Checks whether element's attributes are "nul" - i.e. all properties have
default values. Used to determine whether to output formatting information for
an element.
@return True if element's attributes are nul.
}
begin
Result := (fFontStyle = []) and (fForeColor = clNone);
end;
procedure THiliteElemAttrs.SetFontStyle(const AFontStyle: TFontStyles);
{Sets font style to use for element.
@param AFontStyle [in] Required set of font styles.
}
begin
fFontStyle := AFontStyle;
end;
procedure THiliteElemAttrs.SetForeColor(const AColor: TColor);
{Sets foreground colour (i.e. text colour) to use for element.
@param AColor [in] Required colour.
}
begin
fForeColor := AColor;
end;
{ THiliteAttrsFactory }
class function THiliteAttrsFactory.CreateDefaultAttrs: IHiliteAttrs;
{Creates a highlighter object that uses program's default highlighting style.
@return Highlighter instance.
}
begin
Result := THiliteAttrsFactory.CreatePredefinedAttrs(hsDelphi2006);
end;
class function THiliteAttrsFactory.CreateDisplayAttrs: IHiliteAttrs;
{Creates a highlighter object to use to render source code in the main
display. Based on user defined highlighter but with program's default mono
font.
@return Highlighter instance.
}
var
Font: TFont; // display mono font
begin
Result := CreateUserAttrs;
// Ensure we use required mono display font
Font := TFont.Create;
try
TFontHelper.SetDefaultMonoFont(Font, True);
Result.FontName := Font.Name;
Result.FontSize := Font.Size;
finally
Font.Free;
end;
end;
class function THiliteAttrsFactory.CreateNulAttrs: IHiliteAttrs;
{Creates a nul highlighter object: one that provides no additional formatting
information other than default font and size.
@return Highlighter instance.
}
begin
// Just create highlighter object: nul values are defaults
Result := THiliteAttrs.Create;
end;
class function THiliteAttrsFactory.CreatePredefinedAttrs(
const Style: TPredefinedHiliteStyle): IHiliteAttrs;
{Creates a predefined highlighter object.
@return Highlighter instance.
}
type
{
TAttrsTable:
Table of highlighter attributes.
}
TAttrsTable = array[THiliteElement] of record
ForeColor: TColor; // foreground (text) colour
FontStyle: TFontStyles; // set of font styles
end;
const
// Defines predefined styles
cPredefinedStyle: array[TPredefinedHiliteStyle] of TAttrsTable = (
( // hsNul
(ForeColor: clNone; FontStyle: [];), // heWhitespace
(ForeColor: clNone; FontStyle: [];), // heComment
(ForeColor: clNone; FontStyle: [];), // heReserved
(ForeColor: clNone; FontStyle: [];), // heIdentifier
(ForeColor: clNone; FontStyle: [];), // heSymbol
(ForeColor: clNone; FontStyle: [];), // heString
(ForeColor: clNone; FontStyle: [];), // heNumber
(ForeColor: clNone; FontStyle: [];), // heFloat
(ForeColor: clNone; FontStyle: [];), // heHex
(ForeColor: clNone; FontStyle: [];), // hePreProcessor
(ForeColor: clNone; FontStyle: [];), // heAssembler
(ForeColor: clNone; FontStyle: [];) // heError
),
( // hsCodeSnip
(ForeColor: clNone; FontStyle: [];), // heWhitespace
(ForeColor: clNavy; FontStyle: [fsItalic];), // heComment
(ForeColor: clNone; FontStyle: [fsBold];), // heReserved
(ForeColor: clNone; FontStyle: [];), // heIdentifier
(ForeColor: clNone; FontStyle: [];), // heSymbol
(ForeColor: clPurple; FontStyle: [];), // heString
(ForeColor: clMaroon; FontStyle: [];), // heNumber
(ForeColor: clMaroon; FontStyle: [];), // heFloat
(ForeColor: clMaroon; FontStyle: [];), // heHex
(ForeColor: clGreen; FontStyle: [];), // hePreProcessor
(ForeColor: clNone; FontStyle: [fsItalic];), // heAssembler
(ForeColor: clRed; FontStyle: [];) // heError
),
( // hsDelphi7
(ForeColor: clNone; FontStyle: [];), // heWhitespace
(ForeColor: clNavy; FontStyle: [fsItalic];), // heComment
(ForeColor: clBlack; FontStyle: [fsBold];), // heReserved
(ForeColor: clBlack; FontStyle: [];), // heIdentifier
(ForeColor: clBlack; FontStyle: [];), // heSymbol
(ForeColor: clNavy; FontStyle: [];), // heString
(ForeColor: clNavy; FontStyle: [];), // heNumber
(ForeColor: clNavy; FontStyle: [];), // heFloat
(ForeColor: clNavy; FontStyle: [];), // heHex
(ForeColor: clNavy; FontStyle: [];), // hePreProcessor
(ForeColor: clBlack; FontStyle: [];), // heAssembler
(ForeColor: clRed; FontStyle: [];) // heError
),
( // hsDelphi2006
(ForeColor: clNone; FontStyle: [];), // heWhitespace
(ForeColor: clGreen; FontStyle: [fsItalic];), // heComment
(ForeColor: clNavy; FontStyle: [fsBold];), // heReserved
(ForeColor: clNone; FontStyle: [];), // heIdentifier
(ForeColor: clNone; FontStyle: [];), // heSymbol
(ForeColor: clBlue; FontStyle: [];), // heString
(ForeColor: clBlue; FontStyle: [];), // heNumber
(ForeColor: clBlue; FontStyle: [];), // heFloat
(ForeColor: clBlue; FontStyle: [];), // heHex
(ForeColor: clTeal; FontStyle: [];), // hePreProcessor
(ForeColor: clNone; FontStyle: [];), // heAssembler
(ForeColor: clRed; FontStyle: [];) // heError
),
( // hsVisualStudio
(ForeColor: clNone; FontStyle: [];), // heWhitespace
(ForeColor: clGreen; FontStyle: [];), // heComment
(ForeColor: clBlue; FontStyle: [];), // heReserved
(ForeColor: clBlack; FontStyle: [];), // heIdentifier
(ForeColor: clNone; FontStyle: [];), // heSymbol
(ForeColor: clNone; FontStyle: [];), // heString
(ForeColor: clNone; FontStyle: [];), // heNumber
(ForeColor: clNone; FontStyle: [];), // heFloat
(ForeColor: clNone; FontStyle: [];), // heHex
(ForeColor: clBlue; FontStyle: [];), // hePreProcessor
(ForeColor: clBlack; FontStyle: [];), // heAssembler
(ForeColor: clRed; FontStyle: [];) // heError
)
);
var
Elem: THiliteElement; // loops thru highlighter elements
begin
// Create highlighter with default (nul) values
// (we accept default font name and size)
Result := THiliteAttrs.Create;
// Set required properties for highlight elements per table
for Elem := Low(THiliteElement) to High(THiliteElement) do
begin
Result[Elem].FontStyle := cPredefinedStyle[Style][Elem].FontStyle;
Result[Elem].ForeColor := cPredefinedStyle[Style][Elem].ForeColor;
end;
end;
class function THiliteAttrsFactory.CreatePrintAttrs(
const Attrs: IHiliteAttrs; const UseColour: Boolean): IHiliteAttrs;
{Creates a copy of a highlighter suitable for printing. Ensures font is
Courier New and removes colours if mono printing required.
@param Attrs [in] Highlighter attributes to be converted for printing. If
nul then the nul highlighter is used.
@param UseColour [in] Flag indicating whether colour required. When False
all colour information is removed.
@return New instance of highlighter adapted for printing.
}
var
Elem: THiliteElement; // loops thru all highlighter elements
begin
if not Assigned(Attrs) then
// No highlighter: use nul
Result := CreateNulAttrs
else
begin
// Copy provided Attr
Result := THiliteAttrs.Create;
(Result as IAssignable).Assign(Attrs);
if not UseColour then
// Remove colour information: not used
for Elem := Low(THiliteElement) to High(THiliteElement) do
Result[Elem].ForeColor := clNone;
end;
// Ensure we use required printing fonts
Result.ResetDefaultFont;
end;
class function THiliteAttrsFactory.CreateUserAttrs: IHiliteAttrs;
{Creates a highlighter object that uses highlighting style defined by user.
@return Highlighter instance.
}
begin
Result := THiliteAttrs.Create;
(Result as IAssignable).Assign(Preferences.HiliteAttrs);
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.