Wednesday, November 23, 2016

Coding Latinizer

Steps to create MyNotepad's user interface:
. Start Delphi and Select File-New Application.
. Place one Memo, OpenDialog, SaveDialog two Buttons on a form.
. Rename Button1 to btnOpen, Button2 to btnSave.

 Coding

1. Use Object Inspector to assign the following code to the FormCreate event:

procedure TForm1.FormCreate(Sender: TObject);
begin
with OpenDialog1 do begin
Options:=Options+[ofPathMustExist,ofFileMustExist];
InitialDir:=ExtractFilePath(Application.ExeName);
Filter:='Text files (*.txt)|*.txt';
end;
with SaveDialog1 do begin
InitialDir:=ExtractFilePath(Application.ExeName);
Filter:='Text files (*.txt)|*.txt';
end;
Memo1.ScrollBars := ssBoth;
end;
This code sets some of the Open dialog properties as discussed in the beginning of the article.

2. Add this code for the Onclick event of btnOpen and btnSave buttons:

procedure TForm1.btnOpenClick(Sender: TObject);
begin
if OpenDialog1.Execute then begin
Form1.Caption := OpenDialog1.FileName;
Memo1.Lines.LoadFromFile
(OpenDialog1.FileName);
Memo1.SelStart := 0;
end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  selectedFile: string;
  dlg: TOpenDialog;
begin
  selectedFile := '';
  dlg := TOpenDialog.Create(nil);
  try
    dlg.InitialDir := 'C:\';
    dlg.Filter := 'All files (*.*)|*.*';
    if dlg.Execute(Handle) then
      selectedFile := dlg.FileName;
  finally
    dlg.Free;
  end;

  if selectedFile <> '' then
    <your code here to handle the selected file>
end;





procedure TForm1.btnSaveClick(Sender: TObject);
begin
SaveDialog1.FileName := Form1.Caption;
if SaveDialog1.Execute then begin
Memo1.Lines.SaveToFile
(SaveDialog1.FileName + '.txt');
Form1.Caption:=SaveDialog1.FileName;
end;
end;
Run your project. You cannot believe it; files are opening and saving just like with the "real" Notepad.















//----------------------


function Latinizer(s: string) : string;
var r: string;
begin
   r := s;
   r := StringReplace(r,'а','a',[rfReplaceAll]);
   r := StringReplace(r,'А','A',[rfReplaceAll]);
   r := StringReplace(r,'б','b',[rfReplaceAll]);
   r := StringReplace(r,'Б','B',[rfReplaceAll]);
   r := StringReplace(r,'в','v',[rfReplaceAll]);
   r := StringReplace(r,'В','V',[rfReplaceAll]);
   r := StringReplace(r,'г','g',[rfReplaceAll]);
   r := StringReplace(r,'Г','G',[rfReplaceAll]);
   r := StringReplace(r,'д','d',[rfReplaceAll]);
   r := StringReplace(r,'Д','D',[rfReplaceAll]);
   r := StringReplace(r,'ђ','dj',[rfReplaceAll]);
   r := StringReplace(r,'Ђ','Dj',[rfReplaceAll]);
   r := StringReplace(r,'љ','lj',[rfReplaceAll]);
   r := StringReplace(r,'Љ','Lj',[rfReplaceAll]);
   r := StringReplace(r,'њ','nj',[rfReplaceAll]);
   r := StringReplace(r,'Њ','Nj',[rfReplaceAll]);
   r := StringReplace(r,'р','r',[rfReplaceAll]);
   r := StringReplace(r,'Р','R',[rfReplaceAll]);
   r := StringReplace(r,'т','t',[rfReplaceAll]);
   r := StringReplace(r,'Т','T',[rfReplaceAll]);
   r := StringReplace(r,'з','z',[rfReplaceAll]);
   r := StringReplace(r,'З','Z',[rfReplaceAll]);
   r := StringReplace(r,'у','u',[rfReplaceAll]);
   r := StringReplace(r,'У','U',[rfReplaceAll]);
   r := StringReplace(r,'и','i',[rfReplaceAll]);
   r := StringReplace(r,'И','I',[rfReplaceAll]);
   r := StringReplace(r,'о','o',[rfReplaceAll]);
   r := StringReplace(r,'О','O',[rfReplaceAll]);
   r := StringReplace(r,'п','p',[rfReplaceAll]);
   r := StringReplace(r,'П','P',[rfReplaceAll]);
   r := StringReplace(r,'ш','s',[rfReplaceAll]);
   r := StringReplace(r,'Ш','S',[rfReplaceAll]);
   r := StringReplace(r,'ч','c',[rfReplaceAll]);
   r := StringReplace(r,'Ч','C',[rfReplaceAll]);
   r := StringReplace(r,'ћ','c',[rfReplaceAll]);
   r := StringReplace(r,'Ћ','C',[rfReplaceAll]);
   r := StringReplace(r,'ж','z',[rfReplaceAll]);
   r := StringReplace(r,'Ж','Z',[rfReplaceAll]);
   r := StringReplace(r,'с','s',[rfReplaceAll]);
   r := StringReplace(r,'С','S',[rfReplaceAll]);
   r := StringReplace(r,'ф','F',[rfReplaceAll]);
   r := StringReplace(r,'Ф','f',[rfReplaceAll]);
   r := StringReplace(r,'х','h',[rfReplaceAll]);
   r := StringReplace(r,'Х','H',[rfReplaceAll]);
   r := StringReplace(r,'ј','j',[rfReplaceAll]);
   r := StringReplace(r,'Ј','J',[rfReplaceAll]);
   r := StringReplace(r,'к','k',[rfReplaceAll]);
   r := StringReplace(r,'К','K',[rfReplaceAll]);
   r := StringReplace(r,'л','l',[rfReplaceAll]);
   r := StringReplace(r,'Л','L',[rfReplaceAll]);
   r := StringReplace(r,'џ','dz',[rfReplaceAll]);
   r := StringReplace(r,'Џ','Dz',[rfReplaceAll]);
   r := StringReplace(r,'ц','c',[rfReplaceAll]);
   r := StringReplace(r,'Ц','C',[rfReplaceAll]);
   r := StringReplace(r,'н','n',[rfReplaceAll]);
   r := StringReplace(r,'Н','N',[rfReplaceAll]);
   r := StringReplace(r,'м','m',[rfReplaceAll]);
   r := StringReplace(r,'М','M',[rfReplaceAll]);
   result := r ;
end;

Thursday, November 17, 2016

hta

ghostscript img reduce in pdf

The elephant in the room are the embedded bitmaps, which can blow up the file size really fast. I have a 400 page book manuscript with lots of molecular structures rendered at 600 dpi (for print), and the overall file size is 50 Mb.
I just googled around for a way to reduce the resolution of embedded images and found that ghostscript can do this:
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=small.pdf big.pdf
The level of compression is adjusted by the -dPDFSETTINGS switch:
-dPDFSETTINGS=/screen   (screen-view-only quality, 72 dpi images)
-dPDFSETTINGS=/ebook    (low quality, 150 dpi images)
-dPDFSETTINGS=/printer  (high quality, 300 dpi images)
-dPDFSETTINGS=/prepress (high quality, color preserving, 300 dpi imgs)
-dPDFSETTINGS=/default  (almost identical to /screen)
With my file, the down-sampled images produced with the /screen and /ebook settings look very coarse, but the output of the /printer setting looks very nice on screen, while still bringing the file size down from 50 to 13 Mb. Hyperlinks were preserved. So, this seems to be straightforward and viable option.