KDP(電子出版)のメモ 急急如律令

Amazon Kindleダイレクト・パブリッシングでの電子出版や電子書籍の作成販売について、文章やイラストの作成や編集方法について書いています。

SVG表紙の生成機能を試してみた。

縦書きはキンドルでは横書きにされる。 横書きでは文字のサイズを合わせるか改行ができないのではみ出す。

表紙の設定は4項目に

  • SVG表紙

を追加

jComboCover = new JComboBox<String>(new String[]{"[先頭の挿絵]", "[入力ファイル名と同じ画像(png,jpg)]", "[表紙無し]", "http://","[SVG表紙生成]"});
     //表紙にする挿絵の位置-1なら挿絵は使わない
        int coverImageIndex = -1;       
        //表紙画像をSVGで生成するか
        boolean coverSvg = false;
        //表紙情報追加
        String coverFileName = this.jComboCover.getEditor().getItem().toString();
        if (coverFileName.equals(this.jComboCover.getItemAt(0))) {
            coverFileName = ""; //先頭の挿絵
            coverImageIndex = 0;
        } else if (coverFileName.equals(this.jComboCover.getItemAt(1))) {
            coverFileName = AozoraEpub3.getSameCoverFileName(srcFile); //入力ファイルと同じ名前+.jpg/.png
        } else if (coverFileName.equals(this.jComboCover.getItemAt(2))) {
            coverFileName = null; //表紙無し
        } else if (coverFileName.equals(this.jComboCover.getItemAt(4))) {
            coverSvg=true; //SVG表紙画像
            coverFileName = null; //表紙無し
        }

変換時にSVG表紙画像の設定をどうやって渡すか。bookinfoに追加するか。

     //変換実行
        AozoraEpub3.convertFile(
            srcFile, ext, outFile,
            this.aozoraConverter,
            writer,
            encType,
            bookInfo, imageInfoReader, txtIdx
        );

BookInfo.javaのクラスを参考にsvgCoverImageというのを作る。

 /** SVG表紙画像ありならtrue */
    public boolean svgCoverImage = false;
    public boolean isSvgCoverImage()
    {
        return svgCoverImage;
    }
    public void setSvgCoverImage(boolean svgCoverImage)
    {
        this.svgCoverImage = svgCoverImage;
    }

bookInfoに設定の追加

        //表紙ページの情報をbookInfoに設定
        bookInfo.coverFileName = coverFileName;
        bookInfo.coverImageIndex = coverImageIndex;
        bookInfo.svgCoverImage=coverSvg;
         if (insertCoverInfo != null) {
                SectionInfo sectionInfo = new SectionInfo("cover-page");
                if (this.imageSizeType != SectionInfo.IMAGE_SIZE_TYPE_AUTO) {
                    //画像が横長なら幅100% それ以外は高さ100%
                    if ((double)insertCoverInfo.getWidth()/insertCoverInfo.getHeight() >= (double)this.coverW/this.coverH) sectionInfo.setImageFitW(true);
                    else sectionInfo.setImageFitH(true);
                } else {
                    sectionInfo.setImageFitW(false);
                    sectionInfo.setImageFitH(false);
                }
                this.velocityContext.put("sectionInfo", sectionInfo);
                this.velocityContext.put("coverImage", insertCoverInfo);
                zos.putArchiveEntry(new ZipArchiveEntry(OPS_PATH+XHTML_PATH+COVER_FILE));
                bw = new BufferedWriter(new OutputStreamWriter(zos, StandardCharsets.UTF_8));
                Velocity.mergeTemplate(templatePath+OPS_PATH+XHTML_PATH+COVER_VM, "UTF-8", velocityContext, bw);
                bw.flush();
                zos.closeArchiveEntry();
            } else if (bookInfo.svgCoverImage) {
                zos.putArchiveEntry(new ZipArchiveEntry(OPS_PATH+XHTML_PATH+COVER_FILE));
                bw = new BufferedWriter(new OutputStreamWriter(zos, StandardCharsets.UTF_8));
                Velocity.mergeTemplate(templatePath+OPS_PATH+XHTML_PATH+COVER_VM, "UTF-8", velocityContext, bw);
                bw.flush();
                zos.closeArchiveEntry();
            } else {
                //画像がなかったら表紙ページ無し
                bookInfo.insertCoverPage = false;
            }
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:epub="http://www.idpf.org/2007/ops"
xml:lang="ja"
>
<head>
<meta charset="UTF-8"/>
<title>${title}</title>
<link rel="stylesheet" type="text/css" href="../style/fixed-layout-jp.css"/>
#if (${bookInfo.svgCoverImage})
<meta name="viewport" content="width=800, height=1200"/>
#else
<meta name="viewport" content="width=${coverImage.Width}, height=${coverImage.Height}"/>
#end
</head>
<body epub:type="cover">
<div class="main">
#if (${bookInfo.svgCoverImage})
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="100%" height="100%" viewBox="0 0 800 1200">
    <!-- ドロップシャドウ -->
<filter id="dropshadow" height="130%">
      <feGaussianBlur in="SourceAlpha" stdDeviation="3"/>
      <feOffset dx="2" dy="2" result="offsetblur"/>
      <feComponentTransfer>
        <feFuncA type="linear" slope="0.5"/>
      </feComponentTransfer>
      <feMerge> 
        <feMergeNode/>
        <feMergeNode in="SourceGraphic"/>
      </feMerge>
    </filter>
    <!-- 背景の四角形 -->
    <rect width="100%" height="100%" fill="#f4f4f4" />
    <rect x="80" y="80" width="80%" height="85%" fill="#1858DB" filter="url(#dropshadow)" rx="10" ry="10"/>
    <!-- タイトル (横書き) -->
      <text  x="50%" y="20%" dominant-baseline="middle" text-anchor="middle" font-family="serif" font-size="48" fill="#fff"  filter="url(#dropshadow)">
        ${title}
    </text>

    <!-- 著者名 (横書き) -->
    <text x="50%" y="70%" dominant-baseline="middle" text-anchor="middle" font-family="serif" font-size="36" fill="#fff"  filter="url(#dropshadow)">
        ${creator}
    </text>
</svg>
#else
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"
xmlns:xlink="http://www.w3.org/1999/xlink"
width="100%" height="100%" viewBox="0 0 ${coverImage.Width} ${coverImage.Height}">
<image width="${coverImage.Width}" height="${coverImage.Height}" xlink:href="../image/${coverImage.OutFileName}"/>
</svg>
#end
</div>
</body>
</html>

99nyorituryo.hatenablog.com