From ba2b86fa5d718c434521a7b9722e6ccd677864ad Mon Sep 17 00:00:00 2001 From: Pavel Sanda Date: Thu, 2 Jun 2016 22:40:21 -0700 Subject: [PATCH] Introduce basic support for microtype.sty. https://www.mail-archive.com/lyx-devel@lists.lyx.org/msg192743.html --- development/FORMAT | 5 ++ lib/lyx2lyx/LyX.py | 5 +- lib/lyx2lyx/Makefile.am | 1 + lib/lyx2lyx/lyx_2_3.py | 75 ++++++++++++++++++++ src/BufferParams.cpp | 7 ++ src/BufferParams.h | 2 + src/LaTeXFeatures.cpp | 4 ++ src/frontends/qt4/GuiDocument.cpp | 6 ++ src/frontends/qt4/ui/FontUi.ui | 110 ++++++++++++++++-------------- src/tex2lyx/Preamble.cpp | 14 +++- src/tex2lyx/Preamble.h | 1 + src/version.h | 4 +- 12 files changed, 178 insertions(+), 56 deletions(-) create mode 100644 lib/lyx2lyx/lyx_2_3.py diff --git a/development/FORMAT b/development/FORMAT index 01319dff51..f7bb30f08b 100644 --- a/development/FORMAT +++ b/development/FORMAT @@ -11,6 +11,11 @@ adjustments are made to tex2lyx and bugs are fixed in lyx2lyx. ----------------------- +2016-06-16 Pavel Sanda + * Format incremented to 509. + New parameter "\use_microtype bool" for including microtype LaTeX + package into preamble. + 2016-04-05 Enrico Forestieri * Format incremented to 508 New kind of Separator inset (latexpar). The old parbreak separator diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py index 1e2147d549..fa2bbba619 100644 --- a/lib/lyx2lyx/LyX.py +++ b/lib/lyx2lyx/LyX.py @@ -33,7 +33,7 @@ try: import lyx2lyx_version version__ = lyx2lyx_version.version except: # we are running from build directory so assume the last version - version__ = '2.2' + version__ = '2.3' default_debug__ = 2 @@ -86,7 +86,8 @@ format_relation = [("0_06", [200], minor_versions("0.6" , 4)), ("1_6", list(range(277,346)), minor_versions("1.6" , 10)), ("2_0", list(range(346,414)), minor_versions("2.0" , 8)), ("2_1", list(range(414,475)), minor_versions("2.1" , 5)), - ("2_2", list(range(475,509)), minor_versions("2.2" , 0)) + ("2_2", list(range(475,509)), minor_versions("2.2" , 0)), + ("2_3", list(range(509,510)), minor_versions("2.3" , 0)) ] #################################################################### diff --git a/lib/lyx2lyx/Makefile.am b/lib/lyx2lyx/Makefile.am index 4490cb72f3..89ba23b1f2 100644 --- a/lib/lyx2lyx/Makefile.am +++ b/lib/lyx2lyx/Makefile.am @@ -33,6 +33,7 @@ dist_lyx2lyx_PYTHON = \ lyx_2_0.py \ lyx_2_1.py \ lyx_2_2.py \ + lyx_2_3.py \ profiling.py \ test_parser_tools.py diff --git a/lib/lyx2lyx/lyx_2_3.py b/lib/lyx2lyx/lyx_2_3.py new file mode 100644 index 0000000000..08ca2f05d9 --- /dev/null +++ b/lib/lyx2lyx/lyx_2_3.py @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +# This file is part of lyx2lyx +# -*- coding: utf-8 -*- +# Copyright (C) 2016 The LyX team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +""" Convert files to the file format generated by lyx 2.3""" + +import re, string +import unicodedata +import sys, os + +# Uncomment only what you need to import, please. + +#from parser_tools import find_token, find_end_of, find_tokens, \ +# find_token_exact, find_end_of_inset, find_end_of_layout, \ +# find_token_backwards, is_in_inset, get_value, get_quoted_value, \ +# del_token, check_token, get_option_value + +#from lyx2lyx_tools import add_to_preamble, put_cmd_in_ert, get_ert, lyx2latex, \ +# lyx2verbatim, length_in_bp, convert_info_insets +# insert_to_preamble, latex_length, revert_flex_inset, \ +# revert_font_attrs, hex2ratio, str2bool + +from parser_tools import find_token + +#################################################################### +# Private helper functions + + + +############################################################################### +### +### Conversion and reversion routines +### +############################################################################### + +def revert_microtype(document): + " Remove microtype settings. " + i = find_token(document.header, "\\use_microtype", 0) + if i == -1: + return + del document.header[i] + + + +## +# Conversion hub +# + +supported_versions = ["2.3.0", "2.3"] +convert = [ + [509, []] + ] + +revert = [ + [508, [revert_microtype]] + ] + + +if __name__ == "__main__": + pass diff --git a/src/BufferParams.cpp b/src/BufferParams.cpp index 9d121cb4f4..bcf8d64503 100644 --- a/src/BufferParams.cpp +++ b/src/BufferParams.cpp @@ -388,6 +388,7 @@ BufferParams::BufferParams() fonts_math[1] = "auto"; fonts_default_family = "default"; useNonTeXFonts = false; + use_microtype = false; fonts_expert_sc = false; fonts_old_figures = false; fonts_sans_scale[0] = 100; @@ -771,6 +772,8 @@ string BufferParams::readToken(Lexer & lex, string const & token, lex >> fonts_typewriter_scale[1]; } else if (token == "\\font_cjk") { lex >> fonts_cjk; + } else if (token == "\\use_microtype") { + lex >> use_microtype; } else if (token == "\\paragraph_separation") { string parsep; lex >> parsep; @@ -1139,6 +1142,7 @@ void BufferParams::writeFile(ostream & os, Buffer const * buf) const if (!fonts_cjk.empty()) { os << "\\font_cjk " << fonts_cjk << '\n'; } + os << "\\use_microtype " << convert(use_microtype) << '\n'; os << "\\graphics " << graphics_driver << '\n'; os << "\\default_output_format " << default_output_format << '\n'; os << "\\output_sync " << output_sync << '\n'; @@ -1411,6 +1415,9 @@ void BufferParams::validate(LaTeXFeatures & features) const if (useNonTeXFonts && fontsMath() != "auto") features.require("unicode-math"); + + if (use_microtype) + features.require("microtype"); if (!language->requires().empty()) features.require(language->requires()); diff --git a/src/BufferParams.h b/src/BufferParams.h index f62911c262..7314b421b2 100644 --- a/src/BufferParams.h +++ b/src/BufferParams.h @@ -278,6 +278,8 @@ public: int fontsTypewriterScale() const { return fonts_typewriter_scale[useNonTeXFonts]; } /// the font used by the CJK command std::string fonts_cjk; + /// use LaTeX microtype package + bool use_microtype; /// Spacing & spacing(); Spacing const & spacing() const; diff --git a/src/LaTeXFeatures.cpp b/src/LaTeXFeatures.cpp index 4fa2c82941..3a2e775316 100644 --- a/src/LaTeXFeatures.cpp +++ b/src/LaTeXFeatures.cpp @@ -1132,6 +1132,10 @@ string const LaTeXFeatures::getPackages() const if (mustProvide("footmisc")) packages << "\\PassOptionsToPackage{stable}{footmisc}\n"; + if (mustProvide("microtype")){ + packages << "\\usepackage{microtype}\n"; + } + return packages.str(); } diff --git a/src/frontends/qt4/GuiDocument.cpp b/src/frontends/qt4/GuiDocument.cpp index 15fb71958f..25356d921e 100644 --- a/src/frontends/qt4/GuiDocument.cpp +++ b/src/frontends/qt4/GuiDocument.cpp @@ -809,6 +809,8 @@ GuiDocument::GuiDocument(GuiView & lv) this, SLOT(change_adaptor())); connect(fontModule->cjkFontLE, SIGNAL(textChanged(const QString &)), this, SLOT(change_adaptor())); + connect(fontModule->microtypeCB, SIGNAL(clicked()), + this, SLOT(change_adaptor())); connect(fontModule->scaleSansSB, SIGNAL(valueChanged(int)), this, SLOT(change_adaptor())); connect(fontModule->scaleTypewriterSB, SIGNAL(valueChanged(int)), @@ -2889,6 +2891,8 @@ void GuiDocument::applyView() bp_.fonts_cjk = fromqstr(fontModule->cjkFontLE->text()); + bp_.use_microtype = fontModule->microtypeCB->isChecked(); + bp_.fonts_sans_scale[nontexfonts] = fontModule->scaleSansSB->value(); bp_.fonts_sans_scale[!nontexfonts] = fontModule->font_sf_scale; @@ -3368,6 +3372,8 @@ void GuiDocument::paramsToDialog() toqstr(bp_.fonts_cjk)); else fontModule->cjkFontLE->setText(QString()); + + fontModule->microtypeCB->setChecked(bp_.use_microtype); fontModule->fontScCB->setChecked(bp_.fonts_expert_sc); fontModule->fontOsfCB->setChecked(bp_.fonts_old_figures); diff --git a/src/frontends/qt4/ui/FontUi.ui b/src/frontends/qt4/ui/FontUi.ui index ba7a5ea76b..3c0e14c094 100644 --- a/src/frontends/qt4/ui/FontUi.ui +++ b/src/frontends/qt4/ui/FontUi.ui @@ -6,47 +6,14 @@ 0 0 - 407 - 285 + 541 + 319 FontUi - - - - Use OpenType and TrueType fonts with the fontspec package (requires XeTeX or LuaTeX) - - - &Use non-TeX fonts (via XeTeX/LuaTeX) - - - - - - - Qt::Horizontal - - - - 59 - 20 - - - - - - - - &Default family: - - - fontsDefaultCO - - - @@ -101,6 +68,39 @@ + + + + Use OpenType and TrueType fonts with the fontspec package (requires XeTeX or LuaTeX) + + + &Use non-TeX fonts (via XeTeX/LuaTeX) + + + + + + + Qt::Horizontal + + + + 59 + 20 + + + + + + + + &Default family: + + + fontsDefaultCO + + + @@ -135,6 +135,13 @@ + + + + Select the typewriter (monospaced) typeface + + + @@ -158,13 +165,6 @@ - - - - Select the typewriter (monospaced) typeface - - - @@ -175,6 +175,13 @@ + + + + Select the math typeface + + + @@ -198,13 +205,6 @@ - - - - Select the math typeface - - - @@ -242,7 +242,7 @@ - + Qt::Vertical @@ -255,6 +255,16 @@ + + + + Activate extensions such as character protusion and font expansion via the microtype package + + + Enable micr&o-typographic extensions + + + diff --git a/src/tex2lyx/Preamble.cpp b/src/tex2lyx/Preamble.cpp index 24c403d23e..2481a41db5 100644 --- a/src/tex2lyx/Preamble.cpp +++ b/src/tex2lyx/Preamble.cpp @@ -457,7 +457,8 @@ string remove_braces(string const & value) Preamble::Preamble() : one_language(true), explicit_babel(false), - title_layout_found(false), index_number(0), h_font_cjk_set(false) + title_layout_found(false), index_number(0), h_font_cjk_set(false), + h_use_microtype(false) { //h_backgroundcolor; //h_boxbgcolor; @@ -543,6 +544,7 @@ Preamble::Preamble() : one_language(true), explicit_babel(false), h_use_geometry = "false"; h_use_default_options = "false"; h_use_hyperref = "false"; + h_use_microtype = false; h_use_refstyle = false; h_use_packages["amsmath"] = "1"; h_use_packages["amssymb"] = "0"; @@ -1013,6 +1015,13 @@ void Preamble::handle_package(Parser &p, string const & name, h_options += ',' + join(options, ","); } } + else if (name == "microtype") { + //we internally support only microtype without params + if (options.empty()) + h_use_microtype = true; + else + h_preamble << "\\usepackage[" << opts << "]{microtype}"; + } else if (!in_lyx_preamble) { if (options.empty()) @@ -1162,7 +1171,8 @@ bool Preamble::writeLyXHeader(ostream & os, bool subdoc, string const & outfiled << ' ' << h_font_tt_scale[1] << '\n'; if (!h_font_cjk.empty()) os << "\\font_cjk " << h_font_cjk << '\n'; - os << "\\graphics " << h_graphics << '\n' + os << "\\use_microtype " << h_use_microtype << '\n' + << "\\graphics " << h_graphics << '\n' << "\\default_output_format " << h_default_output_format << "\n" << "\\output_sync " << h_output_sync << "\n"; if (h_output_sync == "1") diff --git a/src/tex2lyx/Preamble.h b/src/tex2lyx/Preamble.h index f9342efca2..350644d305 100644 --- a/src/tex2lyx/Preamble.h +++ b/src/tex2lyx/Preamble.h @@ -151,6 +151,7 @@ private: std::string h_font_tt_scale[2]; bool h_font_cjk_set; std::string h_font_cjk; + bool h_use_microtype; std::string h_graphics; std::string h_default_output_format; std::string h_html_be_strict; diff --git a/src/version.h b/src/version.h index 757f43f3c1..d8aecddd33 100644 --- a/src/version.h +++ b/src/version.h @@ -32,8 +32,8 @@ extern char const * const lyx_version_info; // Do not remove the comment below, so we get merge conflict in // independent branches. Instead add your own. -#define LYX_FORMAT_LYX 508 // forenr: convert parbreak to latexpar -#define LYX_FORMAT_TEX2LYX 508 +#define LYX_FORMAT_LYX 509 // ps: microtype +#define LYX_FORMAT_TEX2LYX 509 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX #ifndef _MSC_VER -- 2.39.2