Announcement

Collapse
No announcement yet.

Register Font

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Register Font

    Hello.

    I have created my 'programs pack' using AMS. I managed to make it install and use custom fonts even if the target PC doesn't have them, look:

    obtenerCarpetaFuentes = Shell.GetFolder(SHF_FONTS);
    File.Copy("AutoPlay\\Fonts\\Adorable.TTF", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\Angelina.TTF", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\Base 02.ttf", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\BIRTH OF A HERO.ttf", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\Complete in Him.ttf", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\Eight Track Program 3.ttf", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\Eurostile.ttf", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\Freestyle Script.TTF", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\Intruder AOE.TTF", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\MaiandraGD.TTF", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\Meiryo UI.TTC", obtenerCarpetaFuentes);
    File.Copy("AutoPlay\\Fonts\\Meiryo.TTC", obtenerCarpetaFuentes);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Ador able.TTF", "Adorable", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Ange lina.TTF", "Angelina", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Base 02.ttf", "Base 02", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\BIRT H OF A HERO.ttf", "Birth of a Hero", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Comp lete in Him.ttf", "Complete in Him", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Eigh t Track Program 3.ttf", "Eight Track Program 3", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Euro stile.ttf", "Eurostile", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Free style Script.TTF", "Freestyle Script", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Intr uder AOE.TTF", "Intruder AOE", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Maia ndraGD.TTF", "Maiandra GD", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Meir yo UI.TTC", "Meiryo UI", true);
    System.RegisterFont(obtenerCarpetaFuentes.."\\Meir yo.TTC", "Meiryo", true);

    The code works, but I'm suspecting it is copying the fonts over and over again everytime the app is launched. Is there a way to make the program check if these fonts already exist on the system? If they exist, then don't copy.

  • #2
    Hi and Welcome to the forum
    A simple search install fonts found -


    In the thread there is code to test if font file exists in system font folder, also note that this requires Administrative priveleges

    Code:
    FontsFolder = Shell.GetFolder(SHF_FONTS);
    does_exist = File.DoesExist(FontsFolder.."\\yufen__0.ttf");
     if does_exist  then
     return true;
    else
    do File.Copy("AutoPlay\\Fonts\\yufen__0.ttf", FontsFolder.."\\yufen__0.ttf", true, true, false, true, nil);
    System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\\yufen__0.ttf", "Yufenom", true);
     end
     end
    Cheers

    Comment


    • #3
      I tried to combine the code you suggested with my code, but it always gives me an error, it says:
      Line=85: 'end' expected (to close 'if' at line 80) near '<eof>'

      Here's what I did:

      FontsFolder = Shell.GetFolder(SHF_FONTS);
      does_exist = File.DoesExist(FontsFolder.."\\Adorable.TTF");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Adorable.TTF", FontsFolder.."\\Adorable.TTF", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Adorable.TTF", "Adorable", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\Angelina.TTF");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Angelina.TTF", FontsFolder.."\\Angelina.TTF", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Angelina.TTF", "Angelina", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\Base 02.ttf");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Base 02.ttf", FontsFolder.."\\Base 02.ttf", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Base 02.ttf", "Base 02", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\BIRTH OF A HERO.ttf");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\BIRTH OF A HERO.ttf", FontsFolder.."\\BIRTH OF A HERO.ttf", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \BIRTH OF A HERO.ttf", "Birth of a Hero", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\Complete in Him.ttf");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Complete in Him.ttf", FontsFolder.."\\Complete in Him.ttf", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Complete in Him.ttf", "Complete in Him", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\Eight Track Program 3.ttf");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Eight Track Program 3.ttf", FontsFolder.."\\Eight Track Program 3.ttf", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Eight Track Program 3.ttf", "Eight Track Program 3", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\Eurostile.ttf");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Eurostile.ttf", FontsFolder.."\\Eurostile.ttf", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Eurostile.ttf", "Eurostile", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\Freestyle Script.TTF");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Freestyle Script.TTF", FontsFolder.."\\Freestyle Script.TTF", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Freestyle Script.TTF", "Freestyle Script", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\Intruder AOE.TTF");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Intruder AOE.TTF", FontsFolder.."\\Intruder AOE.TTF", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Intruder AOE.TTF", "Intruder AOE", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\MaiandraGD.TTF");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\MaiandraGD.TTF", FontsFolder.."\\MaiandraGD.TTF", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \MaiandraGD.TTF", "Maiandra GD", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\Meiryo UI.TTC");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Meiryo UI.TTC", FontsFolder.."\\Meiryo UI.TTC", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Meiryo UI.TTC", "Meiryo UI", true);
      end
      does_exist = File.DoesExist(FontsFolder.."\\Meiryo.TTC");
      if does_exist then
      return true;
      else
      do File.Copy("AutoPlay\\Fonts\\Meiryo.TTC", FontsFolder.."\\Meiryo.TTC", true, true, false, true, nil);
      System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Meiryo.TTC", "Meiryo", true);
      end
      end

      Comment


      • #4
        You forgot the end for the do File.Copy when you copy / pasted
        eg
        Code:
        does_exist = File.DoesExist(FontsFolder.."\\Adorable.TTF");
        if does_exist then
        return true;
        else
        do File.Copy("AutoPlay\\Fonts\\Adorable.TTF", FontsFolder.."\\Adorable.TTF", true, true, false, true, nil);
        System.RegisterFont(Shell.GetFolder(SHF_FONTS).."\ \Adorable.TTF", "Adorable", true);
        end[COLOR=#008000] -- end for if[/COLOR]
        [COLOR=#0000FF]end[/COLOR][COLOR=#00FF00] [/COLOR][COLOR=#008000]-- end for do[/COLOR]
        You should also format your code as it's terrible and hard to read

        Comment


        • #5
          In my opinion, even if you correct this script, it will still look like spaghetti code. If you need to repeat some action several times, it makes sense to optimize it a bit, writing a function once, and then call this function repeatedly, passing the object to be worked on (the font, in your case) as an argument. The code becomes less redundant, more readable and maintainable.

          But before we get there, I must repeat the warning I posted in my older post. Fonts can be registered permanently for all users only by admininstrators. If a font should be installed on the target system and remain available to all users even after a reboot, then this needs to be done with admin rights. Admin rights will trigger the User Account Control dialog, and this should happen only when installing a new software on the computer. Once installed, an application should NOT, ever, request admin rights. In conclusion, fonts which must become and remain available to all users and applications on the computer should be installed via setup.

          Now, that does not mean that you cannot register a font without admin rights, because you can. Your AutoPlay Media Studio can register fonts temporarily (until the next reboot, when the font will be removed), and it is also not required to copy the font to the system's Font folder. The downside is that you cannot easily test (with AutoPlay Media Studio) if a font is already registered: If you only temporily registered a font, the file may be at the expected location, but it the PC was rebooted, the font is no longer registered. So to make sure, you would need to register again at each program start. But registering one font takes a couple of seconds, and if you have a few fonts, this can delay normal program startup. Do you understand how this is not something that should be done by the application?

          I am showing you here a sample Lua script, which will register all *.ttf files found in the "Docs" sub folder of your project. If the application is run "as administrator", then the files will be copied to the SHF_APPLICATIONDATA_COMMON folder (if not already there) and registered permanently. If the application is running with user privileges, then the fonts will be copied to SHF_APPLICATIONDATA_LOCAL (if not already there) and registered temporarily.

          This allows you to build the AutoPlay Media Studio application with normal "as invoker" settings, and if the user runs it once with the right-click "run as administrator" option, the fonts will be installed permanently. Later executions "as invoker" will not attempt to register the fonts again. So it is some kind of compromise.

          Code:
          -- Check if program is running "as administrator"
          local bIsAdmin = System.GetUserInfo().IsAdmin;
          
          -- define the installation folder for the custom fonts (C:\ProgramData on Windows 10) - note, no need to copy to C:\Windows\Fonts
          local sCommonFontFolder = Shell.GetFolder(SHF_APPLICATIONDATA_COMMON) .. "\\MyFonts\\";
          -- define the installation folder as the user's application data folder (C:\Users\[username]\AppData\Local on Windows 10)
          local sUserFontFolder = Shell.GetFolder(SHF_APPLICATIONDATA_LOCAL) .. "\\MyFonts\\";
          
          -- check folders
          if bIsAdmin then
              if not Folder.DoesExist(sCommonFontFolder) then
                  Folder.Create(sCommonFontFolder);
                  error = Application.GetLastError();
                  if (error ~= 0) then
                      Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
                      Application.Exit();
                  else
                      -- will install fonts permanently on the computer
                      sTargetFolder = sCommonFontFolder;
                  end
              end
          else
              if not Folder.DoesExist(sCommonFontFolder) then
                  if not Folder.DoesExist(sUserFontFolder) then
                      Folder.Create(sUserFontFolder);
                      error = Application.GetLastError();
                      if (error ~= 0) then
                          Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
                          Application.Exit();
                      else
                          -- will install fonts temporarily on the computer, need to register again after reboot
                          sTargetFolder = sUserFontFolder;    
                      end
                  else
                      sTargetFolder = sUserFontFolder;
                  end
              else
                  sTargetFolder = sCommonFontFolder;
              end
          end
          
          function CopyFont(sSourcePath)
              local tPath = String.SplitPath(sSourcePath);
              local sTargetPath = sTargetFolder .. tPath.Filename .. tPath.Extension;
              if not File.DoesExist(sTargetPath) then
                  File.Copy(sSourcePath, sTargetPath);
                  error = Application.GetLastError();
                  if (error ~= 0) then
                      Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
                      Application.Exit();
                  else
                      -- after copying, register the font
                      StatusDlg.SetTitle("Checking/registering fonts");
                      StatusDlg.SetMessage("Preparing your system");
                      StatusDlg.SetStatusText("This may take a moment...");
                      System.RegisterFont(sTargetPath, tPath.Filename, bIsAdmin);
                      error = Application.GetLastError();
                      if (error ~= 0) then
                          Dialog.Message("Error", _tblErrorMessages[error], MB_OK, MB_ICONEXCLAMATION);
                          Application.Exit();
                      end
                  end
              else
                  if not bIsAdmin then
                      -- file previously copied, but need to register the font
                      StatusDlg.SetTitle("Checking/registering fonts");
                      StatusDlg.SetMessage("Preparing your system");
                      StatusDlg.SetStatusText("This may take a moment...");
                      System.RegisterFont(sTargetPath, tPath.Filename, false);
                  end
              end
          end
          
          StatusDlg.Show();
          StatusDlg.SetTitle("Checking/registering fonts");
          StatusDlg.SetMessage("Preparing your system");
          StatusDlg.SetStatusText("This may take a moment...");
          StatusDlg.ShowCancelButton(false);
          StatusDlg.ShowProgressMeter(false);
          StatusDlg.SetAutoSize(true);
          
          -- scan the Docs folder for *.ttf files and register them
          local tFonts = File.Find("AutoPlay\\Docs", "*.TTF", false, false, nil, nil);
          for i = 1, #tFonts do
              -- call the custom function to copy and register TTF font
              CopyFont(tFonts[i]);
          end
          StatusDlg.Hide();
          Ulrich

          Comment


          • #6
            Originally posted by Dr. Schnellinger View Post
            Hello.


            The code works, but I'm suspecting it is copying the fonts over and over again everytime the app is launched. Is there a way to make the program check if these fonts already exist on the system? If they exist, then don't copy.
            You should be running "as administrator
            Click image for larger version

Name:	2020-05-01_064800.png
Views:	549
Size:	54.8 KB
ID:	305074 Put the fonts in a file named ( Fonts) in a folder ( AutoPlay)
            Then in (OnstartUP )
            Put the following code

            PHP Code:
            fonts_path Shell.GetFolder(SHF_FONTS);

            fonts File.Find(_SourceFolder.."\\AutoPlay\\Fonts\", "*.*", true, true, nil, nil);
            if fonts then
            for x,y in pairs (fonts) do
            File.Copy(y, fonts_path, false, false, false, true);
            end
            end 
            It will copy all fonts to your fonts folder

            Comment


            • #7
              Well... the only reason why I want to install fonts is because my program has buttons with custom fonts that are not Windows standards. If the custom font remains installed then it's good, if not, it's okay, I only need it for the program's buttons. You can download a zip to see screenshots of my program.

              So, in conclusion, should I just add another "end" as colc says or should I adopt Ulrich's code? Also, my program setting "Requested user privilege level" is set to "Highest available" I guess I should set it to "require administrator", right?

              Comment


              • #8
                Originally posted by Dr. Schnellinger View Post
                Well... the only reason why I want to install fonts is because my program has buttons with custom fonts that are not Windows standards.
                If that is the only reason, you do not need to register the font at all, because AutoPlay Media Studio takes care of that. Build your project, and run it on a PC where you know these custom fonts were not installed. You should see that the button shows the expected custom font.

                Also, my program setting "Requested user privilege level" is set to "Highest available" I guess I should set it to "require administrator", right?
                Wrong. It should be "as invoker (default)".

                Ulrich

                Comment


                • #9
                  Originally posted by Ulrich View Post

                  If that is the only reason, you do not need to register the font at all, because AutoPlay Media Studio takes care of that. Build your project, and run it on a PC where you know these custom fonts were not installed. You should see that the button shows the expected custom font.



                  Wrong. It should be "as invoker (default)".

                  Ulrich
                  It's not working. The app is not showing custom fonts.

                  Photos "A" and "C" were taken on my PC.
                  Photos "B" and "D" were taken on another PC.

                  I created a folder named "Fonts" under \AutoPlay that contains the fonts used in this project. But it's still not working.

                  The app is in a shared folder, it's called "Canaima Deployment".

                  ================================================== ==================================================

                  Also, on another theme, I'm having a problem with another project, it's called "Packed Software HeadQuarters". When I create/modify a button that launches an .exe, AMS automatically copies the .exe from its original folder to the folder AutoPlay\Docs, and it won't let me set a custom folder for the .exe. For now (As a workaround) I have to edit the .autoplay file with Notepad++, then open it with AMS and click build every time I need to update something. How do I fix this?

                  This does not happen to other projects, only to this one.

                  Comment


                  • #10
                    If you are a licensed customer and use our product legally, please associate your forum username with your customer account, which can be done on the portal ( https://www.indigorose.com/customers/ ). If you have active product support, please attach your *.apz to a support ticket so I can have a look at it and understand what is going on.

                    Ulrich

                    Comment

                    Working...
                    X
                    😀
                    🥰
                    🤢
                    😎
                    😡
                    👍
                    👎