Repo for the search and displace ingest module that takes odf, docx and pdf and transforms it into .md to be used with search and displace operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
6.9 KiB

3 years ago
  1. unit Options;
  2. interface
  3. uses
  4. Classes, SysUtils, ImagingTypes, IniFiles;
  5. type
  6. TForcedOutputFormat = (
  7. fofNone,
  8. fofBinary1,
  9. fofGray8,
  10. fofRgb24,
  11. fofRgba32
  12. );
  13. TFileFormat = (
  14. ffSameAsInput,
  15. ffPng,
  16. ffJpeg,
  17. ffTiff,
  18. ffBmp,
  19. ffPsd,
  20. ffTga,
  21. ffJng,
  22. ffPpm
  23. );
  24. { TOptions }
  25. TOptions = class
  26. private
  27. FFiles: TStrings;
  28. function GetEffectiveExecutablePath: string;
  29. function GetOutputFilePath(const InputFilePath: string): string;
  30. public
  31. // Basic options
  32. DefaultOutputFileOptions: Boolean;
  33. OutputFolder: string;
  34. OutputFileFormat: TFileFormat;
  35. BackgroundColor: TColor32;
  36. // Advanced options
  37. MaxAngle: Double;
  38. ThresholdLevel: Integer;
  39. ForcedOutputFormat: TForcedOutputFormat;
  40. SkipAngle: Double;
  41. JpegCompressionQuality: Integer;
  42. TiffCompressionScheme: Integer;
  43. DefaultExecutable: Boolean;
  44. CustomExecutablePath: string;
  45. constructor Create;
  46. destructor Destroy; override;
  47. procedure ToCmdLineParameters(AParams: TStrings; AFileIndex: Integer);
  48. procedure SaveToIni(Ini: TIniFile);
  49. procedure LoadFromIni(Ini: TIniFile);
  50. procedure Reset;
  51. property Files: TStrings read FFiles;
  52. property EffectiveExecutablePath: string read GetEffectiveExecutablePath;
  53. end;
  54. implementation
  55. uses
  56. ImagingUtility, Utils;
  57. const
  58. DefaultBackgroundColor = $FFFFFFFF; // white
  59. DefaultMaxAngle = 10.0;
  60. DefaultSkipAngle = 0.01;
  61. DefaultThresholdLevel = -1; // auto
  62. DefaultJpegCompressionQuality = -1; // use imaginglib default
  63. DefaultTiffCompressionScheme = -1; // use imaginglib default
  64. DefaultOutputFileNamePrefix = 'deskewed-';
  65. FileExts: array[TFileFormat] of string = (
  66. '', // ffSameAsInput
  67. 'png', // ffPng
  68. 'jpg', // ffJpeg
  69. 'tif', // ffTiff
  70. 'bmp', // ffBmp
  71. 'psd', // ffPsd
  72. 'tga', // ffTga
  73. 'jng', // ffJng
  74. 'ppm' // ffPpm
  75. );
  76. FormatIds: array[TForcedOutputFormat] of string = (
  77. '', // fofNone,
  78. 'b1', // fofBinary1
  79. 'g8', // fofGray8
  80. 'rgb24', // fofRgb24
  81. 'rgba32' // fofRgba32
  82. );
  83. IniSectionOptions = 'Options';
  84. IniSectionAdvanced = 'Advanced';
  85. { TOptions }
  86. constructor TOptions.Create;
  87. begin
  88. FFiles := TStringList.Create;
  89. Reset;
  90. end;
  91. destructor TOptions.Destroy;
  92. begin
  93. FFiles.Free;
  94. inherited Destroy;
  95. end;
  96. function TOptions.GetEffectiveExecutablePath: string;
  97. begin
  98. if DefaultExecutable then
  99. Result := Utils.FindDeskewExePath
  100. else
  101. Result := CustomExecutablePath;
  102. end;
  103. function TOptions.GetOutputFilePath(const InputFilePath: string): string;
  104. var
  105. FileName: string;
  106. begin
  107. FileName := ExtractFileName(InputFilePath);
  108. if DefaultOutputFileOptions then
  109. begin
  110. Result := ExtractFilePath(InputFilePath) + DefaultOutputFileNamePrefix + FileName;
  111. end
  112. else
  113. begin
  114. if OutputFileFormat <> ffSameAsInput then
  115. FileName := ChangeFileExt(FileName, '.' + FileExts[OutputFileFormat]);
  116. Result := IncludeTrailingPathDelimiter(OutputFolder) + FileName;
  117. // Try to avoid overwriting existing file (in case in-folder = out-folder)
  118. if FileExists(Result) then
  119. Result := IncludeTrailingPathDelimiter(OutputFolder) + DefaultOutputFileNamePrefix + FileName;
  120. end;
  121. end;
  122. procedure TOptions.ToCmdLineParameters(AParams: TStrings; AFileIndex: Integer);
  123. function FloatToStrFmt(const F: Double): string;
  124. begin
  125. Result := Format('%.2f', [F], ImagingUtility.GetFormatSettingsForFloats);
  126. end;
  127. begin
  128. Assert(AFileIndex < FFiles.Count);
  129. AParams.Clear;
  130. AParams.AddStrings(['-o', GetOutputFilePath(FFiles[AFileIndex])]);
  131. if BackgroundColor <> $FF000000 then
  132. AParams.AddStrings(['-b', IntToHex(BackgroundColor, 8)]);
  133. // Advanced options
  134. if not SameFloat(MaxAngle, DefaultMaxAngle, 0.1) then
  135. AParams.AddStrings(['-a', FloatToStrFmt(MaxAngle)]);
  136. if not SameFloat(SkipAngle, DefaultSkipAngle, 0.01) then
  137. AParams.AddStrings(['-l', FloatToStrFmt(SkipAngle)]);
  138. if ForcedOutputFormat <> fofNone then
  139. AParams.AddStrings(['-f', FormatIds[ForcedOutputFormat]]);
  140. {$IFDEF DEBUG}
  141. AParams.AddStrings(['-s', 'p']);
  142. {$ENDIF}
  143. AParams.Add(FFiles[AFileIndex]);
  144. end;
  145. procedure TOptions.SaveToIni(Ini: TIniFile);
  146. begin
  147. Ini.WriteString(IniSectionOptions, 'DefaultOutputFileOptions', BoolToStr(DefaultOutputFileOptions, True));
  148. Ini.WriteString(IniSectionOptions, 'OutputFolder', OutputFolder);
  149. Ini.WriteString(IniSectionOptions, 'OutputFileFormat', TEnumUtils<TFileFormat>.EnumToStr(OutputFileFormat));
  150. Ini.WriteString(IniSectionOptions, 'BackgroundColor', ColorToString(BackgroundColor));
  151. Ini.WriteFloat(IniSectionAdvanced, 'MaxAngle', MaxAngle);
  152. Ini.WriteInteger(IniSectionAdvanced, 'ThresholdLevel', ThresholdLevel);
  153. Ini.WriteString(IniSectionAdvanced, 'ForcedOutputFormat', TEnumUtils<TForcedOutputFormat>.EnumToStr(ForcedOutputFormat));
  154. Ini.WriteFloat(IniSectionAdvanced, 'SkipAngle', SkipAngle);
  155. Ini.WriteInteger(IniSectionAdvanced, 'JpegCompressionQuality', JpegCompressionQuality);
  156. Ini.WriteInteger(IniSectionAdvanced, 'TiffCompressionScheme', TiffCompressionScheme);
  157. Ini.WriteString(IniSectionAdvanced, 'DefaultExecutable', BoolToStr(DefaultExecutable, True));
  158. Ini.WriteString(IniSectionAdvanced, 'CustomExecutablePath', CustomExecutablePath);
  159. end;
  160. procedure TOptions.LoadFromIni(Ini: TIniFile);
  161. begin
  162. DefaultOutputFileOptions := StrToBoolDef(Ini.ReadString(IniSectionOptions, 'DefaultOutputFileOptions', ''), True);
  163. OutputFolder := Ini.ReadString(IniSectionOptions, 'OutputFolder', '');
  164. OutputFileFormat := TEnumUtils<TFileFormat>.StrToEnum(Ini.ReadString(IniSectionOptions, 'OutputFileFormat', ''));
  165. BackgroundColor := StringToColorDef(Ini.ReadString(IniSectionOptions, 'BackgroundColor', ''), DefaultBackgroundColor);
  166. MaxAngle := Ini.ReadFloat(IniSectionAdvanced, 'MaxAngle', DefaultMaxAngle);
  167. ThresholdLevel := Ini.ReadInteger(IniSectionAdvanced, 'ThresholdLevel', DefaultThresholdLevel);
  168. ForcedOutputFormat := TEnumUtils<TForcedOutputFormat>.StrToEnum(Ini.ReadString(IniSectionAdvanced, 'ForcedOutputFormat', ''));
  169. SkipAngle := Ini.ReadFloat(IniSectionAdvanced, 'SkipAngle', DefaultSkipAngle);
  170. JpegCompressionQuality := Ini.ReadInteger(IniSectionAdvanced, 'JpegCompressionQuality', DefaultJpegCompressionQuality);
  171. TiffCompressionScheme := Ini.ReadInteger(IniSectionAdvanced, 'TiffCompressionScheme', DefaultTiffCompressionScheme);
  172. DefaultExecutable := StrToBoolDef(Ini.ReadString(IniSectionAdvanced, 'DefaultExecutable', ''), True);
  173. CustomExecutablePath := Ini.ReadString(IniSectionAdvanced, 'CustomExecutablePath', '');
  174. end;
  175. procedure TOptions.Reset;
  176. begin
  177. DefaultOutputFileOptions := True;
  178. OutputFolder := '';
  179. OutputFileFormat := ffSameAsInput;
  180. BackgroundColor := DefaultBackgroundColor;
  181. MaxAngle := DefaultMaxAngle;
  182. ThresholdLevel := DefaultThresholdLevel;
  183. ForcedOutputFormat := fofNone;
  184. SkipAngle := DefaultSkipAngle;
  185. JpegCompressionQuality := DefaultJpegCompressionQuality;
  186. TiffCompressionScheme := DefaultTiffCompressionScheme;
  187. DefaultExecutable := True;
  188. CustomExecutablePath := '';
  189. end;
  190. end.