Voilà:
Modified command:
@ <Row>, <Col> HPDFPRINT [DATE] <xData> TO <nToRow>, <nToCol>
[FONT <cFontName>]
[SIZE <nFontSize>]
[BOLD [IF lBold]]
[ITALIC [IF lItalic]]
[UNDERLINE [IF lUnderline]]
[STRIKEOUT [IF lStrikeout]]
[COLOR <aColor>]
[RIGHT | CENTER | JUSTIFY]
[WRAP]
[FONTSIZEFIT | HEIGHTFIT]
[GETBOTTOM <@nBottRowVar> ]
WRAP option: word-wrap even when there is no split character.
FONTSIZEFIT option: adjusting the font size (by reducing) so that all content fits within the declared area.
HEIGHTFIT option: adjusting the height of the area depending on: content, font, font size and set line spacing.
The
FONTSIZEFIT and
HEIGHTFIT options can not be used simultaneously.
Option
GETBOTTOM <@nBottRowVar>: the variable
<nBottRowVar> will get the value of the position of the bottom row of the printed area by reference. Value expressed in mm.
New feature:
_HMG_HPDF_GetHeight_MULTILINE_PRINT (<cText>, [<nLenght>], [<cFontName>], [<nFontSize>], [<lBold>], [<lItalic>], [<lWrap>]) --> nAreaHeight
Returns the calculated height of the printed area. Value expressed in mm. The height of the area depends on the function parameters and on the set line spacing ( SET HPDFPAGE LINESPACING TO <nSpacing> )
Arguments:
<cText> - printed content
<nLenght> - length of the line in mm (width of the area)
<cFontName> - font name
<nFontSize> - font size
<lBold> - .T. if the text is in bold
<lItalic> - .T. if the text is italic
<lWrap> - .T. if the word-wrap, even if there is no split character.
Step 1:
Insert this code into the hmg_hpdf.ch file by replacing the command "#xcommand @ <Row>, <Col> HPDFPRINT [DATE] <cText>;
TO <ToRow>, <ToCol>; ... ":
Code: Select all
#xcommand @ <Row> , <Col> HPDFPRINT [ DATA ] <cText> ;
TO <ToRow> , <ToCol> ;
[ <lfont : FONT> <cFontName> ] ;
[ <lsize : SIZE> <nFontSize> ] ;
[ <bold : BOLD> [ IF <lBold> ] ] ;
[ <italic : ITALIC> [ IF <lItalic> ] ] ;
[ <underline : UNDERLINE> [ IF <lUnderline> ] ] ;
[ <strikeout : STRIKEOUT> [ IF <lStrikeout> ] ] ;
[ <lcolor : COLOR> <aColor> ] ;
[ <align:CENTER,RIGHT,JUSTIFY> ] ;
[ <wrap : WRAP> ] ;
[ <fit : FONTSIZEFIT,HEIGHTFIT> ] ;
[ <lGetBottom : GETBOTTOM> <xVariable> ] ;
=> ;
_HMG_HPDF_MULTILINE_PRINT ( <Row> , <Col> , <ToRow> , <ToCol> , <cFontName> , <nFontSize> , <aColor>\[1\] , <aColor>\[2\] , <aColor>\[3\] , <cText> , ;
<.bold.> .AND. iif( HB_IsLogical(<lBold>), <lBold>, HB_IsNil(<lBold>) ) ,;
<.italic.> .AND. iif( HB_IsLogical(<lItalic>), <lItalic>, HB_IsNil(<lItalic>) ) ,;
<.underline.> .AND. iif( HB_IsLogical(<lUnderline>), <lUnderline>, HB_IsNil(<lUnderline>) ) ,;
<.strikeout.> .AND. iif( HB_IsLogical(<lStrikeout>), <lStrikeout>, HB_IsNil(<lStrikeout>) ) ,;
<.lcolor.> , <.lfont.> , <.lsize.> , <"align">, <.wrap.>, <"fit">, <xVariable> )
Step 2:
Insert this code into the file h_HMG_HPDF.Prg replacing the function "_HMG_HPDF_MULTILINE_PRINT":
Code: Select all
************************************************************************************
#define bbox_left 1
#define bbox_top 2
#define bbox_right 3
#define bbox_bottom 4
Function _HMG_HPDF_MULTILINE_PRINT ( nRow, nCol, nToRow, nToCol, cFontName, nFontSize, nRColor, nGColor, nBColor, cText, lBold, lItalic, lUnderline, lStrikeout, lColor, lFont, lSize, cAlign, lWrap, cFit, xVariable )
Local nWidth := _HMG_SYSDATA[ 150 ][ 4 ]
Local nHeight := _HMG_SYSDATA[ 150 ][ 5 ]
Local nTextWidth := 0
Local nxPos := _HMG_HPDF_MM2Pixel( nCol )
Local nyPos := nHeight - _HMG_HPDF_MM2Pixel( nRow )
Local nToxPos := nxPos + _HMG_HPDF_MM2Pixel( nToCol - nCol )
Local nToyPos := nyPos - _HMG_HPDF_MM2Pixel( nToRow - nRow )
Local oFont := Nil
Local cFont := ''
Local cTtfFnt // Pablo June, 2014
Local cTextIn, cTextOut, cLine, nLineSpacingPrev, nLineSpacing, nLineCount, nLineLen, bbox, nAreaHeight
default cFontName := ''
//default nFontSize := 12
default nFontSize := _HMG_SYSDATA[ 150 ][ 9 ]
default nRColor := 0
default nGColor := 0
default nBColor := 0
default lBold := .f.
default lItalic := .f.
default lUnderline := .f.
default lStrikeout := .f.
default lColor := .f.
default lFont := .f.
default lSize := .f.
default cAlign := ''
default lWrap := .f.
default cFit := ''
default xVariable := Nil
cTtfFnt := cFontName // Pablo June, 2014
If _HMG_SYSDATA[ 150 ][ 1 ] == Nil // PDF object not found!
_HMG_HPDF_Error( 3 )
Return Nil
endif
If _HMG_SYSDATA[ 150 ][ 7 ] == Nil // PDF Page object not found!
_HMG_HPDF_Error( 5 )
Return Nil
endif
// set color
If lColor
HPDF_Page_SetRGBFill( _HMG_SYSDATA[ 150 ][ 7 ], nRColor/255, nGColor/255, nBColor/255 )
else
HPDF_Page_SetRGBFill( _HMG_SYSDATA[ 150 ][ 7 ], 0.0, 0.0, 0.0 )
endIf
// set font
//If HMG_LEN( alltrim( cFontName ) ) == 0
// cFontName := _HMG_HPDF_SetFont( cFontName, lBold, lItalic )
// oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, _HMG_SYSDATA[ 150 ][ 10 ] )
//else
// cFontName := alltrim( cFontName )
cFontName := AllTrim(_HMG_HPDF_SetFont( cFontName, lBold, lItalic ))
if HMG_UPPER (cFileExt (cFontName)) == '.TTF' // load ttf font
cFont := HPDF_LOADTTFONTFROMFILE( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, .t. )
If HMG_LEN( alltrim( cFont ) ) == 0
_HMG_HPDF_Error( 6 , cFontName )
Return Nil
endif
oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFont, _HMG_SYSDATA[ 150 ][ 10 ] )
else
If HMG_UPPER( alltrim( cFontName ) ) == "SYMBOL" .or. HMG_UPPER( alltrim( cFontName ) ) == "ZAPFDINGBATS"
oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, Nil )
else
oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, _HMG_SYSDATA[ 150 ][ 10 ] )
endIf
endif
//endIf
If oFont == Nil
_HMG_HPDF_Error( 6 , cFontName )
Return Nil
else
nLineSpacingPrev := HPDF_Page_GetTextLeading(_HMG_SYSDATA[ 150 ][ 7 ])
nLineSpacing := nLineSpacingPrev
bbox := HPDF_Font_GetBBox (oFont)
IF nLineSpacing == 0 //set default Line spacing, based on: hpdfpago.c -> HPDF_Page_TextRect
nLineSpacing := ( bbox [bbox_top] - bbox [bbox_bottom] ) / 1000 * nFontSize
ENDIF
DO WHILE .T. //Fitting text
HPDF_Page_SetFontAndSize( _HMG_SYSDATA[ 150 ][ 7 ], oFont, nFontSize )
HPDF_Page_SetTextLeading( _HMG_SYSDATA[ 150 ][ 7 ], nLineSpacing )
cTextIn := cText
cTextOut := ""
nLineCount := 0
DO WHILE !EMPTY(cTextIn)
nLineLen := HPDF_Page_MeasureText( _HMG_SYSDATA[ 150 ][ 7 ], cTextIn, nToxPos - nxPos, !lWrap, NIL )
IF nLineLen = 0
EXIT
ENDIF
nLineCount ++
cLine := Trim( hb_USubStr( cTextIn , 1 , nLineLen ) )
cTextIn := hb_USubStr( cTextIn, nLineLen + 1 )
cTextOut += cLine + IF( !EMPTY( cTextIn ) , " " /* hb_eol() */ , "")
ENDDO
nAreaHeight := ( (nLineCount - 1) * nLineSpacing /* Lines: 1 .. Last-1. The height of each line equals the line spacing. */ ) + ;
( ( bbox [bbox_top] - bbox [bbox_bottom] ) / 1000 * nFontSize /* The height of the last line equals the default line spacing for this font */ ) + .01 /* area higher than calculated (floating point and HPDF_Page_TextRect issue) */
IF !(HMG_UPPER( cFit ) == 'FONTSIZEFIT') /* Don't fit font size */ .OR. ;
nAreaHeight <= nyPos - nToyPos /* Fits */ .OR. ;
nFontSize <= 4 /* Abandon further reducing the font. */
EXIT
ENDIF
nFontSize -= 0.1 //decrease font size
nLineSpacing := ( bbox [bbox_top] - bbox [bbox_bottom] ) / 1000 * nFontSize //decrease line spacing
ENDDO
cText := cTextOut
IF HMG_UPPER( cFit ) == 'HEIGHTFIT'
nToyPos := nyPos - nAreaHeight
ENDIF
xVariable := _HMG_HPDF_Pixel2MM ( nyPos - nToyPos ) + nRow //Return the bottom row "TO nToRow" in mm as a variable by reference.
HPDF_Page_BeginText( _HMG_SYSDATA[ 150 ][ 7 ] )
do case
case HMG_UPPER( cAlign ) == 'CENTER'
HPDF_Page_TextRect( _HMG_SYSDATA[ 150 ][ 7 ], nxPos, nyPos, nToxPos, nToyPos, cText, HPDF_TALIGN_CENTER, Nil )
case HMG_UPPER( cAlign ) == 'RIGHT'
HPDF_Page_TextRect( _HMG_SYSDATA[ 150 ][ 7 ], nxPos, nyPos, nToxPos, nToyPos, cText, HPDF_TALIGN_RIGHT, Nil )
case HMG_UPPER( cAlign ) == 'JUSTIFY'
HPDF_Page_TextRect( _HMG_SYSDATA[ 150 ][ 7 ], nxPos, nyPos, nToxPos, nToyPos, cText, HPDF_TALIGN_JUSTIFY, Nil )
otherwise
HPDF_Page_TextRect( _HMG_SYSDATA[ 150 ][ 7 ], nxPos, nyPos, nToxPos, nToyPos, cText, HPDF_TALIGN_LEFT, Nil )
endcase
HPDF_Page_EndText( _HMG_SYSDATA[ 150 ][ 7 ] )
//restore previous line spacing
HPDF_Page_SetTextLeading(_HMG_SYSDATA[ 150 ][ 7 ], nLineSpacingPrev )
If lDoLog // Pablo June, 2014
HTML_TABLE_LINE(nFHandle,{{StrZero(nRow,4,0)+" ("+StrZero(nyPos,7,2)+")","#FFFFFF",.t.},;
{StrZero(nCol,4,0)+" ("+StrZero(nxPos,7,2)+")","#FFFFFF",.t.},;
{StrZero(_HMG_SYSDATA[ 150 ][ 13 ],3,0),"#FFFFFF",.t.},;
{PadR(If(Empty(cTtfFnt),"<Default>",cTtfFnt),If(HMG_Len(cTtfFnt)>30,HMG_Len(cTtfFnt),30)),"#FFFFFF",.f.},;
{StrZero(nFontSize,2,0),"#FFFFFF",.t.},;
{If(lBold,"BOLD" ,Space(4))+If(lItalic," ITALIC",Space(7)),"#FFFFFF",.f.},;
{cFontName,If(If(Empty(cTtfFnt),HMG_Len(cFontName),HMG_Len(cTtfFnt))==HMG_Len(cFontName),"#FFFFFF","#FFFF66"),.f.}})
Endif
endif
Return Nil
Step 3:
Add this code to the file h_HMG_HPDF.Prg:
Code: Select all
****************************************************************
Function _HMG_HPDF_GetHeight_MULTILINE_PRINT ( cText, nLen, cFontName, nFontSize, lBold, lItalic, lWrap )
Local oFont := Nil
Local cFont := ''
Local nLineSpacing, nLineCount, nLineLen, bbox, nAreaHeight
default cText := ''
default nLen := 0
default cFontName := ''
default nFontSize := _HMG_SYSDATA[ 150 ][ 9 ]
default lBold := .f.
default lItalic := .f.
default lWrap := .f.
nLen := _HMG_HPDF_MM2Pixel ( nLen )
If _HMG_SYSDATA[ 150 ][ 1 ] == Nil // PDF object not found!
_HMG_HPDF_Error( 3 )
Return 0
endif
If _HMG_SYSDATA[ 150 ][ 7 ] == Nil // PDF Page object not found!
_HMG_HPDF_Error( 5 )
Return 0
endif
cFontName := AllTrim(_HMG_HPDF_SetFont( cFontName, lBold, lItalic ))
if HMG_UPPER (cFileExt (cFontName)) == '.TTF' // load ttf font
cFont := HPDF_LOADTTFONTFROMFILE( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, .t. )
If HMG_LEN( alltrim( cFont ) ) == 0
_HMG_HPDF_Error( 6 , cFontName )
Return 0
endif
oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFont, _HMG_SYSDATA[ 150 ][ 10 ] )
else
If HMG_UPPER( alltrim( cFontName ) ) == "SYMBOL" .or. HMG_UPPER( alltrim( cFontName ) ) == "ZAPFDINGBATS"
oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, Nil )
else
oFont := HPDF_GetFont( _HMG_SYSDATA[ 150 ][ 1 ], cFontName, _HMG_SYSDATA[ 150 ][ 10 ] )
endIf
endif
If oFont == Nil
_HMG_HPDF_Error( 6 , cFontName )
Return 0
else
nLineSpacing := HPDF_Page_GetTextLeading(_HMG_SYSDATA[ 150 ][ 7 ])
bbox := HPDF_Font_GetBBox (oFont)
IF nLineSpacing == 0 //set default Linespacing, based on: hpdfpago.c -> HPDF_Page_TextRect
nLineSpacing := ( bbox [bbox_top] - bbox [bbox_bottom] ) / 1000 * nFontSize
ENDIF
HPDF_Page_SetFontAndSize( _HMG_SYSDATA[ 150 ][ 7 ], oFont, nFontSize )
nLineCount := 0
DO WHILE !EMPTY( cText )
nLineLen := HPDF_Page_MeasureText( _HMG_SYSDATA[ 150 ][ 7 ], cText, nLen, !lWrap, NIL )
IF nLineLen = 0
EXIT
ENDIF
nLineCount ++
cText := hb_USubStr( cText, nLineLen + 1 )
ENDDO
Endif
nAreaHeight := ( (nLineCount - 1) * nLineSpacing /* Lines: 1 .. Last-1. The height of each line equals the line spacing */ ) + ;
( ( bbox [bbox_top] - bbox [bbox_bottom] ) / 1000 * nFontSize /* The height of the last line equals the default line spacing for this font */ ) + .01 /* area higher than calculated (floating point and HPDF_Page_TextRect issue) */
Return _HMG_HPDF_Pixel2MM ( nAreaHeight )
***************************************************************************************************
Step 4:
Rebuild HMG with BuildLib32.bat
Sample of new features:
Code: Select all
#include "hmg.ch"
#include "harupdf.ch"
Function Main()
Local cFonte1:= "Helvetica"
Local cEdit := "Whitnesseth that said Grantor, for and in consideration of the sum of ten dollars, and other good and valuable considerations " +;
"to said Grantor in hand paid by said Grantee, the receipt whereof is hereby acknowledged, has remised, released and quit claimed, " +;
"and by these presents does remise, release and quitclaim unto the Grantee all the right, title, interest, claim and demand which " +;
"the said Grantor has in and the following described land situate, lying and being in current state."
Local nHeight, nNextRow, nBottomRow
SELECT HPDFDOC "sample.pdf" PAPERLENGTH 300 PAPERWIDTH 300 LOG
START HPDFDOC
SET HPDFDOC ENCODING TO "WinAnsiEncoding"
START HPDFPAGE
//Adjust the height of the text of the area and get the bottom row using a variable by reference
@ 20, 15 HPDFPRINT cEdit TO 25, 185 FONT cFonte1 size 9 JUSTIFY HEIGHTFIT GETBOTTOM @nBottomRow
@ 20, 15 HPDFPRINT RECTANGLE TO nBottomRow, 185 PENWIDTH 0.1
nNextRow := nBottomRow + 1
//Calculate the height of the text of the area with word wrapping.
nHeight := _HMG_HPDF_GetHeight_MULTILINE_PRINT ( cEdit /* cText */ , 185 - 15 /* nLen ( nToCol - nCol ) */ , cFonte1 /* cFontName */, 9 /* nFontSize */, /* lBold */, /* lItalic */ , .T. /* lWrap */ )
//Print and draw area with calculated height.
@ nNextRow, 15 HPDFPRINT RECTANGLE TO nNextRow + nHeight , 185 PENWIDTH 0.1
@ nNextRow, 15 HPDFPRINT cEdit TO nNextRow + nHeight , 185 FONT cFonte1 size 9 JUSTIFY WRAP
nNextRow += nHeight + 1
//Calculate the height of the text of the area without word wrapping.
nHeight := _HMG_HPDF_GetHeight_MULTILINE_PRINT ( cEdit /* cText */ , 185 - 15 /* nLen ( nToCol - nCol ) */ , cFonte1 /* cFontName */, 10 /* nFontSize */, .T. /* lBold */, /* lItalic */ , .F. /* lWrap */ )
//Print and draw area with calculated height.
@ nNextRow, 15 HPDFPRINT RECTANGLE TO nNextRow + nHeight , 185 PENWIDTH 0.1
@ nNextRow, 15 HPDFPRINT cEdit TO nNextRow + nHeight , 185 FONT cFonte1 size 10 BOLD CENTER
nNextRow += nHeight + 1
//Change line spacing to 6 px
SET HPDFPAGE LINESPACING TO _HMG_HPDF_Pixel2MM( 6 )
//Calculate the text height of the area with the previously set line spacing.
nHeight := _HMG_HPDF_GetHeight_MULTILINE_PRINT ( cEdit /* cText */ , 185 - 15 /* nLen ( nToCol - nCol ) */ , cFonte1 /* cFontName */, 8.5 /* nFontSize */, .T. /* lBold */, .T. /* lItalic */ , .F. /* lWrap */ )
//Print and draw area with calculated height.
@ nNextRow, 15 HPDFPRINT RECTANGLE TO nNextRow + nHeight , 185 PENWIDTH 0.1
@ nNextRow, 15 HPDFPRINT cEdit TO nNextRow + nHeight, 185 FONT cFonte1 size 8.5 BOLD ITALIC RIGHT
//Restore line spacing to defaults
SET HPDFPAGE LINESPACING TO 0
nNextRow += nHeight + 1
//Adjust the height of the text of the area and get the bottom row using a variable by reference
@ nNextRow, 15 HPDFPRINT "Some short text ABC Ó xyz" TO nNextRow, 185 FONT cFonte1 size 8 HEIGHTFIT GETBOTTOM @nBottomRow
@ nNextRow, 15 HPDFPRINT RECTANGLE TO nBottomRow, 185 PENWIDTH 0.1
nNextRow := nBottomRow + 1
//Calculate the text height of the area.
nHeight := _HMG_HPDF_GetHeight_MULTILINE_PRINT ( "Some short text ABC Ó xyz" /* cText */ , 185 - 15 /* nLen ( nToCol - nCol ) */ , cFonte1 /* cFontName */, 8 /* nFontSize */, .T. /* lBold */, /* lItalic */ , /* lWrap */ )
//Print and draw area with calculated height.
@ nNextRow, 15 HPDFPRINT RECTANGLE TO nNextRow + nHeight , 185 PENWIDTH 0.1
@ nNextRow, 15 HPDFPRINT "Some short text ABC Ó xyz" TO nNextRow + nHeight, 185 FONT cFonte1 size 8 BOLD
nNextRow += nHeight + 1
//Adjust the font size of the text to the fixed height of the area and get the bottom row using the variable by reference.
@ nNextRow, 15 HPDFPRINT cEdit TO nNextRow + 12, 185 FONT cFonte1 size 10 FONTSIZEFIT GETBOTTOM @nBottomRow
@ nNextRow, 15 HPDFPRINT RECTANGLE TO nBottomRow , 185 PENWIDTH 0.1
//Print the line of text in the last row.
@ nBottomRow, 15 HPDFPRINT "Last Row := " + AllTrim(Str(nBottomRow)) + " mm" SIZE 6
END HPDFPAGE
END HPDFDOC
Execute File 'sample.pdf'
Return Nil
- sample.png (55.07 KiB) Viewed 3137 times
@Jair Pinho:
You owe me a
big bottle of Cachaça Pirassununga 51.