fix font error

This commit is contained in:
2025-04-20 02:13:59 +10:00
parent 6028e7d8c2
commit 9d1d3f0f17
8 changed files with 115 additions and 14 deletions

View File

@ -1,4 +1,4 @@
package downloader
package bilinovel
import (
"bilinovel-downloader/model"
@ -286,6 +286,11 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
return fmt.Errorf("failed to render cover: %v", err)
}
err = DownloadFont(filepath.Join(outputPath, "OEBPS/Fonts"))
if err != nil {
return fmt.Errorf("failed to download font: %v", err)
}
contentsXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/contents.xhtml")
err = os.MkdirAll(path.Dir(contentsXHTMLPath), 0755)
if err != nil {
@ -331,7 +336,7 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
return fmt.Errorf("failed to create toc ncx: %v", err)
}
err = utils.CreateEpub(outputPath)
err = CreateEpub(outputPath)
if err != nil {
return fmt.Errorf("failed to create epub: %v", err)
}
@ -407,6 +412,7 @@ func downloadChapterByPage(page, chapterIdx int, chapter *model.Chapter, outputP
content.Find(".cgo").Remove()
content.Find("center").Remove()
content.Find(".google-auto-placed").Remove()
content.Find("p").Last().AddClass("read-font")
content.Find("img").Each(func(i int, s *goquery.Selection) {
if err != nil {
@ -549,21 +555,26 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
Media: "application/x-dtbncx+xml",
})
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: "cover",
ID: "cover.xhtml",
Link: "Text/cover.xhtml",
Media: "application/xhtml+xml",
})
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: "contents",
ID: "contents.xhtml",
Link: "Text/contents.xhtml",
Media: "application/xhtml+xml",
Properties: "nav",
})
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: "images-cover",
ID: "images-cover" + path.Ext(volume.Cover),
Link: fmt.Sprintf("Images/cover%s", path.Ext(volume.Cover)),
Media: fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(path.Ext(volume.Cover), "."), "jpg", "jpeg")),
})
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: "read.woff2",
Link: "Fonts/read.woff2",
Media: "font/woff2",
})
for _, chapter := range volume.Chapters {
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: path.Base(chapter.TextOEBPSPath),
@ -655,3 +666,24 @@ func CreateTocNCX(dirPath string, uuid string, volume *model.Volume) error {
}
return nil
}
func DownloadFont(outputPath string) error {
log.Printf("Downloading Font: read.woff2")
fontPath := filepath.Join(outputPath, "read.woff2")
err := os.MkdirAll(path.Dir(fontPath), 0755)
if err != nil {
return fmt.Errorf("failed to create font directory: %v", err)
}
resp, err := utils.Request().Get("https://www.bilinovel.com/public/font/read.woff2")
if err != nil {
return fmt.Errorf("failed to download font: %v", err)
}
err = os.WriteFile(fontPath, resp.Body(), 0644)
if err != nil {
return fmt.Errorf("failed to write font: %v", err)
}
return nil
}

View File

@ -0,0 +1,119 @@
package bilinovel
import (
"archive/zip"
"io"
"log"
"os"
"path/filepath"
)
func CreateEpub(path string) error {
log.Printf("Creating epub for %s", path)
savePath := path + ".epub"
zipFile, err := os.Create(savePath)
if err != nil {
return err
}
defer zipFile.Close()
zipWriter := zip.NewWriter(zipFile)
defer zipWriter.Close()
err = addStringToZip(zipWriter, "mimetype", "application/epub+zip", zip.Store)
if err != nil {
return err
}
err = addDirContentToZip(zipWriter, path, zip.Deflate)
if err != nil {
return err
}
err = addStringToZip(zipWriter, "OEBPS/Styles/style.css", StyleCSS, zip.Deflate)
if err != nil {
return err
}
return nil
}
// func addFileToZip(zipWriter *zip.Writer, filename string, relPath string, method uint16) error {
// file, err := os.Open(filename)
// if err != nil {
// return err
// }
// defer file.Close()
// info, err := file.Stat()
// if err != nil {
// return err
// }
// header, err := zip.FileInfoHeader(info)
// if err != nil {
// return err
// }
// header.Name = relPath
// header.Method = method
// writer, err := zipWriter.CreateHeader(header)
// if err != nil {
// return err
// }
// _, err = io.Copy(writer, file)
// return err
// }
func addStringToZip(zipWriter *zip.Writer, relPath, content string, method uint16) error {
header := &zip.FileHeader{
Name: relPath,
Method: method,
}
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
_, err = writer.Write([]byte(content))
return err
}
func addDirContentToZip(zipWriter *zip.Writer, dirPath string, method uint16) error {
return filepath.Walk(dirPath, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
relPath, err := filepath.Rel(dirPath, filePath)
if err != nil {
return err
}
file, err := os.Open(filePath)
if err != nil {
return err
}
defer file.Close()
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Name = relPath
header.Method = method
writer, err := zipWriter.CreateHeader(header)
if err != nil {
return err
}
_, err = io.Copy(writer, file)
return err
})
}

View File

@ -0,0 +1,52 @@
package bilinovel
const StyleCSS = `
@font-face{
font-family: "read";
src: url(../Fonts/read.woff2);
}
.read-font{
font-family: "read" !important;
}
body > div {
margin: 0 auto;
padding: 20px;
box-sizing: border-box;
background-color: #fff;
line-height: 1.6;
text-align: justify;
color: #333333;
}
h1 {
text-align: center;
font-size: 1.5em;
margin: 2em auto;
font-weight: bold;
color: #2c3e50;
}
p {
text-indent: 2em;
margin: 0.8em 0;
font-size: 1.1em;
}
hr {
border: none;
border-bottom: 1px solid #e0e0e0;
margin: 1.5em 20%;
}
img {
max-width: 80%;
height: auto;
display: block;
margin-left: auto !important;
margin-right: auto !important;
margin-top: 1em;
margin-bottom: 1em;
}
`