fpspreadsheet: Add font writing support to ods. Add font ods test cases. Passed.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3151 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2014-06-06 13:09:14 +00:00
parent 20567b639c
commit 712e0f9f27
4 changed files with 113 additions and 34 deletions

View File

@ -35,7 +35,7 @@ begin
// Add some formatting // Add some formatting
MyWorksheet.WriteUsedFormatting(0, 0, [uffBold]); MyWorksheet.WriteUsedFormatting(0, 0, [uffBold]);
MyWorksheet.WriteFontColor(0, 1, scRed); MyWorksheet.WriteFont(0, 1, 'Times New Roman', 16, [], scRed);
// Creates a new worksheet // Creates a new worksheet
MyWorksheet := MyWorkbook.AddWorksheet('My Worksheet 2'); MyWorksheet := MyWorkbook.AddWorksheet('My Worksheet 2');

View File

@ -125,6 +125,8 @@ type
function WriteBackgroundColorStyleXMLAsString(const AFormat: TCell): String; function WriteBackgroundColorStyleXMLAsString(const AFormat: TCell): String;
function WriteBorderStyleXMLAsString(const AFormat: TCell): String; function WriteBorderStyleXMLAsString(const AFormat: TCell): String;
function WriteDefaultFontXMLAsString: String;
function WriteFontNamesXMLAsString: String;
function WriteFontStyleXMLAsString(const AFormat: TCell): String; function WriteFontStyleXMLAsString(const AFormat: TCell): String;
function WriteHorAlignmentStyleXMLAsString(const AFormat: TCell): String; function WriteHorAlignmentStyleXMLAsString(const AFormat: TCell): String;
function WriteTextRotationStyleXMLAsString(const AFormat: TCell): String; function WriteTextRotationStyleXMLAsString(const AFormat: TCell): String;
@ -1811,11 +1813,12 @@ begin
'" xmlns:text="' + SCHEMAS_XMLNS_TEXT + '" xmlns:text="' + SCHEMAS_XMLNS_TEXT +
'" xmlns:v="' + SCHEMAS_XMLNS_V + '">' + LineEnding + '" xmlns:v="' + SCHEMAS_XMLNS_V + '">' + LineEnding +
'<office:font-face-decls>' + LineEnding + '<office:font-face-decls>' + LineEnding +
' <style:font-face style:name="Arial" svg:font-family="Arial" />' + LineEnding + ' '+WriteFontNamesXMLAsString + LineEnding +
// ' <style:font-face style:name="Arial" svg:font-family="Arial" />' + LineEnding +
'</office:font-face-decls>' + LineEnding + '</office:font-face-decls>' + LineEnding +
'<office:styles>' + LineEnding + '<office:styles>' + LineEnding +
' <style:style style:name="Default" style:family="table-cell">' + LineEnding + ' <style:style style:name="Default" style:family="table-cell">' + LineEnding +
' <style:text-properties fo:font-size="10" style:font-name="Arial" />' + LineEnding + ' ' + WriteDefaultFontXMLAsString + LineEnding +
' </style:style>' + LineEnding + ' </style:style>' + LineEnding +
'</office:styles>' + LineEnding + '</office:styles>' + LineEnding +
'<office:automatic-styles>' + LineEnding + '<office:automatic-styles>' + LineEnding +
@ -1891,7 +1894,8 @@ begin
// Fonts // Fonts
' <office:font-face-decls>' + LineEnding + ' <office:font-face-decls>' + LineEnding +
' <style:font-face style:name="Arial" svg:font-family="Arial" xmlns:v="urn:schemas-microsoft-com:vml" />' + LineEnding + ' ' + WriteFontNamesXMLAsString + LineEnding +
// ' <style:font-face style:name="Arial" svg:font-family="Arial" xmlns:v="urn:schemas-microsoft-com:vml" />' + LineEnding +
' </office:font-face-decls>' + LineEnding + ' </office:font-face-decls>' + LineEnding +
// Automatic styles // Automatic styles
@ -2436,6 +2440,41 @@ begin
Result := Result + 'fo:border-top="none" '; Result := Result + 'fo:border-top="none" ';
end; end;
function TsSpreadOpenDocWriter.WriteDefaultFontXMLAsString: String;
var
fnt: TsFont;
begin
fnt := Workbook.GetFont(0);
Result := Format(
'<style:text-properties style:font-name="%s" fo:font-size="%.1f" />',
[fnt.FontName, fnt.Size], FPointSeparatorSettings
);
end;
function TsSpreadOpenDocWriter.WriteFontNamesXMLAsString: String;
var
L: TStringList;
fnt: TsFont;
i: Integer;
begin
Result := '';
L := TStringList.Create;
try
for i:=0 to Workbook.GetFontCount-1 do begin
fnt := Workbook.GetFont(i);
if (fnt <> nil) and (L.IndexOf(fnt.FontName) = -1) then
L.Add(fnt.FontName);
end;
for i:=0 to L.Count-1 do
Result := Format(
'<style:font-face style:name="%s" svg:font-family="%s" />',
[ L[i], L[i] ]
);
finally
L.Free;
end;
end;
function TsSpreadOpenDocWriter.WriteFontStyleXMLAsString(const AFormat: TCell): String; function TsSpreadOpenDocWriter.WriteFontStyleXMLAsString(const AFormat: TCell): String;
var var
fnt: TsFont; fnt: TsFont;
@ -2443,6 +2482,9 @@ var
begin begin
Result := ''; Result := '';
if not (uffFont in AFormat.UsedFormattingFields) then
exit;
fnt := Workbook.GetFont(AFormat.FontIndex); fnt := Workbook.GetFont(AFormat.FontIndex);
defFnt := Workbook.GetFont(0); // Defaultfont defFnt := Workbook.GetFont(0); // Defaultfont

