Skip to content

Commit

Permalink
Re-renamed FindStyle back to Find. (with refractoring)
Browse files Browse the repository at this point in the history
Moved 4 Methods from Paragraph to OdfContent
Appended AddTab, AddLineBreak and AddNBSpace (still some Todo there)
  • Loading branch information
joecare99 committed Oct 31, 2019
1 parent d6b6b84 commit a810877
Showing 1 changed file with 109 additions and 66 deletions.
175 changes: 109 additions & 66 deletions odf_types.pas
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,16 @@ TOdfElement = class(TDOMElement)
const Values: array of string);


function AppendOdfElement(AType: TElementType): TOdfElement;
function AppendOdfElement(AType: TElementType;AClass: TOdfElementClass): TOdfElement; overload;
function AppendOdfElement(AType: TElementType; at: TAttributeType;
AValue: string;AClass: TOdfElementClass): TOdfElement;
function AppendOdfElement(AType: TElementType): TOdfElement; overload;
function AppendOdfElement(AType: TElementType; at: TAttributeType;
AValue: string): TOdfElement;
function OdfGetFirstElement: TOdfElement;
function HasOdfElement(AType: TElementType): boolean;

function FindStyle(aName: String): TOdfElement;
function Find(aName: String;recursive:boolean=false): TOdfElement;
function GetAttribute(AType: TAttributeType): TDOMAttr;
function GetAttributeString(AType: TAttributeType): String;
function HasAttribute(AType: TAttributeType): boolean;
Expand Down Expand Up @@ -517,7 +520,8 @@ TOdfDocument = class
property ManifestRdf: TDOMElement read FManifestRdf write FManifestRdf;
end;


{Foreward declaration}
THyperLink=class;

{ TOdfContent }

Expand All @@ -529,23 +533,25 @@ TOdfContent = class(TOdfElement)
public
//p1-6.1.1
Procedure AppendText(aText:String);
function AddLineBreak: TOdfContent;
function AddNBSpace(FontStyles: TFontStyles): TOdfContent;
function AddTab(FontStyles: TFontStyles): TOdfContent;
function AddSpan(AText: string; FontStyles: TFontStyles): TSpan;overload;
function AddSpan(AText: string; aFont: TFont;const doc: TOdfDocument): TSpan;
overload;
function AddSpan(AText: string; aStyle: string): TSpan;overload;
function AddLink(AText: string; FontStyles: TFontStyles;aBMName:string): THyperLink;
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: TFontStyles): TSpan;overload;
function AddSpan(AText: string; aFont: TFont;const doc: TOdfDocument): TSpan;
overload;
function AddSpan(AText: string; aStyle: string): TSpan;overload;
function AddBookmark(AText: string; FontStyles: TFontStyles;aBMName:string): TSpan;
function AddLink(AText: string; FontStyles: TFontStyles;aBMName:string): THyperLink;
function AddBookmark(AText: string; FontStyles: TFontStyles;aBMName:string): TSpan;
end;


Expand Down Expand Up @@ -924,28 +930,6 @@ constructor TOdfElementTypeSet.Create(EtArray: TElementTypeArray);

{ TOdfParagraph }

function TOdfParagraph.AddSpan(AText: string; FontStyles: TFontStyles): TSpan;
begin
result:=TSpan.CreateSpan(self.OwnerDocument as TXMLDocument, AText);
result.SetStyle(FontStyles);
AppendChild(result);
end;

