Skip to content

Commit 6525c13

Browse files
committed
Extensive changes to fix issue #12, need lots more testing !
1 parent d0d5f70 commit 6525c13

File tree

1 file changed

+134
-65
lines changed

1 file changed

+134
-65
lines changed

tomboy-ng/savenote.pas

Lines changed: 134 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
into the time offset figure, eg +11:00. Old notes written
3939
with previous vesions will fail with file sync until rewritten.
4040
2017/11/12 Added code to replace < and > with char codes.
41+
2017/12/02 Fixed a bug were we were skipping newline where there were 2 in a row
42+
2017/12/02 Extensive changes to ensure font setting stanning part of a bullet
43+
list are saved correctly.
44+
2017/12/02 Restructured AddTag to ensure tags laid out in correct order.
45+
2017/12/02 changed the way that we ensure there are no hanging tags at end of
46+
a note.
4147
}
4248

4349
{$mode objfpc}{$H+}
@@ -58,10 +64,16 @@ TBSaveNote = class
5864
Bold : boolean;
5965
Italics : boolean;
6066
HiLight : boolean;
67+
PrevFSize : integer;
68+
PrevBold : boolean;
69+
PrevItalics : boolean;
70+
PrevHiLight : boolean;
6171
InList : boolean;
6272
KM : TKMemo;
6373
function AddTag(const FT : TFont; var Buff : ANSIString) : ANSIString;
74+
function BlockAttributes(Bk: TKMemoBlock): AnsiString;
6475
procedure BulletList(var Buff: ANSIString);
76+
function FontAttributes(Ft: TFont): ANSIString;
6577
function RemoveBadCharacters(const InStr: ANSIString): ANSIString;
6678
function SetFontXML(Size : integer; TurnOn : boolean) : string;
6779
function Header() : ANSIstring;
@@ -100,89 +112,122 @@ function TBSaveNote.SetFontXML(Size : integer; TurnOn : boolean) : string;
100112
function TBSaveNote.AddTag(const FT : TFont; var Buff : ANSIString) : ANSIString;
101113
begin
102114
// Important that we keep the tag order consistent. Good xml requires no cross over
103-
// tags. If the note is to be readable by Tomboy, must comply. (RTomboy does not care)
104-
// FontSize, HiLite, Ital, Bold, Bullet, BulletOff, BoldOff, ItalOff, HiLiteOff
105-
// Note Bullets are not dealt with here, have to be done later.
115+
// tags. If the note is to be readable by Tomboy, must comply. (EditBox does not care)
116+
// Tag order -
117+
// FontSize HiLite Ital Bold Bullet TEXT BulletOff BoldOff ItalOff HiLiteOff FontSize
118+
// Processing Order is the reverese -
119+
// BoldOff ItalicsOff HiLiteOff FontSize HiLite Ital Bold
106120

