FontManagement.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2011 ProFUSION Embedded Systems
  3. * Copyright (C) 2011 Samsung Electronics
  4. * Copyright (C) 2012 Intel Corporation. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  19. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "config.h"
  27. #include "FontManagement.h"
  28. #include <Ecore_File.h>
  29. #include <cstdio>
  30. #include <fontconfig/fontconfig.h>
  31. #include <wtf/Vector.h>
  32. #include <wtf/text/CString.h>
  33. #include <wtf/text/StringBuilder.h>
  34. static CString buildPath(const char* base, const char* first, ...)
  35. {
  36. va_list ap;
  37. StringBuilder result;
  38. result.append(base);
  39. if (const char* current = first) {
  40. va_start(ap, first);
  41. do {
  42. result.append('/');
  43. result.append(current);
  44. } while ((current = va_arg(ap, const char*)));
  45. va_end(ap);
  46. }
  47. return result.toString().utf8();
  48. }
  49. static Vector<CString> getCoreFontFiles()
  50. {
  51. Vector<CString> fontFilePaths;
  52. // Ahem is used by many layout tests.
  53. fontFilePaths.append(CString(FONTS_CONF_DIR "/AHEM____.TTF"));
  54. // A font with no valid Fontconfig encoding to test https://bugs.webkit.org/show_bug.cgi?id=47452
  55. fontFilePaths.append(CString(FONTS_CONF_DIR "/FontWithNoValidEncoding.fon"));
  56. for (int i = 1; i <= 9; i++) {
  57. char fontPath[EINA_PATH_MAX];
  58. snprintf(fontPath, EINA_PATH_MAX - 1, FONTS_CONF_DIR "/../../fonts/WebKitWeightWatcher%i00.ttf", i);
  59. fontFilePaths.append(CString(fontPath));
  60. }
  61. return fontFilePaths;
  62. }
  63. static void addFontDirectory(const CString& fontDirectory, FcConfig* config)
  64. {
  65. const char* fontPath = fontDirectory.data();
  66. if (!fontPath || !FcConfigAppFontAddDir(config, reinterpret_cast<const FcChar8*>(fontPath)))
  67. fprintf(stderr, "Could not add font directory %s!\n", fontPath);
  68. }
  69. static void addFontFiles(const Vector<CString>& fontFiles, FcConfig* config)
  70. {
  71. Vector<CString>::const_iterator it, end = fontFiles.end();
  72. for (it = fontFiles.begin(); it != end; ++it) {
  73. const char* filePath = (*it).data();
  74. if (!FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(filePath)))
  75. fprintf(stderr, "Could not load font at %s!\n", filePath);
  76. }
  77. }
  78. static CString getCustomBuildDir()
  79. {
  80. if (const char* userChosenBuildDir = getenv("WEBKITOUTPUTDIR")) {
  81. if (ecore_file_is_dir(userChosenBuildDir))
  82. return userChosenBuildDir;
  83. fprintf(stderr, "WEBKITOUTPUTDIR set to '%s', but path doesn't exist.\n", userChosenBuildDir);
  84. }
  85. return CString();
  86. }
  87. static CString getPlatformFontsPath()
  88. {
  89. CString customBuildDir = getCustomBuildDir();
  90. if (!customBuildDir.isNull()) {
  91. CString fontsPath = buildPath(customBuildDir.data(), "Dependencies", "Root", "webkitgtk-test-fonts", 0);
  92. if (!ecore_file_exists(fontsPath.data()))
  93. fprintf(stderr, "WEBKITOUTPUTDIR set to '%s', but could not local test fonts.\n", customBuildDir.data());
  94. return fontsPath;
  95. }
  96. CString fontsPath = CString(DOWNLOADED_FONTS_DIR);
  97. if (ecore_file_exists(fontsPath.data()))
  98. return fontsPath;
  99. fprintf(stderr, "Could not locate tests fonts, try setting WEBKITOUTPUTDIR.\n");
  100. return CString();
  101. }
  102. void addFontsToEnvironment()
  103. {
  104. FcInit();
  105. // Load our configuration file, which sets up proper aliases for family
  106. // names like sans, serif and monospace.
  107. FcConfig* config = FcConfigCreate();
  108. const char* fontConfigFilename = FONTS_CONF_DIR "/fonts.conf";
  109. if (!FcConfigParseAndLoad(config, reinterpret_cast<const FcChar8*>(fontConfigFilename), true)) {
  110. fprintf(stderr, "Couldn't load font configuration file from: %s\n", fontConfigFilename);
  111. exit(1);
  112. }
  113. addFontFiles(getCoreFontFiles(), config);
  114. addFontDirectory(getPlatformFontsPath(), config);
  115. if (!FcConfigSetCurrent(config)) {
  116. fprintf(stderr, "Could not set the current font configuration!\n");
  117. exit(1);
  118. }
  119. }