From 0efb6964efd973bf048cc6007be8fde7596b9adb Mon Sep 17 00:00:00 2001 From: Dane Springmeyer Date: Wed, 1 Oct 2014 20:59:05 -0700 Subject: [PATCH] per map load cache if a fonts is known as valid --- src/load_map.cpp | 56 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/src/load_map.cpp b/src/load_map.cpp index a73d8eb16..a87f37e00 100644 --- a/src/load_map.cpp +++ b/src/load_map.cpp @@ -89,6 +89,9 @@ public: filename_(filename), font_library_(), font_file_mapping_(map.get_font_file_mapping()), + font_name_cache_(), + file_sources_(), + fontsets_(), xml_base_path_() {} void parse_map(Map & map, xml_node const& node, std::string const& base_path); @@ -131,6 +134,7 @@ private: std::map datasource_templates_; font_library font_library_; freetype_engine::font_file_mapping_type & font_file_mapping_; + std::map font_name_cache_; std::map file_sources_; std::map fontsets_; std::string xml_base_path_; @@ -512,24 +516,34 @@ void map_parser::parse_fontset(Map & map, xml_node const& node) } } -bool map_parser::parse_font(font_set &fset, xml_node const& f) +bool map_parser::parse_font(font_set & fset, xml_node const& f) { - optional face_name = f.get_opt_attr("face-name"); - if (face_name) + optional has_face_name = f.get_opt_attr("face-name"); + if (has_face_name) { - // TODO - cache results to avoid repeated opens - if (freetype_engine::can_open(*face_name, - font_library_, - font_file_mapping_, - freetype_engine::get_mapping())) + std::string face_name = *has_face_name; + bool found = false; + auto itr = font_name_cache_.find(face_name); + if (itr != font_name_cache_.end()) { - fset.add_face_name(*face_name); + found = itr->second; + } + else + { + found = freetype_engine::can_open(face_name, + font_library_, + font_file_mapping_, + freetype_engine::get_mapping()); + font_name_cache_.emplace(face_name,found); + } + if (found) + { + fset.add_face_name(face_name); return true; } else if (strict_) { - throw config_error("Failed to find font face '" + - *face_name + "'"); + throw config_error("Failed to find font face '" + face_name + "'"); } } else @@ -1523,11 +1537,21 @@ void map_parser::parse_pair_layout(group_symbolizer_properties & prop, xml_node void map_parser::ensure_font_face(std::string const& face_name) { - // TODO - cache results to avoid repeated opens - if (!freetype_engine::can_open(face_name, - font_library_, - font_file_mapping_, - freetype_engine::get_mapping())) + bool found = false; + auto itr = font_name_cache_.find(face_name); + if (itr != font_name_cache_.end()) + { + found = itr->second; + } + else + { + found = freetype_engine::can_open(face_name, + font_library_, + font_file_mapping_, + freetype_engine::get_mapping()); + font_name_cache_.emplace(face_name,found); + } + if (!found) { throw config_error("Failed to find font face '" + face_name + "'");