107-
// When Bold Turns OFF
121+
writeln('Bold=', Bold=True, ' Italics=', Italics=True, ' Colour=', HiLight=True, ' ', inttostr(FSize));
122+
123+
// When Bold Turns OFF
108124
if (Bold and (not (fsBold in FT.Style))) then begin
109-
if FSize <> Sett.FontNormal then
110-
Buff := Buff + SetFontXML(FSize, false);
111-
if Hilight then Buff := Buff + '</highlight>';
112-
if Italics then Buff := Buff + '</italic>';
113125
Buff := Buff + '</bold>';
114-
if Italics then Buff := Buff + '<italic>';
115-
if Hilight then Buff := Buff + '<highlight>';
116-
if FSize <> Sett.FontNormal then
117-
Buff := Buff + SetFontXML(FSize, true);
118126
Bold := false;
119127
end;
128+
120129
// When Italic turns OFF
121130
if (Italics and (not (fsItalic in FT.Style))) then begin
122-
if FSize <> Sett.FontNormal then
123-
Buff := Buff + SetFontXML(FSize, false);
124-
if Hilight then Buff := Buff + '</highlight>';
125-
Buff := Buff + '</italic>';
126-
if Hilight then Buff := Buff + '<highlight>';
127-
if FSize <> Sett.FontNormal then
128-
Buff := Buff + SetFontXML(FSize, true);
131+
if Bold then Buff := Buff + '</bold>';
132+
Buff := Buff + '</italic>';
133+
if Bold then Buff := Buff + '<bold>';
129134
Italics := false;
130135
end;
136+
131137
// When Highlight turns OFF
132138
if (HiLight and (not (FT.Color = HiColor))) then begin
133-
if FSize <> Sett.FontNormal then
139+
140+
if Bold then Buff := Buff + '</bold>';
141+
if Italics then Buff := Buff + '</italic>';
142+
Buff := Buff + '</highlight>';
143+
if Italics then Buff := Buff + '<italic>';
144+
if Bold then Buff := Buff + '<bold>';
145+
146+
{ if FSize <> Sett.FontNormal then
134147
Buff := Buff + SetFontXML(FSize, false);
135148
Buff := Buff + '</highlight>';
136149
if FSize <> Sett.FontNormal then
137-
Buff := Buff + SetFontXML(FSize, true);
150+
Buff := Buff + SetFontXML(FSize, true); }
138151
HiLight := false;
139152
end;
153+
140154
// When Font size changes
141-
if FSize <> FT.Size then begin
155+
if (FSize <> FT.Size) and (FT.Size <> Sett.FontTitle) then begin
156+
if Bold then Buff := Buff + '</bold>';
157+
if Italics then Buff := Buff + '</italic>';
158+
if HiLight then Buff := Buff + '</highlight>';
142159
Buff := Buff + SetFontXML(FSize, false);
160+
Buff := Buff + SetFontXML(FT.Size, true);
161+
if HiLight then Buff := Buff + '<highlight>';
162+
if Italics then Buff := Buff + '<italic>';
163+
if Bold then Buff := Buff + '<bold>';
143164
FSize := FT.Size;
144-
Buff := Buff + SetFontXML(FSize, true);
145165
end;
166+
146167
// Highlight turns ON
147168
if ((not HiLight) and (FT.Color = HiColor)) then begin
148-
if FSize <> Sett.FontNormal then
149-
Buff := Buff + SetFontXML(FSize, false);
169+
if Bold then Buff := Buff + '</bold>';
170+
if Italics then Buff := Buff + '</italic>';
150171
Buff := Buff + '<highlight>';
151-
if FSize <> Sett.FontNormal then
152-
Buff := Buff + SetFontXML(FSize, true);
172+
if Italics then Buff := Buff + '<italic>';
173+
if Bold then Buff := Buff + '<bold>';
153174
HiLight := true;
154175
end;
176+
155177
// Italic turns On
156178
if ((not Italics) and (fsItalic in FT.Style)) then begin
157-
if Hilight then Buff := Buff + '</highlight>';
158-
if FSize <> Sett.FontNormal then
159-
Buff := Buff + SetFontXML(FSize, false);
179+
if Bold then Buff := Buff + '</bold>';
160180
Buff := Buff + '<italic>';
161-
if FSize <> Sett.FontNormal then
162-
Buff := Buff + SetFontXML(FSize, true);
163-
if Hilight then Buff := Buff + '<highlight>';
181+
if Bold then Buff := Buff + '<bold>';
164182
Italics := true;
165183
end;
184+
166185
// Bold turns On
167186
if ((not Bold) and (fsBold in FT.Style)) then begin
168-
if Italics then Buff := Buff + '</italic>';
169-
if Hilight then Buff := Buff + '</highlight>';
170-
if FSize <> Sett.FontNormal then
171-
Buff := Buff + SetFontXML(FSize, false);
172187
Buff := Buff + '<bold>';
173-
if FSize <> Sett.FontNormal then
174-
Buff := Buff + SetFontXML(FSize, true);
175-
if Hilight then Buff := Buff + '<highlight>';
176-
if Italics then Buff := Buff + '<italic>';
177188
Bold := true;
178189
end;
179-
180190
Result := Buff;
181191
end;
182192