View File

@ -565,6 +565,7 @@ type
procedure CopyFontList(ASource: TFPList); procedure CopyFontList(ASource: TFPList);
function FindFont(const AFontName: String; ASize: Single; function FindFont(const AFontName: String; ASize: Single;
AStyle: TsFontStyles; AColor: TsColor): Integer; AStyle: TsFontStyles; AColor: TsColor): Integer;
function GetDefaultFont: TsFont;
function GetDefaultFontSize: Single; function GetDefaultFontSize: Single;
function GetFont(AIndex: Integer): TsFont; function GetFont(AIndex: Integer): TsFont;
function GetFontCount: Integer; function GetFontCount: Integer;
@ -2955,6 +2956,14 @@ begin
end; end;
end; end;
{@@
Returns the default font. This is the first font (index 0) in the font list
}
function TsWorkbook.GetDefaultFont: TsFont;
begin
Result := GetFont(0);
end;
{@@ {@@
Returns the point size of the default font Returns the point size of the default font
} }

View File

@ -38,22 +38,28 @@ type
published published
// BIFF2 test cases // BIFF2 test cases
procedure TestWriteReadBold_BIFF2; procedure TestWriteRead_BIFF2_Bold;
procedure TestWriteReadFont_BIFF2_Arial; procedure TestWriteRead_BIFF2_Font_Arial;
procedure TestWriteReadFont_BIFF2_TimesNewRoman; procedure TestWriteRead_BIFF2_Font_TimesNewRoman;
procedure TestWriteReadFont_BIFF2_CourierNew; procedure TestWriteRead_BIFF2_Font_CourierNew;
// BIFF5 test cases // BIFF5 test cases
procedure TestWriteReadBold_BIFF5; procedure TestWriteRead_BIFF5_Bold;
procedure TestWriteReadFont_BIFF5_Arial; procedure TestWriteRead_BIFF5_Font_Arial;
procedure TestWriteReadFont_BIFF5_TimesNewRoman; procedure TestWriteRead_BIFF5_Font_TimesNewRoman;
procedure TestWriteReadFont_BIFF5_CourierNew; procedure TestWriteRead_BIFF5_Font_CourierNew;
// BIFF8 test cases // BIFF8 test cases
procedure TestWriteReadBold_BIFF8; procedure TestWriteRead_BIFF8_Bold;
procedure TestWriteReadFont_BIFF8_Arial; procedure TestWriteRead_BIFF8_Font_Arial;
procedure TestWriteReadFont_BIFF8_TimesNewRoman; procedure TestWriteRead_BIFF8_Font_TimesNewRoman;
procedure TestWriteReadFont_BIFF8_CourierNew; procedure TestWriteRead_BIFF8_Font_CourierNew;
// ODS test cases
procedure TestWriteRead_ODS_Bold;
procedure TestWriteRead_ODS_Font_Arial;
procedure TestWriteRead_ODS_Font_TimesNewRoman;
procedure TestWriteRead_ODS_Font_CourierNew;
end; end;
implementation implementation
@ -190,21 +196,26 @@ begin
DeleteFile(TempFile); DeleteFile(TempFile);
end; end;
procedure TSpreadWriteReadFontTests.TestWriteReadBold_BIFF2; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF2_Bold;
begin begin
TestWriteReadBold(sfExcel2); TestWriteReadBold(sfExcel2);
end; end;
procedure TSpreadWriteReadFontTests.TestWriteReadBold_BIFF5; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF5_Bold;
begin begin
TestWriteReadBold(sfExcel5); TestWriteReadBold(sfExcel5);
end; end;
procedure TSpreadWriteReadFontTests.TestWriteReadBold_BIFF8; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF8_Bold;
begin begin
TestWriteReadBold(sfExcel8); TestWriteReadBold(sfExcel8);
end; end;
procedure TSpreadWriteReadFontTests.TestWriteRead_ODS_Bold;
begin
TestWriteReadBold(sfOpenDocument);
end;
procedure TSpreadWriteReadFontTests.TestWriteReadFont(AFormat: TsSpreadsheetFormat; procedure TSpreadWriteReadFontTests.TestWriteReadFont(AFormat: TsSpreadsheetFormat;
AFontName: String); AFontName: String);
var var
@ -282,51 +293,68 @@ begin
end; end;
{ BIFF2 } { BIFF2 }
procedure TSpreadWriteReadFontTests.TestWriteReadFont_BIFF2_Arial; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF2_Font_Arial;
begin begin
TestWriteReadFont(sfExcel2, 'Arial'); TestWriteReadFont(sfExcel2, 'Arial');
end; end;
procedure TSpreadWriteReadFontTests.TestWriteReadFont_BIFF2_TimesNewRoman; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF2_Font_TimesNewRoman;
begin begin
TestWriteReadFont(sfExcel2, 'TimesNewRoman'); TestWriteReadFont(sfExcel2, 'Times New Roman');
end; end;
procedure TSpreadWriteReadFontTests.TestWriteReadFont_BIFF2_CourierNew; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF2_Font_CourierNew;
begin begin
TestWriteReadFont(sfExcel2, 'CourierNew'); TestWriteReadFont(sfExcel2, 'Courier New');
end; end;
{ BIFF5 } { BIFF5 }
procedure TSpreadWriteReadFontTests.TestWriteReadFont_BIFF5_Arial; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF5_Font_Arial;
begin begin
TestWriteReadFont(sfExcel5, 'Arial'); TestWriteReadFont(sfExcel5, 'Arial');
end; end;
procedure TSpreadWriteReadFontTests.TestWriteReadFont_BIFF5_TimesNewRoman; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF5_Font_TimesNewRoman;
begin begin
TestWriteReadFont(sfExcel5, 'TimesNewRoman'); TestWriteReadFont(sfExcel5, 'Times New Roman');
end; end;
procedure TSpreadWriteReadFontTests.TestWriteReadFont_BIFF5_CourierNew; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF5_Font_CourierNew;
begin begin
TestWriteReadFont(sfExcel5, 'CourierNew'); TestWriteReadFont(sfExcel5, 'Courier New');
end; end;
{ BIFF8 } { BIFF8 }
procedure TSpreadWriteReadFontTests.TestWriteReadFont_BIFF8_Arial; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF8_Font_Arial;
begin begin
TestWriteReadFont(sfExcel8, 'Arial'); TestWriteReadFont(sfExcel8, 'Arial');
end; end;
procedure TSpreadWriteReadFontTests.TestWriteReadFont_BIFF8_TimesNewRoman; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF8_Font_TimesNewRoman;
begin begin
TestWriteReadFont(sfExcel8, 'TimesNewRoman'); TestWriteReadFont(sfExcel8, 'Times New Roman');
end; end;
procedure TSpreadWriteReadFontTests.TestWriteReadFont_BIFF8_CourierNew; procedure TSpreadWriteReadFontTests.TestWriteRead_BIFF8_Font_CourierNew;
begin begin
TestWriteReadFont(sfExcel8, 'CourierNew'); TestWriteReadFont(sfExcel8, 'Courier New');
end;
{ ODS }
procedure TSpreadWriteReadFontTests.TestWriteRead_ODS_Font_Arial;
begin
TestWriteReadFont(sfOpenDocument, 'Arial');
end;
procedure TSpreadWriteReadFontTests.TestWriteRead_ODS_Font_TimesNewRoman;
begin
TestWriteReadFont(sfOpenDocument, 'Times New Roman');
end;
procedure TSpreadWriteReadFontTests.TestWriteRead_ODS_Font_CourierNew;
begin
TestWriteReadFont(sfOpenDocument, 'Courier New');
end; end;