gfxFT2FontBase.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #include "gfxFT2FontBase.h"
  6. #include "gfxFT2Utils.h"
  7. #include "harfbuzz/hb.h"
  8. #include "mozilla/Likely.h"
  9. #include "gfxFontConstants.h"
  10. #include "gfxFontUtils.h"
  11. using namespace mozilla::gfx;
  12. gfxFT2FontBase::gfxFT2FontBase(cairo_scaled_font_t *aScaledFont,
  13. gfxFontEntry *aFontEntry,
  14. const gfxFontStyle *aFontStyle)
  15. : gfxFont(aFontEntry, aFontStyle, kAntialiasDefault, aScaledFont),
  16. mSpaceGlyph(0),
  17. mHasMetrics(false)
  18. {
  19. cairo_scaled_font_reference(mScaledFont);
  20. gfxFT2LockedFace face(this);
  21. mFUnitsConvFactor = face.XScale();
  22. }
  23. gfxFT2FontBase::~gfxFT2FontBase()
  24. {
  25. cairo_scaled_font_destroy(mScaledFont);
  26. }
  27. uint32_t
  28. gfxFT2FontBase::GetGlyph(uint32_t aCharCode)
  29. {
  30. // FcFreeTypeCharIndex needs to lock the FT_Face and can end up searching
  31. // through all the postscript glyph names in the font. Therefore use a
  32. // lightweight cache, which is stored on the cairo_font_face_t.
  33. cairo_font_face_t *face =
  34. cairo_scaled_font_get_font_face(CairoScaledFont());
  35. if (cairo_font_face_status(face) != CAIRO_STATUS_SUCCESS)
  36. return 0;
  37. // This cache algorithm and size is based on what is done in
  38. // cairo_scaled_font_text_to_glyphs and pango_fc_font_real_get_glyph. I
  39. // think the concept is that adjacent characters probably come mostly from
  40. // one Unicode block. This assumption is probably not so valid with
  41. // scripts with large character sets as used for East Asian languages.
  42. struct CmapCacheSlot {
  43. uint32_t mCharCode;
  44. uint32_t mGlyphIndex;
  45. };
  46. const uint32_t kNumSlots = 256;
  47. static cairo_user_data_key_t sCmapCacheKey;
  48. CmapCacheSlot *slots = static_cast<CmapCacheSlot*>
  49. (cairo_font_face_get_user_data(face, &sCmapCacheKey));
  50. if (!slots) {
  51. // cairo's caches can keep some cairo_font_faces alive past our last
  52. // destroy, so the destroy function (free) for the cache must be
  53. // callable from cairo without any assumptions about what other
  54. // modules have not been shutdown.
  55. slots = static_cast<CmapCacheSlot*>
  56. (calloc(kNumSlots, sizeof(CmapCacheSlot)));
  57. if (!slots)
  58. return 0;
  59. cairo_status_t status =
  60. cairo_font_face_set_user_data(face, &sCmapCacheKey, slots, free);
  61. if (status != CAIRO_STATUS_SUCCESS) { // OOM
  62. free(slots);
  63. return 0;
  64. }
  65. // Invalidate slot 0 by setting its char code to something that would
  66. // never end up in slot 0. All other slots are already invalid
  67. // because they have mCharCode = 0 and a glyph for char code 0 will
  68. // always be in the slot 0.
  69. slots[0].mCharCode = 1;
  70. }
  71. CmapCacheSlot *slot = &slots[aCharCode % kNumSlots];
  72. if (slot->mCharCode != aCharCode) {
  73. slot->mCharCode = aCharCode;
  74. slot->mGlyphIndex = gfxFT2LockedFace(this).GetGlyph(aCharCode);
  75. }
  76. return slot->mGlyphIndex;
  77. }
  78. void
  79. gfxFT2FontBase::GetGlyphExtents(uint32_t aGlyph, cairo_text_extents_t* aExtents)
  80. {
  81. NS_PRECONDITION(aExtents != nullptr, "aExtents must not be NULL");
  82. cairo_glyph_t glyphs[1];
  83. glyphs[0].index = aGlyph;
  84. glyphs[0].x = 0.0;
  85. glyphs[0].y = 0.0;
  86. // cairo does some caching for us here but perhaps a small gain could be
  87. // made by caching more. It is usually only the advance that is needed,
  88. // so caching only the advance could allow many requests to be cached with
  89. // little memory use. Ideally this cache would be merged with
  90. // gfxGlyphExtents.
  91. cairo_scaled_font_glyph_extents(CairoScaledFont(), glyphs, 1, aExtents);
  92. }
  93. const gfxFont::Metrics&
  94. gfxFT2FontBase::GetHorizontalMetrics()
  95. {
  96. if (mHasMetrics)
  97. return mMetrics;
  98. if (MOZ_UNLIKELY(GetStyle()->size <= 0.0) ||
  99. MOZ_UNLIKELY(GetStyle()->sizeAdjust == 0.0)) {
  100. new(&mMetrics) gfxFont::Metrics(); // zero initialize
  101. mSpaceGlyph = GetGlyph(' ');
  102. } else {
  103. gfxFT2LockedFace face(this);
  104. face.GetMetrics(&mMetrics, &mSpaceGlyph);
  105. }
  106. SanitizeMetrics(&mMetrics, false);
  107. #if 0
  108. // printf("font name: %s %f\n", NS_ConvertUTF16toUTF8(GetName()).get(), GetStyle()->size);
  109. // printf ("pango font %s\n", pango_font_description_to_string (pango_font_describe (font)));
  110. fprintf (stderr, "Font: %s\n", NS_ConvertUTF16toUTF8(GetName()).get());
  111. fprintf (stderr, " emHeight: %f emAscent: %f emDescent: %f\n", mMetrics.emHeight, mMetrics.emAscent, mMetrics.emDescent);
  112. fprintf (stderr, " maxAscent: %f maxDescent: %f\n", mMetrics.maxAscent, mMetrics.maxDescent);
  113. fprintf (stderr, " internalLeading: %f externalLeading: %f\n", mMetrics.externalLeading, mMetrics.internalLeading);
  114. fprintf (stderr, " spaceWidth: %f aveCharWidth: %f xHeight: %f\n", mMetrics.spaceWidth, mMetrics.aveCharWidth, mMetrics.xHeight);
  115. fprintf (stderr, " uOff: %f uSize: %f stOff: %f stSize: %f\n", mMetrics.underlineOffset, mMetrics.underlineSize, mMetrics.strikeoutOffset, mMetrics.strikeoutSize);
  116. #endif
  117. mHasMetrics = true;
  118. return mMetrics;
  119. }
  120. // Get the glyphID of a space
  121. uint32_t
  122. gfxFT2FontBase::GetSpaceGlyph()
  123. {
  124. GetHorizontalMetrics();
  125. return mSpaceGlyph;
  126. }
  127. uint32_t
  128. gfxFT2FontBase::GetGlyph(uint32_t unicode, uint32_t variation_selector)
  129. {
  130. if (variation_selector) {
  131. uint32_t id =
  132. gfxFT2LockedFace(this).GetUVSGlyph(unicode, variation_selector);
  133. if (id) {
  134. return id;
  135. }
  136. unicode = gfxFontUtils::GetUVSFallback(unicode, variation_selector);
  137. if (unicode) {
  138. return GetGlyph(unicode);
  139. }
  140. return 0;
  141. }
  142. return GetGlyph(unicode);
  143. }
  144. int32_t
  145. gfxFT2FontBase::GetGlyphWidth(DrawTarget& aDrawTarget, uint16_t aGID)
  146. {
  147. cairo_text_extents_t extents;
  148. GetGlyphExtents(aGID, &extents);
  149. // convert to 16.16 fixed point
  150. return NS_lround(0x10000 * extents.x_advance);
  151. }
  152. bool
  153. gfxFT2FontBase::SetupCairoFont(DrawTarget* aDrawTarget)
  154. {
  155. // The scaled font ctm is not relevant right here because
  156. // cairo_set_scaled_font does not record the scaled font itself, but
  157. // merely the font_face, font_matrix, font_options. The scaled_font used
  158. // for the target can be different from the scaled_font passed to
  159. // cairo_set_scaled_font. (Unfortunately we have measured only for an
  160. // identity ctm.)
  161. cairo_scaled_font_t *cairoFont = CairoScaledFont();
  162. if (cairo_scaled_font_status(cairoFont) != CAIRO_STATUS_SUCCESS) {
  163. // Don't cairo_set_scaled_font as that would propagate the error to
  164. // the cairo_t, precluding any further drawing.
  165. return false;
  166. }
  167. // Thoughts on which font_options to set on the context:
  168. //
  169. // cairoFont has been created for screen rendering.
  170. //
  171. // When the context is being used for screen rendering, we should set
  172. // font_options such that the same scaled_font gets used (when the ctm is
  173. // the same). The use of explicit font_options recorded in
  174. // CreateScaledFont ensures that this will happen.
  175. //
  176. // XXXkt: For pdf and ps surfaces, I don't know whether it's better to
  177. // remove surface-specific options, or try to draw with the same
  178. // scaled_font that was used to measure. As the same font_face is being
  179. // used, its font_options will often override some values anyway (unless
  180. // perhaps we remove those from the FcPattern at face creation).
  181. //
  182. // I can't see any significant difference in printing, irrespective of
  183. // what is set here. It's too late to change things here as measuring has
  184. // already taken place. We should really be measuring with a different
  185. // font for pdf and ps surfaces (bug 403513).
  186. cairo_set_scaled_font(gfxFont::RefCairo(aDrawTarget), cairoFont);
  187. return true;
  188. }