[GTK] Turn off antialiasing when rendering with Ahem (v2)
https://bugs.webkit.org/show_bug.cgi?id=204671

Reviewed by Carlos Garcia Campos.

Fix leak caused by r254567 where a RefPtr its created from a FcPattern without adoptRef().
Meanwhile at it, also change defaultFontconfigOptions() to return a RefPtr,
because after r254567 we always modify the pattern.
Change also the FontPlatformData() constructor to take an rvalue reference,
and some of its callers to move the RefPtr, avoiding extra not needed
reference increments/decrements.

Covered by existing tests.

* platform/graphics/FontPlatformData.h:
* platform/graphics/freetype/FontCacheFreeType.cpp:
(WebCore::FontCache::systemFallbackForCharacters):
(WebCore::FontCache::createFontPlatformData):
* platform/graphics/freetype/FontCustomPlatformDataFreeType.cpp:
(WebCore::defaultFontconfigOptions):
(WebCore::FontCustomPlatformData::fontPlatformData):
* platform/graphics/freetype/FontPlatformDataFreeType.cpp:
(WebCore::FontPlatformData::FontPlatformData):

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@254741 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/ChangeLog b/Source/WebCore/ChangeLog
index 07a31cd..337b12c 100644
--- a/Source/WebCore/ChangeLog
+++ b/Source/WebCore/ChangeLog
@@ -1,3 +1,29 @@
+2020-01-16  Carlos Alberto Lopez Perez  <clopez@igalia.com>
+
+        [GTK] Turn off antialiasing when rendering with Ahem (v2)
+        https://bugs.webkit.org/show_bug.cgi?id=204671
+
+        Reviewed by Carlos Garcia Campos.
+
+        Fix leak caused by r254567 where a RefPtr its created from a FcPattern without adoptRef().
+        Meanwhile at it, also change defaultFontconfigOptions() to return a RefPtr,
+        because after r254567 we always modify the pattern.
+        Change also the FontPlatformData() constructor to take an rvalue reference,
+        and some of its callers to move the RefPtr, avoiding extra not needed
+        reference increments/decrements.
+
+        Covered by existing tests.
+
+        * platform/graphics/FontPlatformData.h:
+        * platform/graphics/freetype/FontCacheFreeType.cpp:
+        (WebCore::FontCache::systemFallbackForCharacters):
+        (WebCore::FontCache::createFontPlatformData):
+        * platform/graphics/freetype/FontCustomPlatformDataFreeType.cpp:
+        (WebCore::defaultFontconfigOptions):
+        (WebCore::FontCustomPlatformData::fontPlatformData):
+        * platform/graphics/freetype/FontPlatformDataFreeType.cpp:
+        (WebCore::FontPlatformData::FontPlatformData):
+
 2020-01-16  Yusuke Suzuki  <ysuzuki@apple.com>
 
         Compact sizeof(HTMLAnchorElement) and sizeof(HTMLLinkElement)
diff --git a/Source/WebCore/platform/graphics/FontPlatformData.h b/Source/WebCore/platform/graphics/FontPlatformData.h
index 24b2c96..d38d4dd 100644
--- a/Source/WebCore/platform/graphics/FontPlatformData.h
+++ b/Source/WebCore/platform/graphics/FontPlatformData.h
@@ -111,7 +111,7 @@
 #endif
 
 #if USE(FREETYPE)
-    FontPlatformData(cairo_font_face_t*, FcPattern*, float size, bool fixedWidth, bool syntheticBold, bool syntheticOblique, FontOrientation);
+    FontPlatformData(cairo_font_face_t*, RefPtr<FcPattern>&&, float size, bool fixedWidth, bool syntheticBold, bool syntheticOblique, FontOrientation);
 #endif
 
 #if PLATFORM(WIN)
diff --git a/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp b/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp
index 854a65b..4590e61 100644
--- a/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp
+++ b/Source/WebCore/platform/graphics/freetype/FontCacheFreeType.cpp
@@ -272,7 +272,7 @@
     getFontPropertiesFromPattern(resultPattern.get(), description, fixedWidth, syntheticBold, syntheticOblique);
 
     RefPtr<cairo_font_face_t> fontFace = adoptRef(cairo_ft_font_face_create_for_pattern(resultPattern.get()));
-    FontPlatformData alternateFontData(fontFace.get(), resultPattern.get(), description.computedPixelSize(), fixedWidth, syntheticBold, syntheticOblique, description.orientation());
+    FontPlatformData alternateFontData(fontFace.get(), WTFMove(resultPattern), description.computedPixelSize(), fixedWidth, syntheticBold, syntheticOblique, description.orientation());
     return fontForPlatformData(alternateFontData);
 }
 
@@ -579,7 +579,7 @@
             FcPatternAddString(resultPattern.get(), FC_FONT_VARIATIONS, reinterpret_cast<const FcChar8*>(variants.utf8().data()));
     }
 #endif
