Page 1 of 1

column to start printing

Posted: Thu Dec 09, 2021 10:44 pm
by carlazza
Hi all,

I need to print a fixed word followed by a variable field. How can I calculate the column to start printing the variable?

ex:

NAME := "Carlo"

@030,050 PRINT 'My name is ' Font 'calibri' Size 12
@030,??? PRINT NAME Font 'calibri' SIZE 12 BOLD

I hope I was clear

Thanks
Carlo

Re: column to start printing

Posted: Thu Dec 09, 2021 11:45 pm
by AUGE_OHR
hi,

use

Code: Select all

nPixelX := GetTextWidth( hDC, TRIM( cText ), hFont )
to calculate Size of Text

Re: column to start printing

Posted: Fri Dec 10, 2021 5:06 pm
by franco
I would use

NAME := "Carlo"
Ctxt := 'My name is '
@030,050 PRINT Ctxt Font 'calibri' Size 12
@030,050+len(Ctxt) PRINT NAME Font 'calibri' SIZE 12 BOLD

Re: column to start printing

Posted: Fri Dec 10, 2021 9:27 pm
by carlazza
AUGE_OHR wrote: Thu Dec 09, 2021 11:45 pm hi,

use

Code: Select all

nPixelX := GetTextWidth( hDC, TRIM( cText ), hFont )
to calculate Size of Text
Thanks AUGE_OHR

Could you please explain it to me better, using my example?

Carlo

Re: column to start printing

Posted: Fri Dec 10, 2021 9:30 pm
by carlazza
franco wrote: Fri Dec 10, 2021 5:06 pm I would use

NAME := "Carlo"
Ctxt := 'My name is '
@030,050 PRINT Ctxt Font 'calibri' Size 12
@030,050+len(Ctxt) PRINT NAME Font 'calibri' SIZE 12 BOLD
Thanks franco but I think that your solution doesn't work

Function "LEN" does not take into account the text's font and his size

Carlo

Re: column to start printing

Posted: Sat Dec 11, 2021 1:09 am
by AUGE_OHR
hi,
carlazza wrote: Fri Dec 10, 2021 9:27 pm Could you please explain it to me better, using my example?
try this

Code: Select all

LOCAL cNAME := "Carlo"
LOCAL cFont := 'calibri' 
LOCAL nSize := 12

   @030,050 PRINT 'My name is ' Font cFont Size nSize
// correction
// nCol := CalcWide(cName,cFont , nSize)
   nCol := CalcWide('My name is ',cFont , nSize)
   @030,nCol PRINT cNAME Font cFont SIZE nSize 

FUNCTION CalcWidth(cName,cFont,nSize, lBold)
LOCAL hDC := GetDC(0)
// HMG_CreateFont ( [ hDC ] , cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeOut ) --> hFont
LOCAL hFont := HMG_CreateFont( hDC, cFont , nSize, IF(lBold,.T.,.F.), .F., .F., .F. )
   nPixelX := GetTextWidth( hDC, cName, hFont )
RETURN nPixelX

Re: column to start printing

Posted: Sat Dec 11, 2021 3:22 pm
by edk
AUGE_OHR wrote: Sat Dec 11, 2021 1:09 am hi,
carlazza wrote: Fri Dec 10, 2021 9:27 pm Could you please explain it to me better, using my example?
try this

Code: Select all

LOCAL cNAME := "Carlo"
LOCAL cFont := 'calibri' 
LOCAL nSize := 12

   @030,050 PRINT 'My name is ' Font cFont Size nSize
// correction
// nCol := CalcWide(cName,cFont , nSize)
   nCol := CalcWide('My name is ',cFont , nSize)
   @030,nCol PRINT cNAME Font cFont SIZE nSize 

FUNCTION CalcWidth(cName,cFont,nSize, lBold)
LOCAL hDC := GetDC(0)
// HMG_CreateFont ( [ hDC ] , cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeOut ) --> hFont
LOCAL hFont := HMG_CreateFont( hDC, cFont , nSize, IF(lBold,.T.,.F.), .F., .F., .F. )
   nPixelX := GetTextWidth( hDC, cName, hFont )
