fix: kavita 无法正确加载字体

This commit is contained in:
2025-07-17 14:03:10 +08:00
parent ca3fdf8980
commit 75745b9431
13 changed files with 70 additions and 193 deletions

View File

@@ -260,7 +260,7 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
return fmt.Errorf("failed to write volume: %v", err)
}
coverPath := filepath.Join(outputPath, "OEBPS/Images/cover.jpg")
coverPath := filepath.Join(outputPath, "cover.jpeg")
err = os.MkdirAll(path.Dir(coverPath), 0755)
if err != nil {
return fmt.Errorf("failed to create cover directory: %v", err)
@@ -279,10 +279,7 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
if err != nil {
return fmt.Errorf("failed to create cover file: %v", err)
}
err = template.ContentXHTML(&model.Chapter{
Title: "封面",
Content: fmt.Sprintf(`<img src="../Images/cover%s" />`, path.Ext(volume.Cover)),
}).Render(context.Background(), file)
err = template.CoverXHTML(fmt.Sprintf(`../../cover%s`, strings.ReplaceAll(path.Ext(volume.Cover), "jpg", "jpeg"))).Render(context.Background(), file)
if err != nil {
return fmt.Errorf("failed to render cover: %v", err)
}
@@ -332,11 +329,6 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
return fmt.Errorf("failed to create content opf: %v", err)
}
err = CreateTocNCX(outputPath, u.String(), volume)
if err != nil {
return fmt.Errorf("failed to create toc ncx: %v", err)
}
err = CreateEpub(outputPath)
if err != nil {
return fmt.Errorf("failed to create epub: %v", err)
@@ -530,7 +522,7 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
Metas: []model.DublinCoreMeta{
{
Name: "cover",
Content: "images-cover" + path.Ext(volume.Cover),
Content: "cover",
},
{
Property: "dcterms:modified",
@@ -549,42 +541,38 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
manifest := &model.Manifest{
Items: make([]model.ManifestItem, 0),
}
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: "ncx",
Link: "toc.ncx",
Media: "application/x-dtbncx+xml",
})
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: "cover.xhtml",
Link: "Text/cover.xhtml",
Link: "OEBPS/Text/cover.xhtml",
Media: "application/xhtml+xml",
})
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: "contents.xhtml",
Link: "Text/contents.xhtml",
Link: "OEBPS/Text/contents.xhtml",
Media: "application/xhtml+xml",
Properties: "nav",
})
manifest.Items = append(manifest.Items, model.ManifestItem{
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")),
ID: "cover",
Link: fmt.Sprintf("cover%s", strings.ReplaceAll(path.Ext(volume.Cover), "jpg", "jpeg")),
Media: fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(path.Ext(volume.Cover), "."), "jpg", "jpeg")),
Properties: "cover-image",
})
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: "read.ttf",
Link: "Fonts/read.ttf",
Link: "OEBPS/Fonts/read.ttf",
Media: "application/vnd.ms-opentype",
})
for _, chapter := range volume.Chapters {
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: path.Base(chapter.TextOEBPSPath),
Link: chapter.TextOEBPSPath,
Link: "OEBPS/" + chapter.TextOEBPSPath,
Media: "application/xhtml+xml",
})
for _, image := range chapter.ImageOEBPSPaths {
item := model.ManifestItem{
ID: strings.Join(strings.Split(strings.ToLower(image), string(filepath.Separator)), "-"),
Link: image,
Link: "OEBPS/" + image,
}
item.Media = fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(path.Ext(volume.Cover), "."), "jpg", "jpeg"))
manifest.Items = append(manifest.Items, item)
@@ -592,7 +580,7 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
}
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: "style",
Link: "Styles/style.css",
Link: "style.css",
Media: "text/css",
})
@@ -606,7 +594,7 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
})
}
}
contentOPFPath := filepath.Join(dirPath, "OEBPS/content.opf")
contentOPFPath := filepath.Join(dirPath, "content.opf")
err := os.MkdirAll(path.Dir(contentOPFPath), 0755)
if err != nil {
return fmt.Errorf("failed to create content directory: %v", err)
@@ -622,51 +610,6 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
return nil
}
func CreateTocNCX(dirPath string, uuid string, volume *model.Volume) error {
navMap := &model.NavMap{Points: make([]*model.NavPoint, 0)}
navMap.Points = append(navMap.Points, &model.NavPoint{
Id: "cover",
PlayOrder: 1,
Label: "封面",
Content: model.NavPointContent{Src: "Text/cover.xhtml"},
})
navMap.Points = append(navMap.Points, &model.NavPoint{
Id: "contents",
PlayOrder: 2,
Label: "目录",
Content: model.NavPointContent{Src: "Text/contents.xhtml"},
})
for idx, chapter := range volume.Chapters {
navMap.Points = append(navMap.Points, &model.NavPoint{
Id: fmt.Sprintf("chapter-%03v", idx+1),
PlayOrder: len(navMap.Points) + 1,
Label: chapter.Title,
Content: model.NavPointContent{Src: chapter.TextOEBPSPath},
})
}
head := &model.TocNCXHead{
Meta: []model.TocNCXHeadMeta{
{Name: "dtb:uid", Content: fmt.Sprintf("urn:uuid:%s", uuid)},
},
}
ncxPath := filepath.Join(dirPath, "OEBPS/toc.ncx")
err := os.MkdirAll(path.Dir(ncxPath), 0755)
if err != nil {
return fmt.Errorf("failed to create toc directory: %v", err)
}
file, err := os.Create(ncxPath)
if err != nil {
return fmt.Errorf("failed to create toc file: %v", err)
}
err = template.TocNCX(volume.Title, head, navMap).Render(context.Background(), file)
if err != nil {
return fmt.Errorf("failed to render toc: %v", err)
}
return nil
}
//go:embed read.ttf
var readTTF []byte

View File

@@ -31,7 +31,7 @@ func CreateEpub(path string) error {
return err
}
err = addStringToZip(zipWriter, "OEBPS/Styles/style.css", StyleCSS, zip.Deflate)
err = addStringToZip(zipWriter, "style.css", StyleCSS, zip.Deflate)
if err != nil {
return err
}

View File

@@ -3,11 +3,15 @@ package bilinovel
const StyleCSS = `
@font-face {
font-family: "MI LANTING";
src: url(../Fonts/read.ttf);
src: url(OEBPS/Fonts/read.ttf);
}
.read-font {
font-family: "MI LANTING", serif !important;
display: block;
font-family: "MI LANTING", serif;
font-size: 1.33333em;
text-indent: 2em;
margin: 0.8em 0;
}
body > div {