Update required FreeType to 2.8.1

Drop support for FreeType older than 2.8.1. Remove dynamic loading
checks and constant definitions which are now known to exist. Update
includes to no longer require macros. Changes to take advantage of
thread safety will be done later.

Change-Id: Idf82147cad0df8315c6737e878ec6750537c7b28
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/429898
Reviewed-by: Herb Derby <herb@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
diff --git a/src/ports/SkFontHost_FreeType.cpp b/src/ports/SkFontHost_FreeType.cpp
index 0304538..fa40e14 100644
--- a/src/ports/SkFontHost_FreeType.cpp
+++ b/src/ports/SkFontHost_FreeType.cpp
@@ -26,6 +26,7 @@
 #include "src/core/SkMask.h"
 #include "src/core/SkMaskGamma.h"
 #include "src/core/SkScalerContext.h"
+#include "src/core/SkTSearch.h"
 #include "src/ports/SkFontHost_FreeType_common.h"
 #include "src/sfnt/SkOTUtils.h"
 #include "src/utils/SkCallableTraits.h"
@@ -35,21 +36,21 @@
 #include <tuple>
 
 #include <ft2build.h>
-#include FT_ADVANCES_H
-#include FT_BITMAP_H
-#ifdef FT_COLOR_H
-#   include FT_COLOR_H
+#include <freetype/ftadvanc.h>
+#include <freetype/ftbitmap.h>
+#ifdef FT_COLOR_H  // 2.10.0
+#   include <freetype/ftcolor.h>
 #endif
-#include FT_FREETYPE_H
-#include FT_LCD_FILTER_H
-#include FT_MODULE_H
-#include FT_MULTIPLE_MASTERS_H
-#include FT_OUTLINE_H
-#include FT_SIZES_H
-#include FT_SYSTEM_H
-#include FT_TRUETYPE_TABLES_H
-#include FT_TYPE1_TABLES_H
-#include FT_XFREE86_H
+#include <freetype/freetype.h>
+#include <freetype/ftlcdfil.h>
+#include <freetype/ftmodapi.h>
+#include <freetype/ftmm.h>
+#include <freetype/ftoutln.h>
+#include <freetype/ftsizes.h>
+#include <freetype/ftsystem.h>
+#include <freetype/tttables.h>
+#include <freetype/t1tables.h>
+#include <freetype/ftfntfmt.h>
 
 // SK_FREETYPE_MINIMUM_RUNTIME_VERSION 0x<major><minor><patch><flags>
 // Flag SK_FREETYPE_DLOPEN: also try dlopen to get newer features.
@@ -58,33 +59,13 @@
 #  if defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) || defined (SK_BUILD_FOR_GOOGLE3)
 #    define SK_FREETYPE_MINIMUM_RUNTIME_VERSION (((FREETYPE_MAJOR) << 24) | ((FREETYPE_MINOR) << 16) | ((FREETYPE_PATCH) << 8))
 #  else
-#    define SK_FREETYPE_MINIMUM_RUNTIME_VERSION ((2 << 24) | (3 << 16) | (11 << 8) | (SK_FREETYPE_DLOPEN))
+#    define SK_FREETYPE_MINIMUM_RUNTIME_VERSION ((2 << 24) | (8 << 16) | (1 << 8) | (SK_FREETYPE_DLOPEN))
 #  endif
 #endif
 #if SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN
 #  include <dlfcn.h>
 #endif
 
-// FT_LOAD_COLOR and the corresponding FT_Pixel_Mode::FT_PIXEL_MODE_BGRA
-// were introduced in FreeType 2.5.0.
-// The following may be removed once FreeType 2.5.0 is required to build.
-#ifndef FT_LOAD_COLOR
-#    define FT_LOAD_COLOR ( 1L << 20 )
-#    define FT_PIXEL_MODE_BGRA 7
-#endif
-
-// FT_LOAD_BITMAP_METRICS_ONLY was introduced in FreeType 2.7.1
-// The following may be removed once FreeType 2.7.1 is required to build.
-#ifndef FT_LOAD_BITMAP_METRICS_ONLY
-#    define FT_LOAD_BITMAP_METRICS_ONLY ( 1L << 22 )
-#endif
-
-// FT_VAR_AXIS_FLAG_HIDDEN was introduced in FreeType 2.8.1
-// The variation axis should not be exposed to user interfaces.
-#ifndef FT_VAR_AXIS_FLAG_HIDDEN
-#    define FT_VAR_AXIS_FLAG_HIDDEN 1
-#endif
-
 //#define ENABLE_GLYPH_SPEW     // for tracing calls
 //#define DUMP_STRIKE_CREATION
 //#define SK_FONTHOST_FREETYPE_RUNTIME_VERSION
