2016-02-16 00:14:15 +00:00
|
|
|
--[[--
|
|
|
|
Font module.
|
|
|
|
]]
|
|
|
|
|
2019-08-24 09:12:06 +00:00
|
|
|
local FontList = require("fontlist")
|
2017-04-29 08:38:09 +00:00
|
|
|
local Freetype = require("ffi/freetype")
|
2019-03-03 02:50:05 +00:00
|
|
|
local Screen = require("device").screen
|
2019-08-24 09:12:06 +00:00
|
|
|
local logger = require("logger")
|
2012-03-10 08:41:23 +00:00
|
|
|
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
-- Known regular (and italic) fonts with an available bold font file
|
|
|
|
local _bold_font_variant = {}
|
|
|
|
_bold_font_variant["NotoSans-Regular.ttf"] = "NotoSans-Bold.ttf"
|
|
|
|
_bold_font_variant["NotoSans-Italic.ttf"] = "NotoSans-BoldItalic.ttf"
|
|
|
|
_bold_font_variant["NotoSansArabicUI-Regular.ttf"] = "NotoSansArabicUI-Bold.ttf"
|
|
|
|
_bold_font_variant["NotoSerif-Regular.ttf"] = "NotoSerif-Bold.ttf"
|
|
|
|
_bold_font_variant["NotoSerif-Italic.ttf"] = "NotoSerif-BoldItalic.ttf"
|
|
|
|
|
|
|
|
-- Build the reverse mapping, so we can know a font is bold
|
|
|
|
local _regular_font_variant = {}
|
|
|
|
for regular, bold in pairs(_bold_font_variant) do
|
|
|
|
_regular_font_variant[bold] = regular
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local Font = {
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
-- Make these available in the Font object, so other code
|
|
|
|
-- can complete them if needed.
|
|
|
|
bold_font_variant = _bold_font_variant,
|
|
|
|
regular_font_variant = _regular_font_variant,
|
|
|
|
|
|
|
|
-- Allow globally not promoting fonts to their bold variants
|
|
|
|
-- (and use thiner and narrower synthetized bold instead).
|
|
|
|
use_bold_font_for_bold = G_reader_settings:nilOrTrue("use_bold_font_for_bold"),
|
|
|
|
|
|
|
|
-- Widgets can provide "bold = Font.FORCE_SYNTHETIZED_BOLD" instead
|
|
|
|
-- of "bold = true" to explicitely request synthetized bold, which,
|
|
|
|
-- with XText, makes a bold string the same width as itself non-bold.
|
|
|
|
FORCE_SYNTHETIZED_BOLD = "FORCE_SYNTHETIZED_BOLD",
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
fontmap = {
|
|
|
|
-- default font for menu contents
|
2019-09-04 18:50:25 +00:00
|
|
|
cfont = "NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
-- default font for title
|
|
|
|
--tfont = "NimbusSanL-BoldItal.cff",
|
2019-09-04 18:50:25 +00:00
|
|
|
tfont = "NotoSans-Bold.ttf",
|
|
|
|
smalltfont = "NotoSans-Bold.ttf",
|
|
|
|
x_smalltfont = "NotoSans-Bold.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
-- default font for footer
|
2019-09-04 18:50:25 +00:00
|
|
|
ffont = "NotoSans-Regular.ttf",
|
|
|
|
smallffont = "NotoSans-Regular.ttf",
|
|
|
|
largeffont = "NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- default font for reading position info
|
2019-09-04 18:50:25 +00:00
|
|
|
rifont = "NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- default font for pagination display
|
2019-09-04 18:50:25 +00:00
|
|
|
pgfont = "NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- selectmenu: font for item shortcut
|
2019-08-24 09:12:06 +00:00
|
|
|
scfont = "DroidSansMono.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- help page: font for displaying keys
|
2019-08-24 09:12:06 +00:00
|
|
|
hpkfont = "DroidSansMono.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
-- font for displaying help messages
|
2019-09-04 18:50:25 +00:00
|
|
|
hfont = "NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- font for displaying input content
|
|
|
|
-- we have to use mono here for better distance controlling
|
2019-08-24 09:12:06 +00:00
|
|
|
infont = "DroidSansMono.ttf",
|
2018-10-08 16:58:43 +00:00
|
|
|
-- small mono font for displaying code
|
2019-08-24 09:12:06 +00:00
|
|
|
smallinfont = "DroidSansMono.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
-- font for info messages
|
2019-09-04 18:50:25 +00:00
|
|
|
infofont = "NotoSans-Regular.ttf",
|
2017-04-29 08:38:09 +00:00
|
|
|
|
|
|
|
-- small font for info messages
|
2019-09-04 18:50:25 +00:00
|
|
|
smallinfofont = "NotoSans-Regular.ttf",
|
2017-04-29 08:38:09 +00:00
|
|
|
-- small bold font for info messages
|
2019-09-04 18:50:25 +00:00
|
|
|
smallinfofontbold = "NotoSans-Bold.ttf",
|
2017-04-29 08:38:09 +00:00
|
|
|
-- extra small font for info messages
|
2019-09-04 18:50:25 +00:00
|
|
|
x_smallinfofont = "NotoSans-Regular.ttf",
|
2017-04-29 08:38:09 +00:00
|
|
|
-- extra extra small font for info messages
|
2019-09-04 18:50:25 +00:00
|
|
|
xx_smallinfofont = "NotoSans-Regular.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
2016-02-15 23:00:38 +00:00
|
|
|
sizemap = {
|
|
|
|
cfont = 24,
|
|
|
|
tfont = 26,
|
2017-04-29 08:38:09 +00:00
|
|
|
smalltfont = 24,
|
|
|
|
x_smalltfont = 22,
|
2016-02-15 23:00:38 +00:00
|
|
|
ffont = 20,
|
2017-04-29 08:38:09 +00:00
|
|
|
smallffont = 15,
|
|
|
|
largeffont = 25,
|
2016-02-15 23:00:38 +00:00
|
|
|
pgfont = 20,
|
|
|
|
scfont = 20,
|
|
|
|
rifont = 16,
|
|
|
|
hpkfont = 20,
|
|
|
|
hfont = 24,
|
|
|
|
infont = 22,
|
2018-10-08 16:58:43 +00:00
|
|
|
smallinfont = 16,
|
2016-02-15 23:00:38 +00:00
|
|
|
infofont = 24,
|
2017-04-29 08:38:09 +00:00
|
|
|
smallinfofont = 22,
|
|
|
|
smallinfofontbold = 22,
|
|
|
|
x_smallinfofont = 20,
|
|
|
|
xx_smallinfofont = 18,
|
2016-02-15 23:00:38 +00:00
|
|
|
},
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
-- This fallback fonts list should only contain
|
|
|
|
-- regular weight (non bold) font files.
|
2014-03-13 13:52:43 +00:00
|
|
|
fallbacks = {
|
2019-09-04 18:50:25 +00:00
|
|
|
[1] = "NotoSans-Regular.ttf",
|
|
|
|
[2] = "NotoSansCJKsc-Regular.otf",
|
2019-11-15 14:14:36 +00:00
|
|
|
[3] = "NotoSansArabicUI-Regular.ttf",
|
2020-01-31 16:25:13 +00:00
|
|
|
[4] = "NotoSansDevanagariUI-Regular.ttf",
|
|
|
|
[5] = "nerdfonts/symbols.ttf",
|
|
|
|
[6] = "freefont/FreeSans.ttf",
|
|
|
|
[7] = "freefont/FreeSerif.ttf",
|
2014-03-13 13:52:43 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
-- face table
|
|
|
|
faces = {},
|
2012-03-10 08:41:23 +00:00
|
|
|
}
|
|
|
|
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
-- Helper functions with explicite names around
|
|
|
|
-- bold/regular_font_variant tables
|
|
|
|
function Font:hasBoldVariant(name)
|
|
|
|
return self.bold_font_variant[name] and true or false
|
|
|
|
end
|
|
|
|
|
|
|
|
function Font:getBoldVariantName(name)
|
|
|
|
return self.bold_font_variant[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
function Font:isRealBoldFont(name)
|
|
|
|
return self.regular_font_variant[name] and true or false
|
|
|
|
end
|
|
|
|
|
|
|
|
function Font:getRegularVariantName(name)
|
|
|
|
return self.regular_font_variant[name] or name
|
|
|
|
end
|
|
|
|
|
2019-11-15 14:14:36 +00:00
|
|
|
-- Synthetized bold strength can be tuned:
|
|
|
|
-- local bold_strength_factor = 1 -- really too bold
|
|
|
|
-- local bold_strength_factor = 1/2 -- bold enough
|
|
|
|
local bold_strength_factor = 3/8 -- as crengine, lighter
|
|
|
|
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
-- Add some properties to a face object as needed
|
|
|
|
local _completeFaceProperties = function(face_obj)
|
|
|
|
if not face_obj.embolden_half_strength then
|
|
|
|
-- Cache this value in case we use bold, to avoid recomputation
|
|
|
|
face_obj.embolden_half_strength = face_obj.ftface:getEmboldenHalfStrength(bold_strength_factor)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-11-15 14:14:36 +00:00
|
|
|
-- Callback to be used by libkoreader-xtext.so to get Freetype
|
|
|
|
-- instantiated fallback fonts when needed for shaping text
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
-- (Beware: any error in this code won't be noticed when this
|
|
|
|
-- is called from the C module...)
|
2019-11-15 14:14:36 +00:00
|
|
|
local _getFallbackFont = function(face_obj, num)
|
|
|
|
if not num or num == 0 then -- return the main font
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
_completeFaceProperties(face_obj)
|
2019-11-15 14:14:36 +00:00
|
|
|
return face_obj
|
|
|
|
end
|
|
|
|
if not face_obj.fallbacks then
|
|
|
|
face_obj.fallbacks = {}
|
|
|
|
end
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
if face_obj.fallbacks[num] ~= nil then -- (false means: no more fallback font)
|
2019-11-15 14:14:36 +00:00
|
|
|
return face_obj.fallbacks[num]
|
|
|
|
end
|
|
|
|
local next_num = #face_obj.fallbacks + 1
|
|
|
|
local cur_num = 0
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
local realname = face_obj.realname
|
|
|
|
if face_obj.is_real_bold then
|
|
|
|
-- Get the regular name, to skip it from Font.fallbacks
|
|
|
|
realname = Font:getRegularVariantName(realname)
|
|
|
|
end
|
2019-11-15 14:14:36 +00:00
|
|
|
for index, fontname in pairs(Font.fallbacks) do
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
if fontname ~= realname then -- Skip base one if among fallbacks
|
|
|
|
-- If main font is a real bold, or if it's not but we want bold,
|
|
|
|
-- get the bold variant of the fallback if one exists.
|
|
|
|
-- But if one exists, use the regular variant as an additional
|
|
|
|
-- fallback, drawn with synthetized bold (often, bold fonts
|
|
|
|
-- have less glyphs than their regular counterpart).
|
|
|
|
if face_obj.is_real_bold or face_obj.wants_bold == true then
|
|
|
|
-- (not if wants_bold==Font.FORCE_SYNTHETIZED_BOLD)
|
|
|
|
local bold_variant_name = Font:getBoldVariantName(fontname)
|
|
|
|
if bold_variant_name then
|
|
|
|
-- There is a bold variant of that fallback font, that we can use
|
|
|
|
local fb_face = Font:getFace(bold_variant_name, face_obj.orig_size)
|
|
|
|
if fb_face ~= nil then -- valid font
|
|
|
|
cur_num = cur_num + 1
|
|
|
|
if cur_num == next_num then
|
|
|
|
_completeFaceProperties(fb_face)
|
|
|
|
face_obj.fallbacks[next_num] = fb_face
|
|
|
|
return fb_face
|
|
|
|
end
|
|
|
|
-- otherwise, go on with the regular variant
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-11-15 14:14:36 +00:00
|
|
|
local fb_face = Font:getFace(fontname, face_obj.orig_size)
|
|
|
|
if fb_face ~= nil then -- valid font
|
|
|
|
cur_num = cur_num + 1
|
|
|
|
if cur_num == next_num then
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
_completeFaceProperties(fb_face)
|
2019-11-15 14:14:36 +00:00
|
|
|
face_obj.fallbacks[next_num] = fb_face
|
|
|
|
return fb_face
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- no more fallback font
|
|
|
|
face_obj.fallbacks[next_num] = false
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2017-04-29 08:38:09 +00:00
|
|
|
--- Gets font face object.
|
|
|
|
-- @string font
|
|
|
|
-- @int size optional size
|
|
|
|
-- @treturn table @{FontFaceObj}
|
2012-04-09 17:04:26 +00:00
|
|
|
function Font:getFace(font, size)
|
2015-09-09 03:10:17 +00:00
|
|
|
-- default to content font
|
|
|
|
if not font then font = self.cfont end
|
2014-03-13 13:52:43 +00:00
|
|
|
|
2016-02-15 23:00:38 +00:00
|
|
|
if not size then size = self.sizemap[font] end
|
2014-08-20 01:39:50 +00:00
|
|
|
-- original size before scaling by screen DPI
|
|
|
|
local orig_size = size
|
2019-03-03 02:50:05 +00:00
|
|
|
size = Screen:scaleBySize(size)
|
2014-03-13 13:52:43 +00:00
|
|
|
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
local realname = self.fontmap[font]
|
|
|
|
if not realname then
|
|
|
|
realname = font
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Avoid emboldening already bold fonts
|
|
|
|
local is_real_bold = self:isRealBoldFont(realname)
|
|
|
|
|
|
|
|
-- Make a hash from the realname (many fonts in our fontmap use
|
|
|
|
-- the same font file: have them share their glyphs cache)
|
|
|
|
local hash = realname..size
|
|
|
|
|
2015-09-09 03:10:17 +00:00
|
|
|
local face_obj = self.faces[hash]
|
2014-03-13 13:52:43 +00:00
|
|
|
-- build face if not found
|
2015-09-09 03:10:17 +00:00
|
|
|
if not face_obj then
|
2019-08-24 09:12:06 +00:00
|
|
|
local builtin_font_location = FontList.fontdir.."/"..realname
|
|
|
|
local ok, face = pcall(Freetype.newFace, builtin_font_location, size)
|
2019-09-04 18:50:25 +00:00
|
|
|
|
2019-08-24 09:12:06 +00:00
|
|
|
-- Not all fonts are bundled on all platforms because they come with the system.
|
|
|
|
-- In that case, search through all font folders for the requested font.
|
2019-09-04 18:50:25 +00:00
|
|
|
if not ok then
|
2019-08-24 09:12:06 +00:00
|
|
|
local fonts = FontList:getFontList()
|
2019-09-04 18:50:25 +00:00
|
|
|
local escaped_realname = realname:gsub("[-]", "%%-")
|
|
|
|
|
2019-08-24 09:12:06 +00:00
|
|
|
for _k, _v in ipairs(fonts) do
|
2019-09-04 18:50:25 +00:00
|
|
|
if _v:find(escaped_realname) then
|
|
|
|
logger.dbg("Found font:", realname, "in", _v)
|
2019-08-24 09:12:06 +00:00
|
|
|
ok, face = pcall(Freetype.newFace, _v, size)
|
2019-09-04 18:50:25 +00:00
|
|
|
|
|
|
|
if ok then break end
|
2019-08-24 09:12:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
if not ok then
|
2019-08-24 09:12:06 +00:00
|
|
|
logger.err("#! Font ", font, " (", realname, ") not supported: ", face)
|
2014-03-13 13:52:43 +00:00
|
|
|
return nil
|
|
|
|
end
|
2016-02-16 00:14:15 +00:00
|
|
|
--- Freetype font face wrapper object
|
|
|
|
-- @table FontFaceObj
|
2019-01-15 19:38:25 +00:00
|
|
|
-- @field orig_font font name requested
|
2016-02-16 00:14:15 +00:00
|
|
|
-- @field size size of the font face (after scaled by screen size)
|
|
|
|
-- @field orig_size raw size of the font face (before scale)
|
|
|
|
-- @field ftface font face object from freetype
|
|
|
|
-- @field hash hash key for this font face
|
2015-09-09 03:10:17 +00:00
|
|
|
face_obj = {
|
2019-01-15 19:38:25 +00:00
|
|
|
orig_font = font,
|
2019-11-15 14:14:36 +00:00
|
|
|
realname = realname,
|
2015-09-09 03:10:17 +00:00
|
|
|
size = size,
|
|
|
|
orig_size = orig_size,
|
|
|
|
ftface = face,
|
2019-11-15 14:14:36 +00:00
|
|
|
hash = hash,
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
is_real_bold = is_real_bold,
|
2015-09-09 03:10:17 +00:00
|
|
|
}
|
|
|
|
self.faces[hash] = face_obj
|
2019-11-15 14:14:36 +00:00
|
|
|
|
|
|
|
-- Callback to be used by libkoreader-xtext.so to get Freetype
|
|
|
|
-- instantiated fallback fonts when needed for shaping text
|
|
|
|
face_obj.getFallbackFont = function(num)
|
|
|
|
return _getFallbackFont(face_obj, num)
|
|
|
|
end
|
|
|
|
-- Font features, to be given by libkoreader-xtext.so to HarfBuzz.
|
|
|
|
-- (Could be tweaked by font if needed. Note that NotoSans does not
|
|
|
|
-- have common ligatures, like for "fi" or "fl", so we won't see
|
|
|
|
-- them in the UI.)
|
|
|
|
-- Use HB defaults, and be sure to enable kerning and ligatures
|
|
|
|
-- (which might be part of HB defaults, or not, not sure).
|
|
|
|
face_obj.hb_features = { "+kern", "+liga" }
|
|
|
|
-- If we'd wanted to disable all features that might be enabled
|
|
|
|
-- by HarfBuzz (see harfbuzz/src/hb-ot-shape.cc, quite unclear
|
|
|
|
-- what's enabled or not by default):
|
|
|
|
-- face_obj.hb_features = {
|
|
|
|
-- "-kern", "-mark", "-mkmk", "-curs", "-locl", "-liga",
|
|
|
|
-- "-rlig", "-clig", "-ccmp", "-calt", "-rclt", "-rvrn",
|
|
|
|
-- "-ltra", "-ltrm", "-rtla", "-rtlm", "-frac", "-numr",
|
|
|
|
-- "-dnom", "-rand", "-trak", "-vert", }
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2015-09-09 03:10:17 +00:00
|
|
|
return face_obj
|
2012-04-09 17:04:26 +00:00
|
|
|
end
|
|
|
|
|
UI font rendering: use available bold fonts for bold (#5675)
A few fixes and enhancement related to bold text:
- When using bold=true with a regular font, use its bold
variant if one exists (can be prevented by manually
adding a setting: "use_bold_font_for_bold" = false).
- When using a bold font without bold=true, promote bold
to true, so fallback fonts are drawn bold too.
- Whether using a bold font, or using bold=true, ensure
fallback fonts are drawn bold, with their available bold
variant if one exists, or with synthetized bold.
- When using a bold variant of a fallback font, keep using
the regular variant as another fallback (as bold fonts
may contain less glyphs than their regular counterpart).
- Allow providing bold=Font.FORCE_SYNTHETIZED_BOLD to
get synth bold even when a bold font exists (might be
interesting to get text in bold the same width as the
same text non-bold).
- Use the font realname in the key when caching glyphs,
instead of our aliases (cfont, infont...) to avoid
duplication and wasting memory.
2019-12-08 19:31:27 +00:00
|
|
|
--- Returns an alternative face instance to be used for measuring
|
|
|
|
-- and drawing (in most cases, the one provided untouched)
|
|
|
|
--
|
|
|
|
-- If 'bold' is true, or if 'face' is a real bold face, we may need to
|
|
|
|
-- use an alternative instance of the font, with possibly the associated
|
|
|
|
-- real bold font, and/or with tweaks so fallback fonts are rendered
|
|
|
|
-- bold too, without affecting the regular 'face'.
|
|
|
|
-- (This function should only be used by TextWidget and TextBoxWidget.
|
|
|
|
-- Other widgets should not use it, and neither _getFallbackFont()
|
|
|
|
-- which will do its own processing.)
|
|
|
|
--
|
|
|
|
-- @tparam ui.font.FontFaceObj provided face font face
|
|
|
|
-- @bool bold whether bold is requested
|
|
|
|
-- @treturn ui.font.FontFaceObj face face to use for drawing
|
|
|
|
-- @treturn bool bold adjusted bold properties
|
|
|
|
function Font:getAdjustedFace(face, bold)
|
|
|
|
if face.is_real_bold then
|
|
|
|
-- No adjustment needed: main real bold font will ensure
|
|
|
|
-- fallback fonts use their associated bold font or
|
|
|
|
-- get synthetized bold - whether bold is requested or not
|
|
|
|
-- (Set returned bold to true, to force synthetized bold
|
|
|
|
-- on fallback fonts with no associated real bold)
|
|
|
|
-- (Drop bold=FORCE_SYNTHETIZED_BOLD and use 'true' if
|
|
|
|
-- we were given a real bold font.)
|
|
|
|
return face, true
|
|
|
|
end
|
|
|
|
if not bold then
|
|
|
|
-- No adjustment needed: regular main font, and regular
|
|
|
|
-- fallback fonts untouched.
|
|
|
|
return face, false
|
|
|
|
end
|
|
|
|
-- We have bold requested, and a regular/non-bold font.
|
|
|
|
if not self.use_bold_font_for_bold then
|
|
|
|
-- If promotion to real bold is not wished, force synth bold
|
|
|
|
bold = Font.FORCE_SYNTHETIZED_BOLD
|
|
|
|
end
|
|
|
|
if bold ~= Font.FORCE_SYNTHETIZED_BOLD then
|
|
|
|
-- See if a bold font file exists for that regular font.
|
|
|
|
local bold_variant_name = self:getBoldVariantName(face.realname)
|
|
|
|
if bold_variant_name then
|
|
|
|
face = Font:getFace(bold_variant_name, face.orig_size)
|
|
|
|
-- It has is_real_bold=true: no adjustment needed
|
|
|
|
return face, true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Only the regular font is available, and bold requested:
|
|
|
|
-- we'll have synthetized bold - but _getFallbackFont() should
|
|
|
|
-- build a list of fallback fonts either synthetized, or possibly
|
|
|
|
-- using the bold variant of a regular fallback font.
|
|
|
|
-- We don't want to collide with the regular font face_obj.fallbacks
|
|
|
|
-- so let's make a shallow clone of this face_obj, and have it cached.
|
|
|
|
-- (Different hash if real bold accepted or not, as the fallback
|
|
|
|
-- fonts list may then be different.)
|
|
|
|
local hash = face.hash..(bold == Font.FORCE_SYNTHETIZED_BOLD and "synthbold" or "realbold")
|
|
|
|
local face_obj = self.faces[hash]
|
|
|
|
if face_obj then
|
|
|
|
return face_obj, bold
|
|
|
|
end
|
|
|
|
face_obj = {
|
|
|
|
orig_font = face.orig_font,
|
|
|
|
realname = face.realname,
|
|
|
|
size = face.size,
|
|
|
|
orig_size = face.orig_size,
|
|
|
|
-- We can keep the same FT object and the same hash in this face_obj
|
|
|
|
-- (which is only used to identify cached glyphs, that we don't need
|
|
|
|
-- to distinguish as "bold" is appended when synthetized as bold)
|
|
|
|
ftface = face.ftface,
|
|
|
|
hash = face.hash,
|
|
|
|
hb_features = face.hb_features,
|
|
|
|
is_real_bold = nil,
|
|
|
|
wants_bold = bold, -- true or Font.FORCE_SYNTHETIZED_BOLD, used
|
|
|
|
-- to pick the appropritate fallback fonts
|
|
|
|
}
|
|
|
|
face_obj.getFallbackFont = function(num)
|
|
|
|
return _getFallbackFont(face_obj, num)
|
|
|
|
end
|
|
|
|
self.faces[hash] = face_obj
|
|
|
|
return face_obj, bold
|
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return Font
|