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

Add support for dynamic sizing #1576

Merged
merged 3 commits into from
Jan 12, 2025
Merged

Conversation

hollandjake
Copy link
Contributor

What kind of change does this PR introduce?

  • New Feature:
    • Enable defining sizes using any units (defaulting to Points)
    • This also allows us to define sizes based on the current font context i.e. em's
    • The new public sizeToPoint method allows users to also interact with these sizes to generate the correct point sizes
    • Examples provided in docs

Checklist:

  • Unit Tests
  • Documentation
  • Update CHANGELOG.md
  • Ready to be merged

@blikblum
Copy link
Member

LGTM

@hollandjake
Copy link
Contributor Author

@blikblum let me know if you want this added to all the mixins too?

if (size > 0) return size;
return defaultValue;
}
if (typeof size === 'boolean') return Number(size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What situation a boolean value makes sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its mainly for a falsy value of saying margin: false to indicate that you don't want a margin to render at all

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true would return 1 -> does not make sense

I am not sure how useful would be.

margin: 0 seems enough to not have a margin

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we are totally happy with not having margin: false i'm happy removing it, I only added it so devs could make it explicitly clear they don't want a margin, rather than confusingly saying they want a margin of width 0

@blikblum
Copy link
Member

What kind of change does this PR introduce?

It also introduce new ways to define margin e.g. with an array of number

@hollandjake
Copy link
Contributor Author

What kind of change does this PR introduce?

It also introduce new ways to define margin e.g. with an array of number

Good point, it mainly aims to unify all the units across the package for consistent definitions

Copy link
Member

@blikblum blikblum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep my two considerations.

The first is difference how margin is defined in an array different from pdfmake. In favor of your implementation is that is aligned with css spec. I'd like to know what other devs think

The second is a NIT the boolean handling in size to point. While it does not make sense, it does not hurt

* Side definitions
* - To define all sides, use a single value
* - To define up-down left-right, use a `[Y, X]` array
* - To define each side, use `[top, right, bottom, left]` array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is different how pdfmake defines: https://pdfmake.github.io/docs/0.1/document-definition-object/margins/

It can cause confusion

To be fair, the way you defined is compatible with the CSS order: /* top | right | bottom | left */ -> https://developer.mozilla.org/en-US/docs/Web/CSS/margin

@liborm85

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My goal was always to make it more like CSS than pdfmake. Im not quite sure the reason why pdfmake didnt do the same

Copy link
Collaborator

@liborm85 liborm85 Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why the parameters are defined in this order in pdfmake, but it would be better to use the CSS order in pdfkit.

if (size > 0) return size;
return defaultValue;
}
if (typeof size === 'boolean') return Number(size);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true would return 1 -> does not make sense

I am not sure how useful would be.

margin: 0 seems enough to not have a margin

@blikblum
Copy link
Member

@hollandjake the tests are failing

@hollandjake
Copy link
Contributor Author

hollandjake commented Jan 10, 2025

@hollandjake the tests are failing

@blikblum will investigate now, most likely a regression from merging in main

- Enable defining sizes using any units (defaulting to Points)
- This also allows us to define sizes based on the current font context i.e. em's
- The new public `sizeToPoint` method allows users to also interact with these sizes to generate the correct point sizes
lib/utils.js Outdated
Comment on lines 86 to 91
!(
"top" in sides ||
"right" in sides ||
"bottom" in sides ||
"left" in sides
)
Copy link
Member

@blikblum blikblum Jan 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe {top: 1, right: 1} should get invalid value: {top: 1, right: 1, left: {top: 1, right: 1}, bottom: {top: 1, right: 1}}

Or do i miss something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this check at all assuming is undefined is treated as 0. If i do {top: 1, right: 1} i expect left and bottom be treated as 0

Copy link
Contributor Author

@hollandjake hollandjake Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea is to allow partials, so if anyone provides at least one of the keys it will be used and the others set to undefined, however if none of the keys match the object itself will be treated as the value for all keys.

e.g. {top: 1} would produce {top: 1, right: undefined, bottom: undefined, left: undefined}

but {hello: "world"} would produce {top: {hello: "world"}, right: {hello: "world"}, bottom: {hello: "world"}, left: {hello: "world"}}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other option we could take is to just say if its an object and you didnt provide the keys then it'll be undefined

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thinking about it now, it really doesnt make any sense to provide this, its the users fault

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blikblum please see new changes which removes this logic

@blikblum blikblum merged commit 36549b3 into foliojs:master Jan 12, 2025
3 checks passed
@hollandjake hollandjake deleted the dynamic-sizing branch January 13, 2025 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants