PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/docx4j/fonts/fop/fonts/autodetect/WindowsFontDirFinder.java

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