1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/thebes/gfxFT2Utils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,94 @@ 1.4 +/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef GFX_FT2UTILS_H 1.10 +#define GFX_FT2UTILS_H 1.11 + 1.12 +#include "cairo-ft.h" 1.13 +#include "gfxFT2FontBase.h" 1.14 +#include "mozilla/Likely.h" 1.15 + 1.16 +// Rounding and truncation functions for a FreeType fixed point number 1.17 +// (FT26Dot6) stored in a 32bit integer with high 26 bits for the integer 1.18 +// part and low 6 bits for the fractional part. 1.19 +#define FLOAT_FROM_26_6(x) ((x) / 64.0) 1.20 +#define FLOAT_FROM_16_16(x) ((x) / 65536.0) 1.21 +#define ROUND_26_6_TO_INT(x) ((x) >= 0 ? ((32 + (x)) >> 6) \ 1.22 + : -((32 - (x)) >> 6)) 1.23 + 1.24 +typedef struct FT_FaceRec_* FT_Face; 1.25 + 1.26 +class gfxFT2LockedFace { 1.27 +public: 1.28 + gfxFT2LockedFace(gfxFT2FontBase *aFont) : 1.29 + mGfxFont(aFont), 1.30 + mFace(cairo_ft_scaled_font_lock_face(aFont->CairoScaledFont())) 1.31 + { } 1.32 + ~gfxFT2LockedFace() 1.33 + { 1.34 + if (mFace) { 1.35 + cairo_ft_scaled_font_unlock_face(mGfxFont->CairoScaledFont()); 1.36 + } 1.37 + } 1.38 + 1.39 + FT_Face get() { return mFace; }; 1.40 + 1.41 + /** 1.42 + * Get the glyph id for a Unicode character representable by a single 1.43 + * glyph, or return zero if there is no such glyph. This does no caching, 1.44 + * so you probably want gfxFcFont::GetGlyph. 1.45 + */ 1.46 + uint32_t GetGlyph(uint32_t aCharCode); 1.47 + /** 1.48 + * Returns 0 if there is no variation selector cmap subtable. 1.49 + */ 1.50 + uint32_t GetUVSGlyph(uint32_t aCharCode, uint32_t aVariantSelector); 1.51 + 1.52 + void GetMetrics(gfxFont::Metrics* aMetrics, uint32_t* aSpaceGlyph); 1.53 + 1.54 + // A scale factor for use in converting horizontal metrics from font units 1.55 + // to pixels. 1.56 + gfxFloat XScale() 1.57 + { 1.58 + if (MOZ_UNLIKELY(!mFace)) 1.59 + return 0.0; 1.60 + 1.61 + const FT_Size_Metrics& ftMetrics = mFace->size->metrics; 1.62 + 1.63 + if (FT_IS_SCALABLE(mFace)) { 1.64 + // Prefer FT_Size_Metrics::x_scale to x_ppem as x_ppem does not 1.65 + // have subpixel accuracy. 1.66 + // 1.67 + // FT_Size_Metrics::x_scale is in 16.16 fixed point format. Its 1.68 + // (fractional) value is a factor that converts vertical metrics 1.69 + // from design units to units of 1/64 pixels, so that the result 1.70 + // may be interpreted as pixels in 26.6 fixed point format. 1.71 + return FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.x_scale)); 1.72 + } 1.73 + 1.74 + // Not scalable. 1.75 + // FT_Size_Metrics doc says x_scale is "only relevant for scalable 1.76 + // font formats". 1.77 + return gfxFloat(ftMetrics.x_ppem) / gfxFloat(mFace->units_per_EM); 1.78 + } 1.79 + 1.80 +protected: 1.81 + /** 1.82 + * Get extents for a simple character representable by a single glyph. 1.83 + * The return value is the glyph id of that glyph or zero if no such glyph 1.84 + * exists. aExtents is only set when this returns a non-zero glyph id. 1.85 + */ 1.86 + uint32_t GetCharExtents(char aChar, cairo_text_extents_t* aExtents); 1.87 + 1.88 + typedef FT_UInt (*CharVariantFunction)(FT_Face face, 1.89 + FT_ULong charcode, 1.90 + FT_ULong variantSelector); 1.91 + CharVariantFunction FindCharVariantFunction(); 1.92 + 1.93 + nsRefPtr<gfxFT2FontBase> mGfxFont; 1.94 + FT_Face mFace; 1.95 +}; 1.96 + 1.97 +#endif /* GFX_FT2UTILS_H */