Skip to content

Commit

Permalink
Default painter text to pdf_utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
corymickelson committed Aug 21, 2018
1 parent ec05303 commit 3bc684f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
14 changes: 4 additions & 10 deletions spec/unit/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,6 @@ tap('IDocument password protected', (t: Test) => {
* @todo: Append only works with Document + Document, fix to support StreamDocument as well
*/
tap('IDocument append doc', (t: Test) => {
// let doc = new npdf.StreamDocument('/tmp/append.test.pdf')
// let page = doc.createPage(new npdf.Rect(0, 0, 612, 792))
// let painter = new npdf.Painter(doc)
// let font = doc.createFont({fontName: 'Courier'})
// painter.setPage(page)
// painter.font = font
// painter.drawTextAligned({x:365, y: 690, width: 40}, 'Testing', NPDFAlignment.Center)
// painter.finishPage()
let doc = new npdf.Document()
doc.load(filePath, e => {
if (e) t.fail(e.message)
Expand Down Expand Up @@ -223,12 +215,14 @@ tap('IDocument Destinations, Outline', t => {
tap('StreamDocument write to buffer', t => {
const doc = new npdf.StreamDocument()
const page = doc.createPage(new npdf.Rect(0,0,612, 792))
const font = doc.createFont({fontName: 'monospace', embed: true})
const font = doc.createFont({fontName: 'Carlito', embed: true})
font.size = 12
const painter = new npdf.Painter(doc)
painter.setPage(page)
painter.font = font
painter.setColor([0,0,0])
painter.drawText({x: 150, y: 400}, 'TESTING')
t.comment('utf8 test. To verify print test output to disk and open file to verify test is correct')
painter.drawText({x: 150, y: 400}, 'Unicode Umlauts: ÄÖÜß')
painter.finishPage()
const output = doc.close()
t.assert(Buffer.isBuffer(output))
Expand Down
3 changes: 2 additions & 1 deletion src/doc/BaseDocument.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ BaseDocument::BaseDocument(const Napi::CallbackInfo& info, bool inMem)
}
}
if (info.Length() > 0 && info[0].IsString()) {
output = info[0].As<String>().Utf8Value();
base = new PdfStreamedDocument(
info[0].As<String>().Utf8Value().c_str(), version, encrypt, writeMode);
output.c_str(), version, encrypt, writeMode);
} else {
cout << "Streaming to Buffer" << endl;
streamDocRefCountedBuffer = new PdfRefCountedBuffer(2048);
Expand Down
20 changes: 15 additions & 5 deletions src/doc/Painter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ void
Painter::SetPage(const Napi::CallbackInfo& info)
{
if (!info[0].IsObject()) {
throw Napi::Error::New(info.Env(), "Page must be an instance of Page.");
throw Napi::Error::New(info.Env(), "args[0] must be an instance of Page.");
}
if (info[0].As<Object>().InstanceOf(Page::constructor.Value())) {
auto canvas = Page::Unwrap(info[0].As<Object>());
Expand Down Expand Up @@ -252,7 +252,8 @@ Painter::DrawText(const CallbackInfo& info)
x = d.Get("x").As<Number>();
y = d.Get("y").As<Number>();
try {
painter->DrawText(x, y, text.c_str());
painter->DrawText(
x, y, PdfString(reinterpret_cast<const pdf_utf8*>(text.c_str())));
} catch (PdfError& err) {
ErrorHandler(err, info);
}
Expand Down Expand Up @@ -280,7 +281,10 @@ Painter::DrawMultiLineText(const CallbackInfo& info)
}
try {
painter->DrawMultiLineText(
rect, PdfString(text), alignment, verticalAlignment);
rect,
PdfString(reinterpret_cast<const pdf_utf8*>(text.c_str())),
alignment,
verticalAlignment);
} catch (PdfError& err) {
ErrorHandler(err, info);
}
Expand Down Expand Up @@ -717,7 +721,12 @@ Painter::DrawTextAligned(const CallbackInfo& info)
y = o.Get("y").As<Number>();
width = o.Get("width").As<Number>();
try {
painter->DrawTextAligned(x, y, width, PdfString(text), alignment);
painter->DrawTextAligned(
x,
y,
width,
PdfString(reinterpret_cast<const pdf_utf8*>(text.c_str())),
alignment);
} catch (PdfError& err) {
ErrorHandler(err, info);
}
Expand Down Expand Up @@ -768,7 +777,8 @@ void
Painter::AddText(const CallbackInfo& info)
{
try {
painter->AddText(PdfString(info[0].As<String>().Utf8Value()));
painter->AddText(PdfString(reinterpret_cast<const pdf_utf8*>(
info[0].As<String>().Utf8Value().c_str())));
} catch (PdfError& err) {
ErrorHandler(err, info);
}
Expand Down

0 comments on commit 3bc684f

Please sign in to comment.