View | Details | Raw Unified | Return to bug 51622
Collapse All | Expand All

(-)src/java/org/apache/poi/ss/util/SheetUtil.java (-2 / +8 lines)
Lines 21-26 Link Here
21
import java.awt.font.TextAttribute;
21
import java.awt.font.TextAttribute;
22
import java.awt.font.TextLayout;
22
import java.awt.font.TextLayout;
23
import java.awt.geom.AffineTransform;
23
import java.awt.geom.AffineTransform;
24
import java.awt.geom.Rectangle2D;
24
import java.text.AttributedString;
25
import java.text.AttributedString;
25
import java.util.Locale;
26
import java.util.Locale;
26
import java.util.Map;
27
import java.util.Map;
Lines 165-170 Link Here
165
    private static double getCellWidth(int defaultCharWidth, int colspan,
166
    private static double getCellWidth(int defaultCharWidth, int colspan,
166
            CellStyle style, double width, AttributedString str) {
167
            CellStyle style, double width, AttributedString str) {
167
        TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
168
        TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
169
        final Rectangle2D bounds;
168
        if(style.getRotation() != 0){
170
        if(style.getRotation() != 0){
169
            /*
171
            /*
170
             * Transform the text using a scale so that it's height is increased by a multiple of the leading,
172
             * Transform the text using a scale so that it's height is increased by a multiple of the leading,
Lines 177-186 Link Here
177
            trans.concatenate(
179
            trans.concatenate(
178
            AffineTransform.getScaleInstance(1, fontHeightMultiple)
180
            AffineTransform.getScaleInstance(1, fontHeightMultiple)
179
            );
181
            );
180
            width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + style.getIndention());
182
            bounds = layout.getOutline(trans).getBounds();
181
        } else {
183
        } else {
182
            width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + style.getIndention());
184
            bounds = layout.getBounds();
183
        }
185
        }
186
        // entireWidth accounts for leading spaces which is excluded from bounds.getWidth()
187
        final double frameWidth = bounds.getX() + bounds.getWidth();
188
        width = Math.max(width, ((frameWidth / colspan) / defaultCharWidth) + style.getIndention());
184
        return width;
189
        return width;
185
    }
190
    }
186
191
Lines 300-305 Link Here
300
    }
305
    }
301
306
302
    public static boolean containsCell(CellRangeAddress cr, int rowIx, int colIx) {
307
    public static boolean containsCell(CellRangeAddress cr, int rowIx, int colIx) {
308
        //FIXME: isn't this the same as cr.isInRange(rowInd, colInd) ?
303
        if (cr.getFirstRow() <= rowIx && cr.getLastRow() >= rowIx
309
        if (cr.getFirstRow() <= rowIx && cr.getLastRow() >= rowIx
304
                && cr.getFirstColumn() <= colIx && cr.getLastColumn() >= colIx)
310
                && cr.getFirstColumn() <= colIx && cr.getLastColumn() >= colIx)
305
        {
311
        {
(-)src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java (-2 / +69 lines)
Lines 31-36 Link Here
31
import java.util.HashMap;
31
import java.util.HashMap;
32
import java.util.Map;
32
import java.util.Map;
33
33
34
import java.awt.geom.Rectangle2D;
35
34
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
36
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
35
import org.apache.poi.hssf.util.PaneInformation;
37
import org.apache.poi.hssf.util.PaneInformation;
36
import org.apache.poi.ss.ITestDataProvider;
38
import org.apache.poi.ss.ITestDataProvider;
Lines 416-422 Link Here
416
        sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 50681 reports exception at this point
418
        sheet.setColumnWidth(0, sheet.getColumnWidth(0)); // Bug 50681 reports exception at this point
417
        wb.close();
419
        wb.close();
418
    }
420
    }
421
    
422
    @Test
423
    public final void bug51622_testAutoSizeShouldRecognizeLeadingSpaces() throws IOException {
424
        Workbook wb = _testDataProvider.createWorkbook();
425
        BaseTestSheetAutosizeColumn.fixFonts(wb);
426
        Sheet sheet = wb.createSheet();
427
        Row row = sheet.createRow(0);
428
        Cell cell0 = row.createCell(0);
429
        Cell cell1 = row.createCell(1);
430
        Cell cell2 = row.createCell(2);
431
        
432
        cell0.setCellValue("Test Column AutoSize");
433
        cell1.setCellValue("         Test Column AutoSize");
434
        cell2.setCellValue("Test Column AutoSize         ");
435
        
436
        sheet.autoSizeColumn(0);
437
        sheet.autoSizeColumn(1);
438
        sheet.autoSizeColumn(2);
439
        
440
        int noWhitespaceColWidth = sheet.getColumnWidth(0);
441
        int leadingWhitespaceColWidth = sheet.getColumnWidth(1);
442
        int trailingWhitespaceColWidth = sheet.getColumnWidth(2);
443
        
444
        // Based on the amount of text and whitespace used, and the default font
445
        // assume that the cell with whitespace should be at least 20% wider than
446
        // the cell without whitespace. This number is arbitrary, but should be large
447
        // enough to guarantee that the whitespace cell isn't wider due to chance.
448
        // Experimentally, I calculated the ratio as 1.2478181, though this ratio may change
449
        // if the default font or margins change.
450
        final double expectedRatioThreshold = 1.2f;
451
        double leadingWhitespaceRatio = ((double) leadingWhitespaceColWidth)/noWhitespaceColWidth;
452
        double trailingWhitespaceRatio = ((double) leadingWhitespaceColWidth)/noWhitespaceColWidth;
453
        
454
        assertGreaterThan("leading whitespace is longer than no whitespace", leadingWhitespaceRatio, expectedRatioThreshold);
455
        assertGreaterThan("trailing whitespace is longer than no whitespace", trailingWhitespaceRatio, expectedRatioThreshold);
456
        assertEquals("cells with equal leading and trailing whitespace have equal width",
457
                leadingWhitespaceColWidth, trailingWhitespaceColWidth);
458
        
459
        wb.close();
460
    }
461
    
462
    /**
463
     * Test if a > b. Fails if false.
464
     *
465
     * @param message
466
     * @param a
467
     * @param b
468
     */
469
    private void assertGreaterThan(String message, double a, double b) {
470
        if (a > b) { // expected
471
        } else {
472
            String msg = "Expected: " + a + " > " + b;
473
            fail(message + ": " + msg);
474
        }
475
    }
419
476
477
    // FIXME: this function is a self-fulfilling prophecy: this test will always pass as long
478
    // as the code-under-test and the testcase code are written the same way (have the same bugs). 
420
    private double computeCellWidthManually(Cell cell0, Font font) {
479
    private double computeCellWidthManually(Cell cell0, Font font) {
421
        final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
480
        final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
422
        RichTextString rt = cell0.getRichStringCellValue();
481
        RichTextString rt = cell0.getRichStringCellValue();
Lines 432-439 Link Here
432
        }
491
        }
433
492
434
        TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
493
        TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
435
        return ((layout.getBounds().getWidth() / 1) / 8);
494
        double frameWidth = getFrameWidth(layout);
495
        return ((frameWidth / 1) / 8);
436
    }
496
    }
497
    
498
    private double getFrameWidth(TextLayout layout) {
499
        Rectangle2D bounds = layout.getBounds();
500
        double frameWidth = bounds.getX() + bounds.getWidth();
501
        return frameWidth;
502
    }
437
503
438
    private double computeCellWidthFixed(Font font, String txt) {
504
    private double computeCellWidthFixed(Font font, String txt) {
439
        final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
505
        final FontRenderContext fontRenderContext = new FontRenderContext(null, true, true);
Lines 441-447 Link Here
441
        copyAttributes(font, str, 0, txt.length());
507
        copyAttributes(font, str, 0, txt.length());
442
508
443
        TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
509
        TextLayout layout = new TextLayout(str.getIterator(), fontRenderContext);
444
        return layout.getBounds().getWidth();
510
        double frameWidth = getFrameWidth(layout);
511
        return frameWidth;
445
    }
512
    }
446
513
447
    private static void copyAttributes(Font font, AttributedString str, int startIdx, int endIdx) {
514
    private static void copyAttributes(Font font, AttributedString str, int startIdx, int endIdx) {

Return to bug 51622