PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 108 lines | 69 code | 6 blank | 33 comment | 18 complexity | f0d3d64ee6cafb08ec5b51697aad4697 MD5 | raw file
Possible License(s): Apache-2.0, 0BSD, IPL-1.0, LGPL-2.0, BSD-3-Clause
  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 679326 2008-07-24 09:35:34Z vhennebert $ */
  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 FontFinder {
  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 find() {
  49. List fontDirList = new java.util.ArrayList();
  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, psFontsDir = null;
  65. if (windir != null) {
  66. // remove any trailing '/'
  67. if (windir.endsWith("/")) {
  68. windir = windir.substring(0, windir.length() - 1);
  69. }
  70. osFontsDir = new File(windir + File.separator + "FONTS");
  71. if (osFontsDir.exists() && osFontsDir.canRead()) {
  72. fontDirList.add(osFontsDir);
  73. }
  74. psFontsDir = new File(windir.substring(0, 2) + File.separator + "PSFONTS");
  75. if (psFontsDir.exists() && psFontsDir.canRead()) {
  76. fontDirList.add(psFontsDir);
  77. }
  78. } else {
  79. String windowsDirName = osName.endsWith("NT") ? "WINNT" : "WINDOWS";
  80. // look for true type font folder
  81. for (char driveLetter = 'C'; driveLetter <= 'E'; driveLetter++) {
  82. osFontsDir = new File(
  83. driveLetter + ":"
  84. + File.separator + windowsDirName
  85. + File.separator + "FONTS");
  86. if (osFontsDir.exists() && osFontsDir.canRead()) {
  87. fontDirList.add(osFontsDir);
  88. break;
  89. }
  90. }
  91. // look for type 1 font folder
  92. for (char driveLetter = 'C'; driveLetter <= 'E'; driveLetter++) {
  93. psFontsDir = new File(driveLetter + ":" + File.separator + "PSFONTS");
  94. if (psFontsDir.exists() && psFontsDir.canRead()) {
  95. fontDirList.add(psFontsDir);
  96. break;
  97. }
  98. }
  99. }
  100. return fontDirList;
  101. }
  102. }