Skip to content

Commit

Permalink
fix: support end item of key info block, and start support startdict
Browse files Browse the repository at this point in the history
  • Loading branch information
terasum committed Sep 19, 2023
1 parent 5065db9 commit abf638d
Show file tree
Hide file tree
Showing 45 changed files with 1,399 additions and 473 deletions.
11 changes: 4 additions & 7 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ package main

import (
"context"
"github.com/labstack/gommon/log"
"github.com/op/go-logging"
"github.com/terasum/medict/internal/entry"
"github.com/terasum/medict/pkg/backserver"
"github.com/terasum/medict/pkg/model"
)

var log = logging.MustGetLogger("default")

// App struct
type App struct {
ctx context.Context
Expand Down Expand Up @@ -91,12 +93,7 @@ func (b *App) shutdown(ctx context.Context) {
}

func (b *App) Dispatch(apiName string, args map[string]interface{}) *model.Resp {
log.Infof("[wails] IPC request dispatch [%s] args %v", apiName, args)
if args != nil {
for k, v := range args {
log.Infof("[wails] IPC dispatch args [%s]: {%s : %v}", apiName, k, v)
}
}
log.Infof("[wails] IPC request dispatch [%s] | args: %v\n", apiName, args)
return b.bs.DispatchIPCReq(apiName, args)
}

Expand Down
12 changes: 12 additions & 0 deletions frontend/src/apis/dicts-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ export const GetAllDicts = async function (): Promise<Array<IDict>> {
}
}

// BuildIndex
export const BuildIndex = async function (): Promise<model.Resp> {
try{
let resp = await requestBackend("BuildIndex", {})
console.log("[dicts-api] BuildIndex: ", resp)
return resp.data as unknown as model.Resp
} catch (error) {
console.error("[dicts-api] BuildIndex: " ,error)
return Promise.reject(error)
}
}


