Merge pull request #486 from chrox/master

fix cannot highlight hyphenated words in crereader
pull/487/head
Paulo Matias 11 years ago
commit e9cc47133f

@ -135,9 +135,10 @@ end
function CreDocument:getWordFromPosition(pos)
local word_box = self._document:getWordFromPosition(pos.x, pos.y)
local text_range = self._document:getTextFromPositions(pos.x, pos.y, pos.x, pos.y)
if word_box.word then
return {
word = word_box.word,
word = text_range.text == "" and word_box.word or text_range.text,
page = self._document:getCurrentPage(),
sbox = Geom:new{
x = word_box.x0, y = word_box.y0,
@ -156,7 +157,7 @@ function CreDocument:getTextFromPositions(pos0, pos1)
text = text_range.text,
pos0 = text_range.pos0,
pos1 = text_range.pos1,
sboxes = line_boxes, -- boxes on screen
--sboxes = line_boxes, -- boxes on screen
}
end
@ -273,6 +274,10 @@ function CreDocument:setFontFace(new_font_face)
end
end
function CreDocument:clearSelection()
self._document:clearSelection()
end
function CreDocument:getFontSize()
return self._document:getFontSize()
end

@ -2,7 +2,6 @@ local Geom = require("ui/geometry")
local Cache = require("cache")
local CacheItem = require("cacheitem")
local KoptOptions = require("ui/data/koptoptions")
local KoptInterface = require("document/koptinterface")
local Document = require("document/document")
local Configurable = require("ui/reader/configurable")
local DrawContext = require("ffi/drawcontext")
@ -13,7 +12,7 @@ local DjvuDocument = Document:new{
djvulibre_cache_size = nil,
dc_null = DrawContext.new(),
options = KoptOptions,
koptinterface = KoptInterface,
koptinterface = nil,
}
-- check DjVu magic string to validate
@ -27,7 +26,8 @@ local function validDjvuFile(filename)
end
function DjvuDocument:init()
require "libs/libkoreader-djvu"
local djvu = require("libs/libkoreader-djvu")
self.koptinterface = require("document/koptinterface")
self.configurable:loadDefaults(self.options)
if not validDjvuFile(self.file) then
self.error_message = "Not a valid DjVu file"

@ -1,7 +1,6 @@
local Cache = require("cache")
local CacheItem = require("cacheitem")
local KoptOptions = require("ui/data/koptoptions")
local KoptInterface = require("document/koptinterface")
local Document = require("document/document")
local Configurable = require("ui/reader/configurable")
local DrawContext = require("ffi/drawcontext")
@ -12,11 +11,12 @@ local PdfDocument = Document:new{
mupdf_cache_size = 5 * 1024 * 1024,
dc_null = DrawContext.new(),
options = KoptOptions,
koptinterface = KoptInterface,
koptinterface = nil,
}
function PdfDocument:init()
require "libs/libkoreader-pdf"
local pdf = require("libs/libkoreader-pdf")
self.koptinterface = require("document/koptinterface")
self.configurable:loadDefaults(self.options)
local ok
ok, self._document = pcall(pdf.openDocument, self.file, self.mupdf_cache_size)

@ -159,9 +159,13 @@ end
function ReaderHighlight:onTap(arg, ges)
if self.hold_pos then
self.view.highlight.temp[self.hold_pos.page] = nil
UIManager:setDirty(self.dialog, "partial")
if self.ui.document.info.has_pages then
self.view.highlight.temp[self.hold_pos.page] = nil
else
self.ui.document:clearSelection()
end
self.hold_pos = nil
UIManager:setDirty(self.dialog, "partial")
return true
end
if self.ui.document.info.has_pages then
@ -260,9 +264,11 @@ function ReaderHighlight:onHold(arg, ges)
self.selected_word = self.ui.document:getWordFromPosition(self.hold_pos)
DEBUG("selected word:", self.selected_word)
if self.selected_word then
local boxes = {}
table.insert(boxes, self.selected_word.sbox)
self.view.highlight.temp[self.hold_pos.page] = boxes
if self.ui.document.info.has_pages then
local boxes = {}
table.insert(boxes, self.selected_word.sbox)
self.view.highlight.temp[self.hold_pos.page] = boxes
end
UIManager:setDirty(self.dialog, "partial")
end
return true

@ -61,22 +61,22 @@ local function utf8Chars(input)
return read_next_glyph, input, 1
end
function RenderText:getGlyph(face, charcode, bgcolor, fgcolor)
function RenderText:getGlyph(face, charcode, bold, bgcolor, fgcolor)
if bgcolor == nil then bgcolor = 0.0 end
if fgcolor == nil then fgcolor = 1.0 end
local hash = "glyph|"..face.hash.."|"..charcode.."|"..bgcolor.."|"..fgcolor
local hash = "glyph|"..face.hash.."|"..charcode.."|"..(bold and 1 or 0).."|"..bgcolor.."|"..fgcolor
local glyph = GlyphCache:check(hash)
if glyph then
-- cache hit
return glyph[1]
end
local rendered_glyph = face.ftface:renderGlyph(charcode, bgcolor, fgcolor)
local rendered_glyph = face.ftface:renderGlyph(charcode, bgcolor, fgcolor, bold)
if face.ftface:checkGlyph(charcode) == 0 then
for index, font in pairs(Font.fallbacks) do
-- rescale face size by DPI since it will be scaled in getFace again
local fb_face = Font:getFace(font, Screen:rescaleByDPI(face.size))
if fb_face.ftface:checkGlyph(charcode) ~= 0 then
rendered_glyph = fb_face.ftface:renderGlyph(charcode, bgcolor, fgcolor)
rendered_glyph = fb_face.ftface:renderGlyph(charcode, bgcolor, fgcolor, bold)
--DEBUG("fallback to font", font)
break
end
@ -92,13 +92,13 @@ function RenderText:getGlyph(face, charcode, bgcolor, fgcolor)
return rendered_glyph
end
function RenderText:getSubTextByWidth(text, face, width, kerning)
function RenderText:getSubTextByWidth(text, face, width, kerning, bold)
local pen_x = 0
local prevcharcode = 0
local char_list = {}
for _, charcode, uchar in utf8Chars(text) do
if pen_x < width then
local glyph = self:getGlyph(face, charcode)
local glyph = self:getGlyph(face, charcode, bold)
if kerning and prevcharcode then
local kern = face.ftface:getKerning(prevcharcode, charcode)
pen_x = pen_x + kern
@ -115,7 +115,7 @@ function RenderText:getSubTextByWidth(text, face, width, kerning)
return table.concat(char_list)
end
function RenderText:sizeUtf8Text(x, width, face, text, kerning)
function RenderText:sizeUtf8Text(x, width, face, text, kerning, bold)
if not text then
DEBUG("sizeUtf8Text called without text");
return
@ -129,7 +129,7 @@ function RenderText:sizeUtf8Text(x, width, face, text, kerning)
local prevcharcode = 0
for _, charcode, uchar in utf8Chars(text) do
if pen_x < (width - x) then
local glyph = self:getGlyph(face, charcode)
local glyph = self:getGlyph(face, charcode, bold)
if kerning and (prevcharcode ~= 0) then
pen_x = pen_x + (face.ftface):getKerning(prevcharcode, charcode)
end
@ -143,7 +143,7 @@ function RenderText:sizeUtf8Text(x, width, face, text, kerning)
return { x = pen_x, y_top = pen_y_top, y_bottom = pen_y_bottom}
end
function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bgcolor, fgcolor, width)
function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bold, bgcolor, fgcolor, width)
if not text then
DEBUG("renderUtf8Text called without text");
return 0
@ -159,7 +159,7 @@ function RenderText:renderUtf8Text(buffer, x, y, face, text, kerning, bgcolor, f
end
for _, charcode, uchar in utf8Chars(text) do
if pen_x < text_width then
local glyph = self:getGlyph(face, charcode, bgcolor, fgcolor)
local glyph = self:getGlyph(face, charcode, bold, bgcolor, fgcolor)
if kerning and (prevcharcode ~= 0) then
pen_x = pen_x + face.ftface:getKerning(prevcharcode, charcode)
end

@ -34,6 +34,7 @@ function Button:init()
text = self.text,
bgcolor = 0.0,
fgcolor = self.enabled and 1.0 or 0.5,
bold = true,
face = Font:getFace(self.text_font_face, self.text_font_size)
}
else

