ActivateFontsGtk.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * Copyright (C) 2005, 2006 Apple Computer, Inc. All rights reserved.
  3. * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
  4. * Copyright (C) 2010 Igalia S.L.
  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. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  16. * its contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  20. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  26. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "config.h"
  31. #include "ActivateFonts.h"
  32. #include "InjectedBundleUtilities.h"
  33. #include <fontconfig/fontconfig.h>
  34. #include <gtk/gtk.h>
  35. #include <wtf/gobject/GlibUtilities.h>
  36. #include <wtf/gobject/GOwnPtr.h>
  37. namespace WTR {
  38. void initializeGtkSettings()
  39. {
  40. GtkSettings* settings = gtk_settings_get_default();
  41. if (!settings)
  42. return;
  43. g_object_set(settings,
  44. "gtk-xft-dpi", 98304,
  45. "gtk-xft-antialias", 1,
  46. "gtk-xft-hinting", 0,
  47. "gtk-font-name", "Liberation Sans 12",
  48. "gtk-theme-name", "Raleigh",
  49. "gtk-icon-theme-name", "gnome",
  50. "gtk-xft-rgba", "none", NULL);
  51. }
  52. CString getOutputDir()
  53. {
  54. const char* webkitOutputDir = g_getenv("WEBKITOUTPUTDIR");
  55. if (webkitOutputDir)
  56. return webkitOutputDir;
  57. CString topLevelPath = WTR::topLevelPath();
  58. GOwnPtr<char> outputDir(g_build_filename(topLevelPath.data(), "WebKitBuild", NULL));
  59. return outputDir.get();
  60. }
  61. static CString getFontsPath()
  62. {
  63. CString webkitOutputDir = getOutputDir();
  64. GOwnPtr<char> fontsPath(g_build_filename(webkitOutputDir.data(), "Dependencies", "Root", "webkitgtk-test-fonts", NULL));
  65. if (g_file_test(fontsPath.get(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
  66. return fontsPath.get();
  67. // Try alternative fonts path.
  68. fontsPath.set(g_build_filename(webkitOutputDir.data(), "webkitgtk-test-fonts", NULL));
  69. if (g_file_test(fontsPath.get(), static_cast<GFileTest>(G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR)))
  70. return fontsPath.get();
  71. return CString();
  72. }
  73. void inititializeFontConfigSetting()
  74. {
  75. FcInit();
  76. // If a test resulted a font being added or removed via the @font-face rule, then
  77. // we want to reset the FontConfig configuration to prevent it from affecting other tests.
  78. static int numFonts = 0;
  79. FcFontSet* appFontSet = FcConfigGetFonts(0, FcSetApplication);
  80. if (appFontSet && numFonts && appFontSet->nfont == numFonts)
  81. return;
  82. // Load our configuration file, which sets up proper aliases for family
  83. // names like sans, serif and monospace.
  84. FcConfig* config = FcConfigCreate();
  85. GOwnPtr<gchar> fontConfigFilename(g_build_filename(FONTS_CONF_DIR, "fonts.conf", NULL));
  86. if (!g_file_test(fontConfigFilename.get(), G_FILE_TEST_IS_REGULAR))
  87. g_error("Cannot find fonts.conf at %s\n", fontConfigFilename.get());
  88. if (!FcConfigParseAndLoad(config, reinterpret_cast<FcChar8*>(fontConfigFilename.get()), true))
  89. g_error("Couldn't load font configuration file from: %s", fontConfigFilename.get());
  90. CString fontsPath = getFontsPath();
  91. if (fontsPath.isNull())
  92. g_error("Could not locate test fonts at %s. Is WEBKIT_TOP_LEVEL set?", fontsPath.data());
  93. GOwnPtr<GDir> fontsDirectory(g_dir_open(fontsPath.data(), 0, 0));
  94. while (const char* directoryEntry = g_dir_read_name(fontsDirectory.get())) {
  95. if (!g_str_has_suffix(directoryEntry, ".ttf") && !g_str_has_suffix(directoryEntry, ".otf"))
  96. continue;
  97. GOwnPtr<gchar> fontPath(g_build_filename(fontsPath.data(), directoryEntry, NULL));
  98. if (!FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(fontPath.get())))
  99. g_error("Could not load font at %s!", fontPath.get());
  100. }
  101. // Ahem is used by many layout tests.
  102. GOwnPtr<gchar> ahemFontFilename(g_build_filename(FONTS_CONF_DIR, "AHEM____.TTF", NULL));
  103. if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(ahemFontFilename.get())))
  104. g_error("Could not load font at %s!", ahemFontFilename.get());
  105. static const char* fontFilenames[] = {
  106. "WebKitWeightWatcher100.ttf",
  107. "WebKitWeightWatcher200.ttf",
  108. "WebKitWeightWatcher300.ttf",
  109. "WebKitWeightWatcher400.ttf",
  110. "WebKitWeightWatcher500.ttf",
  111. "WebKitWeightWatcher600.ttf",
  112. "WebKitWeightWatcher700.ttf",
  113. "WebKitWeightWatcher800.ttf",
  114. "WebKitWeightWatcher900.ttf",
  115. 0
  116. };
  117. for (size_t i = 0; fontFilenames[i]; ++i) {
  118. GOwnPtr<gchar> fontFilename(g_build_filename(FONTS_CONF_DIR, "..", "..", "fonts", fontFilenames[i], NULL));
  119. if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(fontFilename.get())))
  120. g_error("Could not load font at %s!", fontFilename.get());
  121. }
  122. // A font with no valid Fontconfig encoding to test https://bugs.webkit.org/show_bug.cgi?id=47452
  123. GOwnPtr<gchar> fontWithNoValidEncodingFilename(g_build_filename(FONTS_CONF_DIR, "FontWithNoValidEncoding.fon", NULL));
  124. if (!FcConfigAppFontAddFile(config, reinterpret_cast<FcChar8*>(fontWithNoValidEncodingFilename.get())))
  125. g_error("Could not load font at %s!", fontWithNoValidEncodingFilename.get());
  126. if (!FcConfigSetCurrent(config))
  127. g_error("Could not set the current font configuration!");
  128. numFonts = FcConfigGetFonts(config, FcSetApplication)->nfont;
  129. }
  130. void activateFonts()
  131. {
  132. initializeGtkSettings();
  133. inititializeFontConfigSetting();
  134. }
  135. }