?
Solved

printing diffent fonts in one line

Posted on 1998-10-09
5
Medium Priority
?
321 Views
Last Modified: 2013-12-03
hi,

i have the following problem in D3 under WIN95 /98:
i will print some text, which consists of
different fonts, in one line.
i tried to care for the different text-height,
but the whole text doesn't stand in one line.
The diffence is very large by changing between fonts of
different pitch : variable and fixed.
below is an (simple) example for what i mean.
in the end should be printed in one(!!) line
ABCDEFABCDEF .
But the second part stands a little bit heigher
than the first.

var
a,b:Pchar;
fo1,fo2:Tfont;
il1,il2:integer;
ih1,ih2:integer;
size:Tsize;
x,y:integer;
zz:shortstring;

begin
a:='ABCDEF';
b:='ABCDEF';
fo1:=Tfont.create;
fo1.name:='Times';
fo1.size:=60;
fo1.pitch:=fpvariable;
fo2:=Tfont.create;
fo2.name:='COURIER';
fo2.size:=60;
fo2.pitch:=fpfixed;
x:=100;
y:=200;
printer.BeginDoc;
selectobject(printer.canvas.handle,fo1.handle);
gettextextentpoint32(printer.canvas.handle,a,strlen(a),size);
il1:=size.cx;
ih1:=size.cy;
zz:=strpas(a);
printer.canvas.textout(x,y,zz);
x:=x+il1;
selectobject(printer.canvas.handle,fo2.handle);
gettextextentpoint32(printer.canvas.handle,b,strlen(b),size);
il2:=size.cx;
ih2:=size.cy;
zz:=strpas(b);
y:=y+ih1-ih2;
printer.canvas.textout(x,y,zz);
printer.enddoc;
fo1.free;
fo2.free;
end;

What is the reason and what can i do ??

thanks
titz

0
Comment
Question by:titz
  • 3
5 Comments
 
LVL 1

Accepted Solution

by:
Roadrunner100598 earned 150 total points
ID: 1342384
Hello titz,

try this...

place a button and two lables on a form.


procedure TForm1.Button1Click(Sender: TObject);
var
  ARect      : TRect;
  Textmetric : TTextmetric;
  Ascent     : integer;
begin
  ARect:=Rect(100,100,1000,1000);
  Canvas.Font.Name :='Times';
  Canvas.Font.Size :=30;
  Canvas.Font.Style:=[fsBold];
  GetTextMetrics(Canvas.Handle, Textmetric);

  { Determin distance  'top - basline' for first font... }
  Ascent:=Textmetric.tmAscent;
  DrawText(Canvas.Handle, 'AaGg', -1, ARect, DT_CalcRect or DT_BOTTOM);
  DrawText(Canvas.Handle, 'AaGg', -1, ARect, DT_BOTTOM);
  label1.Caption:=format('%d  %d  %d  %d', [ARect.Top, ARect.Bottom, ARect.Left, ARect.Right]);

  Canvas.Font.Name :='Courier';
  GetTextMetrics(Canvas.Handle, Textmetric);
  { Offset by width of text and correct for baseline change in second font }
  OffsetRect(ARect, ARect.Right-ARect.Left, Ascent-Textmetric.tmAscent);
  DrawText(Canvas.Handle, 'AaGg', -1, ARect, DT_CalcRect or DT_BOTTOM);
  DrawText(Canvas.Handle, 'AaGg', -1, ARect, DT_BOTTOM);
  label2.Caption:=format('%d  %d  %d  %d', [ARect.Top, ARect.Bottom, ARect.Left, ARect.Right]);
end;

The main trick is the GetTextmetrics( ) to find the different baselines of the fonts. The rest is relatively simple. I prefer the use of DrawText( ), because it has much more flexibility than TextOut( ).



Hope you like this,

beep, beep

Roadrunner
0
 
LVL 1

Expert Comment

by:borup
ID: 1342385
You can also consider printing through the TRichEdit component which seems to take care of the baseline problem.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    PrintButton: TButton;
    procedure PrintButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.PrintButtonClick(Sender: TObject);
var fo1, fo2  : TFont;
begin
  //Font 1
  fo1:=TFont.Create;
  fo1.name:='Times';
  fo1.size:=60;
  fo1.pitch:=fpvariable;

  //Font 2
  fo2:=TFont.Create;
  fo2.name:='COURIER';
  fo2.size:=60;
  fo2.pitch:=fpfixed;

  with RichEdit1 do
    begin
      RichEdit1.Lines.Add('ABCDEFABCDEF');

      //Change the first ABCDEF to font 1
      SelStart := 0;
      SelLength := 6;
      SelAttributes.Assign(fo1);

      //Change the second ABCDEF to font 2
      SelStart := 6;
      SelLength := 6;
      SelAttributes.Assign(fo2);

      //Print the text
      Print('Document Title');
    end;
end;

end.
0
 
LVL 1

Expert Comment

by:borup
ID: 1342386
You can also consider printing through the TRichEdit component which seems to take care of the baseline problem.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    PrintButton: TButton;
    procedure PrintButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.PrintButtonClick(Sender: TObject);
var fo1, fo2  : TFont;
begin
  //Font 1
  fo1:=TFont.Create;
  fo1.name:='Times';
  fo1.size:=60;
  fo1.pitch:=fpvariable;

  //Font 2
  fo2:=TFont.Create;
  fo2.name:='COURIER';
  fo2.size:=60;
  fo2.pitch:=fpfixed;

  with RichEdit1 do
    begin
      RichEdit1.Lines.Add('ABCDEFABCDEF');

      //Change the first ABCDEF to font 1
      SelStart := 0;
      SelLength := 6;
      SelAttributes.Assign(fo1);

      //Change the second ABCDEF to font 2
      SelStart := 6;
      SelLength := 6;
      SelAttributes.Assign(fo2);

      //Print the text
      Print('Document Title');
    end;
end;

end.
0
 
LVL 1

Expert Comment

by:borup
ID: 1342387
Sorry for submitting my comment twice :-)
0
 
LVL 1

Author Comment

by:titz
ID: 1342388
thanks roadrunner,
that was what i was looking for !
titz
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question