function TOdfParagraph.AddSpan(AText: string; aFont: TFont;
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: TFontStyles;
aBMName: string): TSpan;
begin
Expand All @@ -954,14 +938,6 @@ function TOdfParagraph.AddBookmark(AText: string; FontStyles: TFontStyles;
AppendChild(result);
end;

function TOdfParagraph.AddLink(AText: string; FontStyles: TFontStyles;
aBMName: string): THyperLink;
begin
result := THyperLink.CreateLink(self.OwnerDocument as TXMLDocument,AText,aBMName);
result.SetStyle(FontStyles);
AppendChild(result);
end;

{ TBookMark }

class function TBookMark.CreateBookmark(doc: TXMLDocument; AText,
Expand Down Expand Up @@ -1052,22 +1028,21 @@ procedure TSpan.SetStyle(const doc: TOdfDocument; aFont: TFont);

if aFont.name <> 'default' then
begin
lFontdcls := FFontFaceDecls.FindNode(afont.Name);
if not assigned(lFontdcls) then
begin
lFontdcls := CreateOdfElement(oetStyleFontFace,oatStyleName,afont.Name);
TOdfElement(lFontdcls).SetAttribute(oatSvgFontFamily,afont.Name.QuotedString(''''));
FontFaceDecls.AppendChild(lFontdcls);
end;
lStyleprop.SetAttribute(oatStyleFontName,afont.Name) ;
lFontdcls := TOdfElement(FFontFaceDecls).Find(afont.Name);
if not assigned(lFontdcls) then
begin
lFontdcls := CreateOdfElement(oetStyleFontFace,oatStyleName,afont.Name);
TOdfElement(lFontdcls).SetAttribute(oatSvgFontFamily,afont.Name.QuotedString(''''));
FontFaceDecls.AppendChild(lFontdcls);
end;
lStyleprop.SetAttribute(oatStyleFontName,afont.Name) ;
end;
if afont.Color <>clDefault then
begin
RedGreenBlue(afont.Color,lR,lG,lB);
lStyleprop.SetAttribute(oatFoColor,'#'+IntToHex(Integer(RGBToColor(lb,lg,lr)),6)) ;

end;
if afont.Size>0 then
if afont.Size>0 then //Todo -ojc: Handling of negative font-sizes
lStyleprop.SetAttribute(oatFoFontSize,inttostr(afont.Size)+'pt') ;
if aFont.Bold then
lStyleprop.SetAttribute(oatFoFontWeight,OdfFontWeightValues[fwBold]);
Expand Down Expand Up @@ -1148,6 +1123,51 @@ procedure TOdfContent.AppendText(aText: String);
until (et = oetNone) and (s='');
end;

function TOdfContent.AddLineBreak: TOdfContent;
begin
result := TOdfContent(AppendOdfElement(oetTextLineBreak,TOdfContent));
end;

function TOdfContent.AddNBSpace(FontStyles: TFontStyles): TOdfContent;
begin
result := AddSpan('',FontStyles);
end;

function TOdfContent.AddTab(FontStyles: TFontStyles): TOdfContent;
begin
result := TOdfContent(AppendOdfElement(oetTextTab,TOdfContent));
end;

function TOdfContent.AddSpan(AText: string; FontStyles: TFontStyles): TSpan;
begin
result:=TSpan.CreateSpan(self.OwnerDocument as TXMLDocument, AText);
result.SetStyle(FontStyles);
AppendChild(result);
end;

function TOdfContent.AddSpan(AText: string; aFont: TFont;
const doc: TOdfDocument): TSpan;
begin
result:=TSpan.CreateSpan(self.OwnerDocument as TXMLDocument, AText);
result.SetStyle(doc,aFont);
AppendChild(result);
end;

function TOdfContent.AddSpan(AText: string; aStyle: string): TSpan;
begin
result:=TSpan.CreateSpan(self.OwnerDocument as TXMLDocument, AText);
result.SetStyle(aStyle);
AppendChild(result);
end;

function TOdfContent.AddLink(AText: string; FontStyles: TFontStyles;
aBMName: string): THyperLink;
begin
result := THyperLink.CreateLink(self.OwnerDocument as TXMLDocument,AText,aBMName);
result.SetStyle(FontStyles);
AppendChild(result);
end;

function TOdfContent.GetCharacterContent(Recursive: boolean): string;
var
n: TDOMNode;
Expand Down Expand Up @@ -1305,7 +1325,7 @@ function TOdfTextDocument.AddHeadline(aLevel: integer): TOdfContent;
result.SetAttribute(oatTextOutlineLevel, Inttostr(aLevel));
FText.AppendChild(result);

lParaStyle:= TOdfElement(FStyles).FindStyle(CStyleTextBody) ;
lParaStyle:= TOdfElement(FStyles).Find(CStyleTextBody) ;
if not assigned(lParaStyle) then
begin
vStyle:=CreateOdfElement(oetStyleStyle);
Expand All @@ -1321,7 +1341,7 @@ function TOdfTextDocument.AddHeadline(aLevel: integer): TOdfContent;

end;

lParaStyle:= TOdfElement(FStyles).FindStyle(CStyleHeadingStb+Inttostr(aLevel)) ;
lParaStyle:= TOdfElement(FStyles).Find(CStyleHeadingStb+Inttostr(aLevel)) ;
if not assigned(lParaStyle) then
begin
vStyle:=CreateOdfElement(oetStyleStyle);
Expand Down Expand Up @@ -1478,17 +1498,29 @@ class procedure TOdfElement.SetAttributes(atts: array of TAttributeType;
end;
end;

function TOdfElement.AppendOdfElement(AType: TElementType;
AClass: TOdfElementClass): TOdfElement;
begin
result:=CreateOdfElement(AType,AClass, self.OwnerDocument as TXMLDocument);
self.AppendChild(result);
end;

function TOdfElement.AppendOdfElement(AType: TElementType; at: TAttributeType;
AValue: string; AClass: TOdfElementClass): TOdfElement;
begin
result:=AppendOdfElement(AType,AClass);
result.SetAttribute(at, AValue);
end;

function TOdfElement.AppendOdfElement(AType: TElementType): TOdfElement;
begin
result:=CreateOdfElement(AType, self.OwnerDocument as TXMLDocument);
self.AppendChild(result);
result:=AppendOdfElement(AType,TOdfElement);
end;

function TOdfElement.AppendOdfElement(AType: TElementType; at: TAttributeType;
AValue: string): TOdfElement;
begin
result:=AppendOdfElement(AType);
result.SetAttribute(at, AValue);
result:=AppendOdfElement(AType,at,AValue,TOdfElement);
end;

function TOdfElement.OdfGetFirstElement: TOdfElement;
Expand Down Expand Up @@ -1527,23 +1559,39 @@ function TOdfElement.HasOdfElement(AType: TElementType): boolean;
end;
end;

function TOdfElement.FindStyle(aName: String): TOdfElement;
function TOdfElement.Find(aName: String; recursive: boolean): TOdfElement;
var
lChlds: TDOMNode;
lChlds, lNext: TDOMNode;
begin
lChlds := FirstChild;
lNext:= GetNextNodeSkipChildren;
result := nil;
while assigned(lChlds) do
while assigned(lChlds) and (lChlds<>lNext) do
begin
if not (lChlds is TDOMElement)
then
raise Exception.Create('Unexpected Node Type: ' + lChlds.ClassName);

if (TOdfStyleStyle(lChlds).OdfStyleName=aname)
{for some reason lChlds.inheritsfrom(TOdfStyleStyle)=False !}
if (TOdfElement(lChlds).GetAttributeString(oatStyleName)=aName)

This comment has been minimized.

Copy link
@dgaspary

dgaspary Oct 31, 2019

Owner

I don't understand why do you insist with the generic name "Find" when the function has code that is specific to Find Styles.

This comment has been minimized.

Copy link
@joecare99

joecare99 via email Oct 31, 2019

Author Contributor

This comment has been minimized.

Copy link
@dgaspary

dgaspary Oct 31, 2019

Owner

oatTextStyleName, oatStyleName...

Ok, to be fair.. maybe .. FindStylesAndFontsByName?

Just kidding, don't take so serious..

Anyway, I just don't like a generic "Find" name when internally it has code to specific elements.

This comment has been minimized.

Copy link
@joecare99

joecare99 via email Oct 31, 2019

Author Contributor

This comment has been minimized.

Copy link
@dgaspary

dgaspary Oct 31, 2019

Owner

Sounds Better..

Could also keep "Find" adding an Att param:

Find(AttName, AttValue: String; recursive: boolean); overload;
Find(Att: TAttributeType; AttValue: String; recursive: boolean); overload;

What do you think ?

This comment has been minimized.

Copy link
@joecare99

joecare99 via email Oct 31, 2019

Author Contributor

This comment has been minimized.

Copy link
@dgaspary

dgaspary Oct 31, 2019

Owner

Do you see my comment about XPathSearch?

It's on TParagraph Issue.

Sometimes simple XPath expressions can avoid the need to write whole new methods.

This comment has been minimized.

Copy link
@joecare99

joecare99 via email Oct 31, 2019

Author Contributor

This comment has been minimized.

Copy link
@joecare99

joecare99 via email Oct 31, 2019

Author Contributor
then
exit(TOdfElement(lChlds));

lChlds:=lChlds.GetNextNodeSkipChildren;
if (TOdfElement(lChlds).GetAttributeString(oatFoFontFamily)=aName)
then
exit(TOdfElement(lChlds));

{for some reason lChlds.inheritsfrom(TOdfContent)=False !}
if (TOdfElement(lChlds).GetAttributeString(oatTextStyleName)=aName)
then
exit(TOdfElement(lChlds));


if recursive
then
lChlds:=lChlds.GetNextNode
else
lChlds:=lChlds.GetNextNodeSkipChildren;
end;
end;

Expand Down Expand Up @@ -1739,7 +1787,6 @@ class function TOdfDocument.ParseXmlFile(AStream: TStream): TXMLDocument;
begin
try
Src:=TXMLInputSource.Create(AStream);

Parser := TDOMParser.Create;
Parser.Options.Namespaces:=True;
Parser.Options.IgnoreComments:=True;
Expand Down Expand Up @@ -2656,10 +2703,6 @@ function TOdfDocument.CreateSpan(AText: string; FontStyles: TFontStyles): TSpan;
result.SetStyle(FontStyles);
end;





{ TODO : Create a SiblingNode Parameter.
If assigned the method will begin the search using it as a starting point,
a context node. }
Expand Down

0 comments on commit a810877

Please sign in to comment.