Forum > Graphics

May be useful to somebody

(1/1)

Ten_Mile_Hike:

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---The attached picture uses only Tlabels to depict an exponent as thoughit was a superscript. I did this by placing a tiny label on top of the base label as wellas adjusting the text layout properties and font sizes of both. The tiny TLabel onlydisplays the exponent as its caption.I am only posting this in the chance that it might help someone who wants a rich texteffect but doesn't want to use a richtext component

Handoko:
Nice.

I have a different solution, using TCanvas:
https://forum.lazarus.freepascal.org/index.php/topic,46775.msg333936.html#msg333936

KodeZwerg:
Good one Handoko, I was not aware about yours and started my own, basics are same I think.
My thingy aint perfect nor ready, just some basics prepared to make it better.
Button1 initiate the action on Panel2.
2 checkboxes and a TEdit is used.
As of right now my way does write only to the width, height/wordwrap is not teached yet but you can call method seperate with proper coordinate.

TODO:
better color managment, currently it is fixed to black background and white text.
boundschecking if written text is inside rect, wordwrap etc


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.

Navigation

[0] Message Index

Go to full version