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

Small improvements in CFFCompiler.encodeNumber and CFFCompiler.encodeFloat #12002

Merged
merged 2 commits into from
Jun 15, 2020
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 src/core/cff_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,8 @@ var CFFOffsetTracker = (function CFFOffsetTrackerClosure() {

// Takes a CFF and converts it to the binary representation.
var CFFCompiler = (function CFFCompilerClosure() {
let EncodeFloatRegExp = null; // Lazily initialized by `encodeFloat`.

// eslint-disable-next-line no-shadow
function CFFCompiler(cff) {
this.cff = cff;
Expand Down Expand Up @@ -1483,17 +1485,19 @@ var CFFCompiler = (function CFFCompilerClosure() {
return output.data;
},
encodeNumber: function CFFCompiler_encodeNumber(value) {
if (parseFloat(value) === parseInt(value, 10) && !isNaN(value)) {
// isInt
if (Number.isInteger(value)) {
return this.encodeInteger(value);
}
return this.encodeFloat(value);
},
encodeFloat: function CFFCompiler_encodeFloat(num) {
var value = num.toString();

// rounding inaccurate doubles
var m = /\.(\d*?)(?:9{5,20}|0{5,20})\d{0,2}(?:e(.+)|$)/.exec(value);
if (!EncodeFloatRegExp) {
EncodeFloatRegExp = /\.(\d*?)(?:9{5,20}|0{5,20})\d{0,2}(?:e(.+)|$)/;
}
// Rounding inaccurate doubles.
var m = EncodeFloatRegExp.exec(value);
if (m) {
var epsilon = parseFloat("1e" + ((m[2] ? +m[2] : 0) + m[1].length));
value = (Math.round(num * epsilon) / epsilon).toString();
Expand Down