2014-11-14 23:27:49 +00:00
|
|
|
unit fpsvisualutils;
|
|
|
|
|
2015-01-12 11:42:23 +00:00
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
|
2014-11-14 23:27:49 +00:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
Classes, SysUtils, Graphics,
|
2015-01-17 22:57:23 +00:00
|
|
|
fpstypes, fpspreadsheet;
|
2014-11-14 23:27:49 +00:00
|
|
|
|
2015-05-31 16:06:22 +00:00
|
|
|
procedure Convert_sFont_to_Font(sFont: TsFont; AFont: TFont); overload;
|
|
|
|
procedure Convert_sFont_to_Font(AWorkbook: TsWorkbook; sFont: TsFont; AFont: TFont); overload; deprecated;
|
|
|
|
|
|
|
|
procedure Convert_Font_to_sFont(AFont: TFont; sFont: TsFont); overload;
|
|
|
|
procedure Convert_Font_to_sFont(AWorkbook: TsWorkbook; AFont: TFont; sFont: TsFont); overload; deprecated;
|
|
|
|
|
2014-11-14 23:27:49 +00:00
|
|
|
function WrapText(ACanvas: TCanvas; const AText: string; AMaxWidth: integer): string;
|
|
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2015-05-31 16:06:22 +00:00
|
|
|
Types, LCLType, LCLIntf, fpsUtils;
|
2014-11-14 23:27:49 +00:00
|
|
|
|
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Converts a spreadsheet font to a font used for painting (TCanvas.Font).
|
|
|
|
|
|
|
|
@param sFont Font as used by fpspreadsheet (input)
|
|
|
|
@param AFont Font as used by TCanvas for painting (output)
|
|
|
|
-------------------------------------------------------------------------------}
|
2015-05-31 16:06:22 +00:00
|
|
|
procedure Convert_sFont_to_Font(sFont: TsFont; AFont: TFont);
|
2014-11-14 23:27:49 +00:00
|
|
|
begin
|
|
|
|
if Assigned(AFont) and Assigned(sFont) then begin
|
|
|
|
AFont.Name := sFont.FontName;
|
|
|
|
AFont.Size := round(sFont.Size);
|
|
|
|
AFont.Style := [];
|
|
|
|
if fssBold in sFont.Style then AFont.Style := AFont.Style + [fsBold];
|
|
|
|
if fssItalic in sFont.Style then AFont.Style := AFont.Style + [fsItalic];
|
|
|
|
if fssUnderline in sFont.Style then AFont.Style := AFont.Style + [fsUnderline];
|
|
|
|
if fssStrikeout in sFont.Style then AFont.Style := AFont.Style + [fsStrikeout];
|
2015-05-28 20:08:24 +00:00
|
|
|
AFont.Color := TColor(sFont.Color and $00FFFFFF);
|
2014-11-14 23:27:49 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2015-05-31 16:06:22 +00:00
|
|
|
procedure Convert_sFont_to_Font(AWorkbook: TsWorkbook; sFont: TsFont; AFont: TFont);
|
|
|
|
begin
|
|
|
|
Unused(AWorkbook);
|
|
|
|
Convert_sFont_to_Font(sFont, AFont);
|
|
|
|
end;
|
|
|
|
|
2014-11-14 23:27:49 +00:00
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Converts a font used for painting (TCanvas.Font) to a spreadsheet font.
|
|
|
|
|
|
|
|
@param AFont Font as used by TCanvas for painting (input)
|
|
|
|
@param sFont Font as used by fpspreadsheet (output)
|
|
|
|
-------------------------------------------------------------------------------}
|
2015-05-31 16:06:22 +00:00
|
|
|
procedure Convert_Font_to_sFont(AFont: TFont; sFont: TsFont);
|
2014-11-14 23:27:49 +00:00
|
|
|
begin
|
|
|
|
if Assigned(AFont) and Assigned(sFont) then begin
|
|
|
|
sFont.FontName := AFont.Name;
|
|
|
|
sFont.Size := AFont.Size;
|
|
|
|
sFont.Style := [];
|
|
|
|
if fsBold in AFont.Style then Include(sFont.Style, fssBold);
|
|
|
|
if fsItalic in AFont.Style then Include(sFont.Style, fssItalic);
|
|
|
|
if fsUnderline in AFont.Style then Include(sFont.Style, fssUnderline);
|
|
|
|
if fsStrikeout in AFont.Style then Include(sFont.Style, fssStrikeout);
|
2015-05-28 20:08:24 +00:00
|
|
|
sFont.Color := ColorToRGB(AFont.Color);
|
2014-11-14 23:27:49 +00:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2015-05-31 16:06:22 +00:00
|
|
|
procedure Convert_Font_to_sFont(AWorkbook: TsWorkbook; AFont: TFont; sFont: TsFont);
|
2014-11-14 23:27:49 +00:00
|
|
|
begin
|
2015-05-31 16:06:22 +00:00
|
|
|
Unused(AWorkbook);
|
|
|
|
Convert_Font_to_sFont(AFont, sFont);
|
2014-11-14 23:27:49 +00:00
|
|
|
end;
|
2015-05-31 16:06:22 +00:00
|
|
|
|
2014-11-14 23:27:49 +00:00
|
|
|
{@@ ----------------------------------------------------------------------------
|
|
|
|
Wraps text by inserting line ending characters so that the lines are not
|
|
|
|
longer than AMaxWidth.
|
|
|
|
|
|
|
|
@param ACanvas Canvas on which the text will be drawn
|
|
|
|
@param AText Text to be drawn
|
|
|
|
@param AMaxWidth Maximimum line width (in pixels)
|
|
|
|
@return Text with inserted line endings such that the lines are shorter than
|
|
|
|
AMaxWidth.
|
|
|
|
|
|
|
|
@note Based on ocde posted by user "taazz" in the Lazarus forum
|
|
|
|
http://forum.lazarus.freepascal.org/index.php/topic,21305.msg124743.html#msg124743
|
|
|
|
-------------------------------------------------------------------------------}
|
|
|
|
function WrapText(ACanvas: TCanvas; const AText: string; AMaxWidth: integer): string;
|
|
|
|
var
|
|
|
|
DC: HDC;
|
|
|
|
textExtent: TSize = (cx:0; cy:0);
|
|
|
|
S, P, E: PChar;
|
|
|
|
line: string;
|
|
|
|
isFirstLine: boolean;
|
|
|
|
begin
|
|
|
|
Result := '';
|
|
|
|
DC := ACanvas.Handle;
|
|
|
|
isFirstLine := True;
|
|
|
|
P := PChar(AText);
|
|
|
|
while P^ = ' ' do
|
|
|
|
Inc(P);
|
|
|
|
while P^ <> #0 do begin
|
|
|
|
S := P;
|
|
|
|
E := nil;
|
|
|
|
while (P^ <> #0) and (P^ <> #13) and (P^ <> #10) do begin
|
|
|
|
LCLIntf.GetTextExtentPoint(DC, S, P - S + 1, textExtent);
|
|
|
|
if (textExtent.CX > AMaxWidth) and (E <> nil) then begin
|
|
|
|
if (P^ <> ' ') and (P^ <> ^I) then begin
|
|
|
|
while (E >= S) do
|
|
|
|
case E^ of
|
|
|
|
'.', ',', ';', '?', '!', '-', ':',
|
|
|
|
')', ']', '}', '>', '/', '\', ' ':
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
Dec(E);
|
|
|
|
end;
|
|
|
|
if E < S then
|
|
|
|
E := P - 1;
|
|
|
|
end;
|
|
|
|
Break;
|
|
|
|
end;
|
|
|
|
E := P;
|
|
|
|
Inc(P);
|
|
|
|
end;
|
|
|
|
if E <> nil then begin
|
|
|
|
while (E >= S) and (E^ = ' ') do
|
|
|
|
Dec(E);
|
|
|
|
end;
|
|
|
|
if E <> nil then
|
|
|
|
SetString(Line, S, E - S + 1)
|
|
|
|
else
|
|
|
|
SetLength(Line, 0);
|
|
|
|
if (P^ = #13) or (P^ = #10) then begin
|
|
|
|
Inc(P);
|
|
|
|
if (P^ <> (P - 1)^) and ((P^ = #13) or (P^ = #10)) then
|
|
|
|
Inc(P);
|
|
|
|
if P^ = #0 then
|
|
|
|
line := line + LineEnding;
|
|
|
|
end
|
|
|
|
else if P^ <> ' ' then
|
|
|
|
P := E + 1;
|
|
|
|
while P^ = ' ' do
|
|
|
|
Inc(P);
|
|
|
|
if isFirstLine then begin
|
|
|
|
Result := Line;
|
|
|
|
isFirstLine := False;
|
|
|
|
end else
|
|
|
|
Result := Result + LineEnding + line;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|