@@ -129,98 +110,17 @@
 
 class FreeTypeLibrary : SkNoncopyable {
 public:
-    FreeTypeLibrary()
-        : fGetVarDesignCoordinates(nullptr)
-        , fGetVarAxisFlags(nullptr)
-        , fLibrary(nullptr)
-        , fIsLCDSupported(false)
-        , fLightHintingIsYOnly(false)
-        , fLCDExtra(0)
-    {
+    FreeTypeLibrary() : fLibrary(nullptr) {
         if (FT_New_Library(&gFTMemory, &fLibrary)) {
             return;
         }
         FT_Add_Default_Modules(fLibrary);
-
-        // When using dlsym
-        // *(void**)(&procPtr) = dlsym(self, "proc");
-        // is non-standard, but safe for POSIX. Cannot write
-        // *reinterpret_cast<void**>(&procPtr) = dlsym(self, "proc");
-        // because clang has not implemented DR573. See http://clang.llvm.org/cxx_dr_status.html .
-
-        using Version = std::tuple<FT_Int, FT_Int, FT_Int>;
-        Version version;
-        FT_Library_Version(fLibrary, &std::get<0>(version),
-                                     &std::get<1>(version),
-                                     &std::get<2>(version));
-
-#if SK_FREETYPE_MINIMUM_RUNTIME_VERSION >= 0x02070100
-        fGetVarDesignCoordinates = FT_Get_Var_Design_Coordinates;
-#elif SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN
-        if (Version(2,7,0) <= version) {
-            //The FreeType library is already loaded, so symbols are available in process.
-            void* self = dlopen(nullptr, RTLD_LAZY);
-            if (self) {
-                *(void**)(&fGetVarDesignCoordinates) = dlsym(self, "FT_Get_Var_Design_Coordinates");
-                dlclose(self);
-            }
-        }
-#endif
-
-#if SK_FREETYPE_MINIMUM_RUNTIME_VERSION >= 0x02070200
         FT_Set_Default_Properties(fLibrary);
-#elif SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN
-        if (Version(2,7,1) <= version) {
-            //The FreeType library is already loaded, so symbols are available in process.
-            void* self = dlopen(nullptr, RTLD_LAZY);
-            if (self) {
-                FT_Set_Default_PropertiesProc setDefaultProperties;
-                *(void**)(&setDefaultProperties) = dlsym(self, "FT_Set_Default_Properties");
-                dlclose(self);
 
-                if (setDefaultProperties) {
-                    setDefaultProperties(fLibrary);
-                }
-            }
-        }
-#endif
-
-// The 'light' hinting is vertical only starting in 2.8.0.
-#if SK_FREETYPE_MINIMUM_RUNTIME_VERSION >= 0x02080000
-        fLightHintingIsYOnly = true;
-#else
-        if (Version(2,8,0) <= version) {
-            fLightHintingIsYOnly = true;
-        }
-#endif
-
-
-#if SK_FREETYPE_MINIMUM_RUNTIME_VERSION >= 0x02080100
-        fGetVarAxisFlags = FT_Get_Var_Axis_Flags;
-#elif SK_FREETYPE_MINIMUM_RUNTIME_VERSION & SK_FREETYPE_DLOPEN
-        if (Version(2,7,0) <= version) {
-            //The FreeType library is already loaded, so symbols are available in process.
-            void* self = dlopen(nullptr, RTLD_LAZY);
-            if (self) {
-                *(void**)(&fGetVarAxisFlags) = dlsym(self, "FT_Get_Var_Axis_Flags");
-                dlclose(self);
-            }
-        }
-#endif
-
-        fIsLCDSupported =
-            // Subpixel anti-aliasing may be unfiltered until the LCD filter is set.
-            // Newer versions may still need this, so this test with side effects must come first.
-            // The default has changed over time, so this doesn't mean the same thing to all users.
-            (FT_Library_SetLcdFilter(fLibrary, FT_LCD_FILTER_DEFAULT) == 0) ||
-
-            // In 2.8.1 and later FreeType always provides some form of subpixel anti-aliasing.
-            ((SK_FREETYPE_MINIMUM_RUNTIME_VERSION) >= 0x02080100) ||
-            (Version(2,8,1) <= version);
-
-        if (fIsLCDSupported) {
-            fLCDExtra = 2; // Using a filter may require up to one full pixel to each side.
-        }
+        // Subpixel anti-aliasing may be unfiltered until the LCD filter is set.
+        // Newer versions may still need this, so this test with side effects must come first.
+        // The default has changed over time, so this doesn't mean the same thing to all users.
+        FT_Library_SetLcdFilter(fLibrary, FT_LCD_FILTER_DEFAULT);
     }
     ~FreeTypeLibrary() {
         if (fLibrary) {
@@ -229,42 +129,31 @@
     }
 
     FT_Library library() { return fLibrary; }
-    bool isLCDSupported() { return fIsLCDSupported; }
-    int lcdExtra() { return fLCDExtra; }
-    bool lightHintingIsYOnly() { return fLightHintingIsYOnly; }
-
-    // FT_Get_{MM,Var}_{Blend,Design}_Coordinates were added in FreeType 2.7.1.
-    // Prior to this there was no way to get the coordinates out of the FT_Face.
-    // This wasn't too bad because you needed to specify them anyway, and the clamp was provided.
-    // However, this doesn't work when face_index specifies named variations as introduced in 2.6.1.
-    using FT_Get_Var_Blend_CoordinatesProc = FT_Error (*)(FT_Face, FT_UInt, FT_Fixed*);
-    FT_Get_Var_Blend_CoordinatesProc fGetVarDesignCoordinates;
-
-    // FT_Get_Var_Axis_Flags was introduced in FreeType 2.8.1
-    // Get the ‘flags’ field of an OpenType Variation Axis Record.
-    // Not meaningful for Adobe MM fonts (‘*flags’ is always zero).
-    using FT_Get_Var_Axis_FlagsProc = FT_Error (*)(FT_MM_Var*, FT_UInt, FT_UInt*);
-    FT_Get_Var_Axis_FlagsProc fGetVarAxisFlags;
 
 private:
     FT_Library fLibrary;
-    bool fIsLCDSupported;
-    bool fLightHintingIsYOnly;
-    int fLCDExtra;
 
-    // FT_Library_SetLcdFilterWeights was introduced in FreeType 2.4.0.
-    // The following platforms provide FreeType of at least 2.4.0.
-    // Ubuntu >= 11.04 (previous deprecated April 2013)
-    // Debian >= 6.0 (good)
-    // OpenSuse >= 11.4 (previous deprecated January 2012 / Nov 2013 for Evergreen 11.2)
-    // Fedora >= 14 (good)
-    // Android >= Gingerbread (good)
-    // RHEL >= 7 (6 has 2.3.11, EOL Nov 2020, Phase 3 May 2017)
-    using FT_Library_SetLcdFilterWeightsProc = FT_Error (*)(FT_Library, unsigned char*);
+    // FT_Library_SetLcdFilterWeights 2.4.0
+    // FT_LOAD_COLOR 2.5.0
+    // FT_Pixel_Mode::FT_PIXEL_MODE_BGRA 2.5.0
+    // Thread safety in 2.6.0
+    // freetype/ftfntfmt.h (rename) 2.6.0
+    // Direct header inclusion 2.6.1
+    // FT_Get_Var_Design_Coordinates 2.7.1
+    // FT_LOAD_BITMAP_METRICS_ONLY 2.7.1
+    // FT_Set_Default_Properties 2.7.2
+    // The 'light' hinting is vertical only from 2.8.0
+    // FT_Get_Var_Axis_Flags 2.8.1
+    // FT_VAR_AXIS_FLAG_HIDDEN was introduced in FreeType 2.8.1
+    // --------------------
+    // FT_Done_MM_Var 2.9.0 (Currenty setting ft_free to a known allocator.)
+    // freetype/ftcolor.h 2.10.0 (Currently assuming if compiled with FT_COLOR_H runtime available.)
 
-    // FreeType added the ability to read global properties in 2.7.0. After 2.7.1 a means for users
-    // of FT_New_Library to request these global properties to be read was added.
-    using FT_Set_Default_PropertiesProc = void (*)(FT_Library);
+    // Ubuntu 18.04       2.8.1
+    // Debian 10          2.9.1
+    // openSUSE Leap 15.2 2.10.1
+    // Fedora 32          2.10.4
+    // RHEL 8             2.9.1
 };
 
 static SkMutex& f_t_mutex() {
@@ -282,16 +171,6 @@
     FT_StreamRec fFTStream;
     std::unique_ptr<SkStreamAsset> fSkStream;
 
-    // FreeType prior to 2.7.1 does not implement retreiving variation design metrics.
-    // Cache the variation design metrics used to create the font if the user specifies them.
-    SkAutoSTMalloc<4, SkFixed> fAxes;
-    int fAxesCount;
-
-    // FreeType from 2.6.1 (14d6b5d7) until 2.7.0 (ee3f36f6b38) uses font_index for both font index
-    // and named variation index on input, but masks the named variation index part on output.
-    // Manually keep track of when a named variation is requested for 2.6.1 until 2.7.1.
-    bool fNamedVariationSpecified;
-
     static std::unique_ptr<FaceRec> Make(const SkTypeface_FreeType* typeface);
     ~FaceRec();
 
@@ -351,7 +230,7 @@
 }
 
 SkTypeface_FreeType::FaceRec::FaceRec(std::unique_ptr<SkStreamAsset> stream)
-        : fSkStream(std::move(stream)), fAxesCount(0), fNamedVariationSpecified(false)
+        : fSkStream(std::move(stream))
 {
     sk_bzero(&fFTStream, sizeof(fFTStream));
     fFTStream.size = fSkStream->getLength();
@@ -376,7 +255,6 @@
 
     // If a named variation is requested, don't overwrite the named variation's position.
     if (data.getIndex() > 0xFFFF) {
-        fNamedVariationSpecified = true;
         return;
     }
 
@@ -405,12 +283,6 @@
                  rec->fFace->family_name);
         return;
     }
-
-    fAxesCount = data.getAxisCount();
-    fAxes.reset(fAxesCount);
-    for (int i = 0; i < fAxesCount; ++i) {
-        fAxes[i] = data.getAxis()[i];
-    }
 }
 
 // Will return nullptr on failure
