yWorks / jsPDF

JavaScript PDF generation to work with SVG
MIT License
56 stars 23 forks source link

Add Support for getLineHeightFactor() and setLineHeightFactor() functions #22

Open Fadiabb opened 5 years ago

Fadiabb commented 5 years ago

Hallo, The following MrRio/jsPDF functions are not supported in yWork-jspdf Fork : getLineHeightFactor() : http://raw.githack.com/MrRio/jsPDF/master/docs/jsPDF.html#setLineHeightFactor setLineDash() : https://github.com/MrRio/jsPDF/blob/master/examples/js/lines.js#LC36

trying to use them :

<html>
<script src="../node_modules/jspdf-yworks/dist/jspdf.min.js"></script>
<body>
<script>
 const pdf = new jsPDF("l", "mm", "a3");
   pdf.setLineDash();
   pdf.getLineHeightFactor();
</script>
</body>
</html>

throws Errors: Uncaught TypeError: pdf.getLineHeightFactor is not a function Uncaught TypeError: pdf.setLineDash is not a function

Is the support of these function planned ?

yGuy commented 5 years ago

These functions have been added only recently and we will have to merge with the master to get these. What's the use-case for these functions?

Fadiabb commented 5 years ago

setLineDash() : we use it to set the dash pattern for upcoming lines (line,circle,......) getLineHeighFactor(): we use it in a function to calculate the text height of many lines text

yGuy commented 5 years ago

The next time we merge with master (if this is still feasible), the functions will be added. There is currently no roadmap for this, but it will happen sooner or later.

bernhardreiter commented 5 years ago

Here is a patch to add setLineDash() against 3561de8fa953a48e98a08255bb97c9d8a9028eff (current master) which works for me:

diff --git a/jspdf.js b/jspdf.js
index 9a8cc01..7175b77 100644
--- a/jspdf.js
+++ b/jspdf.js
@@ -3942,6 +3942,33 @@ var jsPDF = (function(global) {
     };

     /**
+     * Sets the dash pattern for upcoming lines.
+     *
+     * To reset the settings simply call the method without any parameters.
+     * @param {array} dashArray The pattern of the line, expects numbers.
+     * @param {number} dashPhase The phase at which the dash pattern starts.
+     * @function
+     * @instance
+     * @returns {jsPDF}
+     * @memberOf jsPDF
+     * @name setLineDash
+     */
+    API.setLineDash = function(dashArray, dashPhase) {
+      dashArray = dashArray || [];
+      dashPhase = dashPhase || 0;
+
+      if (isNaN(dashPhase) || !Array.isArray(dashArray)) {
+        throw new Error('Invalid arguments passed to setLineDash.');
+      }
+
+      dashArray = dashArray.map(function (x) {return (x * k).toFixed(3)}).join(' ');
+      dashPhase = parseFloat((dashPhase * k).toFixed(3));
+
+      out('[' + dashArray + '] ' + dashPhase + ' d');
+      return this;
+    };
+
+    /**
      * Sets the stroke color for upcoming elements.
      *
      * Depending on the number of arguments given, Gray, RGB, or CMYK

Shall I prepare a pull request? (I've not included the test cases, because I haven't checked what the new place for them would be. I did slight change the Error message to end with a fullstop and to remove the jsPDF. part.)

HackbrettXXX commented 5 years ago

There is already a setLineDashPattern method. We can probably just delegate to it. Also, in "advanced" API mode we should not scale the values by k.

bernhardreiter commented 5 years ago

The upcoming jsPDF with https://github.com/MrRio/jsPDF/commit/849ce007 now already accepts setLineDashPattern() as part of getting more compatible, so implementing setLineDash is not necessary anymore.

bernhardreiter commented 5 years ago

Thanks for the hint about setLineDashPattern, I've created a new issue #23. So remaining here (in this issue) is the support for settting and getting the LineHeightFactor.

@Fadiabb please edit the title to Support getLineHeightFactor() and setLineHeightFactor() next time you are looking into this.