|
26 | 26 | */ |
27 | 27 |
|
28 | 28 | (function(jsPDFAPI) { |
29 | | - var DrillForContent, FontNameDB, FontStyleMap, FontWeightMap, GetCSS, PurgeWhiteSpace, Renderer, ResolveFont, ResolveUnitedNumber, UnitedNumberMap, elementHandledElsewhere, images, loadImgs, process, tableToJson; |
| 29 | + var clone,DrillForContent, FontNameDB, FontStyleMap, FontWeightMap, GetCSS, PurgeWhiteSpace, Renderer, ResolveFont, ResolveUnitedNumber, UnitedNumberMap, elementHandledElsewhere, images, loadImgs, process, tableToJson; |
| 30 | + clone = (function(){ |
| 31 | + return function (obj) { Clone.prototype=obj; return new Clone() }; |
| 32 | + function Clone(){} |
| 33 | + }()); |
30 | 34 | PurgeWhiteSpace = function(array) { |
31 | 35 | var fragment, i, l, lTrimmed, r, rTrimmed, trailingSpace; |
32 | 36 | i = 0; |
|
140 | 144 | tmp = void 0; |
141 | 145 | css["font-family"] = ResolveFont(computedCSSElement("font-family")) || "times"; |
142 | 146 | css["font-style"] = FontStyleMap[computedCSSElement("font-style")] || "normal"; |
| 147 | + css["text-align"] = TextAlignMap[computedCSSElement("text-align")] || "left"; |
143 | 148 | tmp = FontWeightMap[computedCSSElement("font-weight")] || "normal"; |
144 | 149 | if (tmp === "bold") { |
145 | 150 | if (css["font-style"] === "normal") { |
|
399 | 404 | } |
400 | 405 | } |
401 | 406 | } |
| 407 | + |
| 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')) { |
| 410 | + for(var i = 0; i < lines.length; ++i) { |
| 411 | + var length = this.pdf.getStringUnitWidth(lines[i][0][0], fragmentSpecificMetrics) * fragmentSpecificMetrics.fontSize / k; |
| 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 | + if (i > 0) { |
| 414 | + lines[i][0][1] = clone(lines[i][0][1]); |
| 415 | + } |
| 416 | + var space = (maxLineLength-length); |
| 417 | + console.log(lines[i][0][0]+" with space: "+space) |
| 418 | + if (style['text-align'] === 'right') { |
| 419 | + lines[i][0][1]['margin-left'] = space; |
| 420 | + //if alignment is not right, it has to be center so split the space to the left and the right |
| 421 | + } else { |
| 422 | + lines[i][0][1]['margin-left'] = space/2; |
| 423 | + } |
| 424 | + }; |
| 425 | + } |
| 426 | + |
402 | 427 | return lines; |
403 | | - }; |
| 428 | + }; |
404 | 429 | Renderer.prototype.RenderTextFragment = function(text, style) { |
405 | 430 | var defaultFontSize, font; |
406 | 431 | if (this.pdf.internal.pageSize.height - this.pdf.margins_doc.bottom < this.y + this.pdf.internal.getFontSize()) { |
|
411 | 436 | } |
412 | 437 | defaultFontSize = 12; |
413 | 438 | font = this.pdf.internal.getFont(style["font-family"], style["font-style"]); |
414 | | - return this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj"); |
| 439 | + return this.pdf.internal.write("/" + font.id, (defaultFontSize * style["font-size"]).toFixed(2), "Tf", "(" + this.pdf.internal.pdfEscape(text) + ") Tj");; |
415 | 440 | }; |
416 | 441 | Renderer.prototype.renderParagraph = function() { |
417 | 442 | var blockstyle, defaultFontSize, fontToUnitRatio, fragments, i, l, line, lines, maxLineHeight, out, paragraphspacing_after, paragraphspacing_before, priorblockstype, styles; |
|
440 | 465 | l = void 0; |
441 | 466 | this.y += paragraphspacing_before; |
442 | 467 | out("q", "BT", this.pdf.internal.getCoordinateString(this.x), this.pdf.internal.getVerticalCoordinateString(this.y), "Td"); |
443 | | - while (lines.length) { |
| 468 | + |
| 469 | + //stores the current indent of cursor position |
| 470 | + var currentIndent = 0; |
| 471 | + |
| 472 | + while (lines.length) { |
444 | 473 | line = lines.shift(); |
445 | 474 | maxLineHeight = 0; |
446 | 475 | i = 0; |
|
451 | 480 | } |
452 | 481 | i++; |
453 | 482 | } |
454 | | - out(0, (-1 * defaultFontSize * maxLineHeight).toFixed(2), "Td"); |
| 483 | + //if we have to move the cursor to adapt the indent |
| 484 | + var indentMove = 0; |
| 485 | + //if a margin was added (by e.g. a text-alignment), move the cursor |
| 486 | + if (line[0][1]["margin-left"] !== undefined && line[0][1]["margin-left"] > 0) { |
| 487 | + wantedIndent = this.pdf.internal.getCoordinateString(line[0][1]["margin-left"]); |
| 488 | + indentMove = wantedIndent-currentIndent; |
| 489 | + currentIndent = wantedIndent; |
| 490 | + } |
| 491 | + //move the cursor |
| 492 | + out(indentMove, (-1 * defaultFontSize * maxLineHeight).toFixed(2), "Td"); |
455 | 493 | i = 0; |
456 | 494 | l = line.length; |
457 | 495 | while (i !== l) { |
|
504 | 542 | italic: "italic", |
505 | 543 | oblique: "italic" |
506 | 544 | }; |
| 545 | + TextAlignMap = { |
| 546 | + left: "left", |
| 547 | + right: "right", |
| 548 | + center: "center" |
| 549 | + }; |
507 | 550 | UnitedNumberMap = { |
508 | 551 | normal: 1 |
509 | 552 | /* |
|
0 commit comments