Font.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Checked.h>
  8. #include <AK/MappedFile.h>
  9. #include <AK/Utf32View.h>
  10. #include <AK/Utf8View.h>
  11. #include <LibCore/File.h>
  12. #include <LibTTF/Cmap.h>
  13. #include <LibTTF/Font.h>
  14. #include <LibTTF/Glyf.h>
  15. #include <LibTTF/Tables.h>
  16. #include <LibTextCodec/Decoder.h>
  17. #include <math.h>
  18. #include <sys/mman.h>
  19. namespace TTF {
  20. u16 be_u16(const u8* ptr);
  21. u32 be_u32(const u8* ptr);
  22. i16 be_i16(const u8* ptr);
  23. float be_fword(const u8* ptr);
  24. u32 tag_from_str(const char* str);
  25. u16 be_u16(const u8* ptr)
  26. {
  27. return (((u16)ptr[0]) << 8) | ((u16)ptr[1]);
  28. }
  29. u32 be_u32(const u8* ptr)
  30. {
  31. return (((u32)ptr[0]) << 24) | (((u32)ptr[1]) << 16) | (((u32)ptr[2]) << 8) | ((u32)ptr[3]);
  32. }
  33. i16 be_i16(const u8* ptr)
  34. {
  35. return (((i16)ptr[0]) << 8) | ((i16)ptr[1]);
  36. }
  37. float be_fword(const u8* ptr)
  38. {
  39. return (float)be_i16(ptr) / (float)(1 << 14);
  40. }
  41. u32 tag_from_str(const char* str)
  42. {
  43. return be_u32((const u8*)str);
  44. }
  45. Optional<Head> Head::from_slice(const ReadonlyBytes& slice)
  46. {
  47. if (slice.size() < (size_t)Sizes::Table) {
  48. return {};
  49. }
  50. return Head(slice);
  51. }
  52. u16 Head::units_per_em() const
  53. {
  54. return be_u16(m_slice.offset_pointer((u32)Offsets::UnitsPerEM));
  55. }
  56. i16 Head::xmin() const
  57. {
  58. return be_i16(m_slice.offset_pointer((u32)Offsets::XMin));
  59. }
  60. i16 Head::ymin() const
  61. {
  62. return be_i16(m_slice.offset_pointer((u32)Offsets::YMin));
  63. }
  64. i16 Head::xmax() const
  65. {
  66. return be_i16(m_slice.offset_pointer((u32)Offsets::XMax));
  67. }
  68. i16 Head::ymax() const
  69. {
  70. return be_i16(m_slice.offset_pointer((u32)Offsets::YMax));
  71. }
  72. u16 Head::lowest_recommended_ppem() const
  73. {
  74. return be_u16(m_slice.offset_pointer((u32)Offsets::LowestRecPPEM));
  75. }
  76. IndexToLocFormat Head::index_to_loc_format() const
  77. {
  78. i16 raw = be_i16(m_slice.offset_pointer((u32)Offsets::IndexToLocFormat));
  79. switch (raw) {
  80. case 0:
  81. return IndexToLocFormat::Offset16;
  82. case 1:
  83. return IndexToLocFormat::Offset32;
  84. default:
  85. VERIFY_NOT_REACHED();
  86. }
  87. }
  88. Optional<Hhea> Hhea::from_slice(const ReadonlyBytes& slice)
  89. {
  90. if (slice.size() < (size_t)Sizes::Table) {
  91. return {};
  92. }
  93. return Hhea(slice);
  94. }
  95. i16 Hhea::ascender() const
  96. {
  97. return be_i16(m_slice.offset_pointer((u32)Offsets::Ascender));
  98. }
  99. i16 Hhea::descender() const
  100. {
  101. return be_i16(m_slice.offset_pointer((u32)Offsets::Descender));
  102. }
  103. i16 Hhea::line_gap() const
  104. {
  105. return be_i16(m_slice.offset_pointer((u32)Offsets::LineGap));
  106. }
  107. u16 Hhea::advance_width_max() const
  108. {
  109. return be_u16(m_slice.offset_pointer((u32)Offsets::AdvanceWidthMax));
  110. }
  111. u16 Hhea::number_of_h_metrics() const
  112. {
  113. return be_u16(m_slice.offset_pointer((u32)Offsets::NumberOfHMetrics));
  114. }
  115. Optional<Maxp> Maxp::from_slice(const ReadonlyBytes& slice)
  116. {
  117. if (slice.size() < (size_t)Sizes::TableV0p5) {
  118. return {};
  119. }
  120. return Maxp(slice);
  121. }
  122. u16 Maxp::num_glyphs() const
  123. {
  124. return be_u16(m_slice.offset_pointer((u32)Offsets::NumGlyphs));
  125. }
  126. Optional<Hmtx> Hmtx::from_slice(const ReadonlyBytes& slice, u32 num_glyphs, u32 number_of_h_metrics)
  127. {
  128. if (slice.size() < number_of_h_metrics * (u32)Sizes::LongHorMetric + (num_glyphs - number_of_h_metrics) * (u32)Sizes::LeftSideBearing) {
  129. return {};
  130. }
  131. return Hmtx(slice, num_glyphs, number_of_h_metrics);
  132. }
  133. Optional<Name> Name::from_slice(const ReadonlyBytes& slice)
  134. {
  135. return Name(slice);
  136. }
  137. String Name::string_for_id(NameId id) const
  138. {
  139. auto num_entries = be_u16(m_slice.offset_pointer(2));
  140. auto string_offset = be_u16(m_slice.offset_pointer(4));
  141. Vector<int> valid_ids;
  142. for (int i = 0; i < num_entries; ++i) {
  143. auto this_id = be_u16(m_slice.offset_pointer(6 + i * 12 + 6));
  144. if (this_id == (u16)id)
  145. valid_ids.append(i);
  146. }
  147. if (valid_ids.is_empty())
  148. return String::empty();
  149. auto it = valid_ids.find_if([this](auto const& i) {
  150. // check if font has naming table for en-US language id
  151. auto platform = be_u16(m_slice.offset_pointer(6 + i * 12 + 0));
  152. auto language_id = be_u16(m_slice.offset_pointer(6 + i * 12 + 4));
  153. return (platform == (u16)Platform::Macintosh && language_id == (u16)MacintoshLanguage::English)
  154. || (platform == (u16)Platform::Windows && language_id == (u16)WindowsLanguage::EnglishUnitedStates);
  155. });
  156. auto i = it != valid_ids.end() ? *it : valid_ids.first();
  157. auto platform = be_u16(m_slice.offset_pointer(6 + i * 12 + 0));
  158. auto length = be_u16(m_slice.offset_pointer(6 + i * 12 + 8));
  159. auto offset = be_u16(m_slice.offset_pointer(6 + i * 12 + 10));
  160. if (platform == (u16)Platform::Windows) {
  161. static auto& decoder = *TextCodec::decoder_for("utf-16be");
  162. return decoder.to_utf8(StringView { (const char*)m_slice.offset_pointer(string_offset + offset), length });
  163. }
  164. return String((const char*)m_slice.offset_pointer(string_offset + offset), length);
  165. }
  166. GlyphHorizontalMetrics Hmtx::get_glyph_horizontal_metrics(u32 glyph_id) const
  167. {
  168. VERIFY(glyph_id < m_num_glyphs);
  169. if (glyph_id < m_number_of_h_metrics) {
  170. auto offset = glyph_id * (u32)Sizes::LongHorMetric;
  171. u16 advance_width = be_u16(m_slice.offset_pointer(offset));
  172. i16 left_side_bearing = be_i16(m_slice.offset_pointer(offset + 2));
  173. return GlyphHorizontalMetrics {
  174. .advance_width = advance_width,
  175. .left_side_bearing = left_side_bearing,
  176. };
  177. }
  178. auto offset = m_number_of_h_metrics * (u32)Sizes::LongHorMetric + (glyph_id - m_number_of_h_metrics) * (u32)Sizes::LeftSideBearing;
  179. u16 advance_width = be_u16(m_slice.offset_pointer((m_number_of_h_metrics - 1) * (u32)Sizes::LongHorMetric));
  180. i16 left_side_bearing = be_i16(m_slice.offset_pointer(offset));
  181. return GlyphHorizontalMetrics {
  182. .advance_width = advance_width,
  183. .left_side_bearing = left_side_bearing,
  184. };
  185. }
  186. Result<NonnullRefPtr<Font>, String> Font::try_load_from_file(String path, unsigned index)
  187. {
  188. auto file_or_error = MappedFile::map(path);
  189. if (file_or_error.is_error())
  190. return String { file_or_error.error().string() };
  191. auto& file = *file_or_error.value();
  192. auto result = try_load_from_externally_owned_memory(file.bytes(), index);
  193. if (result.is_error())
  194. return result.error();
  195. auto& font = *result.value();
  196. font.m_mapped_file = file_or_error.release_value();
  197. return result;
  198. }
  199. Result<NonnullRefPtr<Font>, String> Font::try_load_from_externally_owned_memory(ReadonlyBytes buffer, unsigned index)
  200. {
  201. if (buffer.size() < 4)
  202. return String { "Font file too small" };
  203. u32 tag = be_u32(buffer.data());
  204. if (tag == tag_from_str("ttcf")) {
  205. // It's a font collection
  206. if (buffer.size() < (u32)Sizes::TTCHeaderV1 + sizeof(u32) * (index + 1))
  207. return String { "Font file too small" };
  208. u32 offset = be_u32(buffer.offset_pointer((u32)Sizes::TTCHeaderV1 + sizeof(u32) * index));
  209. return try_load_from_offset(move(buffer), offset);
  210. }
  211. if (tag == tag_from_str("OTTO"))
  212. return String { "CFF fonts not supported yet" };
  213. if (tag != 0x00010000)
  214. return String { "Not a valid font" };
  215. return try_load_from_offset(move(buffer), 0);
  216. }
  217. // FIXME: "loca" and "glyf" are not available for CFF fonts.
  218. Result<NonnullRefPtr<Font>, String> Font::try_load_from_offset(ReadonlyBytes buffer, u32 offset)
  219. {
  220. if (Checked<u32>::addition_would_overflow(offset, (u32)Sizes::OffsetTable))
  221. return String { "Invalid offset in font header" };
  222. if (buffer.size() < offset + (u32)Sizes::OffsetTable)
  223. return String { "Font file too small" };
  224. Optional<ReadonlyBytes> opt_head_slice = {};
  225. Optional<ReadonlyBytes> opt_name_slice = {};
  226. Optional<ReadonlyBytes> opt_hhea_slice = {};
  227. Optional<ReadonlyBytes> opt_maxp_slice = {};
  228. Optional<ReadonlyBytes> opt_hmtx_slice = {};
  229. Optional<ReadonlyBytes> opt_cmap_slice = {};
  230. Optional<ReadonlyBytes> opt_loca_slice = {};
  231. Optional<ReadonlyBytes> opt_glyf_slice = {};
  232. Optional<Head> opt_head = {};
  233. Optional<Name> opt_name = {};
  234. Optional<Hhea> opt_hhea = {};
  235. Optional<Maxp> opt_maxp = {};
  236. Optional<Hmtx> opt_hmtx = {};
  237. Optional<Cmap> opt_cmap = {};
  238. Optional<Loca> opt_loca = {};
  239. auto num_tables = be_u16(buffer.offset_pointer(offset + (u32)Offsets::NumTables));
  240. if (buffer.size() < offset + (u32)Sizes::OffsetTable + num_tables * (u32)Sizes::TableRecord)
  241. return String { "Font file too small" };
  242. for (auto i = 0; i < num_tables; i++) {
  243. u32 record_offset = offset + (u32)Sizes::OffsetTable + i * (u32)Sizes::TableRecord;
  244. u32 tag = be_u32(buffer.offset_pointer(record_offset));
  245. u32 table_offset = be_u32(buffer.offset_pointer(record_offset + (u32)Offsets::TableRecord_Offset));
  246. u32 table_length = be_u32(buffer.offset_pointer(record_offset + (u32)Offsets::TableRecord_Length));
  247. if (Checked<u32>::addition_would_overflow(table_offset, table_length))
  248. return String { "Invalid table offset/length in font." };
  249. if (buffer.size() < table_offset + table_length)
  250. return String { "Font file too small" };
  251. auto buffer_here = ReadonlyBytes(buffer.offset_pointer(table_offset), table_length);
  252. // Get the table offsets we need.
  253. if (tag == tag_from_str("head")) {
  254. opt_head_slice = buffer_here;
  255. } else if (tag == tag_from_str("name")) {
  256. opt_name_slice = buffer_here;
  257. } else if (tag == tag_from_str("hhea")) {
  258. opt_hhea_slice = buffer_here;
  259. } else if (tag == tag_from_str("maxp")) {
  260. opt_maxp_slice = buffer_here;
  261. } else if (tag == tag_from_str("hmtx")) {
  262. opt_hmtx_slice = buffer_here;
  263. } else if (tag == tag_from_str("cmap")) {
  264. opt_cmap_slice = buffer_here;
  265. } else if (tag == tag_from_str("loca")) {
  266. opt_loca_slice = buffer_here;
  267. } else if (tag == tag_from_str("glyf")) {
  268. opt_glyf_slice = buffer_here;
  269. }
  270. }
  271. if (!opt_head_slice.has_value() || !(opt_head = Head::from_slice(opt_head_slice.value())).has_value())
  272. return String { "Could not load Head" };
  273. auto head = opt_head.value();
  274. if (!opt_name_slice.has_value() || !(opt_name = Name::from_slice(opt_name_slice.value())).has_value())
  275. return String { "Could not load Name" };
  276. auto name = opt_name.value();
  277. if (!opt_hhea_slice.has_value() || !(opt_hhea = Hhea::from_slice(opt_hhea_slice.value())).has_value())
  278. return String { "Could not load Hhea" };
  279. auto hhea = opt_hhea.value();
  280. if (!opt_maxp_slice.has_value() || !(opt_maxp = Maxp::from_slice(opt_maxp_slice.value())).has_value())
  281. return String { "Could not load Maxp" };
  282. auto maxp = opt_maxp.value();
  283. if (!opt_hmtx_slice.has_value() || !(opt_hmtx = Hmtx::from_slice(opt_hmtx_slice.value(), maxp.num_glyphs(), hhea.number_of_h_metrics())).has_value())
  284. return String { "Could not load Hmtx" };
  285. auto hmtx = opt_hmtx.value();
  286. if (!opt_cmap_slice.has_value() || !(opt_cmap = Cmap::from_slice(opt_cmap_slice.value())).has_value())
  287. return String { "Could not load Cmap" };
  288. auto cmap = opt_cmap.value();
  289. if (!opt_loca_slice.has_value() || !(opt_loca = Loca::from_slice(opt_loca_slice.value(), maxp.num_glyphs(), head.index_to_loc_format())).has_value())
  290. return String { "Could not load Loca" };
  291. auto loca = opt_loca.value();
  292. if (!opt_glyf_slice.has_value())
  293. return String { "Could not load Glyf" };
  294. auto glyf = Glyf(opt_glyf_slice.value());
  295. // Select cmap table. FIXME: Do this better. Right now, just looks for platform "Windows"
  296. // and corresponding encoding "Unicode full repertoire", or failing that, "Unicode BMP"
  297. for (u32 i = 0; i < cmap.num_subtables(); i++) {
  298. auto opt_subtable = cmap.subtable(i);
  299. if (!opt_subtable.has_value()) {
  300. continue;
  301. }
  302. auto subtable = opt_subtable.value();
  303. if (subtable.platform_id() == Cmap::Subtable::Platform::Windows) {
  304. if (subtable.encoding_id() == (u16)Cmap::Subtable::WindowsEncoding::UnicodeFullRepertoire) {
  305. cmap.set_active_index(i);
  306. break;
  307. }
  308. if (subtable.encoding_id() == (u16)Cmap::Subtable::WindowsEncoding::UnicodeBMP) {
  309. cmap.set_active_index(i);
  310. break;
  311. }
  312. }
  313. }
  314. return adopt_ref(*new Font(move(buffer), move(head), move(name), move(hhea), move(maxp), move(hmtx), move(cmap), move(loca), move(glyf)));
  315. }
  316. ScaledFontMetrics Font::metrics(float x_scale, float y_scale) const
  317. {
  318. auto ascender = m_hhea.ascender() * y_scale;
  319. auto descender = m_hhea.descender() * y_scale;
  320. auto line_gap = m_hhea.line_gap() * y_scale;
  321. auto advance_width_max = m_hhea.advance_width_max() * x_scale;
  322. return ScaledFontMetrics {
  323. .ascender = (int)roundf(ascender),
  324. .descender = (int)roundf(descender),
  325. .line_gap = (int)roundf(line_gap),
  326. .advance_width_max = (int)roundf(advance_width_max),
  327. };
  328. }
  329. // FIXME: "loca" and "glyf" are not available for CFF fonts.
  330. ScaledGlyphMetrics Font::glyph_metrics(u32 glyph_id, float x_scale, float y_scale) const
  331. {
  332. if (glyph_id >= glyph_count()) {
  333. glyph_id = 0;
  334. }
  335. auto horizontal_metrics = m_hmtx.get_glyph_horizontal_metrics(glyph_id);
  336. auto glyph_offset = m_loca.get_glyph_offset(glyph_id);
  337. auto glyph = m_glyf.glyph(glyph_offset);
  338. int ascender = glyph.ascender();
  339. int descender = glyph.descender();
  340. return ScaledGlyphMetrics {
  341. .ascender = (int)roundf(ascender * y_scale),
  342. .descender = (int)roundf(descender * y_scale),
  343. .advance_width = (int)roundf(horizontal_metrics.advance_width * x_scale),
  344. .left_side_bearing = (int)roundf(horizontal_metrics.left_side_bearing * x_scale),
  345. };
  346. }
  347. // FIXME: "loca" and "glyf" are not available for CFF fonts.
  348. RefPtr<Gfx::Bitmap> Font::raster_glyph(u32 glyph_id, float x_scale, float y_scale) const
  349. {
  350. if (glyph_id >= glyph_count()) {
  351. glyph_id = 0;
  352. }
  353. auto glyph_offset = m_loca.get_glyph_offset(glyph_id);
  354. auto glyph = m_glyf.glyph(glyph_offset);
  355. return glyph.raster(x_scale, y_scale, [&](u16 glyph_id) {
  356. if (glyph_id >= glyph_count()) {
  357. glyph_id = 0;
  358. }
  359. auto glyph_offset = m_loca.get_glyph_offset(glyph_id);
  360. return m_glyf.glyph(glyph_offset);
  361. });
  362. }
  363. u32 Font::glyph_count() const
  364. {
  365. return m_maxp.num_glyphs();
  366. }
  367. u16 Font::units_per_em() const
  368. {
  369. return m_head.units_per_em();
  370. }
  371. String Font::family() const
  372. {
  373. auto string = m_name.typographic_family_name();
  374. if (!string.is_empty())
  375. return string;
  376. return m_name.family_name();
  377. }
  378. String Font::variant() const
  379. {
  380. auto string = m_name.typographic_subfamily_name();
  381. if (!string.is_empty())
  382. return string;
  383. return m_name.subfamily_name();
  384. }
  385. u16 Font::weight() const
  386. {
  387. // FIXME: This is pretty naive, read weight from the actual font table(s)
  388. auto variant_name = variant();
  389. if (variant_name == "Thin")
  390. return 100;
  391. if (variant_name == "Extra Light")
  392. return 200;
  393. if (variant_name == "Light")
  394. return 300;
  395. if (variant_name == "Regular")
  396. return 400;
  397. if (variant_name == "Medium")
  398. return 500;
  399. if (variant_name == "Semi Bold")
  400. return 600;
  401. if (variant_name == "Bold")
  402. return 700;
  403. if (variant_name == "Extra Bold")
  404. return 800;
  405. if (variant_name == "Black")
  406. return 900;
  407. if (variant_name == "Extra Black")
  408. return 950;
  409. return 400;
  410. }
  411. bool Font::is_fixed_width() const
  412. {
  413. // FIXME: Read this information from the font file itself.
  414. // FIXME: Although, it appears some application do similar hacks
  415. return glyph_metrics(glyph_id_for_code_point('.'), 1, 1).advance_width == glyph_metrics(glyph_id_for_code_point('X'), 1, 1).advance_width;
  416. }
  417. int ScaledFont::width(StringView const& view) const { return unicode_view_width(Utf8View(view)); }
  418. int ScaledFont::width(Utf8View const& view) const { return unicode_view_width(view); }
  419. int ScaledFont::width(Utf32View const& view) const { return unicode_view_width(view); }
  420. template<typename T>
  421. ALWAYS_INLINE int ScaledFont::unicode_view_width(T const& view) const
  422. {
  423. if (view.is_empty())
  424. return 0;
  425. int width = 0;
  426. int longest_width = 0;
  427. for (auto code_point : view) {
  428. if (code_point == '\n' || code_point == '\r') {
  429. longest_width = max(width, longest_width);
  430. width = 0;
  431. continue;
  432. }
  433. u32 glyph_id = glyph_id_for_code_point(code_point);
  434. auto metrics = glyph_metrics(glyph_id);
  435. width += metrics.advance_width;
  436. }
  437. longest_width = max(width, longest_width);
  438. return longest_width;
  439. }
  440. RefPtr<Gfx::Bitmap> ScaledFont::raster_glyph(u32 glyph_id) const
  441. {
  442. auto glyph_iterator = m_cached_glyph_bitmaps.find(glyph_id);
  443. if (glyph_iterator != m_cached_glyph_bitmaps.end())
  444. return glyph_iterator->value;
  445. auto glyph_bitmap = m_font->raster_glyph(glyph_id, m_x_scale, m_y_scale);
  446. m_cached_glyph_bitmaps.set(glyph_id, glyph_bitmap);
  447. return glyph_bitmap;
  448. }
  449. Gfx::Glyph ScaledFont::glyph(u32 code_point) const
  450. {
  451. auto id = glyph_id_for_code_point(code_point);
  452. auto bitmap = raster_glyph(id);
  453. auto metrics = glyph_metrics(id);
  454. return Gfx::Glyph(bitmap, metrics.left_side_bearing, metrics.advance_width, metrics.ascender);
  455. }
  456. u8 ScaledFont::glyph_width(size_t code_point) const
  457. {
  458. auto id = glyph_id_for_code_point(code_point);
  459. auto metrics = glyph_metrics(id);
  460. return metrics.advance_width;
  461. }
  462. int ScaledFont::glyph_or_emoji_width(u32 code_point) const
  463. {
  464. auto id = glyph_id_for_code_point(code_point);
  465. auto metrics = glyph_metrics(id);
  466. return metrics.advance_width;
  467. }
  468. u8 ScaledFont::glyph_fixed_width() const
  469. {
  470. return glyph_metrics(glyph_id_for_code_point(' ')).advance_width;
  471. }
  472. }