]> git.lyx.org Git - features.git/commitdiff
Amend 3058deb: Make convert_fonts robust
authorKornel Benko <kornel@lyx.org>
Sat, 11 Aug 2018 11:04:57 +0000 (13:04 +0200)
committerKornel Benko <kornel@lyx.org>
Sat, 11 Aug 2018 11:04:57 +0000 (13:04 +0200)
The preamble handling needs to know, which package belongs to
which font-type
* The conversion is now independent of the sequence in the preamble
  This is important for instance, if the user created the preamble manually

lib/lyx2lyx/lyx_2_4.py

index 323187868a4081c7e619d80db08faef22917ea77..1574fb5d3cbb3e6e72216ef5912070f5ed85af1e 100644 (file)
@@ -43,26 +43,25 @@ from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble)
 ####################################################################
 # Private helper functions
 
-def convert_fonts(document, font_list):
+def convert_fonts(document, font_list, font_type, scale_type):
     " Handle font definition to LaTeX "
 
-    font_types = ["\\font_roman", "\\font_sans,sf", "\\font_typewriter,tt"]
     rpkg = re.compile(r'^\\usepackage(\[scaled=([^\]]*)\])?\{([^\}]+)\}')
-    for ft1 in font_types:
-        fts = ft1.split(",")
-        ft = fts[0]
-        if len(fts) > 1:
-            fontscale = "\\font_" + fts[1] + "_scale"
-        else:
-            fontscale = None
-        i = 0
+    ft = font_type
+    if scale_type == None:
+        fontscale = None
+    else:
+        fontscale = "\\font_" + scale_type + "_scale"
+    i = 0
+    while i < len(document.preamble):
         i = find_re(document.preamble, rpkg, i)
         if i == -1:
-            continue
+            return
         mo = rpkg.search(document.preamble[i])
         option = mo.group(2)
         pkg = mo.group(3)
         if not pkg in font_list:
+            i += 1
             continue
         del document.preamble[i]
         if i > 0 and document.preamble[i-1] == "% Added by lyx2lyx":
@@ -116,9 +115,13 @@ def revert_fonts(document, font_list):
 def convert_dejavu(document):
     " Handle DejaVu font definition to LaTeX "
 
-    dejavu_fonts = ['DejaVuSerif', 'DejaVuSerifCondensed', 'DejaVuSans',
-                    'DejaVuSansMono', 'DejaVuSansCondensed']
-    convert_fonts(document, dejavu_fonts)
+    dejavu_fonts_roman = ['DejaVuSerif', 'DejaVuSerifCondensed']
+    dejavu_fonts_sans = ['DejaVuSans','DejaVuSansCondensed']
+    dejavu_fonts_typewriter = ['DejaVuSansMono']
+
+    convert_fonts(document, dejavu_fonts_roman, "\\font_roman", None)
+    convert_fonts(document, dejavu_fonts_sans, "\\font_sans", "sf")
+    convert_fonts(document, dejavu_fonts_typewriter, "\\font_typewriter", "tt")
 
 def revert_dejavu(document):
     " Revert native DejaVu font definition to LaTeX "