@@ -40,6 +40,9 @@ public class SheetUtil
40
40
// */
41
41
private static char defaultChar = '0' ;
42
42
43
+ // Default dpi
44
+ private static int dpi = 96 ;
45
+
43
46
// /**
44
47
// * This is the multiple that the font height is scaled by when determining the
45
48
// * boundary of rotated text.
@@ -269,19 +272,19 @@ public static IRow CopyRow(ISheet sheet, int sourceRowIndex, int targetRowIndex)
269
272
270
273
public static double GetRowHeight ( IRow row , bool useMergedCells , int firstColumnIdx , int lastColumnIdx )
271
274
{
272
- double width = - 1 ;
275
+ double height = - 1 ;
273
276
274
277
for ( int cellIdx = firstColumnIdx ; cellIdx <= lastColumnIdx ; ++ cellIdx )
275
278
{
276
279
ICell cell = row . GetCell ( cellIdx ) ;
277
280
if ( row != null && cell != null )
278
281
{
279
- double cellWidth = GetCellHeight ( cell , useMergedCells ) ;
280
- width = Math . Max ( width , cellWidth ) ;
282
+ double cellHeight = GetCellHeight ( cell , useMergedCells ) ;
283
+ height = Math . Max ( height , cellHeight ) ;
281
284
}
282
285
}
283
286
284
- return width ;
287
+ return height ;
285
288
}
286
289
287
290
public static double GetRowHeight ( ISheet sheet , int rowIdx , bool useMergedCells , int firstColumnIdx , int lastColumnIdx )
@@ -366,8 +369,7 @@ private static int GetNumberOfRowsInMergedRegion(ICell cell)
366
369
367
370
private static double GetCellConetntHeight ( double actualHeight , int numberOfRowsInMergedRegion )
368
371
{
369
- var correction = 1.1 ;
370
- return Math . Max ( - 1 , actualHeight / numberOfRowsInMergedRegion * correction ) ;
372
+ return Math . Max ( - 1 , actualHeight / numberOfRowsInMergedRegion ) ;
371
373
}
372
374
373
375
private static string GetCellStringValue ( ICell cell )
@@ -416,7 +418,7 @@ private static Font GetWindowsFont(ICell cell)
416
418
private static double GetRotatedContentHeight ( ICell cell , string stringValue , Font windowsFont )
417
419
{
418
420
var angle = cell . CellStyle . Rotation * 2.0 * Math . PI / 360.0 ;
419
- var measureResult = TextMeasurer . Measure ( stringValue , new TextOptions ( windowsFont ) ) ;
421
+ var measureResult = TextMeasurer . MeasureAdvance ( stringValue , new TextOptions ( windowsFont ) { Dpi = dpi } ) ;
420
422
421
423
var x1 = Math . Abs ( measureResult . Height * Math . Cos ( angle ) ) ;
422
424
var x2 = Math . Abs ( measureResult . Width * Math . Sin ( angle ) ) ;
@@ -426,7 +428,7 @@ private static double GetRotatedContentHeight(ICell cell, string stringValue, Fo
426
428
427
429
private static double GetContentHeight ( string stringValue , Font windowsFont )
428
430
{
429
- var measureResult = TextMeasurer . Measure ( stringValue , new TextOptions ( windowsFont ) ) ;
431
+ var measureResult = TextMeasurer . MeasureAdvance ( stringValue , new TextOptions ( windowsFont ) { Dpi = dpi } ) ;
430
432
431
433
return Math . Round ( measureResult . Height , 0 , MidpointRounding . ToEven ) ;
432
434
}
@@ -480,7 +482,7 @@ public static double GetCellWidth(ICell cell, int defaultCharWidth, DataFormatte
480
482
String [ ] lines = rt . String . Split ( "\n " . ToCharArray ( ) ) ;
481
483
for ( int i = 0 ; i < lines . Length ; i ++ )
482
484
{
483
- String txt = lines [ i ] + defaultChar ;
485
+ String txt = lines [ i ] ;
484
486
485
487
//AttributedString str = new AttributedString(txt);
486
488
//copyAttributes(font, str, 0, txt.length());
@@ -514,7 +516,7 @@ public static double GetCellWidth(ICell cell, int defaultCharWidth, DataFormatte
514
516
}
515
517
if ( sval != null )
516
518
{
517
- String txt = sval + defaultChar ;
519
+ String txt = sval ;
518
520
//str = new AttributedString(txt);
519
521
//copyAttributes(font, str, 0, txt.length());
520
522
windowsFont = IFont2Font ( font ) ;
@@ -530,25 +532,20 @@ private static double GetCellWidth(int defaultCharWidth, int colspan,
530
532
{
531
533
//Rectangle bounds;
532
534
double actualWidth ;
533
- FontRectangle sf = TextMeasurer . Measure ( str , new TextOptions ( windowsFont ) ) ;
535
+ FontRectangle sf = TextMeasurer . MeasureSize ( str , new TextOptions ( windowsFont ) { Dpi = dpi } ) ;
534
536
if ( style . Rotation != 0 )
535
537
{
536
538
double angle = style . Rotation * 2.0 * Math . PI / 360.0 ;
537
539
double x1 = Math . Abs ( sf . Height * Math . Sin ( angle ) ) ;
538
540
double x2 = Math . Abs ( sf . Width * Math . Cos ( angle ) ) ;
539
541
actualWidth = Math . Round ( x1 + x2 , 0 , MidpointRounding . ToEven ) ;
540
- //bounds = layout.getOutline(trans).getBounds();
541
542
}
542
543
else
543
- {
544
- //bounds = layout.getBounds();
545
- actualWidth = Math . Round ( sf . Width , 0 , MidpointRounding . ToEven ) ;
546
- }
547
- // entireWidth accounts for leading spaces which is excluded from bounds.getWidth()
548
- //double frameWidth = bounds.getX() + bounds.getWidth();
549
- //width = Math.max(width, ((frameWidth / colspan) / defaultCharWidth) + style.getIndention());
550
- var correction = 1.1 ;
551
- width = Math . Max ( width , ( actualWidth / colspan / defaultCharWidth * correction ) + cell . CellStyle . Indention ) ;
544
+ actualWidth = Math . Round ( sf . Width , 0 , MidpointRounding . ToEven ) ;
545
+
546
+ int padding = 5 ;
547
+ double correction = 1.05 ;
548
+ width = Math . Max ( width , ( ( actualWidth + padding ) / colspan / defaultCharWidth * correction ) + cell . CellStyle . Indention ) ;
552
549
return width ;
553
550
}
554
551
@@ -610,13 +607,9 @@ public static double GetColumnWidth(ISheet sheet, int column, bool useMergedCell
610
607
public static int GetDefaultCharWidth ( IWorkbook wb )
611
608
{
612
609
IFont defaultFont = wb . GetFontAt ( ( short ) 0 ) ;
613
-
614
- //AttributedString str = new AttributedString(String.valueOf(defaultChar));
615
- //copyAttributes(defaultFont, str, 0, 1);
616
- //TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
617
- //int defaultCharWidth = (int)layout.getAdvance();
618
610
Font font = IFont2Font ( defaultFont ) ;
619
- return ( int ) Math . Ceiling ( TextMeasurer . Measure ( new string ( defaultChar , 1 ) , new TextOptions ( font ) ) . Width ) ;
611
+
612
+ return ( int ) Math . Ceiling ( TextMeasurer . MeasureSize ( new string ( defaultChar , 1 ) , new TextOptions ( font ) { Dpi = dpi } ) . Width ) ;
620
613
}
621
614
622
615
/**
0 commit comments