@@ -476,11 +348,6 @@
     }
 
     FT_Face face() { return fFaceRec ? fFaceRec->fFace.get() : nullptr; }
-    int getAxesCount() { return fFaceRec ? fFaceRec->fAxesCount : 0; }
-    SkFixed* getAxes() { return fFaceRec ? fFaceRec->fAxes.get() : nullptr; }
-    bool isNamedVariationSpecified() {
-        return fFaceRec ? fFaceRec->fNamedVariationSpecified : false;
-    }
 
 private:
     SkTypeface_FreeType::FaceRec* fFaceRec;
@@ -746,25 +613,12 @@
     }
 
     SkAutoSTMalloc<4, FT_Fixed> coords(variations->num_axis);
-    // FT_Get_{MM,Var}_{Blend,Design}_Coordinates were added in FreeType 2.7.1.
-    if (gFTLibrary->fGetVarDesignCoordinates &&
-        !gFTLibrary->fGetVarDesignCoordinates(face, variations->num_axis, coords.get()))
-    {
-        for (FT_UInt i = 0; i < variations->num_axis; ++i) {
-            coordinates[i].axis = variations->axis[i].tag;
-            coordinates[i].value = SkFixedToScalar(coords[i]);
-        }
-    } else if (static_cast<FT_UInt>(fta.getAxesCount()) == variations->num_axis) {
-        for (FT_UInt i = 0; i < variations->num_axis; ++i) {
-            coordinates[i].axis = variations->axis[i].tag;
-            coordinates[i].value = SkFixedToScalar(fta.getAxes()[i]);
-        }
-    } else if (fta.isNamedVariationSpecified()) {
-        // The font has axes, they cannot be retrieved, and some named axis was specified.
+    if (FT_Get_Var_Design_Coordinates(face, variations->num_axis, coords.get())) {
         return -1;
-    } else {
-        // The font has axes, they cannot be retrieved, but no named instance was specified.
-        return 0;
+    }
+    for (FT_UInt i = 0; i < variations->num_axis; ++i) {
+        coordinates[i].axis = variations->axis[i].tag;
+        coordinates[i].value = SkFixedToScalar(coords[i]);
     }
 
     return variations->num_axis;
@@ -805,15 +659,6 @@
         rec->fTextSize = SkIntToScalar(1 << 14);
     }
 
