unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
Spin;
type
TTextStyle = (tsNone, tsSuper, tsSub);
TOneLine = packed record
X, Y: Integer;
TextStyle: TTextStyle;
Line: string;
end;
TAllLines = array of TOneLine;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
cbSub: TCheckBox;
cbSuper: TCheckBox;
Edit1: TEdit;
Label1: TLabel;
Panel1: TPanel;
Panel2: TPanel;
SpinEdit1: TSpinEdit;
procedure Button1Click(Sender: TObject);
procedure cbSuperChange(Sender: TObject);
procedure cbSubChange(Sender: TObject);
procedure FormCreate(Sender: TObject);
strict private
FBMP: TBitmap;
Lines: TAllLines;
strict private
procedure AddLine(const ABitmap: TBitmap; const AFont: TFont;
const AText: string; const AStyle: TTextStyle = tsNone;
const X: Integer = -1; const Y: Integer = -1);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
begin
FBMP := TBitmap.Create;
FBMP.SetSize(Panel2.ClientRect.Width, Panel2.ClientRect.Height);
end;
procedure TForm1.AddLine(const ABitmap: TBitmap; const AFont: TFont;
const AText: string; const AStyle: TTextStyle = tsNone;
const X: Integer = -1; const Y: Integer = -1);
var
LX, LY: Integer;
i: Integer;
FontSize: Integer;
begin
// sanity check
if AText = '' then
Exit;
ABitmap.Canvas.Brush.Style := bsClear;
ABitmap.Canvas.Font := AFont;
ABitmap.Canvas.Font.Color := clWhite;
// precheck X/Y
if (X = -1) and (Length(Lines) = 0) then
LX := 0;
if (Y = -1) and (Length(Lines) = 0) then
LY := 0;
if (X = -1) and (Length(Lines) > 0) then
LX := Lines[High(Lines)].X + ABitmap.Canvas.TextWidth(Lines[High(Lines)].Line);
if (Y = -1) and (Length(Lines) > 0) then
LY := Lines[High(Lines)].Y;
i := Length(Lines);
SetLength(Lines, Succ(i));
Lines[i].Line := AText;
Lines[i].X := LX;
Lines[i].Y := LY;
Lines[i].TextStyle := AStyle;
case AStyle of
tsNone: ABitmap.Canvas.TextOut(Lines[i].X, Lines[i].Y, Lines[i].Line);
tsSuper: begin
if ABitmap.Canvas.Font.Size = 0 then
FontSize := (9 * 2) div 3
else
FontSize := (ABitmap.Canvas.Font.Size * 2) div 3
ABitmap.Canvas.Font.Size := FontSize;
ABitmap.Canvas.TextOut(Lines[i].X, Lines[i].Y - FontSize, Lines[i].Line);
end;
tsSub: begin
if ABitmap.Canvas.Font.Size = 0 then
FontSize := (9 * 2) div 3
else
FontSize := (ABitmap.Canvas.Font.Size * 2) div 3
ABitmap.Canvas.Font.Size := FontSize;
ABitmap.Canvas.TextOut(Lines[i].X, Lines[i].Y + FontSize, Lines[i].Line);
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if ((not cbSuper.Checked) and (not cbSub.Checked)) then
AddLine(FBMP, Self.Font, Edit1.Text);
if (cbSuper.Checked and (not cbSub.Checked)) then
AddLine(FBMP, Self.Font, Edit1.Text, tsSuper);
if ((not cbSuper.Checked) and cbSub.Checked) then
AddLine(FBMP, Self.Font, Edit1.Text, tsSub);
Panel2.Canvas.Draw(0, 0, FBMP);
end;
procedure TForm1.cbSuperChange(Sender: TObject);
begin
if cbSuper.Checked then
cbSub.Checked := False;
end;
procedure TForm1.cbSubChange(Sender: TObject);
begin
if cbSub.Checked then
cbSuper.Checked := False;
end;
end.