@ -85,6 +85,7 @@ function DictQuickLookup:update()
TextWidget:new{
text = self.dictionary,
face = self.title_face,
bold = true,
width = self.width - self.button_padding,
}
}
@ -96,6 +97,7 @@ function DictQuickLookup:update()
TextBoxWidget:new{
text = self.lookupword,
face = self.word_face,
bold = true,
width = self.width,
},
}

@ -9,7 +9,7 @@ FixedTextWidget
local FixedTextWidget = TextWidget:new{}
function FixedTextWidget:getSize()
local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true)
local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true, self.bold)
if not tsize then
return Geom:new{}
end
@ -22,8 +22,8 @@ function FixedTextWidget:getSize()
end
function FixedTextWidget:paintTo(bb, x, y)
RenderText:renderUtf8Text(bb, x, y+self._height, self.face, self.text,
true, self.bgcolor, self.fgcolor)
RenderText:renderUtf8Text(bb, x, y+self._height, self.face, self.text, true, self.bold,
self.bgcolor, self.fgcolor)
end
return FixedTextWidget

@ -11,6 +11,7 @@ A TextWidget that handles long text wrapping
local TextBoxWidget = Widget:new{
text = nil,
face = nil,
bold = nil,
bgcolor = 0.0, -- [0.0, 1.0]
fgcolor = 1.0, -- [0.0, 1.0]
width = 400, -- in pixels
@ -115,7 +116,7 @@ function TextBoxWidget:_getVerticalList(alg)
for w in word:gsplit("%p+", true) do
local word_box = {}
word_box.word = w
word_box.width = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, w, true).x
word_box.width = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, w, true, self.bold).x
table.insert(h_list, word_box)
end
end
@ -215,8 +216,7 @@ function TextBoxWidget:_render(v_list)
for _,w in ipairs(l) do
--@TODO Don't use kerning for monospaced fonts. (houqp)
-- refert to cb25029dddc42693cc7aaefbe47e9bd3b7e1a750 in master tree
RenderText:renderUtf8Text(self._bb, pen_x, y, self.face, w.word, true,
self.bgcolor, self.fgcolor)
RenderText:renderUtf8Text(self._bb, pen_x, y, self.face, w.word, true, self.bold, self.bgcolor, self.fgcolor)
pen_x = pen_x + w.width
end
y = y + line_height_px + font_height

