hb-ot-cmap-table.hh 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072
  1. /*
  2. * Copyright © 2014 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef HB_OT_CMAP_TABLE_HH
  27. #define HB_OT_CMAP_TABLE_HH
  28. #include "hb-ot-os2-table.hh"
  29. #include "hb-ot-shaper-arabic-pua.hh"
  30. #include "hb-open-type.hh"
  31. #include "hb-set.hh"
  32. /*
  33. * cmap -- Character to Glyph Index Mapping
  34. * https://docs.microsoft.com/en-us/typography/opentype/spec/cmap
  35. */
  36. #define HB_OT_TAG_cmap HB_TAG('c','m','a','p')
  37. namespace OT {
  38. struct CmapSubtableFormat0
  39. {
  40. bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
  41. {
  42. hb_codepoint_t gid = codepoint < 256 ? glyphIdArray[codepoint] : 0;
  43. if (unlikely (!gid))
  44. return false;
  45. *glyph = gid;
  46. return true;
  47. }
  48. unsigned get_language () const
  49. {
  50. return language;
  51. }
  52. void collect_unicodes (hb_set_t *out) const
  53. {
  54. for (unsigned int i = 0; i < 256; i++)
  55. if (glyphIdArray[i])
  56. out->add (i);
  57. }
  58. void collect_mapping (hb_set_t *unicodes, /* OUT */
  59. hb_map_t *mapping /* OUT */) const
  60. {
  61. for (unsigned i = 0; i < 256; i++)
  62. if (glyphIdArray[i])
  63. {
  64. hb_codepoint_t glyph = glyphIdArray[i];
  65. unicodes->add (i);
  66. mapping->set (i, glyph);
  67. }
  68. }
  69. bool sanitize (hb_sanitize_context_t *c) const
  70. {
  71. TRACE_SANITIZE (this);
  72. return_trace (c->check_struct (this));
  73. }
  74. protected:
  75. HBUINT16 format; /* Format number is set to 0. */
  76. HBUINT16 length; /* Byte length of this subtable. */
  77. HBUINT16 language; /* Ignore. */
  78. HBUINT8 glyphIdArray[256];/* An array that maps character
  79. * code to glyph index values. */
  80. public:
  81. DEFINE_SIZE_STATIC (6 + 256);
  82. };
  83. struct CmapSubtableFormat4
  84. {
  85. template<typename Iterator,
  86. typename Writer,
  87. hb_requires (hb_is_iterator (Iterator))>
  88. void to_ranges (Iterator it, Writer& range_writer)
  89. {
  90. hb_codepoint_t start_cp = 0, prev_run_start_cp = 0, run_start_cp = 0, end_cp = 0, last_gid = 0;
  91. int run_length = 0 , delta = 0, prev_delta = 0;
  92. enum {
  93. FIRST_SUB_RANGE,
  94. FOLLOWING_SUB_RANGE,
  95. } mode;
  96. while (it) {
  97. // Start a new range
  98. {
  99. const auto& pair = *it;
  100. start_cp = pair.first;
  101. prev_run_start_cp = start_cp;
  102. run_start_cp = start_cp;
  103. end_cp = start_cp;
  104. last_gid = pair.second;
  105. run_length = 1;
  106. prev_delta = 0;
  107. }
  108. delta = last_gid - start_cp;
  109. mode = FIRST_SUB_RANGE;
  110. it++;
  111. while (it) {
  112. // Process range
  113. const auto& pair = *it;
  114. hb_codepoint_t next_cp = pair.first;
  115. hb_codepoint_t next_gid = pair.second;
  116. if (next_cp != end_cp + 1) {
  117. // Current range is over, stop processing.
  118. break;
  119. }
  120. if (next_gid == last_gid + 1) {
  121. // The current run continues.
  122. end_cp = next_cp;
  123. run_length++;
  124. last_gid = next_gid;
  125. it++;
  126. continue;
  127. }
  128. // A new run is starting, decide if we want to commit the current run.
  129. int split_cost = (mode == FIRST_SUB_RANGE) ? 8 : 16;
  130. int run_cost = run_length * 2;
  131. if (run_cost >= split_cost) {
  132. commit_current_range(start_cp,
  133. prev_run_start_cp,
  134. run_start_cp,
  135. end_cp,
  136. delta,
  137. prev_delta,
  138. split_cost,
  139. range_writer);
  140. start_cp = next_cp;
  141. }
  142. // Start the new run
  143. mode = FOLLOWING_SUB_RANGE;
  144. prev_run_start_cp = run_start_cp;
  145. run_start_cp = next_cp;
  146. end_cp = next_cp;
  147. prev_delta = delta;
  148. delta = next_gid - run_start_cp;
  149. run_length = 1;
  150. last_gid = next_gid;
  151. it++;
  152. }
  153. // Finalize range
  154. commit_current_range (start_cp,
  155. prev_run_start_cp,
  156. run_start_cp,
  157. end_cp,
  158. delta,
  159. prev_delta,
  160. 8,
  161. range_writer);
  162. }
  163. if (likely (end_cp != 0xFFFF)) {
  164. range_writer (0xFFFF, 0xFFFF, 1);
  165. }
  166. }
  167. /*
  168. * Writes the current range as either one or two ranges depending on what is most efficient.
  169. */
  170. template<typename Writer>
  171. void commit_current_range (hb_codepoint_t start,
  172. hb_codepoint_t prev_run_start,
  173. hb_codepoint_t run_start,
  174. hb_codepoint_t end,
  175. int run_delta,
  176. int previous_run_delta,
  177. int split_cost,
  178. Writer& range_writer) {
  179. bool should_split = false;
  180. if (start < run_start && run_start < end) {
  181. int run_cost = (end - run_start + 1) * 2;
  182. if (run_cost >= split_cost) {
  183. should_split = true;
  184. }
  185. }
  186. // TODO(grieger): handle case where delta is legitimately 0, mark range offset array instead?
  187. if (should_split) {
  188. if (start == prev_run_start)
  189. range_writer (start, run_start - 1, previous_run_delta);
  190. else
  191. range_writer (start, run_start - 1, 0);
  192. range_writer (run_start, end, run_delta);
  193. return;
  194. }
  195. if (start == run_start) {
  196. // Range is only a run
  197. range_writer (start, end, run_delta);
  198. return;
  199. }
  200. // Write only a single non-run range.
  201. range_writer (start, end, 0);
  202. }
  203. template<typename Iterator,
  204. hb_requires (hb_is_iterator (Iterator))>
  205. unsigned serialize_find_segcount (Iterator it) {
  206. struct Counter {
  207. unsigned segcount = 0;
  208. void operator() (hb_codepoint_t start,
  209. hb_codepoint_t end,
  210. int delta) {
  211. segcount++;
  212. }
  213. } counter;
  214. to_ranges (+it, counter);
  215. return counter.segcount;
  216. }
  217. template<typename Iterator,
  218. hb_requires (hb_is_iterator (Iterator))>
  219. bool serialize_start_end_delta_arrays (hb_serialize_context_t *c,
  220. Iterator it,
  221. int segcount)
  222. {
  223. struct Writer {
  224. hb_serialize_context_t *serializer_;
  225. HBUINT16* end_code_;
  226. HBUINT16* start_code_;
  227. HBINT16* id_delta_;
  228. int index_;
  229. Writer(hb_serialize_context_t *serializer)
  230. : serializer_(serializer),
  231. end_code_(nullptr),
  232. start_code_(nullptr),
  233. id_delta_(nullptr),
  234. index_ (0) {}
  235. void operator() (hb_codepoint_t start,
  236. hb_codepoint_t end,
  237. int delta) {
  238. start_code_[index_] = start;
  239. end_code_[index_] = end;
  240. id_delta_[index_] = delta;
  241. index_++;
  242. }
  243. } writer(c);
  244. writer.end_code_ = c->allocate_size<HBUINT16> (HBUINT16::static_size * segcount);
  245. c->allocate_size<HBUINT16> (2); // padding
  246. writer.start_code_ = c->allocate_size<HBUINT16> (HBUINT16::static_size * segcount);
  247. writer.id_delta_ = c->allocate_size<HBINT16> (HBINT16::static_size * segcount);
  248. if (unlikely (!writer.end_code_ || !writer.start_code_ || !writer.id_delta_)) return false;
  249. to_ranges (+it, writer);
  250. return true;
  251. }
  252. template<typename Iterator,
  253. hb_requires (hb_is_iterator (Iterator))>
  254. HBUINT16* serialize_rangeoffset_glyid (hb_serialize_context_t *c,
  255. Iterator it,
  256. HBUINT16 *endCode,
  257. HBUINT16 *startCode,
  258. HBINT16 *idDelta,
  259. unsigned segcount)
  260. {
  261. hb_map_t cp_to_gid { it };
  262. HBUINT16 *idRangeOffset = c->allocate_size<HBUINT16> (HBUINT16::static_size * segcount);
  263. if (unlikely (!c->check_success (idRangeOffset))) return nullptr;
  264. if (unlikely ((char *)idRangeOffset - (char *)idDelta != (int) segcount * (int) HBINT16::static_size)) return nullptr;
  265. for (unsigned i : + hb_range (segcount)
  266. | hb_filter ([&] (const unsigned _) { return idDelta[_] == 0; }))
  267. {
  268. idRangeOffset[i] = 2 * (c->start_embed<HBUINT16> () - idRangeOffset - i);
  269. for (hb_codepoint_t cp = startCode[i]; cp <= endCode[i]; cp++)
  270. {
  271. HBUINT16 gid;
  272. gid = cp_to_gid[cp];
  273. c->copy<HBUINT16> (gid);
  274. }
  275. }
  276. return idRangeOffset;
  277. }
  278. template<typename Iterator,
  279. hb_requires (hb_is_iterator (Iterator))>
  280. void serialize (hb_serialize_context_t *c,
  281. Iterator it)
  282. {
  283. auto format4_iter =
  284. + it
  285. | hb_filter ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> _)
  286. { return _.first <= 0xFFFF; })
  287. ;
  288. if (!format4_iter) return;
  289. unsigned table_initpos = c->length ();
  290. if (unlikely (!c->extend_min (this))) return;
  291. this->format = 4;
  292. hb_vector_t<hb_pair_t<hb_codepoint_t, hb_codepoint_t>> cp_to_gid {
  293. format4_iter
  294. };
  295. //serialize endCode[], startCode[], idDelta[]
  296. HBUINT16* endCode = c->start_embed<HBUINT16> ();
  297. unsigned segcount = serialize_find_segcount (cp_to_gid.iter());
  298. if (unlikely (!serialize_start_end_delta_arrays (c, cp_to_gid.iter(), segcount)))
  299. return;
  300. HBUINT16 *startCode = endCode + segcount + 1;
  301. HBINT16 *idDelta = ((HBINT16*)startCode) + segcount;
  302. HBUINT16 *idRangeOffset = serialize_rangeoffset_glyid (c,
  303. cp_to_gid.iter (),
  304. endCode,
  305. startCode,
  306. idDelta,
  307. segcount);
  308. if (unlikely (!c->check_success (idRangeOffset))) return;
  309. this->length = c->length () - table_initpos;
  310. if ((long long) this->length != (long long) c->length () - table_initpos)
  311. {
  312. // Length overflowed. Discard the current object before setting the error condition, otherwise
  313. // discard is a noop which prevents the higher level code from reverting the serializer to the
  314. // pre-error state in cmap4 overflow handling code.
  315. c->pop_discard ();
  316. c->err (HB_SERIALIZE_ERROR_INT_OVERFLOW);
  317. return;
  318. }
  319. this->segCountX2 = segcount * 2;
  320. this->entrySelector = hb_max (1u, hb_bit_storage (segcount)) - 1;
  321. this->searchRange = 2 * (1u << this->entrySelector);
  322. this->rangeShift = segcount * 2 > this->searchRange
  323. ? 2 * segcount - this->searchRange
  324. : 0;
  325. }
  326. unsigned get_language () const
  327. {
  328. return language;
  329. }
  330. struct accelerator_t
  331. {
  332. accelerator_t () {}
  333. accelerator_t (const CmapSubtableFormat4 *subtable) { init (subtable); }
  334. void init (const CmapSubtableFormat4 *subtable)
  335. {
  336. segCount = subtable->segCountX2 / 2;
  337. endCount = subtable->values.arrayZ;
  338. startCount = endCount + segCount + 1;
  339. idDelta = startCount + segCount;
  340. idRangeOffset = idDelta + segCount;
  341. glyphIdArray = idRangeOffset + segCount;
  342. glyphIdArrayLength = (subtable->length - 16 - 8 * segCount) / 2;
  343. }
  344. bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
  345. {
  346. struct CustomRange
  347. {
  348. int cmp (hb_codepoint_t k,
  349. unsigned distance) const
  350. {
  351. if (k > last) return +1;
  352. if (k < (&last)[distance]) return -1;
  353. return 0;
  354. }
  355. HBUINT16 last;
  356. };
  357. const HBUINT16 *found = hb_bsearch (codepoint,
  358. this->endCount,
  359. this->segCount,
  360. 2,
  361. _hb_cmp_method<hb_codepoint_t, CustomRange, unsigned>,
  362. this->segCount + 1);
  363. if (unlikely (!found))
  364. return false;
  365. unsigned int i = found - endCount;
  366. hb_codepoint_t gid;
  367. unsigned int rangeOffset = this->idRangeOffset[i];
  368. if (rangeOffset == 0)
  369. gid = codepoint + this->idDelta[i];
  370. else
  371. {
  372. /* Somebody has been smoking... */
  373. unsigned int index = rangeOffset / 2 + (codepoint - this->startCount[i]) + i - this->segCount;
  374. if (unlikely (index >= this->glyphIdArrayLength))
  375. return false;
  376. gid = this->glyphIdArray[index];
  377. if (unlikely (!gid))
  378. return false;
  379. gid += this->idDelta[i];
  380. }
  381. gid &= 0xFFFFu;
  382. if (unlikely (!gid))
  383. return false;
  384. *glyph = gid;
  385. return true;
  386. }
  387. HB_INTERNAL static bool get_glyph_func (const void *obj, hb_codepoint_t codepoint, hb_codepoint_t *glyph)
  388. { return ((const accelerator_t *) obj)->get_glyph (codepoint, glyph); }
  389. void collect_unicodes (hb_set_t *out) const
  390. {
  391. unsigned int count = this->segCount;
  392. if (count && this->startCount[count - 1] == 0xFFFFu)
  393. count--; /* Skip sentinel segment. */
  394. for (unsigned int i = 0; i < count; i++)
  395. {
  396. hb_codepoint_t start = this->startCount[i];
  397. hb_codepoint_t end = this->endCount[i];
  398. unsigned int rangeOffset = this->idRangeOffset[i];
  399. out->add_range(start, end);
  400. if (rangeOffset == 0)
  401. {
  402. for (hb_codepoint_t codepoint = start; codepoint <= end; codepoint++)
  403. {
  404. hb_codepoint_t gid = (codepoint + this->idDelta[i]) & 0xFFFFu;
  405. if (unlikely (!gid))
  406. out->del(codepoint);
  407. }
  408. }
  409. else
  410. {
  411. for (hb_codepoint_t codepoint = start; codepoint <= end; codepoint++)
  412. {
  413. unsigned int index = rangeOffset / 2 + (codepoint - this->startCount[i]) + i - this->segCount;
  414. if (unlikely (index >= this->glyphIdArrayLength))
  415. {
  416. out->del_range (codepoint, end);
  417. break;
  418. }
  419. hb_codepoint_t gid = this->glyphIdArray[index];
  420. if (unlikely (!gid))
  421. out->del(codepoint);
  422. }
  423. }
  424. }
  425. }
  426. void collect_mapping (hb_set_t *unicodes, /* OUT */
  427. hb_map_t *mapping /* OUT */) const
  428. {
  429. // TODO(grieger): optimize similar to collect_unicodes
  430. // (ie. use add_range())
  431. unsigned count = this->segCount;
  432. if (count && this->startCount[count - 1] == 0xFFFFu)
  433. count--; /* Skip sentinel segment. */
  434. for (unsigned i = 0; i < count; i++)
  435. {
  436. hb_codepoint_t start = this->startCount[i];
  437. hb_codepoint_t end = this->endCount[i];
  438. unsigned rangeOffset = this->idRangeOffset[i];
  439. if (rangeOffset == 0)
  440. {
  441. for (hb_codepoint_t codepoint = start; codepoint <= end; codepoint++)
  442. {
  443. hb_codepoint_t gid = (codepoint + this->idDelta[i]) & 0xFFFFu;
  444. if (unlikely (!gid))
  445. continue;
  446. unicodes->add (codepoint);
  447. mapping->set (codepoint, gid);
  448. }
  449. }
  450. else
  451. {
  452. for (hb_codepoint_t codepoint = start; codepoint <= end; codepoint++)
  453. {
  454. unsigned index = rangeOffset / 2 + (codepoint - this->startCount[i]) + i - this->segCount;
  455. if (unlikely (index >= this->glyphIdArrayLength))
  456. break;
  457. hb_codepoint_t gid = this->glyphIdArray[index];
  458. if (unlikely (!gid))
  459. continue;
  460. unicodes->add (codepoint);
  461. mapping->set (codepoint, gid);
  462. }
  463. }
  464. }
  465. }
  466. const HBUINT16 *endCount;
  467. const HBUINT16 *startCount;
  468. const HBUINT16 *idDelta;
  469. const HBUINT16 *idRangeOffset;
  470. const HBUINT16 *glyphIdArray;
  471. unsigned int segCount;
  472. unsigned int glyphIdArrayLength;
  473. };
  474. bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
  475. {
  476. accelerator_t accel (this);
  477. return accel.get_glyph_func (&accel, codepoint, glyph);
  478. }
  479. void collect_unicodes (hb_set_t *out) const
  480. {
  481. accelerator_t accel (this);
  482. accel.collect_unicodes (out);
  483. }
  484. void collect_mapping (hb_set_t *unicodes, /* OUT */
  485. hb_map_t *mapping /* OUT */) const
  486. {
  487. accelerator_t accel (this);
  488. accel.collect_mapping (unicodes, mapping);
  489. }
  490. bool sanitize (hb_sanitize_context_t *c) const
  491. {
  492. TRACE_SANITIZE (this);
  493. if (unlikely (!c->check_struct (this)))
  494. return_trace (false);
  495. if (unlikely (!c->check_range (this, length)))
  496. {
  497. /* Some broken fonts have too long of a "length" value.
  498. * If that is the case, just change the value to truncate
  499. * the subtable at the end of the blob. */
  500. uint16_t new_length = (uint16_t) hb_min ((uintptr_t) 65535,
  501. (uintptr_t) (c->end -
  502. (char *) this));
  503. if (!c->try_set (&length, new_length))
  504. return_trace (false);
  505. }
  506. return_trace (16 + 4 * (unsigned int) segCountX2 <= length);
  507. }
  508. protected:
  509. HBUINT16 format; /* Format number is set to 4. */
  510. HBUINT16 length; /* This is the length in bytes of the
  511. * subtable. */
  512. HBUINT16 language; /* Ignore. */
  513. HBUINT16 segCountX2; /* 2 x segCount. */
  514. HBUINT16 searchRange; /* 2 * (2**floor(log2(segCount))) */
  515. HBUINT16 entrySelector; /* log2(searchRange/2) */
  516. HBUINT16 rangeShift; /* 2 x segCount - searchRange */
  517. UnsizedArrayOf<HBUINT16>
  518. values;
  519. #if 0
  520. HBUINT16 endCount[segCount]; /* End characterCode for each segment,
  521. * last=0xFFFFu. */
  522. HBUINT16 reservedPad; /* Set to 0. */
  523. HBUINT16 startCount[segCount]; /* Start character code for each segment. */
  524. HBINT16 idDelta[segCount]; /* Delta for all character codes in segment. */
  525. HBUINT16 idRangeOffset[segCount];/* Offsets into glyphIdArray or 0 */
  526. UnsizedArrayOf<HBUINT16>
  527. glyphIdArray; /* Glyph index array (arbitrary length) */
  528. #endif
  529. public:
  530. DEFINE_SIZE_ARRAY (14, values);
  531. };
  532. struct CmapSubtableLongGroup
  533. {
  534. friend struct CmapSubtableFormat12;
  535. friend struct CmapSubtableFormat13;
  536. template<typename U>
  537. friend struct CmapSubtableLongSegmented;
  538. friend struct cmap;
  539. int cmp (hb_codepoint_t codepoint) const
  540. {
  541. if (codepoint < startCharCode) return -1;
  542. if (codepoint > endCharCode) return +1;
  543. return 0;
  544. }
  545. bool sanitize (hb_sanitize_context_t *c) const
  546. {
  547. TRACE_SANITIZE (this);
  548. return_trace (c->check_struct (this));
  549. }
  550. private:
  551. HBUINT32 startCharCode; /* First character code in this group. */
  552. HBUINT32 endCharCode; /* Last character code in this group. */
  553. HBUINT32 glyphID; /* Glyph index; interpretation depends on
  554. * subtable format. */
  555. public:
  556. DEFINE_SIZE_STATIC (12);
  557. };
  558. DECLARE_NULL_NAMESPACE_BYTES (OT, CmapSubtableLongGroup);
  559. template <typename UINT>
  560. struct CmapSubtableTrimmed
  561. {
  562. bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
  563. {
  564. /* Rely on our implicit array bound-checking. */
  565. hb_codepoint_t gid = glyphIdArray[codepoint - startCharCode];
  566. if (unlikely (!gid))
  567. return false;
  568. *glyph = gid;
  569. return true;
  570. }
  571. unsigned get_language () const
  572. {
  573. return language;
  574. }
  575. void collect_unicodes (hb_set_t *out) const
  576. {
  577. hb_codepoint_t start = startCharCode;
  578. unsigned int count = glyphIdArray.len;
  579. for (unsigned int i = 0; i < count; i++)
  580. if (glyphIdArray[i])
  581. out->add (start + i);
  582. }
  583. void collect_mapping (hb_set_t *unicodes, /* OUT */
  584. hb_map_t *mapping /* OUT */) const
  585. {
  586. hb_codepoint_t start_cp = startCharCode;
  587. unsigned count = glyphIdArray.len;
  588. for (unsigned i = 0; i < count; i++)
  589. if (glyphIdArray[i])
  590. {
  591. hb_codepoint_t unicode = start_cp + i;
  592. hb_codepoint_t glyphid = glyphIdArray[i];
  593. unicodes->add (unicode);
  594. mapping->set (unicode, glyphid);
  595. }
  596. }
  597. bool sanitize (hb_sanitize_context_t *c) const
  598. {
  599. TRACE_SANITIZE (this);
  600. return_trace (c->check_struct (this) && glyphIdArray.sanitize (c));
  601. }
  602. protected:
  603. UINT formatReserved; /* Subtable format and (maybe) padding. */
  604. UINT length; /* Byte length of this subtable. */
  605. UINT language; /* Ignore. */
  606. UINT startCharCode; /* First character code covered. */
  607. ArrayOf<HBGlyphID16, UINT>
  608. glyphIdArray; /* Array of glyph index values for character
  609. * codes in the range. */
  610. public:
  611. DEFINE_SIZE_ARRAY (5 * sizeof (UINT), glyphIdArray);
  612. };
  613. struct CmapSubtableFormat6 : CmapSubtableTrimmed<HBUINT16> {};
  614. struct CmapSubtableFormat10 : CmapSubtableTrimmed<HBUINT32> {};
  615. template <typename T>
  616. struct CmapSubtableLongSegmented
  617. {
  618. friend struct cmap;
  619. bool get_glyph (hb_codepoint_t codepoint, hb_codepoint_t *glyph) const
  620. {
  621. hb_codepoint_t gid = T::group_get_glyph (groups.bsearch (codepoint), codepoint);
  622. if (unlikely (!gid))
  623. return false;
  624. *glyph = gid;
  625. return true;
  626. }
  627. unsigned get_language () const
  628. {
  629. return language;
  630. }
  631. void collect_unicodes (hb_set_t *out, unsigned int num_glyphs) const
  632. {
  633. for (unsigned int i = 0; i < this->groups.len; i++)
  634. {
  635. hb_codepoint_t start = this->groups[i].startCharCode;
  636. hb_codepoint_t end = hb_min ((hb_codepoint_t) this->groups[i].endCharCode,
  637. (hb_codepoint_t) HB_UNICODE_MAX);
  638. hb_codepoint_t gid = this->groups[i].glyphID;
  639. if (!gid)
  640. {
  641. /* Intention is: if (hb_is_same (T, CmapSubtableFormat13)) continue; */
  642. if (! T::group_get_glyph (this->groups[i], end)) continue;
  643. start++;
  644. gid++;
  645. }
  646. if (unlikely ((unsigned int) gid >= num_glyphs)) continue;
  647. if (unlikely ((unsigned int) (gid + end - start) >= num_glyphs))
  648. end = start + (hb_codepoint_t) num_glyphs - gid;
  649. out->add_range (start, hb_min (end, 0x10FFFFu));
  650. }
  651. }
  652. void collect_mapping (hb_set_t *unicodes, /* OUT */
  653. hb_map_t *mapping, /* OUT */
  654. unsigned num_glyphs) const
  655. {
  656. hb_codepoint_t last_end = 0;
  657. for (unsigned i = 0; i < this->groups.len; i++)
  658. {
  659. hb_codepoint_t start = this->groups[i].startCharCode;
  660. hb_codepoint_t end = hb_min ((hb_codepoint_t) this->groups[i].endCharCode,
  661. (hb_codepoint_t) HB_UNICODE_MAX);
  662. if (unlikely (start > end || start < last_end)) {
  663. // Range is not in order and is invalid, skip it.
  664. continue;
  665. }
  666. last_end = end;
  667. hb_codepoint_t gid = this->groups[i].glyphID;
  668. if (!gid)
  669. {
  670. /* Intention is: if (hb_is_same (T, CmapSubtableFormat13)) continue; */
  671. if (! T::group_get_glyph (this->groups[i], end)) continue;
  672. start++;
  673. gid++;
  674. }
  675. if (unlikely ((unsigned int) gid >= num_glyphs)) continue;
  676. if (unlikely ((unsigned int) (gid + end - start) >= num_glyphs))
  677. end = start + (hb_codepoint_t) num_glyphs - gid;
  678. for (unsigned cp = start; cp <= end; cp++)
  679. {
  680. unicodes->add (cp);
  681. mapping->set (cp, gid);
  682. gid++;
  683. }
  684. }
  685. }
  686. bool sanitize (hb_sanitize_context_t *c) const
  687. {
  688. TRACE_SANITIZE (this);
  689. return_trace (c->check_struct (this) && groups.sanitize (c));
  690. }
  691. protected:
  692. HBUINT16 format; /* Subtable format; set to 12. */
  693. HBUINT16 reserved; /* Reserved; set to 0. */
  694. HBUINT32 length; /* Byte length of this subtable. */
  695. HBUINT32 language; /* Ignore. */
  696. SortedArray32Of<CmapSubtableLongGroup>
  697. groups; /* Groupings. */
  698. public:
  699. DEFINE_SIZE_ARRAY (16, groups);
  700. };
  701. struct CmapSubtableFormat12 : CmapSubtableLongSegmented<CmapSubtableFormat12>
  702. {
  703. static hb_codepoint_t group_get_glyph (const CmapSubtableLongGroup &group,
  704. hb_codepoint_t u)
  705. { return likely (group.startCharCode <= group.endCharCode) ?
  706. group.glyphID + (u - group.startCharCode) : 0; }
  707. template<typename Iterator,
  708. hb_requires (hb_is_iterator (Iterator))>
  709. void serialize (hb_serialize_context_t *c,
  710. Iterator it)
  711. {
  712. if (!it) return;
  713. unsigned table_initpos = c->length ();
  714. if (unlikely (!c->extend_min (this))) return;
  715. hb_codepoint_t startCharCode = (hb_codepoint_t) -1, endCharCode = (hb_codepoint_t) -1;
  716. hb_codepoint_t glyphID = 0;
  717. for (const auto& _ : +it)
  718. {
  719. if (startCharCode == (hb_codepoint_t) -1)
  720. {
  721. startCharCode = _.first;
  722. endCharCode = _.first;
  723. glyphID = _.second;
  724. }
  725. else if (!_is_gid_consecutive (endCharCode, startCharCode, glyphID, _.first, _.second))
  726. {
  727. CmapSubtableLongGroup grouprecord;
  728. grouprecord.startCharCode = startCharCode;
  729. grouprecord.endCharCode = endCharCode;
  730. grouprecord.glyphID = glyphID;
  731. c->copy<CmapSubtableLongGroup> (grouprecord);
  732. startCharCode = _.first;
  733. endCharCode = _.first;
  734. glyphID = _.second;
  735. }
  736. else
  737. endCharCode = _.first;
  738. }
  739. CmapSubtableLongGroup record;
  740. record.startCharCode = startCharCode;
  741. record.endCharCode = endCharCode;
  742. record.glyphID = glyphID;
  743. c->copy<CmapSubtableLongGroup> (record);
  744. this->format = 12;
  745. this->reserved = 0;
  746. this->length = c->length () - table_initpos;
  747. this->groups.len = (this->length - min_size) / CmapSubtableLongGroup::static_size;
  748. }
  749. static size_t get_sub_table_size (const hb_sorted_vector_t<CmapSubtableLongGroup> &groups_data)
  750. { return 16 + 12 * groups_data.length; }
  751. private:
  752. static bool _is_gid_consecutive (hb_codepoint_t endCharCode,
  753. hb_codepoint_t startCharCode,
  754. hb_codepoint_t glyphID,
  755. hb_codepoint_t cp,
  756. hb_codepoint_t new_gid)
  757. {
  758. return (cp - 1 == endCharCode) &&
  759. new_gid == glyphID + (cp - startCharCode);
  760. }
  761. };
  762. struct CmapSubtableFormat13 : CmapSubtableLongSegmented<CmapSubtableFormat13>
  763. {
  764. static hb_codepoint_t group_get_glyph (const CmapSubtableLongGroup &group,
  765. hb_codepoint_t u HB_UNUSED)
  766. { return group.glyphID; }
  767. };
  768. typedef enum
  769. {
  770. GLYPH_VARIANT_NOT_FOUND = 0,
  771. GLYPH_VARIANT_FOUND = 1,
  772. GLYPH_VARIANT_USE_DEFAULT = 2
  773. } glyph_variant_t;
  774. struct UnicodeValueRange
  775. {
  776. int cmp (const hb_codepoint_t &codepoint) const
  777. {
  778. if (codepoint < startUnicodeValue) return -1;
  779. if (codepoint > startUnicodeValue + additionalCount) return +1;
  780. return 0;
  781. }
  782. bool sanitize (hb_sanitize_context_t *c) const
  783. {
  784. TRACE_SANITIZE (this);
  785. return_trace (c->check_struct (this));
  786. }
  787. HBUINT24 startUnicodeValue; /* First value in this range. */
  788. HBUINT8 additionalCount; /* Number of additional values in this
  789. * range. */
  790. public:
  791. DEFINE_SIZE_STATIC (4);
  792. };
  793. struct DefaultUVS : SortedArray32Of<UnicodeValueRange>
  794. {
  795. void collect_unicodes (hb_set_t *out) const
  796. {
  797. unsigned int count = len;
  798. for (unsigned int i = 0; i < count; i++)
  799. {
  800. hb_codepoint_t first = arrayZ[i].startUnicodeValue;
  801. hb_codepoint_t last = hb_min ((hb_codepoint_t) (first + arrayZ[i].additionalCount),
  802. (hb_codepoint_t) HB_UNICODE_MAX);
  803. out->add_range (first, last);
  804. }
  805. }
  806. DefaultUVS* copy (hb_serialize_context_t *c,
  807. const hb_set_t *unicodes) const
  808. {
  809. DefaultUVS *out = c->start_embed<DefaultUVS> ();
  810. if (unlikely (!out)) return nullptr;
  811. auto snap = c->snapshot ();
  812. HBUINT32 len;
  813. len = 0;
  814. if (unlikely (!c->copy<HBUINT32> (len))) return nullptr;
  815. unsigned init_len = c->length ();
  816. if (this->len > unicodes->get_population () * hb_bit_storage ((unsigned) this->len))
  817. {
  818. hb_codepoint_t start = HB_SET_VALUE_INVALID;
  819. hb_codepoint_t end = HB_SET_VALUE_INVALID;
  820. for (hb_codepoint_t u = HB_SET_VALUE_INVALID;
  821. unicodes->next (&u);)
  822. {
  823. if (!as_array ().bsearch (u))
  824. continue;
  825. if (start == HB_SET_VALUE_INVALID)
  826. {
  827. start = u;
  828. end = start - 1;
  829. }
  830. if (end + 1 != u || end - start == 255)
  831. {
  832. UnicodeValueRange rec;
  833. rec.startUnicodeValue = start;
  834. rec.additionalCount = end - start;
  835. c->copy<UnicodeValueRange> (rec);
  836. start = u;
  837. }
  838. end = u;
  839. }
  840. if (start != HB_SET_VALUE_INVALID)
  841. {
  842. UnicodeValueRange rec;
  843. rec.startUnicodeValue = start;
  844. rec.additionalCount = end - start;
  845. c->copy<UnicodeValueRange> (rec);
  846. }
  847. }
  848. else
  849. {
  850. hb_codepoint_t lastCode = HB_SET_VALUE_INVALID;
  851. int count = -1;
  852. for (const UnicodeValueRange& _ : *this)
  853. {
  854. hb_codepoint_t curEntry = (hb_codepoint_t) (_.startUnicodeValue - 1);
  855. hb_codepoint_t end = curEntry + _.additionalCount + 2;
  856. for (; unicodes->next (&curEntry) && curEntry < end;)
  857. {
  858. count += 1;
  859. if (lastCode == HB_SET_VALUE_INVALID)
  860. lastCode = curEntry;
  861. else if (lastCode + count != curEntry)
  862. {
  863. UnicodeValueRange rec;
  864. rec.startUnicodeValue = lastCode;
  865. rec.additionalCount = count - 1;
  866. c->copy<UnicodeValueRange> (rec);
  867. lastCode = curEntry;
  868. count = 0;
  869. }
  870. }
  871. }
  872. if (lastCode != HB_MAP_VALUE_INVALID)
  873. {
  874. UnicodeValueRange rec;
  875. rec.startUnicodeValue = lastCode;
  876. rec.additionalCount = count;
  877. c->copy<UnicodeValueRange> (rec);
  878. }
  879. }
  880. if (c->length () - init_len == 0)
  881. {
  882. c->revert (snap);
  883. return nullptr;
  884. }
  885. else
  886. {
  887. if (unlikely (!c->check_assign (out->len,
  888. (c->length () - init_len) / UnicodeValueRange::static_size,
  889. HB_SERIALIZE_ERROR_INT_OVERFLOW))) return nullptr;
  890. return out;
  891. }
  892. }
  893. public:
  894. DEFINE_SIZE_ARRAY (4, *this);
  895. };
  896. struct UVSMapping
  897. {
  898. int cmp (const hb_codepoint_t &codepoint) const
  899. { return unicodeValue.cmp (codepoint); }
  900. bool sanitize (hb_sanitize_context_t *c) const
  901. {
  902. TRACE_SANITIZE (this);
  903. return_trace (c->check_struct (this));
  904. }
  905. HBUINT24 unicodeValue; /* Base Unicode value of the UVS */
  906. HBGlyphID16 glyphID; /* Glyph ID of the UVS */
  907. public:
  908. DEFINE_SIZE_STATIC (5);
  909. };
  910. struct NonDefaultUVS : SortedArray32Of<UVSMapping>
  911. {
  912. void collect_unicodes (hb_set_t *out) const
  913. {
  914. for (const auto& a : as_array ())
  915. out->add (a.unicodeValue);
  916. }
  917. void collect_mapping (hb_set_t *unicodes, /* OUT */
  918. hb_map_t *mapping /* OUT */) const
  919. {
  920. for (const auto& a : as_array ())
  921. {
  922. hb_codepoint_t unicode = a.unicodeValue;
  923. hb_codepoint_t glyphid = a.glyphID;
  924. unicodes->add (unicode);
  925. mapping->set (unicode, glyphid);
  926. }
  927. }
  928. void closure_glyphs (const hb_set_t *unicodes,
  929. hb_set_t *glyphset) const
  930. {
  931. + as_array ()
  932. | hb_filter (unicodes, &UVSMapping::unicodeValue)
  933. | hb_map (&UVSMapping::glyphID)
  934. | hb_sink (glyphset)
  935. ;
  936. }
  937. NonDefaultUVS* copy (hb_serialize_context_t *c,
  938. const hb_set_t *unicodes,
  939. const hb_set_t *glyphs_requested,
  940. const hb_map_t *glyph_map) const
  941. {
  942. NonDefaultUVS *out = c->start_embed<NonDefaultUVS> ();
  943. if (unlikely (!out)) return nullptr;
  944. auto it =
  945. + as_array ()
  946. | hb_filter ([&] (const UVSMapping& _)
  947. {
  948. return unicodes->has (_.unicodeValue) || glyphs_requested->has (_.glyphID);
  949. })
  950. ;
  951. if (!it) return nullptr;
  952. HBUINT32 len;
  953. len = it.len ();
  954. if (unlikely (!c->copy<HBUINT32> (len))) return nullptr;
  955. for (const UVSMapping& _ : it)
  956. {
  957. UVSMapping mapping;
  958. mapping.unicodeValue = _.unicodeValue;
  959. mapping.glyphID = glyph_map->get (_.glyphID);
  960. c->copy<UVSMapping> (mapping);
  961. }
  962. return out;
  963. }
  964. public:
  965. DEFINE_SIZE_ARRAY (4, *this);
  966. };
  967. struct VariationSelectorRecord
  968. {
  969. glyph_variant_t get_glyph (hb_codepoint_t codepoint,
  970. hb_codepoint_t *glyph,
  971. const void *base) const
  972. {
  973. if ((base+defaultUVS).bfind (codepoint))
  974. return GLYPH_VARIANT_USE_DEFAULT;
  975. const UVSMapping &nonDefault = (base+nonDefaultUVS).bsearch (codepoint);
  976. if (nonDefault.glyphID)
  977. {
  978. *glyph = nonDefault.glyphID;
  979. return GLYPH_VARIANT_FOUND;
  980. }
  981. return GLYPH_VARIANT_NOT_FOUND;
  982. }
  983. VariationSelectorRecord(const VariationSelectorRecord& other)
  984. {
  985. *this = other;
  986. }
  987. void operator= (const VariationSelectorRecord& other)
  988. {
  989. varSelector = other.varSelector;
  990. HBUINT32 offset = other.defaultUVS;
  991. defaultUVS = offset;
  992. offset = other.nonDefaultUVS;
  993. nonDefaultUVS = offset;
  994. }
  995. void collect_unicodes (hb_set_t *out, const void *base) const
  996. {
  997. (base+defaultUVS).collect_unicodes (out);
  998. (base+nonDefaultUVS).collect_unicodes (out);
  999. }
  1000. void collect_mapping (const void *base,
  1001. hb_set_t *unicodes, /* OUT */
  1002. hb_map_t *mapping /* OUT */) const
  1003. {
  1004. (base+defaultUVS).collect_unicodes (unicodes);
  1005. (base+nonDefaultUVS).collect_mapping (unicodes, mapping);
  1006. }
  1007. int cmp (const hb_codepoint_t &variation_selector) const
  1008. { return varSelector.cmp (variation_selector); }
  1009. bool sanitize (hb_sanitize_context_t *c, const void *base) const
  1010. {
  1011. TRACE_SANITIZE (this);
  1012. return_trace (c->check_struct (this) &&
  1013. defaultUVS.sanitize (c, base) &&
  1014. nonDefaultUVS.sanitize (c, base));
  1015. }
  1016. hb_pair_t<unsigned, unsigned>
  1017. copy (hb_serialize_context_t *c,
  1018. const hb_set_t *unicodes,
  1019. const hb_set_t *glyphs_requested,
  1020. const hb_map_t *glyph_map,
  1021. const void *base) const
  1022. {
  1023. auto snap = c->snapshot ();
  1024. auto *out = c->embed<VariationSelectorRecord> (*this);
  1025. if (unlikely (!out)) return hb_pair (0, 0);
  1026. out->defaultUVS = 0;
  1027. out->nonDefaultUVS = 0;
  1028. unsigned non_default_uvs_objidx = 0;
  1029. if (nonDefaultUVS != 0)
  1030. {
  1031. c->push ();
  1032. if (c->copy (base+nonDefaultUVS, unicodes, glyphs_requested, glyph_map))
  1033. non_default_uvs_objidx = c->pop_pack ();
  1034. else c->pop_discard ();
  1035. }
  1036. unsigned default_uvs_objidx = 0;
  1037. if (defaultUVS != 0)
  1038. {
  1039. c->push ();
  1040. if (c->copy (base+defaultUVS, unicodes))
  1041. default_uvs_objidx = c->pop_pack ();
  1042. else c->pop_discard ();
  1043. }
  1044. if (!default_uvs_objidx && !non_default_uvs_objidx)
  1045. c->revert (snap);
  1046. return hb_pair (default_uvs_objidx, non_default_uvs_objidx);
  1047. }
  1048. HBUINT24 varSelector; /* Variation selector. */
  1049. Offset32To<DefaultUVS>
  1050. defaultUVS; /* Offset to Default UVS Table. May be 0. */
  1051. Offset32To<NonDefaultUVS>
  1052. nonDefaultUVS; /* Offset to Non-Default UVS Table. May be 0. */
  1053. public:
  1054. DEFINE_SIZE_STATIC (11);
  1055. };
  1056. struct CmapSubtableFormat14
  1057. {
  1058. glyph_variant_t get_glyph_variant (hb_codepoint_t codepoint,
  1059. hb_codepoint_t variation_selector,
  1060. hb_codepoint_t *glyph) const
  1061. { return record.bsearch (variation_selector).get_glyph (codepoint, glyph, this); }
  1062. void collect_variation_selectors (hb_set_t *out) const
  1063. {
  1064. for (const auto& a : record.as_array ())
  1065. out->add (a.varSelector);
  1066. }
  1067. void collect_variation_unicodes (hb_codepoint_t variation_selector,
  1068. hb_set_t *out) const
  1069. { record.bsearch (variation_selector).collect_unicodes (out, this); }
  1070. void serialize (hb_serialize_context_t *c,
  1071. const hb_set_t *unicodes,
  1072. const hb_set_t *glyphs_requested,
  1073. const hb_map_t *glyph_map,
  1074. const void *base)
  1075. {
  1076. auto snap = c->snapshot ();
  1077. unsigned table_initpos = c->length ();
  1078. const char* init_tail = c->tail;
  1079. if (unlikely (!c->extend_min (this))) return;
  1080. this->format = 14;
  1081. auto src_tbl = reinterpret_cast<const CmapSubtableFormat14*> (base);
  1082. /*
  1083. * Some versions of OTS require that offsets are in order. Due to the use
  1084. * of push()/pop_pack() serializing the variation records in order results
  1085. * in the offsets being in reverse order (first record has the largest
  1086. * offset). While this is perfectly valid, it will cause some versions of
  1087. * OTS to consider this table bad.
  1088. *
  1089. * So to prevent this issue we serialize the variation records in reverse
  1090. * order, so that the offsets are ordered from small to large. Since
  1091. * variation records are supposed to be in increasing order of varSelector
  1092. * we then have to reverse the order of the written variation selector
  1093. * records after everything is finalized.
  1094. */
  1095. hb_vector_t<hb_pair_t<unsigned, unsigned>> obj_indices;
  1096. for (int i = src_tbl->record.len - 1; i >= 0; i--)
  1097. {
  1098. hb_pair_t<unsigned, unsigned> result = src_tbl->record[i].copy (c, unicodes, glyphs_requested, glyph_map, base);
  1099. if (result.first || result.second)
  1100. obj_indices.push (result);
  1101. }
  1102. if (c->length () - table_initpos == CmapSubtableFormat14::min_size)
  1103. {
  1104. c->revert (snap);
  1105. return;
  1106. }
  1107. if (unlikely (!c->check_success (!obj_indices.in_error ())))
  1108. return;
  1109. int tail_len = init_tail - c->tail;
  1110. c->check_assign (this->length, c->length () - table_initpos + tail_len,
  1111. HB_SERIALIZE_ERROR_INT_OVERFLOW);
  1112. c->check_assign (this->record.len,
  1113. (c->length () - table_initpos - CmapSubtableFormat14::min_size) /
  1114. VariationSelectorRecord::static_size,
  1115. HB_SERIALIZE_ERROR_INT_OVERFLOW);
  1116. /* Correct the incorrect write order by reversing the order of the variation
  1117. records array. */
  1118. _reverse_variation_records ();
  1119. /* Now that records are in the right order, we can set up the offsets. */
  1120. _add_links_to_variation_records (c, obj_indices);
  1121. }
  1122. void _reverse_variation_records ()
  1123. {
  1124. record.as_array ().reverse ();
  1125. }
  1126. void _add_links_to_variation_records (hb_serialize_context_t *c,
  1127. const hb_vector_t<hb_pair_t<unsigned, unsigned>>& obj_indices)
  1128. {
  1129. for (unsigned i = 0; i < obj_indices.length; i++)
  1130. {
  1131. /*
  1132. * Since the record array has been reversed (see comments in copy())
  1133. * but obj_indices has not been, the indices at obj_indices[i]
  1134. * are for the variation record at record[j].
  1135. */
  1136. int j = obj_indices.length - 1 - i;
  1137. c->add_link (record[j].defaultUVS, obj_indices[i].first);
  1138. c->add_link (record[j].nonDefaultUVS, obj_indices[i].second);
  1139. }
  1140. }
  1141. void closure_glyphs (const hb_set_t *unicodes,
  1142. hb_set_t *glyphset) const
  1143. {
  1144. + hb_iter (record)
  1145. | hb_filter (hb_bool, &VariationSelectorRecord::nonDefaultUVS)
  1146. | hb_map (&VariationSelectorRecord::nonDefaultUVS)
  1147. | hb_map (hb_add (this))
  1148. | hb_apply ([=] (const NonDefaultUVS& _) { _.closure_glyphs (unicodes, glyphset); })
  1149. ;
  1150. }
  1151. void collect_unicodes (hb_set_t *out) const
  1152. {
  1153. for (const VariationSelectorRecord& _ : record)
  1154. _.collect_unicodes (out, this);
  1155. }
  1156. void collect_mapping (hb_set_t *unicodes, /* OUT */
  1157. hb_map_t *mapping /* OUT */) const
  1158. {
  1159. for (const VariationSelectorRecord& _ : record)
  1160. _.collect_mapping (this, unicodes, mapping);
  1161. }
  1162. bool sanitize (hb_sanitize_context_t *c) const
  1163. {
  1164. TRACE_SANITIZE (this);
  1165. return_trace (c->check_struct (this) &&
  1166. record.sanitize (c, this));
  1167. }
  1168. protected:
  1169. HBUINT16 format; /* Format number is set to 14. */
  1170. HBUINT32 length; /* Byte length of this subtable. */
  1171. SortedArray32Of<VariationSelectorRecord>
  1172. record; /* Variation selector records; sorted
  1173. * in increasing order of `varSelector'. */
  1174. public:
  1175. DEFINE_SIZE_ARRAY (10, record);
  1176. };
  1177. struct CmapSubtable
  1178. {
  1179. /* Note: We intentionally do NOT implement subtable formats 2 and 8. */
  1180. bool get_glyph (hb_codepoint_t codepoint,
  1181. hb_codepoint_t *glyph) const
  1182. {
  1183. switch (u.format) {
  1184. case 0: return u.format0 .get_glyph (codepoint, glyph);
  1185. case 4: return u.format4 .get_glyph (codepoint, glyph);
  1186. case 6: return u.format6 .get_glyph (codepoint, glyph);
  1187. case 10: return u.format10.get_glyph (codepoint, glyph);
  1188. case 12: return u.format12.get_glyph (codepoint, glyph);
  1189. case 13: return u.format13.get_glyph (codepoint, glyph);
  1190. case 14:
  1191. default: return false;
  1192. }
  1193. }
  1194. void collect_unicodes (hb_set_t *out, unsigned int num_glyphs = UINT_MAX) const
  1195. {
  1196. switch (u.format) {
  1197. case 0: u.format0 .collect_unicodes (out); return;
  1198. case 4: u.format4 .collect_unicodes (out); return;
  1199. case 6: u.format6 .collect_unicodes (out); return;
  1200. case 10: u.format10.collect_unicodes (out); return;
  1201. case 12: u.format12.collect_unicodes (out, num_glyphs); return;
  1202. case 13: u.format13.collect_unicodes (out, num_glyphs); return;
  1203. case 14:
  1204. default: return;
  1205. }
  1206. }
  1207. void collect_mapping (hb_set_t *unicodes, /* OUT */
  1208. hb_map_t *mapping, /* OUT */
  1209. unsigned num_glyphs = UINT_MAX) const
  1210. {
  1211. switch (u.format) {
  1212. case 0: u.format0 .collect_mapping (unicodes, mapping); return;
  1213. case 4: u.format4 .collect_mapping (unicodes, mapping); return;
  1214. case 6: u.format6 .collect_mapping (unicodes, mapping); return;
  1215. case 10: u.format10.collect_mapping (unicodes, mapping); return;
  1216. case 12: u.format12.collect_mapping (unicodes, mapping, num_glyphs); return;
  1217. case 13: u.format13.collect_mapping (unicodes, mapping, num_glyphs); return;
  1218. case 14:
  1219. default: return;
  1220. }
  1221. }
  1222. unsigned get_language () const
  1223. {
  1224. switch (u.format) {
  1225. case 0: return u.format0 .get_language ();
  1226. case 4: return u.format4 .get_language ();
  1227. case 6: return u.format6 .get_language ();
  1228. case 10: return u.format10.get_language ();
  1229. case 12: return u.format12.get_language ();
  1230. case 13: return u.format13.get_language ();
  1231. case 14:
  1232. default: return 0;
  1233. }
  1234. }
  1235. template<typename Iterator,
  1236. hb_requires (hb_is_iterator (Iterator))>
  1237. void serialize (hb_serialize_context_t *c,
  1238. Iterator it,
  1239. unsigned format,
  1240. const hb_subset_plan_t *plan,
  1241. const void *base)
  1242. {
  1243. switch (format) {
  1244. case 4: return u.format4.serialize (c, it);
  1245. case 12: return u.format12.serialize (c, it);
  1246. case 14: return u.format14.serialize (c, plan->unicodes, plan->glyphs_requested, plan->glyph_map, base);
  1247. default: return;
  1248. }
  1249. }
  1250. bool sanitize (hb_sanitize_context_t *c) const
  1251. {
  1252. TRACE_SANITIZE (this);
  1253. if (!u.format.sanitize (c)) return_trace (false);
  1254. switch (u.format) {
  1255. case 0: return_trace (u.format0 .sanitize (c));
  1256. case 4: return_trace (u.format4 .sanitize (c));
  1257. case 6: return_trace (u.format6 .sanitize (c));
  1258. case 10: return_trace (u.format10.sanitize (c));
  1259. case 12: return_trace (u.format12.sanitize (c));
  1260. case 13: return_trace (u.format13.sanitize (c));
  1261. case 14: return_trace (u.format14.sanitize (c));
  1262. default:return_trace (true);
  1263. }
  1264. }
  1265. public:
  1266. union {
  1267. HBUINT16 format; /* Format identifier */
  1268. CmapSubtableFormat0 format0;
  1269. CmapSubtableFormat4 format4;
  1270. CmapSubtableFormat6 format6;
  1271. CmapSubtableFormat10 format10;
  1272. CmapSubtableFormat12 format12;
  1273. CmapSubtableFormat13 format13;
  1274. CmapSubtableFormat14 format14;
  1275. } u;
  1276. public:
  1277. DEFINE_SIZE_UNION (2, format);
  1278. };
  1279. struct EncodingRecord
  1280. {
  1281. int cmp (const EncodingRecord &other) const
  1282. {
  1283. int ret;
  1284. ret = platformID.cmp (other.platformID);
  1285. if (ret) return ret;
  1286. ret = encodingID.cmp (other.encodingID);
  1287. if (ret) return ret;
  1288. return 0;
  1289. }
  1290. bool sanitize (hb_sanitize_context_t *c, const void *base) const
  1291. {
  1292. TRACE_SANITIZE (this);
  1293. return_trace (c->check_struct (this) &&
  1294. subtable.sanitize (c, base));
  1295. }
  1296. template<typename Iterator,
  1297. hb_requires (hb_is_iterator (Iterator))>
  1298. EncodingRecord* copy (hb_serialize_context_t *c,
  1299. Iterator it,
  1300. unsigned format,
  1301. const void *base,
  1302. const hb_subset_plan_t *plan,
  1303. /* INOUT */ unsigned *objidx) const
  1304. {
  1305. TRACE_SERIALIZE (this);
  1306. auto snap = c->snapshot ();
  1307. auto *out = c->embed (this);
  1308. if (unlikely (!out)) return_trace (nullptr);
  1309. out->subtable = 0;
  1310. if (*objidx == 0)
  1311. {
  1312. CmapSubtable *cmapsubtable = c->push<CmapSubtable> ();
  1313. unsigned origin_length = c->length ();
  1314. cmapsubtable->serialize (c, it, format, plan, &(base+subtable));
  1315. if (c->length () - origin_length > 0) *objidx = c->pop_pack ();
  1316. else c->pop_discard ();
  1317. }
  1318. if (*objidx == 0)
  1319. {
  1320. c->revert (snap);
  1321. return_trace (nullptr);
  1322. }
  1323. c->add_link (out->subtable, *objidx);
  1324. return_trace (out);
  1325. }
  1326. HBUINT16 platformID; /* Platform ID. */
  1327. HBUINT16 encodingID; /* Platform-specific encoding ID. */
  1328. Offset32To<CmapSubtable>
  1329. subtable; /* Byte offset from beginning of table to the subtable for this encoding. */
  1330. public:
  1331. DEFINE_SIZE_STATIC (8);
  1332. };
  1333. struct cmap;
  1334. struct SubtableUnicodesCache {
  1335. private:
  1336. hb_blob_ptr_t<cmap> base_blob;
  1337. const char* base;
  1338. hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> cached_unicodes;
  1339. public:
  1340. static SubtableUnicodesCache* create (hb_blob_ptr_t<cmap> source_table)
  1341. {
  1342. SubtableUnicodesCache* cache =
  1343. (SubtableUnicodesCache*) hb_malloc (sizeof(SubtableUnicodesCache));
  1344. new (cache) SubtableUnicodesCache (source_table);
  1345. return cache;
  1346. }
  1347. static void destroy (void* value) {
  1348. if (!value) return;
  1349. SubtableUnicodesCache* cache = (SubtableUnicodesCache*) value;
  1350. cache->~SubtableUnicodesCache ();
  1351. hb_free (cache);
  1352. }
  1353. SubtableUnicodesCache(const void* cmap_base)
  1354. : base_blob(),
  1355. base ((const char*) cmap_base),
  1356. cached_unicodes ()
  1357. {}
  1358. SubtableUnicodesCache(hb_blob_ptr_t<cmap> base_blob_)
  1359. : base_blob(base_blob_),
  1360. base ((const char *) base_blob.get()),
  1361. cached_unicodes ()
  1362. {}
  1363. ~SubtableUnicodesCache()
  1364. {
  1365. base_blob.destroy ();
  1366. }
  1367. bool same_base(const void* other) const
  1368. {
  1369. return other == (const void*) base;
  1370. }
  1371. const hb_set_t* set_for (const EncodingRecord* record,
  1372. SubtableUnicodesCache& mutable_cache) const
  1373. {
  1374. if (cached_unicodes.has ((unsigned) ((const char *) record - base)))
  1375. return cached_unicodes.get ((unsigned) ((const char *) record - base));
  1376. return mutable_cache.set_for (record);
  1377. }
  1378. const hb_set_t* set_for (const EncodingRecord* record)
  1379. {
  1380. if (!cached_unicodes.has ((unsigned) ((const char *) record - base)))
  1381. {
  1382. hb_set_t *s = hb_set_create ();
  1383. if (unlikely (s->in_error ()))
  1384. return hb_set_get_empty ();
  1385. (base+record->subtable).collect_unicodes (s);
  1386. if (unlikely (!cached_unicodes.set ((unsigned) ((const char *) record - base), hb::unique_ptr<hb_set_t> {s})))
  1387. return hb_set_get_empty ();
  1388. return s;
  1389. }
  1390. return cached_unicodes.get ((unsigned) ((const char *) record - base));
  1391. }
  1392. };
  1393. static inline uint_fast16_t
  1394. _hb_symbol_pua_map (unsigned codepoint)
  1395. {
  1396. if (codepoint <= 0x00FFu)
  1397. {
  1398. /* For symbol-encoded OpenType fonts, we duplicate the
  1399. * U+F000..F0FF range at U+0000..U+00FF. That's what
  1400. * Windows seems to do, and that's hinted about at:
  1401. * https://docs.microsoft.com/en-us/typography/opentype/spec/recom
  1402. * under "Non-Standard (Symbol) Fonts". */
  1403. return 0xF000u + codepoint;
  1404. }
  1405. return 0;
  1406. }
  1407. struct cmap
  1408. {
  1409. static constexpr hb_tag_t tableTag = HB_OT_TAG_cmap;
  1410. static SubtableUnicodesCache* create_filled_cache(hb_blob_ptr_t<cmap> source_table) {
  1411. const cmap* cmap = source_table.get();
  1412. auto it =
  1413. + hb_iter (cmap->encodingRecord)
  1414. | hb_filter ([&](const EncodingRecord& _) {
  1415. return cmap::filter_encoding_records_for_subset (cmap, _);
  1416. })
  1417. ;
  1418. SubtableUnicodesCache* cache = SubtableUnicodesCache::create(source_table);
  1419. for (const EncodingRecord& _ : it)
  1420. cache->set_for(&_); // populate the cache for this encoding record.
  1421. return cache;
  1422. }
  1423. template<typename Iterator, typename EncodingRecIter,
  1424. hb_requires (hb_is_iterator (EncodingRecIter))>
  1425. bool serialize (hb_serialize_context_t *c,
  1426. Iterator it,
  1427. EncodingRecIter encodingrec_iter,
  1428. const void *base,
  1429. hb_subset_plan_t *plan,
  1430. bool drop_format_4 = false)
  1431. {
  1432. if (unlikely (!c->extend_min ((*this)))) return false;
  1433. this->version = 0;
  1434. unsigned format4objidx = 0, format12objidx = 0, format14objidx = 0;
  1435. auto snap = c->snapshot ();
  1436. SubtableUnicodesCache local_unicodes_cache (base);
  1437. const SubtableUnicodesCache* unicodes_cache = &local_unicodes_cache;
  1438. if (plan->accelerator &&
  1439. plan->accelerator->cmap_cache &&
  1440. plan->accelerator->cmap_cache->same_base (base))
  1441. unicodes_cache = plan->accelerator->cmap_cache;
  1442. for (const EncodingRecord& _ : encodingrec_iter)
  1443. {
  1444. if (c->in_error ())
  1445. return false;
  1446. unsigned format = (base+_.subtable).u.format;
  1447. if (format != 4 && format != 12 && format != 14) continue;
  1448. const hb_set_t* unicodes_set = unicodes_cache->set_for (&_, local_unicodes_cache);
  1449. if (!drop_format_4 && format == 4)
  1450. {
  1451. c->copy (_, + it | hb_filter (*unicodes_set, hb_first), 4u, base, plan, &format4objidx);
  1452. if (c->in_error () && c->only_overflow ())
  1453. {
  1454. // cmap4 overflowed, reset and retry serialization without format 4 subtables.
  1455. c->revert (snap);
  1456. return serialize (c, it,
  1457. encodingrec_iter,
  1458. base,
  1459. plan,
  1460. true);
  1461. }
  1462. }
  1463. else if (format == 12)
  1464. {
  1465. if (_can_drop (_,
  1466. *unicodes_set,
  1467. base,
  1468. *unicodes_cache,
  1469. local_unicodes_cache,
  1470. + it | hb_map (hb_first), encodingrec_iter))
  1471. continue;
  1472. c->copy (_, + it | hb_filter (*unicodes_set, hb_first), 12u, base, plan, &format12objidx);
  1473. }
  1474. else if (format == 14) c->copy (_, it, 14u, base, plan, &format14objidx);
  1475. }
  1476. c->check_assign(this->encodingRecord.len,
  1477. (c->length () - cmap::min_size)/EncodingRecord::static_size,
  1478. HB_SERIALIZE_ERROR_INT_OVERFLOW);
  1479. // Fail if format 4 was dropped and there is no cmap12.
  1480. return !drop_format_4 || format12objidx;
  1481. }
  1482. template<typename Iterator, typename EncodingRecordIterator,
  1483. hb_requires (hb_is_iterator (Iterator)),
  1484. hb_requires (hb_is_iterator (EncodingRecordIterator))>
  1485. bool _can_drop (const EncodingRecord& cmap12,
  1486. const hb_set_t& cmap12_unicodes,
  1487. const void* base,
  1488. const SubtableUnicodesCache& unicodes_cache,
  1489. SubtableUnicodesCache& local_unicodes_cache,
  1490. Iterator subset_unicodes,
  1491. EncodingRecordIterator encoding_records)
  1492. {
  1493. for (auto cp : + subset_unicodes | hb_filter (cmap12_unicodes))
  1494. {
  1495. if (cp >= 0x10000) return false;
  1496. }
  1497. unsigned target_platform;
  1498. unsigned target_encoding;
  1499. unsigned target_language = (base+cmap12.subtable).get_language ();
  1500. if (cmap12.platformID == 0 && cmap12.encodingID == 4)
  1501. {
  1502. target_platform = 0;
  1503. target_encoding = 3;
  1504. } else if (cmap12.platformID == 3 && cmap12.encodingID == 10) {
  1505. target_platform = 3;
  1506. target_encoding = 1;
  1507. } else {
  1508. return false;
  1509. }
  1510. for (const auto& _ : encoding_records)
  1511. {
  1512. if (_.platformID != target_platform
  1513. || _.encodingID != target_encoding
  1514. || (base+_.subtable).get_language() != target_language)
  1515. continue;
  1516. const hb_set_t* sibling_unicodes = unicodes_cache.set_for (&_, local_unicodes_cache);
  1517. auto cmap12 = + subset_unicodes | hb_filter (cmap12_unicodes);
  1518. auto sibling = + subset_unicodes | hb_filter (*sibling_unicodes);
  1519. for (; cmap12 && sibling; cmap12++, sibling++)
  1520. {
  1521. unsigned a = *cmap12;
  1522. unsigned b = *sibling;
  1523. if (a != b) return false;
  1524. }
  1525. return !cmap12 && !sibling;
  1526. }
  1527. return false;
  1528. }
  1529. void closure_glyphs (const hb_set_t *unicodes,
  1530. hb_set_t *glyphset) const
  1531. {
  1532. + hb_iter (encodingRecord)
  1533. | hb_map (&EncodingRecord::subtable)
  1534. | hb_map (hb_add (this))
  1535. | hb_filter ([&] (const CmapSubtable& _) { return _.u.format == 14; })
  1536. | hb_apply ([=] (const CmapSubtable& _) { _.u.format14.closure_glyphs (unicodes, glyphset); })
  1537. ;
  1538. }
  1539. bool subset (hb_subset_context_t *c) const
  1540. {
  1541. TRACE_SUBSET (this);
  1542. cmap *cmap_prime = c->serializer->start_embed<cmap> ();
  1543. if (unlikely (!c->serializer->check_success (cmap_prime))) return_trace (false);
  1544. auto encodingrec_iter =
  1545. + hb_iter (encodingRecord)
  1546. | hb_filter ([&](const EncodingRecord& _) {
  1547. return cmap::filter_encoding_records_for_subset (this, _);
  1548. })
  1549. ;
  1550. if (unlikely (!encodingrec_iter.len ())) return_trace (false);
  1551. const EncodingRecord *unicode_bmp= nullptr, *unicode_ucs4 = nullptr, *ms_bmp = nullptr, *ms_ucs4 = nullptr;
  1552. bool has_format12 = false;
  1553. for (const EncodingRecord& _ : encodingrec_iter)
  1554. {
  1555. unsigned format = (this + _.subtable).u.format;
  1556. if (format == 12) has_format12 = true;
  1557. const EncodingRecord *table = std::addressof (_);
  1558. if (_.platformID == 0 && _.encodingID == 3) unicode_bmp = table;
  1559. else if (_.platformID == 0 && _.encodingID == 4) unicode_ucs4 = table;
  1560. else if (_.platformID == 3 && _.encodingID == 1) ms_bmp = table;
  1561. else if (_.platformID == 3 && _.encodingID == 10) ms_ucs4 = table;
  1562. }
  1563. if (unlikely (!has_format12 && !unicode_bmp && !ms_bmp)) return_trace (false);
  1564. if (unlikely (has_format12 && (!unicode_ucs4 && !ms_ucs4))) return_trace (false);
  1565. auto it =
  1566. + c->plan->unicode_to_new_gid_list.iter ()
  1567. | hb_filter ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> _)
  1568. { return (_.second != HB_MAP_VALUE_INVALID); })
  1569. ;
  1570. return_trace (cmap_prime->serialize (c->serializer,
  1571. it,
  1572. encodingrec_iter,
  1573. this,
  1574. c->plan));
  1575. }
  1576. const CmapSubtable *find_best_subtable (bool *symbol = nullptr) const
  1577. {
  1578. if (symbol) *symbol = false;
  1579. const CmapSubtable *subtable;
  1580. /* Symbol subtable.
  1581. * Prefer symbol if available.
  1582. * https://github.com/harfbuzz/harfbuzz/issues/1918 */
  1583. if ((subtable = this->find_subtable (3, 0)))
  1584. {
  1585. if (symbol) *symbol = true;
  1586. return subtable;
  1587. }
  1588. /* 32-bit subtables. */
  1589. if ((subtable = this->find_subtable (3, 10))) return subtable;
  1590. if ((subtable = this->find_subtable (0, 6))) return subtable;
  1591. if ((subtable = this->find_subtable (0, 4))) return subtable;
  1592. /* 16-bit subtables. */
  1593. if ((subtable = this->find_subtable (3, 1))) return subtable;
  1594. if ((subtable = this->find_subtable (0, 3))) return subtable;
  1595. if ((subtable = this->find_subtable (0, 2))) return subtable;
  1596. if ((subtable = this->find_subtable (0, 1))) return subtable;
  1597. if ((subtable = this->find_subtable (0, 0))) return subtable;
  1598. /* Meh. */
  1599. return &Null (CmapSubtable);
  1600. }
  1601. struct accelerator_t
  1602. {
  1603. accelerator_t (hb_face_t *face)
  1604. {
  1605. this->table = hb_sanitize_context_t ().reference_table<cmap> (face);
  1606. bool symbol;
  1607. this->subtable = table->find_best_subtable (&symbol);
  1608. this->subtable_uvs = &Null (CmapSubtableFormat14);
  1609. {
  1610. const CmapSubtable *st = table->find_subtable (0, 5);
  1611. if (st && st->u.format == 14)
  1612. subtable_uvs = &st->u.format14;
  1613. }
  1614. this->get_glyph_data = subtable;
  1615. if (unlikely (symbol))
  1616. {
  1617. switch ((unsigned) face->table.OS2->get_font_page ()) {
  1618. case OS2::font_page_t::FONT_PAGE_NONE:
  1619. this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable, _hb_symbol_pua_map>;
  1620. break;
  1621. #ifndef HB_NO_OT_SHAPER_ARABIC_FALLBACK
  1622. case OS2::font_page_t::FONT_PAGE_SIMP_ARABIC:
  1623. this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable, _hb_arabic_pua_simp_map>;
  1624. break;
  1625. case OS2::font_page_t::FONT_PAGE_TRAD_ARABIC:
  1626. this->get_glyph_funcZ = get_glyph_from_symbol<CmapSubtable, _hb_arabic_pua_trad_map>;
  1627. break;
  1628. #endif
  1629. default:
  1630. this->get_glyph_funcZ = get_glyph_from<CmapSubtable>;
  1631. break;
  1632. }
  1633. }
  1634. else
  1635. {
  1636. switch (subtable->u.format) {
  1637. /* Accelerate format 4 and format 12. */
  1638. default:
  1639. this->get_glyph_funcZ = get_glyph_from<CmapSubtable>;
  1640. break;
  1641. case 12:
  1642. this->get_glyph_funcZ = get_glyph_from<CmapSubtableFormat12>;
  1643. break;
  1644. case 4:
  1645. {
  1646. this->format4_accel.init (&subtable->u.format4);
  1647. this->get_glyph_data = &this->format4_accel;
  1648. this->get_glyph_funcZ = this->format4_accel.get_glyph_func;
  1649. break;
  1650. }
  1651. }
  1652. }
  1653. }
  1654. ~accelerator_t () { this->table.destroy (); }
  1655. bool get_nominal_glyph (hb_codepoint_t unicode,
  1656. hb_codepoint_t *glyph) const
  1657. {
  1658. if (unlikely (!this->get_glyph_funcZ)) return false;
  1659. return this->get_glyph_funcZ (this->get_glyph_data, unicode, glyph);
  1660. }
  1661. unsigned int get_nominal_glyphs (unsigned int count,
  1662. const hb_codepoint_t *first_unicode,
  1663. unsigned int unicode_stride,
  1664. hb_codepoint_t *first_glyph,
  1665. unsigned int glyph_stride) const
  1666. {
  1667. if (unlikely (!this->get_glyph_funcZ)) return 0;
  1668. hb_cmap_get_glyph_func_t get_glyph_funcZ = this->get_glyph_funcZ;
  1669. const void *get_glyph_data = this->get_glyph_data;
  1670. unsigned int done;
  1671. for (done = 0;
  1672. done < count && get_glyph_funcZ (get_glyph_data, *first_unicode, first_glyph);
  1673. done++)
  1674. {
  1675. first_unicode = &StructAtOffsetUnaligned<hb_codepoint_t> (first_unicode, unicode_stride);
  1676. first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
  1677. }
  1678. return done;
  1679. }
  1680. bool get_variation_glyph (hb_codepoint_t unicode,
  1681. hb_codepoint_t variation_selector,
  1682. hb_codepoint_t *glyph) const
  1683. {
  1684. switch (this->subtable_uvs->get_glyph_variant (unicode,
  1685. variation_selector,
  1686. glyph))
  1687. {
  1688. case GLYPH_VARIANT_NOT_FOUND: return false;
  1689. case GLYPH_VARIANT_FOUND: return true;
  1690. case GLYPH_VARIANT_USE_DEFAULT: break;
  1691. }
  1692. return get_nominal_glyph (unicode, glyph);
  1693. }
  1694. void collect_unicodes (hb_set_t *out, unsigned int num_glyphs) const
  1695. { subtable->collect_unicodes (out, num_glyphs); }
  1696. void collect_mapping (hb_set_t *unicodes, hb_map_t *mapping,
  1697. unsigned num_glyphs = UINT_MAX) const
  1698. { subtable->collect_mapping (unicodes, mapping, num_glyphs); }
  1699. void collect_variation_selectors (hb_set_t *out) const
  1700. { subtable_uvs->collect_variation_selectors (out); }
  1701. void collect_variation_unicodes (hb_codepoint_t variation_selector,
  1702. hb_set_t *out) const
  1703. { subtable_uvs->collect_variation_unicodes (variation_selector, out); }
  1704. protected:
  1705. typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj,
  1706. hb_codepoint_t codepoint,
  1707. hb_codepoint_t *glyph);
  1708. typedef uint_fast16_t (*hb_pua_remap_func_t) (unsigned);
  1709. template <typename Type>
  1710. HB_INTERNAL static bool get_glyph_from (const void *obj,
  1711. hb_codepoint_t codepoint,
  1712. hb_codepoint_t *glyph)
  1713. {
  1714. const Type *typed_obj = (const Type *) obj;
  1715. return typed_obj->get_glyph (codepoint, glyph);
  1716. }
  1717. template <typename Type, hb_pua_remap_func_t remap>
  1718. HB_INTERNAL static bool get_glyph_from_symbol (const void *obj,
  1719. hb_codepoint_t codepoint,
  1720. hb_codepoint_t *glyph)
  1721. {
  1722. const Type *typed_obj = (const Type *) obj;
  1723. if (likely (typed_obj->get_glyph (codepoint, glyph)))
  1724. return true;
  1725. if (hb_codepoint_t c = remap (codepoint))
  1726. return typed_obj->get_glyph (c, glyph);
  1727. return false;
  1728. }
  1729. private:
  1730. hb_nonnull_ptr_t<const CmapSubtable> subtable;
  1731. hb_nonnull_ptr_t<const CmapSubtableFormat14> subtable_uvs;
  1732. hb_cmap_get_glyph_func_t get_glyph_funcZ;
  1733. const void *get_glyph_data;
  1734. CmapSubtableFormat4::accelerator_t format4_accel;
  1735. public:
  1736. hb_blob_ptr_t<cmap> table;
  1737. };
  1738. protected:
  1739. const CmapSubtable *find_subtable (unsigned int platform_id,
  1740. unsigned int encoding_id) const
  1741. {
  1742. EncodingRecord key;
  1743. key.platformID = platform_id;
  1744. key.encodingID = encoding_id;
  1745. const EncodingRecord &result = encodingRecord.bsearch (key);
  1746. if (!result.subtable)
  1747. return nullptr;
  1748. return &(this+result.subtable);
  1749. }
  1750. const EncodingRecord *find_encodingrec (unsigned int platform_id,
  1751. unsigned int encoding_id) const
  1752. {
  1753. EncodingRecord key;
  1754. key.platformID = platform_id;
  1755. key.encodingID = encoding_id;
  1756. return encodingRecord.as_array ().bsearch (key);
  1757. }
  1758. bool find_subtable (unsigned format) const
  1759. {
  1760. auto it =
  1761. + hb_iter (encodingRecord)
  1762. | hb_map (&EncodingRecord::subtable)
  1763. | hb_map (hb_add (this))
  1764. | hb_filter ([&] (const CmapSubtable& _) { return _.u.format == format; })
  1765. ;
  1766. return it.len ();
  1767. }
  1768. public:
  1769. bool sanitize (hb_sanitize_context_t *c) const
  1770. {
  1771. TRACE_SANITIZE (this);
  1772. return_trace (c->check_struct (this) &&
  1773. likely (version == 0) &&
  1774. encodingRecord.sanitize (c, this));
  1775. }
  1776. private:
  1777. static bool filter_encoding_records_for_subset(const cmap* cmap,
  1778. const EncodingRecord& _)
  1779. {
  1780. return
  1781. (_.platformID == 0 && _.encodingID == 3) ||
  1782. (_.platformID == 0 && _.encodingID == 4) ||
  1783. (_.platformID == 3 && _.encodingID == 1) ||
  1784. (_.platformID == 3 && _.encodingID == 10) ||
  1785. (cmap + _.subtable).u.format == 14;
  1786. }
  1787. protected:
  1788. HBUINT16 version; /* Table version number (0). */
  1789. SortedArray16Of<EncodingRecord>
  1790. encodingRecord; /* Encoding tables. */
  1791. public:
  1792. DEFINE_SIZE_ARRAY (4, encodingRecord);
  1793. };
  1794. struct cmap_accelerator_t : cmap::accelerator_t {
  1795. cmap_accelerator_t (hb_face_t *face) : cmap::accelerator_t (face) {}
  1796. };
  1797. } /* namespace OT */
  1798. #endif /* HB_OT_CMAP_TABLE_HH */