PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/fontbox/src/main/java/org/apache/fontbox/util/autodetect/WindowsFontDirFinder.java

https://github.com/apache/pdfbox
Java | 133 lines | 94 code | 5 blank | 34 comment | 16 complexity | 73937716a25e4ed10944317f2bc1aefa MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.fontbox.util.autodetect;
  18. import java.io.BufferedReader;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.io.InputStreamReader;
  22. import java.util.List;
  23. /**
  24. * FontFinder for native Windows platforms. This class is based on a class provided by Apache FOP. see
  25. * org.apache.fop.fonts.autodetect.WindowsFontDirFinder
  26. */
  27. public class WindowsFontDirFinder implements FontDirFinder
  28. {
  29. /**
  30. * Attempts to read windir environment variable on windows (disclaimer: This is a bit dirty but seems to work
  31. * nicely).
  32. */
  33. private String getWinDir(String osName) throws IOException
  34. {
  35. Process process;
  36. Runtime runtime = Runtime.getRuntime();
  37. if (osName.startsWith("Windows 9"))
  38. {
  39. process = runtime.exec("command.com /c echo %windir%");
  40. }
  41. else
  42. {
  43. process = runtime.exec("cmd.exe /c echo %windir%");
  44. }
  45. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
  46. process.getInputStream()));
  47. String winDir = bufferedReader.readLine();
  48. bufferedReader.close();
  49. return winDir;
  50. }
  51. /**
  52. * {@inheritDoc}
  53. *
  54. * @return a list of detected font files
  55. */
  56. @Override
  57. public List<File> find()
  58. {
  59. List<File> fontDirList = new java.util.ArrayList<File>();
  60. String windir = null;
  61. try
  62. {
  63. windir = System.getProperty("env.windir");
  64. }
  65. catch (SecurityException e)
  66. {
  67. // should continue if this fails
  68. }
  69. String osName = System.getProperty("os.name");
  70. if (windir == null)
  71. {
  72. try
  73. {
  74. windir = getWinDir(osName);
  75. }
  76. catch (IOException e)
  77. {
  78. // should continue if this fails
  79. }
  80. }
  81. File osFontsDir;
  82. File psFontsDir;
  83. if (windir != null)
  84. {
  85. // remove any trailing '/'
  86. if (windir.endsWith("/"))
  87. {
  88. windir = windir.substring(0, windir.length() - 1);
  89. }
  90. osFontsDir = new File(windir + File.separator + "FONTS");
  91. if (osFontsDir.exists() && osFontsDir.canRead())
  92. {
  93. fontDirList.add(osFontsDir);
  94. }
  95. psFontsDir = new File(windir.substring(0, 2) + File.separator + "PSFONTS");
  96. if (psFontsDir.exists() && psFontsDir.canRead())
  97. {
  98. fontDirList.add(psFontsDir);
  99. }
  100. }
  101. else
  102. {
  103. String windowsDirName = osName.endsWith("NT") ? "WINNT" : "WINDOWS";
  104. // look for true type font folder
  105. for (char driveLetter = 'C'; driveLetter <= 'E'; driveLetter++)
  106. {
  107. osFontsDir = new File(driveLetter + ":" + File.separator + windowsDirName
  108. + File.separator + "FONTS");
  109. if (osFontsDir.exists() && osFontsDir.canRead())
  110. {
  111. fontDirList.add(osFontsDir);
  112. break;
  113. }
  114. }
  115. // look for type 1 font folder
  116. for (char driveLetter = 'C'; driveLetter <= 'E'; driveLetter++)
  117. {
  118. psFontsDir = new File(driveLetter + ":" + File.separator + "PSFONTS");
  119. if (psFontsDir.exists() && psFontsDir.canRead())
  120. {
  121. fontDirList.add(psFontsDir);
  122. break;
  123. }
  124. }
  125. }
  126. return fontDirList;
  127. }
  128. }