1313#include < string_view>
1414#include < unordered_map>
1515
16- namespace odr ::internal::oldms {
17-
18- text::CharacterIndex text::read_character_index (std::istream &in) {
19- CharacterIndex result;
20-
21- read_Clx (in, skip_Prc, [&](std::istream &) {
22- if (const int c = in.get (); c != 0x2 ) {
23- throw std::runtime_error (" Unexpected input: " + std::to_string (c));
24- }
25- const std::uint32_t lcb = util::byte_stream::read<std::uint32_t >(in);
26- std::string plcPcd = util::stream::read (in, lcb);
27- const PlcPcdMap plc_pcd_map (plcPcd.data (), plcPcd.size ());
28-
29- for (std::uint32_t i = 0 ; i < plc_pcd_map.n (); ++i) {
30- const bool is_compressed = plc_pcd_map.aData (i).fc .fCompressed != 0 ;
31- const std::size_t data_offset = is_compressed
32- ? plc_pcd_map.aData (i).fc .fc / 2
33- : plc_pcd_map.aData (i).fc .fc ;
34- const std::size_t length_cp = plc_pcd_map.aCP (i + 1 ) - plc_pcd_map.aCP (i);
35- result.append (plc_pcd_map.aCP (i), length_cp, data_offset, is_compressed);
36- }
37- });
38-
39- return result;
40- }
16+ namespace odr ::internal::oldms::text {
4117
42- void text:: CharacterRuns::append_run (const std::uint32_t begin_fc,
43- const std::uint32_t end_fc,
44- const std::uint32_t style_index) {
18+ void CharacterRuns::append_run (const std::uint32_t begin_fc,
19+ const std::uint32_t end_fc,
20+ const std::uint32_t style_index) {
4521 if (begin_fc >= end_fc ||
4622 (!m_runs.empty () && begin_fc < m_runs.back ().end_fc )) {
4723 throw std::runtime_error (" doc: character runs must be ascending" );
@@ -51,10 +27,10 @@ void text::CharacterRuns::append_run(const std::uint32_t begin_fc,
5127 m_runs.back ().end_fc = end_fc;
5228 return ;
5329 }
54- m_runs.push_back ({ begin_fc, end_fc, style_index} );
30+ m_runs.emplace_back ( begin_fc, end_fc, style_index);
5531}
5632
57- std::uint32_t text:: CharacterRuns::index_at (const std::uint32_t fc) const {
33+ std::uint32_t CharacterRuns::index_at (const std::uint32_t fc) const {
5834 const auto it = std::ranges::upper_bound (m_runs, fc, {}, &Run::begin_fc);
5935 if (it == m_runs.begin ()) {
6036 return 0 ;
@@ -63,7 +39,7 @@ std::uint32_t text::CharacterRuns::index_at(const std::uint32_t fc) const {
6339 return fc < run.end_fc ? run.style_index : 0 ;
6440}
6541
66- std::uint32_t text:: CharacterRuns::chunk_end (const std::uint32_t fc) const {
42+ std::uint32_t CharacterRuns::chunk_end (const std::uint32_t fc) const {
6743 const auto it = std::ranges::upper_bound (m_runs, fc, {}, &Run::begin_fc);
6844 if (it != m_runs.begin () && fc < std::prev (it)->end_fc ) {
6945 return std::prev (it)->end_fc ;
@@ -74,17 +50,46 @@ std::uint32_t text::CharacterRuns::chunk_end(const std::uint32_t fc) const {
7450 return std::numeric_limits<std::uint32_t >::max ();
7551}
7652
77- text::CharacterRuns text::read_character_runs (std::istream &document_stream,
78- std::istream &table_stream,
79- const FcLcb plcf_bte_chpx,
80- StyleRegistry &style_registry) {
53+ } // namespace odr::internal::oldms::text
54+
55+ namespace odr ::internal::oldms {
56+
57+ text::CharacterIndex text::read_character_index (std::istream &in) {
58+ CharacterIndex result;
59+
60+ read_Clx (in, skip_Prc, [&](std::istream &) {
61+ if (const int c = in.get (); c != 0x2 ) {
62+ throw std::runtime_error (" Unexpected input: " + std::to_string (c));
63+ }
64+ const std::uint32_t lcb = util::byte_stream::read<std::uint32_t >(in);
65+ std::string plcPcd = util::stream::read (in, lcb);
66+ const PlcPcdMap plc_pcd_map (plcPcd.data (), plcPcd.size ());
67+
68+ for (std::uint32_t i = 0 ; i < plc_pcd_map.n (); ++i) {
69+ const bool is_compressed = plc_pcd_map.aData (i).fc .fCompressed != 0 ;
70+ const std::size_t data_offset = is_compressed
71+ ? plc_pcd_map.aData (i).fc .fc / 2
72+ : plc_pcd_map.aData (i).fc .fc ;
73+ const std::size_t length_cp = plc_pcd_map.aCP (i + 1 ) - plc_pcd_map.aCP (i);
74+ result.append (plc_pcd_map.aCP (i), length_cp, data_offset, is_compressed);
75+ }
76+ });
77+
78+ return result;
79+ }
80+
81+ text::CharacterRuns
82+ text::read_character_runs (std::istream &document_stream,
83+ std::istream &table_stream, const FcLcb plcf_bte_chpx,
84+ std::vector<TextStyle> &styles,
85+ const std::span<const std::string> font_names) {
8186 CharacterRuns result;
8287 if (plcf_bte_chpx.lcb == 0 ) {
8388 return result;
8489 }
8590
86- // Copy: `add_style ` may reallocate the registry's style storage .
87- const TextStyle default_style = style_registry. text_style (0 );
91+ // Copy: appending to `styles ` may reallocate.
92+ const TextStyle default_style = styles. at (0 );
8893
8994 table_stream.seekg (plcf_bte_chpx.fc );
9095 std::string plc_bytes = util::stream::read (table_stream, plcf_bte_chpx.lcb );
@@ -96,14 +101,13 @@ text::CharacterRuns text::read_character_runs(std::istream &document_stream,
96101 if (grpprl.empty ()) {
97102 return 0 ;
98103 }
99- const auto it = style_cache.find (std::string (grpprl));
100- if (it != style_cache.end ()) {
101- return it->second ;
104+ const auto [it, inserted] = style_cache.try_emplace (std::string (grpprl));
105+ if (inserted) {
106+ styles.push_back (
107+ apply_character_sprms (default_style, grpprl, font_names));
108+ it->second = static_cast <std::uint32_t >(styles.size () - 1 );
102109 }
103- const std::uint32_t index = style_registry.add_style (apply_character_sprms (
104- default_style, grpprl, style_registry.font_names ()));
105- style_cache.emplace (std::string (grpprl), index);
106- return index;
110+ return it->second ;
107111 };
108112
109113 for (std::uint32_t i = 0 ; i < plc.n (); ++i) {
0 commit comments