38
38
into the time offset figure, eg +11:00. Old notes written
39
39
with previous vesions will fail with file sync until rewritten.
40
40
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.
41
47
}
42
48
43
49
{ $mode objfpc}{ $H+}
@@ -58,10 +64,16 @@ TBSaveNote = class
58
64
Bold : boolean;
59
65
Italics : boolean;
60
66
HiLight : boolean;
67
+ PrevFSize : integer;
68
+ PrevBold : boolean;
69
+ PrevItalics : boolean;
70
+ PrevHiLight : boolean;
61
71
InList : boolean;
62
72
KM : TKMemo;
63
73
function AddTag (const FT : TFont; var Buff : ANSIString) : ANSIString;
74
+ function BlockAttributes (Bk: TKMemoBlock): AnsiString;
64
75
procedure BulletList (var Buff: ANSIString);
76
+ function FontAttributes (Ft: TFont): ANSIString;
65
77
function RemoveBadCharacters (const InStr: ANSIString): ANSIString;
66
78
function SetFontXML (Size : integer; TurnOn : boolean) : string;
67
79
function Header () : ANSIstring;
@@ -100,89 +112,122 @@ function TBSaveNote.SetFontXML(Size : integer; TurnOn : boolean) : string;
100
112
function TBSaveNote.AddTag (const FT : TFont; var Buff : ANSIString) : ANSIString;
101
113
begin
102
114
// 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
106
120
107
- // When Bold Turns OFF
121
+ writeln(' Bold=' , Bold=True, ' Italics=' , Italics=True, ' Colour=' , HiLight=True, ' ' , inttostr(FSize));
122
+
123
+ // When Bold Turns OFF
108
124
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>' ;
113
125
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);
118
126
Bold := false;
119
127
end ;
128
+
120
129
// When Italic turns OFF
121
130
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>' ;
129
134
Italics := false;
130
135
end ;
136
+
131
137
// When Highlight turns OFF
132
138
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
134
147
Buff := Buff + SetFontXML(FSize, false);
135
148
Buff := Buff + '</highlight>';
136
149
if FSize <> Sett.FontNormal then
137
- Buff := Buff + SetFontXML(FSize, true);
150
+ Buff := Buff + SetFontXML(FSize, true); }
138
151
HiLight := false;
139
152
end ;
153
+
140
154
// 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>' ;
142
159
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>' ;
143
164
FSize := FT.Size;
144
- Buff := Buff + SetFontXML(FSize, true);
145
165
end ;
166
+
146
167
// Highlight turns ON
147
168
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> ' ;
150
171
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> ' ;
153
174
HiLight := true;
154
175
end ;
176
+
155
177
// Italic turns On
156
178
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>' ;
160
180
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>' ;
164
182
Italics := true;
165
183
end ;
184
+
166
185
// Bold turns On
167
186
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);
172
187
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>' ;
177
188
Bold := true;
178
189
end ;
179
-
180
190
Result := Buff;
181
191
end ;
182
192
183
193
procedure TBSaveNote.BulletList (var Buff : ANSIString);
194
+ var
195
+ StartStartSt, StartEndSt, EndStartSt, EndEndSt : ANSIString;
184
196
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;
186
231
end ;
187
232
188
233
function TBSaveNote.RemoveBadCharacters (const InStr : ANSIString) : ANSIString;
@@ -212,6 +257,31 @@ function TBSaveNote.RemoveBadCharacters(const InStr : ANSIString) : ANSIString;
212
257
Result := Result + UTF8Copy(InStr, Start, Index - Start);
213
258
end ;
214
259
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 ;
215
285
216
286
procedure TBSaveNote.Save (FileName : ANSIString; KM1 : TKMemo);
217
287
var
@@ -222,7 +292,7 @@ procedure TBSaveNote.Save(FileName : ANSIString; KM1 : TKMemo);
222
292
BlankFont : TFont;
223
293
begin
224
294
KM := KM1;
225
- FSize := 0 ;
295
+ FSize := Sett.FontNormal ;
226
296
Bold := false;
227
297
Italics := False;
228
298
HiLight := False;
@@ -238,18 +308,27 @@ procedure TBSaveNote.Save(FileName : ANSIString; KM1 : TKMemo);
238
308
Buff := ' ' ;
239
309
try
240
310
repeat
311
+ PrevFSize := FSize;
312
+ PrevBold := Bold;
313
+ PrevItalics := Italics;
314
+ PrevHiLight := HiLight;
315
+ PrevFSize := FSize;
241
316
repeat
242
317
Block := KM1.Blocks.Items[BlockNo];
318
+ if Block.ClassNameIs(' TKMemoParagraph' ) then break; // two newlines
243
319
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 ;
247
325
if Block.ClassNameIs(' TKMemoHyperlink' ) then begin
248
326
AddTag(TKMemoHyperlink(Block).TextStyle.Font, Buff);
249
327
Buff := Buff + RemoveBadCharacters(Block.Text);
250
328
end ;
251
329
inc(BlockNo);
252
330
if BlockNo >= KM1.Blocks.Count then break;
331
+
253
332
until KM1.Blocks.Items[BlockNo].ClassNameIs(' TKMemoParagraph' );
254
333
if BlockNo >= KM1.Blocks.Count then break;
255
334
if TKMemoParagraph(KM1.Blocks.Items[BlockNo]).Numbering = pnuBullets then
@@ -266,20 +345,14 @@ procedure TBSaveNote.Save(FileName : ANSIString; KM1 : TKMemo);
266
345
could still have hanging xml tags. So either case, send it to add tag with
267
346
an empty Font.
268
347
}
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));
283
356
Buff := Footer();
284
357
OutStream.Write(Buff[1 ], length(Buff));
285
358
@@ -319,10 +392,6 @@ procedure TBSaveNote.Save(FileName : ANSIString; KM1 : TKMemo);
319
392
Res := res + ' 00'
320
393
else Res := Res + inttostr(abs(Off mod 60 ));
321
394
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); }
326
395
end ;
327
396
328
397
Function TBSaveNote.Header () : ANSIstring;
0 commit comments