]> granicus.if.org Git - docbook-dsssl/commitdiff
This resolves bug #2873142, Please add support for multiple embedded fonts
authorKeith Fahlgren <abdelazer@users.sourceforge.net>
Sat, 10 Oct 2009 00:40:43 +0000 (00:40 +0000)
committerKeith Fahlgren <abdelazer@users.sourceforge.net>
Sat, 10 Oct 2009 00:40:43 +0000 (00:40 +0000)
If you navigate to a checkout of DocBook-XSL and go to:
xsl/epub/bin/spec/files
You can now run the following command:

../../dbtoepub -f DejaVuSerif.otf -f DejaVuSerif-Italic.otf -c test.css
-s test_cust.xsl orm.book.001.xml

In dbtoepub, the following option can be used more than once:
-f, --font [OTF FILE] Embed OTF FILE in .epub.

The underlying stylesheet now accepts a comma-separated list of font file
names rather than just one as the RENAMED epub.embedded.fonts ('s' added).

The runnable EPUB spec now includes:
- should be valid .epub after including more than one embedded font

xsl/epub/bin/lib/docbook.rb
xsl/epub/bin/spec/epub_spec.rb
xsl/epub/bin/spec/files/DejaVuSerif-Italic.otf [new file with mode: 0644]
xsl/epub/bin/spec/files/test.css
xsl/epub/docbook.xsl

index 2664999bb74f9a6ae27824c1443fac7cb8cb2a92..2bb90a97b082612b07f08caa04f2074eed89c262 100755 (executable)
@@ -26,7 +26,6 @@ module DocBook
       @oebps_dir = File.join(@output_dir, OEBPS_DIR)
       @css_file = css_file ? File.expand_path(css_file) : css_file
       @embedded_fonts = embedded_fonts
-      raise NotImplementedError if @embedded_fonts.length > 1
       @to_delete = []
       
       if customization_layer
@@ -70,7 +69,8 @@ module DocBook
       html_stylesheet = "--stringparam html.stylesheet #{File.basename(@css_file)}" if @css_file
       base =            "--stringparam base.dir #{OEBPS_DIR}/" 
       unless @embedded_fonts.empty? 
-        font =            "--stringparam epub.embedded.font \"#{File.basename(@embedded_fonts.first)}\"" 
+        embedded_fonts = @embedded_fonts.map {|f| File.basename(f)}.join(',')
+        font =            "--stringparam epub.embedded.fonts \"#{embedded_fonts}\"" 
       end  
       meta =            "--stringparam epub.metainf.dir #{META_DIR}/" 
       oebps =           "--stringparam epub.oebps.dir #{OEBPS_DIR}/" 
index 6ab69c952a5851b7ed3e6b8cbfc1013b3e967b5b..a77494a2365e539ed98bec67ebf768efac33c530 100755 (executable)
@@ -39,8 +39,11 @@ describe DocBook::Epub do
     @css_file = File.join(@filedir, @css_file_base)
     customization_layer = nil
     @embedded_font_file_base = "DejaVuSerif.otf"
-    embedded_fonts = [File.join(@filedir, @embedded_font_file_base)]
-    @css_epub = DocBook::Epub.new(File.join(@testdocsdir, "book.002.xml"), @tmpdir, @css_file, customization_layer, embedded_fonts)
+    @embedded_font_file2_base = "DejaVuSerif-Italic.otf"
+    @embedded_fonts = [File.join(@filedir, @embedded_font_file_base),
+                       File.join(@filedir, @embedded_font_file2_base),
+                      ]
+    @css_epub = DocBook::Epub.new(File.join(@testdocsdir, "book.002.xml"), @tmpdir, @css_file, customization_layer, @embedded_fonts)
     @css_epubfile = File.join(@tmpdir, "css.epub")
     @css_epub.render_to_file(@css_epubfile, $DEBUG)
 
@@ -233,8 +236,11 @@ describe DocBook::Epub do
       success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{@css_epubfile}")
       raise "Could not unzip #{@css_epubfile}" unless success
       opf_files = Dir.glob(File.join(tmpdir, "**", "*.opf"))
