|
406 | 406 | }
|
407 | 407 |
|
408 | 408 | //if text alignment was set, set margin/indent of each line
|
409 |
| - if (style['text-align'] !== undefined && (style['text-align'] === 'center' || style['text-align'] === 'right')) { |
| 409 | + if (style['text-align'] !== undefined && (style['text-align'] === 'center' || style['text-align'] === 'right' || style['text-align'] === 'justify')) { |
410 | 410 | for(var i = 0; i < lines.length; ++i) {
|
411 | 411 | var length = this.pdf.getStringUnitWidth(lines[i][0][0], fragmentSpecificMetrics) * fragmentSpecificMetrics.fontSize / k;
|
412 | 412 | //if there is more than on line we have to clone the style object as all lines hold a reference on this object
|
413 | 413 | if (i > 0) {
|
414 | 414 | lines[i][0][1] = clone(lines[i][0][1]);
|
415 | 415 | }
|
416 | 416 | var space = (maxLineLength-length);
|
417 |
| - console.log(lines[i][0][0]+" with space: "+space) |
| 417 | + |
418 | 418 | if (style['text-align'] === 'right') {
|
419 | 419 | lines[i][0][1]['margin-left'] = space;
|
420 | 420 | //if alignment is not right, it has to be center so split the space to the left and the right
|
421 |
| - } else { |
| 421 | + } else if (style['text-align'] === 'center') { |
422 | 422 | lines[i][0][1]['margin-left'] = space/2;
|
| 423 | + //if justify was set, calculate the word spacing and define in by using the css property |
| 424 | + } else if (style['text-align'] === 'justify') { |
| 425 | + var countSpaces = lines[i][0][0].split(' ').length-1; |
| 426 | + lines[i][0][1]['word-spacing'] = space/countSpaces; |
| 427 | + //ignore the last line in justify mode |
| 428 | + if (i === (lines.length-1)) { |
| 429 | + lines[i][0][1]['word-spacing'] = 0; |
| 430 | + } |
423 | 431 | }
|
424 |
| - }; |
| 432 | + } |
425 | 433 | }
|
426 | 434 |
|
427 | 435 | return lines;
|
|
436 | 444 | }
|
437 | 445 | defaultFontSize = 12;
|
438 | 446 | font = this.pdf.internal.getFont(style["font-family"], style["font-style"]);
|
439 |
| - return this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj");; |
| 447 | + |
| 448 | + //set the word spacing for e.g. justify style |
| 449 | + if (style['word-spacing'] !== undefined && style['word-spacing'] > 0) { |
| 450 | + this.pdf.internal.write(style['word-spacing'].toFixed(2), "Tw"); |
| 451 | + } |
| 452 | + |
| 453 | + this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj"); |
| 454 | + |
| 455 | + //set the word spacing back to neutral => 0 |
| 456 | + if (style['word-spacing'] !== undefined) { |
| 457 | + this.pdf.internal.write(0, "Tw"); |
| 458 | + } |
| 459 | + |
440 | 460 | };
|
441 | 461 | Renderer.prototype.renderParagraph = function() {
|
442 | 462 | var blockstyle, defaultFontSize, fontToUnitRatio, fragments, i, l, line, lines, maxLineHeight, out, paragraphspacing_after, paragraphspacing_before, priorblockstype, styles;
|
|
487 | 507 | wantedIndent = this.pdf.internal.getCoordinateString(line[0][1]["margin-left"]);
|
488 | 508 | indentMove = wantedIndent-currentIndent;
|
489 | 509 | currentIndent = wantedIndent;
|
490 |
| - } |
| 510 | + } |
491 | 511 | //move the cursor
|
492 | 512 | out(indentMove, (-1 * defaultFontSize * maxLineHeight).toFixed(2), "Td");
|
493 | 513 | i = 0;
|
|
545 | 565 | TextAlignMap = {
|
546 | 566 | left: "left",
|
547 | 567 | right: "right",
|
548 |
| - center: "center" |
| 568 | + center: "center", |
| 569 | + justify: "justify" |
549 | 570 | };
|
550 | 571 | UnitedNumberMap = {
|
551 | 572 | normal: 1
|
|
0 commit comments