183193
procedure TBSaveNote.BulletList(var Buff : ANSIString);
194+
var
195+
StartStartSt, StartEndSt, EndStartSt, EndEndSt : ANSIString;
184196
begin
185-
Buff := '<list><list-item dir="ltr">' + Buff + '</list-item></list>';
197+
if PrevBold then begin
198+
StartStartSt := '</bold>';
199+
StartEndSt := '<bold>';
200+
end;
201+
if Bold then begin
202+
EndStartSt := '</bold>';
203+
EndEndSt := '<bold>';
204+
end;
205+
if PrevItalics then begin
206+
StartStartSt := StartStartSt + '</italic>';
207+
StartEndSt := '<italic>' + StartEndSt;
208+
end;
209+
if Italics then begin
210+
EndStartSt := EndStartSt + '</italic>';
211+
EndEndSt := '<italic>' + EndEndSt;
212+
end;
213+
if PrevHiLight then begin
214+
StartStartSt := StartStartSt + '</highlight>';
215+
StartEndSt := '<highlight>' + StartEndSt;
216+
end;
217+
if HiLight then begin
218+
EndStartSt := EndStartSt + '</highlight>';
219+
EndEndSt := '<highlight>' + EndEndSt;
220+
end;
221+
if PrevFSize <> Sett.FontNormal then begin
222+
StartStartSt := StartStartSt + SetFontXML(PrevFSize, False);
223+
StartEndSt := SetFontXML(PrevFSize, True);
224+
end;
225+
if FSize <> Sett.FontNormal then begin
226+
EndStartSt := EndStartSt + SetFontXML(FSize, False);
227+
EndEndSt := SetFontXML(FSize, True);
228+
end;
229+
Buff := StartStartSt + '<list><list-item dir="ltr">' + StartEndSt
230+
+ Buff + EndStartSt + '</list-item></list>' + EndEndSt;
186231
end;
187232

188233
function TBSaveNote.RemoveBadCharacters(const InStr : ANSIString) : ANSIString;
@@ -212,6 +257,31 @@ function TBSaveNote.RemoveBadCharacters(const InStr : ANSIString) : ANSIString;
212257
Result := Result + UTF8Copy(InStr, Start, Index - Start);
213258
end;
214259

260+
function TBSaveNote.BlockAttributes(Bk : TKMemoBlock) : AnsiString;
261+
begin
262+
Result := '';
263+
if fsBold in TKMemoTextBlock(BK).TextStyle.Font.Style then
264+
Result := Result + ' Bold ';
265+
if fsItalic in TKMemoTextBlock(BK).TextStyle.Font.Style then
266+
Result := Result + ' Italic ';
267+
if TKMemoTextBlock(BK).TextStyle.Font.Color = HiColor then
268+
Result := Result + ' Colour ';
269+
Result := Result + inttostr(TKMemoTextBlock(BK).TextStyle.Font.Size);
270+
271+
end;
272+
273+
function TBSaveNote.FontAttributes(Ft : TFont) : ANSIString;
274+
begin
275+
Result := '';
276+
if fsBold in Ft.Style then
277+
Result := Result + ' Bold ';
278+
if fsItalic in Ft.Style then
279+
Result := Result + ' Italic ';
280+
if Ft.Color = HiColor then
281+
Result := Result + ' Colour ';
282+
Result := Result + inttostr(Ft.Size);
283+
284+
end;
215285

