unit QExport4MSOfficeCommon;
{$I QExport4VerCtrl.inc}
interface

uses
  {$IFDEF VCL16}
    System.Classes;
  {$ELSE}
    Classes;
  {$ENDIF}

const
  MAX_DATA_LENGTH = 32767;
  sStringTooLarge = 'String too large!';

type
  TMSCellAlignment = (caLeft, caRight, caCenter, caJustify); //caJustify = WrapText for Excel 2007
  TMSCellVerticalAligment = (cvaTop, cvaMiddle, cvaBottom);
  TMSStripStyleType = (ssNone, ssColumn, ssRow);
  TXlsxBorderStyle = (xbsThin, xbsDashed, xbsDashDot, xbsDashDotDot, xbsDotted, xbsHair,
                    xbsDouble, xbsThick, xbsMedium, xbsMediumDashed, xbsMediumDashDot,
                    xbsSlantDashDot, xbsMediumDashDotDot);
                    
  TDocxBorderStyle = (dbsSingle, dbsThick, dbsDouble, dbsHairline, dbsDot,
                      dbsDashLargeGap, dbsDotDash, dbsDotDotDash, dbsTriple,
                      dbsThinThickSmallGap, dbsThickThinSmallGap, dbsThinThickThinSmallGap,
                      dbsThinThickMediumGap, dbsThickThinMediumGap, dbsThinThickThinMediumGap,
                      dbsThinThickLargeGap, dbsThickThinLargeGap, dbsThinThickThinLargeGap,
                      dbsWave, dbsDoubleWave, dbsDashSmallGap, dbsDashDotStroker, dbsEmboss3D,
                      dbsEngrave3D);

  TMSHighlightColor = (hcNone,
    hcYellow, hcGreen, hcCyan, hcMagenta, hcBlue,
    hcRed, hcDarkBlue, hcDarkCyan, hcDarkGreen, hcDarkMagenta,
    hcDarkRed, hcDarkYellow, hcDarkGray, hcLightGray, hcBlack);

function MSHighlightColorToStr(Color: TMSHighlightColor): WideString;
function MSCellAlignmentToStr(CellAlignment: TMSCellAlignment): WideString; overload;
function MSCellAlignmentToStr(CellAlignment: TMSCellVerticalAligment): WideString; overload;
function MSBorderStyleToStr(Style: TXlsxBorderStyle): WideString; overload;
function MSBorderStyleToStr(Style: TDocxBorderStyle): WideString; overload;
function ReplaceSymbols(const Value: WideString; RemoveNonprintable: Boolean =
    False): WideString;

implementation

uses QExport4EmsWideStrUtils, QExport4Common;

function ReplaceSymbols(const Value: WideString; RemoveNonprintable: Boolean =
    False): WideString;
var
  i: Integer;
  S: WideString;
begin
  Result := Value;
  i := 1;
  while i <= Length(Result) do
  begin
    if RemoveNonprintable and CharInSet(Result[i], [#00..#08, #11, #12, #14..#31]) then
      Delete(Result, i, 1)
    else if CharInSet(Result[i], ['&', '>', '<', '"', '''']) then
    begin
      case Result[i] of
        '&': S := '&amp;';
        '>': S := '&gt;';
        '<': S := '&lt;';
        '"': S := '&quot;';
        '''': S := '&#39;';
      end;
      Delete(Result, i, 1);
      Insert(S, Result, i);
      Inc(i, Length(S));
    end
    else
      Inc(i);
  end;
end;

function MSHighlightColorToStr(Color: TMSHighlightColor): WideString;
begin
  Result := '';
  case Color of
    hcYellow:
      Result := 'yellow';
    hcGreen:
      Result := 'green';
    hcCyan:
      Result := 'cyan';
    hcMagenta:
      Result := 'magenta';
    hcBlue:
      Result := 'blue';
    hcRed:
      Result := 'red';
    hcDarkBlue:
      Result := 'darkBlue';
    hcDarkCyan:
      Result := 'darkCyan';
    hcDarkGreen:
      Result := 'darkGreen';
    hcDarkMagenta:
      Result := 'darkMagenta';
    hcDarkRed:
      Result := 'darkRed';
    hcDarkYellow:
      Result := 'darkYellow';
    hcDarkGray:
      Result := 'darkGray';
    hcLightGray:
      Result := 'lightGray';
    hcBlack:
      Result := 'black';
  end;
end;

function MSCellAlignmentToStr(CellAlignment: TMSCellAlignment): WideString;
begin
  Result := '';
  case CellAlignment of
    caLeft:
      Result := 'left';
    caCenter:
      Result := 'center';
    caRight:
      Result := 'right';
  end;
end;

function MSCellAlignmentToStr(CellAlignment: TMSCellVerticalAligment): WideString;
begin
  Result := '';
  case CellAlignment of
    cvaTop:
      Result := 'top';
    cvaMiddle:
      Result := 'center';
    cvaBottom:
      Result := 'bottom';
  end;
end;

function MSBorderStyleToStr(Style: TXlsxBorderStyle): WideString;
begin
  Result := '';
  case Style of
    xbsThin:
      Result := 'thin';
    xbsDashed:
      Result := 'dashed';
    xbsDashDot:
      Result := 'dashDot';
    xbsDashDotDot:
      Result := 'dashDotDot';
    xbsDotted:
      Result := 'dotted';
    xbsHair:
      Result := 'hair';
    xbsDouble:
      Result := 'double';
    xbsThick:
      Result := 'thick';
    xbsMedium:
      Result := 'medium';
    xbsMediumDashed:
      Result := 'mediumDashed';
    xbsMediumDashDot:
      Result := 'mediumDashDot';
    xbsSlantDashDot:
      Result := 'slantDashDot';
    xbsMediumDashDotDot:
      Result := 'mediumDashDotDot';
  end;
end;

function MSBorderStyleToStr(Style: TDocxBorderStyle): WideString; overload;
begin
  Result := '';
  case Style of
    dbsSingle:
      Result := 'single';
    dbsThick:
      Result := 'thick';
    dbsDouble:
      Result := 'double';
    dbsHairline:
      Result := 'hairline';
    dbsDot:
      Result := 'dot';
    dbsDashLargeGap:
      Result := 'dashLargeGap';
    dbsDotDash:
      Result := 'dotDash';
    dbsDotDotDash:
      Result := 'dotDotDash';
    dbsTriple:
      Result := 'triple';
    dbsThinThickSmallGap:
      Result := 'thinThickSmallGap';
    dbsThickThinSmallGap:
      Result := 'thickThinSmallGap';
    dbsThinThickThinSmallGap:
      Result := 'thinThickThinSmallGap';
    dbsThinThickMediumGap:
      Result := 'thinThickMediumGap';
    dbsThickThinMediumGap:
      Result := 'thickThinMediumGap';
    dbsThinThickThinMediumGap:
      Result := 'thinThickThinMediumGap';
    dbsThinThickLargeGap:
      Result := 'thinThickLargeGap';
    dbsThickThinLargeGap:
      Result := 'thickThinLargeGap';
    dbsThinThickThinLargeGap:
      Result := 'thinThickThinLargeGap';
    dbsWave:
      Result := 'wave';
    dbsDoubleWave:
      Result := 'doubleWave';
    dbsDashSmallGap:
      Result := 'dashSmallGap';
    dbsDashDotStroker:
      Result := 'dashDotStroker';
    dbsEmboss3D:
      Result := 'emboss3D';
    dbsEngrave3D:
      Result := 'engrave3D';
  end;
end;

end.
