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,8 +251,14 @@ 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
if escaping then
begin
DrawNormal(X, Y, AText[i]);
escaping := false;
end else
case AText[i] of case AText[i] of
'0'..'9': '0'..'9':
begin begin
@@ -292,6 +300,7 @@ begin
end; end;
'-': '-':
begin
begin begin
j := i+1; j := i+1;
while (j <= Length(AText)) and (AText[j] = '-') do inc(j); while (j <= Length(AText)) and (AText[j] = '-') do inc(j);
@@ -301,13 +310,19 @@ begin
i := j; i := j;
end else // superscript - end else // superscript -
DrawSup(X, Y, '-'); DrawSup(X, Y, '-');
end;
subNos := false; subNos := false;
end; end;
ESCAPE_CHAR:
escaping := true;
else 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
not (AText[j] in ['0'..'9', '+', '-', '<', ESCAPE_CHAR])
do
inc(j); inc(j);
s := Copy(AText, i, j-i); s := Copy(AText, i, j-i);
DrawNormal(X, Y, s); DrawNormal(X, Y, s);