Open box8 opened 5 years ago
Example PDF: test-spaces.pdf
Trailing spaces are clearly missing.
#include "PageContentContext.h"
#include "PDFPage.h"
#include "PDFUsedFont.h"
#include "PDFWriter.h"
#define FONT_SIZE 30
int main(int argc, char **argv)
{
const char *str[6] = {
" test",
"te st",
"test ",
"\xc2\xa0test",
"te\xc2\xa0st",
"test\xc2\xa0"
};
PDFWriter pdf;
auto font = pdf.GetFontForFile("NotoSerifCJKjp-Regular.otf");
pdf.StartPDF("test-spaces.pdf", ePDFVersionMax);
PDFPage* page = new PDFPage();
page->SetMediaBox(PDFRectangle(0, 0, 595, 842));
auto pcc = pdf.StartPageContentContext(page);
for (int i = 0; i < 6; ++i) {
auto tm = font->CalculateTextDimensions(str[i], FONT_SIZE);
pcc->q();
pcc->cm(1, 0, 0, 1, 50, 800 - 50 * i);
pcc->q();
pcc->K(0, 0.8, 0.8, 0);
pcc->k(0, 0.8, 0.8, 0);
pcc->w(0);
pcc->re(tm.xMin, tm.yMin, tm.width, tm.height);
pcc->B();
pcc->Q();
pcc->BT();
pcc->Tf(font, FONT_SIZE);
pcc->Tm(1, 0, 0, 1, 0, 0);
pcc->Tj(str[i]);
pcc->Tf(font, 12);
pcc->Tj(" ");
pcc->Tj(std::to_string(tm.xMin));
pcc->Tj(" ");
pcc->Tj(std::to_string(tm.width));
pcc->ET();
pcc->Q();
}
pdf.EndPageContentContext(pcc);
pdf.WritePage(page);
delete page;
pdf.EndPDF();
return 0;
}
This is still an issue (with trailing and with leading blanks), as it seems. I'm using a mono font here and release 4.5.6 (as by the release notes there are no relevant changes up to the most recent release). This is the output of my test (string and its width):
size of "x00:00x": 48.948
size of " : ": 43.2
size of "x : x": 48.948
size of " : x": 49.68
size of "x : ": 42.468
The output is produced by the line
BOOST_LOG_TRIVIAL(trace) << "size of \"" << text_[0] << "\": " << font_->CalculateTextDimensions(text_[0],fontSize_).width;
(with boost_logs mgmt data removed manually).
It shows that blanks within the string take the same room as other characters, but leading or trailing blanks being handled differently. Is there a solution or a work-around to get correct numbers?
test.cpp
result: