Exemplo n.º 1
0
docstring sgml::escapeString(docstring const & raw)
{
	docstring bin;
	bin.reserve(raw.size() * 2); // crude approximation is sufficient
	for (size_t i = 0; i != raw.size(); ++i)
		bin += sgml::escapeChar(raw[i]);

	return bin;
}
Exemplo n.º 2
0
int InsetText::plaintext(odocstringstream & os,
        OutputParams const & runparams, size_t max_length) const
{
	ParagraphList::const_iterator beg = paragraphs().begin();
	ParagraphList::const_iterator end = paragraphs().end();
	ParagraphList::const_iterator it = beg;
	bool ref_printed = false;
	int len = 0;
	for (; it != end; ++it) {
		if (it != beg) {
			os << '\n';
			if (runparams.linelen > 0)
				os << '\n';
		}
		odocstringstream oss;
		writePlaintextParagraph(buffer(), *it, oss, runparams, ref_printed, max_length);
		docstring const str = oss.str();
		os << str;
		// FIXME: len is not computed fully correctly; in principle,
		// we have to count the characters after the last '\n'
		len = str.size();
		if (os.str().size() >= max_length)
			break;
	}

	return len;
}
Exemplo n.º 3
0
int InsetRef::plaintext(odocstringstream & os,
        OutputParams const &, size_t) const
{
	docstring const str = getParam("reference");
	os << '[' << str << ']';
	return 2 + str.size();
}
Exemplo n.º 4
0
int InsetCommand::plaintext(odocstream & os, OutputParams const &) const
{
	docstring const str = "[" + buffer().B_("LaTeX Command: ")
		+ from_utf8(getCmdName()) + "]";
	os << str;
	return str.size();
}
Exemplo n.º 5
0
int GuiFontMetrics::signedWidth(docstring const & s) const
{
	if (s.empty())
		return 0;

	if (s[0] == '-')
		return -width(s.substr(1, s.size() - 1));
	else
		return width(s);
}
Exemplo n.º 6
0
void InsetMath::write(WriteStream & os) const
{
	MathEnsurer ensurer(os);
	docstring const s = name();
	os << "\\" << s;
	// We need an extra ' ' unless this is a single-char-non-ASCII name
	// or anything non-ASCII follows
	if (s.size() != 1 || isAlphaASCII(s[0]))
		os.pendingSpace(true);
}
Exemplo n.º 7
0
static docstring convertDelimToLatexName(docstring const & name)
{
	if (name.size() == 1) {
		char_type const c = name[0];
		if (c == '<' || c == '(' || c == '[' || c == '.'
		    || c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
			return name;
	}
	return '\\' + name + ' ';
}
Exemplo n.º 8
0
int InsetFormulaMacro::plaintext(odocstream & os, OutputParams const & runparams) const
{
	odocstringstream oss;
	WriteStream wi(oss, false, true, WriteStream::wsDefault, runparams.encoding);
	tmpl()->write(wi);

	docstring const str = oss.str();
	os << str;
	return str.size();
}
Exemplo n.º 9
0
int InsetCitation::plaintext(odocstringstream & os,
       OutputParams const &, size_t) const
{
	string const & cmd = getCmdName();
	if (cmd == "nocite")
		return 0;

	docstring const label = generateLabel(false);
	os << label;
	return label.size();
}
Exemplo n.º 10
0
int InsetHyperlink::plaintext(odocstream & os, OutputParams const &) const
{
	odocstringstream oss;

	oss << '[' << getParam("target");
	if (getParam("name").empty())
		oss << ']';
	else
		oss << "||" << getParam("name") << ']';

	docstring const str = oss.str();
	os << str;
	return str.size();
}
Exemplo n.º 11
0
// the ref argument is the label name we are referencing.
// we expect ref to be in the form: pfx:suffix.
//
// if it isn't, then we can't produce a formatted reference, 
// so we return "\ref" and put ref into label.
//
// for refstyle, we return "\pfxcmd", and put suffix into 
// label and pfx into prefix. this is because refstyle expects
// the command: \pfxcmd{suffix}.
// 
// for prettyref, we return "\prettyref" and put ref into label
// and pfx into prefix. this is because prettyref uses the whole
// label, thus: \prettyref{pfx:suffix}.
//
docstring InsetRef::getFormattedCmd(docstring const & ref, 
	docstring & label, docstring & prefix) const
{
	static docstring const defcmd = from_ascii("\\ref");
	static docstring const prtcmd = from_ascii("\\prettyref");
	
	label = split(ref, prefix, ':');

	// we have to have xxx:xxxxx...
	if (label.empty()) {
		LYXERR0("Label `" << ref << "' contains no prefix.");
		label = ref;
		prefix = from_ascii("");
		return defcmd;
	}

	if (prefix.empty()) {
		// we have ":xxxx"
		label = ref;
		return defcmd;
	}
	
	if (!buffer().params().use_refstyle) {
		// \prettyref uses the whole label
		label = ref;
		return prtcmd;
	}

	// make sure the prefix is legal for a latex command
	int const len = prefix.size();
	for (int i = 0; i < len; i++) {
		char_type const c = prefix[i];
		if (!isAlphaASCII(c)) {
			LYXERR0("Prefix `" << prefix << "' is invalid for LaTeX.");
			// restore the label
			label = ref;
			return defcmd;
		}
	}
	return from_ascii("\\") + prefix + from_ascii("ref");
}
Exemplo n.º 12
0
int GuiFontMetrics::width(docstring const & s) const
{
	size_t ls = s.size();
	int w = 0;
	for (unsigned int i = 0; i < ls; ++i) {
		//FIXME: we need to detect surrogate pairs and act accordingly
		/**
		if isSurrogateBase(s[i]) {
			docstring c = s[i];
			if (smallcaps_shape_)
				w += metrics_.width(toqstr(c + s[i + 1]));
			else
				w += smallcaps_metrics_.width(toqstr(c + s[i + 1]));
			++i;
		}
		else
		*/
		w += width(s[i]);
	}

	return w;
}
Exemplo n.º 13
0
void PDFOptions::writeLaTeX(OutputParams & runparams, otexstream & os,
			    bool hyperref_already_provided) const
{
	// FIXME Unicode
	string opt;
	string hyperset;
	
	// since LyX uses unicode, also set the PDF strings to unicode strings with the
	// hyperref option "unicode"
	opt += "unicode=true,";

	// only use the hyperref settings if hyperref is enabled by the user
	// see bug #7052
	if (use_hyperref) {
		// try to extract author and title from document when none is
		// explicitly given
		if (pdfusetitle && title.empty() && author.empty())
			opt += "pdfusetitle,";
		opt += "\n ";
		opt += "bookmarks=" + convert<string>(bookmarks) + ',';
		if (bookmarks) {
			opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
			opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
			if (bookmarksopen)
				opt += "bookmarksopenlevel="
				+ convert<string>(bookmarksopenlevel) + ',';
		}
		opt += "\n ";
		opt += "breaklinks=" + convert<string>(breaklinks) + ',';
		opt += "pdfborder={0 0 ";
		opt += (pdfborder ? '0' : '1');
		opt += "},";
		opt += "backref=" + backref + ',';
		opt += "colorlinks=" + convert<string>(colorlinks) + ',';
		if (!pagemode.empty())
			opt += "pdfpagemode=" + pagemode + ',';

		// load the pdftitle etc. as hypersetup, otherwise you'll get
		// LaTeX-errors when using non-latin characters
		if (!title.empty())
			hyperset += "pdftitle={" + title + "},";
		if (!author.empty())			
			hyperset += "\n pdfauthor={" + author + "},";
		if (!subject.empty())
			hyperset += "\n pdfsubject={" + subject + "},";
		if (!keywords.empty())
			hyperset += "\n pdfkeywords={" + keywords + "},";
		if (!quoted_options.empty()){
			hyperset += "\n ";
			hyperset += quoted_options;
		}
		hyperset = rtrim(hyperset,",");
	}

	// check if the hyperref settings use an encoding that exceeds
	// ours. If so, we have to switch to utf8.
	Encoding const * const enc = runparams.encoding;
	docstring const hs = from_utf8(hyperset);
	bool need_unicode = false;
	if (enc) {
		for (size_t n = 0; n < hs.size(); ++n) {
			if (!enc->encodable(hs[n]))
				need_unicode = true;
		}
	}

	// use in \\usepackage parameter as not all options can be handled inside \\hypersetup
	if (!hyperref_already_provided) {
		opt = rtrim(opt, ",");
		opt = "\\usepackage[" + opt + "]\n {hyperref}\n";

		if (!hyperset.empty())
			opt += "\\hypersetup{" + hyperset + "}\n";
	} else {
		// only in case hyperref is already loaded by the current text class
		// try to put it into hyperset
		//
		// FIXME: rename in this case the PDF settings dialog checkbox
		//  label from "Use Hyperref" to "Customize Hyperref Settings"
		//  as discussd in bug #6293
		opt = "\\hypersetup{" + rtrim(opt + hyperset, ",") + "}\n";
	}

	// hyperref expects utf8!
	if (need_unicode && enc && enc->iconvName() != "UTF-8"
	    &&!runparams.isFullUnicode()) {
		os << "\\inputencoding{utf8}\n"
		   << setEncoding("UTF-8");
	}
	// If hyperref is loaded by the document class, we output
	// \hypersetup \AtBeginDocument if hypersetup is not (yet)
	// defined. In this case, the class loads hyperref late
	// (see bug #7048).
	if (hyperref_already_provided && !opt.empty()) {
		os << "\\ifx\\hypersetup\\undefined\n"
		   << "  \\AtBeginDocument{%\n    "
		   << from_utf8(opt)
		   << "  }\n"
		   << "\\else\n  "
		   << from_utf8(opt)
		   << "\\fi\n";
	} else
		os << from_utf8(opt);

	if (need_unicode && enc && enc->iconvName() != "UTF-8"
	    &&!runparams.isFullUnicode()) {
		os << setEncoding(enc->iconvName())
		   << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
	}
}
Exemplo n.º 14
0
int PDFOptions::writeLaTeX(OutputParams & runparams, odocstream & os,
			    bool hyperref_already_provided) const
{
	int lines = 0;
	// FIXME Unicode
	string opt;
	
	// since LyX uses unicode, also set the PDF strings to unicode strings with the
	// hyperref option "unicode"
	opt += "unicode=true, ";
	
	// try to extract author and title from document when none is
	// explicitly given
	if (pdfusetitle && title.empty() && author.empty())
		opt += "pdfusetitle,";
	opt += "\n ";

	opt += "bookmarks=" + convert<string>(bookmarks) + ',';
	if (bookmarks) {
		opt += "bookmarksnumbered=" + convert<string>(bookmarksnumbered) + ',';
		opt += "bookmarksopen=" + convert<string>(bookmarksopen) + ',';
		if (bookmarksopen)
			opt += "bookmarksopenlevel="
			    + convert<string>(bookmarksopenlevel) + ',';
	}
	opt += "\n ";
	opt += "breaklinks=" + convert<string>(breaklinks) + ',';

	opt += "pdfborder={0 0 ";
	opt += (pdfborder ? '0' : '1');
	opt += "},";

	opt += "backref=" + backref + ',';
	opt += "colorlinks=" + convert<string>(colorlinks) + ',';
	if (!pagemode.empty())
		opt += "pdfpagemode=" + pagemode + ',';
	


	// load the pdftitle etc. as hypersetup, otherwise you'll get
	// LaTeX-errors when using non-latin characters
	string hyperset;
	if (!title.empty())
		hyperset += "pdftitle={" + title + "},";
	if (!author.empty())
		hyperset += "\n pdfauthor={" + author + "},";
	if (!subject.empty())
		hyperset += "\n pdfsubject={" + subject + "},";
	if (!keywords.empty())
		hyperset += "\n pdfkeywords={" + keywords + "},";
	if (!quoted_options.empty()){
		hyperset += "\n ";
		hyperset += quoted_options;
	}
	hyperset = rtrim(hyperset,",");

	// check if the hyperref settings use an encoding that exceeds
	// ours. If so, we have to switch to utf8.
	Encoding const * const enc = runparams.encoding;
	docstring const hs = from_utf8(hyperset);
	bool need_unicode = false;
	if (enc) {
		for (size_t n = 0; n < hs.size(); ++n) {
			if (enc->latexChar(hs[n], true) != docstring(1, hs[n]))
				need_unicode = true;
		}
	}

	// use in \\usepackage parameter as not all options can be handled inside \\hypersetup
	if (!hyperref_already_provided) {
		opt = rtrim(opt, ",");
		opt = "\\usepackage[" + opt + "]\n {hyperref}\n";

		if (!hyperset.empty())
			opt += "\\hypersetup{" + hyperset + "}\n";
	} else
		// only in case hyperref is already loaded by the current text class
		// try to put it into hyperset
		//
		// FIXME: this still does not fix the cases where hyperref is loaded
		//        and the option is active only when part of usepackage parameter
		//        (e.g. pdfusetitle).
		{
			opt = "\\hypersetup{" + opt + hyperset + "}\n";
		}

	lines = int(count(opt.begin(), opt.end(), '\n'));

	// hyperref expects utf8!
	if (need_unicode && enc && enc->iconvName() != "UTF-8") {
		os << "\\inputencoding{utf8}\n"
		   << setEncoding("UTF-8");
		++lines;
	}
	os << from_utf8(opt);
	if (need_unicode && enc && enc->iconvName() != "UTF-8") {
		os << setEncoding(enc->iconvName())
		   << "\\inputencoding{" << from_ascii(enc->latexName()) << "}\n";
		++lines;
	}
	return lines;
}
Exemplo n.º 15
0
docstring sgml::cleanID(Buffer const & buf, OutputParams const & runparams,
	docstring const & orig)
{
	// The standard DocBook SGML declaration only allows letters,
	// digits, '-' and '.' in a name.
	// Since users might change that declaration one has to cater
	// for additional allowed characters.
	// This routine replaces illegal characters by '-' or '.'
	// and adds a number for uniqueness.
	// If you know what you are doing, you can set allowed==""
	// to disable this mangling.
	DocumentClass const & tclass = buf.params().documentClass();
	docstring const allowed = from_ascii(
		runparams.flavor == OutputParams::XML ? ".-_:" : tclass.options());

	if (allowed.empty())
		return orig;

	docstring::const_iterator it  = orig.begin();
	docstring::const_iterator end = orig.end();

	docstring content;

	// FIXME THREAD
	typedef map<docstring, docstring> MangledMap;
	static MangledMap mangledNames;
	static int mangleID = 1;

	MangledMap::const_iterator const known = mangledNames.find(orig);
	if (known != mangledNames.end())
		return known->second;

	// make sure it starts with a letter
	if (!isAlphaASCII(*it) && allowed.find(*it) >= allowed.size())
		content += "x";

	bool mangle = false;
	for (; it != end; ++it) {
		char_type c = *it;
		if (isAlphaASCII(c) || isDigitASCII(c) || c == '-' || c == '.'
		      || allowed.find(c) < allowed.size())
			content += c;
		else if (c == '_' || c == ' ') {
			mangle = true;
			content += "-";
		}
		else if (c == ':' || c == ',' || c == ';' || c == '!') {
			mangle = true;
			content += ".";
		}
		else {
			mangle = true;
		}
	}

	if (mangle)
		content += "-" + convert<docstring>(mangleID++);
	else if (isDigitASCII(content[content.size() - 1]))
		content += ".";

	mangledNames[orig] = content;

	return content;
}
Exemplo n.º 16
0
string const to_utf8(docstring const & ucs4)
{
	vector<char> const utf8 = ucs4_to_utf8(ucs4.data(), ucs4.size());
	return string(utf8.begin(), utf8.end());
}