@ -9,6 +9,7 @@ A TextWidget puts a string on a single line
local TextWidget = Widget:new{
text = nil,
face = nil,
bold = nil,
bgcolor = 0.0, -- [0.0, 1.0]
fgcolor = 1.0, -- [0.0, 1.0]
_bb = nil,
@ -20,7 +21,7 @@ local TextWidget = Widget:new{
--function TextWidget:_render()
--local h = self.face.size * 1.3
--self._bb = Blitbuffer.new(self._maxlength, h)
--self._length = RenderText:renderUtf8Text(self._bb, 0, h*0.8, self.face, self.text, self.color)
--self._length = RenderText:renderUtf8Text(self._bb, 0, h*0.8, self.face, self.text, true, self.bold)
--end
function TextWidget:getSize()
@ -28,7 +29,7 @@ function TextWidget:getSize()
--self:_render()
--end
--return { w = self._length, h = self._bb:getHeight() }
local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true)
local tsize = RenderText:sizeUtf8Text(0, Screen:getWidth(), self.face, self.text, true, self.bold)
if not tsize then
return Geom:new{}
end
@ -46,8 +47,8 @@ function TextWidget:paintTo(bb, x, y)
--end
--bb:blitFrom(self._bb, x, y, 0, 0, self._length, self._bb:getHeight())
--@TODO Don't use kerning for monospaced fonts. (houqp)
RenderText:renderUtf8Text(bb, x, y+self._height*0.7, self.face, self.text,
true, self.bgcolor, self.fgcolor, self.width)
RenderText:renderUtf8Text(bb, x, y+self._height*0.7, self.face, self.text, true, self.bold,
self.bgcolor, self.fgcolor, self.width)
end
function TextWidget:free()

@ -14,12 +14,13 @@ local DEBUG = require("dbg")
local _ = require("gettext")
local ToggleLabel = TextWidget:new{
bold = true,
bgcolor = 0,
fgcolor = 1,
}
function ToggleLabel:paintTo(bb, x, y)
RenderText:renderUtf8Text(bb, x, y+self._height*0.75, self.face, self.text, true, self.bgcolor, self.fgcolor)
RenderText:renderUtf8Text(bb, x, y+self._height*0.75, self.face, self.text, true, self.bold, self.bgcolor, self.fgcolor)
end
local ToggleSwitch = InputContainer:new{

@ -1 +1 @@
Subproject commit 04a517ad2734fb7357924f082f479c63ae4c4f49
Subproject commit f35cbdff906b265712afffc8831bafe5fbb21f3b
Loading…
Cancel
Save