|
27 | 27 | */
|
28 | 28 |
|
29 | 29 | (function(jsPDFAPI) {
|
30 |
| - var DrillForContent, FontNameDB, FontStyleMap, FontWeightMap, GetCSS, PurgeWhiteSpace, Renderer, ResolveFont, ResolveUnitedNumber, UnitedNumberMap, elementHandledElsewhere, images, loadImgs, process, tableToJson; |
| 30 | + var clone,DrillForContent, FontNameDB, FontStyleMap, FontWeightMap, GetCSS, PurgeWhiteSpace, Renderer, ResolveFont, ResolveUnitedNumber, UnitedNumberMap, elementHandledElsewhere, images, loadImgs, process, tableToJson; |
| 31 | + clone = (function(){ |
| 32 | + return function (obj) { Clone.prototype=obj; return new Clone() }; |
| 33 | + function Clone(){} |
| 34 | + }()); |
31 | 35 | PurgeWhiteSpace = function(array) {
|
32 | 36 | var fragment, i, l, lTrimmed, r, rTrimmed, trailingSpace;
|
33 | 37 | i = 0;
|
|
151 | 155 | tmp = void 0;
|
152 | 156 | css["font-family"] = ResolveFont(computedCSSElement("font-family")) || "times";
|
153 | 157 | css["font-style"] = FontStyleMap[computedCSSElement("font-style")] || "normal";
|
| 158 | + css["text-align"] = TextAlignMap[computedCSSElement("text-align")] || "left"; |
154 | 159 | tmp = FontWeightMap[computedCSSElement("font-weight")] || "normal";
|
155 | 160 | if (tmp === "bold") {
|
156 | 161 | if (css["font-style"] === "normal") {
|
|
441 | 446 | }
|
442 | 447 | }
|
443 | 448 | }
|
| 449 | + |
| 450 | + //if text alignment was set, set margin/indent of each line |
| 451 | + if (style['text-align'] !== undefined && (style['text-align'] === 'center' || style['text-align'] === 'right' || style['text-align'] === 'justify')) { |
| 452 | + for(var i = 0; i < lines.length; ++i) { |
| 453 | + var length = this.pdf.getStringUnitWidth(lines[i][0][0], fragmentSpecificMetrics) * fragmentSpecificMetrics.fontSize / k; |
| 454 | + //if there is more than on line we have to clone the style object as all lines hold a reference on this object |
| 455 | + if (i > 0) { |
| 456 | + lines[i][0][1] = clone(lines[i][0][1]); |
| 457 | + } |
| 458 | + var space = (maxLineLength-length); |
| 459 | + |
| 460 | + if (style['text-align'] === 'right') { |
| 461 | + lines[i][0][1]['margin-left'] = space; |
| 462 | + //if alignment is not right, it has to be center so split the space to the left and the right |
| 463 | + } else if (style['text-align'] === 'center') { |
| 464 | + lines[i][0][1]['margin-left'] = space/2; |
| 465 | + //if justify was set, calculate the word spacing and define in by using the css property |
| 466 | + } else if (style['text-align'] === 'justify') { |
| 467 | + var countSpaces = lines[i][0][0].split(' ').length-1; |
| 468 | + lines[i][0][1]['word-spacing'] = space/countSpaces; |
| 469 | + //ignore the last line in justify mode |
| 470 | + if (i === (lines.length-1)) { |
| 471 | + lines[i][0][1]['word-spacing'] = 0; |
| 472 | + } |
| 473 | + } |
| 474 | + } |
| 475 | + } |
| 476 | + |
444 | 477 | return lines;
|
445 |
| - }; |
| 478 | + }; |
446 | 479 | Renderer.prototype.RenderTextFragment = function(text, style) {
|
447 | 480 | var defaultFontSize, font;
|
448 | 481 | if (this.pdf.internal.pageSize.height - this.pdf.margins_doc.bottom < this.y + this.pdf.internal.getFontSize()) {
|
|
453 | 486 | }
|
454 | 487 | defaultFontSize = 12;
|
455 | 488 | font = this.pdf.internal.getFont(style["font-family"], style["font-style"]);
|
456 |
| - return this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj"); |
| 489 | + |
| 490 | + //set the word spacing for e.g. justify style |
| 491 | + if (style['word-spacing'] !== undefined && style['word-spacing'] > 0) { |
| 492 | + this.pdf.internal.write(style['word-spacing'].toFixed(2), "Tw"); |
| 493 | + } |
| 494 | + |
| 495 | + this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj"); |
| 496 | + |
| 497 | + //set the word spacing back to neutral => 0 |
| 498 | + if (style['word-spacing'] !== undefined) { |
| 499 | + this.pdf.internal.write(0, "Tw"); |
| 500 | + } |
| 501 | + |
457 | 502 | };
|
458 | 503 | Renderer.prototype.renderParagraph = function(cb) {
|
459 | 504 | var blockstyle, defaultFontSize, fontToUnitRatio, fragments, i, l, line, lines, maxLineHeight, out, paragraphspacing_after, paragraphspacing_before, priorblockstype, styles, fontSize;
|
|
482 | 527 | l = void 0;
|
483 | 528 | this.y += paragraphspacing_before;
|
484 | 529 | out("q", "BT", this.pdf.internal.getCoordinateString(this.x), this.pdf.internal.getVerticalCoordinateString(this.y), "Td");
|
485 |
| - while (lines.length) { |
| 530 | + |
| 531 | + //stores the current indent of cursor position |
| 532 | + var currentIndent = 0; |
| 533 | + |
| 534 | + while (lines.length) { |
486 | 535 | line = lines.shift();
|
487 | 536 | maxLineHeight = 0;
|
488 | 537 | i = 0;
|
|
494 | 543 | }
|
495 | 544 | i++;
|
496 | 545 | }
|
497 |
| - out(0, (-1 * defaultFontSize * maxLineHeight).toFixed(2), "Td"); |
| 546 | + //if we have to move the cursor to adapt the indent |
| 547 | + var indentMove = 0; |
| 548 | + //if a margin was added (by e.g. a text-alignment), move the cursor |
| 549 | + if (line[0][1]["margin-left"] !== undefined && line[0][1]["margin-left"] > 0) { |
| 550 | + wantedIndent = this.pdf.internal.getCoordinateString(line[0][1]["margin-left"]); |
| 551 | + indentMove = wantedIndent-currentIndent; |
| 552 | + currentIndent = wantedIndent; |
| 553 | + } |
| 554 | + //move the cursor |
| 555 | + out(indentMove, (-1 * defaultFontSize * maxLineHeight).toFixed(2), "Td"); |
498 | 556 | i = 0;
|
499 | 557 | l = line.length;
|
500 | 558 | while (i !== l) {
|
|
550 | 608 | italic: "italic",
|
551 | 609 | oblique: "italic"
|
552 | 610 | };
|
| 611 | + TextAlignMap = { |
| 612 | + left: "left", |
| 613 | + right: "right", |
| 614 | + center: "center", |
| 615 | + justify: "justify" |
| 616 | + }; |
553 | 617 | UnitedNumberMap = {
|
554 | 618 | normal: 1
|
555 | 619 | /*
|
|
0 commit comments