michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef GFX_FT2UTILS_H michael@0: #define GFX_FT2UTILS_H michael@0: michael@0: #include "cairo-ft.h" michael@0: #include "gfxFT2FontBase.h" michael@0: #include "mozilla/Likely.h" michael@0: michael@0: // Rounding and truncation functions for a FreeType fixed point number michael@0: // (FT26Dot6) stored in a 32bit integer with high 26 bits for the integer michael@0: // part and low 6 bits for the fractional part. michael@0: #define FLOAT_FROM_26_6(x) ((x) / 64.0) michael@0: #define FLOAT_FROM_16_16(x) ((x) / 65536.0) michael@0: #define ROUND_26_6_TO_INT(x) ((x) >= 0 ? ((32 + (x)) >> 6) \ michael@0: : -((32 - (x)) >> 6)) michael@0: michael@0: typedef struct FT_FaceRec_* FT_Face; michael@0: michael@0: class gfxFT2LockedFace { michael@0: public: michael@0: gfxFT2LockedFace(gfxFT2FontBase *aFont) : michael@0: mGfxFont(aFont), michael@0: mFace(cairo_ft_scaled_font_lock_face(aFont->CairoScaledFont())) michael@0: { } michael@0: ~gfxFT2LockedFace() michael@0: { michael@0: if (mFace) { michael@0: cairo_ft_scaled_font_unlock_face(mGfxFont->CairoScaledFont()); michael@0: } michael@0: } michael@0: michael@0: FT_Face get() { return mFace; }; michael@0: michael@0: /** michael@0: * Get the glyph id for a Unicode character representable by a single michael@0: * glyph, or return zero if there is no such glyph. This does no caching, michael@0: * so you probably want gfxFcFont::GetGlyph. michael@0: */ michael@0: uint32_t GetGlyph(uint32_t aCharCode); michael@0: /** michael@0: * Returns 0 if there is no variation selector cmap subtable. michael@0: */ michael@0: uint32_t GetUVSGlyph(uint32_t aCharCode, uint32_t aVariantSelector); michael@0: michael@0: void GetMetrics(gfxFont::Metrics* aMetrics, uint32_t* aSpaceGlyph); michael@0: michael@0: // A scale factor for use in converting horizontal metrics from font units michael@0: // to pixels. michael@0: gfxFloat XScale() michael@0: { michael@0: if (MOZ_UNLIKELY(!mFace)) michael@0: return 0.0; michael@0: michael@0: const FT_Size_Metrics& ftMetrics = mFace->size->metrics; michael@0: michael@0: if (FT_IS_SCALABLE(mFace)) { michael@0: // Prefer FT_Size_Metrics::x_scale to x_ppem as x_ppem does not michael@0: // have subpixel accuracy. michael@0: // michael@0: // FT_Size_Metrics::x_scale is in 16.16 fixed point format. Its michael@0: // (fractional) value is a factor that converts vertical metrics michael@0: // from design units to units of 1/64 pixels, so that the result michael@0: // may be interpreted as pixels in 26.6 fixed point format. michael@0: return FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.x_scale)); michael@0: } michael@0: michael@0: // Not scalable. michael@0: // FT_Size_Metrics doc says x_scale is "only relevant for scalable michael@0: // font formats". michael@0: return gfxFloat(ftMetrics.x_ppem) / gfxFloat(mFace->units_per_EM); michael@0: } michael@0: michael@0: protected: michael@0: /** michael@0: * Get extents for a simple character representable by a single glyph. michael@0: * The return value is the glyph id of that glyph or zero if no such glyph michael@0: * exists. aExtents is only set when this returns a non-zero glyph id. michael@0: */ michael@0: uint32_t GetCharExtents(char aChar, cairo_text_extents_t* aExtents); michael@0: michael@0: typedef FT_UInt (*CharVariantFunction)(FT_Face face, michael@0: FT_ULong charcode, michael@0: FT_ULong variantSelector); michael@0: CharVariantFunction FindCharVariantFunction(); michael@0: michael@0: nsRefPtr mGfxFont; michael@0: FT_Face mFace; michael@0: }; michael@0: michael@0: #endif /* GFX_FT2UTILS_H */