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

View File

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

View File

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