From 34db57596783f606b13f4f2b6b304becbb8c91a1 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Sun, 27 Apr 2014 15:59:16 +0000 Subject: [PATCH] fpspreadsheet: Implement text rotation for BIFF5. Add text rotation test cases for BIIF5 and BIFF8 (no text rotation in BIFF2 by design). Pass. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2974 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/excel5demo/excel5read.lpi | 21 ++++-- .../examples/excel5demo/excel5write.lpr | 11 ++- .../fpspreadsheet/tests/formattests.pas | 71 +++++++++++++++++++ components/fpspreadsheet/xlsbiff5.pas | 16 +++-- components/fpspreadsheet/xlsbiff8.pas | 8 +++ components/fpspreadsheet/xlscommon.pas | 8 +++ 6 files changed, 117 insertions(+), 18 deletions(-) diff --git a/components/fpspreadsheet/examples/excel5demo/excel5read.lpi b/components/fpspreadsheet/examples/excel5demo/excel5read.lpi index 7198405ec..0adfe826e 100644 --- a/components/fpspreadsheet/examples/excel5demo/excel5read.lpi +++ b/components/fpspreadsheet/examples/excel5demo/excel5read.lpi @@ -1,21 +1,23 @@ - + + - - <UseAppBundle Value="False"/> </General> <VersionInfo> - <ProjectVersion Value=""/> + <StringTable ProductVersion=""/> </VersionInfo> + <BuildModes Count="1"> + <Item1 Name="default" Default="True"/> + </BuildModes> <PublishOptions> <Version Value="2"/> <IgnoreBinaries Value="False"/> @@ -42,12 +44,17 @@ </Units> </ProjectOptions> <CompilerOptions> - <Version Value="8"/> + <Version Value="11"/> <PathDelim Value="\"/> <SearchPaths> - <OtherUnitFiles Value="..\"/> - <SrcPath Value="..\"/> + <OtherUnitFiles Value=".."/> + <SrcPath Value=".."/> </SearchPaths> + <Parsing> + <SyntaxOptions> + <UseAnsiStrings Value="False"/> + </SyntaxOptions> + </Parsing> <Other> <CompilerPath Value="$(CompPath)"/> </Other> diff --git a/components/fpspreadsheet/examples/excel5demo/excel5write.lpr b/components/fpspreadsheet/examples/excel5demo/excel5write.lpr index 4c78d3c12..f5c825c45 100644 --- a/components/fpspreadsheet/examples/excel5demo/excel5write.lpr +++ b/components/fpspreadsheet/examples/excel5demo/excel5write.lpr @@ -37,9 +37,6 @@ begin // Write some cells MyWorksheet.WriteNumber(0, 0, 1.0);// A1 - MyWorksheet.WriteUsedFormatting(0, 0, [uffBold]); - MyWorksheet.WriteBackgroundColor(0, 0, scRed); - MyWorksheet.WriteNumber(0, 1, 2.0);// B1 MyWorksheet.WriteNumber(0, 2, 3.0);// C1 MyWorksheet.WriteNumber(0, 3, 4.0);// D1 @@ -59,11 +56,11 @@ begin MyWorksheet.WriteUTF8Text(4, 5, 'Stacked text'); MyWorksheet.WriteTextRotation(4, 5, rtStacked); - MyWorksheet.WriteUTF8Text(4, 5, 'Rotated text'); - MyWorksheet.WriteTextRotation(4, 5, rt90DegreeClockwiseRotation); + MyWorksheet.WriteUTF8Text(4, 6, 'CW-rotated text'); + MyWorksheet.WriteTextRotation(4, 6, rt90DegreeClockwiseRotation); - MyWorksheet.WriteUTF8Text(4, 6, 'Rotated text'); - MyWorksheet.WriteTextRotation(4, 5, rt90DegreeCounterClockwiseRotation); + MyWorksheet.WriteUTF8Text(4, 7, 'CCW-rotated text'); + MyWorksheet.WriteTextRotation(4, 7, rt90DegreeCounterClockwiseRotation); // Write current date/time MyWorksheet.WriteDateTime(5, 0, now); diff --git a/components/fpspreadsheet/tests/formattests.pas b/components/fpspreadsheet/tests/formattests.pas index 7199ebe19..4a614e51c 100644 --- a/components/fpspreadsheet/tests/formattests.pas +++ b/components/fpspreadsheet/tests/formattests.pas @@ -49,6 +49,8 @@ type procedure TestWriteReadBorder(AFormat: TsSpreadsheetFormat); // Test column widths procedure TestWriteReadColWidths(AFormat: TsSpreadsheetFormat); + // Test text rotation + procedure TestWriteReadTextRotation(AFormat:TsSpreadsheetFormat); // Test word wrapping procedure TestWriteReadWordWrap(AFormat: TsSpreadsheetFormat); @@ -60,17 +62,22 @@ type procedure TestWriteReadBIFF2_Alignment; procedure TestWriteReadBIFF2_Border; procedure TestWriteReadBIFF2_ColWidths; + // These features are not supported by Excel2 --> no test cases required! + // - TextRotation + // - Wordwrap { BIFF5 Tests } procedure TestWriteReadBIFF5_Alignment; procedure TestWriteReadBIFF5_Border; procedure TestWriteReadBIFF5_ColWidths; + procedure TestWriteReadBIFF5_TextRotation; procedure TestWriteReadBIFF5_WordWrap; { BIFF8 Tests } procedure TestWriteReadBIFF8_Alignment; procedure TestWriteReadBIFF8_Border; procedure TestWriteReadBIFF8_ColWidths; + procedure TestWriteReadBIFF8_TextRotation; procedure TestWriteReadBIFF8_WordWrap; procedure TestWriteReadNumberFormats; // Repeat with date/times @@ -88,6 +95,7 @@ const ColWidthSheet = 'ColWidths'; BordersSheet = 'CellBorders'; AlignmentSheet = 'TextAlignments'; + TextRotationSheet = 'TextRotation'; WordwrapSheet = 'Wordwrap'; // Initialize array with variables that represent the values @@ -513,6 +521,69 @@ begin TestWriteReadColWidths(sfExcel8); end; +procedure TSpreadWriteReadFormatTests.TestWriteReadTextRotation(AFormat: TsSpreadsheetFormat); +const + col = 0; +var + MyWorksheet: TsWorksheet; + MyWorkbook: TsWorkbook; + MyCell: PCell; + ActualColWidth: Single; + tr: TsTextRotation; + row: Integer; + TempFile: string; //write xls/xml to this file and read back from it +begin + TempFile:=GetTempFileName; + {// Not needed: use workbook.writetofile with overwrite=true + if fileexists(TempFile) then + DeleteFile(TempFile); + } + // Write out all test values + MyWorkbook := TsWorkbook.Create; + MyWorkSheet:= MyWorkBook.AddWorksheet(TextRotationSheet); + for tr := Low(TsTextRotation) to High(TsTextRotation) do begin + row := ord(tr); + MyWorksheet.WriteTextRotation(row, col, tr); + MyCell := MyWorksheet.GetCell(row, col); + CheckEquals(ord(tr), ord(MyCell^.TextRotation), + 'Test unsaved textrotation mismatch, cell ' + CellNotation(MyWorksheet, row, col)); + end; + MyWorkBook.WriteToFile(TempFile, AFormat, true); + MyWorkbook.Free; + + // Open the spreadsheet + MyWorkbook := TsWorkbook.Create; + MyWorkbook.ReadFromFile(TempFile, AFormat); + if AFormat = sfExcel2 then + MyWorksheet := MyWorkbook.GetFirstWorksheet + else + MyWorksheet := GetWorksheetByName(MyWorkBook, TextRotationSheet); + if MyWorksheet=nil then + fail('Error in test code. Failed to get named worksheet'); + for row := 0 to MyWorksheet.GetLastRowNumber do begin + MyCell := MyWorksheet.FindCell(row, col); + if MyCell = nil then + fail('Error in test code. Failed to get cell'); + tr := MyCell^.TextRotation; + CheckEquals(ord(TsTextRotation(row)), ord(MyCell^.TextRotation), + 'Test saved textrotation mismatch, cell ' + CellNotation(MyWorksheet, row, col)); + end; + // Finalization + MyWorkbook.Free; + + DeleteFile(TempFile); +end; + +procedure TSpreadWriteReadFormatTests.TestWriteReadBIFF5_TextRotation; +begin + TestWriteReadTextRotation(sfExcel5); +end; + +procedure TSpreadWriteReadFormatTests.TestWriteReadBIFF8_TextRotation; +begin + TestWriteReadTextRotation(sfExcel8); +end; + procedure TSpreadWriteReadFormatTests.TestWriteReadWordWrap(AFormat: TsSpreadsheetFormat); const LONGTEXT = 'This is a very, very, very, very long text.'; diff --git a/components/fpspreadsheet/xlsbiff5.pas b/components/fpspreadsheet/xlsbiff5.pas index 21560d7db..a726bc17a 100755 --- a/components/fpspreadsheet/xlsbiff5.pas +++ b/components/fpspreadsheet/xlsbiff5.pas @@ -1279,10 +1279,10 @@ begin if uffTextRotation in FFormattingStyles[i].UsedFormattingFields then begin case FFormattingStyles[i].TextRotation of - trHorizontal : lTextRotation := XF_ROTATION_HORIZONTAL; - rt90DegreeClockwiseRotation : lTextRotation := XF_ROTATION_90DEG_CW; - rt90DegreeCounterClockwiseRotation: lTextRotation := XF_ROTATION_90DEG_CCW; - rtStacked : lTextRotation := XF_ROTATION_STACKED; + trHorizontal : lTextRotation := XF_ROTATION_HORIZONTAL; + rt90DegreeClockwiseRotation : lTextRotation := XF_ROTATION_90DEG_CW; + rt90DegreeCounterClockwiseRotation : lTextRotation := XF_ROTATION_90DEG_CCW; + rtStacked : lTextRotation := XF_ROTATION_STACKED; end; end; @@ -1734,6 +1734,14 @@ begin // Word wrap lData.WordWrap := (xf.Align_TextBreak and MASK_XF_TEXTWRAP) <> 0; + // Text rotation + case xf.XFRotation of + XF_ROTATION_HORIZONTAL : lData.TextRotation := trHorizontal; + XF_ROTATION_90DEG_CCW : ldata.TextRotation := rt90DegreeCounterClockwiseRotation; + XF_ROTATION_90DEG_CW : lData.TextRotation := rt90DegreeClockwiseRotation; + XF_ROTATION_STACKED : lData.TextRotation := rtStacked; + end; + // Cell borders and background xf.Border_Background_1 := DWordLEToN(xf.Border_Background_1); xf.Border_Background_2 := DWordLEToN(xf.Border_Background_2); diff --git a/components/fpspreadsheet/xlsbiff8.pas b/components/fpspreadsheet/xlsbiff8.pas index 998053963..fd48af4a9 100755 --- a/components/fpspreadsheet/xlsbiff8.pas +++ b/components/fpspreadsheet/xlsbiff8.pas @@ -2317,6 +2317,14 @@ begin // Word wrap lData.WordWrap := (xf.Align_TextBreak and MASK_XF_TEXTWRAP) <> 0; + // TextRotation + case xf.XFRotation of + XF_ROTATION_HORIZONTAL : lData.TextRotation := trHorizontal; + XF_ROTATION_90DEG_CCW : ldata.TextRotation := rt90DegreeCounterClockwiseRotation; + XF_ROTATION_90DEG_CW : lData.TextRotation := rt90DegreeClockwiseRotation; + XF_ROTATION_STACKED : lData.TextRotation := rtStacked; + end; + // Cell borders xf.Border_Background_1 := DWordLEToN(xf.Border_Background_1); lData.Borders := []; diff --git a/components/fpspreadsheet/xlscommon.pas b/components/fpspreadsheet/xlscommon.pas index 4834caa10..b52e3c596 100644 --- a/components/fpspreadsheet/xlscommon.pas +++ b/components/fpspreadsheet/xlscommon.pas @@ -305,6 +305,7 @@ type HorAlignment: TsHorAlignment; VertAlignment: TsVertAlignment; WordWrap: Boolean; + TextRotation: TsTextRotation; Borders: TsCellBorders; BackgroundColor: TsColor; end; @@ -470,6 +471,13 @@ begin else Exclude(lCell^.UsedFormattingFields, uffWordWrap); + // Text rotation + if XFData.TextRotation > trHorizontal then + Include(lCell^.UsedFormattingFields, uffTextRotation) + else + Exclude(lCell^.UsedFormattingFields, uffTextRotation); + lCell^.TextRotation := XFData.TextRotation; + // Borders if XFData.Borders <> [] then begin Include(lCell^.UsedFormattingFields, uffBorder);