ChemText: Extended syntax for more flexible sub- and superscripts.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7976 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2021-03-12 18:48:46 +00:00
parent bc29b17193
commit b8e2cac824

View File

@ -24,6 +24,16 @@ interface
uses uses
LclIntf, LCLType, LCLVersion, Types, SysUtils, Classes, Graphics, StdCtrls; LclIntf, LCLType, LCLVersion, Types, SysUtils, Classes, Graphics, StdCtrls;
const
DEFAULT_SMALLFONT_SIZE = 67;
DEFAULT_SUBSCRIPT_OFFSET = 50;
DEFAULT_SUPERSCRIPT_OFFSET = 25;
var
SmallFontSizePercent: Integer = DEFAULT_SMALLFONT_SIZE;
SubscriptFontOffsetPercent: Integer = DEFAULT_SUBSCRIPT_OFFSET;
SuperscriptFontOffsetPercent: Integer = DEFAULT_SUPERSCRIPT_OFFSET;
type type
TChemArrow = ( TChemArrow = (
caASCIISingle, caASCIIDouble, caUTF8, caUTF8Single, caUTF8Double, caUTF8Half caASCIISingle, caASCIIDouble, caUTF8, caUTF8Single, caUTF8Double, caUTF8Half
@ -129,11 +139,15 @@ type
TArrowDir = (adLeft, adRight, adBoth); TArrowDir = (adLeft, adRight, adBoth);
const const
SUBFONT_SIZE_MULTIPLIER = 75;
SUBFONT_OFFSET_MULTIPLIER = 50;
SUBFONT_DIVISOR = 100;
ARROW_LINE: array[boolean] of char = ('-', '='); ARROW_LINE: array[boolean] of char = ('-', '=');
SUBFONT_DIVISOR = 100;
ESCAPE_CHAR = '\'; ESCAPE_CHAR = '\';
SAVE_CURRENT_POS = '@';
RETURN_TO_POS = '|';
SUPERSCRIPT_CHAR = '^';
SUBSCRIPT_CHAR = '_';
HYDRATE_DOT = #$E2#$80#$A2; HYDRATE_DOT = #$E2#$80#$A2;
function ChemTextHeight(ACanvas: TCanvas; const AText: String; function ChemTextHeight(ACanvas: TCanvas; const AText: String;
@ -172,8 +186,8 @@ var
begin begin
h := ACanvas.Font.Height; h := ACanvas.Font.Height;
try try
ACanvas.Font.Height := MulDiv(h, SUBFONT_SIZE_MULTIPLIER, SUBFONT_DIVISOR); ACanvas.Font.Height := MulDiv(h, SmallFontSizePercent, 100);
yoff := abs(MulDiv(h, SUBFONT_OFFSET_MULTIPLIER, SUBFONT_DIVISOR)); yoff := abs(MulDiv(h, SubscriptFontOffsetPercent, 100));
if not Measure then if not Measure then
ACanvas.TextOut(x, y + yoff, s); ACanvas.TextOut(x, y + yoff, s);
x := x + ACanvas.TextWidth(s); x := x + ACanvas.TextWidth(s);
@ -186,12 +200,14 @@ var
procedure DrawSup(var x: Integer; y: Integer; const s: String); procedure DrawSup(var x: Integer; y: Integer; const s: String);
var var
h: Integer; h: Integer;
yoff: Integer;
begin begin
h := ACanvas.Font.Height; h := ACanvas.Font.Height;
try try
ACanvas.Font.Height := MulDiv(h, SUBFONT_SIZE_MULTIPLIER, SUBFONT_DIVISOR); ACanvas.Font.Height := MulDiv(h, SmallFontSizePercent, 100);
yoff := abs(MulDiv(h, SuperscriptFontOffsetPercent, 100));
if not Measure then if not Measure then
ACanvas.TextOut(x, y - 1, s); ACanvas.TextOut(x, y - yoff div 2, s);
inc(x, ACanvas.TextWidth(s)); inc(x, ACanvas.TextWidth(s));
finally finally
ACanvas.Font.Height := h; ACanvas.Font.Height := h;
@ -239,6 +255,7 @@ var
s: string; s: string;
subNos: boolean; // "subscript numbers" subNos: boolean; // "subscript numbers"
escaping: Boolean; escaping: Boolean;
savedX: Integer;
begin begin
Result := Size(0, 0); Result := Size(0, 0);
if AText = '' then if AText = '' then
@ -324,11 +341,29 @@ begin
ESCAPE_CHAR: ESCAPE_CHAR:
escaping := true; escaping := true;
SAVE_CURRENT_POS:
savedX := X;
RETURN_TO_POS:
X := savedX;
SUPERSCRIPT_CHAR:
begin
inc(i);
DrawSup(X, Y, AText[i]);
end;
SUBSCRIPT_CHAR:
begin
inc(i);
DrawSub(X, Y, AText[i]);
end;
else else
begin begin
j := i+1; j := i+1;
while (j <= Length(AText)) and while (j <= Length(AText)) and
not (AText[j] in ['0'..'9', '+', '-', '<', '.', ESCAPE_CHAR]) not (AText[j] in ['0'..'9', '+', '-', '<', '.', ESCAPE_CHAR, SAVE_CURRENT_POS, RETURN_TO_POS, SUPERSCRIPT_CHAR, SUBSCRIPT_CHAR])
do do
inc(j); inc(j);
s := Copy(AText, i, j-i); s := Copy(AText, i, j-i);