2020-01-04 00:18:51 +00:00
local BD = require ( " ui/bidi " )
2013-10-22 15:11:31 +00:00
local CenterContainer = require ( " ui/widget/container/centercontainer " )
2018-05-11 15:48:26 +00:00
local ConfirmBox = require ( " ui/widget/confirmbox " )
2014-10-30 18:42:18 +00:00
local Device = require ( " device " )
2013-10-22 15:11:31 +00:00
local Event = require ( " ui/event " )
2020-10-05 18:26:33 +00:00
local Font = require ( " ui/font " )
2020-10-20 04:30:41 +00:00
local FontList = require ( " fontlist " )
2017-04-22 07:18:10 +00:00
local Input = Device.input
local InputContainer = require ( " ui/widget/container/inputcontainer " )
local Menu = require ( " ui/widget/menu " )
local MultiConfirmBox = require ( " ui/widget/multiconfirmbox " )
local Notification = require ( " ui/widget/notification " )
local Screen = require ( " device " ) . screen
2013-10-18 20:38:07 +00:00
local UIManager = require ( " ui/uimanager " )
2014-11-28 11:48:15 +00:00
local T = require ( " ffi/util " ) . template
2013-10-18 20:38:07 +00:00
local _ = require ( " gettext " )
2019-08-24 07:25:38 +00:00
local C_ = _.pgettext
2013-10-18 20:38:07 +00:00
local ReaderFont = InputContainer : new {
2014-03-13 13:52:43 +00:00
font_face = nil ,
font_size = nil ,
line_space_percent = nil ,
2017-09-13 10:44:37 +00:00
font_menu_title = _ ( " Font " ) ,
2014-03-13 13:52:43 +00:00
face_table = nil ,
-- default gamma from crengine's lvfntman.cpp
gamma_index = nil ,
2017-06-29 18:38:39 +00:00
steps = { 0 , 1 , 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 4 , 4 , 5 } ,
gestureScale = Screen : getWidth ( ) * FRONTLIGHT_SENSITIVITY_DECREASE ,
2012-06-12 10:04:08 +00:00
}
2012-06-12 12:58:15 +00:00
function ReaderFont : init ( )
2014-03-13 13:52:43 +00:00
if Device : hasKeyboard ( ) then
-- add shortcut for keyboard
self.key_events = {
2014-06-05 06:58:53 +00:00
ShowFontMenu = { { " F " } , doc = " show font menu " } ,
2014-03-13 13:52:43 +00:00
IncreaseSize = {
{ " Shift " , Input.group . PgFwd } ,
2014-06-05 06:58:53 +00:00
doc = " increase font size " ,
2014-03-13 13:52:43 +00:00
event = " ChangeSize " , args = " increase " } ,
DecreaseSize = {
{ " Shift " , Input.group . PgBack } ,
2014-06-05 06:58:53 +00:00
doc = " decrease font size " ,
2014-03-13 13:52:43 +00:00
event = " ChangeSize " , args = " decrease " } ,
IncreaseLineSpace = {
{ " Alt " , Input.group . PgFwd } ,
2014-06-05 06:58:53 +00:00
doc = " increase line space " ,
2014-03-13 13:52:43 +00:00
event = " ChangeLineSpace " , args = " increase " } ,
DecreaseLineSpace = {
{ " Alt " , Input.group . PgBack } ,
2014-06-05 06:58:53 +00:00
doc = " decrease line space " ,
2014-03-13 13:52:43 +00:00
event = " ChangeLineSpace " , args = " decrease " } ,
}
end
2020-04-28 21:57:10 +00:00
-- Build face_table for menu
2014-03-13 13:52:43 +00:00
self.face_table = { }
2020-04-28 21:57:10 +00:00
-- Font settings
table.insert ( self.face_table , {
text = _ ( " Font settings " ) ,
sub_item_table = self : getFontSettingsTable ( ) ,
separator = true ,
} )
-- Font list
2014-03-13 13:52:43 +00:00
local face_list = cre.getFontFaces ( )
for k , v in ipairs ( face_list ) do
2020-10-20 04:30:41 +00:00
local font_filename , font_faceindex = cre.getFontFaceFilenameAndFaceIndex ( v )
2014-03-13 13:52:43 +00:00
table.insert ( self.face_table , {
2018-05-11 15:48:26 +00:00
text_func = function ( )
-- defaults are hardcoded in credocument.lua
local default_font = G_reader_settings : readSetting ( " cre_font " ) or self.ui . document.default_font
2020-04-25 18:20:41 +00:00
local fallback_font = G_reader_settings : readSetting ( " fallback_font " ) or self.ui . document.fallback_fonts [ 1 ]
2018-05-11 15:48:26 +00:00
local text = v
2020-10-20 04:30:41 +00:00
if font_filename and font_faceindex then
text = FontList : getLocalizedFontName ( font_filename , font_faceindex ) or text
end
2018-05-11 15:48:26 +00:00
if v == default_font then
text = text .. " ★ "
end
if v == fallback_font then
text = text .. " <20> "
end
return text
end ,
2020-10-05 18:26:33 +00:00
font_func = function ( size )
if G_reader_settings : nilOrTrue ( " font_menu_use_font_face " ) then
if font_filename and font_faceindex then
return Font : getFace ( font_filename , size , font_faceindex )
end
end
end ,
2014-03-13 13:52:43 +00:00
callback = function ( )
self : setFont ( v )
2014-07-02 14:53:06 +00:00
end ,
2018-09-04 21:55:58 +00:00
hold_callback = function ( touchmenu_instance )
self : makeDefault ( v , touchmenu_instance )
2014-07-02 14:53:06 +00:00
end ,
2014-11-15 15:23:27 +00:00
checked_func = function ( )
return v == self.font_face
2021-04-02 16:12:06 +00:00
end ,
menu_item_id = v ,
2014-03-13 13:52:43 +00:00
} )
end
2021-04-02 16:12:06 +00:00
self.face_table . open_on_menu_item_id_func = function ( )
return self.font_face
end
2014-03-13 13:52:43 +00:00
self.ui . menu : registerToMainMenu ( self )
2012-06-26 16:59:47 +00:00
end
function ReaderFont : onSetDimensions ( dimen )
2014-03-13 13:52:43 +00:00
self.dimen = dimen
2012-06-26 16:59:47 +00:00
end
function ReaderFont : onReadSettings ( config )
2014-08-17 13:45:48 +00:00
self.font_face = config : readSetting ( " font_face " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " cre_font " )
or self.ui . document.default_font
2014-03-13 13:52:43 +00:00
self.ui . document : setFontFace ( self.font_face )
2014-08-17 13:45:48 +00:00
self.header_font_face = config : readSetting ( " header_font_face " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " header_font " )
or self.ui . document.header_font
2014-03-13 13:52:43 +00:00
self.ui . document : setHeaderFont ( self.header_font_face )
2014-08-17 13:45:48 +00:00
self.font_size = config : readSetting ( " font_size " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " copt_font_size " )
or DCREREADER_CONFIG_DEFAULT_FONT_SIZE
or 22
2014-11-20 22:07:39 +00:00
self.ui . document : setFontSize ( Screen : scaleBySize ( self.font_size ) )
2014-03-13 13:52:43 +00:00
2014-08-17 13:45:48 +00:00
self.font_embolden = config : readSetting ( " font_embolden " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " copt_font_weight " )
or 0
2014-06-04 13:54:01 +00:00
self.ui . document : toggleFontBolder ( self.font_embolden )
2014-03-13 13:52:43 +00:00
2017-09-08 15:38:51 +00:00
self.font_hinting = config : readSetting ( " font_hinting " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " copt_font_hinting " )
or 2 -- auto (default in cre.cpp)
2017-09-08 15:38:51 +00:00
self.ui . document : setFontHinting ( self.font_hinting )
2018-08-17 21:08:21 +00:00
self.font_kerning = config : readSetting ( " font_kerning " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " copt_font_kerning " )
or 3 -- harfbuzz (slower, but needed for proper arabic)
2018-08-17 21:08:21 +00:00
self.ui . document : setFontKerning ( self.font_kerning )
2019-11-28 21:39:06 +00:00
self.word_spacing = config : readSetting ( " word_spacing " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " copt_word_spacing " )
or { 95 , 75 }
2019-11-28 21:39:06 +00:00
self.ui . document : setWordSpacing ( self.word_spacing )
2018-06-30 14:55:43 +00:00
2020-05-03 16:06:58 +00:00
self.word_expansion = config : readSetting ( " word_expansion " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " copt_word_expansion " )
or 0
2020-05-03 16:06:58 +00:00
self.ui . document : setWordExpansion ( self.word_expansion )
2014-08-17 13:45:48 +00:00
self.line_space_percent = config : readSetting ( " line_space_percent " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " copt_line_spacing " )
or DCREREADER_CONFIG_LINE_SPACE_PERCENT_MEDIUM
2014-06-04 13:54:01 +00:00
self.ui . document : setInterlineSpacePercent ( self.line_space_percent )
2014-08-17 13:45:48 +00:00
self.gamma_index = config : readSetting ( " gamma_index " )
2021-03-06 21:44:18 +00:00
or G_reader_settings : readSetting ( " copt_font_gamma " )
or DCREREADER_CONFIG_DEFAULT_FONT_GAMMA
or 15 -- gamma = 1.0
2014-03-13 13:52:43 +00:00
self.ui . document : setGammaIndex ( self.gamma_index )
2016-10-04 17:38:32 +00:00
-- Dirty hack: we have to add following call in order to set
2014-03-13 13:52:43 +00:00
-- m_is_rendered(member of LVDocView) to true. Otherwise position inside
-- document will be reset to 0 on first view render.
-- So far, I don't know why this call will alter the value of m_is_rendered.
table.insert ( self.ui . postInitCallback , function ( )
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
end )
2012-06-12 12:58:15 +00:00
end
2012-06-12 10:04:08 +00:00
function ReaderFont : onShowFontMenu ( )
2014-03-13 13:52:43 +00:00
-- build menu widget
local main_menu = Menu : new {
title = self.font_menu_title ,
item_table = self.face_table ,
width = Screen : getWidth ( ) - 100 ,
2020-06-14 00:21:41 +00:00
height = math.floor ( Screen : getHeight ( ) * 0.5 ) ,
2018-02-04 18:48:39 +00:00
single_line = true ,
2021-02-04 16:43:52 +00:00
items_per_page = 8 ,
items_font_size = Menu.getItemFontSize ( 8 ) ,
2014-03-13 13:52:43 +00:00
}
-- build container
local menu_container = CenterContainer : new {
dimen = Screen : getSize ( ) ,
2021-02-04 16:43:52 +00:00
main_menu ,
2014-03-13 13:52:43 +00:00
}
main_menu.close_callback = function ( )
UIManager : close ( menu_container )
end
-- show menu
2014-05-31 12:43:44 +00:00
main_menu.show_parent = menu_container
2014-03-13 13:52:43 +00:00
UIManager : show ( menu_container )
2014-05-31 12:43:44 +00:00
2014-03-13 13:52:43 +00:00
return true
2012-06-12 12:58:15 +00:00
end
2012-12-22 05:27:46 +00:00
--[[
2014-03-13 13:52:43 +00:00
UpdatePos event is used to tell ReaderRolling to update pos .
2012-12-22 05:27:46 +00:00
--]]
2017-06-29 18:38:39 +00:00
function ReaderFont : onChangeSize ( direction , font_delta )
2019-03-26 22:40:22 +00:00
local delta = direction == " decrease " and - 0.5 or 0.5
2017-06-29 18:38:39 +00:00
if font_delta then
self.font_size = self.font_size + font_delta * delta
else
self.font_size = self.font_size + delta
end
2014-03-13 13:52:43 +00:00
self.ui : handleEvent ( Event : new ( " SetFontSize " , self.font_size ) )
return true
2012-06-12 10:04:08 +00:00
end
2013-01-07 04:21:11 +00:00
function ReaderFont : onSetFontSize ( new_size )
2018-02-10 21:20:09 +00:00
if new_size > 255 then new_size = 255 end
2014-03-13 13:52:43 +00:00
if new_size < 12 then new_size = 12 end
self.font_size = new_size
2017-04-22 07:18:10 +00:00
self.ui . document : setFontSize ( Screen : scaleBySize ( new_size ) )
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
2014-03-13 13:52:43 +00:00
UIManager : show ( Notification : new {
2014-11-28 13:10:37 +00:00
text = T ( _ ( " Font size set to %1. " ) , self.font_size ) ,
2014-12-01 14:39:41 +00:00
} )
2014-03-13 13:52:43 +00:00
return true
2013-01-07 04:21:11 +00:00
end
2014-08-17 13:45:48 +00:00
function ReaderFont : onSetLineSpace ( space )
2019-03-14 23:34:46 +00:00
self.line_space_percent = math.min ( 200 , math.max ( 50 , space ) )
2014-03-13 13:52:43 +00:00
UIManager : show ( Notification : new {
2014-11-28 13:10:37 +00:00
text = T ( _ ( " Line spacing set to %1%. " ) , self.line_space_percent ) ,
2014-03-13 13:52:43 +00:00
} )
self.ui . document : setInterlineSpacePercent ( self.line_space_percent )
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
return true
2012-06-12 13:12:04 +00:00
end
2014-06-04 13:54:01 +00:00
function ReaderFont : onToggleFontBolder ( toggle )
self.font_embolden = toggle
self.ui . document : toggleFontBolder ( toggle )
2014-03-13 13:52:43 +00:00
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
return true
2013-01-07 17:17:34 +00:00
end
2017-09-08 15:38:51 +00:00
function ReaderFont : onSetFontHinting ( mode )
self.font_hinting = mode
self.ui . document : setFontHinting ( mode )
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
return true
end
2018-08-17 21:08:21 +00:00
function ReaderFont : onSetFontKerning ( mode )
self.font_kerning = mode
self.ui . document : setFontKerning ( mode )
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
return true
end
2019-11-28 21:39:06 +00:00
function ReaderFont : onSetWordSpacing ( values )
self.word_spacing = values
self.ui . document : setWordSpacing ( values )
2018-06-30 14:55:43 +00:00
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
return true
end
2020-05-03 16:06:58 +00:00
function ReaderFont : onSetWordExpansion ( value )
self.word_expansion = value
self.ui . document : setWordExpansion ( value )
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
return true
end
2014-08-17 13:45:48 +00:00
function ReaderFont : onSetFontGamma ( gamma )
self.gamma_index = gamma
2018-03-11 13:54:17 +00:00
self.ui . document : setGammaIndex ( self.gamma_index )
local gamma_level = self.ui . document : getGammaLevel ( )
2014-03-13 13:52:43 +00:00
UIManager : show ( Notification : new {
2018-03-11 13:54:17 +00:00
text = T ( _ ( " Font gamma set to %1. " ) , gamma_level ) ,
2014-03-13 13:52:43 +00:00
} )
self.ui : handleEvent ( Event : new ( " RedrawCurrentView " ) )
return true
2013-01-07 21:42:23 +00:00
end
2013-12-27 15:18:16 +00:00
function ReaderFont : onSaveSettings ( )
2014-03-13 13:52:43 +00:00
self.ui . doc_settings : saveSetting ( " font_face " , self.font_face )
self.ui . doc_settings : saveSetting ( " header_font_face " , self.header_font_face )
self.ui . doc_settings : saveSetting ( " font_size " , self.font_size )
2014-06-04 13:54:01 +00:00
self.ui . doc_settings : saveSetting ( " font_embolden " , self.font_embolden )
2017-09-08 15:38:51 +00:00
self.ui . doc_settings : saveSetting ( " font_hinting " , self.font_hinting )
2018-08-17 21:08:21 +00:00
self.ui . doc_settings : saveSetting ( " font_kerning " , self.font_kerning )
2019-11-28 21:39:06 +00:00
self.ui . doc_settings : saveSetting ( " word_spacing " , self.word_spacing )
2020-05-03 16:06:58 +00:00
self.ui . doc_settings : saveSetting ( " word_expansion " , self.word_expansion )
2014-03-13 13:52:43 +00:00
self.ui . doc_settings : saveSetting ( " line_space_percent " , self.line_space_percent )
self.ui . doc_settings : saveSetting ( " gamma_index " , self.gamma_index )
2012-06-26 09:05:49 +00:00
end
2012-12-13 18:32:16 +00:00
function ReaderFont : setFont ( face )
2014-03-13 13:52:43 +00:00
if face and self.font_face ~= face then
self.font_face = face
UIManager : show ( Notification : new {
2014-11-28 13:10:37 +00:00
text = T ( _ ( " Redrawing with font %1. " ) , face ) ,
2014-03-13 13:52:43 +00:00
} )
self.ui . document : setFontFace ( face )
-- signal readerrolling to update pos in new height
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
end
2012-12-13 18:32:16 +00:00
end
2018-09-04 21:55:58 +00:00
function ReaderFont : makeDefault ( face , touchmenu_instance )
2014-07-02 14:53:06 +00:00
if face then
2015-09-02 15:21:38 +00:00
UIManager : show ( MultiConfirmBox : new {
2018-05-11 15:48:26 +00:00
text = T ( _ ( " Would you like %1 to be used as the default font (★), or the fallback font (<28> )? \n \n Characters not found in the active font are shown in the fallback font instead. " ) , face ) ,
2015-09-04 08:59:07 +00:00
choice1_text = _ ( " Default " ) ,
2015-09-02 15:21:38 +00:00
choice1_callback = function ( )
2014-07-02 14:53:06 +00:00
G_reader_settings : saveSetting ( " cre_font " , face )
2018-09-04 21:55:58 +00:00
if touchmenu_instance then touchmenu_instance : updateItems ( ) end
2014-07-02 14:53:06 +00:00
end ,
2019-08-24 07:25:38 +00:00
choice2_text = C_ ( " Font " , " Fallback " ) ,
2015-09-02 15:21:38 +00:00
choice2_callback = function ( )
2020-04-25 18:20:41 +00:00
G_reader_settings : saveSetting ( " fallback_font " , face )
self.ui . document : setupFallbackFontFaces ( )
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
2018-09-04 21:55:58 +00:00
if touchmenu_instance then touchmenu_instance : updateItems ( ) end
2015-09-02 15:21:38 +00:00
end ,
2014-07-02 14:53:06 +00:00
} )
end
end
2017-03-04 13:46:38 +00:00
function ReaderFont : addToMainMenu ( menu_items )
2020-02-14 07:22:25 +00:00
-- Have TouchMenu show half of the usual nb of items, so we
-- have more room to see how the text looks with that font
self.face_table . max_per_page = 5
2014-03-13 13:52:43 +00:00
-- insert table to main reader menu
2017-03-04 13:46:38 +00:00
menu_items.change_font = {
2021-04-02 16:12:06 +00:00
text_func = function ( )
return T ( _ ( " Font: %1 " ) , BD.wrap ( self.font_face ) )
end ,
2014-03-13 13:52:43 +00:00
sub_item_table = self.face_table ,
2017-02-28 21:46:32 +00:00
}
2012-12-15 01:23:02 +00:00
end
2013-10-18 20:38:07 +00:00
2020-07-12 18:47:49 +00:00
function ReaderFont : gesToFontSize ( ges )
2020-07-14 21:39:03 +00:00
if type ( ges ) ~= " table " then return ges end
2019-08-07 18:18:36 +00:00
if ges.distance == nil then
ges.distance = 1
end
2017-06-29 18:38:39 +00:00
local step = math.ceil ( 2 * # self.steps * ges.distance / self.gestureScale )
local delta_int = self.steps [ step ] or self.steps [ # self.steps ]
2020-07-12 18:47:49 +00:00
return delta_int
end
function ReaderFont : onIncreaseFontSize ( ges )
local delta_int = self : gesToFontSize ( ges )
local info = Notification : new { text = _ ( " Increasing font size… " ) }
UIManager : show ( info )
UIManager : forceRePaint ( )
self : onChangeSize ( " increase " , delta_int )
UIManager : close ( info )
return true
end
function ReaderFont : onDecreaseFontSize ( ges )
local delta_int = self : gesToFontSize ( ges )
local info = Notification : new { text = _ ( " Decreasing font size… " ) }
UIManager : show ( info )
UIManager : forceRePaint ( )
self : onChangeSize ( " decrease " , delta_int )
UIManager : close ( info )
2017-04-22 07:18:10 +00:00
return true
end
2020-04-28 21:57:10 +00:00
function ReaderFont : getFontSettingsTable ( )
local settings_table = { }
2020-08-25 21:13:39 +00:00
if Device : isAndroid ( ) or Device : isDesktop ( ) or Device : isEmulator ( ) or Device : isPocketBook ( ) then
2020-04-28 21:57:10 +00:00
for _ , item in ipairs ( require ( " ui/elements/font_settings " ) : getSystemFontMenuItems ( ) ) do
table.insert ( settings_table , item )
end
settings_table [ # settings_table ] . separator = true
end
2020-10-05 18:26:33 +00:00
table.insert ( settings_table , {
text = _ ( " Display font names with their own font " ) ,
checked_func = function ( )
return G_reader_settings : nilOrTrue ( " font_menu_use_font_face " )
end ,
callback = function ( )
G_reader_settings : flipNilOrTrue ( " font_menu_use_font_face " )
end ,
help_text = _ ( [[In the font menu, display each font name with its own font face.]] ) ,
separator = true ,
} )
2020-04-28 21:57:10 +00:00
table.insert ( settings_table , {
text = _ ( " Use additional fallback fonts " ) ,
checked_func = function ( )
return G_reader_settings : nilOrTrue ( " additional_fallback_fonts " )
end ,
callback = function ( )
2020-10-05 18:26:33 +00:00
G_reader_settings : flipNilOrTrue ( " additional_fallback_fonts " )
2020-04-28 21:57:10 +00:00
self.ui . document : setupFallbackFontFaces ( )
self.ui : handleEvent ( Event : new ( " UpdatePos " ) )
end ,
help_text = T ( _ ( [ [
Enable additional fallback fonts , for the most complete script and language coverage .
These fonts will be used in this order :
% 1
You can set a preferred fallback font with a long - press on a font name , and it will be used before these .
If that font happens to be part of this list already , it will be used first . ] ] ) ,
table.concat ( self.ui . document.fallback_fonts , " \n " ) ) ,
separator = true ,
} )
table.insert ( settings_table , {
text = _ ( " Generate font test document " ) ,
callback = function ( )
UIManager : show ( ConfirmBox : new {
text = _ ( " Would you like to generate an HTML document showing some sample text rendered with each available font? " ) ;
ok_callback = function ( )
self : buildFontsTestDocument ( )
end
} )
end ,
} )
return settings_table
2018-05-11 15:48:26 +00:00
end
2020-04-28 21:57:10 +00:00
-- Default sample file
2020-09-12 08:47:35 +00:00
local FONT_TEST_DEFAULT_SAMPLE_PATH = " frontend/ui/elements/font-test-sample-default.template "
2020-04-28 21:57:10 +00:00
-- Users can set their own sample file, that will be used if found
local FONT_TEST_USER_SAMPLE_PATH = require ( " datastorage " ) : getSettingsDir ( ) .. " /font-test-sample.html "
-- This document will be generated in the home or default directory
local FONT_TEST_FINAL_FILENAME = " font-test.html "
2018-05-11 15:48:26 +00:00
function ReaderFont : buildFontsTestDocument ( )
2020-04-28 21:57:10 +00:00
local html_sample
local f = io.open ( FONT_TEST_USER_SAMPLE_PATH , " r " )
if f then
html_sample = f : read ( " *all " )
f : close ( )
end
if not html_sample then
f = io.open ( FONT_TEST_DEFAULT_SAMPLE_PATH , " r " )
if not f then return nil end
html_sample = f : read ( " *all " )
f : close ( )
end
2018-05-11 15:48:26 +00:00
local dir = G_reader_settings : readSetting ( " home_dir " )
2021-03-06 21:44:18 +00:00
or require ( " apps/filemanager/filemanagerutil " ) . getDefaultDir ( )
or " . "
2020-04-28 21:57:10 +00:00
local font_test_final_path = dir .. " / " .. FONT_TEST_FINAL_FILENAME
f = io.open ( font_test_final_path , " w " )
if not f then return end
-- Using <section><title>...</title></section> allows for a TOC to be built by crengine
2018-05-11 15:48:26 +00:00
f : write ( string.format ( [ [
< ? xml version = " 1.0 " encoding = " UTF-8 " ? >
< html >
< head >
< title >% s </ title >
2019-03-03 08:16:27 +00:00
< style >
section > title {
font - size : large ;
font - weight : bold ;
text - align : center ;
page - break - before : always ;
margin - bottom : 0.5 em ;
}
a { color : black ; }
</ style >
2018-05-11 15:48:26 +00:00
</ head >
< body >
< section id = " list " >< title >% s </ title ></ section >
] ] , _ ( " Available fonts test document " ) , _ ( " AVAILABLE FONTS " ) ) )
local face_list = cre.getFontFaces ( )
f : write ( " <div style='margin: 2em'> \n " )
for _ , font_name in ipairs ( face_list ) do
local font_id = font_name : gsub ( " " , " _ " ) : gsub ( " ' " , " _ " )
f : write ( string.format ( " <div><a href='#%s'>%s</a></div> \n " , font_id , font_name ) )
end
f : write ( " </div> \n \n " )
for _ , font_name in ipairs ( face_list ) do
local font_id = font_name : gsub ( " " , " _ " ) : gsub ( " ' " , " _ " )
f : write ( string.format ( " <section id='%s'><title>%s</title></section> \n " , font_id , font_name ) )
f : write ( string.format ( " <div style='font-family: %s'> \n " , font_name ) )
f : write ( html_sample )
f : write ( " \n </div> \n \n " )
end
f : write ( " </body></html> \n " )
f : close ( )
UIManager : show ( ConfirmBox : new {
2020-04-28 21:57:10 +00:00
text = T ( _ ( " Document created as: \n %1 \n \n Would you like to read it now? " ) , BD.filepath ( font_test_final_path ) ) ,
2018-05-11 15:48:26 +00:00
ok_callback = function ( )
UIManager : scheduleIn ( 1.0 , function ( )
2020-04-28 21:57:10 +00:00
self.ui : switchDocument ( font_test_final_path )
2018-05-11 15:48:26 +00:00
end )
end ,
} )
end
2013-10-18 20:38:07 +00:00
return ReaderFont