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

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

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