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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
|
unit UHtmlToRtf;
interface
uses Classes, Math, ComCtrls;
procedure HTMLtoRTF(html: string; var rtf: TRichEdit);
implementation
uses RichEdit, Graphics, SysUtils, Generics.Collections;
type
TCharacterFormat = (CFM_Superscript, CFM_Subscript, CFM_Normal);
TPosiStyle = class
Font: TFont;
CharFmt: TCharacterFormat;
SelStart: Integer;
SelLength: Integer;
align : String;
IsPara : Boolean;
IsListe: Boolean;
NbTab : Integer;
destructor destroy; Override;
end;
TPosiStyleList = Class( TObjectList<TPosiStyle>)
public
function Add( aObj : TPosiStyle ) : Integer;
End;
TFontStack = class(TObjectList<TFont>)
public
procedure Pushfont( afont : TFont );
procedure popFont( aFont : TFont );
end;
procedure RE_SetCharFormat(RichEdit: TRichEdit; CharacterFormat: TCharacterFormat);
var
{The CHARFORMAT structure contains information about character formatting
in a rich edit control}
Format: TCharFormat;
begin
FillChar(Format, SizeOf(Format), 0);
with Format do
begin
cbSize := SizeOf(Format);
dwMask := CFM_OFFSET;
{Character offset, in twips, from the baseline. If the value of this member
is positive, the character is a superscript; if it is negative, the
character is a subscript}
case CharacterFormat of
CFM_Superscript: yOffset := 60;
CFM_Subscript: yOffset := -60;
CFM_Normal: yOffset := 0;
end;
end;
{The EM_SETCHARFORMAT message sets character formatting in a rich edit control.
SCF_SELECTION: Applies the formatting to the current selection}
Richedit.Perform(EM_SETCHARFORMAT, SCF_SELECTION, Longint(@Format));
end;
function StringToAlign( aVal : String ) : TAlignment;
begin
Result := taLeftJustify;
if Sametext( aVal, 'right' ) then
Result := taRightJustify
else
if Sametext( aVal, 'left' ) then
Result := taLeftJustify
else
if Sametext( aVal, 'center' ) then
Result := taCenter
end;
function GetTag(s: string; var i: Integer; var Tag, tagparams: string): Boolean;
var
a_tag: Boolean;
begin
GetTag := False;
Tag := '';
tagparams := '';
a_tag := False;
while i <= Length(s) do
begin
Inc(i);
// es wird nochein tag geöffnet --> das erste war kein tag;
if s[i] = '<' then
begin
GetTag := False;
Exit;
end;
if s[i] = '>' then
begin
GetTag := True;
Exit;
end;
if not a_tag then
begin
if s[i] = ' ' then
begin
if Tag <> '' then
a_tag := True;
end
else
Tag := Tag + s[i];
end
else
tagparams := tagparams + s[i];
end;
end;
procedure GetTagParams(tagparams: string; var params: TStringList);
var
i: Integer;
s: string;
gleich: Boolean;
InStartValue : Boolean;
startValchar : Char;
// kontrolliert ob nach dem zeichen bis zum nächsten zeichen ausser
// leerzeichen ein Ist-Gleich-Zeichen kommt
function notGleich(s: string; i: Integer): Boolean;
begin
notGleich := True;
while i <= Length(s) do
begin
Inc(i);
if s[i] = '=' then
begin
notGleich := False;
Exit;
end
else if s[i] <> ' ' then
Exit;
end;
end;
begin
params.Clear;
s := '';
InStartValue := false;
startValchar := ' ';
gleich := false;
for i := 1 to Length(tagparams) do
begin
if (tagparams[i] <> ' ') or InStartValue then
begin
if tagparams[i] <> '=' then
gleich := False;
if ( (tagparams[i] <> '''') and (tagparams[i] <> '"') ) then
s := s + tagparams[i];
if (tagparams[i] = '"') or (tagparams[i] = '''') then
begin
if not InStartValue then
startValchar := tagparams[i];
if startValchar = tagparams[i] then
InStartValue := not InStartValue;
end;
end
else
begin
if (notGleich(tagparams, i)) and (not gleich) then
begin
params.Add(s);
s := '';
end
else
gleich := True;
end;
end;
params.Add(s);
end;
function HtmlToColor(Color: string): TColor;
begin
Result := StringToColor('$' + Copy(Color, 6, 2) + Copy(Color, 4, 2) +
Copy(Color, 2, 2));
end;
procedure TransformSpecialChars(var s: string; i: Integer);
var
c: string;
z, z2: Byte;
i2: Integer;
const
nchars = 9;
chars: array [1 .. nchars, 1 .. 2] of string = (('Ö', 'Ö'), ('ö', 'ö'),
('Ä', 'Ä'), ('ä', 'ä'), ('Ü', 'Ü'), ('ü', 'ü'), ('ß', 'ß'), ('<', '<'),
('>', '>'));
begin
// Maximal die nächsten 7 zeichen auf sonderzeichen überprüfen
c := '';
i2 := i;
for z := 1 to 7 do
begin
c := c + s[i2];
for z2 := 1 to nchars do
begin
if chars[z2, 1] = c then
begin
Delete(s, i, Length(c));
Insert(chars[z2, 2], s, i);
Exit;
end;
end;
Inc(i2);
end;
end;
// HtmlTag Schriftgröße in pdf größe umwandeln
function CalculateRTFSize(pt: Integer): Integer;
begin
case pt of
1:
Result := 8;
2:
Result := 10;
3:
Result := 12;
4:
Result := 18;
5:
Result := 20;
6:
Result := 28;
7 :
Result := 36;
else
Result := 40;
end;
end;
procedure HTMLtoRTF(html: string; var rtf: TRichEdit);
var
aList : TPosiStyleList;
aFmt : TPosiStyle;
FontStack : TFontStack;
aParaList : TPosiStyleList;
aPara : TPosiStyle;
ParaStack : TList<TPosiStyle>;
aListeList : TPosiStyleList;
ListStack : TList<TPosiStyle>;
i, row: Integer;
cfont: TFont; { Standard ecriture }
Tag, tagparams: string;
params: TStringList;
wordwrap, liste: Boolean;
CarFmt : TCharacterFormat;
delta : Integer;
sTemp : String;
aALign : String;
NbTab : Integer;
begin
aList := TPosiStyleList.Create;
FontStack := TFontStack.Create;
aParaList := TPosiStyleList.Create;
ParaStack := TList<TPosiStyle>.Create;
aListeList := TPosiStyleList.Create;
ListStack := TList<TPosiStyle>.Create;
rtf.Lines.BeginUpdate;
rtf.Lines.Clear;
wordwrap := rtf.wordwrap;
rtf.WordWrap := False;
rtf.Lines.Add('');
Params := TStringList.Create;
cfont := TFont.Create;
cfont.Assign(rtf.Font);
FontStack.Pushfont( cfont );
i := 1;
row := 0;
Liste := False;
// Den eigentlichen Text holen und die Formatiorung merken
rtf.selstart := 0;
aALign := 'left';
CarFmt := CFM_Normal;//defaut
if Length(html) = 0 then Exit;
//*** Point de départ ***//
aFmt := TPosiStyle.Create;
aFmt.Font := TFont.Create;
aFmt.Font.Assign( cfont );
aFmt.SelStart := rtf.SelStart;
aFmt.IsListe := liste;
aFmt.CharFmt := CarFmt;
aList.Add( aFmt );
cfont.Assign(rtf.Font);
FontStack.Pushfont( cfont );
aPara := TPosiStyle.Create;
aPara.SelStart := rtf.SelStart;
aPara.align := aALign;
aParaList.Add( aPara );
ParaStack.Add(aPara);
aPara := TPosiStyle.Create;
aPara.SelStart := rtf.SelStart;
aPara.NbTab := 0;
aListeList.Add( aPara );
ListStack.Add(aPara);
repeat;
if html[i] = '<' then
begin
GetTag(html, i, Tag, tagparams);
GetTagParams(tagparams, params);
delta := 0;
// Das Font-Tag
if SameText(Tag,'FONT') then
begin
FontStack.Pushfont( cfont );
// Schrift auf fontstack sichern
if params.Values['size'] <> '' then
cfont.Size := CalculateRTFSize(StrToInt(params.Values['size']));
if params.Values['face'] <> '' then
cfont.Name := params.Values['face'];
if params.Values['color'] <> '' then cfont.Color := htmltocolor(params.Values['color']);
end
else
if SameText(Tag,'/FONT') then
begin
FontStack.popFont(cfont);
end
else
if SameText(Tag,'H1') then
begin
// Schrift auf fontstack sichern
FontStack.Pushfont( cfont );
cfont.Size := CalculateRTFSize(6);
cfont.Style := [fsBold];
end
else
if SameText(Tag, '/H1') then
begin
FontStack.popFont( cfont );
rtf.Lines.Add('');
Inc(delta);
Inc(row);
rtf.Lines.Add('');
Inc(row);
end
else // Die H-Tags-Überschriften
if SameText(Tag,'H2') then
begin
// Schrift auf fontstack sichern
FontStack.Pushfont( cfont );
cfont.Size := CalculateRTFSize(5);
cfont.Style := [fsBold];
end
else
if SameText(Tag, '/H2') then
begin
FontStack.popFont( cfont );
rtf.Lines.Add('');
Inc(delta);
Inc(row);
rtf.Lines.Add('');
Inc(row);
end
else // Die H-Tags-Überschriften
if SameText(Tag,'H3') then
begin
// Schrift auf fontstack sichern
FontStack.Pushfont( cfont );
cfont.Size := CalculateRTFSize(4);
cfont.Style := [fsBold];
end
else
if SameText(Tag, '/H3') then
begin
FontStack.popFont( cfont );
rtf.Lines.Add('');
Inc(delta);
Inc(row);
rtf.Lines.Add('');
Inc(row);
end
else // Die H-Tags-Überschriften
if SameText(Tag,'H4') then
begin
// Schrift auf fontstack sichern
FontStack.Pushfont( cfont );
cfont.Size := CalculateRTFSize(3);
cfont.Style := [fsBold];
end
else
if SameText(Tag, '/H4') then
begin
FontStack.popFont( cfont );
rtf.Lines.Add('');
Inc(delta);
Inc(row);
rtf.Lines.Add('');
Inc(row);
end
else // Die H-Tags-Überschriften
if SameText(Tag,'H5') then
begin
// Schrift auf fontstack sichern
FontStack.Pushfont( cfont );
cfont.Size := CalculateRTFSize(2);
cfont.Style := [fsBold];
end
else
if SameText(Tag, '/H5') then
begin
FontStack.popFont( cfont );
rtf.Lines.Add('');
Inc(row);
rtf.Lines.Add('');
Inc(row);
end
else // Die H-Tags-Überschriften
if SameText(Tag,'H6') then
begin
// Schrift auf fontstack sichern
FontStack.Pushfont( cfont );
cfont.Size := CalculateRTFSize(1);
cfont.Style := [fsBold];
end
else
if SameText(Tag, '/H6') then
begin
FontStack.popFont( cfont );
rtf.Lines.Add('');
Inc(delta);
Inc(row);
rtf.Lines.Add('');
Inc(row);
end
else
if SameText(Tag,'div') then
begin
if params.Values['align'] <> '' then
begin
aALign := params.Values['align'];
sTemp := StringReplace( rtf.Text,sLineBreak, #13, [rfReplaceAll] );
aPara := TPosiStyle.Create;
aPara.SelStart := Length(trim(sTemp)) + 1;
aPara.align := aALign;
aParaList.Add( aPara );
ParaStack.Add( aPara );
end;
end
else
if SameText(Tag,'/div') then
begin
aPara := ParaStack.Last;
ParaStack.Delete( ParaStack.Count - 1 );
aPara.SelLength := Length(trim(sTemp)) - aPara.SelStart;
end
else // Die H-Tags-Überschriften
if SameText(Tag,'B') or SameText(tag,'strong') then
begin
cfont.Style := cfont.Style + [fsbold];
end
else
if SameText(Tag,'/B') or SameText(tag,'/strong') then
begin
cfont.Style := cfont.Style - [fsbold];
end
else // Italic-Tag
if SameText(Tag,'I') or SameText(tag,'em') then
begin
cfont.Style := cfont.Style + [fsitalic];
end
else
if SameText(Tag,'/I') or SameText(tag,'/em') then
begin
cfont.Style := cfont.Style - [fsitalic];
end
else
if SameText(Tag,'U') then
begin
cfont.Style := cfont.Style + [fsunderline];
end
else
if SameText(Tag,'/U') then
begin
cfont.Style := cfont.Style - [fsunderline];
end
else // underline-Tag
if SameText( tag, 'UL') then
begin
liste := true;
Inc(NbTab );
sTemp := StringReplace( rtf.Text,sLineBreak, #13, [rfReplaceAll] );
aPara := TPosiStyle.Create;
aPara.SelStart := rtf.SelStart + 1;// Length(trim(sTemp));
aPara.NbTab := NbTab;
aListeList.Add( aPara );
ListStack.Add(aPara);
end
else
if SameText( tag, '/UL') then
begin
Dec(NbTab );
liste := false;
sTemp := StringReplace( rtf.Text,sLineBreak, #13, [rfReplaceAll] );
aPara := ListStack.Last;
aPara.SelLength := rtf.SelStart - aPara.SelStart;
ListStack.Delete( ListStack.Count - 1 );
if NbTab < 1 then
begin
rtf.Lines.Add('');
Inc(delta);
Inc(row);
rtf.Lines.Add('');
Inc(delta);
Inc(row);
end;
end
else
if SameText(Tag, 'BR') or SameText(Tag, 'BR/') or SameText(Tag, 'LI' ) then
begin
rtf.Lines.Add('');
Inc(row);
end
else
if SameText(tag, 'sub') then
CarFmt := CFM_Subscript
else
if SameText(tag, '/sub') then
CarFmt := CFM_Normal
else
if SameText(tag, 'sup') then
CarFmt := CFM_Superscript
else
if SameText(tag, '/sup') then
CarFmt := CFM_Normal;
// tag inconnus:
//else rtf.Lines[row]:=RTF.lines[row]+'<'+tag+' '+tagparams+'>';
sTemp := StringReplace( rtf.Text,sLineBreak, #13, [rfReplaceAll] );
aFmt := TPosiStyle.Create;
aFmt.Font := TFont.Create;
aFmt.Font.Assign( cfont );
aFmt.SelStart := Length(trim(sTemp)) + IfThen( Liste, 2, 0);
aFmt.IsListe := liste;
aFmt.CharFmt := CarFmt;
aFmt.NbTab := NbTab;
aList.Add( aFmt );
aFmt := aList[aList.Count - 2];
end
else
begin
// Spezialzeichen übersetzen
if html[i] = '&' then Transformspecialchars(html, i);
if (Ord(html[i]) <> 13) and (Ord(html[i]) <> 10) then
rtf.Lines[row] := RTF.Lines[row] + html[i];
end;
Inc(i);
until i >= Length(html);
//*** Fin ***///
rtf.Lines.Add('');
aFmt := TPosiStyle.Create;
aFmt.Font := TFont.Create;
aFmt.Font.Assign( cfont );
aFmt.SelStart := rtf.SelStart;
aFmt.IsListe := liste;
aFmt.CharFmt := CarFmt;
aList.Add( aFmt );
//*** Fin paragraphe ***//
sTemp := StringReplace( rtf.Text,sLineBreak, #13, [rfReplaceAll] );
aPara := ParaStack.Last;
aPara.SelLength := Length(trim(sTemp)) - aPara.SelStart;
sTemp := StringReplace( rtf.Text,sLineBreak, #13, [rfReplaceAll] );
aPara := ListStack.Last;
aPara.SelLength := Length(trim(sTemp)) - aPara.SelStart;
//*** Formatage du texte (on ne prend pas le fake dernier) ***//
for I := 0 to aList.Count - 2 do
begin
aFmt := aList[I];
rtf.SelStart := aFmt.SelStart;
rtf.SelLength := aFmt.SelLength;
rtf.SelAttributes.Style := aFmt.Font.Style;
rtf.SelAttributes.Size := aFmt.Font.Size;
rtf.SelAttributes.Color := aFmt.Font.Color;
rtf.SelAttributes.Name := aFmt.Font.Name;
RE_SetCharFormat(rtf, aFmt.CharFmt );
end;
for I := 0 to aListeList.Count - 1 do
begin
aPara := aListeList[I];
if aPara.NbTab < 1 then Continue;
rtf.SelStart := aPara.SelStart;
rtf.SelLength := aPara.SelLength;
rtf.Paragraph.Numbering := nsBullet;
rtf.Paragraph.FirstIndent := 20 * aPara.NbTab;
end;
for I := 0 to aParaList.Count - 1 do
begin
aPara := aParaList[I];
rtf.SelStart := aPara.SelStart;
rtf.SelLength := aPara.SelLength;
rtf.Paragraph.Alignment := StringToAlign( aPara.align );
end;
rtf.Lines.EndUpdate;
Params.Free;
cfont.Free;
rtf.WordWrap := wordwrap;
FreeAndNil( ParaStack );
FreeAndNil( ListStack );
FreeAndNil( FontStack );
FreeAndNil( aListeList );
FreeAndNil( aParaList );
FreeAndNil( aList );
end;
{ TFontStack }
procedure TFontStack.popFont(aFont: TFont);
begin
if Count > 0 then
begin
aFont.Assign( Last );
Delete(Count - 1 );
end;
end;
procedure TFontStack.Pushfont(afont: TFont);
var
tmpFnt : TFont;
begin
tmpFnt := TFont.Create;
tmpFnt.Assign( afont );
Add( tmpFnt );
end;
{ TPosiStyleList }
function TPosiStyleList.Add(aObj: TPosiStyle): Integer;
var
aLast : TPosiStyle;
begin
aLast := nil;
if Count > 0 then
aLast := Last;
if Assigned( aLast ) then
aLast.SelLength := aObj.SelStart - aLast.SelStart;
Result := Inherited Add( aObj );
end;
{ TPosiStyle }
destructor TPosiStyle.destroy;
begin
if Assigned( Font ) then
FreeAndNil( Font );
inherited;
end;
end. |
Partager