Browse files

adding test for metrics-based string chopper

  • Loading branch information...
1 parent c2f56bd commit ac902c6fd0deb765486d37980463ecd6d83c848d Daniel Dotsenko committed Jun 29, 2012
Showing with 77 additions and 1 deletion.
  1. +54 −0 test/015_splittext.js
  2. BIN test/015_splittext.pdf
  3. +20 −0 test/pdf_generate_tests.js
  4. +3 −1 test/test.html
View
54 test/015_splittext.js
@@ -0,0 +1,54 @@
+;(function() {
+
+ var margin = 0.5
+
+ var 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. Proin ut placerat lectus. Duis vitae felis eget eros feugiat congue. Donec tempus, arcu a consectetur mollis, magna augue consequat augue, vel blandit eros ligula non tellus.'
+
+ var drawMargins = function(pdf){
+ pdf.setDrawColor(0, 0, 255)
+ .setLineWidth(1/72)
+ .line(margin, margin, margin, 11 - margin)
+ .line(8.5 - margin, margin, 8.5-margin, 11-margin)
+ return pdf
+ }
+
+ var test1 = function(jsPDF){
+ var pdf = new jsPDF('p','in','letter')
+ , largetext_size = 16
+ , smalltext_size = 12
+ //splitStr picks up current font, size
+ , largetext = pdf.setFontSize(largetext_size).splitTextToSize(loremipsum, 7.5)
+ // but allows you to pass in alternative fontName, fontStyle, fontSize
+ // as part of settings Object.
+ , smalltext = pdf.splitTextToSize(loremipsum, 7.5, {'fontSize':smalltext_size})
+
+ drawMargins(pdf)
+
+ pdf.text(0.5, 0.5 + largetext_size / 72, largetext)
+
+ var offset = largetext.length * largetext_size / 72 + 2 * smalltext_size / 72
+
+ pdf.setFontSize(smalltext_size).text(0.5, margin + offset, smalltext)
+
+ offset += smalltext.length * smalltext_size / 72 + 2 * smalltext_size / 72
+
+ // here you see all of the options Object properties populated.
+ smalltext = pdf.splitTextToSize(
+ loremipsum
+ , 7.5
+ , {
+ 'fontSize':smalltext_size
+ , 'fontStyle': 'Italic'
+ , 'fontName': 'Times'
+ }
+ )
+
+ pdf.setFont('Times','Italic').text(0.5, margin + offset, smalltext)
+
+ return pdf
+ }
+
+ define(function(){
+ return test1
+ })
+})()
View
BIN test/015_splittext.pdf
Binary file not shown.
View
20 test/pdf_generate_tests.js
@@ -331,6 +331,26 @@ asyncTest('014_addImage', function() {
})
+// handcrafted tests
+asyncTest('015_splittext', function() {
+
+ //QUnit.stop()
+ require(
+ ['015_splittext', 'text!015_splittext.pdf']
+ ).then(function(runner, shouldbe){
+ QUnit.expect(1)
+
+ var pdf = runner(jsPDF)
+
+ QUnit.equal(
+ removeMinorDiffs( pdf.output() )
+ , removeMinorDiffs( shouldbe )
+ )
+ QUnit.start()
+ //stop()
+ })
+})
+
}) // end of document.ready(
View
4 test/test.html
@@ -14,7 +14,9 @@ <h2 id="qunit-userAgent"></h2>
<script src="../jspdf.js"></script>
<script src="../jspdf.plugin.sillysvgrenderer.js"></script>
<script src="../jspdf.plugin.addimage.js"></script>
-
+ <script src="../jspdf.plugin.standard_fonts_metrics.js"></script>
+ <script src="../jspdf.plugin.split_text_to_size.js"></script>
+
<script src="../examples/js/jquery/jquery-1.7.1.min.js"></script>
<script src="libs/qunit.js"></script>
<script type="text/javascript">

0 comments on commit ac902c6

Please sign in to comment.