Hi,

I tried Sans Serif font on Kubuntu 18.04 and other fonts, not sure they are base14 or no...

Drawing: https://github.com/igormironchik/md-pdf/blob/master/md-pdf/renderer.cpp

static const double c_margin = 25.0;

struct PageMargins {
    double left = c_margin;
    double right = c_margin;
    double top = c_margin;
    double bottom = c_margin;
}; // struct PageMargins

struct CoordsPageAttribs {
    PageMargins margins;
    double pageWidth = 0.0;
    double pageHeight = 0.0;
    double x = 0.0;
    double y = 0.0;
}; // struct CoordsPageAttribs

struct PdfAuxData {
    PdfStreamedDocument * doc = nullptr;
    PdfPainter * painter = nullptr;
    PdfPage * page = nullptr;
    CoordsPageAttribs coords;
}; // struct PdfAuxData;

PdfFont * createFont( const QString & name, bool bold, bool italic, float size,
    PdfStreamedDocument * doc )
{
    auto * font = doc->CreateFont( name.toLocal8Bit().data(), bold, italic , false,
        PdfEncodingFactory::GlobalIdentityEncodingInstance() );

    if( !font )
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle )

    font->SetFontSize( size );

    return font;
}

void createPage( PdfAuxData & pdfData )
{
    pdfData.page = pdfData.doc->CreatePage(
        PdfPage::CreateStandardPageSize( ePdfPageSize_A4 ) );

    if( !pdfData.page )
        PODOFO_RAISE_ERROR( ePdfError_InvalidHandle )

    pdfData.painter->SetPage( pdfData.page );

    pdfData.coords = { { c_margin, c_margin, c_margin, c_margin },
        pdfData.page->GetPageSize().GetWidth(),
        pdfData.page->GetPageSize().GetHeight(),
        c_margin, pdfData.page->GetPageSize().GetHeight() - c_margin };
}

PdfString createPdfString( const QString & text )
{
    return PdfString( reinterpret_cast< pdf_utf8* > ( text.toUtf8().data() ) );
}

QString createQString( const PdfString & str )
{
    return QString::fromUtf8( str.GetStringUtf8().c_str(),
        static_cast< int > ( str.GetCharacterLength() ) );
}

void drawHeading( PdfAuxData & pdfData, const RenderOpts & renderOpts,
    MD::Heading * item, QSharedPointer< MD::Document > doc )
{
    PdfFont * font = createFont( renderOpts.m_textFont.toLocal8Bit().data(),         true, false, renderOpts.m_textFontSize + 16 - ( item->level() < 7 ? item->level() * 2 : 12 ),
        pdfData.doc );

    pdfData.painter->SetFont( font );
    pdfData.painter->SetColor( 0.0, 0.0, 0.0 );

    const double width = pdfData.coords.pageWidth - pdfData.coords.margins.left -
        pdfData.coords.margins.right;

    const auto lines = pdfData.painter->GetMultiLineTextAsLines(
        width, createPdfString( item->text() ) );

    const double height = lines.size() * font->GetFontMetrics()->GetLineSpacing();     const double availableHeight = pdfData.coords.pageHeight - pdfData.coords.margins.top -
        pdfData.coords.margins.bottom;

    if( pdfData.coords.y - height > pdfData.coords.margins.bottom )
    {
        pdfData.painter->DrawMultiLineText( pdfData.coords.margins.left,
            pdfData.coords.y - height,
            width, height, createPdfString( item->text() ) );

        pdfData.coords.y -= height;
    }
    else if( height <= availableHeight )
    {
        pdfData.painter->FinishPage();
        createPage( pdfData );
        drawHeading( pdfData, renderOpts, item, doc );
    }
    else
    {
        std::vector< PdfString > tmp;
        double h = 0.0;
        std::size_t i = 0;
        double available = pdfData.coords.pageHeight - pdfData.coords.margins.top -
            pdfData.coords.margins.bottom;
        const double spacing = font->GetFontMetrics()->GetLineSpacing();

        while( available >= spacing )
        {
            tmp.push_back( lines.at( i ) );
            h += spacing;
            ++i;
            available -= spacing;
        }

        QString text;

        for( const auto & s : tmp )
            text.append( createQString( s ) );

        QString toSave = item->text();
        toSave.remove( text );

        item->setText( toSave.simplified() );

        pdfData.painter->DrawMultiLineText( pdfData.coords.margins.left,
            pdfData.coords.y - h,
            width, h, createPdfString( text ) );

        pdfData.coords.y -= height;

        pdfData.painter->FinishPage();

        createPage( pdfData );

        drawHeading( pdfData, renderOpts, item, doc );
    }
}

On 06.12.2019 17:25, Michal Sudolsky wrote:
Hi,

You did not send enough details. I can only guess what font name you used and how you draw string but when I tested your text it shows in pdf without spaces. Btw base14 fonts cannot be used with identity encoding.

On Fri, Dec 6, 2019 at 1:29 PM Igor Mironchik <igor.mironc...@gmail.com <mailto:igor.mironc...@gmail.com>> wrote:

    Hello.

    I try to create PDF file with PoDoFo library. I want to draw
    multi-language text in my PDF file.

    For this I create PdfFont with:

    PdfFont * createFont( const QString & name, bool bold, bool italic,
    float size,
         PdfStreamedDocument * doc )
    {
         auto * font = doc->CreateFont( name.toLocal8Bit().data(), bold,
    italic , false,
    PdfEncodingFactory::GlobalIdentityEncodingInstance() );

         if( !font )
             PODOFO_RAISE_ERROR( ePdfError_InvalidHandle )

         font->SetFontSize( size );

         return font;
    }

    It works. I see English and Russian text in output PDF, but...

    Assume I have text: "The book Книга"

    It writes to PDF but in the output all characters are separated with
    space, like:

    "T h e  b o o k  К н и г а"

    How can I solve this issue?

    Thank you.



    _______________________________________________
    Podofo-users mailing list
    Podofo-users@lists.sourceforge.net
    <mailto:Podofo-users@lists.sourceforge.net>
    https://lists.sourceforge.net/lists/listinfo/podofo-users

_______________________________________________
Podofo-users mailing list
Podofo-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/podofo-users

Reply via email to