-    if (isLCD(*rec)) {
-        SkAutoMutexExclusive ama(f_t_mutex());
-        // getFaceRec creates the FaceRec if needed, which ensures that the gFTLibrary exists.
-        if (this->getFaceRec() && !gFTLibrary->isLCDSupported()) {
-            // If the runtime Freetype library doesn't support LCD, disable it here.
-            rec->fMaskFormat = SkMask::kA8_Format;
-        }
-    }
-
     SkFontHinting h = rec->getHinting();
     if (SkFontHinting::kFull == h && !isLCD(*rec)) {
         // collapse full->normal hinting if we're not doing LCD
@@ -948,9 +793,7 @@
                 break;
             case SkFontHinting::kSlight:
                 loadFlags = FT_LOAD_TARGET_LIGHT;  // This implies FORCE_AUTOHINT
-                if (gFTLibrary->lightHintingIsYOnly()) {
-                    linearMetrics = true;
-                }
+                linearMetrics = true;
                 break;
             case SkFontHinting::kNormal:
                 loadFlags = FT_LOAD_TARGET_NORMAL;
@@ -1202,11 +1045,11 @@
 void SkScalerContext_FreeType::updateGlyphIfLCD(SkGlyph* glyph) {
     if (glyph->fMaskFormat == SkMask::kLCD16_Format) {
         if (fLCDIsVert) {
-            glyph->fHeight += gFTLibrary->lcdExtra();
-            glyph->fTop -= gFTLibrary->lcdExtra() >> 1;
+            glyph->fHeight += 2;
+            glyph->fTop -= 1;
         } else {
-            glyph->fWidth += gFTLibrary->lcdExtra();
-            glyph->fLeft -= gFTLibrary->lcdExtra() >> 1;
+            glyph->fWidth += 2;
+            glyph->fLeft -= 1;
         }
     }
 }
@@ -1818,9 +1661,8 @@
         parameters[i].def = SkFixedToScalar(variations->axis[i].def);
         parameters[i].max = SkFixedToScalar(variations->axis[i].maximum);
         FT_UInt flags = 0;
-        bool hidden = gFTLibrary->fGetVarAxisFlags &&
-            !gFTLibrary->fGetVarAxisFlags(variations, i, &flags) &&
-            (flags & FT_VAR_AXIS_FLAG_HIDDEN);
+        bool hidden = !FT_Get_Var_Axis_Flags(variations, i, &flags) &&
+                      (flags & FT_VAR_AXIS_FLAG_HIDDEN);
         parameters[i].setHidden(hidden);
     }
 
@@ -1934,6 +1776,7 @@
         return;
     }
     FT_Add_Default_Modules(fLibrary);
+    FT_Set_Default_Properties(fLibrary);
 }
 SkTypeface_FreeType::Scanner::~Scanner() {
     if (fLibrary) {
@@ -1988,7 +1831,6 @@
     return true;
 }
 
-#include "src/core/SkTSearch.h"
 bool SkTypeface_FreeType::Scanner::scanFont(
     SkStreamAsset* stream, int ttcIndex,
     SkString* name, SkFontStyle* style, bool* isFixedPitch, AxisDefinitions* axes) const