Issue with PdfString created from const char* and unicode fonts.




In PdfPainter, DrawText and many other methods do accept a PdfString as
input parameter. Do not be lazy, and call such methods with just a const
char pointer, and expect that everything works behind the scene, if you are
using unicode based fonts.



Allways use a correct/explict PdfString constructor. Do not believe the
magic of the automatic conversions.



>From helloworld-base14.cpp:



    // draw sample of all types

      for(i = 0; i < NUM_BASE14_FONTS; ++i)

      {

            x = 56; y = y - 25;

            strcpy((char*)text, (const char*)demo_text);

            strcat((char*)text, GetBase14FontName(i));



            PdfFont *pFont;

            if (i < NUM_BASE14_FONTS - 3) {

                pFont = document.CreateFont( GetBase14FontName(i) );

            }

            else {

                pFont = document.CreateFontSubset(GetBase14FontName(i),

                  false, false, false, new PdfIdentityEncoding(0, 0xffff,
true));

            }



            pFont->SetFontSize( 12.0 );

            painter.SetFont( pFont );



            /* FIXED: Use desired PdfString constructor */

            PdfString pdfText(reinterpret_cast<const pdf_utf8*>(text));

            width  = pFont->GetFontMetrics()->StringWidth(pdfText);

            height = pFont->GetFontMetrics()->GetLineSpacing();



            std::cout <<  GetBase14FontName(i) << " Width = " << width << "
Height = " << height << std::endl;



            // draw red box

            DrawRedFrame( painter, x, y, width, height);



            // draw text

            painter.DrawText( x, y , pdfText);

      }





Lession learned, if you do font subsetting, then be carefull how you
prepare  input, or you can get unexpected results.



With font subsetting for Arial, Times New Roman, and Verdana fonts, you can
reduce the size of helloworld-base14 output to 54 kB.







Wrong way:



      pdfPainter.DrawText(x, y, "wrong way of drawing, as pdf string is not
marked as IsUnicode()");



Better ways:



      pdfPainter.DrawText(x, y, PdfString("this actually will
work").ToUnicode());

      pdfPainter.DrawText(x, y, reinterpret_cast<const pdf_utf8*>("uggly
but it works"));



Preferred:



      pdfPainter.DrawText(x, y, PdfString(L"this is already wchar_t"));



      const pdf_utf8 utf8Text = "a some utf8 encoded text");

      pdfPainter.DrawText(x, y, utf8Text);
------------------------------------------------------------------------------
_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to