PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/java/org/apache/fop/fonts/autodetect/WindowsFontDirFinder.java

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