mirror of
https://github.com/gedoor/legado.git
synced 2024-07-02 23:17:32 +08:00
Compare commits
9 Commits
7ae2070c90
...
c3f4b9e7ab
Author | SHA1 | Date | |
---|---|---|---|
|
c3f4b9e7ab | ||
|
85b1e7b548 | ||
|
d7ae1d4f0a | ||
|
c907ed51b7 | ||
|
576a370da1 | ||
|
d6c2b5eceb | ||
|
94aaee8d9b | ||
|
96a3b6b802 | ||
|
f3d055c9bb |
|
@ -57,6 +57,7 @@ import java.io.File
|
|||
import java.io.FileOutputStream
|
||||
import java.net.URLEncoder
|
||||
import java.nio.charset.Charset
|
||||
import java.security.MessageDigest
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
@ -748,45 +749,68 @@ interface JsExtensions : JsEncodeUtils {
|
|||
//******************文件操作************************//
|
||||
|
||||
/**
|
||||
* 解析字体,返回字体解析类
|
||||
* 解析字体Base64数据,返回字体解析类
|
||||
*/
|
||||
fun queryBase64TTF(base64: String?): QueryTTF? {
|
||||
base64DecodeToByteArray(base64)?.let {
|
||||
return QueryTTF(it)
|
||||
}
|
||||
return null
|
||||
fun queryBase64TTF(data: String?): QueryTTF? {
|
||||
log("queryBase64TTF(String)方法已过时,并将在未来删除;请无脑使用queryTTF(Any)替代,新方法支持传入 url、本地文件、base64、ByteArray 自动判断&自动缓存,特殊情况需禁用缓存请传入第二可选参数false:Boolean")
|
||||
return queryTTF(data)
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回字体解析类
|
||||
* @param str 支持url,本地文件,base64,自动判断,自动缓存
|
||||
* @param data 支持url,本地文件,base64,ByteArray,自动判断,自动缓存
|
||||
* @param useCache 可选开关缓存,不传入该值默认开启缓存
|
||||
*/
|
||||
fun queryTTF(str: String?): QueryTTF? {
|
||||
str ?: return null
|
||||
@OptIn(ExperimentalStdlibApi::class)
|
||||
fun queryTTF(data: Any?, useCache: Boolean): QueryTTF? {
|
||||
try {
|
||||
val key = md5Encode16(str)
|
||||
var qTTF = CacheManager.getQueryTTF(key)
|
||||
var key: String? = null
|
||||
var qTTF: QueryTTF?
|
||||
when (data) {
|
||||
is String -> {
|
||||
if (useCache) {
|
||||
key = MessageDigest.getInstance("SHA-256").digest(data.toByteArray()).toHexString()
|
||||
qTTF = CacheManager.getQueryTTF(key)
|
||||
if (qTTF != null) return qTTF
|
||||
}
|
||||
val font: ByteArray? = when {
|
||||
str.isAbsUrl() -> AnalyzeUrl(str, source = getSource()).getByteArray()
|
||||
str.isContentScheme() -> Uri.parse(str).readBytes(appCtx)
|
||||
str.startsWith("/storage") -> File(str).readBytes()
|
||||
else -> base64DecodeToByteArray(str)
|
||||
data.isContentScheme() -> Uri.parse(data).readBytes(appCtx)
|
||||
data.startsWith("/storage") -> File(data).readBytes()
|
||||
data.isAbsUrl() -> AnalyzeUrl(data, source = getSource()).getByteArray()
|
||||
else -> base64DecodeToByteArray(data)
|
||||
}
|
||||
font ?: return null
|
||||
qTTF = QueryTTF(font)
|
||||
CacheManager.put(key, qTTF) // debug注释掉
|
||||
}
|
||||
|
||||
is ByteArray -> {
|
||||
if (useCache) {
|
||||
key = MessageDigest.getInstance("SHA-256").digest(data).toHexString()
|
||||
qTTF = CacheManager.getQueryTTF(key)
|
||||
if (qTTF != null) return qTTF
|
||||
}
|
||||
qTTF = QueryTTF(data)
|
||||
}
|
||||
|
||||
else -> return null
|
||||
}
|
||||
if (key != null) CacheManager.put(key, qTTF)
|
||||
return qTTF
|
||||
} catch (e: Exception) {
|
||||
AppLog.put("获取字体处理类出错", e)
|
||||
AppLog.put("[queryTTF] 获取字体处理类出错", e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
fun queryTTF(data: Any?): QueryTTF? {
|
||||
return queryTTF(data, true)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param text 包含错误字体的内容
|
||||
* @param errorQueryTTF 错误的字体
|
||||
* @param correctQueryTTF 正确的字体
|
||||
* @param filter 删除 errorQueryTTF 中不存在的字符
|
||||
*/
|
||||
fun replaceFont(
|
||||
text: String,
|
||||
|
@ -802,8 +826,9 @@ interface JsExtensions : JsEncodeUtils {
|
|||
if (errorQueryTTF.isBlankUnicode(oldCode)) {
|
||||
return@forEachIndexed
|
||||
}
|
||||
val glyf = errorQueryTTF.getGlyfByUnicode(oldCode)
|
||||
// 删除轮廓数据不存在的字符
|
||||
var glyf = errorQueryTTF.getGlyfByUnicode(oldCode) // 轮廓数据不存在
|
||||
if (errorQueryTTF.getGlyfIdByUnicode(oldCode) == 0) glyf = null // 轮廓数据指向保留索引0
|
||||
if (filter && (glyf == null)) {
|
||||
contentArray[index] = ""
|
||||
return@forEachIndexed
|
||||
|
@ -817,6 +842,11 @@ interface JsExtensions : JsEncodeUtils {
|
|||
return contentArray.joinToString("")
|
||||
}
|
||||
|
||||
/**
|
||||
* @param text 包含错误字体的内容
|
||||
* @param errorQueryTTF 错误的字体
|
||||
* @param correctQueryTTF 正确的字体
|
||||
*/
|
||||
fun replaceFont(
|
||||
text: String,
|
||||
errorQueryTTF: QueryTTF?,
|
||||
|
|
|
@ -685,6 +685,7 @@ public class QueryTTF {
|
|||
int unicodeInclusive = 0;
|
||||
int unicodeExclusive = f.glyphIdArray.length;
|
||||
for (; unicodeInclusive < unicodeExclusive; unicodeInclusive++) {
|
||||
if (f.glyphIdArray[unicodeInclusive] == 0) continue; // 排除轮廓索引为0的Unicode
|
||||
unicodeToGlyphId.put(unicodeInclusive, f.glyphIdArray[unicodeInclusive]);
|
||||
}
|
||||
break;
|
||||
|
@ -711,12 +712,15 @@ public class QueryTTF {
|
|||
int idDelta = f.idDelta[segmentIndex];
|
||||
int idRangeOffset = f.idRangeOffsets[segmentIndex];
|
||||
for (int unicode = unicodeInclusive; unicode <= unicodeExclusive; unicode++) {
|
||||
int glyphId = 0;
|
||||
if (idRangeOffset == 0) {
|
||||
unicodeToGlyphId.put(unicode, (unicode + idDelta) & 0xFFFF);
|
||||
glyphId = (unicode + idDelta) & 0xFFFF;
|
||||
} else {
|
||||
int gIndex = (idRangeOffset / 2) + unicode - unicodeInclusive + segmentIndex - segCount;
|
||||
unicodeToGlyphId.put(unicode, gIndex < glyphIdArrayLength ? f.glyphIdArray[gIndex] + idDelta : 0);
|
||||
if (gIndex < glyphIdArrayLength) glyphId = f.glyphIdArray[gIndex] + idDelta;
|
||||
}
|
||||
if (glyphId == 0) continue; // 排除轮廓索引为0的Unicode
|
||||
unicodeToGlyphId.put(unicode, glyphId);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -773,12 +777,12 @@ public class QueryTTF {
|
|||
var dataTable = directorys.get("glyf");
|
||||
assert dataTable != null;
|
||||
int glyfCount = maxp.numGlyphs;
|
||||
glyfArray = new GlyfLayout[glyfCount + 1]; // 创建容器时,多创建一个作为保留区
|
||||
glyfArray = new GlyfLayout[glyfCount]; // 创建字形容器
|
||||
|
||||
var reader = new BufferReader(buffer, 0);
|
||||
for (int index = 1; index <= glyfCount; index++) {
|
||||
if (loca[index - 1] == loca[index]) continue; // 当前loca与下一个loca相同,表示这个字形不存在
|
||||
int offset = dataTable.offset + loca[index - 1];
|
||||
for (int index = 0; index < glyfCount; index++) {
|
||||
if (loca[index] == loca[index + 1]) continue; // 当前loca与下一个loca相同,表示这个字形不存在
|
||||
int offset = dataTable.offset + loca[index];
|
||||
// 读GlyphHeaders
|
||||
var glyph = new GlyfLayout();
|
||||
reader.position(offset);
|
||||
|
@ -893,7 +897,7 @@ public class QueryTTF {
|
|||
if ((glyphTableComponent.flags & 0x20) == 0) break;
|
||||
}
|
||||
}
|
||||
glyfArray[index] = glyph; // 根据文档 glyfId=0 作为保留区使用,这里赋值从索引1开始
|
||||
glyfArray[index] = glyph;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -919,7 +923,15 @@ public class QueryTTF {
|
|||
// 复合字形
|
||||
LinkedList<String> glyphIdList = new LinkedList<>();
|
||||
for (var g : glyph.glyphComponent) {
|
||||
glyphIdList.add(String.valueOf(g.glyphIndex));
|
||||
glyphIdList.add("{" +
|
||||
"flags:" + g.flags + "," +
|
||||
"glyphIndex:" + g.glyphIndex + "," +
|
||||
"arg1:" + g.argument1 + "," +
|
||||
"arg2:" + g.argument2 + "," +
|
||||
"xScale:" + g.xScale + "," +
|
||||
"scale01:" + g.scale01 + "," +
|
||||
"scale10:" + g.scale10 + "," +
|
||||
"yScale:" + g.yScale + "}");
|
||||
}
|
||||
glyphString = "[" + String.join(",", glyphIdList) + "]";
|
||||
}
|
||||
|
@ -965,10 +977,11 @@ public class QueryTTF {
|
|||
int glyfArrayLength = glyfArray.length;
|
||||
for (var item : unicodeToGlyphId.entrySet()) {
|
||||
int key = item.getKey();
|
||||
int val = item.getValue() + 1; // glyphArray已根据TTF文档将索引0作为保留位,这里从1开始索引
|
||||
int val = item.getValue();
|
||||
if (val >= glyfArrayLength) continue;
|
||||
String glyfString = getGlyfById(val);
|
||||
unicodeToGlyph.put(key, glyfString);
|
||||
if (glyfString == null) continue; // null 不能用作hashmap的key
|
||||
glyphToUnicode.put(glyfString, key);
|
||||
}
|
||||
// Log.i("QueryTTF", "字体处理完成");
|
||||
|
@ -986,8 +999,8 @@ public class QueryTTF {
|
|||
*/
|
||||
public int getGlyfIdByUnicode(int unicode) {
|
||||
var result = unicodeToGlyphId.get(unicode);
|
||||
if (result == null) return 0;
|
||||
return result + 1; // 根据TTF文档,轮廓索引的定义从1开始
|
||||
if (result == null) return 0; // 如果找不到Unicode对应的轮廓索引,就返回默认值0
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1008,7 +1021,7 @@ public class QueryTTF {
|
|||
*/
|
||||
public int getUnicodeByGlyf(String glyph) {
|
||||
var result = glyphToUnicode.get(glyph);
|
||||
if (result == null) return 0;
|
||||
if (result == null) return 0; // 如果轮廓数据找不到对应的Unicode,就返回默认值0
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
VITE_API=http://192.168.10.11:1122
|
|
@ -1 +0,0 @@
|
|||
VITE_API=
|
|
@ -12,9 +12,8 @@
|
|||
| Edge ≥ 79 | Firefox ≥ 78 | Chrome ≥ 64 | Safari ≥ 12 |
|
||||
|
||||
## 开发
|
||||
> 需要阅读app提供后端服务,开发前修改环境变量`VITE_API`为阅读web服务地址
|
||||
> 需要阅读app提供后端服务
|
||||
|
||||
```bash
|
||||
echo "VITE_API=http://<ip>:<port>" > .env.development
|
||||
pnpm dev
|
||||
```
|
||||
|
|
|
@ -1,10 +1,25 @@
|
|||
import axios from "axios";
|
||||
|
||||
const SECOND = 1000;
|
||||
const remoteIp = ref(localStorage.getItem("remoteIp"));
|
||||
|
||||
const ajax = axios.create({
|
||||
baseURL: import.meta.env.VITE_API || location.origin,
|
||||
// baseURL: import.meta.env.VITE_API || location.origin,
|
||||
timeout: 120 * SECOND,
|
||||
});
|
||||
|
||||
ajax.interceptors.request.use((config) => {
|
||||
config.baseURL = remoteIp.value;
|
||||
return config;
|
||||
});
|
||||
|
||||
export default ajax;
|
||||
|
||||
export const setRemoteIp = (ip) => {
|
||||
remoteIp.value = ip;
|
||||
localStorage.setItem("remoteIp", ip);
|
||||
};
|
||||
|
||||
export const baseUrl = () => {
|
||||
return remoteIp.value;
|
||||
};
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
import ajax from "./axios";
|
||||
import ajax, { baseUrl } from "./axios";
|
||||
import { ElMessage } from "element-plus/es";
|
||||
|
||||
/** https://github.com/gedoor/legado/tree/master/app/src/main/java/io/legado/app/api */
|
||||
/** https://github.com/gedoor/legado/tree/master/app/src/main/java/io/legado/app/web */
|
||||
|
||||
const { hostname, port } = new URL(import.meta.env.VITE_API || location.origin);
|
||||
const getUrl = () => {
|
||||
const { hostname, port } = new URL(baseUrl());
|
||||
return `${hostname}:${Number(port) + 1}`;
|
||||
};
|
||||
|
||||
const isSourecEditor = /source/i.test(location.href);
|
||||
const APIExceptionHandler = (error) => {
|
||||
|
@ -29,7 +32,7 @@ const saveBookProgressWithBeacon = (bookProgress) => {
|
|||
if (!bookProgress) return;
|
||||
// 常规请求可能会被取消 使用Fetch keep-alive 或者 navigator.sendBeacon
|
||||
navigator.sendBeacon(
|
||||
`${import.meta.env.VITE_API || location.origin}/saveBookProgress`,
|
||||
`${baseUrl()}/saveBookProgress`,
|
||||
JSON.stringify(bookProgress),
|
||||
);
|
||||
};
|
||||
|
@ -56,7 +59,7 @@ const search = (
|
|||
/** @type {() => void} */ onFinish,
|
||||
) => {
|
||||
// webSocket
|
||||
const url = `ws://${hostname}:${Number(port) + 1}/searchBook`;
|
||||
const url = `ws://${getUrl()}/searchBook`;
|
||||
const socket = new WebSocket(url);
|
||||
|
||||
socket.onopen = () => {
|
||||
|
@ -100,7 +103,7 @@ const debug = (
|
|||
/** @type {() => void} */ onFinish,
|
||||
) => {
|
||||
// webSocket
|
||||
const url = `ws://${hostname}:${Number(port) + 1}/${
|
||||
const url = `ws://${getUrl()}/${
|
||||
isBookSource ? "bookSource" : "rssSource"
|
||||
}Debug`;
|
||||
|
||||
|
|
3
modules/web/src/auto-imports.d.ts
vendored
3
modules/web/src/auto-imports.d.ts
vendored
|
@ -82,5 +82,6 @@ declare global {
|
|||
// for type re-export
|
||||
declare global {
|
||||
// @ts-ignore
|
||||
export type { Component, ComponentPublicInstance, ComputedRef, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue'
|
||||
export type { Component, ComponentPublicInstance, ComputedRef, ExtractDefaultPropTypes, ExtractPropTypes, ExtractPublicPropTypes, InjectionKey, PropType, Ref, VNode, WritableComputedRef } from 'vue'
|
||||
import('vue')
|
||||
}
|
||||
|
|
|
@ -50,15 +50,14 @@
|
|||
</template>
|
||||
<script setup>
|
||||
import { dateFormat } from "../utils/utils";
|
||||
import { baseUrl } from "@/api/axios.js";
|
||||
const props = defineProps(["books", "isSearch"]);
|
||||
const emit = defineEmits(["bookClick"]);
|
||||
const handleClick = (book) => emit("bookClick", book);
|
||||
const getCover = (coverUrl) => {
|
||||
return /^data:/.test(coverUrl)
|
||||
? coverUrl
|
||||
: (import.meta.env.VITE_API || location.origin) +
|
||||
"/cover?path=" +
|
||||
encodeURIComponent(coverUrl);
|
||||
: baseUrl() + "/cover?path=" + encodeURIComponent(coverUrl);
|
||||
};
|
||||
|
||||
const subJustify = computed(() =>
|
||||
|
|
|
@ -57,12 +57,20 @@ export const useBookStore = defineStore("book", {
|
|||
setConnectType(connectType) {
|
||||
this.connectType = connectType;
|
||||
},
|
||||
resetConnect() {
|
||||
this.connectStatus = "正在连接后端服务器……";
|
||||
this.connectType = "";
|
||||
this.clearBooks();
|
||||
},
|
||||
setNewConnect(newConnect) {
|
||||
this.newConnect = newConnect;
|
||||
},
|
||||
addBooks(books) {
|
||||
this.shelf = books;
|
||||
},
|
||||
clearBooks() {
|
||||
this.shelf = [];
|
||||
},
|
||||
setCatalog(catalog) {
|
||||
this.catalog = catalog;
|
||||
},
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import { formatDate } from "@vueuse/shared";
|
||||
import { baseUrl } from "@/api/axios.js";
|
||||
|
||||
export const isLegadoUrl = (/** @type {string} */ url) =>
|
||||
/,\s*\{/.test(url) ||
|
||||
|
@ -12,7 +13,7 @@ export const isLegadoUrl = (/** @type {string} */ url) =>
|
|||
*/
|
||||
export function getImageFromLegado(src) {
|
||||
return (
|
||||
(import.meta.env.VITE_API || location.origin) +
|
||||
baseUrl() +
|
||||
"/image?path=" +
|
||||
encodeURIComponent(src) +
|
||||
"&url=" +
|
||||
|
|
|
@ -40,14 +40,24 @@
|
|||
</div>
|
||||
<div class="setting-wrapper">
|
||||
<div class="setting-title">基本设定</div>
|
||||
<div class="setting-item">
|
||||
<div class="setting-ip">
|
||||
<el-input
|
||||
class="setting-input"
|
||||
size="small"
|
||||
:disabled="ipInput.disable"
|
||||
v-model="ipInput.ip"
|
||||
@keydown.enter="setIP"
|
||||
/>
|
||||
<el-tag
|
||||
:type="connectType"
|
||||
size="large"
|
||||
class="setting-connect"
|
||||
:class="{ 'no-point': newConnect }"
|
||||
@click="setIP"
|
||||
type="primary"
|
||||
class="setting-toggle"
|
||||
@click="toggleIpConfig"
|
||||
>
|
||||
{{ ipInput.disable ? "修改" : "取消" }}
|
||||
</el-tag>
|
||||
</div>
|
||||
<div class="setting-item">
|
||||
<el-tag :type="connectType" size="large" class="setting-connect">
|
||||
{{ connectStatus }}
|
||||
</el-tag>
|
||||
</div>
|
||||
|
@ -81,6 +91,7 @@ import githubUrl from "@/assets/imgs/github.png";
|
|||
import { useLoading } from "@/hooks/loading";
|
||||
import { Search } from "@element-plus/icons-vue";
|
||||
import API from "@api";
|
||||
import { baseUrl, setRemoteIp } from "@/api/axios.js";
|
||||
|
||||
const store = useBookStore();
|
||||
const { connectStatus, connectType, newConnect, shelf } = storeToRefs(store);
|
||||
|
@ -136,7 +147,7 @@ const searchBook = () => {
|
|||
}
|
||||
try {
|
||||
store.setSearchBooks(JSON.parse(data));
|
||||
books.value = store.searchBooks
|
||||
books.value = store.searchBooks;
|
||||
//store.searchBooks.forEach((item) => books.value.push(item));
|
||||
} catch (e) {
|
||||
ElMessage.error("后端数据错误");
|
||||
|
@ -152,7 +163,19 @@ const searchBook = () => {
|
|||
);
|
||||
};
|
||||
|
||||
const setIP = () => {};
|
||||
const ipInput = reactive({
|
||||
ip: baseUrl(),
|
||||
disable: true,
|
||||
});
|
||||
const toggleIpConfig = () => {
|
||||
ipInput.ip = baseUrl();
|
||||
ipInput.disable = !ipInput.disable;
|
||||
};
|
||||
const setIP = () => {
|
||||
setRemoteIp(ipInput.ip);
|
||||
ipInput.disable = true;
|
||||
loadShelf();
|
||||
};
|
||||
|
||||
const router = useRouter();
|
||||
const handleBookClick = async (book) => {
|
||||
|
@ -195,13 +218,19 @@ onMounted(() => {
|
|||
readingRecent.value.chapterIndex = 0;
|
||||
}
|
||||
}
|
||||
loadShelf();
|
||||
});
|
||||
|
||||
const loadShelf = () => {
|
||||
store.resetConnect();
|
||||
loadingWrapper(
|
||||
store
|
||||
.saveBookProgress()
|
||||
//确保各种网络情况下同步请求先完成
|
||||
.finally(fetchBookShelfData),
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const fetchBookShelfData = () => {
|
||||
return API.getBookShelf()
|
||||
.then((response) => {
|
||||
|
@ -308,15 +337,31 @@ const fetchBookShelfData = () => {
|
|||
font-family: FZZCYSK;
|
||||
}
|
||||
|
||||
.setting-ip {
|
||||
margin-top: 16px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.setting-input {
|
||||
width: 216px;
|
||||
margin-right: 4px;
|
||||
}
|
||||
|
||||
.no-point {
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
.setting-connect {
|
||||
font-size: 8px;
|
||||
margin-top: 16px;
|
||||
// color: #6B7C87;
|
||||
.setting-toggle {
|
||||
font-size: 10px;
|
||||
cursor: pointer;
|
||||
//margin-top: 4px;
|
||||
}
|
||||
|
||||
.setting-connect {
|
||||
font-size: 10px;
|
||||
margin-top: 4px;
|
||||
// color: #6B7C87;
|
||||
//cursor: pointer;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user