@@ -80,11 +80,14 @@ export async function resolveSize(
8080 widthInInches = svgWidthInInches ;
8181 }
8282
83+ console . log ( { options } ) ;
8384 return {
8485 widthInInches,
8586 heightInInches,
8687 widthInPoints : Math . round ( widthInInches * 96 ) ,
8788 heightInPoints : Math . round ( heightInInches * 96 ) ,
89+ explicitWidth : options ?. [ kFigWidth ] !== undefined ,
90+ explicitHeight : options ?. [ kFigHeight ] !== undefined ,
8891 } ;
8992}
9093
@@ -96,13 +99,13 @@ export const fixupAlignment = (svg: Element, align: string) => {
9699
97100 switch ( align ) {
98101 case "left" :
99- style = `${ style } display: block; margin: auto auto auto 0` ;
102+ style = `${ style } ; display: block; margin: auto auto auto 0` ;
100103 break ;
101104 case "right" :
102- style = `${ style } display: block; margin: auto 0 auto auto` ;
105+ style = `${ style } ; display: block; margin: auto 0 auto auto` ;
103106 break ;
104107 case "center" :
105- style = `${ style } display: block; margin: auto auto auto auto` ;
108+ style = `${ style } ; display: block; margin: auto auto auto auto` ;
106109 break ;
107110 }
108111 svg . setAttribute ( "style" , style ) ;
@@ -120,15 +123,46 @@ export async function setSvgSize(
120123 const {
121124 widthInPoints,
122125 heightInPoints,
126+ explicitHeight,
127+ explicitWidth,
123128 } = await resolveSize ( mappedSvgSrc . value , options ) ;
124129
130+ console . log ( {
131+ widthInPoints,
132+ heightInPoints,
133+ explicitHeight,
134+ explicitWidth,
135+ } ) ;
136+
125137 const dom = ( await getDomParser ( ) ) . parseFromString (
126138 mappedSvgSrc . value ,
127139 "text/html" ,
128140 ) ;
129141 const svg = dom ! . querySelector ( "svg" ) ! ;
130- svg . setAttribute ( "width" , widthInPoints ) ;
131- svg . setAttribute ( "height" , heightInPoints ) ;
142+ if ( explicitWidth && explicitHeight ) {
143+ svg . setAttribute ( "width" , widthInPoints ) ;
144+ svg . setAttribute ( "height" , heightInPoints ) ;
145+ // if explicit width and height are given, we need to remove max-width and max-height
146+ // so that the figure doesn't get squished.
147+ svg . setAttribute (
148+ "style" ,
149+ ( svg . attributes . getNamedItem ( "style" ) ?. value || "" ) +
150+ "; max-width: none; max-height: none" ,
151+ ) ;
152+ } else {
153+ // we don't have access to svg.style as a property here...
154+ // so we have to do it the roundabout way.
155+ let style = svg . attributes . getNamedItem ( "style" ) ?. value || "" ;
156+ if ( explicitWidth ) {
157+ style = `${ style } ; max-width: ${ widthInPoints } px` ;
158+ }
159+ if ( explicitHeight ) {
160+ style = `${ style } ; max-height: ${ heightInPoints } px` ;
161+ }
162+ if ( explicitWidth || explicitHeight ) {
163+ svg . setAttribute ( "style" , style ) ;
164+ }
165+ }
132166 if ( extra ) {
133167 extra ( svg ) ;
134168 }
0 commit comments