-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,105 @@ export function PDFNumber(n) { | |
// @see ISO 32000-1 Annex C.2 (real numbers) | ||
return Math.fround(n); | ||
} | ||
|
||
/** | ||
* Measurement of size | ||
* | ||
* @typedef {number | `${number}` | `${number}${'em' | 'in' | 'px' | 'cm' | 'mm' | 'pc' | 'ex' | 'ch' | 'rem' | 'vw' | 'vmin' | 'vmax' | '%' | 'pt'}`} Size | ||
*/ | ||
|
||
/** | ||
* Measurement of how wide something is, false means 0 and true means 1 | ||
* | ||
* @typedef {Size | boolean} Wideness | ||
*/ | ||
|
||
/** | ||
* 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
* - Or `{vertical: SideValue, horizontal: SideValue}` | ||
* - Or `{top: SideValue, right: SideValue, bottom: SideValue, left: SideValue}` | ||
* | ||
* @template T | ||
* @typedef {T| [T, T]| [T, T, T, T]| { vertical: T; horizontal: T }| { top: T; right: T; bottom: T; left: T }} SideDefinition<T> | ||
**/ | ||
|
||
/** | ||
* @template T | ||
* @typedef {{ top: T; right: T; bottom: T; left: T }} ExpandedSideDefinition<T> | ||
*/ | ||
|
||
/** | ||
* Convert any side definition into a static structure | ||
* | ||
* @template S | ||
* @template D | ||
* @template O | ||
* @template {S | D} T | ||
* @param {SideDefinition<S>} sides - The sides to convert | ||
* @param {SideDefinition<D>} defaultDefinition - The value to use when no definition is provided | ||
* @param {function(T): O} transformer - The transformation to apply to the sides once normalized | ||
* @returns {ExpandedSideDefinition<O>} | ||
*/ | ||
export function normalizeSides( | ||
sides, | ||
defaultDefinition = undefined, | ||
transformer = (v) => v, | ||
) { | ||
if ( | ||
sides === undefined || | ||
(typeof sides === "object" && Object.keys(sides).length === 0) | ||
hollandjake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) { | ||
sides = defaultDefinition; | ||
} | ||
if (typeof sides !== "object" || sides === null || sides === undefined) { | ||
hollandjake marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sides = [sides, sides, sides, sides]; | ||
} | ||
if (Array.isArray(sides)) { | ||
if (sides.length === 2) { | ||
sides = { vertical: sides[0], horizontal: sides[1] }; | ||
} else { | ||
sides = { | ||
top: sides[0], | ||
right: sides[1], | ||
bottom: sides[2], | ||
left: sides[3], | ||
}; | ||
} | ||
} | ||
|
||
if ("vertical" in sides || "horizontal" in sides) { | ||
sides = { | ||
top: sides.vertical, | ||
right: sides.horizontal, | ||
bottom: sides.vertical, | ||
left: sides.horizontal, | ||
}; | ||
} | ||
|
||
if ( | ||
!( | ||
"top" in sides || | ||
"right" in sides || | ||
"bottom" in sides || | ||
"left" in sides | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. but There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @blikblum please see new changes which removes this logic |
||
) { | ||
sides = { top: sides, right: sides, bottom: sides, left: sides }; | ||
} | ||
|
||
return { | ||
top: transformer(sides.top), | ||
right: transformer(sides.right), | ||
bottom: transformer(sides.bottom), | ||
left: transformer(sides.left), | ||
}; | ||
} | ||
|
||
export const MM_TO_CM = 1 / 10; // 1MM = 1CM | ||
export const CM_TO_IN = 1 / 2.54; // 1CM = 1/2.54 IN | ||
export const PX_TO_IN = 1 / 96; // 1 PX = 1/96 IN | ||
export const IN_TO_PT = 72; // 1 IN = 72 PT | ||
export const PC_TO_PT = 12; // 1 PC = 12 PT |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 allThere was a problem hiding this comment.
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 senseI am not sure how useful would be.
margin: 0 seems enough to not have a margin
There was a problem hiding this comment.
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