-    auto platformData = makeUnique<FontPlatformData>(fontFace.get(), resultPattern.get(), fontDescription.computedPixelSize(), fixedWidth, syntheticBold, syntheticOblique, fontDescription.orientation());
+    auto platformData = makeUnique<FontPlatformData>(fontFace.get(), WTFMove(resultPattern), fontDescription.computedPixelSize(), fixedWidth, syntheticBold, syntheticOblique, fontDescription.orientation());
     // Verify that this font has an encoding compatible with Fontconfig. Fontconfig currently
     // supports three encodings in FcFreeTypeCharIndex: Unicode, Symbol and AppleRoman.
     // If this font doesn't have one of these three encodings, don't select it.
diff --git a/Source/WebCore/platform/graphics/freetype/FontCustomPlatformDataFreeType.cpp b/Source/WebCore/platform/graphics/freetype/FontCustomPlatformDataFreeType.cpp
index 8c84163..297d329 100644
--- a/Source/WebCore/platform/graphics/freetype/FontCustomPlatformDataFreeType.cpp
+++ b/Source/WebCore/platform/graphics/freetype/FontCustomPlatformDataFreeType.cpp
@@ -56,7 +56,7 @@
         reinterpret_cast<cairo_destroy_func_t>(reinterpret_cast<void(*)(void)>(FT_Done_Face)));
 }
 
-static FcPattern* defaultFontconfigOptions()
+static RefPtr<FcPattern> defaultFontconfigOptions()
 {
     // Get some generic default settings from fontconfig for web fonts. Strategy
     // from Behdad Esfahbod in https://code.google.com/p/chromium/issues/detail?id=173207#c35
@@ -72,14 +72,14 @@
         FcPatternDel(pattern, FC_FAMILY);
         FcConfigSubstitute(nullptr, pattern, FcMatchFont);
     }, pattern);
-    return pattern;
+    return adoptRef(FcPatternDuplicate(pattern));
 }
 
 FontPlatformData FontCustomPlatformData::fontPlatformData(const FontDescription& description, bool bold, bool italic, const FontFeatureSettings&, FontSelectionSpecifiedCapabilities)
 {
     auto* freeTypeFace = static_cast<FT_Face>(cairo_font_face_get_user_data(m_fontFace.get(), &freeTypeFaceKey));
     ASSERT(freeTypeFace);
-    RefPtr<FcPattern> pattern = FcPatternDuplicate(defaultFontconfigOptions());
+    RefPtr<FcPattern> pattern = defaultFontconfigOptions();
     FcPatternAddString(pattern.get(), FC_FAMILY, reinterpret_cast<const FcChar8*>(freeTypeFace->family_name));
 #if ENABLE(VARIATION_FONTS)
     auto variants = buildVariationSettings(freeTypeFace, description);
@@ -87,7 +87,7 @@
         FcPatternAddString(pattern.get(), FC_FONT_VARIATIONS, reinterpret_cast<const FcChar8*>(variants.utf8().data()));
     }
 #endif
-    return FontPlatformData(m_fontFace.get(), pattern.get(), description.computedPixelSize(), freeTypeFace->face_flags & FT_FACE_FLAG_FIXED_WIDTH, bold, italic, description.orientation());
+    return FontPlatformData(m_fontFace.get(), WTFMove(pattern), description.computedPixelSize(), freeTypeFace->face_flags & FT_FACE_FLAG_FIXED_WIDTH, bold, italic, description.orientation());
 }
 
 static bool initializeFreeTypeLibrary(FT_Library& library)
diff --git a/Source/WebCore/platform/graphics/freetype/FontPlatformDataFreeType.cpp b/Source/WebCore/platform/graphics/freetype/FontPlatformDataFreeType.cpp
index 16d5d93..450cda0 100644
--- a/Source/WebCore/platform/graphics/freetype/FontPlatformDataFreeType.cpp
+++ b/Source/WebCore/platform/graphics/freetype/FontPlatformDataFreeType.cpp
@@ -111,10 +111,10 @@
 #endif
 }
 
-FontPlatformData::FontPlatformData(cairo_font_face_t* fontFace, FcPattern* pattern, float size, bool fixedWidth, bool syntheticBold, bool syntheticOblique, FontOrientation orientation)
+FontPlatformData::FontPlatformData(cairo_font_face_t* fontFace, RefPtr<FcPattern>&& pattern, float size, bool fixedWidth, bool syntheticBold, bool syntheticOblique, FontOrientation orientation)
     : FontPlatformData(size, syntheticBold, syntheticOblique, orientation)
 {
-    m_pattern = pattern;
+    m_pattern = WTFMove(pattern);
     m_fixedWidth = fixedWidth;
 
     buildScaledFont(fontFace);