export const LookupWord = async function (dictid:string, word: string) : Promise<model.Resp> {
try{
Expand Down
87 changes: 41 additions & 46 deletions frontend/src/view/main/AppRightToolbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,30 +72,42 @@
<div class="toolbar-top"></div>
<div class="toolbar-content">
<div class="dictionaries">
<span
class="dictionary-item"


<n-popover
v-for="item in state.dictList"
:key="item.id"
:data-dict-id="item.id"
:data-dict-name="item.name"
:data-background="item.background"
@click="chooseDict(item)"
>{{ item.name }}</span
>
:overlap="false" placement="left" trigger="hover">
<template #trigger>
<span
class="dictionary-item"
:key="item.id"
@click="chooseDict(item)"
:style="getBackground(item)"
></span
>
</template>
<div class="large-text">
<div>{{ item.description && item.description.title ? item.description.title : item.name}} </div>
<div style='max-width: 260px; max-height: 200px; overflow-y: auto'><p v-html='item.description.description'></p> </div>
</div>
</n-popover>


</div>
</div>
</div>
</template>
<script setup>
import { useDictQueryStore } from '@/store/dict';
import { reactive, onMounted } from 'vue';
import { debounce } from '@/utils';
import {GetDictCover} from "@/apis/dicts-api"
import { BuildIndex } from '@/apis/dicts-api';
import {NPopover} from "naive-ui";
const dictQueryStore = useDictQueryStore();
const state = reactive({
dictList: [],
});
Expand All @@ -106,6 +118,18 @@ function chooseDict(item) {
dictQueryStore.updatePendingList([]);
}
function getBackground(item) {
if (item.background) {
let style = `background:url(data:image/jpg;base64,${item.background});`
style += `background-size:cover;`
style += `background-repeat:no-repeat;`
style += `background-position:center;`;
style += `color: #fff;`;
return style
}
return "";
}
function loadDictionaries() {
dictQueryStore.queryDictList().then((res) => {
console.log(res);
Expand All @@ -117,44 +141,15 @@ function loadDictionaries() {
state.dictList.push(res[i]);
}
refreshDict();
setTimeout(() => {
// build-index
BuildIndex().then((resp) => {
console.log('building index success:', resp);
});
}, 1000);
});
}
const refreshDict = debounce(refreshDictBackground);
function refreshDictBackground() {
document.querySelectorAll('.dictionary-item').forEach((item) => {
if (item.dataset.background) {
let sp = item.dataset.background.split('?');
if (sp.length == 2) {
let cover_name = sp[0];
let path_params = sp[1].split("&");
let dict_id = "";
for (let i = 0; i < path_params.length; i++) {
let pair = path_params[i].split('=');
if (pair[0] == 'dict_id') {
dict_id = pair[1];
break
}
}
GetDictCover(dict_id, cover_name).then((res) => {
if (cover_name.endsWith(".jpg")) {
item.style.backgroundImage = `url("data:image/jpeg;base64,${res}")`;
} else {
item.style.backgroundImage = `url("data:image/png;base64,${res}")`;
}
item.style.backgroundPosition = `center`;
item.style.backgroundSize = `cover`;
item.style.backgroundRepeat = `no-repeat`;
item.innerText = ""
})
}
}
});
}
onMounted(() => {
loadDictionaries();
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/c0mm4nd/go-ripemd v0.0.0-20200326052756-bd1759ad7d10
github.com/creasty/go-levenshtein v0.0.0-20161128082938-38ce641d5030
github.com/gin-gonic/gin v1.9.1
github.com/labstack/gommon v0.4.0
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/rasky/go-lzo v0.0.0-20200203143853-96a758eda86e
github.com/spf13/viper v1.10.1
golang.org/x/text v0.9.0
Expand All @@ -36,6 +36,7 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/labstack/echo/v4 v4.10.2 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/leaanthony/go-ansi-parser v1.6.0 // indirect
github.com/leaanthony/gosod v1.0.3 // indirect
github.com/leaanthony/slicer v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/pelletier/go-toml v1.9.4 h1:tjENF6MfZAg8e4ZmZTeWaWiT2vXtsoO6+iuOjFhECwM=
github.com/pelletier/go-toml v1.9.4/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.8 h1:0ctb6s9mE31h0/lhu+J6OPmVeDxJn+kYnJc2jZR9tGQ=
Expand Down
2 changes: 1 addition & 1 deletion internal/entry/config_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
package entry

const configTmpl = `
BaseDictDir="$HOME/.medict/dicts"
DictDir="$HOME/.medict/dicts"
StaticServerPort=59183
`
22 changes: 15 additions & 7 deletions internal/gomdict/mdict.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ package gomdict
import (
"errors"
"fmt"
"github.com/agatan/bktree"
"github.com/creasty/go-levenshtein"
"path/filepath"
"sort"
"strings"

"github.com/agatan/bktree"
"github.com/creasty/go-levenshtein"
"github.com/op/go-logging"
)

var log = logging.MustGetLogger("default")

type Mdict struct {
bktree *bktree.BKTree
*MdictBase
Expand Down Expand Up @@ -53,7 +57,11 @@ func (mdict *Mdict) init() error {
return err
}

err = mdict.ReadKeyBlockInfo()
return nil
}

func (mdict *Mdict) BuildIndex() error {
err := mdict.ReadKeyBlockInfo()
if err != nil {
return err
}
Expand All @@ -77,14 +85,14 @@ func (mdict *Mdict) init() error {
if err != nil {
return err
}

return nil
}

func (mdict *Mdict) Lookup(word string) ([]byte, error) {

for _, keyBlockEntry := range mdict.KeyBlockData.KeyEntries {
if strings.TrimSpace(keyBlockEntry.KeyWord) == strings.TrimSpace(word) {
word = strings.TrimSpace(word)
for id, keyBlockEntry := range mdict.KeyBlockData.KeyEntries {
if keyBlockEntry.KeyWord == word {
log.Infof("mdict.Lookup hitted entries[%d/%d] key:(%s), entry-key:(%s), equals(%v)", id, len(mdict.KeyBlockData.KeyEntries), word, keyBlockEntry.KeyWord, keyBlockEntry.KeyWord == word)
return mdict.LocateRecordDefinition(keyBlockEntry)
}
}
Expand Down
38 changes: 33 additions & 5 deletions internal/gomdict/mdict_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (
"encoding/binary"
"errors"
"fmt"
"github.com/rasky/go-lzo"
"os"
"strconv"
"strings"

"github.com/rasky/go-lzo"
)

// ReadDictHeader reads the dictionary header.
Expand Down Expand Up @@ -102,6 +103,11 @@ func (mdict *MdictBase) ReadDictHeader() error {
// 4 bytes header size + header_bytes_size + 4bytes alder checksum
meta.KeyBlockMetaStartOffset = int64(4 + dictHeader.HeaderBytesSize + 4)

meta.Description = headerInfo.Description
meta.Title = headerInfo.Title
meta.CreationDate = headerInfo.CreationDate
meta.GeneratedByEngineVersion = headerInfo.GeneratedByEngineVersion

mdict.Meta = meta

return nil
Expand Down Expand Up @@ -635,7 +641,7 @@ func (mdict *MdictBase) splitKeyBlock(keyBlock []byte) []*MDictKeyBlockEntry {
keyList[len(keyList)-2].RecordEndOffset = keyList[len(keyList)-1].RecordStartOffset
}
}
//keyList[len(keyList)-1].RecordEndOffset = mdict.RecordBlockMeta.RecordBlockCompSize
//keyList[len(keyList)-1].RecordEndOffset = 0

return keyList
}
Expand Down Expand Up @@ -761,8 +767,9 @@ func (mdict *MdictBase) decodeRecordBlockInfo(data []byte, startOffset, endOffse
var offset = 0
var compAccu = int64(0)
var decompAccu = int64(0)
var i = int64(0)

for i := int64(0); i < mdict.RecordBlockMeta.RecordBlockNum; i++ {
for i = int64(0); i < mdict.RecordBlockMeta.RecordBlockNum; i++ {
compSize := int64(0)
if mdict.Meta.Version >= 2.0 {
compSize = int64(beBinToU64(data[offset : offset+mdict.Meta.NumberWidth]))
Expand Down Expand Up @@ -791,12 +798,16 @@ func (mdict *MdictBase) decodeRecordBlockInfo(data []byte, startOffset, endOffse
compAccu += compSize
decompAccu += decompSize
}
if int64(i) != mdict.RecordBlockMeta.RecordBlockNum {
return fmt.Errorf("RecordBlockInfo (i) not equals to meta.RecordBlockNum [%d/%d] compA/decompA(%d/%d)", i, mdict.RecordBlockMeta.RecordBlockNum, compAccu, decompAccu)
}
if int64(offset) != mdict.RecordBlockMeta.RecordBlockInfoCompSize {
return errors.New("RecordBlockInfo offset not equals to meta.RecordBlockInfoCompSize")
}
if int64(compAccu) != mdict.RecordBlockMeta.RecordBlockCompSize {
return errors.New("RecordBlockInfo compAccu not equals to meta.RecordBlockCompSize")
}

recordBlockInfo := &MDictRecordBlockInfo{
RecordInfoList: recordBlockInfoList,
RecordBlockInfoStartOffset: startOffset,
Expand Down Expand Up @@ -986,17 +997,26 @@ func (mdict *MdictBase) LocateRecordDefinition(item *MDictKeyBlockEntry) ([]byte

var recordBlockInfo *MDictRecordBlockInfoListItem

for i := 0; i < len(mdict.RecordBlockInfo.RecordInfoList)-1; i++ {
var i = 0
for ; i < len(mdict.RecordBlockInfo.RecordInfoList)-1; i++ {
curr := mdict.RecordBlockInfo.RecordInfoList[i]
next := mdict.RecordBlockInfo.RecordInfoList[i+1]
if item.RecordStartOffset >= curr.DeCompressAccumulatorOffset && item.RecordStartOffset < next.DeCompressAccumulatorOffset {
recordBlockInfo = curr
break
}
}

// the last one
if i == len(mdict.RecordBlockInfo.RecordInfoList)-1 {
lastOne := mdict.RecordBlockInfo.RecordInfoList[len(mdict.RecordBlockInfo.RecordInfoList)-1]
if item.RecordStartOffset < lastOne.DeCompressAccumulatorOffset+lastOne.DeCompressSize {
recordBlockInfo = lastOne
}
}

if recordBlockInfo == nil {
fmt.Printf("record block info is nil, current keyBlockEntry: %+v, last RecordBlockInfo: %+v\n", item, mdict.RecordBlockInfo.RecordInfoList[len(mdict.RecordBlockInfo.RecordInfoList)-1])
return nil, errors.New("key-item record info not found")
}

Expand Down Expand Up @@ -1064,7 +1084,15 @@ func (mdict *MdictBase) LocateRecordDefinition(item *MDictKeyBlockEntry) ([]byte
return nil, errors.New("recordBlock length not equals decompress Size")
}

data := recordBlock[item.RecordStartOffset-recordBlockInfo.DeCompressAccumulatorOffset : item.RecordEndOffset-recordBlockInfo.DeCompressAccumulatorOffset]
start := item.RecordStartOffset - recordBlockInfo.DeCompressAccumulatorOffset
var end int64
if item.RecordEndOffset == 0 {
end = int64(len(recordBlock))
} else {
end = item.RecordEndOffset - recordBlockInfo.DeCompressAccumulatorOffset
}

data := recordBlock[start:end]

if mdict.FileType == MdictTypeMdd {
return data, nil
Expand Down
5 changes: 5 additions & 0 deletions internal/gomdict/mdict_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ type MDictMeta struct {

// key-block part bytes start offset in the mdx/mdd file
KeyBlockMetaStartOffset int64

Description string
Title string
CreationDate string
GeneratedByEngineVersion string
}

type MDictKeyBlockMeta struct {
Expand Down
Loading

0 comments on commit abf638d

Please sign in to comment.