RETURN nPixelX
Unfortunately, I can't quite agree with what you wrote.
First, there is a logical error in the placement of the second phrase in the printout. It should be

Code: Select all

@030,050 + nCol PRINT cNAME Font cFont SIZE nSize BOLD
Secondly, your CalcWidth function returns a value expressed in pixels, and we use millimeters to define a printout.
Third, you refer to the object hDC: = GetDC (0), which is the desktop / screen and its DPI. The DPI of the screen has nothing to do with the DPI of the printer.

So, we need some additional information:
DPI of the printer, converting pixels to millimeters.

I would do it this way ;) :

Code: Select all

cTextToPrint := 'My name is '
cName := "Carlo"

cPrintFontName := "Calibri"
nPrintFontSize := 12

nLenTextToPrint := GetTxtLen ( cTextToPrint, cPrintFontName, nPrintFontSize )

@030,050 PRINT cTextToPrint Font cPrintFontName Size nPrintFontSize
@030,050 + nLenTextToPrint PRINT cName Font cPrintFontName SIZE nPrintFontSize BOLD

***************************************************************************************************
FUNCTION GetTxtLen ( cText, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeout )
RETURN HMG_GethDCTextWidthinMM (OpenPrinterGetPageDC(), cText, cPrintFont, nFontSize, lBold, lItalic, lUnderline, lStrikeout )
***************************************************************************************************
FUNCTION HMG_GethDCTextWidthInMM (hDC, cText, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeout)
RETURN (HMG_GethDCTextWidthInPx (hDC, cText, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeout) * 25.4 ) / HMG_GetPrinterResolution( hDC )[1] 
***************************************************************************************************
FUNCTION HMG_GethDCTextWidthInPx (hDC, cText, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeout)
Local hFont
Default cText:=""
Default cFontName := _HMG_SYSDATA [ 342 ]
Default nFontSize := _HMG_SYSDATA [ 343 ]
Default lItalic := .F.
Default lUnderline := .F.
Default lStrikeout := .F.
Default lBold := .F.

hFont := HMG_CreateFont (hDC, cFontName, nFontSize, lBold, lItalic, lUnderline, lStrikeOut )
RETURN GetTextWidth( hDC, cText, hFont )
***********************************************************************
#pragma begindump
#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"

#include <windows.h>
#include <hbapi.h>

HB_FUNC ( HMG_GETPRINTERRESOLUTION )
{
	hb_reta (2);
	hb_storvni ( GetDeviceCaps( (HDC) hb_parnl (1), LOGPIXELSX ),         -1, 1 ); 
	hb_storvni ( GetDeviceCaps( (HDC) hb_parnl (1), LOGPIXELSY ),         -1, 2 ); 
} 

#pragma ENDDUMP
************************************************************************************************************
It seems to me that this problem has probably already been discussed on the forum, but I cannot recall the topic.

Re: column to start printing

Posted: Sat Dec 11, 2021 4:51 pm
by franco
Is hpdfprint in pixels or millimeters ?
I am having this problem with printing memo files to hpdfprint.
One of these options could work for me, I may need help because I need to make the line length to stop at the end of a word.
Someone may have this solution already.
Thanks.

Re: column to start printing

Posted: Sat Dec 11, 2021 7:41 pm
by edk
franco wrote: Sat Dec 11, 2021 4:51 pm Is hpdfprint in pixels or millimeters ?
I am having this problem with printing memo files to hpdfprint.
One of these options could work for me, I may need help because I need to make the line length to stop at the end of a word.
Someone may have this solution already.
Thanks.
Hpdfprint is in millimeters also.
Look at this topic, I think this may be helpful https://www.hmgforum.com/viewtopic.php?p=57056#p57056

Re: column to start printing

Posted: Sat Dec 18, 2021 5:43 pm
by franco
I have been offline for a bit.
Thanks to all for advive. I now have time to check this out.