X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=lib%2Flyx2lyx%2Flyx_2_4.py;h=3fd876c826797d3c3f906e59117fba09c8104f59;hb=d0a27e3afc3444d10694df10dc4cb8d604287765;hp=d4e8b0a7b5f20c6575b62d85b4cdfc661aa66dcf;hpb=e121bd04bbaed32b4c23700dc492fcd1459ff6a6;p=lyx.git diff --git a/lib/lyx2lyx/lyx_2_4.py b/lib/lyx2lyx/lyx_2_4.py index d4e8b0a7b5..3fd876c826 100644 --- a/lib/lyx2lyx/lyx_2_4.py +++ b/lib/lyx2lyx/lyx_2_4.py @@ -194,6 +194,9 @@ def createFontMapping(fontlist): 'FiraSansUltralight,ultralight'], "sans", "sf", "FiraSans", "scaled", "lf", "true") fm.expandFontMapping(['FiraMono'], "typewriter", "tt", "FiraMono", "scaled", "lf", "true") + elif font == 'libertinus': + fm.expandFontMapping(['libertinus,serif'], "roman", None, "libertinus", None, "osf") + fm.expandFontMapping(['libertinusmath'], "math", None, "libertinust1math", None, None) return fm def convert_fonts(document, fm, osfoption = "osf"): @@ -3472,6 +3475,15 @@ def revert_memoir_endnotes(document): def revert_totalheight(document): " Reverts graphics height parameter from totalheight to height " + relative_heights = { + "\\textwidth" : "text%", + "\\columnwidth" : "col%", + "\\paperwidth" : "page%", + "\\linewidth" : "line%", + "\\textheight" : "theight%", + "\\paperheight" : "pheight%", + "\\baselineskip " : "baselineskip%" + } i = 0 while (True): i = find_token(document.body, "\\begin_inset Graphics", i) @@ -3484,6 +3496,7 @@ def revert_totalheight(document): continue rx = re.compile(r'\s*special\s*(\S+)$') + rxx = re.compile(r'(\d*\.*\d+)(\S+)$') k = find_re(document.body, rx, i, j) special = "" oldheight = "" @@ -3495,6 +3508,16 @@ def revert_totalheight(document): for spc in mspecial: if spc[:7] == "height=": oldheight = spc.split('=')[1] + ms = rxx.search(oldheight) + if ms: + oldval = ms.group(1) + oldunit = ms.group(2) + if oldval[1] == ".": + oldval = "0" + oldval + if oldunit in list(relative_heights.keys()): + oldval = str(float(oldval) * 100) + oldunit = relative_heights[oldunit] + oldheight = oldval + oldunit mspecial.remove(spc) break if len(mspecial) > 0: @@ -3531,6 +3554,15 @@ def revert_totalheight(document): def convert_totalheight(document): " Converts graphics height parameter from totalheight to height " + relative_heights = { + "text%" : "\\textwidth", + "col%" : "\\columnwidth", + "page%" : "\\paperwidth", + "line%" : "\\linewidth", + "theight%" : "\\textheight", + "pheight%" : "\\paperheight", + "baselineskip%" : "\\baselineskip" + } i = 0 while (True): i = find_token(document.body, "\\begin_inset Graphics", i) @@ -3561,19 +3593,23 @@ def convert_totalheight(document): else: special = "" - rx = re.compile(r'(\s*height\s*)(\S+)$') + rx = re.compile(r'(\s*height\s*)(\d+)(\S+)$') kk = find_re(document.body, rx, i, j) if kk != -1: m = rx.match(document.body[kk]) val = "" if m: val = m.group(2) + unit = m.group(3) + if unit in list(relative_heights.keys()): + val = str(float(val) / 100) + unit = relative_heights[unit] if k != -1: if special != "": - val = val + "," + special + val = val + unit + "," + special document.body[k] = "\tspecial " + "height=" + val else: - document.body.insert(kk + 1, "\tspecial height=" + val) + document.body.insert(kk + 1, "\tspecial height=" + val + unit) if newheight != "": document.body[kk] = m.group(1) + newheight else: @@ -3741,7 +3777,7 @@ def revert_counter_inset(document): ert = put_cmd_in_ert("\\setcounter{%s}{\\value{%s}}" % (cnt, savecnt)) else: document.warning("Unknown counter command `%s' in inset at line %d!" % (cnt, i)) - + if ert: document.body[i : j + 1] = ert i += 1 @@ -3768,7 +3804,7 @@ def revert_ams_spaces(document): subst = put_cmd_in_ert(inset) document.body[i : end + 1] = subst Found = True - + if Found == True: # load amsmath in the preamble if not already loaded i = find_token(document.header, "\\use_package amsmath 2", 0) @@ -3779,18 +3815,18 @@ def revert_ams_spaces(document): def convert_parskip(document): " Move old parskip settings to preamble " - + i = find_token(document.header, "\\paragraph_separation skip", 0) if i == -1: return - + j = find_token(document.header, "\\defskip", 0) if j == -1: document.warning("Malformed LyX document! Missing \\defskip.") return - + val = get_value(document.header, "\\defskip", j) - + skipval = "\\medskipamount" if val == "smallskip" or val == "medskip" or val == "bigskip": skipval = "\\" + val + "amount" @@ -3798,25 +3834,25 @@ def convert_parskip(document): skipval = val add_to_preamble(document, ["\\setlength{\\parskip}{" + skipval + "}", "\\setlength{\\parindent}{0pt}"]) - + document.header[i] = "\\paragraph_separation indent" document.header[j] = "\\paragraph_indentation default" def revert_parskip(document): " Revert new parskip settings to preamble " - + i = find_token(document.header, "\\paragraph_separation skip", 0) if i == -1: return - + j = find_token(document.header, "\\defskip", 0) if j == -1: document.warning("Malformed LyX document! Missing \\defskip.") return - + val = get_value(document.header, "\\defskip", j) - + skipval = "" if val == "smallskip" or val == "medskip" or val == "bigskip": skipval = "[skip=\\" + val + "amount]" @@ -3824,9 +3860,9 @@ def revert_parskip(document): skipval = "[skip=\\baselineskip]" elif val != "halfline": skipval = "[skip={" + val + "}]" - + add_to_preamble(document, ["\\usepackage" + skipval + "{parskip}"]) - + document.header[i] = "\\paragraph_separation indent" document.header[j] = "\\paragraph_indentation default" @@ -3849,6 +3885,81 @@ def revert_line_vspaces(document): subst = put_cmd_in_ert(insets[inset]) document.body[i : end + 1] = subst +def convert_libertinus_rm_fonts(document): + """Handle Libertinus serif fonts definition to LaTeX""" + + if not get_bool_value(document.header, "\\use_non_tex_fonts"): + fm = createFontMapping(['Libertinus']) + convert_fonts(document, fm) + +def revert_libertinus_rm_fonts(document): + """Revert Libertinus serif font definition to LaTeX""" + + if not get_bool_value(document.header, "\\use_non_tex_fonts"): + fontmap = dict() + fm = createFontMapping(['libertinus']) + if revert_fonts(document, fm, fontmap): + add_preamble_fonts(document, fontmap) + +def revert_libertinus_sftt_fonts(document): + " Revert Libertinus sans and tt font definitions to LaTeX " + + if find_token(document.header, "\\use_non_tex_fonts false", 0) != -1: + preamble = "" + # first sf font + i = find_token(document.header, "\\font_sans \"LibertinusSans-LF\"", 0) + if i != -1: + j = find_token(document.header, "\\font_sans_osf true", 0) + if j != -1: + add_to_preamble(document, ["\\renewcommand{\\sfdefault}{LibertinusSans-OsF}"]) + document.header[j] = "\\font_sans_osf false" + else: + add_to_preamble(document, ["\\renewcommand{\\sfdefault}{LibertinusSans-LF}"]) + document.header[i] = document.header[i].replace("LibertinusSans-LF", "default") + sf_scale = 100.0 + sfval = find_token(document.header, "\\font_sf_scale", 0) + if sfval == -1: + document.warning("Malformed LyX document: Missing \\font_sf_scale.") + else: + sfscale = document.header[sfval].split() + val = sfscale[1] + sfscale[1] = "100" + document.header[sfval] = " ".join(sfscale) + try: + # float() can throw + sf_scale = float(val) + except: + document.warning("Invalid font_sf_scale value: " + val) + if sf_scale != "100.0": + add_to_preamble(document, ["\\renewcommand*{\\LibertinusSans@scale}{" + str(sf_scale / 100.0) + "}"]) + # now tt font + i = find_token(document.header, "\\font_typewriter \"LibertinusMono-TLF\"", 0) + if i != -1: + add_to_preamble(document, ["\\renewcommand{\\ttdefault}{LibertinusMono-TLF}"]) + document.header[i] = document.header[i].replace("LibertinusMono-TLF", "default") + tt_scale = 100.0 + ttval = find_token(document.header, "\\font_tt_scale", 0) + if ttval == -1: + document.warning("Malformed LyX document: Missing \\font_tt_scale.") + else: + ttscale = document.header[ttval].split() + val = ttscale[1] + ttscale[1] = "100" + document.header[ttval] = " ".join(ttscale) + try: + # float() can throw + tt_scale = float(val) + except: + document.warning("Invalid font_tt_scale value: " + val) + if tt_scale != "100.0": + add_to_preamble(document, ["\\renewcommand*{\\LibertinusMono@scale}{" + str(tt_scale / 100.0) + "}"]) + + +def revert_docbook_table_output(document): + i = find_token(document.header, '\\docbook_table_output') + if i != -1: + del document.header[i] + ## # Conversion hub @@ -3907,10 +4018,14 @@ convert = [ [593, [convert_counter_maintenance]], [594, []], [595, []], - [596, [convert_parskip]] + [596, [convert_parskip]], + [597, [convert_libertinus_rm_fonts]], + [598, []] ] -revert = [[595, [revert_parskip,revert_line_vspaces]], +revert = [[597, [revert_docbook_table_output]], + [596, [revert_libertinus_rm_fonts,revert_libertinus_sftt_fonts]], + [595, [revert_parskip,revert_line_vspaces]], [594, [revert_ams_spaces]], [593, [revert_counter_inset]], [592, [revert_counter_maintenance]],