Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert inches to pt #193

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ function htmlToPdfMake(htmlText, options) {
*/
this.parseColor = function(color) {
// e.g. `#fff` or `#ff0048`
var haxRegex = new RegExp('^#([0-9a-f]{3}|[0-9a-f]{6})$');
var haxRegex = new RegExp('^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$');

// e.g. rgb(0,255,34) or rgb(22, 0, 0) or rgb(100%, 100%, 100%) or rgba(0,125,250,0.8)
var rgbRegex = /^rgba?\(\s*(\d+(\.\d+)?%?),\s*(\d+(\.\d+)?%?),\s*(\d+(\.\d+)?%?)(,\s*\d+(\.\d+)?)?\)$/;
Expand All @@ -875,7 +875,7 @@ function htmlToPdfMake(htmlText, options) {
var hslRegex = new RegExp('^hsl\\((\\d+(\\.\\d+)?%?),\\s*(\\d+(\\.\\d+)?%?),\\s*(\\d+(\\.\\d+)?%?)\\)$');

// e.g. "white" or "red"
var nameRegex = new RegExp('^[a-z]+$');
var nameRegex = new RegExp('^[a-zA-Z]+$');

var decimalColors, decimalValue, hexString, ret=[];

Expand Down Expand Up @@ -927,15 +927,15 @@ function htmlToPdfMake(htmlText, options) {
}

/**
* Convert 'px'/'rem'/'cm'/'em' to 'pt', and return false for the other ones. If it's only a number, it will just return it
* Convert 'px'/'rem'/'cm'/'em'/'in' to 'pt', and return false for the other ones. If it's only a number, it will just return it
*
* @param {String} val The value with units (e.g. 12px)
* @return {Number|Boolean} Return the pt value, or false
*/
this.convertToUnit = function(val) {
// if it's just a number, then return it
if (!isNaN(parseFloat(val)) && isFinite(val)) return val*1;
var mtch = (val+"").trim().match(/^(\d*(\.\d+)?)(pt|px|r?em|cm)$/);
var mtch = (val+"").trim().match(/^(\d*(\.\d+)?)(pt|px|r?em|cm|in)$/);
// if we don't have a number with supported units, then return false
if (!mtch) return false;
val = mtch[1];
Expand All @@ -953,6 +953,10 @@ function htmlToPdfMake(htmlText, options) {
val = Math.round(val * 28.34646); // 1cm => 28.34646
break;
}
case 'in':{
val *= 72; // 1in => 72 pt
break;
}
}
return val*1;
}
Expand Down