Line data Source code
1 : /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef GFX_FT2UTILS_H
7 : #define GFX_FT2UTILS_H
8 :
9 : #include "cairo-ft.h"
10 : #include "gfxFT2FontBase.h"
11 : #include "mozilla/Likely.h"
12 :
13 : // Rounding and truncation functions for a FreeType fixed point number
14 : // (FT26Dot6) stored in a 32bit integer with high 26 bits for the integer
15 : // part and low 6 bits for the fractional part.
16 : #define FLOAT_FROM_26_6(x) ((x) / 64.0)
17 : #define FLOAT_FROM_16_16(x) ((x) / 65536.0)
18 : #define ROUND_26_6_TO_INT(x) ((x) >= 0 ? ((32 + (x)) >> 6) \
19 : : -((32 - (x)) >> 6))
20 :
21 : typedef struct FT_FaceRec_* FT_Face;
22 :
23 : class gfxFT2LockedFace {
24 : public:
25 90 : explicit gfxFT2LockedFace(gfxFT2FontBase *aFont) :
26 : mGfxFont(aFont),
27 90 : mFace(cairo_ft_scaled_font_lock_face(aFont->CairoScaledFont()))
28 90 : { }
29 90 : ~gfxFT2LockedFace()
30 90 : {
31 90 : if (mFace) {
32 90 : cairo_ft_scaled_font_unlock_face(mGfxFont->CairoScaledFont());
33 : }
34 90 : }
35 :
36 6 : FT_Face get() { return mFace; };
37 :
38 : /**
39 : * Get the glyph id for a Unicode character representable by a single
40 : * glyph, or return zero if there is no such glyph. This does no caching,
41 : * so you probably want gfxFcFont::GetGlyph.
42 : */
43 : uint32_t GetGlyph(uint32_t aCharCode);
44 : /**
45 : * Returns 0 if there is no variation selector cmap subtable.
46 : */
47 : uint32_t GetUVSGlyph(uint32_t aCharCode, uint32_t aVariantSelector);
48 :
49 : void GetMetrics(gfxFont::Metrics* aMetrics, uint32_t* aSpaceGlyph);
50 :
51 : // A scale factor for use in converting horizontal metrics from font units
52 : // to pixels.
53 8 : gfxFloat XScale()
54 : {
55 8 : if (MOZ_UNLIKELY(!mFace))
56 0 : return 0.0;
57 :
58 8 : const FT_Size_Metrics& ftMetrics = mFace->size->metrics;
59 :
60 8 : if (FT_IS_SCALABLE(mFace)) {
61 : // Prefer FT_Size_Metrics::x_scale to x_ppem as x_ppem does not
62 : // have subpixel accuracy.
63 : //
64 : // FT_Size_Metrics::x_scale is in 16.16 fixed point format. Its
65 : // (fractional) value is a factor that converts vertical metrics
66 : // from design units to units of 1/64 pixels, so that the result
67 : // may be interpreted as pixels in 26.6 fixed point format.
68 8 : return FLOAT_FROM_26_6(FLOAT_FROM_16_16(ftMetrics.x_scale));
69 : }
70 :
71 : // Not scalable.
72 : // FT_Size_Metrics doc says x_scale is "only relevant for scalable
73 : // font formats".
74 0 : return gfxFloat(ftMetrics.x_ppem) / gfxFloat(mFace->units_per_EM);
75 : }
76 :
77 : protected:
78 : /**
79 : * Get extents for a simple character representable by a single glyph.
80 : * The return value is the glyph id of that glyph or zero if no such glyph
81 : * exists. aExtents is only set when this returns a non-zero glyph id.
82 : */
83 : uint32_t GetCharExtents(char aChar, cairo_text_extents_t* aExtents);
84 :
85 : typedef FT_UInt (*CharVariantFunction)(FT_Face face,
86 : FT_ULong charcode,
87 : FT_ULong variantSelector);
88 : CharVariantFunction FindCharVariantFunction();
89 :
90 : RefPtr<gfxFT2FontBase> mGfxFont;
91 : FT_Face mFace;
92 : };
93 :
94 : #endif /* GFX_FT2UTILS_H */
|