-
Notifications
You must be signed in to change notification settings - Fork 8
/
odf_types.pas
3648 lines (2920 loc) · 100 KB
/
odf_types.pas
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ odf_types.pas is part of the fpOdf.
fpOdf is a library used to help users to create and to modify OpenDocument
Files(ODF)
Copyright (C) 2013-2021 Daniel F. Gaspary https://github.com/dgaspary
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version with the following modification:
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent modules,and
to copy and distribute the resulting executable under terms of your choice,
provided that you also meet, for each linked independent module, the terms
and conditions of the license of that module. An independent module is a
module which is not derived from or based on this library. If you modify
this library, you may extend this exception to your version of the library,
but you are not obligated to do so. If you do not wish to do so, delete this
exception statement from your 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 Library General Public License
for more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit odf_types;
{$mode objfpc}
//{$define patched_dom}
//{$define fpodf_debug}
interface
uses
Classes, SysUtils, FileUtil, LazFileUtils, zipper, zstream, fgl, LazUTF8, FPCanvas,
{ $Define ODF_LOGGING}
{$ifdef patched_dom}
DOM_patched, XMLRead_patched
{$else}
Laz2_DOM, laz2_XMLRead, laz2_xpath
{$endif},
{$ifdef odf_logging}
eventlog,
{$endif}
xmlutils, odf_mimetypes, odf_xmlutils;
const
cCharSpace = #32;
cNone = '<NONE>';
cFileContent = 'content.xml';
cFileStyles = 'styles.xml';
cFileMimetype = 'mimetype';
cIsoDateFormat = 'yyyy-mm-dd"T"hh:nn:ss'; //Is defined somewhere at fcl?
cMetaGenerator = 'fpOdf 0.1'; { TODO : Need to change to a proper format,
as specified at p1-4.3.2.1 }
cUrnOpenDocument = 'urn:oasis:names:tc:opendocument:xmlns:';
cUrlOasis = 'http://docs.oasis-open.org/';
cUriOffice12Meta = cUrlOasis + 'ns/office/1.2/meta/';
cUriOdfMeta = cUriOffice12Meta + 'odf#'; { TODO : Check why it's not on namespace enum and array }
cUrlW3 = 'http://www.w3.org/';
//Namespaces
////////////
type
TOdfNamespace = (onsNone, onsOffice, onsGrddl, onsStyle, onsRdf,
onsManifest, onsMeta, onsDc, onsConfig, onsScript,
onsPresentation, onsSvg, onsText, onsChart, onsTable,
onsForm, onsXforms, onsDraw, onsMath, onsDr3d, onsNumber,
onsAnim, onsDb, onsXlink, onsXml, onsFo, onsSmil,
onsXhtml, onsPkg, onsOf);
TOdfNamespaces = set of TOdfNamespace;
const
OdfNamespaceDefaultPrefixes: array[TOdfNamespace] of String = (cNone,
'office',
'grddl',
'style',
'rdf',
'manifest',
'meta',
'dc',
'config',
'script',
'presentation',
'svg',
'text',
'chart',
'table',
'form',
'xforms',
'draw',
'math',
'dr3d',
'number',
'anim',
'db',
'xlink',
'xml',
'fo',
'smil',
'xhtml',
'pkg',
'of');
OdfNamespaceURIs: array[TOdfNamespace] of String = (cNone,
cUrnOpenDocument + 'office:1.0',
cUrlW3 + '2003/g/data-view#',
cUrnOpenDocument + 'style:1.0',
cUrlW3 + '1999/02/22-rdf-syntax-ns#',
cUrnOpenDocument + 'manifest:1.0',
cUrnOpenDocument + 'meta:1.0',
'http://purl.org/dc/elements/1.1/',
cUrnOpenDocument + 'config:1.0',
cUrnOpenDocument + 'script:1.0',
cUrnOpenDocument + 'presentation:1.0',
cUrnOpenDocument + 'svg-compatible:1.0',
cUrnOpenDocument + 'text:1.0',
cUrnOpenDocument + 'chart:1.0',
cUrnOpenDocument + 'table:1.0',
cUrnOpenDocument + 'form:1.0',
cUrlW3 + '2002/xforms',
cUrnOpenDocument + 'drawing:1.0',
cUrlW3 + '1998/Math/MathML',
cUrnOpenDocument + 'dr3d:1.0',
cUrnOpenDocument + 'datastyle:1.0',
cUrnOpenDocument + 'animation:1.0',
cUrnOpenDocument + 'database:1.0',
cUrlW3 + '1999/xlink',
cUrlW3 + 'XML/1998/namespace',
cUrnOpenDocument + 'xsl-fo-compatible:1.0',
cUrnOpenDocument + 'smil-compatible:1.0',
cUrlW3 + '1999/xhtml',
cUriOffice12Meta + 'pkg#',
cUrnOpenDocument + 'of:1.2');
function GetURI(ns: TOdfNamespace): string;
function OdfGetNsUri(APrefix: string): string;
//Elements
//////////
{$INCLUDE incs/ElementEnum.inc}
// TOdfRootElements = oetOfficeDocumentContent .. oetOfficeDocumentSettings;
TElementTypeArray = array of TElementType; //Max Length: oetTextSenderEmail
{ TOdfElementTypeSet }
TOdfElementTypeSet = class(specialize TFPGList<TElementType>)
function Add(const Item: TElementType): Integer;
function HasItem(const Item: TElementType): boolean;
function HasItem(ALocalName, ANsUri: string): boolean;
constructor Create(EtArray: TElementTypeArray);
end;
TOdfDomElementList = specialize TFPGList<TDOMElement>;
function OdfGetElementLocalName(et: TElementType): string;
function OdfGetElementQName(et: TElementType; out uri: string): string; overload;
function OdfGetElementQName(et: TElementType): string; overload;
procedure OdfElementGetNsAndName(et: TElementType; out prefix: string;
out localname: string;
out uri: string);
function OdfGetElementTypeByName(ALocalName, ANsUri: string): TElementType;
function OdfGetElement(et: TElementType; AParent: TDOMElement;
Recursive: boolean = false): TDOMElement;
function OdfGetChildByName(ANsURI, ALocalName: String; AParent: TDOMElement;
Recursive: boolean = false): TDOMElement;
function OdfGetElementList(AUri, ALocalName: string; AParent: TDOMElement;
Recursive: boolean = false): TOdfDomElementList;
function OdfGetElementList(et: TElementType; AParent: TDOMElement;
Recursive: boolean = false): TOdfDomElementList;
procedure OdfElementSetNamespaceAtt(DestElement: TDOMElement; ns: TOdfNamespace); overload;
procedure OdfElementSetNamespaceAtt(DestElement: TDOMElement; nsSet: TOdfNamespaces); overload;
//Attributes
////////////
//type
{$INCLUDE incs/Atts-Enum.inc}
//TAttributeTypeSet = set of TAttributeType;
TAttributeTypeArray = array of TAttributeType;
function OdfGetAttributeLocalName(at: TAttributeType; out uri: string): string; overload;
function OdfGetAttributeLocalName(at: TAttributeType): string; overload;
function OdfGetAttributeQName(at: TAttributeType; out uri: string): string; overload;
function OdfGetAttributeQName(at: TAttributeType): string; overload;
function OdfGetAttributeTypeByName(const AAttributeName: string): TAttributeType;
function OdfGetAttribute(at: TAttributeType; AParent: TDOMElement): TDOMAttr;
function OdfGetAttributeValue(at: TAttributeType; AParent: TDOMElement): String;
function OdfGetAttributeDefaultValue(at: TAttributeType;
et: TElementType): string;
function OdfSetAttributeValue(at: TAttributeType; AElement: TDOMElement;
AValue: string): TDOMAttr;
function OdfSetAttributeDefaultValue(at: TAttributeType;
AElement: TDOMElement): TDOMAttr;
function OdfGetAttributeNamespace(at: TAttributeType): TOdfNamespace;
procedure OdfElementGetChildrenAndAtts(et: TElementType;
out Children: TElementTypeArray;
out Atts: TAttributeTypeArray);
function OdfElementGetChildrenTypes(et: TElementType): TElementTypeArray;
function OdfElementGetAttsTypes(et: TElementType): TAttributeTypeArray;
//Package Files
///////////////
type
//p1-3.1.3.1 and p3-2.2.1
TOdfFile = (ofMimetype, ofImage, ofObject, ofManifestRdf, ofManifest,
ofContent, ofStyles, ofMeta, ofSettings);
TOdfXmlFiles = ofManifestRdf .. High(TOdfFile);
const
OdfXmlFileRoot: array[TOdfXmlFiles] of TElementType =
(
oetNone,//oetRdfRdf,
oetManifestManifest,
oetOfficeDocumentContent,
oetOfficeDocumentStyles,
oetOfficeDocumentMeta,
oetOfficeDocumentSettings
);
OdfXmlFilename: array[TOdfXmlFiles] of string =
(
'manifest.rdf',
'META-INF/manifest.xml',
'content.xml',
'styles.xml',
'meta.xml',
'settings.xml'
);
function OdfGetXmlFileRoot(f: TOdfXmlFiles): TElementType;
procedure OdfFileGetDetails(f: TOdfFile; out RootLocalname: string;
out RootNsPrefix: string;
out RootNsUri: string;
out Filename: string;
out Children: TElementTypeArray);
type
{ TElementEnumerator }
TElementEnumerator = class
private
FCurrent: TDOMElement;
FParent: TDOMElement;
public
constructor Create(AParentElement: TDOMElement);
function MoveNext: Boolean;
property Current: TDOMElement read FCurrent;
end;
TOdfElement = class;
TSpan = class;
TOdfElementClass = class of TOdfElement;
TOdfDocument = class;
{ TOdfElement }
TOdfElement = class(TDOMElement)
private
class function GetElementType(AIndex: TElementType): TElementType;
public
class function CreateDomElement(AType: TElementType;
Doc: TXMLDocument): TDOMElement; overload;
class function CreateDomElement(AType: TElementType; Doc: TXMLDocument;
at: TAttributeType; AttValue: String): TDOMElement; overload;
class function CreateOdfElement(AType: TElementType;
Doc: TXMLDocument): TOdfElement; overload;
class function CreateOdfElement(AType: TElementType;
AClass: TOdfElementClass; Doc: TXMLDocument): TOdfElement; overload;
class function OdfGetElementType(e: TDOMElement): TElementType;
class function SameType(e1, e2: TDomElement): boolean;
class function SameType(e: TDomElement; AType: TElementType): boolean;
class procedure SetAttribute(at: TAttributeType;
AParent: TDOMElement;
AValue: string);
class procedure SetAttributes(atts: array of TAttributeType;
AParent: TDOMElement;
const Values: array of string);
function AppendOdfElement(AType: TElementType): TOdfElement;
function AppendOdfElement(AType: TElementType; at: TAttributeType;
AValue: string): TOdfElement;
function OdfGetFirstElement: TOdfElement;
function HasOdfElement(AType: TElementType): boolean;
function FindStyle(aName: String): TOdfElement;
function GetAttribute(AType: TAttributeType): TDOMAttr;
function GetAttributeString(AType: TAttributeType): String;
function HasAttribute(AType: TAttributeType): boolean;
function RemoveAttribute(AType: TAttributeType): TDOMAttr;
procedure DeleteAttributes(const atts: array of TAttributeType);
procedure SetAttribute(AType: TAttributeType; AValue: string);
procedure SetAttributes(const atts: array of TAttributeType;
const Values: array of String);
procedure SetOrDeleteAttributes(const atts: array of TAttributeType;
const Value: String = '');
//property OdfElementType: TElementType read GetElementType;
//class property OdfElementType: TElementType index oetNone read GetElementType;
end;
{ TFontFaceDecls }
TFontFaceDecls = class(TOdfElement)
function AddFontFace(StyleName, SvgFontFamily: string): TOdfElement;
function AddFontFace(StyleName, SvgFontFamily, FontFamilyGeneric,
FontPitch: string): TOdfElement;
end;
{ TTextSequenceDecls }
TTextSequenceDecls = class(TOdfElement)
function AddSequenceDecl(DisplayOutlineLevel, AName: string): TOdfElement;
end;
{ TConfigConfigItemSet }
TConfigConfigItemSet = class(TOdfElement)
public
function AddConfigItem(AName, AType, AContent: string): TOdfElement;
property ConfigName: string index oatConfigName
read GetAttributeString write SetAttribute;
end;
{$INCLUDE incs/styles-decl.inc}
type
TOdfColor = record
Red,
Green,
Blue: Byte;
end;
{ TOdfFont }
TOdfFont = class(TFPCustomFont)
private
FColor: TOdfColor;
public
property Color: TOdfColor read FColor write FColor;
end;
{ TODO : Odf Font Style has different items than TFontStyle }
TOdfFontStyles = set of TOdfFontStyle;
TOdfNodeSet = TNodeSet;
TOdfXPathNsResolver = class;
{ TOdfDocument }
TOdfDocument = class
private
FTempDir: string;
FRemoveTempDir: boolean;
FXmlDocument: TXMLDocument;
//Document Root Elements (Ref: Table 7)
{ TODO : Are the variables really Needed? Maybe the best is to use
an indexed(with TElementType) get method. }
FMeta: TDOMElement;
FSettings: TDOMElement;
FScripts: TDOMElement;
FFontFaceDecls: TDOMElement;
FStyles: TDOMElement;
FAutomaticStyles: TDOMElement;
FMasterStyles: TDOMElement;
FBody: TDOMElement;
FManifest: TXMLDocument;
FManifestRdf: TDOMElement;
FXPathNsResolver: TOdfXPathNsResolver;
function GetRootChild(et: TElementType): TDOMElement;
procedure InitFonts; virtual;
procedure InitStyles; virtual;
procedure InitXmlDocument; virtual;
procedure FindOdfRootElements;virtual;
procedure InitBodyContent; virtual; abstract;
procedure ResetRootElements; virtual;
function GetMimeType: TOdfMimetype;
procedure SetCreationMeta;
procedure SetMimeType(AValue: TOdfMimetype);
//public { TODO : Make it public to test. }
function StylesUsed(AParent: TDomElement): TStrings;
private
class function ParseXmlFile(AStream: TStream): TXMLDocument;
class procedure ReadPackage(ADir: String; AOdf: TOdfDocument);
procedure SetXmlDocument(AValue: TXMLDocument);
class procedure WritePackage(DestFile: String;
AOdf: TOdfDocument;
ATempDir: string = '');
public
constructor Create;
destructor Destroy; override;
class function GetMimeType(ADoc: TXMLDocument): TOdfMimetype;
class function ElementOdfClassByType(et: TElementType): TOdfElementClass;
class function ConvertOdfElement(AClass: TOdfElementClass;
e: TDOMElement): TOdfElement;
class function CreateDocument(AMimeType: TOdfMimetype): TOdfDocument;
class function LoadFromFile(AFilename: string): TOdfDocument;
class function LoadFromZipFile(AFilename, TempDir: string): TOdfDocument;
class function CreateFromXml(ADoc: TXMLDocument): TOdfDocument;
class function LoadFromSingleXml(AStream: TStream): TOdfDocument; overload;
class function LoadFromSingleXml(AFilename: string): TOdfDocument; overload;
class procedure SaveToSingleXml(AOdf: TOdfDocument;
AFilename: string); overload;
function CreateOdfElement(et: TElementType): TOdfElement; overload;
function CreateOdfElement(et: TElementType; at: TAttributeType;
AttValue: String): TOdfElement; overload;
function CreateStyle(AStyleName, AFamily: string): TOdfStyleStyle;
function CreateStyle(AStyleName: string;
AFamily: TStyleFamilyValue): TOdfStyleStyle;
function CreateStyle(AStyleName: string;
AParentStyle: TOdfStyleStyle): TOdfStyleStyle;
function CreateSpan(AText: string; FontStyles: TOdfFontStyles): TSpan;
function SearchText(AText: string; AParent: TDOMElement;
Accepted: TElementTypeArray;
out FoundAt: TDOMText;
Partial: boolean = true): boolean; overload;
function SearchText(AText: string; AParent: TDOMElement;
Accepted: array of TElementType;
out FoundAt: TDOMText;
Partial: boolean = true): boolean; overload;
function XPathSearch(AXPathExpression: string; AParent: TDOMElement;
Accepted: array of TElementType;
out Results: TOdfNodeSet;
Partial: boolean = true): boolean;
Procedure Clear;virtual;
procedure SaveToSingleXml(AFilename: string); overload;
class procedure SaveToZipFile(AOdf: TOdfDocument;
AFilename: string; ATempDir: string='');
procedure SaveToZipFile(AFilename: string); overload;
property XmlDocument: TXMLDocument read FXmlDocument write SetXmlDocument;
property MimeType: TOdfMimetype read GetMimeType write SetMimeType;
property Body: TDOMElement read FBody write FBody;
property Meta: TDOMElement read FMeta write FMeta;
//property Meta: TDOMElement index oetOfficeMeta read GetRootChild;
class function GetStyleName(AStartNode: TDOMNode; out StyleNode: TDOMNode
): String;
class function GetStyleName(AStartNode: TDOMNode): String;
function GetStyleElementByName(AStyleName: string): TDOMElement;
//Params with default values will be ignored in search.
//Example: 0 for AFontSize, '' for AStyleFamily and fwNone for AFontWeight
function GetStyleByProperties(AFontSize: integer; AFontSizeUnit: string;
AFontStyle: TOdfFontStyle; AFontWeight: TOdfFontWeight;
AFontUnderlineStyle: string): TStrings;
function Clone: TOdfDocument; virtual;
property Settings: TDOMElement read FSettings write FSettings;
property Scripts: TDOMElement read FScripts write FScripts;
property FontFaceDecls: TDOMElement read FFontFaceDecls write FFontFaceDecls;
property Styles: TDOMElement read FStyles write FStyles;
property AutomaticStyles: TDOMElement read FAutomaticStyles write FAutomaticStyles;
property MasterStyles: TDOMElement read FMasterStyles write FMasterStyles;
property Manifest: TXMLDocument read FManifest write FManifest;
property ManifestRdf: TDOMElement read FManifestRdf write FManifestRdf;
property RemoveTempDir: boolean read FRemoveTempDir write FRemoveTempDir;
end;
{ TOdfContent }
TOdfContent = class(TOdfElement)
private
function AddTextNode(const AValue: DOMString): TDOMText;
procedure OdfSetTextContent(const AValue: DOMString);
public
//p1-6.1.1
Procedure AppendText(aText:String);
function GetCharacterContent(Recursive: boolean = true): string;
property TextContent read GetTextContent write OdfSetTextContent;
end;
THyperLink=class;
{ TOdfParagraph }
TOdfParagraph = class(TOdfContent)
private
public
function AddSpan(AText: string; FontStyles: TOdfFontStyles): TSpan;overload;
function AddSpan(AText: string; aFont: TOdfFont;const doc: TOdfDocument): TSpan;
overload;
function AddSpan(AText: string; aStyle: string): TSpan;overload;
function AddBookmark(AText: string; FontStyles: TOdfFontStyles;aBMName:string): TSpan;
function AddLink(AText: string; FontStyles: TOdfFontStyles;aBMName:string): THyperLink;
//p1-7.4.15
{ TODO : Text input can also be included on Span, oetTextH, etc }
function AddTextInput(ADescription: string = ''): TOdfElement;
end;
{ TSpan }
TSpan = class(TOdfContent)
private
public
class function CreateSpan(doc: TXMLDocument; AText: string): TSpan;
procedure SetStyle(fs: TOdfFontStyles);overload;
procedure SetStyle(const doc: TOdfDocument; aFont: TOdfFont); overload;
procedure SetStyle(aStyleName: string);overload;
end;
{ TBookMark }
TBookMark = class(TSpan)
private
public
class function CreateBookmark(doc: TXMLDocument; AText, aBMName: string): TSpan;
procedure SetBookmark(aBMName:string);
end;
{ THyperLink }
THyperLink = class(TSpan)
private
public
class function CreateLink(doc: TXMLDocument; AText, aBMName: string
): THyperLink;
Procedure SetLink(aBMName:string);
end;
{ TOdfTextDocument }
TOdfTextDocument = class(TOdfDocument)
private
FText: TDOMElement;
procedure InitXmlDocument; override;
procedure ResetRootElements; override;
procedure InitBodyContent; override;
Procedure GenerateAutoStyles;
public
constructor Create;
Procedure Clear; override;
{ TODO : Replace AddParagraph, param ATextSytleName, with A Style Object or interface}
function AddParagraph(ATextStyleName: String): TOdfParagraph;
function AddHeadline(aLevel: integer): TOdfContent;
function AddAutomaticStyle:TOdfStyleStyle;overload;
function AddAutomaticStyle(aName:string):TOdfStyleStyle;overload;
function AddStyle(aName: String; aFamily: TStyleFamilyValue): TOdfStyleStyle;
overload;
function SearchText(AText: string; out FoundAt: TDOMText): boolean; overload;
function SearchText(AText: string; out FoundAt: TDOMText;
out AParagraph: TOdfParagraph): boolean; overload;
{ TODO : Needed: A function that searches a Style using its name }
property Text: TDOMElement read FText write FText;
end;
TDefaultStyle = class(TOdfElement)
public
property StyleFamily: string index oatStyleFamily
read GetAttributeString write SetAttribute;
end;
{$INCLUDE incs/Elements-Proc-Declaration.inc}
{$INCLUDE incs/Atts-Proc-Declaration.inc}
procedure WriteStringToFile(str, AFileName: string);
procedure OdfXmlSetDefaultAtts(doc: TXMLDocument);
function OdfPrepareString(AText: UTF8String; out Segment1: UTF8String;
out Segment2: UTF8String;
out NoSpaces: word): TElementType;
type
{ TOdfXPathNsResolver }
TOdfXPathNsResolver = class(TXPathNSResolver)
function LookupNamespaceURI(const aPrefix: DOMString): DOMString;
override;
end;
function OdfXPathSearch(AXPathExpression: string; AParent: TDOMElement;
out Results: TOdfNodeSet;
Resolver: TOdfXPathNsResolver = nil): boolean;
implementation
{$IfDef ODF_LOGGING}
type
{ TOdfEventLog }
TOdfEventLog = class(TEventLog)
private
FIdentation: integer;
fProcs: TStrings;
const cIdentSize = 4;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Log(EventType: TEventType; const Msg: String);
procedure Log(const Msg: String);
procedure EnterProc(ProcName: string);
procedure ExitProc;
end;
constructor TOdfEventLog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FIdentation:=0;
fProcs:=TStringList.Create;
end;
destructor TOdfEventLog.Destroy;
begin
fProcs.Free;
inherited Destroy;
end;
procedure TOdfEventLog.Log(EventType: TEventType; const Msg: String);
var
s: string;
begin
s:='';
if FIdentation>0
then
s:=Space(FIdentation);
inherited Log(EventType, s + Msg);
end;
procedure TOdfEventLog.Log(const Msg: String);
begin
self.Log(DefaultEventType, Msg);
end;
procedure TOdfEventLog.EnterProc(ProcName: string);
begin
Log('Entering Proc: ' + ProcName);
inc(FIdentation, cIdentSize);
fProcs.Add(ProcName);
end;
procedure TOdfEventLog.ExitProc;
var
i: integer;
ProcName: string;
begin
i:=fProcs.Count - 1 ;
ProcName:=fProcs[i];
fProcs.Delete(i);
Dec(FIdentation, cIdentSize);
Log('Leaving Proc: ' + ProcName);
end;
var
odfEventLog: TOdfEventLog;
{$EndIf}
procedure OdfLog(msg: string);
begin
{$IfDef ODF_LOGGING}
odfEventLog.Log(msg);
{$EndIf}
end;
procedure OdfLogError(msg: string);
begin
{$IfDef ODF_LOGGING}
odfEventLog.Error(msg);
{$EndIf}
end;
procedure OdfLogEnterProc(ProcName: string);
begin
{$IfDef ODF_LOGGING}
odfEventLog.EnterProc(ProcName);
{$EndIf}
end;
procedure OdfLogExitProc;
begin
{$IfDef ODF_LOGGING}
odfEventLog.ExitProc;
{$EndIf}
end;
procedure NotYetImplemented(FunctionName: string);
begin
raise Exception.Create(FunctionName + ' not yet implemented');
end;
{$INCLUDE incs/proc.inc}
{$INCLUDE incs/Atts-Proc-Implemetation.inc}
{$INCLUDE incs/styles-impl.inc}
procedure OdfXmlSetDefaultAtts(doc: TXMLDocument);
var
root: TDOMElement;
begin
root:=doc.DocumentElement;
OdfSetAttributeDefaultValue(oatGrddlTransformation, root);
OdfSetAttributeDefaultValue(oatOfficeVersion, root);
end;
procedure WriteStringToFile(str, AFileName: string);
var
f: TextFile;
begin
AssignFile(f, AFileName);
try
Rewrite(f);
Write(f, str);
finally
CloseFile(f);
end;
end;
{If result is not oetNone, breaks AText in two text segments.
Segment1 have no special characters and can be add as a Text Node.
Segment2 is the remaing text after the special character found(identified in result).
Segment2 CAN have additional special characters.
NoSpaces contains the number of spaces when result is oetTextS.}
function OdfPrepareString(AText: UTF8String; out Segment1: UTF8String;
out Segment2: UTF8String;
out NoSpaces: word): TElementType;
var
s: UTF8String;
SpaceFound: boolean;
i, index, vTextLength: PtrInt;
function GetSegment2: string;
begin
if NoSpaces>0
then
result:=UTF8Copy(AText, i, vTextLength)
else
result:=UTF8Copy(AText, i + 1, vTextLength - i);
end;
begin
result:=oetNone;
SpaceFound:=false;
Segment1:='';
Segment2:='';
NoSpaces:=0;
vTextLength:=UTF8Length(AText);
for i:=1 to vTextLength do
begin
s:=UTF8Copy(AText, i, 1);
if (s<>cCharSpace) and (NoSpaces>0)
then
begin
Segment2:=GetSegment2;
result:=oetTextS;
break;
end;
case s of
#9 : result:=oetTextTab;
#11{?}, #13: result:=oetTextLineBreak; //????
cCharSpace : begin
if SpaceFound
then
Inc(NoSpaces)
else
SpaceFound:=true;
end;
end;
if result = oetNone
then
begin
if NoSpaces=0
then
Segment1+=s;
end
else
begin
Segment2:=GetSegment2;
break;
end;
end;
if NoSpaces > 0
then
result:=oetTextS;
end;
procedure ReorderElements(AOdf: TOdfDocument);
begin
with AOdf, AOdf.XmlDocument.DocumentElement do
begin
while ChildNodes.Count>0 do
RemoveChild(FirstChild);
AppendChild(Meta);
AppendChild(Settings);
AppendChild(Scripts);
AppendChild(FontFaceDecls);
AppendChild(Styles);
AppendChild(AutomaticStyles);
AppendChild(MasterStyles);
AppendChild(Body);
end;
end;
{ TOdfElementTypeSet }
function TOdfElementTypeSet.Add(const Item: TElementType): Integer;
begin
Result:=IndexOf(Item);
if Result < 0
then
Result:=inherited Add(Item);
end;
function TOdfElementTypeSet.HasItem(const Item: TElementType): boolean;
begin
result:=IndexOf(item) >= 0;
end;
function TOdfElementTypeSet.HasItem(ALocalName, ANsUri: string): boolean;
var
et: TElementType;
vPrefix, vLocal, vUri: string;
begin
result:=false;
for et in self do
begin
OdfElementGetNsAndName(et, vPrefix, vLocal, vUri);
result:=(vLocal = ALocalName) and (vUri = ANsUri);
if result
then
break;
end;
end;
constructor TOdfElementTypeSet.Create(EtArray: TElementTypeArray);
var
et: TElementType;
begin
inherited Create;
for et in EtArray do
Add(et);
end;
{ TOdfParagraph }
function TOdfParagraph.AddSpan(AText: string; FontStyles: TOdfFontStyles
): TSpan;
begin
result:=TSpan.CreateSpan(self.OwnerDocument as TXMLDocument, AText);
result.SetStyle(FontStyles);
AppendChild(result);
end;
function TOdfParagraph.AddSpan(AText: string; aFont: TOdfFont;
const doc: TOdfDocument): TSpan;
begin
result:=TSpan.CreateSpan(self.OwnerDocument as TXMLDocument, AText);
result.SetStyle(doc,aFont);
AppendChild(result);
end;
function TOdfParagraph.AddSpan(AText: string; aStyle: string): TSpan;
begin
result:=TSpan.CreateSpan(self.OwnerDocument as TXMLDocument, AText);
result.SetStyle(aStyle);
AppendChild(result);
end;
function TOdfParagraph.AddBookmark(AText: string; FontStyles: TOdfFontStyles;
aBMName: string): TSpan;
begin
result := TBookMark.CreateBookmark(self.OwnerDocument as TXMLDocument,AText,aBMName);
result.SetStyle(FontStyles);
AppendChild(result);
end;