-      opf_links = opf_files.find_all {|opf_file| File.open(opf_file).readlines.to_s =~ /<(opf:item|item) [^>]*#{@embedded_font_file_base}/}
-      opf_links.should_not be_empty
+
+      @embedded_fonts.each {|font|
+        opf_links = opf_files.find_all {|opf_file| File.open(opf_file).readlines.to_s =~ /<(opf:item|item) [^>]*#{File.basename(font)}/}
+        opf_links.should_not be_empty
+      }  
     rescue => e
       raise e
     ensure
@@ -248,8 +254,10 @@ describe DocBook::Epub do
       
       success = system("unzip -q -d #{File.expand_path(tmpdir)} -o #{@css_epubfile}")
       raise "Could not unzip #{@css_epubfile}" unless success
-      font_files = Dir.glob(File.join(tmpdir, "**", @embedded_font_file_base))
-      font_files.should_not be_empty
+      @embedded_fonts.each {|font|
+        font_files = Dir.glob(File.join(tmpdir, "**", File.basename(font)))
+        font_files.should_not be_empty
+      }  
     rescue => e
       raise e
     ensure
@@ -257,6 +265,10 @@ describe DocBook::Epub do
     end
   end
 
+  it "should be valid .epub after including more than one embedded font" do
+    @css_epubfile.should be_valid_epub
+  end
+
   it "should include one and only one <h1> in each HTML file in rendered ePub files for <book>s" do
     begin
       tmpdir = File.join(Dir::tmpdir(), "epubtoctest"); Dir.mkdir(tmpdir) rescue Errno::EEXIST
diff --git a/xsl/epub/bin/spec/files/DejaVuSerif-Italic.otf b/xsl/epub/bin/spec/files/DejaVuSerif-Italic.otf
new file mode 100644 (file)
index 0000000..af33591
Binary files /dev/null and b/xsl/epub/bin/spec/files/DejaVuSerif-Italic.otf differ
index cd70946dc3e0e1bc3eae57e30f655bcc36423a6c..19594597227d25809c4c56c5badf5b751b87aa85 100644 (file)
@@ -4,8 +4,17 @@
   font-weight: normal;
   src:url(DejaVuSerif.otf);
 }
+@font-face {
+  font-family: "DejaVu Serif Italic";
+  font-style: italic;
+  font-weight: normal;
+  src:url(DejaVuSerif-Italic.otf);
+}
 h2 {
   font-family: "DejaVu Serif";
   text-align: center;
   color: #dda0dd;
 }
+em {
+  font-family: "DejaVu Serif Italic";
+}
index 1ca985fe69bd12af9bed21f0a7713cbbcb6a1d58..48c601e952cbda3848a5cf05bd4cce6bdcae5e1e 100644 (file)
@@ -8,11 +8,12 @@
   xmlns:ng="http://docbook.org/docbook-ng"
   xmlns:opf="http://www.idpf.org/2007/opf"
   xmlns:stext="http://nwalsh.com/xslt/ext/com.nwalsh.saxon.TextFactory"
+  xmlns:str="http://exslt.org/strings"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:xtext="xalan://com.nwalsh.xalan.Text"
 
   extension-element-prefixes="stext xtext"
-  exclude-result-prefixes="exsl db dc h ncx ng opf stext xtext"
+  exclude-result-prefixes="exsl db dc h ncx ng opf stext str xtext"
 
   version="1.0">
 
@@ -48,7 +49,7 @@
   <xsl:param name="epub.html.toc.id">htmltoc</xsl:param>
   <xsl:param name="epub.metainf.dir" select="'META-INF/'"/> 
 
-  <xsl:param name="epub.embedded.font"></xsl:param>
+  <xsl:param name="epub.embedded.fonts"></xsl:param>
 
   <!-- Per Bob Stayton:
        """Process your documents with the css.decoration parameter set to zero. 
         </xsl:element>
       </xsl:if>  
 
-     <xsl:if test="$epub.embedded.font != ''">
-        <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
-          <xsl:attribute name="id">epub.embedded.font</xsl:attribute>
-          <xsl:attribute name="href"><xsl:value-of select="$epub.embedded.font"/></xsl:attribute>
-          <xsl:choose>
-            <xsl:when test="contains($epub.embedded.font, 'otf')">
-              <xsl:attribute name="media-type">font/opentype</xsl:attribute>
-            </xsl:when>
-            <xsl:otherwise>
-              <xsl:message>
-                <xsl:text>WARNING: OpenType fonts should be supplied! (</xsl:text>
-                <xsl:value-of select="$epub.embedded.font"/>
-                <xsl:text>)</xsl:text>
-              </xsl:message>
-            </xsl:otherwise>  
-            </xsl:choose>
-        </xsl:element>
-     </xsl:if>
+      <xsl:choose>
+        <xsl:when test="$epub.embedded.fonts != '' and not(contains($epub.embedded.fonts, ','))">
+          <xsl:call-template name="embedded-font-item">
+            <xsl:with-param name="font.file" select="$epub.embedded.fonts"/> <!-- There is just one -->
+          </xsl:call-template>
+        </xsl:when>
+        <xsl:when test="$epub.embedded.fonts != ''">
+          <xsl:variable name="font.file.tokens" select="str:tokenize($epub.embedded.fonts, ',')"/>
+          <xsl:for-each select="exsl:node-set($font.file.tokens)">
+            <xsl:call-template name="embedded-font-item">
+              <xsl:with-param name="font.file" select="."/>
+              <xsl:with-param name="font.order" select="position()"/>
+            </xsl:call-template>
+          </xsl:for-each>
+        </xsl:when>
+      </xsl:choose>
 
       <!-- TODO: be nice to have a id="titlepage" here -->
       <xsl:apply-templates select="//part|
   <xsl:template match="bibliodiv[title]" mode="label.markup">
   </xsl:template>
 
+  <xsl:template match="token" mode="opf.manifest.font">
+    <xsl:call-template name="embedded-font-item">
+      <xsl:with-param name="font.file" select="."/>
+    </xsl:call-template>
+  </xsl:template>
+
+  <xsl:template name="embedded-font-item">
+    <xsl:param name="font.file"/>
+    <xsl:param name="font.order" select="1"/>
+
+    <xsl:element namespace="http://www.idpf.org/2007/opf" name="item">
+      <xsl:attribute name="id">
+        <xsl:value-of select="concat('epub.embedded.font.', $font.order)"/>
+      </xsl:attribute>
+      <xsl:attribute name="href"><xsl:value-of select="$font.file"/></xsl:attribute>
+      <xsl:choose>
+        <xsl:when test="contains($font.file, 'otf')">
+          <xsl:attribute name="media-type">font/opentype</xsl:attribute>
+        </xsl:when>
+        <xsl:otherwise>
+          <xsl:message>
+            <xsl:text>WARNING: OpenType fonts should be supplied! (</xsl:text>
+            <xsl:value-of select="$font.file"/>
+            <xsl:text>)</xsl:text>
+          </xsl:message>
+        </xsl:otherwise>  
+      </xsl:choose>
+    </xsl:element>
+  </xsl:template>
 
 <!-- Change section.heading to improve SEO on generated HTML by doing heading levels 
      "correctly". SEO rules are sometimes silly silly, but this does actually create