216286
procedure TBSaveNote.Save(FileName : ANSIString; KM1 : TKMemo);
217287
var
@@ -222,7 +292,7 @@ procedure TBSaveNote.Save(FileName : ANSIString; KM1 : TKMemo);
222292
BlankFont : TFont;
223293
begin
224294
KM := KM1;
225-
FSize := 0;
295+
FSize := Sett.FontNormal;
226296
Bold := false;
227297
Italics := False;
228298
HiLight := False;
@@ -238,18 +308,27 @@ procedure TBSaveNote.Save(FileName : ANSIString; KM1 : TKMemo);
238308
Buff := '';
239309
try
240310
repeat
311+
PrevFSize := FSize;
312+
PrevBold := Bold;
313+
PrevItalics := Italics;
314+
PrevHiLight := HiLight;
315+
PrevFSize := FSize;
241316
repeat
242317
Block := KM1.Blocks.Items[BlockNo];
318+
if Block.ClassNameIs('TKMemoParagraph') then break; // two newlines
243319
if Block.ClassNameIs('TKMemoTextBlock') then begin
244-
AddTag(TKMemoTextBlock(Block).TextStyle.Font, Buff);
245-
Buff := Buff + RemoveBadCharacters(Block.Text);
246-
end;
320+
if Block.Text.Length > 0 then begin
321+
AddTag(TKMemoTextBlock(Block).TextStyle.Font, Buff);
322+
Buff := Buff + RemoveBadCharacters(Block.Text);
323+
end;
324+
end;
247325
if Block.ClassNameIs('TKMemoHyperlink') then begin
248326
AddTag(TKMemoHyperlink(Block).TextStyle.Font, Buff);
249327
Buff := Buff + RemoveBadCharacters(Block.Text);
250328
end;
251329
inc(BlockNo);
252330
if BlockNo >= KM1.Blocks.Count then break;
331+
253332
until KM1.Blocks.Items[BlockNo].ClassNameIs('TKMemoParagraph');
254333
if BlockNo >= KM1.Blocks.Count then break;
255334
if TKMemoParagraph(KM1.Blocks.Items[BlockNo]).Numbering = pnuBullets then
@@ -266,20 +345,14 @@ procedure TBSaveNote.Save(FileName : ANSIString; KM1 : TKMemo);
266345
could still have hanging xml tags. So either case, send it to add tag with
267346
an empty Font.
268347
}
269-
270-
271-
if not KM1.Blocks.LastBlock.ClassNameIs('TKMemoParagraph') then begin
272-
// If we don't finish with a Para block, then we need to
273-
// add it in as well.
274-
BlankFont := TFont.Create();
275-
BlankFont.Size := Sett.FontNormal;
276-
BlankFont.Color := NormalColor;
277-
BlankFont.Style := [];
278-
AddTag(BlankFont, Buff);
279-
Buff := Buff + KM1.Blocks.LastBlock.Text;
280-
OutStream.Write(Buff[1], length(Buff));
281-
BlankFont.Free;
282-
end;
348+
Buff := '';
349+
if Bold then Buff := '</bold>';
350+
if Italics then Buff := Buff + '</italic>';
351+
if HiLight then Buff := Buff + '</highlight>';
352+
if FSize <> Sett.FontNormal then
353+
Buff := Buff + SetFontXML(FSize, False);
354+
if length(Buff) > 0 then
355+
OutStream.Write(Buff[1], length(Buff));
283356
Buff := Footer();
284357
OutStream.Write(Buff[1], length(Buff));
285358

@@ -319,10 +392,6 @@ procedure TBSaveNote.Save(FileName : ANSIString; KM1 : TKMemo);
319392
Res := res + '00'
320393
else Res := Res + inttostr(abs(Off mod 60));
321394
Result := Result + res;
322-
323-
{ Result := FormatDateTime('YYYY-MM-DD',ThisMoment) + 'T'
324-
+ FormatDateTime('hh:mm:ss.z',ThisMoment) + '0000+'
325-
+ inttostr(GetLocalTimeOffset() div -60); }
326395
end;
327396

328397
Function TBSaveNote.Header() : ANSIstring;

0 commit comments

Comments
 (0)