Permalink
Browse files

Merge pull request #23 from willowsystems/stable

Font metrics. Font encoding (cp1252). Text sizing and splitting
  • Loading branch information...
2 parents 4c51160 + b9f9f86 commit 119e1fca4ad188829f62baad1dbab3e5993745ba @MrRio committed Jul 4, 2012
View
@@ -8,7 +8,8 @@
<script type="text/javascript" src="js/jquery/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery/jquery-ui-1.8.17.custom.min.js"></script>
<script type="text/javascript" src="../jspdf.js"></script>
- <script type="text/javascript" src="../jspdf.plugin.addimage.js"></script>
+ <script type="text/javascript" src="../jspdf.plugin.standard_fonts_metrics.js"></script>
+ <script type="text/javascript" src="../jspdf.plugin.split_text_to_size.js"></script>
<script type="text/javascript" src="js/basic.js"></script>
@@ -168,6 +169,43 @@
// Output as Data URI
doc.output('datauri');</pre>
<a href="javascript:demoTextColors()" class="button">Run Code</a></p></div>
+
+<h2><a href="#">Font-metrics-based line sizing and split</a></h2>
+<div><p><pre>var pdf = new jsPDF('p','in','letter')
+, sizes = [12, 16, 20]
+, fonts = [['Times','Roman'],['Helvetica',''], ['Times','Italic']]
+, font, size, lines
+, verticalOffset = 0.5 // inches on a 8.5 x 11 inch sheet.
+, loremipsum = 'Lorem ipsum dolor sit amet, ...'
+
+for (var i in fonts){
+ if (fonts.hasOwnProperty(i)) {
+ font = fonts[i]
+ size = sizes[i]
+
+ lines = pdf.setFont(font[0], font[1])
+ .setFontSize(size)
+ .splitTextToSize(loremipsum, 7.5)
+ // Don't want to preset font, size to calculate the lines?
+ // .splitTextToSize(text, maxsize, options)
+ // allows you to pass an object with any of the following:
+ // {
+ // 'fontSize': 12
+ // , 'fontStyle': 'Italic'
+ // , 'fontName': 'Times'
+ // }
+ // Without these, .splitTextToSize will use current / default
+ // font Family, Style, Size.
+
+ pdf.text(0.5, verticalOffset + size / 72, lines)
+
+ verticalOffset += (lines.length + 0.5) * size / 72
+ }
+}
+
+pdf.output('dataurl')</pre>
+<a href="javascript:demoStringSplitting()" class="button">Run Code</a></p></div>
+
</div>
</div>
View
@@ -261,3 +261,48 @@ function demoImages() {
getImageFromUrl('thinking-monkey.jpg', createPDF);
}
+
+function demoStringSplitting() {
+
+ var pdf = new jsPDF('p','in','letter')
+ , sizes = [12, 16, 20]
+ , fonts = [['Times','Roman'],['Helvetica',''], ['Times','Italic']]
+ , font, size, lines
+ , margin = 0.5 // inches on a 8.5 x 11 inch sheet.
+ , verticalOffset = margin
+ , loremipsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus id eros turpis. Vivamus tempor urna vitae sapien mollis molestie. Vestibulum in lectus non enim bibendum laoreet at at libero. Etiam malesuada erat sed sem blandit in varius orci porttitor. Sed at sapien urna. Fusce augue ipsum, molestie et adipiscing at, varius quis enim. Morbi sed magna est, vel vestibulum urna. Sed tempor ipsum vel mi pretium at elementum urna tempor. Nulla faucibus consectetur felis, elementum venenatis mi mollis gravida. Aliquam mi ante, accumsan eu tempus vitae, viverra quis justo.\n\nProin feugiat augue in augue rhoncus eu cursus tellus laoreet. Pellentesque eu sapien at diam porttitor venenatis nec vitae velit. Donec ultrices volutpat lectus eget vehicula. Nam eu erat mi, in pulvinar eros. Mauris viverra porta orci, et vehicula lectus sagittis id. Nullam at magna vitae nunc fringilla posuere. Duis volutpat malesuada ornare. Nulla in eros metus. Vivamus a posuere libero.'
+
+ // Margins:
+ pdf.setDrawColor(0, 255, 0)
+ .setLineWidth(1/72)
+ .line(margin, margin, margin, 11 - margin)
+ .line(8.5 - margin, margin, 8.5-margin, 11-margin)
+
+ // the 3 blocks of text
+ for (var i in fonts){
+ if (fonts.hasOwnProperty(i)) {
+ font = fonts[i]
+ size = sizes[i]
+
+ lines = pdf.setFont(font[0], font[1])
+ .setFontSize(size)
+ .splitTextToSize(loremipsum, 7.5)
+ // Don't want to preset font, size to calculate the lines?
+ // .splitTextToSize(text, maxsize, options)
+ // allows you to pass an object with any of the following:
+ // {
+ // 'fontSize': 12
+ // , 'fontStyle': 'Italic'
+ // , 'fontName': 'Times'
+ // }
+ // Without these, .splitTextToSize will use current / default
+ // font Family, Style, Size.
+
+ pdf.text(0.5, verticalOffset + size / 72, lines)
+
+ verticalOffset += (lines.length + 0.5) * size / 72
+ }
+ }
+
+ pdf.output('dataurl')
+}
View

Large diffs are not rendered by default.

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit 119e1fc

Please sign in to comment.