@@ -36,6 +36,8 @@ import {
36
36
import {
37
37
cmRegex ,
38
38
cmToTWIP ,
39
+ emRegex ,
40
+ emToEmu ,
39
41
HIPToTWIP ,
40
42
inchRegex ,
41
43
inchToTWIP ,
@@ -49,6 +51,8 @@ import {
49
51
pointToEIP ,
50
52
pointToHIP ,
51
53
pointToTWIP ,
54
+ remRegex ,
55
+ remToEmu ,
52
56
TWIPToEMU ,
53
57
} from "../utils/unit-conversion.ts"
54
58
import { isValidUrl } from "../utils/url.ts"
@@ -1093,42 +1097,52 @@ function computeImageDimensions(vNode: VNode, attributes: Attributes) {
1093
1097
let modifiedWidth
1094
1098
1095
1099
if ( vNode . properties && vNode . properties . style ) {
1096
- if ( vNode . properties . style . width ) {
1097
- if ( vNode . properties . style . width !== "auto" ) {
1098
- if ( pixelRegex . test ( vNode . properties . style . width ) ) {
1099
- modifiedWidth = pixelToEMU (
1100
- vNode . properties . style . width . match ( pixelRegex ) [ 1 ] ,
1101
- )
1100
+ const styleWidth = vNode . properties . style . width
1101
+ const styleHeight = vNode . properties . style . height
1102
+
1103
+ if ( styleWidth ) {
1104
+ if ( styleWidth !== "auto" ) {
1105
+ if ( pixelRegex . test ( styleWidth ) ) {
1106
+ modifiedWidth = pixelToEMU ( styleWidth . match ( pixelRegex ) [ 1 ] )
1107
+ }
1108
+ else if ( emRegex . test ( styleWidth ) ) {
1109
+ modifiedWidth = emToEmu ( styleWidth . match ( emRegex ) [ 1 ] )
1102
1110
}
1103
- else if ( percentageRegex . test ( vNode . properties . style . width ) ) {
1104
- const percentageValue =
1105
- vNode . properties . style . width . match ( percentageRegex ) [ 1 ]
1111
+ else if ( remRegex . test ( styleWidth ) ) {
1112
+ modifiedWidth = remToEmu ( styleWidth . match ( remRegex ) [ 1 ] )
1113
+ }
1114
+ else if ( percentageRegex . test ( styleWidth ) ) {
1115
+ const percentageValue = styleWidth . match ( percentageRegex ) [ 1 ]
1106
1116
1107
1117
modifiedWidth = Math . round (
1108
1118
( percentageValue / 100 ) * originalWidthInEMU ,
1109
1119
)
1110
1120
}
1121
+ else {
1122
+ modifiedWidth = originalWidthInEMU
1123
+ modifiedHeight = originalHeightInEMU
1124
+ }
1111
1125
}
1112
1126
else {
1113
- if (
1114
- vNode . properties . style . height &&
1115
- vNode . properties . style . height === "auto"
1116
- ) {
1127
+ if ( styleHeight && styleHeight === "auto" ) {
1117
1128
modifiedWidth = originalWidthInEMU
1118
1129
modifiedHeight = originalHeightInEMU
1119
1130
}
1120
1131
}
1121
1132
}
1122
- if ( vNode . properties . style . height ) {
1123
- if ( vNode . properties . style . height !== "auto" ) {
1124
- if ( pixelRegex . test ( vNode . properties . style . height ) ) {
1125
- modifiedHeight = pixelToEMU (
1126
- vNode . properties . style . height . match ( pixelRegex ) [ 1 ] ,
1127
- )
1133
+ if ( styleHeight ) {
1134
+ if ( styleHeight !== "auto" ) {
1135
+ if ( pixelRegex . test ( styleHeight ) ) {
1136
+ modifiedHeight = pixelToEMU ( styleHeight . match ( pixelRegex ) [ 1 ] )
1137
+ }
1138
+ else if ( emRegex . test ( styleWidth ) ) {
1139
+ modifiedWidth = emToEmu ( styleWidth . match ( emRegex ) [ 1 ] )
1128
1140
}
1129
- else if ( percentageRegex . test ( vNode . properties . style . height ) ) {
1130
- const percentageValue =
1131
- vNode . properties . style . width . match ( percentageRegex ) [ 1 ]
1141
+ else if ( remRegex . test ( styleWidth ) ) {
1142
+ modifiedWidth = emToEmu ( styleWidth . match ( remRegex ) [ 1 ] )
1143
+ }
1144
+ else if ( percentageRegex . test ( styleHeight ) ) {
1145
+ const percentageValue = styleWidth . match ( percentageRegex ) [ 1 ]
1132
1146
1133
1147
modifiedHeight = Math . round (
1134
1148
( percentageValue / 100 ) * originalHeightInEMU ,
@@ -1156,14 +1170,18 @@ function computeImageDimensions(vNode: VNode, attributes: Attributes) {
1156
1170
else if ( modifiedHeight && ! modifiedWidth ) {
1157
1171
modifiedWidth = Math . round ( modifiedHeight * aspectRatio )
1158
1172
}
1173
+ else {
1174
+ modifiedWidth = originalWidthInEMU
1175
+ modifiedHeight = originalHeightInEMU
1176
+ }
1159
1177
}
1160
1178
else {
1161
1179
modifiedWidth = originalWidthInEMU
1162
1180
modifiedHeight = originalHeightInEMU
1163
1181
}
1164
1182
1165
1183
attributes . width = modifiedWidth
1166
- attributes . height = modifiedHeight || 0
1184
+ attributes . height = modifiedHeight
1167
1185
}
1168
1186
1169
1187
async function buildParagraph (
@@ -2093,16 +2111,16 @@ function buildTableProperties(attributes: Attributes) {
2093
2111
}
2094
2112
case "tableCellSpacing" : {
2095
2113
const tableCellSpacingFragment = buildTableCellSpacing (
2096
- attributes [ key ] ,
2114
+ attributes . tableCellSpacing ,
2097
2115
)
2098
2116
tablePropertiesFragment . import ( tableCellSpacingFragment )
2099
2117
2100
2118
delete attributes . tableCellSpacing
2101
2119
break
2102
2120
}
2103
2121
case "width" : {
2104
- if ( attributes [ key ] ) {
2105
- const tableWidthFragment = buildTableWidth ( attributes [ key ] )
2122
+ if ( attributes . width ) {
2123
+ const tableWidthFragment = buildTableWidth ( attributes . width )
2106
2124
tablePropertiesFragment . import ( tableWidthFragment )
2107
2125
}
2108
2126
@@ -2376,11 +2394,14 @@ function buildPresetGeometry() {
2376
2394
. up ( )
2377
2395
}
2378
2396
2379
- function buildExtents ( { width = 0 , height = 0 } ) {
2397
+ function buildExtents ( { width, height } : { width ?: number ; height ?: number } ) {
2398
+ if ( ! width && ! height ) {
2399
+ return
2400
+ }
2380
2401
return fragment ( { namespaceAlias : { a : namespaces . a } } )
2381
2402
. ele ( "@a" , "ext" )
2382
- . att ( "cx" , String ( width ) )
2383
- . att ( "cy" , String ( height ) )
2403
+ . att ( "cx" , String ( width || "" ) )
2404
+ . att ( "cy" , String ( height || "" ) )
2384
2405
. up ( )
2385
2406
}
2386
2407
@@ -2393,27 +2414,26 @@ function buildOffset() {
2393
2414
}
2394
2415
2395
2416
function buildGraphicFrameTransform (
2396
- attributes : { width : number ; height : number } ,
2417
+ attributes : { width ? : number ; height ? : number } ,
2397
2418
) {
2398
2419
const graphicFrameTransformFragment = fragment ( {
2399
2420
namespaceAlias : { a : namespaces . a } ,
2400
2421
} )
2401
- . ele (
2402
- "@a" ,
2403
- "xfrm" ,
2404
- )
2422
+ . ele ( "@a" , "xfrm" )
2405
2423
2406
2424
const offsetFragment = buildOffset ( )
2407
2425
graphicFrameTransformFragment . import ( offsetFragment )
2408
2426
const extentsFragment = buildExtents ( attributes )
2409
- graphicFrameTransformFragment . import ( extentsFragment )
2427
+ if ( extentsFragment ) {
2428
+ graphicFrameTransformFragment . import ( extentsFragment )
2429
+ }
2410
2430
2411
2431
graphicFrameTransformFragment . up ( )
2412
2432
2413
2433
return graphicFrameTransformFragment
2414
2434
}
2415
2435
2416
- function buildShapeProperties ( attributes : { width : number ; height : number } ) {
2436
+ function buildShapeProperties ( attributes : { width ? : number ; height ? : number } ) {
2417
2437
const shapeProperties = fragment ( { namespaceAlias : { pic : namespaces . pic } } )
2418
2438
. ele ( "@pic" , "spPr" )
2419
2439
@@ -2538,8 +2558,8 @@ function buildPicture(
2538
2558
fileNameWithExtension,
2539
2559
description,
2540
2560
relationshipId,
2541
- width = 0 ,
2542
- height = 0 ,
2561
+ width,
2562
+ height,
2543
2563
} : Attributes ,
2544
2564
) {
2545
2565
const pictureFragment = fragment ( { namespaceAlias : { pic : namespaces . pic } } )
@@ -2624,11 +2644,14 @@ function buildEffectExtentFragment() {
2624
2644
. up ( )
2625
2645
}
2626
2646
2627
- function buildExtent ( { width = 0 , height = 0 } ) {
2647
+ function buildExtent ( { width, height } : { width ?: number ; height ?: number } ) {
2648
+ if ( ! width && ! height ) {
2649
+ return
2650
+ }
2628
2651
return fragment ( { namespaceAlias : { wp : namespaces . wp } } )
2629
2652
. ele ( "@wp" , "extent" )
2630
- . att ( "cx" , String ( width ) )
2631
- . att ( "cy" , String ( height ) )
2653
+ . att ( "cx" , String ( width || "" ) )
2654
+ . att ( "cy" , String ( height || "" ) )
2632
2655
. up ( )
2633
2656
}
2634
2657
@@ -2684,7 +2707,9 @@ function buildAnchoredDrawing(graphicType: "picture", attributes: Attributes) {
2684
2707
width : attributes . width ,
2685
2708
height : attributes . height ,
2686
2709
} )
2687
- anchoredDrawingFragment . import ( extentFragment )
2710
+ if ( extentFragment ) {
2711
+ anchoredDrawingFragment . import ( extentFragment )
2712
+ }
2688
2713
const effectExtentFragment = buildEffectExtentFragment ( )
2689
2714
anchoredDrawingFragment . import ( effectExtentFragment )
2690
2715
const wrapSquareFragment = buildWrapSquare ( )
@@ -2717,7 +2742,9 @@ function buildInlineDrawing(graphicType: "picture", attributes: Attributes) {
2717
2742
width : attributes . width ,
2718
2743
height : attributes . height ,
2719
2744
} )
2720
- inlineDrawingFragment . import ( extentFragment )
2745
+ if ( extentFragment ) {
2746
+ inlineDrawingFragment . import ( extentFragment )
2747
+ }
2721
2748
const effectExtentFragment = buildEffectExtentFragment ( )
2722
2749
inlineDrawingFragment . import ( effectExtentFragment )
2723
2750
const drawingObjectNonVisualPropertiesFragment =
0 commit comments