gfxFontEntry.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833
  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 "mozilla/DebugOnly.h"
  6. #include "mozilla/MathAlgorithms.h"
  7. #include "mozilla/Logging.h"
  8. #include "nsServiceManagerUtils.h"
  9. #include "nsILanguageAtomService.h"
  10. #include "gfxFontEntry.h"
  11. #include "gfxTextRun.h"
  12. #include "gfxPlatform.h"
  13. #include "nsGkAtoms.h"
  14. #include "gfxTypes.h"
  15. #include "gfxContext.h"
  16. #include "gfxFontConstants.h"
  17. #include "gfxGraphiteShaper.h"
  18. #include "gfxHarfBuzzShaper.h"
  19. #include "gfxUserFontSet.h"
  20. #include "gfxPlatformFontList.h"
  21. #include "nsUnicodeProperties.h"
  22. #include "nsMathUtils.h"
  23. #include "nsBidiUtils.h"
  24. #include "nsUnicodeRange.h"
  25. #include "nsStyleConsts.h"
  26. #include "mozilla/AppUnits.h"
  27. #include "mozilla/FloatingPoint.h"
  28. #include "mozilla/Likely.h"
  29. #include "mozilla/MemoryReporting.h"
  30. #include "mozilla/Preferences.h"
  31. #include "mozilla/Services.h"
  32. #include "mozilla/Telemetry.h"
  33. #include "gfxSVGGlyphs.h"
  34. #include "gfx2DGlue.h"
  35. #include "cairo.h"
  36. #include "harfbuzz/hb.h"
  37. #include "harfbuzz/hb-ot.h"
  38. #include "graphite2/Font.h"
  39. #include <algorithm>
  40. using namespace mozilla;
  41. using namespace mozilla::gfx;
  42. using namespace mozilla::unicode;
  43. using mozilla::services::GetObserverService;
  44. void
  45. gfxCharacterMap::NotifyReleased()
  46. {
  47. gfxPlatformFontList *fontlist = gfxPlatformFontList::PlatformFontList();
  48. if (mShared) {
  49. fontlist->RemoveCmap(this);
  50. }
  51. delete this;
  52. }
  53. gfxFontEntry::gfxFontEntry() :
  54. mStyle(NS_FONT_STYLE_NORMAL), mFixedPitch(false),
  55. mIsValid(true),
  56. mIsBadUnderlineFont(false),
  57. mIsUserFontContainer(false),
  58. mIsDataUserFont(false),
  59. mIsLocalUserFont(false),
  60. mStandardFace(false),
  61. mSymbolFont(false),
  62. mIgnoreGDEF(false),
  63. mIgnoreGSUB(false),
  64. mSVGInitialized(false),
  65. mHasSpaceFeaturesInitialized(false),
  66. mHasSpaceFeatures(false),
  67. mHasSpaceFeaturesKerning(false),
  68. mHasSpaceFeaturesNonKerning(false),
  69. mSkipDefaultFeatureSpaceCheck(false),
  70. mGraphiteSpaceContextualsInitialized(false),
  71. mHasGraphiteSpaceContextuals(false),
  72. mSpaceGlyphIsInvisible(false),
  73. mSpaceGlyphIsInvisibleInitialized(false),
  74. mCheckedForGraphiteTables(false),
  75. mHasCmapTable(false),
  76. mGrFaceInitialized(false),
  77. mCheckedForColorGlyph(false),
  78. mWeight(500), mStretch(NS_FONT_STRETCH_NORMAL),
  79. mUVSOffset(0), mUVSData(nullptr),
  80. mLanguageOverride(NO_FONT_LANGUAGE_OVERRIDE),
  81. mCOLR(nullptr),
  82. mCPAL(nullptr),
  83. mUnitsPerEm(0),
  84. mHBFace(nullptr),
  85. mGrFace(nullptr),
  86. mGrFaceRefCnt(0),
  87. mComputedSizeOfUserFont(0)
  88. {
  89. memset(&mDefaultSubSpaceFeatures, 0, sizeof(mDefaultSubSpaceFeatures));
  90. memset(&mNonDefaultSubSpaceFeatures, 0, sizeof(mNonDefaultSubSpaceFeatures));
  91. }
  92. gfxFontEntry::gfxFontEntry(const nsAString& aName, bool aIsStandardFace) :
  93. mName(aName), mStyle(NS_FONT_STYLE_NORMAL), mFixedPitch(false),
  94. mIsValid(true),
  95. mIsBadUnderlineFont(false),
  96. mIsUserFontContainer(false),
  97. mIsDataUserFont(false),
  98. mIsLocalUserFont(false), mStandardFace(aIsStandardFace),
  99. mSymbolFont(false),
  100. mIgnoreGDEF(false),
  101. mIgnoreGSUB(false),
  102. mSVGInitialized(false),
  103. mHasSpaceFeaturesInitialized(false),
  104. mHasSpaceFeatures(false),
  105. mHasSpaceFeaturesKerning(false),
  106. mHasSpaceFeaturesNonKerning(false),
  107. mSkipDefaultFeatureSpaceCheck(false),
  108. mGraphiteSpaceContextualsInitialized(false),
  109. mHasGraphiteSpaceContextuals(false),
  110. mSpaceGlyphIsInvisible(false),
  111. mSpaceGlyphIsInvisibleInitialized(false),
  112. mCheckedForGraphiteTables(false),
  113. mHasCmapTable(false),
  114. mGrFaceInitialized(false),
  115. mCheckedForColorGlyph(false),
  116. mWeight(500), mStretch(NS_FONT_STRETCH_NORMAL),
  117. mUVSOffset(0), mUVSData(nullptr),
  118. mLanguageOverride(NO_FONT_LANGUAGE_OVERRIDE),
  119. mCOLR(nullptr),
  120. mCPAL(nullptr),
  121. mUnitsPerEm(0),
  122. mHBFace(nullptr),
  123. mGrFace(nullptr),
  124. mGrFaceRefCnt(0),
  125. mComputedSizeOfUserFont(0)
  126. {
  127. memset(&mDefaultSubSpaceFeatures, 0, sizeof(mDefaultSubSpaceFeatures));
  128. memset(&mNonDefaultSubSpaceFeatures, 0, sizeof(mNonDefaultSubSpaceFeatures));
  129. }
  130. gfxFontEntry::~gfxFontEntry()
  131. {
  132. if (mCOLR) {
  133. hb_blob_destroy(mCOLR);
  134. }
  135. if (mCPAL) {
  136. hb_blob_destroy(mCPAL);
  137. }
  138. // For downloaded fonts, we need to tell the user font cache that this
  139. // entry is being deleted.
  140. if (mIsDataUserFont) {
  141. gfxUserFontSet::UserFontCache::ForgetFont(this);
  142. }
  143. if (mFeatureInputs) {
  144. for (auto iter = mFeatureInputs->Iter(); !iter.Done(); iter.Next()) {
  145. hb_set_t*& set = iter.Data();
  146. hb_set_destroy(set);
  147. }
  148. }
  149. // By the time the entry is destroyed, all font instances that were
  150. // using it should already have been deleted, and so the HB and/or Gr
  151. // face objects should have been released.
  152. MOZ_ASSERT(!mHBFace);
  153. MOZ_ASSERT(!mGrFaceInitialized);
  154. }
  155. bool gfxFontEntry::IsSymbolFont()
  156. {
  157. return mSymbolFont;
  158. }
  159. bool gfxFontEntry::TestCharacterMap(uint32_t aCh)
  160. {
  161. if (!mCharacterMap) {
  162. ReadCMAP();
  163. NS_ASSERTION(mCharacterMap, "failed to initialize character map");
  164. }
  165. return mCharacterMap->test(aCh);
  166. }
  167. nsresult gfxFontEntry::InitializeUVSMap()
  168. {
  169. // mUVSOffset will not be initialized
  170. // until cmap is initialized.
  171. if (!mCharacterMap) {
  172. ReadCMAP();
  173. NS_ASSERTION(mCharacterMap, "failed to initialize character map");
  174. }
  175. if (!mUVSOffset) {
  176. return NS_ERROR_FAILURE;
  177. }
  178. if (!mUVSData) {
  179. const uint32_t kCmapTag = TRUETYPE_TAG('c','m','a','p');
  180. AutoTable cmapTable(this, kCmapTag);
  181. if (!cmapTable) {
  182. mUVSOffset = 0; // don't bother to read the table again
  183. return NS_ERROR_FAILURE;
  184. }
  185. UniquePtr<uint8_t[]> uvsData;
  186. unsigned int cmapLen;
  187. const char* cmapData = hb_blob_get_data(cmapTable, &cmapLen);
  188. nsresult rv = gfxFontUtils::ReadCMAPTableFormat14(
  189. (const uint8_t*)cmapData + mUVSOffset,
  190. cmapLen - mUVSOffset, uvsData);
  191. if (NS_FAILED(rv)) {
  192. mUVSOffset = 0; // don't bother to read the table again
  193. return rv;
  194. }
  195. mUVSData = Move(uvsData);
  196. }
  197. return NS_OK;
  198. }
  199. uint16_t gfxFontEntry::GetUVSGlyph(uint32_t aCh, uint32_t aVS)
  200. {
  201. InitializeUVSMap();
  202. if (mUVSData) {
  203. return gfxFontUtils::MapUVSToGlyphFormat14(mUVSData.get(), aCh, aVS);
  204. }
  205. return 0;
  206. }
  207. bool gfxFontEntry::SupportsScriptInGSUB(const hb_tag_t* aScriptTags)
  208. {
  209. hb_face_t *face = GetHBFace();
  210. if (!face) {
  211. return false;
  212. }
  213. unsigned int index;
  214. hb_tag_t chosenScript;
  215. bool found =
  216. hb_ot_layout_table_choose_script(face, TRUETYPE_TAG('G','S','U','B'),
  217. aScriptTags, &index, &chosenScript);
  218. hb_face_destroy(face);
  219. return found && chosenScript != TRUETYPE_TAG('D','F','L','T');
  220. }
  221. nsresult gfxFontEntry::ReadCMAP(FontInfoData *aFontInfoData)
  222. {
  223. NS_ASSERTION(false, "using default no-op implementation of ReadCMAP");
  224. mCharacterMap = new gfxCharacterMap();
  225. return NS_OK;
  226. }
  227. nsString
  228. gfxFontEntry::RealFaceName()
  229. {
  230. AutoTable nameTable(this, TRUETYPE_TAG('n','a','m','e'));
  231. if (nameTable) {
  232. nsAutoString name;
  233. nsresult rv = gfxFontUtils::GetFullNameFromTable(nameTable, name);
  234. if (NS_SUCCEEDED(rv)) {
  235. return name;
  236. }
  237. }
  238. return Name();
  239. }
  240. already_AddRefed<gfxFont>
  241. gfxFontEntry::FindOrMakeFont(const gfxFontStyle *aStyle,
  242. bool aNeedsBold,
  243. gfxCharacterMap* aUnicodeRangeMap)
  244. {
  245. // the font entry name is the psname, not the family name
  246. RefPtr<gfxFont> font =
  247. gfxFontCache::GetCache()->Lookup(this, aStyle, aUnicodeRangeMap);
  248. if (!font) {
  249. gfxFont *newFont = CreateFontInstance(aStyle, aNeedsBold);
  250. if (!newFont)
  251. return nullptr;
  252. if (!newFont->Valid()) {
  253. delete newFont;
  254. return nullptr;
  255. }
  256. font = newFont;
  257. font->SetUnicodeRangeMap(aUnicodeRangeMap);
  258. gfxFontCache::GetCache()->AddNew(font);
  259. }
  260. return font.forget();
  261. }
  262. uint16_t
  263. gfxFontEntry::UnitsPerEm()
  264. {
  265. if (!mUnitsPerEm) {
  266. AutoTable headTable(this, TRUETYPE_TAG('h','e','a','d'));
  267. if (headTable) {
  268. uint32_t len;
  269. const HeadTable* head =
  270. reinterpret_cast<const HeadTable*>(hb_blob_get_data(headTable,
  271. &len));
  272. if (len >= sizeof(HeadTable)) {
  273. mUnitsPerEm = head->unitsPerEm;
  274. }
  275. }
  276. // if we didn't find a usable 'head' table, or if the value was
  277. // outside the valid range, record it as invalid
  278. if (mUnitsPerEm < kMinUPEM || mUnitsPerEm > kMaxUPEM) {
  279. mUnitsPerEm = kInvalidUPEM;
  280. }
  281. }
  282. return mUnitsPerEm;
  283. }
  284. bool
  285. gfxFontEntry::HasSVGGlyph(uint32_t aGlyphId)
  286. {
  287. NS_ASSERTION(mSVGInitialized, "SVG data has not yet been loaded. TryGetSVGData() first.");
  288. return mSVGGlyphs->HasSVGGlyph(aGlyphId);
  289. }
  290. bool
  291. gfxFontEntry::GetSVGGlyphExtents(DrawTarget* aDrawTarget, uint32_t aGlyphId,
  292. gfxRect *aResult)
  293. {
  294. MOZ_ASSERT(mSVGInitialized,
  295. "SVG data has not yet been loaded. TryGetSVGData() first.");
  296. MOZ_ASSERT(mUnitsPerEm >= kMinUPEM && mUnitsPerEm <= kMaxUPEM,
  297. "font has invalid unitsPerEm");
  298. cairo_matrix_t fontMatrix;
  299. cairo_get_font_matrix(gfxFont::RefCairo(aDrawTarget), &fontMatrix);
  300. gfxMatrix svgToAppSpace(fontMatrix.xx, fontMatrix.yx,
  301. fontMatrix.xy, fontMatrix.yy,
  302. fontMatrix.x0, fontMatrix.y0);
  303. svgToAppSpace.Scale(1.0f / mUnitsPerEm, 1.0f / mUnitsPerEm);
  304. return mSVGGlyphs->GetGlyphExtents(aGlyphId, svgToAppSpace, aResult);
  305. }
  306. bool
  307. gfxFontEntry::RenderSVGGlyph(gfxContext *aContext, uint32_t aGlyphId,
  308. SVGContextPaint* aContextPaint)
  309. {
  310. NS_ASSERTION(mSVGInitialized, "SVG data has not yet been loaded. TryGetSVGData() first.");
  311. return mSVGGlyphs->RenderGlyph(aContext, aGlyphId, aContextPaint);
  312. }
  313. bool
  314. gfxFontEntry::TryGetSVGData(gfxFont* aFont)
  315. {
  316. if (!gfxPlatform::GetPlatform()->OpenTypeSVGEnabled()) {
  317. return false;
  318. }
  319. if (!mSVGInitialized) {
  320. mSVGInitialized = true;
  321. // If UnitsPerEm is not known/valid, we can't use SVG glyphs
  322. if (UnitsPerEm() == kInvalidUPEM) {
  323. return false;
  324. }
  325. // We don't use AutoTable here because we'll pass ownership of this
  326. // blob to the gfxSVGGlyphs, once we've confirmed the table exists
  327. hb_blob_t *svgTable = GetFontTable(TRUETYPE_TAG('S','V','G',' '));
  328. if (!svgTable) {
  329. return false;
  330. }
  331. // gfxSVGGlyphs will hb_blob_destroy() the table when it is finished
  332. // with it.
  333. mSVGGlyphs = MakeUnique<gfxSVGGlyphs>(svgTable, this);
  334. }
  335. if (mSVGGlyphs && !mFontsUsingSVGGlyphs.Contains(aFont)) {
  336. mFontsUsingSVGGlyphs.AppendElement(aFont);
  337. }
  338. return !!mSVGGlyphs;
  339. }
  340. void
  341. gfxFontEntry::NotifyFontDestroyed(gfxFont* aFont)
  342. {
  343. mFontsUsingSVGGlyphs.RemoveElement(aFont);
  344. }
  345. void
  346. gfxFontEntry::NotifyGlyphsChanged()
  347. {
  348. for (uint32_t i = 0, count = mFontsUsingSVGGlyphs.Length(); i < count; ++i) {
  349. gfxFont* font = mFontsUsingSVGGlyphs[i];
  350. font->NotifyGlyphsChanged();
  351. }
  352. }
  353. bool
  354. gfxFontEntry::TryGetColorGlyphs()
  355. {
  356. if (mCheckedForColorGlyph) {
  357. return (mCOLR && mCPAL);
  358. }
  359. mCheckedForColorGlyph = true;
  360. mCOLR = GetFontTable(TRUETYPE_TAG('C', 'O', 'L', 'R'));
  361. if (!mCOLR) {
  362. return false;
  363. }
  364. mCPAL = GetFontTable(TRUETYPE_TAG('C', 'P', 'A', 'L'));
  365. if (!mCPAL) {
  366. hb_blob_destroy(mCOLR);
  367. mCOLR = nullptr;
  368. return false;
  369. }
  370. // validation COLR and CPAL table
  371. if (gfxFontUtils::ValidateColorGlyphs(mCOLR, mCPAL)) {
  372. return true;
  373. }
  374. hb_blob_destroy(mCOLR);
  375. hb_blob_destroy(mCPAL);
  376. mCOLR = nullptr;
  377. mCPAL = nullptr;
  378. return false;
  379. }
  380. /**
  381. * FontTableBlobData
  382. *
  383. * See FontTableHashEntry for the general strategy.
  384. */
  385. class gfxFontEntry::FontTableBlobData {
  386. public:
  387. explicit FontTableBlobData(nsTArray<uint8_t>&& aBuffer)
  388. : mTableData(Move(aBuffer))
  389. , mHashtable(nullptr)
  390. , mHashKey(0)
  391. {
  392. MOZ_COUNT_CTOR(FontTableBlobData);
  393. }
  394. ~FontTableBlobData() {
  395. MOZ_COUNT_DTOR(FontTableBlobData);
  396. if (mHashtable && mHashKey) {
  397. mHashtable->RemoveEntry(mHashKey);
  398. }
  399. }
  400. // Useful for creating blobs
  401. const char *GetTable() const
  402. {
  403. return reinterpret_cast<const char*>(mTableData.Elements());
  404. }
  405. uint32_t GetTableLength() const { return mTableData.Length(); }
  406. // Tell this FontTableBlobData to remove the HashEntry when this is
  407. // destroyed.
  408. void ManageHashEntry(nsTHashtable<FontTableHashEntry> *aHashtable,
  409. uint32_t aHashKey)
  410. {
  411. mHashtable = aHashtable;
  412. mHashKey = aHashKey;
  413. }
  414. // Disconnect from the HashEntry (because the blob has already been
  415. // removed from the hashtable).
  416. void ForgetHashEntry()
  417. {
  418. mHashtable = nullptr;
  419. mHashKey = 0;
  420. }
  421. size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
  422. return mTableData.ShallowSizeOfExcludingThis(aMallocSizeOf);
  423. }
  424. size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
  425. return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  426. }
  427. private:
  428. // The font table data block
  429. nsTArray<uint8_t> mTableData;
  430. // The blob destroy function needs to know the owning hashtable
  431. // and the hashtable key, so that it can remove the entry.
  432. nsTHashtable<FontTableHashEntry> *mHashtable;
  433. uint32_t mHashKey;
  434. // not implemented
  435. FontTableBlobData(const FontTableBlobData&);
  436. };
  437. hb_blob_t *
  438. gfxFontEntry::FontTableHashEntry::
  439. ShareTableAndGetBlob(nsTArray<uint8_t>&& aTable,
  440. nsTHashtable<FontTableHashEntry> *aHashtable)
  441. {
  442. Clear();
  443. // adopts elements of aTable
  444. mSharedBlobData = new FontTableBlobData(Move(aTable));
  445. mBlob = hb_blob_create(mSharedBlobData->GetTable(),
  446. mSharedBlobData->GetTableLength(),
  447. HB_MEMORY_MODE_READONLY,
  448. mSharedBlobData, DeleteFontTableBlobData);
  449. if (mBlob == hb_blob_get_empty() ) {
  450. // The FontTableBlobData was destroyed during hb_blob_create().
  451. // The (empty) blob is still be held in the hashtable with a strong
  452. // reference.
  453. return hb_blob_reference(mBlob);
  454. }
  455. // Tell the FontTableBlobData to remove this hash entry when destroyed.
  456. // The hashtable does not keep a strong reference.
  457. mSharedBlobData->ManageHashEntry(aHashtable, GetKey());
  458. return mBlob;
  459. }
  460. void
  461. gfxFontEntry::FontTableHashEntry::Clear()
  462. {
  463. // If the FontTableBlobData is managing the hash entry, then the blob is
  464. // not owned by this HashEntry; otherwise there is strong reference to the
  465. // blob that must be removed.
  466. if (mSharedBlobData) {
  467. mSharedBlobData->ForgetHashEntry();
  468. mSharedBlobData = nullptr;
  469. } else if (mBlob) {
  470. hb_blob_destroy(mBlob);
  471. }
  472. mBlob = nullptr;
  473. }
  474. // a hb_destroy_func for hb_blob_create
  475. /* static */ void
  476. gfxFontEntry::FontTableHashEntry::DeleteFontTableBlobData(void *aBlobData)
  477. {
  478. delete static_cast<FontTableBlobData*>(aBlobData);
  479. }
  480. hb_blob_t *
  481. gfxFontEntry::FontTableHashEntry::GetBlob() const
  482. {
  483. return hb_blob_reference(mBlob);
  484. }
  485. bool
  486. gfxFontEntry::GetExistingFontTable(uint32_t aTag, hb_blob_t **aBlob)
  487. {
  488. if (!mFontTableCache) {
  489. // we do this here rather than on fontEntry construction
  490. // because not all shapers will access the table cache at all
  491. mFontTableCache = MakeUnique<nsTHashtable<FontTableHashEntry>>(8);
  492. }
  493. FontTableHashEntry *entry = mFontTableCache->GetEntry(aTag);
  494. if (!entry) {
  495. return false;
  496. }
  497. *aBlob = entry->GetBlob();
  498. return true;
  499. }
  500. hb_blob_t *
  501. gfxFontEntry::ShareFontTableAndGetBlob(uint32_t aTag,
  502. nsTArray<uint8_t>* aBuffer)
  503. {
  504. if (MOZ_UNLIKELY(!mFontTableCache)) {
  505. // we do this here rather than on fontEntry construction
  506. // because not all shapers will access the table cache at all
  507. mFontTableCache = MakeUnique<nsTHashtable<FontTableHashEntry>>(8);
  508. }
  509. FontTableHashEntry *entry = mFontTableCache->PutEntry(aTag);
  510. if (MOZ_UNLIKELY(!entry)) { // OOM
  511. return nullptr;
  512. }
  513. if (!aBuffer) {
  514. // ensure the entry is null
  515. entry->Clear();
  516. return nullptr;
  517. }
  518. return entry->ShareTableAndGetBlob(Move(*aBuffer), mFontTableCache.get());
  519. }
  520. already_AddRefed<gfxCharacterMap>
  521. gfxFontEntry::GetCMAPFromFontInfo(FontInfoData *aFontInfoData,
  522. uint32_t& aUVSOffset,
  523. bool& aSymbolFont)
  524. {
  525. if (!aFontInfoData || !aFontInfoData->mLoadCmaps) {
  526. return nullptr;
  527. }
  528. return aFontInfoData->GetCMAP(mName, aUVSOffset, aSymbolFont);
  529. }
  530. hb_blob_t *
  531. gfxFontEntry::GetFontTable(uint32_t aTag)
  532. {
  533. hb_blob_t *blob;
  534. if (GetExistingFontTable(aTag, &blob)) {
  535. return blob;
  536. }
  537. nsTArray<uint8_t> buffer;
  538. bool haveTable = NS_SUCCEEDED(CopyFontTable(aTag, buffer));
  539. return ShareFontTableAndGetBlob(aTag, haveTable ? &buffer : nullptr);
  540. }
  541. // callback for HarfBuzz to get a font table (in hb_blob_t form)
  542. // from the font entry (passed as aUserData)
  543. /*static*/ hb_blob_t *
  544. gfxFontEntry::HBGetTable(hb_face_t *face, uint32_t aTag, void *aUserData)
  545. {
  546. gfxFontEntry *fontEntry = static_cast<gfxFontEntry*>(aUserData);
  547. // bug 589682 - ignore the GDEF table in buggy fonts (applies to
  548. // Italic and BoldItalic faces of Times New Roman)
  549. if (aTag == TRUETYPE_TAG('G','D','E','F') &&
  550. fontEntry->IgnoreGDEF()) {
  551. return nullptr;
  552. }
  553. // bug 721719 - ignore the GSUB table in buggy fonts (applies to Roboto,
  554. // at least on some Android ICS devices; set in gfxFT2FontList.cpp)
  555. if (aTag == TRUETYPE_TAG('G','S','U','B') &&
  556. fontEntry->IgnoreGSUB()) {
  557. return nullptr;
  558. }
  559. return fontEntry->GetFontTable(aTag);
  560. }
  561. /*static*/ void
  562. gfxFontEntry::HBFaceDeletedCallback(void *aUserData)
  563. {
  564. gfxFontEntry *fe = static_cast<gfxFontEntry*>(aUserData);
  565. fe->ForgetHBFace();
  566. }
  567. void
  568. gfxFontEntry::ForgetHBFace()
  569. {
  570. mHBFace = nullptr;
  571. }
  572. hb_face_t*
  573. gfxFontEntry::GetHBFace()
  574. {
  575. if (!mHBFace) {
  576. mHBFace = hb_face_create_for_tables(HBGetTable, this,
  577. HBFaceDeletedCallback);
  578. return mHBFace;
  579. }
  580. return hb_face_reference(mHBFace);
  581. }
  582. /*static*/ const void*
  583. gfxFontEntry::GrGetTable(const void *aAppFaceHandle, unsigned int aName,
  584. size_t *aLen)
  585. {
  586. gfxFontEntry *fontEntry =
  587. static_cast<gfxFontEntry*>(const_cast<void*>(aAppFaceHandle));
  588. hb_blob_t *blob = fontEntry->GetFontTable(aName);
  589. if (blob) {
  590. unsigned int blobLength;
  591. const void *tableData = hb_blob_get_data(blob, &blobLength);
  592. fontEntry->mGrTableMap->Put(tableData, blob);
  593. *aLen = blobLength;
  594. return tableData;
  595. }
  596. *aLen = 0;
  597. return nullptr;
  598. }
  599. /*static*/ void
  600. gfxFontEntry::GrReleaseTable(const void *aAppFaceHandle,
  601. const void *aTableBuffer)
  602. {
  603. gfxFontEntry *fontEntry =
  604. static_cast<gfxFontEntry*>(const_cast<void*>(aAppFaceHandle));
  605. void *data;
  606. if (fontEntry->mGrTableMap->Get(aTableBuffer, &data)) {
  607. fontEntry->mGrTableMap->Remove(aTableBuffer);
  608. hb_blob_destroy(static_cast<hb_blob_t*>(data));
  609. }
  610. }
  611. gr_face*
  612. gfxFontEntry::GetGrFace()
  613. {
  614. if (!mGrFaceInitialized) {
  615. gr_face_ops faceOps = {
  616. sizeof(gr_face_ops),
  617. GrGetTable,
  618. GrReleaseTable
  619. };
  620. mGrTableMap = new nsDataHashtable<nsPtrHashKey<const void>,void*>;
  621. mGrFace = gr_make_face_with_ops(this, &faceOps, gr_face_default);
  622. mGrFaceInitialized = true;
  623. }
  624. ++mGrFaceRefCnt;
  625. return mGrFace;
  626. }
  627. void
  628. gfxFontEntry::ReleaseGrFace(gr_face *aFace)
  629. {
  630. MOZ_ASSERT(aFace == mGrFace); // sanity-check
  631. MOZ_ASSERT(mGrFaceRefCnt > 0);
  632. if (--mGrFaceRefCnt == 0) {
  633. gr_face_destroy(mGrFace);
  634. mGrFace = nullptr;
  635. mGrFaceInitialized = false;
  636. delete mGrTableMap;
  637. mGrTableMap = nullptr;
  638. }
  639. }
  640. void
  641. gfxFontEntry::DisconnectSVG()
  642. {
  643. if (mSVGInitialized && mSVGGlyphs) {
  644. mSVGGlyphs = nullptr;
  645. mSVGInitialized = false;
  646. }
  647. }
  648. bool
  649. gfxFontEntry::HasFontTable(uint32_t aTableTag)
  650. {
  651. AutoTable table(this, aTableTag);
  652. return table && hb_blob_get_length(table) > 0;
  653. }
  654. void
  655. gfxFontEntry::CheckForGraphiteTables()
  656. {
  657. mHasGraphiteTables = HasFontTable(TRUETYPE_TAG('S','i','l','f'));
  658. }
  659. bool
  660. gfxFontEntry::HasGraphiteSpaceContextuals()
  661. {
  662. if (!mGraphiteSpaceContextualsInitialized) {
  663. gr_face* face = GetGrFace();
  664. if (face) {
  665. const gr_faceinfo* faceInfo = gr_face_info(face, 0);
  666. mHasGraphiteSpaceContextuals =
  667. faceInfo->space_contextuals != gr_faceinfo::gr_space_none;
  668. }
  669. ReleaseGrFace(face); // always balance GetGrFace, even if face is null
  670. mGraphiteSpaceContextualsInitialized = true;
  671. }
  672. return mHasGraphiteSpaceContextuals;
  673. }
  674. #define FEATURE_SCRIPT_MASK 0x000000ff // script index replaces low byte of tag
  675. static_assert(int(Script::NUM_SCRIPT_CODES) <= FEATURE_SCRIPT_MASK, "Too many script codes");
  676. // high-order three bytes of tag with script in low-order byte
  677. #define SCRIPT_FEATURE(s,tag) (((~FEATURE_SCRIPT_MASK) & (tag)) | \
  678. ((FEATURE_SCRIPT_MASK) & static_cast<uint32_t>(s)))
  679. bool
  680. gfxFontEntry::SupportsOpenTypeFeature(Script aScript, uint32_t aFeatureTag)
  681. {
  682. if (!mSupportedFeatures) {
  683. mSupportedFeatures = MakeUnique<nsDataHashtable<nsUint32HashKey,bool>>();
  684. }
  685. // note: high-order three bytes *must* be unique for each feature
  686. // listed below (see SCRIPT_FEATURE macro def'n)
  687. NS_ASSERTION(aFeatureTag == HB_TAG('s','m','c','p') ||
  688. aFeatureTag == HB_TAG('c','2','s','c') ||
  689. aFeatureTag == HB_TAG('p','c','a','p') ||
  690. aFeatureTag == HB_TAG('c','2','p','c') ||
  691. aFeatureTag == HB_TAG('s','u','p','s') ||
  692. aFeatureTag == HB_TAG('s','u','b','s') ||
  693. aFeatureTag == HB_TAG('v','e','r','t'),
  694. "use of unknown feature tag");
  695. // note: graphite feature support uses the last script index
  696. NS_ASSERTION(int(aScript) < FEATURE_SCRIPT_MASK - 1,
  697. "need to bump the size of the feature shift");
  698. uint32_t scriptFeature = SCRIPT_FEATURE(aScript, aFeatureTag);
  699. bool result;
  700. if (mSupportedFeatures->Get(scriptFeature, &result)) {
  701. return result;
  702. }
  703. result = false;
  704. hb_face_t *face = GetHBFace();
  705. if (hb_ot_layout_has_substitution(face)) {
  706. hb_script_t hbScript =
  707. gfxHarfBuzzShaper::GetHBScriptUsedForShaping(aScript);
  708. // Get the OpenType tag(s) that match this script code
  709. hb_tag_t scriptTags[4] = {
  710. HB_TAG_NONE,
  711. HB_TAG_NONE,
  712. HB_TAG_NONE,
  713. HB_TAG_NONE
  714. };
  715. hb_ot_tags_from_script(hbScript, &scriptTags[0], &scriptTags[1]);
  716. // Replace the first remaining NONE with DEFAULT
  717. hb_tag_t* scriptTag = &scriptTags[0];
  718. while (*scriptTag != HB_TAG_NONE) {
  719. ++scriptTag;
  720. }
  721. *scriptTag = HB_OT_TAG_DEFAULT_SCRIPT;
  722. // Now check for 'smcp' under the first of those scripts that is present
  723. const hb_tag_t kGSUB = HB_TAG('G','S','U','B');
  724. scriptTag = &scriptTags[0];
  725. while (*scriptTag != HB_TAG_NONE) {
  726. unsigned int scriptIndex;
  727. if (hb_ot_layout_table_find_script(face, kGSUB, *scriptTag,
  728. &scriptIndex)) {
  729. if (hb_ot_layout_language_find_feature(face, kGSUB,
  730. scriptIndex,
  731. HB_OT_LAYOUT_DEFAULT_LANGUAGE_INDEX,
  732. aFeatureTag, nullptr)) {
  733. result = true;
  734. }
  735. break;
  736. }
  737. ++scriptTag;
  738. }
  739. }
  740. hb_face_destroy(face);
  741. mSupportedFeatures->Put(scriptFeature, result);
  742. return result;
  743. }
  744. const hb_set_t*
  745. gfxFontEntry::InputsForOpenTypeFeature(Script aScript, uint32_t aFeatureTag)
  746. {
  747. if (!mFeatureInputs) {
  748. mFeatureInputs = MakeUnique<nsDataHashtable<nsUint32HashKey,hb_set_t*>>();
  749. }
  750. NS_ASSERTION(aFeatureTag == HB_TAG('s','u','p','s') ||
  751. aFeatureTag == HB_TAG('s','u','b','s'),
  752. "use of unknown feature tag");
  753. uint32_t scriptFeature = SCRIPT_FEATURE(aScript, aFeatureTag);
  754. hb_set_t *inputGlyphs;
  755. if (mFeatureInputs->Get(scriptFeature, &inputGlyphs)) {
  756. return inputGlyphs;
  757. }
  758. inputGlyphs = hb_set_create();
  759. hb_face_t *face = GetHBFace();
  760. if (hb_ot_layout_has_substitution(face)) {
  761. hb_script_t hbScript =
  762. gfxHarfBuzzShaper::GetHBScriptUsedForShaping(aScript);
  763. // Get the OpenType tag(s) that match this script code
  764. hb_tag_t scriptTags[4] = {
  765. HB_TAG_NONE,
  766. HB_TAG_NONE,
  767. HB_TAG_NONE,
  768. HB_TAG_NONE
  769. };
  770. hb_ot_tags_from_script(hbScript, &scriptTags[0], &scriptTags[1]);
  771. // Replace the first remaining NONE with DEFAULT
  772. hb_tag_t* scriptTag = &scriptTags[0];
  773. while (*scriptTag != HB_TAG_NONE) {
  774. ++scriptTag;
  775. }
  776. *scriptTag = HB_OT_TAG_DEFAULT_SCRIPT;
  777. const hb_tag_t kGSUB = HB_TAG('G','S','U','B');
  778. hb_tag_t features[2] = { aFeatureTag, HB_TAG_NONE };
  779. hb_set_t *featurelookups = hb_set_create();
  780. hb_ot_layout_collect_lookups(face, kGSUB, scriptTags, nullptr,
  781. features, featurelookups);
  782. hb_codepoint_t index = -1;
  783. while (hb_set_next(featurelookups, &index)) {
  784. hb_ot_layout_lookup_collect_glyphs(face, kGSUB, index,
  785. nullptr, inputGlyphs,
  786. nullptr, nullptr);
  787. }
  788. hb_set_destroy(featurelookups);
  789. }
  790. hb_face_destroy(face);
  791. mFeatureInputs->Put(scriptFeature, inputGlyphs);
  792. return inputGlyphs;
  793. }
  794. bool
  795. gfxFontEntry::SupportsGraphiteFeature(uint32_t aFeatureTag)
  796. {
  797. if (!mSupportedFeatures) {
  798. mSupportedFeatures = MakeUnique<nsDataHashtable<nsUint32HashKey,bool>>();
  799. }
  800. // note: high-order three bytes *must* be unique for each feature
  801. // listed below (see SCRIPT_FEATURE macro def'n)
  802. NS_ASSERTION(aFeatureTag == HB_TAG('s','m','c','p') ||
  803. aFeatureTag == HB_TAG('c','2','s','c') ||
  804. aFeatureTag == HB_TAG('p','c','a','p') ||
  805. aFeatureTag == HB_TAG('c','2','p','c') ||
  806. aFeatureTag == HB_TAG('s','u','p','s') ||
  807. aFeatureTag == HB_TAG('s','u','b','s'),
  808. "use of unknown feature tag");
  809. // graphite feature check uses the last script slot
  810. uint32_t scriptFeature = SCRIPT_FEATURE(FEATURE_SCRIPT_MASK, aFeatureTag);
  811. bool result;
  812. if (mSupportedFeatures->Get(scriptFeature, &result)) {
  813. return result;
  814. }
  815. gr_face* face = GetGrFace();
  816. result = face ? gr_face_find_fref(face, aFeatureTag) != nullptr : false;
  817. ReleaseGrFace(face);
  818. mSupportedFeatures->Put(scriptFeature, result);
  819. return result;
  820. }
  821. bool
  822. gfxFontEntry::GetColorLayersInfo(uint32_t aGlyphId,
  823. const mozilla::gfx::Color& aDefaultColor,
  824. nsTArray<uint16_t>& aLayerGlyphs,
  825. nsTArray<mozilla::gfx::Color>& aLayerColors)
  826. {
  827. return gfxFontUtils::GetColorGlyphLayers(mCOLR,
  828. mCPAL,
  829. aGlyphId,
  830. aDefaultColor,
  831. aLayerGlyphs,
  832. aLayerColors);
  833. }
  834. size_t
  835. gfxFontEntry::FontTableHashEntry::SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const
  836. {
  837. size_t n = 0;
  838. if (mBlob) {
  839. n += aMallocSizeOf(mBlob);
  840. }
  841. if (mSharedBlobData) {
  842. n += mSharedBlobData->SizeOfIncludingThis(aMallocSizeOf);
  843. }
  844. return n;
  845. }
  846. void
  847. gfxFontEntry::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
  848. FontListSizes* aSizes) const
  849. {
  850. aSizes->mFontListSize += mName.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
  851. // cmaps are shared so only non-shared cmaps are included here
  852. if (mCharacterMap && mCharacterMap->mBuildOnTheFly) {
  853. aSizes->mCharMapsSize +=
  854. mCharacterMap->SizeOfIncludingThis(aMallocSizeOf);
  855. }
  856. if (mFontTableCache) {
  857. aSizes->mFontTableCacheSize +=
  858. mFontTableCache->SizeOfIncludingThis(aMallocSizeOf);
  859. }
  860. // If the font has UVS data, we count that as part of the character map.
  861. if (mUVSData) {
  862. aSizes->mCharMapsSize += aMallocSizeOf(mUVSData.get());
  863. }
  864. // The following, if present, are essentially cached forms of font table
  865. // data, so we'll accumulate them together with the basic table cache.
  866. if (mUserFontData) {
  867. aSizes->mFontTableCacheSize +=
  868. mUserFontData->SizeOfIncludingThis(aMallocSizeOf);
  869. }
  870. if (mSVGGlyphs) {
  871. aSizes->mFontTableCacheSize +=
  872. mSVGGlyphs->SizeOfIncludingThis(aMallocSizeOf);
  873. }
  874. if (mSupportedFeatures) {
  875. aSizes->mFontTableCacheSize +=
  876. mSupportedFeatures->ShallowSizeOfIncludingThis(aMallocSizeOf);
  877. }
  878. if (mFeatureInputs) {
  879. aSizes->mFontTableCacheSize +=
  880. mFeatureInputs->ShallowSizeOfIncludingThis(aMallocSizeOf);
  881. for (auto iter = mFeatureInputs->ConstIter(); !iter.Done();
  882. iter.Next()) {
  883. // There's no API to get the real size of an hb_set, so we'll use
  884. // an approximation based on knowledge of the implementation.
  885. aSizes->mFontTableCacheSize += 8192; // vector of 64K bits
  886. }
  887. }
  888. // We don't include the size of mCOLR/mCPAL here, because (depending on the
  889. // font backend implementation) they will either wrap blocks of data owned
  890. // by the system (and potentially shared), or tables that are in our font
  891. // table cache and therefore already counted.
  892. }
  893. void
  894. gfxFontEntry::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf,
  895. FontListSizes* aSizes) const
  896. {
  897. aSizes->mFontListSize += aMallocSizeOf(this);
  898. AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
  899. }
  900. // This is used to report the size of an individual downloaded font in the
  901. // user font cache. (Fonts that are part of the platform font list accumulate
  902. // their sizes to the font list's reporter using the AddSizeOf... methods
  903. // above.)
  904. size_t
  905. gfxFontEntry::ComputedSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
  906. {
  907. FontListSizes s = { 0 };
  908. AddSizeOfExcludingThis(aMallocSizeOf, &s);
  909. // When reporting memory used for the main platform font list,
  910. // where we're typically summing the totals for a few hundred font faces,
  911. // we report the fields of FontListSizes separately.
  912. // But for downloaded user fonts, the actual resource data (added below)
  913. // will dominate, and the minor overhead of these pieces isn't worth
  914. // splitting out for an individual font.
  915. size_t result = s.mFontListSize + s.mFontTableCacheSize + s.mCharMapsSize;
  916. if (mIsDataUserFont) {
  917. MOZ_ASSERT(mComputedSizeOfUserFont > 0, "user font with no data?");
  918. result += mComputedSizeOfUserFont;
  919. }
  920. return result;
  921. }
  922. //////////////////////////////////////////////////////////////////////////////
  923. //
  924. // class gfxFontFamily
  925. //
  926. //////////////////////////////////////////////////////////////////////////////
  927. // we consider faces with mStandardFace == true to be "less than" those with false,
  928. // because during style matching, earlier entries are tried first
  929. class FontEntryStandardFaceComparator {
  930. public:
  931. bool Equals(const RefPtr<gfxFontEntry>& a, const RefPtr<gfxFontEntry>& b) const {
  932. return a->mStandardFace == b->mStandardFace;
  933. }
  934. bool LessThan(const RefPtr<gfxFontEntry>& a, const RefPtr<gfxFontEntry>& b) const {
  935. return (a->mStandardFace == true && b->mStandardFace == false);
  936. }
  937. };
  938. void
  939. gfxFontFamily::SortAvailableFonts()
  940. {
  941. mAvailableFonts.Sort(FontEntryStandardFaceComparator());
  942. }
  943. bool
  944. gfxFontFamily::HasOtherFamilyNames()
  945. {
  946. // need to read in other family names to determine this
  947. if (!mOtherFamilyNamesInitialized) {
  948. ReadOtherFamilyNames(gfxPlatformFontList::PlatformFontList()); // sets mHasOtherFamilyNames
  949. }
  950. return mHasOtherFamilyNames;
  951. }
  952. gfxFontEntry*
  953. gfxFontFamily::FindFontForStyle(const gfxFontStyle& aFontStyle,
  954. bool& aNeedsSyntheticBold)
  955. {
  956. AutoTArray<gfxFontEntry*,4> matched;
  957. FindAllFontsForStyle(aFontStyle, matched, aNeedsSyntheticBold);
  958. if (!matched.IsEmpty()) {
  959. return matched[0];
  960. }
  961. return nullptr;
  962. }
  963. #define STYLE_SHIFT 2 // number of bits to contain style distance
  964. // style distance ==> [0,2]
  965. static inline uint32_t
  966. StyleDistance(uint32_t aFontStyle, uint32_t aTargetStyle)
  967. {
  968. if (aFontStyle == aTargetStyle) {
  969. return 0; // styles match exactly ==> 0
  970. }
  971. if (aFontStyle == NS_FONT_STYLE_NORMAL ||
  972. aTargetStyle == NS_FONT_STYLE_NORMAL) {
  973. return 2; // one is normal (but not the other) ==> 2
  974. }
  975. return 1; // neither is normal; must be italic vs oblique ==> 1
  976. }
  977. #define REVERSE_STRETCH_DISTANCE 5
  978. // stretch distance ==> [0,13]
  979. static inline uint32_t
  980. StretchDistance(int16_t aFontStretch, int16_t aTargetStretch)
  981. {
  982. int32_t distance = 0;
  983. if (aTargetStretch != aFontStretch) {
  984. // stretch values are in the range -4 .. +4
  985. // if aTargetStretch is positive, we prefer more-positive values;
  986. // if zero or negative, prefer more-negative
  987. if (aTargetStretch > 0) {
  988. distance = (aFontStretch - aTargetStretch);
  989. } else {
  990. distance = (aTargetStretch - aFontStretch);
  991. }
  992. // if the computed "distance" here is negative, it means that
  993. // aFontEntry lies in the "non-preferred" direction from aTargetStretch,
  994. // so we treat that as larger than any preferred-direction distance
  995. // (max possible is 4) by adding an extra 5 to the absolute value
  996. if (distance < 0) {
  997. distance = -distance + REVERSE_STRETCH_DISTANCE;
  998. }
  999. }
  1000. return uint32_t(distance);
  1001. }
  1002. // CSS currently limits font weights to multiples of 100 but the weight
  1003. // matching code below does not assume this.
  1004. //
  1005. // Calculate weight distance with values in the range (0..1000). In general,
  1006. // heavier weights match towards even heavier weights while lighter weights
  1007. // match towards even lighter weights. Target weight values in the range
  1008. // [400..500] are special, since they will first match up to 500, then down
  1009. // towards 0, then up again towards 999.
  1010. //
  1011. // Example: with target 600 and font weight 800, distance will be 200. With
  1012. // target 300 and font weight 600, distance will be 900, since heavier
  1013. // weights are farther away than lighter weights. If the target is 5 and the
  1014. // font weight 995, the distance would be 1590 for the same reason.
  1015. #define REVERSE_WEIGHT_DISTANCE 600
  1016. #define WEIGHT_SHIFT 11 // number of bits to contain weight distance
  1017. // weight distance ==> [0,1598]
  1018. static inline uint32_t
  1019. WeightDistance(uint32_t aFontWeight, uint32_t aTargetWeight)
  1020. {
  1021. // Compute a measure of the "distance" between the requested
  1022. // weight and the given fontEntry
  1023. int32_t distance = 0, addedDistance = 0;
  1024. if (aTargetWeight != aFontWeight) {
  1025. if (aTargetWeight > 500) {
  1026. distance = aFontWeight - aTargetWeight;
  1027. } else if (aTargetWeight < 400) {
  1028. distance = aTargetWeight - aFontWeight;
  1029. } else {
  1030. // special case - target is between 400 and 500
  1031. // font weights between 400 and 500 are close
  1032. if (aFontWeight >= 400 && aFontWeight <= 500) {
  1033. if (aFontWeight < aTargetWeight) {
  1034. distance = 500 - aFontWeight;
  1035. } else {
  1036. distance = aFontWeight - aTargetWeight;
  1037. }
  1038. } else {
  1039. // font weights outside use rule for target weights < 400 with
  1040. // added distance to separate from font weights in
  1041. // the [400..500] range
  1042. distance = aTargetWeight - aFontWeight;
  1043. addedDistance = 100;
  1044. }
  1045. }
  1046. if (distance < 0) {
  1047. distance = -distance + REVERSE_WEIGHT_DISTANCE;
  1048. }
  1049. distance += addedDistance;
  1050. }
  1051. return uint32_t(distance);
  1052. }
  1053. #define MAX_DISTANCE 0xffffffff
  1054. static inline uint32_t
  1055. WeightStyleStretchDistance(gfxFontEntry* aFontEntry,
  1056. const gfxFontStyle& aTargetStyle)
  1057. {
  1058. // weight/style/stretch priority: stretch >> style >> weight
  1059. uint32_t stretchDist =
  1060. StretchDistance(aFontEntry->mStretch, aTargetStyle.stretch);
  1061. uint32_t styleDist = StyleDistance(aFontEntry->mStyle, aTargetStyle.style);
  1062. uint32_t weightDist =
  1063. WeightDistance(aFontEntry->Weight(), aTargetStyle.weight);
  1064. NS_ASSERTION(weightDist < (1 << WEIGHT_SHIFT), "weight value out of bounds");
  1065. NS_ASSERTION(styleDist < (1 << STYLE_SHIFT), "slope value out of bounds");
  1066. return (stretchDist << (STYLE_SHIFT + WEIGHT_SHIFT)) |
  1067. (styleDist << WEIGHT_SHIFT) |
  1068. weightDist;
  1069. }
  1070. void
  1071. gfxFontFamily::FindAllFontsForStyle(const gfxFontStyle& aFontStyle,
  1072. nsTArray<gfxFontEntry*>& aFontEntryList,
  1073. bool& aNeedsSyntheticBold)
  1074. {
  1075. if (!mHasStyles) {
  1076. FindStyleVariations(); // collect faces for the family, if not already done
  1077. }
  1078. NS_ASSERTION(mAvailableFonts.Length() > 0, "font family with no faces!");
  1079. NS_ASSERTION(aFontEntryList.IsEmpty(), "non-empty fontlist passed in");
  1080. aNeedsSyntheticBold = false;
  1081. int8_t baseWeight = aFontStyle.ComputeWeight();
  1082. bool wantBold = baseWeight >= 6;
  1083. gfxFontEntry *fe = nullptr;
  1084. // If the family has only one face, we simply return it; no further
  1085. // checking needed
  1086. uint32_t count = mAvailableFonts.Length();
  1087. if (count == 1) {
  1088. fe = mAvailableFonts[0];
  1089. aNeedsSyntheticBold =
  1090. wantBold && !fe->IsBold() && aFontStyle.allowSyntheticWeight;
  1091. aFontEntryList.AppendElement(fe);
  1092. return;
  1093. }
  1094. // Most families are "simple", having just Regular/Bold/Italic/BoldItalic,
  1095. // or some subset of these. In this case, we have exactly 4 entries in mAvailableFonts,
  1096. // stored in the above order; note that some of the entries may be nullptr.
  1097. // We can then pick the required entry based on whether the request is for
  1098. // bold or non-bold, italic or non-italic, without running the more complex
  1099. // matching algorithm used for larger families with many weights and/or widths.
  1100. if (mIsSimpleFamily) {
  1101. // Family has no more than the "standard" 4 faces, at fixed indexes;
  1102. // calculate which one we want.
  1103. // Note that we cannot simply return it as not all 4 faces are necessarily present.
  1104. bool wantItalic = (aFontStyle.style != NS_FONT_STYLE_NORMAL);
  1105. uint8_t faceIndex = (wantItalic ? kItalicMask : 0) |
  1106. (wantBold ? kBoldMask : 0);
  1107. // if the desired style is available, return it directly
  1108. fe = mAvailableFonts[faceIndex];
  1109. if (fe) {
  1110. // no need to set aNeedsSyntheticBold here as we matched the boldness request
  1111. aFontEntryList.AppendElement(fe);
  1112. return;
  1113. }
  1114. // order to check fallback faces in a simple family, depending on requested style
  1115. static const uint8_t simpleFallbacks[4][3] = {
  1116. { kBoldFaceIndex, kItalicFaceIndex, kBoldItalicFaceIndex }, // fallbacks for Regular
  1117. { kRegularFaceIndex, kBoldItalicFaceIndex, kItalicFaceIndex },// Bold
  1118. { kBoldItalicFaceIndex, kRegularFaceIndex, kBoldFaceIndex }, // Italic
  1119. { kItalicFaceIndex, kBoldFaceIndex, kRegularFaceIndex } // BoldItalic
  1120. };
  1121. const uint8_t *order = simpleFallbacks[faceIndex];
  1122. for (uint8_t trial = 0; trial < 3; ++trial) {
  1123. // check remaining faces in order of preference to find the first that actually exists
  1124. fe = mAvailableFonts[order[trial]];
  1125. if (fe) {
  1126. aNeedsSyntheticBold =
  1127. wantBold && !fe->IsBold() &&
  1128. aFontStyle.allowSyntheticWeight;
  1129. aFontEntryList.AppendElement(fe);
  1130. return;
  1131. }
  1132. }
  1133. // this can't happen unless we have totally broken the font-list manager!
  1134. NS_NOTREACHED("no face found in simple font family!");
  1135. }
  1136. // Pick the font(s) that are closest to the desired weight, style, and
  1137. // stretch. Iterate over all fonts, measuring the weight/style distance.
  1138. // Because of unicode-range values, there may be more than one font for a
  1139. // given but the 99% use case is only a single font entry per
  1140. // weight/style/stretch distance value. To optimize this, only add entries
  1141. // to the matched font array when another entry already has the same
  1142. // weight/style/stretch distance and add the last matched font entry. For
  1143. // normal platform fonts with a single font entry for each
  1144. // weight/style/stretch combination, only the last matched font entry will
  1145. // be added.
  1146. uint32_t minDistance = MAX_DISTANCE;
  1147. gfxFontEntry* matched = nullptr;
  1148. // iterate in forward order so that faces like 'Bold' are matched before
  1149. // matching style distance faces such as 'Bold Outline' (see bug 1185812)
  1150. for (uint32_t i = 0; i < count; i++) {
  1151. fe = mAvailableFonts[i];
  1152. // weight/style/stretch priority: stretch >> style >> weight
  1153. uint32_t distance = WeightStyleStretchDistance(fe, aFontStyle);
  1154. if (distance < minDistance) {
  1155. matched = fe;
  1156. if (!aFontEntryList.IsEmpty()) {
  1157. aFontEntryList.Clear();
  1158. }
  1159. minDistance = distance;
  1160. } else if (distance == minDistance) {
  1161. if (matched) {
  1162. aFontEntryList.AppendElement(matched);
  1163. }
  1164. matched = fe;
  1165. }
  1166. }
  1167. NS_ASSERTION(matched, "didn't match a font within a family");
  1168. if (matched) {
  1169. aFontEntryList.AppendElement(matched);
  1170. if (!matched->IsBold() && aFontStyle.weight >= 600 &&
  1171. aFontStyle.allowSyntheticWeight) {
  1172. aNeedsSyntheticBold = true;
  1173. }
  1174. }
  1175. }
  1176. void
  1177. gfxFontFamily::CheckForSimpleFamily()
  1178. {
  1179. // already checked this family
  1180. if (mIsSimpleFamily) {
  1181. return;
  1182. }
  1183. uint32_t count = mAvailableFonts.Length();
  1184. if (count > 4 || count == 0) {
  1185. return; // can't be "simple" if there are >4 faces;
  1186. // if none then the family is unusable anyway
  1187. }
  1188. if (count == 1) {
  1189. mIsSimpleFamily = true;
  1190. return;
  1191. }
  1192. int16_t firstStretch = mAvailableFonts[0]->Stretch();
  1193. gfxFontEntry *faces[4] = { 0 };
  1194. for (uint8_t i = 0; i < count; ++i) {
  1195. gfxFontEntry *fe = mAvailableFonts[i];
  1196. if (fe->Stretch() != firstStretch || fe->IsOblique()) {
  1197. // simple families don't have varying font-stretch or oblique
  1198. return;
  1199. }
  1200. uint8_t faceIndex = (fe->IsItalic() ? kItalicMask : 0) |
  1201. (fe->Weight() >= 600 ? kBoldMask : 0);
  1202. if (faces[faceIndex]) {
  1203. return; // two faces resolve to the same slot; family isn't "simple"
  1204. }
  1205. faces[faceIndex] = fe;
  1206. }
  1207. // we have successfully slotted the available faces into the standard
  1208. // 4-face framework
  1209. mAvailableFonts.SetLength(4);
  1210. for (uint8_t i = 0; i < 4; ++i) {
  1211. if (mAvailableFonts[i].get() != faces[i]) {
  1212. mAvailableFonts[i].swap(faces[i]);
  1213. }
  1214. }
  1215. mIsSimpleFamily = true;
  1216. }
  1217. #ifdef DEBUG
  1218. bool
  1219. gfxFontFamily::ContainsFace(gfxFontEntry* aFontEntry) {
  1220. uint32_t i, numFonts = mAvailableFonts.Length();
  1221. for (i = 0; i < numFonts; i++) {
  1222. if (mAvailableFonts[i] == aFontEntry) {
  1223. return true;
  1224. }
  1225. // userfonts contain the actual real font entry
  1226. if (mAvailableFonts[i] && mAvailableFonts[i]->mIsUserFontContainer) {
  1227. gfxUserFontEntry* ufe =
  1228. static_cast<gfxUserFontEntry*>(mAvailableFonts[i].get());
  1229. if (ufe->GetPlatformFontEntry() == aFontEntry) {
  1230. return true;
  1231. }
  1232. }
  1233. }
  1234. return false;
  1235. }
  1236. #endif
  1237. void gfxFontFamily::LocalizedName(nsAString& aLocalizedName)
  1238. {
  1239. // just return the primary name; subclasses should override
  1240. aLocalizedName = mName;
  1241. }
  1242. // metric for how close a given font matches a style
  1243. static int32_t
  1244. CalcStyleMatch(gfxFontEntry *aFontEntry, const gfxFontStyle *aStyle)
  1245. {
  1246. int32_t rank = 0;
  1247. if (aStyle) {
  1248. // italics
  1249. bool wantUpright = (aStyle->style == NS_FONT_STYLE_NORMAL);
  1250. if (aFontEntry->IsUpright() == wantUpright) {
  1251. rank += 10;
  1252. }
  1253. // measure of closeness of weight to the desired value
  1254. rank += 9 - DeprecatedAbs(aFontEntry->Weight() / 100 - aStyle->ComputeWeight());
  1255. } else {
  1256. // if no font to match, prefer non-bold, non-italic fonts
  1257. if (aFontEntry->IsUpright()) {
  1258. rank += 3;
  1259. }
  1260. if (!aFontEntry->IsBold()) {
  1261. rank += 2;
  1262. }
  1263. }
  1264. return rank;
  1265. }
  1266. #define RANK_MATCHED_CMAP 20
  1267. void
  1268. gfxFontFamily::FindFontForChar(GlobalFontMatch *aMatchData)
  1269. {
  1270. if (mFamilyCharacterMapInitialized && !TestCharacterMap(aMatchData->mCh)) {
  1271. // none of the faces in the family support the required char,
  1272. // so bail out immediately
  1273. return;
  1274. }
  1275. bool needsBold;
  1276. gfxFontEntry *fe =
  1277. FindFontForStyle(aMatchData->mStyle ? *aMatchData->mStyle
  1278. : gfxFontStyle(),
  1279. needsBold);
  1280. if (fe && !fe->SkipDuringSystemFallback()) {
  1281. int32_t rank = 0;
  1282. if (fe->HasCharacter(aMatchData->mCh)) {
  1283. rank += RANK_MATCHED_CMAP;
  1284. aMatchData->mCount++;
  1285. LogModule* log = gfxPlatform::GetLog(eGfxLog_textrun);
  1286. if (MOZ_UNLIKELY(MOZ_LOG_TEST(log, LogLevel::Debug))) {
  1287. uint32_t unicodeRange = FindCharUnicodeRange(aMatchData->mCh);
  1288. Script script = GetScriptCode(aMatchData->mCh);
  1289. MOZ_LOG(log, LogLevel::Debug,\
  1290. ("(textrun-systemfallback-fonts) char: u+%6.6x "
  1291. "unicode-range: %d script: %d match: [%s]\n",
  1292. aMatchData->mCh,
  1293. unicodeRange, int(script),
  1294. NS_ConvertUTF16toUTF8(fe->Name()).get()));
  1295. }
  1296. }
  1297. aMatchData->mCmapsTested++;
  1298. if (rank == 0) {
  1299. return;
  1300. }
  1301. // omitting from original windows code -- family name, lang group, pitch
  1302. // not available in current FontEntry implementation
  1303. rank += CalcStyleMatch(fe, aMatchData->mStyle);
  1304. // xxx - add whether AAT font with morphing info for specific lang groups
  1305. if (rank > aMatchData->mMatchRank
  1306. || (rank == aMatchData->mMatchRank &&
  1307. Compare(fe->Name(), aMatchData->mBestMatch->Name()) > 0))
  1308. {
  1309. aMatchData->mBestMatch = fe;
  1310. aMatchData->mMatchedFamily = this;
  1311. aMatchData->mMatchRank = rank;
  1312. }
  1313. }
  1314. }
  1315. void
  1316. gfxFontFamily::SearchAllFontsForChar(GlobalFontMatch *aMatchData)
  1317. {
  1318. uint32_t i, numFonts = mAvailableFonts.Length();
  1319. for (i = 0; i < numFonts; i++) {
  1320. gfxFontEntry *fe = mAvailableFonts[i];
  1321. if (fe && fe->HasCharacter(aMatchData->mCh)) {
  1322. int32_t rank = RANK_MATCHED_CMAP;
  1323. rank += CalcStyleMatch(fe, aMatchData->mStyle);
  1324. if (rank > aMatchData->mMatchRank
  1325. || (rank == aMatchData->mMatchRank &&
  1326. Compare(fe->Name(), aMatchData->mBestMatch->Name()) > 0))
  1327. {
  1328. aMatchData->mBestMatch = fe;
  1329. aMatchData->mMatchedFamily = this;
  1330. aMatchData->mMatchRank = rank;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. /*static*/ void
  1336. gfxFontFamily::ReadOtherFamilyNamesForFace(const nsAString& aFamilyName,
  1337. const char *aNameData,
  1338. uint32_t aDataLength,
  1339. nsTArray<nsString>& aOtherFamilyNames,
  1340. bool useFullName)
  1341. {
  1342. const gfxFontUtils::NameHeader *nameHeader =
  1343. reinterpret_cast<const gfxFontUtils::NameHeader*>(aNameData);
  1344. uint32_t nameCount = nameHeader->count;
  1345. if (nameCount * sizeof(gfxFontUtils::NameRecord) > aDataLength) {
  1346. NS_WARNING("invalid font (name records)");
  1347. return;
  1348. }
  1349. const gfxFontUtils::NameRecord *nameRecord =
  1350. reinterpret_cast<const gfxFontUtils::NameRecord*>(aNameData + sizeof(gfxFontUtils::NameHeader));
  1351. uint32_t stringsBase = uint32_t(nameHeader->stringOffset);
  1352. for (uint32_t i = 0; i < nameCount; i++, nameRecord++) {
  1353. uint32_t nameLen = nameRecord->length;
  1354. uint32_t nameOff = nameRecord->offset; // offset from base of string storage
  1355. if (stringsBase + nameOff + nameLen > aDataLength) {
  1356. NS_WARNING("invalid font (name table strings)");
  1357. return;
  1358. }
  1359. uint16_t nameID = nameRecord->nameID;
  1360. if ((useFullName && nameID == gfxFontUtils::NAME_ID_FULL) ||
  1361. (!useFullName && (nameID == gfxFontUtils::NAME_ID_FAMILY ||
  1362. nameID == gfxFontUtils::NAME_ID_PREFERRED_FAMILY))) {
  1363. nsAutoString otherFamilyName;
  1364. bool ok = gfxFontUtils::DecodeFontName(aNameData + stringsBase + nameOff,
  1365. nameLen,
  1366. uint32_t(nameRecord->platformID),
  1367. uint32_t(nameRecord->encodingID),
  1368. uint32_t(nameRecord->languageID),
  1369. otherFamilyName);
  1370. // add if not same as canonical family name
  1371. if (ok && otherFamilyName != aFamilyName) {
  1372. aOtherFamilyNames.AppendElement(otherFamilyName);
  1373. }
  1374. }
  1375. }
  1376. }
  1377. // returns true if other names were found, false otherwise
  1378. bool
  1379. gfxFontFamily::ReadOtherFamilyNamesForFace(gfxPlatformFontList *aPlatformFontList,
  1380. hb_blob_t *aNameTable,
  1381. bool useFullName)
  1382. {
  1383. uint32_t dataLength;
  1384. const char *nameData = hb_blob_get_data(aNameTable, &dataLength);
  1385. AutoTArray<nsString,4> otherFamilyNames;
  1386. ReadOtherFamilyNamesForFace(mName, nameData, dataLength,
  1387. otherFamilyNames, useFullName);
  1388. uint32_t n = otherFamilyNames.Length();
  1389. for (uint32_t i = 0; i < n; i++) {
  1390. aPlatformFontList->AddOtherFamilyName(this, otherFamilyNames[i]);
  1391. }
  1392. return n != 0;
  1393. }
  1394. void
  1395. gfxFontFamily::ReadOtherFamilyNames(gfxPlatformFontList *aPlatformFontList)
  1396. {
  1397. if (mOtherFamilyNamesInitialized)
  1398. return;
  1399. mOtherFamilyNamesInitialized = true;
  1400. FindStyleVariations();
  1401. // read in other family names for the first face in the list
  1402. uint32_t i, numFonts = mAvailableFonts.Length();
  1403. const uint32_t kNAME = TRUETYPE_TAG('n','a','m','e');
  1404. for (i = 0; i < numFonts; ++i) {
  1405. gfxFontEntry *fe = mAvailableFonts[i];
  1406. if (!fe) {
  1407. continue;
  1408. }
  1409. gfxFontEntry::AutoTable nameTable(fe, kNAME);
  1410. if (!nameTable) {
  1411. continue;
  1412. }
  1413. mHasOtherFamilyNames = ReadOtherFamilyNamesForFace(aPlatformFontList,
  1414. nameTable);
  1415. break;
  1416. }
  1417. // read in other names for the first face in the list with the assumption
  1418. // that if extra names don't exist in that face then they don't exist in
  1419. // other faces for the same font
  1420. if (!mHasOtherFamilyNames)
  1421. return;
  1422. // read in names for all faces, needed to catch cases where fonts have
  1423. // family names for individual weights (e.g. Hiragino Kaku Gothic Pro W6)
  1424. for ( ; i < numFonts; i++) {
  1425. gfxFontEntry *fe = mAvailableFonts[i];
  1426. if (!fe) {
  1427. continue;
  1428. }
  1429. gfxFontEntry::AutoTable nameTable(fe, kNAME);
  1430. if (!nameTable) {
  1431. continue;
  1432. }
  1433. ReadOtherFamilyNamesForFace(aPlatformFontList, nameTable);
  1434. }
  1435. }
  1436. void
  1437. gfxFontFamily::ReadFaceNames(gfxPlatformFontList *aPlatformFontList,
  1438. bool aNeedFullnamePostscriptNames,
  1439. FontInfoData *aFontInfoData)
  1440. {
  1441. // if all needed names have already been read, skip
  1442. if (mOtherFamilyNamesInitialized &&
  1443. (mFaceNamesInitialized || !aNeedFullnamePostscriptNames))
  1444. return;
  1445. bool asyncFontLoaderDisabled = false;
  1446. if (!mOtherFamilyNamesInitialized &&
  1447. aFontInfoData &&
  1448. aFontInfoData->mLoadOtherNames &&
  1449. !asyncFontLoaderDisabled)
  1450. {
  1451. AutoTArray<nsString,4> otherFamilyNames;
  1452. bool foundOtherNames =
  1453. aFontInfoData->GetOtherFamilyNames(mName, otherFamilyNames);
  1454. if (foundOtherNames) {
  1455. uint32_t i, n = otherFamilyNames.Length();
  1456. for (i = 0; i < n; i++) {
  1457. aPlatformFontList->AddOtherFamilyName(this, otherFamilyNames[i]);
  1458. }
  1459. }
  1460. mOtherFamilyNamesInitialized = true;
  1461. }
  1462. // if all needed data has been initialized, return
  1463. if (mOtherFamilyNamesInitialized &&
  1464. (mFaceNamesInitialized || !aNeedFullnamePostscriptNames)) {
  1465. return;
  1466. }
  1467. FindStyleVariations(aFontInfoData);
  1468. // check again, as style enumeration code may have loaded names
  1469. if (mOtherFamilyNamesInitialized &&
  1470. (mFaceNamesInitialized || !aNeedFullnamePostscriptNames)) {
  1471. return;
  1472. }
  1473. uint32_t i, numFonts = mAvailableFonts.Length();
  1474. const uint32_t kNAME = TRUETYPE_TAG('n','a','m','e');
  1475. bool firstTime = true, readAllFaces = false;
  1476. for (i = 0; i < numFonts; ++i) {
  1477. gfxFontEntry *fe = mAvailableFonts[i];
  1478. if (!fe) {
  1479. continue;
  1480. }
  1481. nsAutoString fullname, psname;
  1482. bool foundFaceNames = false;
  1483. if (!mFaceNamesInitialized &&
  1484. aNeedFullnamePostscriptNames &&
  1485. aFontInfoData &&
  1486. aFontInfoData->mLoadFaceNames) {
  1487. aFontInfoData->GetFaceNames(fe->Name(), fullname, psname);
  1488. if (!fullname.IsEmpty()) {
  1489. aPlatformFontList->AddFullname(fe, fullname);
  1490. }
  1491. if (!psname.IsEmpty()) {
  1492. aPlatformFontList->AddPostscriptName(fe, psname);
  1493. }
  1494. foundFaceNames = true;
  1495. // found everything needed? skip to next font
  1496. if (mOtherFamilyNamesInitialized) {
  1497. continue;
  1498. }
  1499. }
  1500. // load directly from the name table
  1501. gfxFontEntry::AutoTable nameTable(fe, kNAME);
  1502. if (!nameTable) {
  1503. continue;
  1504. }
  1505. if (aNeedFullnamePostscriptNames && !foundFaceNames) {
  1506. if (gfxFontUtils::ReadCanonicalName(
  1507. nameTable, gfxFontUtils::NAME_ID_FULL, fullname) == NS_OK)
  1508. {
  1509. aPlatformFontList->AddFullname(fe, fullname);
  1510. }
  1511. if (gfxFontUtils::ReadCanonicalName(
  1512. nameTable, gfxFontUtils::NAME_ID_POSTSCRIPT, psname) == NS_OK)
  1513. {
  1514. aPlatformFontList->AddPostscriptName(fe, psname);
  1515. }
  1516. }
  1517. if (!mOtherFamilyNamesInitialized && (firstTime || readAllFaces)) {
  1518. bool foundOtherName = ReadOtherFamilyNamesForFace(aPlatformFontList,
  1519. nameTable);
  1520. // if the first face has a different name, scan all faces, otherwise
  1521. // assume the family doesn't have other names
  1522. if (firstTime && foundOtherName) {
  1523. mHasOtherFamilyNames = true;
  1524. readAllFaces = true;
  1525. }
  1526. firstTime = false;
  1527. }
  1528. // if not reading in any more names, skip other faces
  1529. if (!readAllFaces && !aNeedFullnamePostscriptNames) {
  1530. break;
  1531. }
  1532. }
  1533. mFaceNamesInitialized = true;
  1534. mOtherFamilyNamesInitialized = true;
  1535. }
  1536. gfxFontEntry*
  1537. gfxFontFamily::FindFont(const nsAString& aPostscriptName)
  1538. {
  1539. // find the font using a simple linear search
  1540. uint32_t numFonts = mAvailableFonts.Length();
  1541. for (uint32_t i = 0; i < numFonts; i++) {
  1542. gfxFontEntry *fe = mAvailableFonts[i].get();
  1543. if (fe && fe->Name() == aPostscriptName)
  1544. return fe;
  1545. }
  1546. return nullptr;
  1547. }
  1548. void
  1549. gfxFontFamily::ReadAllCMAPs(FontInfoData *aFontInfoData)
  1550. {
  1551. FindStyleVariations(aFontInfoData);
  1552. uint32_t i, numFonts = mAvailableFonts.Length();
  1553. for (i = 0; i < numFonts; i++) {
  1554. gfxFontEntry *fe = mAvailableFonts[i];
  1555. // don't try to load cmaps for downloadable fonts not yet loaded
  1556. if (!fe || fe->mIsUserFontContainer) {
  1557. continue;
  1558. }
  1559. fe->ReadCMAP(aFontInfoData);
  1560. mFamilyCharacterMap.Union(*(fe->mCharacterMap));
  1561. }
  1562. mFamilyCharacterMap.Compact();
  1563. mFamilyCharacterMapInitialized = true;
  1564. }
  1565. void
  1566. gfxFontFamily::AddSizeOfExcludingThis(MallocSizeOf aMallocSizeOf,
  1567. FontListSizes* aSizes) const
  1568. {
  1569. aSizes->mFontListSize +=
  1570. mName.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
  1571. aSizes->mCharMapsSize +=
  1572. mFamilyCharacterMap.SizeOfExcludingThis(aMallocSizeOf);
  1573. aSizes->mFontListSize +=
  1574. mAvailableFonts.ShallowSizeOfExcludingThis(aMallocSizeOf);
  1575. for (uint32_t i = 0; i < mAvailableFonts.Length(); ++i) {
  1576. gfxFontEntry *fe = mAvailableFonts[i];
  1577. if (fe) {
  1578. fe->AddSizeOfIncludingThis(aMallocSizeOf, aSizes);
  1579. }
  1580. }
  1581. }
  1582. void
  1583. gfxFontFamily::AddSizeOfIncludingThis(MallocSizeOf aMallocSizeOf,
  1584. FontListSizes* aSizes) const
  1585. {
  1586. aSizes->mFontListSize += aMallocSizeOf(this);
  1587. AddSizeOfExcludingThis(aMallocSizeOf, aSizes);
  1588. }