I found the bug and it works perfectly now.
PdfPainter.cpp
std::vector<PdfString> PdfPainter::GetMultiLineTextAsLines( double dWidth,
const PdfString & rsText)
{
PODOFO_RAISE_LOGIC_IF( !m_pCanvas, "Call SetPage() first before doing
drawing operations." );
if( !m_pFont || !m_pPage || !rsText.IsValid() )
{
PODOFO_RAISE_ERROR( ePdfError_InvalidHandle );
}
if( dWidth <= 0.0 ) // nonsense arguments
return std::vector<PdfString>();
if( rsText.GetCharacterLength() == 0 ) // empty string
return std::vector<PdfString>(1, rsText);
// We will work with utf16 encoded string because it allows us
// fast and easy individual characters access
const std::string& stringUtf8 = rsText.GetStringUtf8();
std::vector<pdf_utf16be> stringUtf16(stringUtf8.length() + 1, 0);
assert(stringUtf16.size() > 0);
const pdf_long converted = PdfString::ConvertUTF8toUTF16(
reinterpret_cast<const pdf_utf8*>(stringUtf8.c_str()),
&stringUtf16[0], stringUtf16.size());
//const pdf_long len = rsText.GetCharacterLength();
assert( converted == (rsText.GetCharacterLength() + 1) );
const pdf_utf16be* const stringUtf16Begin = &stringUtf16[0];
const pdf_utf16be* pszLineBegin = stringUtf16Begin;
const pdf_utf16be* pszCurrentCharacter = stringUtf16Begin;
const pdf_utf16be* pszStartOfCurrentWord = stringUtf16Begin;
bool startOfWord = true;
double dCurWidthOfLine = 0.0;
std::vector<PdfString> vecLines;
// do simple word wrapping
while( *pszCurrentCharacter )
{
if( IsNewLineChar( *pszCurrentCharacter ) ) // hard-break!
{
vecLines.push_back( PdfString( pszLineBegin, pszCurrentCharacter -
pszLineBegin ) );
pszLineBegin = pszCurrentCharacter+1;// skip the line feed
startOfWord = true;
dCurWidthOfLine = 0.0;
}
else if( IsSpaceChar( *pszCurrentCharacter ) )
{
if( dCurWidthOfLine > dWidth )
{
// The previous word does not fit in the current line.
// -> Move it to the next one.
vecLines.push_back( PdfString( pszLineBegin,
pszStartOfCurrentWord - pszLineBegin ) );
pszLineBegin = pszStartOfCurrentWord;
if (!startOfWord)
{
dCurWidthOfLine = m_pFont->GetFontMetrics()->StringWidth(
pszStartOfCurrentWord,
pszCurrentCharacter - pszStartOfCurrentWord );
}
else
{
dCurWidthOfLine = 0.0;
}
}
else
{
dCurWidthOfLine += m_pFont->GetFontMetrics()->UnicodeCharWidth(
SwapCharBytesIfRequired( *pszCurrentCharacter ) );
}
startOfWord = true;
}
else
{
if (startOfWord)
{
pszStartOfCurrentWord = pszCurrentCharacter;
startOfWord = false;
}
//else do nothing
if ((dCurWidthOfLine + m_pFont->GetFontMetrics()->UnicodeCharWidth(
SwapCharBytesIfRequired( *pszCurrentCharacter ))) > dWidth)
{
if ( pszLineBegin == pszStartOfCurrentWord )
{
// This word takes up the whole line.
// Put as much as possible on this line.
vecLines.push_back( PdfString( pszLineBegin,
pszCurrentCharacter - pszLineBegin ) );
pszLineBegin = pszCurrentCharacter;
pszStartOfCurrentWord = pszCurrentCharacter;
dCurWidthOfLine =
m_pFont->GetFontMetrics()->UnicodeCharWidth( SwapCharBytesIfRequired(
*pszCurrentCharacter ) );
}
else
{
// The current word does not fit in the current line.
// -> Move it to the next one.
vecLines.push_back( PdfString( pszLineBegin,
pszStartOfCurrentWord - pszLineBegin ) );
pszLineBegin = pszStartOfCurrentWord;
dCurWidthOfLine = m_pFont->GetFontMetrics()->StringWidth(
pszStartOfCurrentWord, (pszCurrentCharacter-pszStartOfCurrentWord) + 1 );
}
}
else
{
dCurWidthOfLine += m_pFont->GetFontMetrics()->UnicodeCharWidth(
SwapCharBytesIfRequired( *pszCurrentCharacter ) );
}
}
++pszCurrentCharacter;
}
if( (pszCurrentCharacter - pszLineBegin) > 0 )
{
if( dCurWidthOfLine > dWidth )
{
// The previous word does not fit in the current line.
// -> Move it to the next one.
vecLines.push_back( PdfString( pszLineBegin, pszStartOfCurrentWord
- pszLineBegin) );
pszLineBegin = pszStartOfCurrentWord;
}
//else do nothing
if( pszCurrentCharacter - pszLineBegin > 0 )
{
vecLines.push_back( PdfString( pszLineBegin, pszCurrentCharacter -
pszLineBegin) );
}
//else do nothing
}
return vecLines;
}
--
Grüße/Regards,
[heubach-media] | Christian Sakowski
[email protected]
iChat/AIM: SakowskiF
Tel: +49/(0)40/52 10 59-23
> Am 25.11.2015 um 13:41 schrieb Christian Sakowski
> <[email protected]>:
>
> The routine „GetMultiLineTextAsLines“ doesn’t recognize empty lines“.
>
> Example:
>
> PoDoFo::PdfString podofostring(L"line1\nline2\nline3\n\nline5");
>
> std::vector<PoDoFo::PdfString>
> vecLines=painter->GetMultiLineTextAsLines(1000.0f,podofostring);
>
> vecLines returns an array of 4 elements instead of 5!
>
> I use version 0.9.3. Is there a fix available?
> --
>
> Grüße/Regards,
> [heubach-media] | Christian Sakowski
> [email protected]
> iChat/AIM: SakowskiF
> Tel: +49/(0)40/52 10 59-23
>
>
>
>
> --
> ACHTUNG NEUE ADRESSE:
> heubach media
> Osterfeldstr. 12-14 | Haus 1 | Eingang Nord
> 22529 Hamburg
> tel: 040 / 52 10 59 - 10 | fax: -99
> mail: [email protected]
> home: www.heubach-media.de
> Geschäftsführer|CEO: Matthias Heubach
>
> Mieten Sie Ihre Computer, iPads & Drucker für Ihre Events bei:
> http://www.milo-rental.com
>
> Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen.
> Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich
> erhalten haben,
> informieren Sie bitte sofort den Absender und vernichten Sie diese Mail.
> Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht
> gestattet.
>
> This e-mail may contain confidential and/or privileged information.
> If you are not the intended recipient (or have received this e-mail in error)
> please notify the sender immediately and destroy this e-mail.
> Any unauthorized copying, disclosure or distribution of the
> material in this e-mail is strictly forbidden.
>
> ------------------------------------------------------------------------------
> Go from Idea to Many App Stores Faster with Intel(R) XDK
> Give your users amazing mobile app experiences with Intel(R) XDK.
> Use one codebase in this all-in-one HTML5 development environment.
> Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
> http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
> _______________________________________________
> Podofo-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/podofo-users
--
ACHTUNG NEUE ADRESSE:
heubach media
Osterfeldstr. 12-14 | Haus 1 | Eingang Nord
22529 Hamburg
tel: 040 / 52 10 59 - 10 | fax: -99
mail: [email protected]
home: www.heubach-media.de
Geschäftsführer|CEO: Matthias Heubach
Mieten Sie Ihre Computer, iPads & Drucker für Ihre Events bei:
http://www.milo-rental.com
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen.
Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten
haben,
informieren Sie bitte sofort den Absender und vernichten Sie diese Mail.
Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht
gestattet.
This e-mail may contain confidential and/or privileged information.
If you are not the intended recipient (or have received this e-mail in error)
please notify the sender immediately and destroy this e-mail.
Any unauthorized copying, disclosure or distribution of the
material in this e-mail is strictly forbidden.
------------------------------------------------------------------------------
Go from Idea to Many App Stores Faster with Intel(R) XDK
Give your users amazing mobile app experiences with Intel(R) XDK.
Use one codebase in this all-in-one HTML5 development environment.
Design, debug & build mobile apps & 2D/3D high-impact games for multiple OSs.
http://pubads.g.doubleclick.net/gampad/clk?id=254741551&iu=/4140
_______________________________________________
Podofo-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/podofo-users