PageRenderTime 62ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/fop-core/src/main/java/org/apache/fop/fonts/autodetect/WindowsFontDirFinder.java

https://github.com/apache/fop
Java | 120 lines | 80 code | 7 blank | 33 comment | 18 complexity | 8fe1a155a42d60e71888f76897f0c655 MD5 | raw file
Possible License(s): 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. /* $Id$ */
  18. package org.apache.fop.fonts.autodetect;
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStreamReader;
  23. import java.util.List;
  24. import org.apache.commons.io.IOUtils;
  25. /**
  26. * FontFinder for native Windows platforms
  27. */
  28. public class WindowsFontDirFinder implements FontDirFinder {
  29. /**
  30. * Attempts to read windir environment variable on windows
  31. * (disclaimer: This is a bit dirty but seems to work nicely)
  32. */
  33. private String getWinDir(String osName) throws IOException {
  34. Process process = null;
  35. Runtime runtime = Runtime.getRuntime();
  36. if (osName.startsWith("Windows 9")) {
  37. process = runtime.exec("command.com /c echo %windir%");
  38. } else {
  39. process = runtime.exec("cmd.exe /c echo %windir%");
  40. }
  41. InputStreamReader isr = null;
  42. BufferedReader bufferedReader = null;
  43. String dir = "";
  44. try {
  45. isr = new InputStreamReader(process.getInputStream());
  46. bufferedReader = new BufferedReader(isr);
  47. dir = bufferedReader.readLine();
  48. } finally {
  49. IOUtils.closeQuietly(bufferedReader);
  50. IOUtils.closeQuietly(isr);
  51. }
  52. return dir;
  53. }
  54. /**
  55. * {@inheritDoc}
  56. * @return a list of detected font files
  57. */
  58. public List<File> find() {
  59. List<File> fontDirList = new java.util.ArrayList<File>();
  60. String windir = null;
  61. try {
  62. windir = System.getProperty("env.windir");
  63. } catch (SecurityException e) {
  64. // should continue if this fails
  65. }
  66. String osName = System.getProperty("os.name");
  67. if (windir == null) {
  68. try {
  69. windir = getWinDir(osName);
  70. } catch (IOException e) {
  71. // should continue if this fails
  72. }
  73. }
  74. File osFontsDir = null;
  75. File psFontsDir = null;
  76. if (windir != null) {
  77. // remove any trailing '/'
  78. if (windir.endsWith("/")) {
  79. windir = windir.substring(0, windir.length() - 1);
  80. }
  81. osFontsDir = new File(windir + File.separator + "FONTS");
  82. if (osFontsDir.exists() && osFontsDir.canRead()) {
  83. fontDirList.add(osFontsDir);
  84. }
  85. psFontsDir = new File(windir.substring(0, 2) + File.separator + "PSFONTS");
  86. if (psFontsDir.exists() && psFontsDir.canRead()) {
  87. fontDirList.add(psFontsDir);
  88. }
  89. } else {
  90. String windowsDirName = osName.endsWith("NT") ? "WINNT" : "WINDOWS";
  91. // look for true type font folder
  92. for (char driveLetter = 'C'; driveLetter <= 'E'; driveLetter++) {
  93. osFontsDir = new File(
  94. driveLetter + ":"
  95. + File.separator + windowsDirName
  96. + File.separator + "FONTS");
  97. if (osFontsDir.exists() && osFontsDir.canRead()) {
  98. fontDirList.add(osFontsDir);
  99. break;
  100. }
  101. }
  102. // look for type 1 font folder
  103. for (char driveLetter = 'C'; driveLetter <= 'E'; driveLetter++) {
  104. psFontsDir = new File(driveLetter + ":" + File.separator + "PSFONTS");
  105. if (psFontsDir.exists() && psFontsDir.canRead()) {
  106. fontDirList.add(psFontsDir);
  107. break;
  108. }
  109. }
  110. }
  111. return fontDirList;
  112. }
  113. }