mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +00:00
bump crengine: monospace and fallback fonts tweaks (#9343)
Includes: - Text: fix possible overflow with "white-space: pre" - Fonts: getFontFileNameAndFaceIndex(): return family type - Fonts: account for _bias in Font hash - epub.css, fb2.css: remove hardcoded monospace font names - Fonts: allow scaling monospace fonts - Fonts: allow adjusting fallback font sizes to x-height - HTML parser: trust xml encoding before html charset In our list of fonts, show a "M" indicator alongside monospace fonts, and allow long-press to select the one to be use for "font-family: monospace". Add 2 options to Font settings, to scale monospace fonts by a percentage, and to adjust fallback fonts to their x-height.
This commit is contained in:
@@ -30,6 +30,7 @@ local CreDocument = Document:new{
|
||||
|
||||
line_space_percent = 100,
|
||||
default_font = "Noto Serif",
|
||||
monospace_font = "Droid Sans Mono",
|
||||
header_font = "Noto Sans",
|
||||
|
||||
-- Reasons for the fallback font ordering:
|
||||
@@ -132,6 +133,9 @@ function CreDocument:engineInit()
|
||||
-- and bold text with the font_base_weight setting set to its default value of 0 (=400).
|
||||
cre.regularizeRegisteredFontsWeights(true) -- true to print what modifications were made
|
||||
|
||||
-- Set up bias for some specific fonts
|
||||
self:setOtherFontBiases()
|
||||
|
||||
engine_initialized = true
|
||||
end
|
||||
end
|
||||
@@ -237,6 +241,12 @@ function CreDocument:setupDefaultView()
|
||||
-- or cover image, eg. from History hold menu).
|
||||
self:setupFallbackFontFaces()
|
||||
|
||||
-- Adjust or not fallback font sizes
|
||||
self:setAdjustedFallbackFontSizes(G_reader_settings:nilOrTrue("cre_adjusted_fallback_font_sizes"))
|
||||
|
||||
-- set monospace fonts size scaling
|
||||
self:setMonospaceFontScaling(G_reader_settings:readSetting("cre_monospace_scaling") or 100)
|
||||
|
||||
-- adjust font sizes according to dpi set in canvas context
|
||||
self._document:adjustFontSizes(CanvasContext:getDPI())
|
||||
|
||||
@@ -902,8 +912,8 @@ function CreDocument:setFontFace(new_font_face)
|
||||
-- See: crengine/src/lvfntman.cpp LVFontDef::CalcMatch():
|
||||
-- it will compute a score for each font, where it adds:
|
||||
-- + 25600 if standard font family matches (inherit serif sans-serif
|
||||
-- cursive fantasy monospace) (note that crengine registers all fonts as
|
||||
-- "sans-serif", except if their name is "Times" or "Times New Roman")
|
||||
-- cursive fantasy monospace) (note that crengine registers all fonts
|
||||
-- as "sans-serif", except monospace fonts)
|
||||
-- + 6400 if they don't and none are monospace (ie:serif vs sans-serif,
|
||||
-- prefer a sans-serif to a monospace if looking for a serif)
|
||||
-- +256000 if font names match
|
||||
@@ -914,22 +924,44 @@ function CreDocument:setFontFace(new_font_face)
|
||||
-- +25601: uses existing real font-family, but use our font even
|
||||
-- for font-family: monospace
|
||||
-- +256001: prefer our font to any existing font-family font
|
||||
self._document:setAsPreferredFontWithBias(new_font_face, 1)
|
||||
-- +1 +128x5 +256x5: we want our main font, even if it has no italic
|
||||
-- nor bold variant (eg FreeSerif), to win over all other fonts that
|
||||
-- have an italic or bold variant:
|
||||
-- cre.setAsPreferredFontWithBias(new_font_face, 1)
|
||||
-- Rather +1 +128x5 +256x5: we want our main font, even if it has no italic
|
||||
-- nor bold variant (eg FreeSerif), to win over all other fonts that have
|
||||
-- an italic or bold variant:
|
||||
-- italic_match = 5 * (256 for real italic, or 128 for fake italic
|
||||
-- weight_match = 5 * (256 - weight_diff * 256 / 800)
|
||||
-- so give our font a bias enough to win over real italic or bold fonts
|
||||
-- (all others params (size, family, name), used for computing the match
|
||||
-- score, have a factor of 100 or 1000 vs the 5 used for italic & weight,
|
||||
-- so it shouldn't hurt much).
|
||||
-- Note that this is mostly necessary when forcing a not found name,
|
||||
-- as we do in the Ignore font-family style tweak.
|
||||
self._document:setAsPreferredFontWithBias(new_font_face, 1 + 128*5 + 256*5)
|
||||
-- Note that this is mostly necessary when a font name is given and we
|
||||
-- don't have the font.
|
||||
cre.setAsPreferredFontWithBias(new_font_face, 1 + 128*5 + 256*5)
|
||||
|
||||
-- The above call has resetted all other biases, so re-set our other ones
|
||||
self:setOtherFontBiases()
|
||||
end
|
||||
end
|
||||
|
||||
function CreDocument:setOtherFontBiases()
|
||||
-- Make sure the user selected (or the default) monospace font is used even
|
||||
-- if other monospace fonts were registered (same factor as above so its
|
||||
-- synthetic bold or italic are used, in case some other monospace font
|
||||
-- has real bold or italic variants)
|
||||
local monospace_font = G_reader_settings:readSetting("monospace_font") or self.monospace_font
|
||||
cre.setAsPreferredFontWithBias(monospace_font, 1 + 128*5 + 256*5, false)
|
||||
end
|
||||
|
||||
function CreDocument:setMonospaceFontScaling(value)
|
||||
logger.dbg("CreDocument: set monospace font scaling", value)
|
||||
self._document:setIntProperty("font.monospace.size.scale.percent", value or 100)
|
||||
end
|
||||
|
||||
function CreDocument:setAdjustedFallbackFontSizes(toggle)
|
||||
logger.dbg("CreDocument: set adjusted fallback font sizes", toggle)
|
||||
self._document:setIntProperty("crengine.font.fallback.sizes.adjusted", toggle and 1 or 0)
|
||||
end
|
||||
|
||||
function CreDocument:setupFallbackFontFaces()
|
||||
local fallbacks = {}
|
||||
local seen_fonts = {}
|
||||
|
||||
Reference in New Issue
Block a user