Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strange behavior of CalculateTextDimensions with trailing spaces #151

Open
box8 opened this issue Jan 7, 2019 · 2 comments
Open

Strange behavior of CalculateTextDimensions with trailing spaces #151

box8 opened this issue Jan 7, 2019 · 2 comments

Comments

@box8
Copy link

box8 commented Jan 7, 2019

test.cpp

#include <iostream>
#include "PDFUsedFont.h"
#include "PDFWriter.h"

int main(int argc, char **argv) {
	PDFWriter pdfWriter;
	PDFUsedFont* font = pdfWriter.GetFontForFile("NotoSerifCJKjp-Regular.otf");

	// SPACE
	PDFUsedFont::TextMeasures tm1 = font->CalculateTextDimensions(" test", 11);
	std::cout << tm1.width  << std::endl;
	PDFUsedFont::TextMeasures tm2 = font->CalculateTextDimensions("te st", 11);
	std::cout << tm2.width  << std::endl;
	PDFUsedFont::TextMeasures tm3 = font->CalculateTextDimensions("test ", 11);
	std::cout << tm3.width  << std::endl;

	// NO-BREAK SPACE
	PDFUsedFont::TextMeasures tm4 = font->CalculateTextDimensions("\xc2\xa0test", 11);
	std::cout << tm4.width  << std::endl;
	PDFUsedFont::TextMeasures tm5 = font->CalculateTextDimensions("te\xc2\xa0st", 11);
	std::cout << tm5.width  << std::endl;
	PDFUsedFont::TextMeasures tm6 = font->CalculateTextDimensions("test\xc2\xa0", 11);
	std::cout << tm6.width  << std::endl;

	return 0;
}

result:

21.747
21.626
18.876
21.747
21.626
18.876
@box8
Copy link
Author

box8 commented Jan 9, 2019

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;
}

@box8 box8 changed the title Strange behavior of CalculateTextDimensions with leading/trailing spaces Strange behavior of CalculateTextDimensions with trailing spaces Jan 9, 2019
@ka-ba
Copy link

ka-ba commented Sep 14, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants