-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix interaction between styles and sizes. #719
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
Changes from 2 commits
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 |
|---|---|---|
|
|
@@ -5,33 +5,43 @@ | |
| * `.reset` functions. | ||
| */ | ||
|
|
||
| const BASESIZE = 6; | ||
|
|
||
| const sizeStyleMap = [ | ||
| // Each element contains [textsize, scriptsize, scriptscriptsize]. | ||
| // The size mappings are taken from TeX with \normalsize=10pt. | ||
| [1, 1, 1], // size1: [5, 5, 5] \tiny | ||
| [2, 1, 1], // size2: [6, 5, 5] | ||
| [3, 1, 1], // size3: [7, 5, 5] \scriptsize | ||
| [4, 2, 1], // size4: [8, 6, 5] \footnotesize | ||
| [5, 2, 1], // size5: [9, 6, 5] \small | ||
| [6, 3, 1], // size6: [10, 7, 5] \normalsize | ||
| [7, 4, 2], // size7: [12, 8, 6] \large | ||
| [8, 6, 3], // size8: [14.4, 10, 7] \Large | ||
| [9, 7, 6], // size9: [17.28, 12, 10] \LARGE | ||
| [10, 8, 7], // size10: [20.74, 14.4, 12] \huge | ||
| [11, 10, 9], // size11: [24.88, 20.74, 17.28] \HUGE | ||
| ]; | ||
|
|
||
| const sizeMultipliers = [ | ||
| 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.2, 1.44, 1.728, 2.074, 2.488, | ||
| ]; | ||
|
|
||
| /** | ||
| * This is the main options class. It contains the style, size, color, and font | ||
| * of the current parse level. It also contains the style and size of the parent | ||
| * parse level, so size changes can be handled efficiently. | ||
| * This is the main options class. It contains the current style, size, color, | ||
| * and font. | ||
| * | ||
| * Each of the `.with*` and `.reset` functions passes its current style and size | ||
| * as the parentStyle and parentSize of the new options class, so parent | ||
| * handling is taken care of automatically. | ||
| * Options objects should not be modified. To create a new Options with | ||
| * different properties, call a `.having*` method. | ||
| */ | ||
| function Options(data) { | ||
| this.style = data.style; | ||
| this.color = data.color; | ||
| this.size = data.size; | ||
| this.size = data.size || BASESIZE; | ||
| this.textSize = data.textSize || this.size; | ||
| this.phantom = data.phantom; | ||
| this.font = data.font; | ||
|
|
||
| if (data.parentStyle === undefined) { | ||
| this.parentStyle = data.style; | ||
| } else { | ||
| this.parentStyle = data.parentStyle; | ||
| } | ||
|
|
||
| if (data.parentSize === undefined) { | ||
| this.parentSize = data.size; | ||
| } else { | ||
| this.parentSize = data.parentSize; | ||
| } | ||
| this.sizeMultiplier = sizeMultipliers[this.size - 1]; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -42,6 +52,7 @@ Options.prototype.extend = function(extension) { | |
| const data = { | ||
| style: this.style, | ||
| size: this.size, | ||
| textSize: this.textSize, | ||
| color: this.color, | ||
| parentStyle: this.style, | ||
| parentSize: this.size, | ||
|
Member
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. Can we get rid of
Collaborator
Author
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. Yes, good catch. parentStyle too. |
||
|
|
@@ -58,22 +69,66 @@ Options.prototype.extend = function(extension) { | |
| return new Options(data); | ||
| }; | ||
|
|
||
| function sizeAtStyle(size, style) { | ||
| return style.size < 2 ? size : sizeStyleMap[size - 1][style.size - 1]; | ||
|
Member
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. Is this equivalent to the following?
Collaborator
Author
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. No because style.size might be 0 (DISPLAY). It would be equivalent to
Member
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. For some reason I thought the sizes where started counting from |
||
| } | ||
|
|
||
| /** | ||
| * Create a new options object with the given style. | ||
| * Return an options object with the given style. If `this.style === style`, | ||
| * returns `this`. | ||
|
Member
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 seems functionally equivalent as long as we don't mutate these objects. Can you add a warning somewhere not to mutate these objects and to only use the functions provided to create new
Collaborator
Author
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. Sure. FWIW it would also be OK to use the old |
||
| */ | ||
| Options.prototype.withStyle = function(style) { | ||
| return this.extend({ | ||
| style: style, | ||
| }); | ||
| Options.prototype.havingStyle = function(style) { | ||
| if (this.style === style) { | ||
| return this; | ||
| } else { | ||
| return this.extend({ | ||
| style: style, | ||
| size: sizeAtStyle(this.textSize, style), | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Create a new options object with the given size. | ||
| * Return an options object with a cramped version of the current style. If | ||
| * the current style is cramped, returns `this`. | ||
| */ | ||
| Options.prototype.withSize = function(size) { | ||
| return this.extend({ | ||
| size: size, | ||
| }); | ||
| Options.prototype.havingCrampedStyle = function() { | ||
|
Member
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. Where is this used?
Collaborator
Author
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.
Member
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. Thanks. I totally missed that file b/c github auto-collapsed it.
Member
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'll try to review the changes to |
||
| return this.havingStyle(this.style.cramp()); | ||
| }; | ||
|
|
||
| /** | ||
| * Return an options object with the given size and in at least `\textstyle`. | ||
| * Returns `this` if appropriate. | ||
| */ | ||
| Options.prototype.havingSize = function(size) { | ||
| if (this.size === size && this.textSize === size) { | ||
| return this; | ||
| } else { | ||
| return this.extend({ | ||
| style: this.style.text(), | ||
| size: size, | ||
| textSize: size, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Like `this.havingSize(BASESIZE).havingStyle(style)`. If `style` is omitted, | ||
| * changes to at least `\textstyle`. | ||
| */ | ||
| Options.prototype.havingBaseStyle = function(style) { | ||
| style = style || this.style.text(); | ||
| const wantSize = sizeAtStyle(BASESIZE, style); | ||
| if (this.size === wantSize && this.textSize === BASESIZE | ||
| && this.style === style) { | ||
| return this; | ||
| } else { | ||
| return this.extend({ | ||
| style: style, | ||
| size: wantSize, | ||
| baseSize: BASESIZE, | ||
| }); | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -104,11 +159,27 @@ Options.prototype.withFont = function(font) { | |
| }; | ||
|
|
||
| /** | ||
| * Create a new options object with the same style, size, and color. This is | ||
| * used so that parent style and size changes are handled correctly. | ||
| * Return the CSS sizing classes required to switch from enclosing options | ||
| * `oldOptions` to `this`. Returns an array of classes. | ||
|
Member
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. Nice comment. |
||
| */ | ||
| Options.prototype.sizingClasses = function(oldOptions) { | ||
| if (oldOptions.size !== this.size) { | ||
| return ["sizing", "reset-size" + oldOptions.size, "size" + this.size]; | ||
| } else { | ||
| return []; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * Return the CSS sizing classes required to switch to the base size. Like | ||
| * `this.havingSize(BASESIZE).sizingClasses(this)`. | ||
| */ | ||
| Options.prototype.reset = function() { | ||
| return this.extend({}); | ||
| Options.prototype.baseSizingClasses = function() { | ||
| if (this.size !== BASESIZE) { | ||
| return ["sizing", "reset-size" + this.size, "size" + BASESIZE]; | ||
| } else { | ||
| return []; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -186,4 +257,9 @@ Options.prototype.getColor = function() { | |
| } | ||
| }; | ||
|
|
||
| /** | ||
| * The base size index. | ||
| */ | ||
| Options.BASESIZE = BASESIZE; | ||
|
|
||
| module.exports = Options; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -390,8 +390,8 @@ Parser.prototype.parseAtom = function() { | |
|
|
||
| // A list of the size-changing functions, for use in parseImplicitGroup | ||
| const sizeFuncs = [ | ||
| "\\tiny", "\\scriptsize", "\\footnotesize", "\\small", "\\normalsize", | ||
| "\\large", "\\Large", "\\LARGE", "\\huge", "\\Huge", | ||
| "\\tiny", "\\sixptsize", "\\scriptsize", "\\footnotesize", "\\small", | ||
| "\\normalsize", "\\large", "\\Large", "\\LARGE", "\\huge", "\\Huge", | ||
| ]; | ||
|
|
||
| // A list of the style-changing functions, for use in parseImplicitGroup | ||
|
|
@@ -483,7 +483,7 @@ Parser.prototype.parseImplicitGroup = function() { | |
| const body = this.parseExpression(false); | ||
| return new ParseNode("sizing", { | ||
| // Figure out what size to use based on the list of functions above | ||
| size: "size" + (utils.indexOf(sizeFuncs, func) + 1), | ||
| size: utils.indexOf(sizeFuncs, func) + 1, | ||
|
Member
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. nice cleanup |
||
| value: body, | ||
| }, this.mode); | ||
| } else if (utils.contains(styleFuncs, func)) { | ||
|
|
||
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.
The doc string for this function references a
.resetfunction with doesn't currently exist. It needs to be updated.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.
Good catch. The whole comment refers to “parent” functionality that is no longer present or needed. I'll replace this comment with a comment about it being readonly.