Exemplo n.º 1
0
void InsetPreview::preparePreview(DocIterator const & pos) const  
{
	TexRow texrow;
	odocstringstream str;  
	otexstream os(str, texrow);
	OutputParams runparams(&pos.buffer()->params().encoding());
	latex(os, runparams);
	docstring const snippet = str.str();
	preview_->addPreview(snippet, *pos.buffer());  
}
Exemplo n.º 2
0
void PreviewLoader::Impl::dumpPreamble(otexstream & os) const
{
	// Dump the preamble only.
	OutputParams runparams(&buffer_.params().encoding());
	if (buffer_.params().useNonTeXFonts)
		runparams.flavor = OutputParams::XETEX;
	else
		runparams.flavor = OutputParams::LATEX;
	runparams.nice = true;
	runparams.moving_arg = true;
	runparams.free_spacing = true;
	buffer_.writeLaTeXSource(os, buffer_.filePath(), runparams, Buffer::OnlyPreamble);

	// FIXME! This is a HACK! The proper fix is to control the 'true'
	// passed to WriteStream below:
	// int InsetMathNest::latex(Buffer const &, odocstream & os,
	//                          OutputParams const & runparams) const
	// {
	//	WriteStream wi(os, runparams.moving_arg, true);
	//	par_->write(wi);
	//	return wi.line();
	// }
	os << "\n"
	   << "\\def\\lyxlock{}\n"
	   << "\n";

	// All equation labels appear as "(#)" + preview.sty's rendering of
	// the label name
	if (lyxrc.preview_hashed_labels)
		os << "\\renewcommand{\\theequation}{\\#}\n";

	// Use the preview style file to ensure that each snippet appears on a
	// fresh page.
	// Also support PDF output (automatically generated e.g. when
	// \usepackage[pdftex]{hyperref} is used and XeTeX.
	os << "\n"
	   << "\\usepackage[active,delayed,showlabels,lyx]{preview}\n"
	   << "\n";
}
Exemplo n.º 3
0
void PreviewLoader::Impl::startLoading(bool wait)
{
	if (pending_.empty() || !pconverter_)
		return;

	// Only start the process off after the buffer is loaded from file.
	if (!buffer_.isFullyLoaded())
		return;

	LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()");

	// As used by the LaTeX file and by the resulting image files
	string const directory = buffer_.temppath();

	string const filename_base = unique_filename(directory);

	// Create an InProgress instance to place in the map of all
	// such processes if it starts correctly.
	InProgress inprogress(filename_base, pending_, pconverter_->to);

	// clear pending_, so we're ready to start afresh.
	pending_.clear();

	// Output the LaTeX file.
	FileName const latexfile(filename_base + ".tex");

	// we use the encoding of the buffer
	Encoding const & enc = buffer_.params().encoding();
	ofdocstream of;
	try { of.reset(enc.iconvName()); }
	catch (iconv_codecvt_facet_exception const & e) {
		LYXERR0("Caught iconv exception: " << e.what()
			<< "\nUnable to create LaTeX file: " << latexfile);
		return;
	}

	TexRow texrow;
	otexstream os(of, texrow);
	OutputParams runparams(&enc);
	LaTeXFeatures features(buffer_, buffer_.params(), runparams);

	if (!openFileWrite(of, latexfile))
		return;

	if (!of) {
		LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
					<< "Unable to create LaTeX file\n" << latexfile);
		return;
	}
	of << "\\batchmode\n";
	dumpPreamble(os);
	// handle inputenc etc.
	buffer_.params().writeEncodingPreamble(os, features);
	of << "\n\\begin{document}\n";
	dumpData(of, inprogress.snippets);
	of << "\n\\end{document}\n";
	of.close();
	if (of.fail()) {
		LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
					 << "File was not closed properly.");
		return;
	}

	double const font_scaling_factor = 
		buffer_.isExporting() ? 75.0 * buffer_.params().html_math_img_scale 
			: 0.01 * lyxrc.dpi * lyxrc.zoom * lyxrc.preview_scale_factor;

	// FIXME XHTML 
	// The colors should be customizable.
	ColorCode const bg = buffer_.isExporting() 
	               ? Color_white : PreviewLoader::backgroundColor();
	ColorCode const fg = buffer_.isExporting() 
	               ? Color_black : PreviewLoader::foregroundColor();
	// The conversion command.
	ostringstream cs;
	cs << pconverter_->command
	   << " " << quoteName(latexfile.toFilesystemEncoding())
	   << " --dpi " << int(font_scaling_factor)
	   << " --fg " << theApp()->hexName(fg)
	   << " --bg " << theApp()->hexName(bg);
	// FIXME what about LuaTeX?
	if (buffer_.params().useNonTeXFonts)
		cs << " --latex=xelatex";
	if (buffer_.params().encoding().package() == Encoding::japanese)
		cs << " --latex=platex";
	if (buffer_.params().bibtex_command != "default")
		cs << " --bibtex=" << quoteName(buffer_.params().bibtex_command);
	else if (buffer_.params().encoding().package() == Encoding::japanese)
		cs << " --bibtex=" << quoteName(lyxrc.jbibtex_command);
	else
		cs << " --bibtex=" << quoteName(lyxrc.bibtex_command);
	if (buffer_.params().bufferFormat() == "lilypond-book")
		cs << " --lilypond";

	string const command = libScriptSearch(cs.str());

	if (wait) {
		ForkedCall call(buffer_.filePath());
		int ret = call.startScript(ForkedProcess::Wait, command);
		static int fake = (2^20) + 1;
		int pid = fake++;
		inprogress.pid = pid;
		inprogress.command = command;
		in_progress_[pid] = inprogress;
		finishedGenerating(pid, ret);
		return;
	}

	// Initiate the conversion from LaTeX to bitmap images files.
	ForkedCall::SignalTypePtr
		convert_ptr(new ForkedCall::SignalType);
	convert_ptr->connect(bind(&Impl::finishedGenerating, this, _1, _2));

	ForkedCall call(buffer_.filePath());
	int ret = call.startScript(command, convert_ptr);

	if (ret != 0) {
		LYXERR(Debug::GRAPHICS, "PreviewLoader::startLoading()\n"
					<< "Unable to start process\n" << command);
		return;
	}

	// Store the generation process in a list of all such processes
	inprogress.pid = call.pid();
	inprogress.command = command;
	in_progress_[inprogress.pid] = inprogress;
}