chemtext: Add escaping of characters with special treatment

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5963 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2017-06-26 07:56:14 +00:00
parent 0202c3c2af
commit 8afa81832f
3 changed files with 93 additions and 78 deletions

View File

@ -83,17 +83,17 @@ object Form1: TForm1
Width = 17 Width = 17
Shape = bsSpacer Shape = bsSpacer
end end
object Label1: TLabel object Label3: TChemLabel
AnchorSideRight.Control = Spacer AnchorSideRight.Control = Spacer
Left = 103 Left = 16
Height = 18 Height = 18
Top = 40 Top = 96
Width = 67 Width = 154
Anchors = [akTop, akRight] Anchors = [akTop, akRight]
Caption = 'Propanol:' Caption = 'Ion\-molecule reaction:'
ParentColor = False ParentColor = False
end end
object Label2: TLabel object Label2: TChemLabel
AnchorSideRight.Control = Spacer AnchorSideRight.Control = Spacer
Left = 40 Left = 40
Height = 18 Height = 18
@ -103,14 +103,14 @@ object Form1: TForm1
Caption = 'Chemical reaction:' Caption = 'Chemical reaction:'
ParentColor = False ParentColor = False
end end
object Label3: TLabel object Label1: TChemLabel
AnchorSideRight.Control = Spacer AnchorSideRight.Control = Spacer
Left = 19 Left = 103
Height = 18 Height = 18
Top = 96 Top = 40
Width = 151 Width = 67
Anchors = [akTop, akRight] Anchors = [akTop, akRight]
Caption = 'Ion-molecule reaction:' Caption = 'Propanol:'
ParentColor = False ParentColor = False
end end
end end

View File

@ -11,17 +11,17 @@ type
{ TForm1 } { TForm1 }
TForm1 = class(TForm) TForm1 = class(TForm)
Label1: TChemLabel;
Label2: TChemLabel;
Label3: TChemLabel;
Label4: TLabel; Label4: TLabel;
Spacer: TBevel; Spacer: TBevel;
ChemLabel1: TChemLabel; ChemLabel1: TChemLabel;
ChemLabel2: TChemLabel; ChemLabel2: TChemLabel;
ChemLabel3: TChemLabel; ChemLabel3: TChemLabel;
ChemLabel4: TChemLabel;
ChemLabel5: TChemLabel; ChemLabel5: TChemLabel;
Edit1: TEdit; Edit1: TEdit;
ChemLabel4: TChemLabel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Panel1: TPanel; Panel1: TPanel;
procedure Edit1Change(Sender: TObject); procedure Edit1Change(Sender: TObject);
private private

View File

@ -133,6 +133,7 @@ const
SUBFONT_OFFSET_MULTIPLIER = 50; SUBFONT_OFFSET_MULTIPLIER = 50;
SUBFONT_DIVISOR = 100; SUBFONT_DIVISOR = 100;
ARROW_LINE: array[boolean] of char = ('-', '='); ARROW_LINE: array[boolean] of char = ('-', '=');
ESCAPE_CHAR = '\';
function ChemTextHeight(ACanvas: TCanvas; const AText: String; function ChemTextHeight(ACanvas: TCanvas; const AText: String;
Arrow: TChemArrow = caAsciiSingle): Integer; Arrow: TChemArrow = caAsciiSingle): Integer;
@ -236,6 +237,7 @@ var
i, j: integer; i, j: integer;
s: string; s: string;
subNos: boolean; // "subscript numbers" subNos: boolean; // "subscript numbers"
escaping: Boolean;
begin begin
Result := Size(0, 0); Result := Size(0, 0);
if AText = '' then if AText = '' then
@ -249,73 +251,86 @@ begin
x0 := X; x0 := X;
subNos := false; subNos := false;
escaping := false;
i := 1; i := 1;
while i <= Length(AText) do begin while i <= Length(AText) do begin
case AText[i] of if escaping then
'0'..'9': begin
begin DrawNormal(X, Y, AText[i]);
s := AText[i]; escaping := false;
j := i+1; end else
while (j <= Length(AText)) and (AText[j] in ['0'..'9']) do case AText[i] of
inc(j); '0'..'9':
s := Copy(AText, i, j-i);
if subNos then
DrawSub(X, Y, s)
else
DrawNormal(X, Y, s);
i := j-1;
subNos := false;
end;
'<':
begin
j := i+1;
while (j <= Length(AText)) and (AText[j] in ['-', '=']) do
inc(j);
if (AText[j] = '>') then
DrawArrow(X, Y, adBoth, j-i-1)
else begin
DrawArrow(X, Y, adLeft, j-i-1);
dec(j);
end;
i := j;
subNos := false;
end;
'+':
begin
if (i > 1) and (AText[i-1] in ['A'..'Z','a'..'z','0'..'9','+',')']) then
DrawSup(X, Y, '+')
else
DrawNormal(X, Y, '+');
subNos := false;
end;
'-':
begin
j := i+1;
while (j <= Length(AText)) and (AText[j] = '-') do inc(j);
if (j <= Length(AText)) and (AText[j] = '>') then // Arrow
begin begin
DrawArrow(X, y, adRight, j-i); s := AText[i];
i := j; j := i+1;
end else // superscript - while (j <= Length(AText)) and (AText[j] in ['0'..'9']) do
DrawSup(X, Y, '-'); inc(j);
subNos := false; s := Copy(AText, i, j-i);
end; if subNos then
DrawSub(X, Y, s)
else
DrawNormal(X, Y, s);
i := j-1;
subNos := false;
end;
else '<':
begin begin
j := i+1; j := i+1;
while (j <= Length(AText)) and not (AText[j] in ['0'..'9', '+', '-', '<']) do while (j <= Length(AText)) and (AText[j] in ['-', '=']) do
inc(j); inc(j);
s := Copy(AText, i, j-i); if (AText[j] = '>') then
DrawNormal(X, Y, s); DrawArrow(X, Y, adBoth, j-i-1)
i := j-1; else begin
subNos := AText[i] in ['A'..'Z', 'a'..'z', ')']; DrawArrow(X, Y, adLeft, j-i-1);
// In these cases a subsequent number will be subscripted. dec(j);
end; end;
end; i := j;
subNos := false;
end;
'+':
begin
if (i > 1) and (AText[i-1] in ['A'..'Z','a'..'z','0'..'9','+',')']) then
DrawSup(X, Y, '+')
else
DrawNormal(X, Y, '+');
subNos := false;
end;
'-':
begin
begin
j := i+1;
while (j <= Length(AText)) and (AText[j] = '-') do inc(j);
if (j <= Length(AText)) and (AText[j] = '>') then // Arrow
begin
DrawArrow(X, y, adRight, j-i);
i := j;
end else // superscript -
DrawSup(X, Y, '-');
end;
subNos := false;
end;
ESCAPE_CHAR:
escaping := true;
else
begin
j := i+1;
while (j <= Length(AText)) and
not (AText[j] in ['0'..'9', '+', '-', '<', ESCAPE_CHAR])
do
inc(j);
s := Copy(AText, i, j-i);
DrawNormal(X, Y, s);
i := j-1;
subNos := AText[i] in ['A'..'Z', 'a'..'z', ')'];
// In these cases a subsequent number will be subscripted.
end;
end;
inc(i); inc(i);
end; end;
end; end;