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:
| procedure HTMLToRichEdit(strHTMLCode: string; RichEdit: TRichEdit); var iBreakPos, iPosCtrlCharStart, iPosCtrlCharEnd, iPrevLinesLength, i, j: integer; strLine, strCtrlChar, strBreakChar: string; TextLayouts: array of TTextLayout; CurrentTextFormat: TTextFormat; CurrentParagraphFormat: TParagraphFormat; begin RichEdit.Clear; CurrentParagraphFormat.IsItemized := False; CurrentParagraphFormat.Alignment := taLeftJustify;
strHTMLCode := StringReplace(strHTMLCode, ' ', ' ', [rfReplaceAll]);
strLine := strHTMLCode;
GetLineBreak(iBreakPos, strLine, strBreakChar, CurrentParagraphFormat);
iPrevLinesLength := 0; CurrentTextFormat.Bold := swNeutral; CurrentTextFormat.Italic := swNeutral; CurrentTextFormat.Underline := swNeutral;
while iBreakPos <> 0 do begin if iBreakPos = -1 then begin iBreakPos := 0; strHTMLCode := ''; end else begin strHTMLCode := Copy(strHTMLCode, iBreakPos + Length(strBreakChar), Length(strHTMLCode)); end; i := 0; SetLength(TextLayouts, i); iPosCtrlCharStart := Pos('<', strLine); if iPosCtrlCharStart <> 1 then begin SetLength(TextLayouts, i + 1); TextLayouts[i].strText := Copy(strLine, 1, iPosCtrlCharStart - 1); TextLayouts[i].TextFormat.Bold := swNeutral; TextLayouts[i].TextFormat.Italic := swNeutral; TextLayouts[i].TextFormat.Underline := swNeutral; strLine := Copy(strLine, iPosCtrlCharStart, Length(strLine)); inc(i); end; iPosCtrlCharEnd := Pos('>', strLine); while iPosCtrlCharEnd > 0 do begin SetLength(TextLayouts, i + 1); strCtrlChar := Copy(strLine, 2, iPosCtrlCharEnd - 2); strLine := Copy(strLine, iPosCtrlCharEnd + 1, Length(strLine)); iPosCtrlCharStart := Pos('<', strLine); if iPosCtrlCharStart > 0 then begin TextLayouts[i].strText := Copy(strLine, 1, iPosCtrlCharStart - 1); strLine := Copy(strLine, iPosCtrlCharStart, Length(strLine)); end else begin TextLayouts[i].strText := strLine; strLine := ''; end; TextLayouts[i].TextFormat.Bold := swNeutral; TextLayouts[i].TextFormat.Italic := swNeutral; TextLayouts[i].TextFormat.Underline := swNeutral;
if strCtrlChar = 'B' then TextLayouts[i].TextFormat.Bold := swActivate; if strCtrlChar = '/B' then TextLayouts[i].TextFormat.Bold := swDeactivate; if strCtrlChar = 'I' then TextLayouts[i].TextFormat.Italic := swActivate; if strCtrlChar = '/I' then TextLayouts[i].TextFormat.Italic := swDeactivate; if strCtrlChar = 'U' then TextLayouts[i].TextFormat.Underline := swActivate; if strCtrlChar = '/U' then TextLayouts[i].TextFormat.Underline := swDeactivate;
iPosCtrlCharEnd := Pos('>', strLine);
inc(i); end; if Length(strLine) > 0 then begin SetLength(TextLayouts, i + 1); TextLayouts[i].strText := strLine; TextLayouts[i].TextFormat.Bold := swNeutral; TextLayouts[i].TextFormat.Italic := swNeutral; TextLayouts[i].TextFormat.Underline := swNeutral; end;
strLine := ''; for i := Low(TextLayouts) to High(TextLayouts) do begin strLine := strLine + TextLayouts[i].strText; end; RichEdit.Lines.Add(strLine);
RichEdit.Paragraph.Alignment := CurrentParagraphFormat.Alignment;
if CurrentParagraphFormat.IsItemized and (RichEdit.Paragraph.Numbering = nsNone) then RichEdit.Paragraph.Numbering := nsBullet; if (not CurrentParagraphFormat.IsItemized) and (RichEdit.Paragraph.Numbering = nsBullet) then RichEdit.Paragraph.Numbering := nsNone;
j := 0; for i := Low(TextLayouts) to High(TextLayouts) do begin if (TextLayouts[i].TextFormat.Bold = swActivate) then CurrentTextFormat.Bold := swActivate; if (TextLayouts[i].TextFormat.Bold = swDeactivate) then CurrentTextFormat.Bold := swDeactivate;
if (TextLayouts[i].TextFormat.Italic = swActivate) then CurrentTextFormat.Italic := swActivate; if (TextLayouts[i].TextFormat.Italic = swDeactivate) then CurrentTextFormat.Italic := swDeactivate;
if (TextLayouts[i].TextFormat.Underline = swActivate) then CurrentTextFormat.Underline := swActivate; if (TextLayouts[i].TextFormat.Underline = swDeactivate) then CurrentTextFormat.Underline := swDeactivate;
RichEdit.SelStart := j + iPrevLinesLength; RichEdit.SelLength := Length(TextLayouts[i].strText);
if (CurrentTextFormat.Bold = swActivate) and not (fsBold in RichEdit.SelAttributes.Style) then RichEdit.SelAttributes.Style := RichEdit.SelAttributes.Style + [fsBold]; if (CurrentTextFormat.Bold = swDeactivate) and (fsBold in RichEdit.SelAttributes.Style) then RichEdit.SelAttributes.Style := RichEdit.SelAttributes.Style - [fsBold];
if (CurrentTextFormat.Italic = swActivate) and not (fsItalic in RichEdit.SelAttributes.Style) then RichEdit.SelAttributes.Style := RichEdit.SelAttributes.Style + [fsItalic]; if (CurrentTextFormat.Italic = swDeactivate) and (fsItalic in RichEdit.SelAttributes.Style) then RichEdit.SelAttributes.Style := RichEdit.SelAttributes.Style - [fsItalic];
if (CurrentTextFormat.Underline = swActivate) and not (fsUnderline in RichEdit.SelAttributes.Style) then RichEdit.SelAttributes.Style := RichEdit.SelAttributes.Style + [fsUnderline]; if (CurrentTextFormat.Underline = swDeactivate) and (fsUnderline in RichEdit.SelAttributes.Style) then RichEdit.SelAttributes.Style := RichEdit.SelAttributes.Style - [fsUnderline];
j := j + Length(TextLayouts[i].strText); end; iPrevLinesLength := Length(RichEdit.Text);
strLine := strHTMLCode; if iBreakPos > 0 then GetLineBreak(iBreakPos, strLine, strBreakChar, CurrentParagraphFormat); end; end;
procedure GetLineBreak(var iBreakPos: integer; var strLine, strBreakChar: string; var CurrentParagraphFormat: TParagraphFormat); var i, j: integer; str0, str1: string; IsOk: boolean; begin iBreakPos := 0; for i := 1 to 7 do begin case i of 1: str0 := '<BR>'; 2: str0 := '<LI>'; 3: str0 := '</LI>'; 4: str0 := '</P>'; 5: str0 := '<P Align="LEFT">'; 6: str0 := '<P Align="CENTER">'; 7: str0 := '<P Align="RIGHT">'; end; j := Pos(str0, strLine); if j > 0 then begin if iBreakPos = 0 then begin iBreakPos := j end else begin if j < iBreakPos then iBreakPos := j; end; end; end; str1 := Copy(strLine, iBreakPos, Length(strLine)); if iBreakPos > 0 then strLine := Copy(strLine, 1, iBreakPos - 1) else iBreakPos := -1; strBreakChar := ''; IsOk := True; while IsOk do begin IsOk := False; for i := 1 to 7 do begin case i of 1: str0 := '<BR>'; 2: str0 := '<LI>'; 3: str0 := '</LI>'; 4: str0 := '</P>'; 5: str0 := '<P Align="LEFT">'; 6: str0 := '<P Align="CENTER">'; 7: str0 := '<P Align="RIGHT">'; end; if Pos(str0, str1) = 1 then begin if (str0 <> '<BR>') or (Length(strBreakChar) = 0) then strBreakChar := strBreakChar + str0; str1 := Copy(str1, Length(str0) + 1, Length(str1));
if str0 <> '<BR>' then IsOk := True;
if str0 = '</P>' then CurrentParagraphFormat.Alignment := taLeftJustify;
if str0 = '<LI>' then CurrentParagraphFormat.IsItemized := True;
if str0 = '</LI>' then CurrentParagraphFormat.IsItemized := False;
if str0 = '<P Align="LEFT">' then begin CurrentParagraphFormat.Alignment := taLeftJustify; IsOk := True; end; if str0 = '<P Align="CENTER">' then begin CurrentParagraphFormat.Alignment := taCenter; IsOk := True; end; if str0 = '<P Align="RIGHT">' then begin CurrentParagraphFormat.Alignment := taRightJustify; IsOk := True; end; end; end; end; end; |