/vendor/jvcl/run/JvRichEditToHtml.pas

http://my-chuanqi.googlecode.com/ · Pascal · 600 lines · 483 code · 66 blank · 51 comment · 24 complexity · 0711ab07e63adcf0688198774f2436a6 MD5 · raw file

  1. {-----------------------------------------------------------------------------
  2. The contents of this file are subject to the Mozilla Public License
  3. Version 1.1 (the "License"); you may not use this file except in compliance
  4. with the License. You may obtain a copy of the License at
  5. http://www.mozilla.org/MPL/MPL-1.1.html
  6. Software distributed under the License is distributed on an "AS IS" basis,
  7. WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
  8. the specific language governing rights and limitations under the License.
  9. The Original Code is: JvRichEditToHtml.PAS, released on 2001-02-28.
  10. The Initial Developer of the Original Code is Sébastien Buysse [sbuysse att buypin dott com]
  11. Portions created by Sébastien Buysse are Copyright (C) 2001 Sébastien Buysse.
  12. All Rights Reserved.
  13. Contributor(s): Michael Beck [mbeck att bigfoot dott com],
  14. Andreas Hausladen [Andreas dott Hausladen att gmx dott de].
  15. You may retrieve the latest version of this file at the Project JEDI's JVCL home page,
  16. located at http://jvcl.sourceforge.net
  17. Known Issues:
  18. -----------------------------------------------------------------------------}
  19. // $Id: JvRichEditToHtml.pas 11400 2007-06-28 21:24:06Z ahuser $
  20. unit JvRichEditToHtml;
  21. {$I jvcl.inc}
  22. {$I windowsonly.inc}
  23. interface
  24. uses
  25. Windows,
  26. {$IFDEF UNITVERSIONING}
  27. JclUnitVersioning,
  28. {$ENDIF UNITVERSIONING}
  29. SysUtils, Classes, Graphics, Forms, ComCtrls,
  30. JvRgbToHtml, JvStrToHtml, JvRichEdit, JvComponentBase, JclStrings;
  31. type
  32. TJvParaAttributesRec = record
  33. Alignment: TAlignment;
  34. Numbering: TNumberingStyle;
  35. end;
  36. TJvRichEditParaAttributesRec = record
  37. Alignment: TParaAlignment;
  38. Numbering: TJvNumbering;
  39. end;
  40. TFontInfo = class(TPersistent)
  41. private
  42. FFontData: TFontData;
  43. FColor: TColor;
  44. FPixelsPerInch: Integer;
  45. FLink: Boolean;
  46. function GetSize: Integer;
  47. procedure SetSize(const Value: Integer);
  48. public
  49. constructor Create(APixelsPerInch: Integer);
  50. procedure Assign(Source: TPersistent); override;
  51. property Color: TColor read FColor write FColor;
  52. property Link: Boolean read FLink write FLink;
  53. property Size: Integer read GetSize write SetSize;
  54. property Height: Integer read FFontData.Height write FFontData.Height;
  55. property Pitch: TFontPitch read FFontData.Pitch write FFontData.Pitch;
  56. property Style: TFontStylesBase read FFontData.Style write FFontData.Style;
  57. property Charset: TFontCharset read FFontData.Charset write FFontData.Charset;
  58. property Name: TFontDataName read FFontData.Name write FFontData.Name;
  59. end;
  60. TJvRichEditToHtml = class(TJvComponent)
  61. private
  62. FCToH: TJvRgbToHtml;
  63. FCharToH: TJvStrToHtml;
  64. FEndSection: string;
  65. FEndPara: string;
  66. FTitle: string;
  67. FFooter: TStringList;
  68. FHeader: TStringList;
  69. function AttToHtml(Value: TFontInfo): string;
  70. function ParaToHtml(Value: TJvParaAttributesRec): string;overload;
  71. function ParaToHtml(Value: TJvRichEditParaAttributesRec): string;overload;
  72. function GetFooter: TStrings;
  73. function GetHeader: TStrings;
  74. procedure SetFooter(const Value: TStrings);
  75. procedure SetHeader(const Value: TStrings);
  76. public
  77. constructor Create(AOwner: TComponent); override;
  78. destructor Destroy; override;
  79. procedure ConvertToHtml(Value: TRichEdit; const FileName: string);overload;
  80. procedure ConvertToHtml(Value: TJvRichEdit; const FileName: string);overload;
  81. procedure ConvertToHtmlStrings(Value: TRichEdit; Strings: TStrings);overload;
  82. procedure ConvertToHtmlStrings(Value: TJvRichEdit; Strings: TStrings);overload;
  83. published
  84. property Title: string read FTitle write FTitle;
  85. property Header: TStrings read GetHeader write SetHeader;
  86. property Footer: TStrings read GetFooter write SetFooter;
  87. end;
  88. {$IFDEF UNITVERSIONING}
  89. const
  90. UnitVersioning: TUnitVersionInfo = (
  91. RCSfile: '$URL: https://jvcl.svn.sourceforge.net:443/svnroot/jvcl/trunk/jvcl/run/JvRichEditToHtml.pas $';
  92. Revision: '$Revision: 11400 $';
  93. Date: '$Date: 2007-06-28 14:24:06 -0700 (Thu, 28 Jun 2007) $';
  94. LogPath: 'JVCL\run'
  95. );
  96. {$ENDIF UNITVERSIONING}
  97. implementation
  98. const
  99. // (rom) needs renaming?
  100. // cHTMLHeadBegin = '<HTML>';
  101. // cHTMLBodyBegin = '<BODY>';
  102. // cHTMLBodyEnd = '</BODY>';
  103. // cHTMLEnd = '</HTML>';
  104. // cHTMLTitleFmt = '<TITLE>%s</TITLE>';
  105. cHTMLBR = '<BR>';
  106. // cHTMLFontColorBegin = '<FONT COLOR=#';
  107. // cHTMLSize = ' SIZE=';
  108. // cHTMLFace = ' FACE="';
  109. cHTMLFontEnd = '</SPAN>';
  110. cHTMLBoldBegin = '<B>';
  111. cHTMLBoldEnd = '</B>';
  112. cHTMLItalicBegin = '<I>';
  113. cHTMLItalicEnd = '</I>';
  114. cHTMLStrikeoutBegin = '<STRIKE>';
  115. cHTMLStrikeoutEnd = '</STRIKE>';
  116. cHTMLUnderlineBegin = '<U>';
  117. cHTMLUnderlineEnd = '</U>';
  118. cHTMLParaEnd = '</P>';
  119. cHTMLParaLeft = '<P ALIGN="LEFT">';
  120. cHTMLParaRight = '<P ALIGN="RIGHT">';
  121. cHTMLParaCenter = '<P ALIGN="CENTER">';
  122. cHTMLListBegin = '<LI>';
  123. cHTMLListEnd = '</LI>';
  124. //=== { TFontInfo } ==========================================================
  125. constructor TFontInfo.Create(APixelsPerInch: Integer);
  126. begin
  127. inherited Create;
  128. FPixelsPerInch := APixelsPerInch;
  129. end;
  130. procedure TFontInfo.Assign(Source: TPersistent);
  131. begin
  132. if Source is TTextAttributes then
  133. begin
  134. FFontData.Name := TTextAttributes(Source).Name;
  135. FFontData.Height := TTextAttributes(Source).Height;
  136. FFontData.Pitch := TTextAttributes(Source).Pitch;
  137. FFontData.Style := TTextAttributes(Source).Style;
  138. FFontData.Charset := TTextAttributes(Source).Charset;
  139. FColor := TTextAttributes(Source).Color;
  140. FLink := False;
  141. end
  142. else
  143. if Source is TJvTextAttributes then
  144. begin
  145. FFontData.Name := TJvTextAttributes(Source).Name;
  146. FFontData.Height := TJvTextAttributes(Source).Height;
  147. FFontData.Pitch := TJvTextAttributes(Source).Pitch;
  148. FFontData.Style := TJvTextAttributes(Source).Style;
  149. FFontData.Charset := TJvTextAttributes(Source).Charset;
  150. FColor := TJvTextAttributes(Source).Color;
  151. FLink := TJvTextAttributes(Source).Link;
  152. end
  153. else
  154. if Source is TFontInfo then
  155. begin
  156. FFontData := TFontInfo(Source).FFontData;
  157. FColor := TFontInfo(Source).FColor;
  158. FLink := TFontInfo(Source).FLink;
  159. end
  160. else
  161. inherited Assign(Source);
  162. end;
  163. function TFontInfo.GetSize: Integer;
  164. begin
  165. Result := -MulDiv(Height, 72, FPixelsPerInch);
  166. end;
  167. procedure TFontInfo.SetSize(const Value: Integer);
  168. begin
  169. FFontData.Height := -MulDiv(Value, FPixelsPerInch, 72);
  170. end;
  171. //=== { TJvRichEditToHtml } ==================================================
  172. constructor TJvRichEditToHtml.Create(AOwner: TComponent);
  173. begin
  174. inherited Create(AOwner);
  175. FCToH := TJvRgbToHtml.Create(Self);
  176. FCharToH := TJvStrToHtml.Create(Self);
  177. FHeader := TStringList.Create;
  178. FHeader.Add('<HTML>');
  179. FHeader.Add(' <HEAD>');
  180. FHeader.Add(' <TITLE><#TITLE></TITLE>');
  181. FHeader.Add(' </HEAD>');
  182. FHeader.Add(' <BODY>');
  183. FFooter := TStringList.Create;
  184. FFooter.Add(' </BODY>');
  185. FFooter.Add('</HTML>');
  186. end;
  187. destructor TJvRichEditToHtml.Destroy;
  188. begin
  189. FCToH.Free;
  190. FCharToH.Free;
  191. FHeader.Free;
  192. FFooter.Free;
  193. inherited Destroy;
  194. end;
  195. procedure TJvRichEditToHtml.ConvertToHtml(Value: TRichEdit; const FileName: string);
  196. var
  197. S: TStringList;
  198. begin
  199. S := TStringList.Create;
  200. try
  201. ConvertToHtmlStrings(Value, S);
  202. S.SaveToFile(FileName);
  203. finally
  204. S.Free;
  205. end;
  206. end;
  207. function TJvRichEditToHtml.AttToHtml(Value: TFontInfo): string;
  208. var
  209. Size: Integer;
  210. begin
  211. FEndSection := cHTMLFontEnd;
  212. FCToH.RgbColor := Value.Color;
  213. Size := Abs(Value.Size);
  214. if Size = 0 then
  215. Size := 8;
  216. Result := Format('<SPAN style="color: #%s; font-size: %dpt; font-family: %s;">',
  217. [FCToH.HtmlColor, Size, Value.Name]);
  218. if fsBold in Value.Style then
  219. begin
  220. FEndSection := cHTMLBoldEnd + FEndSection;
  221. Result := Result + cHTMLBoldBegin;
  222. end;
  223. if fsItalic in Value.Style then
  224. begin
  225. FEndSection := cHTMLItalicEnd + FEndSection;
  226. Result := Result + cHTMLItalicBegin;
  227. end;
  228. if fsStrikeout in Value.Style then
  229. begin
  230. FEndSection := cHTMLStrikeoutEnd + FEndSection;
  231. Result := Result + cHTMLStrikeoutBegin;
  232. end;
  233. if fsUnderline in Value.Style then
  234. begin
  235. FEndSection := cHTMLUnderlineEnd + FEndSection;
  236. Result := Result + cHTMLUnderlineBegin;
  237. end;
  238. // if Value.Link then
  239. // begin
  240. // FEndSection := '</a>' + FEndSection;
  241. // Result := Result + '<a href="#">';
  242. // end;
  243. end;
  244. function Diff(One, Two: TFontInfo): Boolean;
  245. begin
  246. Result := (One.Color <> Two.Color) or (One.Style <> Two.Style) or
  247. (One.Name <> Two.Name) or (One.Size <> Two.Size) or
  248. (One.Link <> Two.Link);
  249. end;
  250. function DiffPara(One, Two: TJvParaAttributesRec): Boolean;overload;
  251. begin
  252. Result := (One.Alignment <> Two.Alignment) or (One.Numbering <> Two.Numbering);
  253. end;
  254. function DiffPara(One, Two: TJvRichEditParaAttributesRec): Boolean;overload;
  255. begin
  256. Result := (One.Alignment <> Two.Alignment) or (One.Numbering <> Two.Numbering);
  257. end;
  258. procedure TJvRichEditToHtml.ConvertToHtml(Value: TJvRichEdit; const FileName: string);
  259. var
  260. S: TStringList;
  261. begin
  262. S := TStringList.Create;
  263. try
  264. ConvertToHtmlStrings(Value, S);
  265. S.SaveToFile(FileName);
  266. finally
  267. S.Free;
  268. end;
  269. end;
  270. procedure TJvRichEditToHtml.ConvertToHtmlStrings(Value: TRichEdit; Strings: TStrings);
  271. var
  272. I, J: Integer;
  273. Datt, Att, CurrAt: TFontInfo;
  274. DPara, Para, CurrPara: TJvParaAttributesRec;
  275. St: TStringBuilder;
  276. FEnd: string;
  277. LOnChange: TNotifyEvent;
  278. LOnSelectionChange: TNotifyEvent;
  279. Text: string;
  280. Len: Integer;
  281. begin
  282. LOnChange := Value.OnChange;
  283. LOnSelectionChange := Value.OnSelectionChange;
  284. Strings.BeginUpdate;
  285. Value.Lines.BeginUpdate;
  286. try
  287. Value.OnChange := nil;
  288. Value.OnSelectionChange := nil;
  289. Strings.Clear;
  290. if Header.Count > 0 then
  291. Strings.Add(StringReplace(Header.Text, '<#TITLE>', Title, [rfReplaceAll]));
  292. Datt := TFontInfo.Create(Value.Font.PixelsPerInch);
  293. Att := TFontInfo.Create(Value.Font.PixelsPerInch);
  294. CurrAt := TFontInfo.Create(Value.Font.PixelsPerInch);
  295. DPara.Alignment := taLeftJustify;
  296. DPara.Numbering := ComCtrls.nsNone;
  297. CurrPara.Alignment := DPara.Alignment;
  298. CurrPara.Numbering := DPara.Numbering;
  299. Strings.Add(ParaToHtml(Para));
  300. Datt.Assign(Value.DefAttributes);
  301. Strings.Add(AttToHtml(Datt));
  302. CurrAt.Assign(Datt);
  303. Value.SelStart := 0;
  304. Value.SelectAll;
  305. Text := Value.SelText;
  306. Len := Length(Text);
  307. St := TStringBuilder.Create;
  308. try
  309. I := 1;
  310. Value.SelLength := 1;
  311. while I <= Len do
  312. begin
  313. // new line
  314. Value.SelStart := I - 1;
  315. Att.Assign(Value.SelAttributes);
  316. Para.Alignment := Value.Paragraph.Alignment;
  317. Para.Numbering := Value.Paragraph.Numbering;
  318. St.Length := 0;
  319. if DiffPara(Para, CurrPara) or (Para.Numbering = ComCtrls.nsBullet) then
  320. begin
  321. St.Append(FEndSection).Append(FEndPara);
  322. CurrPara.Alignment := Para.Alignment;
  323. CurrPara.Numbering := Para.Numbering;
  324. CurrAt.Assign(Att);
  325. St.Append(ParaToHtml(Para)).Append(AttToHtml(Att));
  326. end;
  327. J := I;
  328. while (J <= Len) and not (Text[J] in [#$A, #$B, #$D]) do { RICHEDIT uses #$B also for line breaking }
  329. begin
  330. Att.Assign(Value.SelAttributes);
  331. if Diff(Att, CurrAt) then
  332. begin
  333. St.Append(FEndSection);
  334. CurrAt.Assign(Att);
  335. St.Append(AttToHtml(Att));
  336. end;
  337. if Text[J] in ['A'..'Z', 'a'..'z', '0'..'9'] then
  338. St.Append(Text[J])
  339. else
  340. St.Append(CharToHtml(Text[J]));
  341. Inc(J);
  342. Value.SelStart := J - 1;
  343. end;
  344. if I = 1 then
  345. Strings.Add(St.ToString())
  346. else
  347. Strings.Add(cHTMLBR + St.ToString());
  348. I := J + 1;
  349. end;
  350. finally
  351. St.Free;
  352. end;
  353. Strings.Add(FEndSection);
  354. Strings.Add(FEndPara);
  355. Datt.Free;
  356. Att.Free;
  357. CurrAt.Free;
  358. Strings.Add(FEnd);
  359. Strings.AddStrings(Footer);
  360. finally
  361. Value.OnChange := LOnChange;
  362. Value.OnSelectionChange := LOnSelectionChange;
  363. Strings.EndUpdate;
  364. Value.Lines.EndUpdate;
  365. end;
  366. end;
  367. procedure TJvRichEditToHtml.ConvertToHtmlStrings(Value: TJvRichEdit; Strings: TStrings);
  368. var
  369. I, J: Integer;
  370. Datt, Att, CurrAt: TFontInfo;
  371. DPara, Para, CurrPara: TJvRichEditParaAttributesRec;
  372. St: TStringBuilder;
  373. FEnd: string;
  374. LOnChange: TNotifyEvent;
  375. LOnSelectionChange: TNotifyEvent;
  376. Text: string;
  377. Len: Integer;
  378. begin
  379. LOnChange := Value.OnChange;
  380. LOnSelectionChange := Value.OnSelectionChange;
  381. Strings.BeginUpdate;
  382. Value.Lines.BeginUpdate;
  383. try
  384. Value.OnChange := nil;
  385. Value.OnSelectionChange := nil;
  386. Strings.Clear;
  387. if Header.Count > 0 then
  388. Strings.Add(StringReplace(Header.Text, '<#TITLE>', Title, [rfReplaceAll]));
  389. Datt := TFontInfo.Create(Value.Font.PixelsPerInch);
  390. Att := TFontInfo.Create(Value.Font.PixelsPerInch);
  391. CurrAt := TFontInfo.Create(Value.Font.PixelsPerInch);
  392. DPara.Alignment := paLeftJustify;
  393. DPara.Numbering := nsNone;
  394. CurrPara.Alignment := DPara.Alignment;
  395. CurrPara.Numbering := DPara.Numbering;
  396. Strings.Add(ParaToHtml(Para));
  397. Datt.Assign(Value.DefAttributes);
  398. Strings.Add(AttToHtml(Datt));
  399. CurrAt.Assign(Datt);
  400. Value.SelStart := 0;
  401. Value.SelectAll;
  402. Text := Value.SelText;
  403. Len := Length(Text);
  404. St := TStringBuilder.Create;
  405. try
  406. I := 1;
  407. Value.SelLength := 1;
  408. while I <= Len do
  409. begin
  410. // new line
  411. Value.SelStart := I - 1;
  412. Att.Assign(Value.SelAttributes);
  413. Para.Alignment := Value.Paragraph.Alignment;
  414. Para.Numbering := Value.Paragraph.Numbering;
  415. St.Length := 0;
  416. if DiffPara(Para, CurrPara) or (Para.Numbering = nsBullet) then
  417. begin
  418. St.Append(FEndSection).Append(FEndPara);
  419. CurrPara.Alignment := Para.Alignment;
  420. CurrPara.Numbering := Para.Numbering;
  421. CurrAt.Assign(Att);
  422. St.Append(ParaToHtml(Para)).Append(AttToHtml(Att));
  423. end;
  424. J := I;
  425. while (J <= Len) and not (Text[J] in [#$A, #$B, #$D]) do { RICHEDIT uses #$B also for line breaking }
  426. begin
  427. Att.Assign(Value.SelAttributes);
  428. if Diff(Att, CurrAt) then
  429. begin
  430. St.Append(FEndSection);
  431. CurrAt.Assign(Att);
  432. St.Append(AttToHtml(Att));
  433. end;
  434. if Text[J] in ['A'..'Z', 'a'..'z', '0'..'9'] then
  435. St.Append(Text[J])
  436. else
  437. St.Append(CharToHtml(Text[J]));
  438. Inc(J);
  439. Value.SelStart := J - 1;
  440. end;
  441. if I = 1 then
  442. Strings.Add(St.ToString())
  443. else
  444. Strings.Add(cHTMLBR + St.ToString());
  445. I := J + 1;
  446. end;
  447. finally
  448. St.Free;
  449. end;
  450. Strings.Add(FEndSection);
  451. Strings.Add(FEndPara);
  452. Datt.Free;
  453. Att.Free;
  454. CurrAt.Free;
  455. Strings.Add(FEnd);
  456. Strings.AddStrings(Footer);
  457. finally
  458. Value.OnChange := LOnChange;
  459. Value.OnSelectionChange := LOnSelectionChange;
  460. Strings.EndUpdate;
  461. Value.Lines.EndUpdate;
  462. end;
  463. end;
  464. function TJvRichEditToHtml.ParaToHtml(Value: TJvRichEditParaAttributesRec): string;
  465. begin
  466. case Value.Alignment of
  467. paLeftJustify:
  468. Result := 'ALIGN="LEFT"';
  469. paRightJustify:
  470. Result := 'ALIGN="RIGHT"';
  471. paCenter:
  472. Result := 'ALIGN="CENTER"';
  473. end;
  474. if Value.Numbering = nsBullet then
  475. begin
  476. Result := '<LI ' + Result + '>';
  477. FEndPara := '</LI>';
  478. end
  479. else
  480. begin
  481. Result := '<P ' + Result + '>';
  482. FEndPara := '</P>';
  483. end
  484. end;
  485. function TJvRichEditToHtml.ParaToHtml(Value: TJvParaAttributesRec): string;
  486. begin
  487. case Value.Alignment of
  488. Classes.taLeftJustify:
  489. Result := 'ALIGN="LEFT"';
  490. Classes.taRightJustify:
  491. Result := 'ALIGN="RIGHT"';
  492. Classes.taCenter:
  493. Result := 'ALIGN="CENTER"';
  494. end;
  495. if Value.Numbering = ComCtrls.nsBullet then
  496. begin
  497. Result := '<LI ' + Result + '>';
  498. FEndPara := '</LI>';
  499. end
  500. else
  501. begin
  502. Result := '<P ' + Result + '>';
  503. FEndPara := '</P>';
  504. end
  505. end;
  506. function TJvRichEditToHtml.GetFooter: TStrings;
  507. begin
  508. Result := FFooter;
  509. end;
  510. function TJvRichEditToHtml.GetHeader: TStrings;
  511. begin
  512. Result := FHeader;
  513. end;
  514. procedure TJvRichEditToHtml.SetFooter(const Value: TStrings);
  515. begin
  516. FFooter.Assign(Value);
  517. end;
  518. procedure TJvRichEditToHtml.SetHeader(const Value: TStrings);
  519. begin
  520. FHeader.Assign(Value);
  521. end;
  522. {$IFDEF UNITVERSIONING}
  523. initialization
  524. RegisterUnitVersion(HInstance, UnitVersioning);
  525. finalization
  526. UnregisterUnitVersion(HInstance);
  527. {$ENDIF UNITVERSIONING}
  528. end.