+
+
+namespace {
+
+string const unique_filename(string const bufferpath)
+{
+ static int theCounter = 0;
+ string const filename = tostr(theCounter++) + "lyxpreview";
+ return AddName(bufferpath, filename);
+}
+
+
+Converter const * setConverter()
+{
+ string const from = "lyxpreview";
+
+ Formats::FormatList::const_iterator it = formats.begin();
+ Formats::FormatList::const_iterator end = formats.end();
+
+ for (; it != end; ++it) {
+ string const to = it->name();
+ if (from == to)
+ continue;
+
+ Converter const * ptr = converters.getConverter(from, to);
+ if (ptr)
+ return ptr;
+ }
+
+ static bool first = true;
+ if (first) {
+ first = false;
+ lyxerr << "PreviewLoader::startLoading()\n"
+ << "No converter from \"lyxpreview\" format has been "
+ "defined."
+ << endl;
+ }
+
+ return 0;
+}
+
+
+void setAscentFractions(vector<double> & ascent_fractions,
+ string const & metrics_file)
+{
+ // If all else fails, then the images will have equal ascents and
+ // descents.
+ vector<double>::iterator it = ascent_fractions.begin();
+ vector<double>::iterator end = ascent_fractions.end();
+ fill(it, end, 0.5);
+
+ ifstream in(metrics_file.c_str());
+ if (!in.good()) {
+ lyxerr[Debug::GRAPHICS]
+ << "setAscentFractions(" << metrics_file << ")\n"
+ << "Unable to open file!" << endl;
+ return;
+ }
+
+ bool error = false;
+
+ // Tightpage dimensions affect all subsequent dimensions
+ int tp_ascent;
+ int tp_descent;
+
+ int snippet_counter = 0;
+ while (!in.eof()) {
+ // Expecting lines of the form
+ // Preview: Tightpage tp_bl_x tp_bl_y tp_tr_x tp_tr_y
+ // Preview: Snippet id ascent descent width
+ string preview;
+ string type;
+ in >> preview >> type;
+
+ if (!in.good())
+ // eof after all
+ break;
+
+ error = preview != "Preview:"
+ || (type != "Tightpage" && type != "Snippet");
+ if (error)
+ break;
+
+ if (type == "Tightpage") {
+ int dummy;
+ in >> dummy >> tp_descent >> dummy >> tp_ascent;
+
+ error = !in.good();
+ if (error)
+ break;
+
+ } else {
+ int dummy;
+ int snippet_id;
+ int ascent;
+ int descent;
+ in >> snippet_id >> ascent >> descent >> dummy;
+
+ error = !in.good() || ++snippet_counter != snippet_id;
+ if (error)
+ break;
+
+ double const a = ascent + tp_ascent;
+ double const d = descent - tp_descent;
+
+ if (!lyx::float_equal(a + d, 0, 0.1))
+ *it = a / (a + d);
+
+ if (++it == end)
+ break;
+ }
+ }
+
+ if (error) {
+ lyxerr[Debug::GRAPHICS]
+ << "setAscentFractions(" << metrics_file << ")\n"
+ << "Error reading file!\n" << endl;
+ }
+}
+
+} // namespace anon