Skip to content

Commit

Permalink
Merge pull request #278 from virxkane/font-weight-fixes
Browse files Browse the repository at this point in the history
Font weight fixes
  • Loading branch information
buggins committed Apr 16, 2021
2 parents 8dfe81f + 1617315 commit 9d5f9db
Show file tree
Hide file tree
Showing 16 changed files with 175 additions and 132 deletions.
15 changes: 10 additions & 5 deletions android/src/org/coolreader/crengine/OptionsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ public static int findBacklightSettingIndex( int value ) {
public static int[] mPagesPerFullSwipe;
public static String[] mPagesPerFullSwipeTitles;
int[] mInterlineSpaces = new int[] {
80, 85, 90, 95, 100, 105, 110, 115, 120, 130, 140, 150, 160, 180, 200
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 125, 130, 135, 140, 145, 150, 155, 160, 165,
170, 175, 180, 185, 190, 195, 200
};
int[] mMinSpaceWidths = new int[] {
25, 30, 40, 50, 60, 70, 80, 90, 100
Expand Down Expand Up @@ -2543,8 +2548,8 @@ private void updateFontWeightValues(ListOption option, String faceName) {
option.add(weight, label);
}
// enable/disable font hinting option
int base_weight = mProperties.getInt(PROP_FONT_BASE_WEIGHT, 400);
mFontHintingOption.setEnabled(nativeWeightsArray.contains(base_weight));
//int base_weight = mProperties.getInt(PROP_FONT_BASE_WEIGHT, 400);
//mFontHintingOption.setEnabled(nativeWeightsArray.contains(base_weight));
}

private void setupReaderOptions()
Expand Down Expand Up @@ -2582,8 +2587,8 @@ private void setupReaderOptions()
ArrayList<Integer> nativeWeightsArray = new ArrayList<>(); // for search
for (int w : nativeWeights)
nativeWeightsArray.add(w);
int base_weight = mProperties.getInt(PROP_FONT_BASE_WEIGHT, 400);
mFontHintingOption.setEnabled(nativeWeightsArray.contains(base_weight));
//int base_weight = mProperties.getInt(PROP_FONT_BASE_WEIGHT, 400);
//mFontHintingOption.setEnabled(nativeWeightsArray.contains(base_weight));
}
});
mOptionsStyles.add(new ListOption(this, getString(R.string.options_font_antialias), PROP_FONT_ANTIALIASING).add(mAntialias, mAntialiasTitles).setDefaultValue("2").setIconIdByAttr(R.attr.cr3_option_text_antialias_drawable, R.drawable.cr3_option_text_antialias));
Expand Down
2 changes: 1 addition & 1 deletion cr3qt/src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,6 @@ void SettingsDlg::on_cbFontWeightChange_currentIndexChanged(int index)
QString face = m_props->getStringDef( PROP_FONT_FACE, "" );
LVArray<int> nativeWeights;
fontMan->GetAvailableFontWeights(nativeWeights, UnicodeToUtf8(qt2cr(face)));
m_ui->cbFontHinting->setEnabled(nativeWeights.indexOf(weight) >= 0);
//m_ui->cbFontHinting->setEnabled(nativeWeights.indexOf(weight) >= 0);
updateStyleSample();
}
2 changes: 1 addition & 1 deletion crengine/include/lvfntman.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class LVFontManager {
/// registers font by name
virtual bool RegisterFont( lString8 name ) = 0;
/// registers font by name and face
virtual bool RegisterExternalFont(lString32 /*name*/, lString8 /*face*/, bool /*bold*/, bool /*italic*/) { return false; }
virtual bool RegisterExternalFont(int /*documentId*/, lString32 /*name*/, lString8 /*face*/, bool /*bold*/, bool /*italic*/) { return false; }
/// registers document font
virtual bool
RegisterDocumentFont(int /*documentId*/, LVContainerRef /*container*/, lString32 /*name*/,
Expand Down
3 changes: 2 additions & 1 deletion crengine/src/epubfmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,7 +820,8 @@ class EmbeddedFontStyleParser {
state = 2;
else if (state == 1) { // ex. [<secure/_stdio.h> or 5/3]
tmp<<('/');
state = 0;
if (c != ('/')) // stay in state 1 if [//]
state = 0;
}
else if (state == 2 && c == ('*')) // ex. [/*he*]
state = 3;
Expand Down
13 changes: 10 additions & 3 deletions crengine/src/lvdocview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6276,7 +6276,14 @@ int LVDocView::onSelectionCommand( int cmd, int param )
}

//static int cr_font_sizes[] = { 24, 29, 33, 39, 44 };
static int cr_interline_spaces[] = { 100, 70, 75, 80, 85, 90, 95, 100, 105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 160, 180, 200 };
static int cr_interline_spaces[] = {
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
120, 125, 130, 135, 140, 145, 150, 155, 160, 165,
170, 175, 180, 185, 190, 195, 200
};

static const char * def_style_macros[] = {
"styles.def.align", "text-align: justify",
Expand Down Expand Up @@ -6399,7 +6406,7 @@ void LVDocView::propsUpdateDefaults(CRPropRef props) {
props->setIntDef(PROP_FONT_SIZE, m_min_font_size + (m_min_font_size + m_max_font_size)/7);
#endif
props->limitValueList(PROP_INTERLINE_SPACE, cr_interline_spaces,
sizeof(cr_interline_spaces) / sizeof(int));
sizeof(cr_interline_spaces) / sizeof(int), 20);
#if CR_INTERNAL_PAGE_ORIENTATION==1
static int def_rot_angle[] = {0, 1, 2, 3};
props->limitValueList( PROP_ROTATE_ANGLE, def_rot_angle, 4 );
Expand Down Expand Up @@ -6748,7 +6755,7 @@ CRPropRef LVDocView::propsApply(CRPropRef props) {
}
} else if (name == PROP_INTERLINE_SPACE) {
int interlineSpace = props->getIntDef(PROP_INTERLINE_SPACE,
cr_interline_spaces[0]);
cr_interline_spaces[20]);
setDefaultInterlineSpace(interlineSpace);//cr_font_sizes
value = lString32::itoa(m_def_interline_space);
#if CR_INTERNAL_PAGE_ORIENTATION==1
Expand Down
2 changes: 1 addition & 1 deletion crengine/src/lvtinydom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18175,7 +18175,7 @@ void ldomDocument::registerEmbeddedFonts()
continue;
}
if (url.startsWithNoCase(lString32("res://")) || url.startsWithNoCase(lString32("file://"))) {
if (!fontMan->RegisterExternalFont(item->getUrl(), item->getFace(), item->getBold(), item->getItalic())) {
if (!fontMan->RegisterExternalFont(getDocIndex(), item->getUrl(), item->getFace(), item->getBold(), item->getItalic())) {
//CRLog::error("Failed to register external font face: %s file: %s", item->getFace().c_str(), LCSTR(item->getUrl()));
}
continue;
Expand Down
2 changes: 1 addition & 1 deletion crengine/src/private/lvbasefont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int LVBaseFont::DrawTextString(LVDrawBuf * buf, int x, int y,
int w = 0;
if (item) {
// avoid soft hyphens inside text string
w = item->advance_26_6 >> 6;
w = item->advance;
if (item->bmp_width && item->bmp_height) {
buf->Draw(x + item->origin_x,
y + baseline - item->origin_y,
Expand Down
4 changes: 2 additions & 2 deletions crengine/src/private/lvfontboldtransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ LVFontGlyphCacheItem *LVFontBoldTransform::getGlyph(lUInt32 ch, lChar32 def_char

item = LVFontGlyphCacheItem::newItem(&_glyph_cache, (lChar32)ch, dx, dy); //, _drawMonochrome
if (item) {
item->advance_26_6 = olditem->advance_26_6 + (_hShift << 6);
item->advance = olditem->advance + _hShift;
item->origin_x = olditem->origin_x;
item->origin_y = olditem->origin_y;

Expand Down Expand Up @@ -183,7 +183,7 @@ int LVFontBoldTransform::DrawTextString(LVDrawBuf *buf, int x, int y, const lCha
int w = 0;
if ( item ) {
// avoid soft hyphens inside text string
w = item->advance_26_6 >> 6;
w = item->advance;
if ( item->bmp_width && item->bmp_height && (!isHyphen || i==len) ) {
buf->Draw( x + item->origin_x,
y + _baseline - item->origin_y,
Expand Down
11 changes: 10 additions & 1 deletion crengine/src/private/lvfontdef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ int LVFontDef::CalcMatch(const LVFontDef &def, bool useBias) const {
weight_diff = 800;
int weight_match = (_weight == -1 || def._weight == -1) ? 256
: (256 - weight_diff * 256 / 800);
// It might happen that 2 fonts with different weights can get the same
// score, e.g. with def._weight=550, a font with _weight=400 and an other
// with _weight=700. Any could then be picked depending on their random
// ordering in the cache, which may mess a book on re-openings.
// To avoid this inconsistency, we give arbitrarily a small increase to
// the score of the smaller weight font (mostly so that with the above
// case, we keep synthesizing the 550 from the 400)
if ( _weight < def._weight )
weight_match += 1;
int italic_match = (_italic == def._italic || _italic == -1 || def._italic == -1) ? 256 : 0;
if ((_italic == 2 || def._italic == 2) && _italic > 0 && def._italic > 0)
italic_match = 128;
Expand All @@ -63,7 +72,7 @@ int LVFontDef::CalcMatch(const LVFontDef &def, bool useBias) const {
// bias
int bias = useBias ? _bias : 0;

// Special handling for synthetized fonts:
// Special handling for synthesized fonts:
// The way this function is called:
// 'this' (or '', properties not prefixed) is either an instance of a
// registered font, or a registered font definition,
Expand Down
13 changes: 13 additions & 0 deletions crengine/src/private/lvfontdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
#include "../../include/cssdef.h"
#include "../../include/lvarray.h"

// LVFontDef carries a font definition, and can be used to identify:
// - registered fonts, from available font files (size=-1 if scalable)
// - instantiated fonts from one of the registered fonts, with some
// updated properties:
// - the specific size, > -1
// - _italic=2 (if font has no real italic, and it is synthesized
// thanks to Freetype from the regular font glyphs)
// - _weight=600 (updated weight if synthesized weight made from
// the regular font glyphs)
// It can be used as a key by caches to retrieve a registered font
// or an instantiated one, and as a query to find in the cache an
// exact or an approximate font.

/**
@brief Font properties definition
*/
Expand Down
2 changes: 1 addition & 1 deletion crengine/src/private/lvfontglyphcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ LVFontGlyphCacheItem *LVFontGlyphCacheItem::newItem(LVFontLocalGlyphCache* local
item->bmp_height = (lUInt16) h;
item->origin_x = 0;
item->origin_y = 0;
item->advance_26_6 = 0;
item->advance = 0;
item->prev_global = NULL;
item->next_global = NULL;
item->prev_local = NULL;
Expand Down
2 changes: 1 addition & 1 deletion crengine/src/private/lvfontglyphcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ struct LVFontGlyphCacheItem {
lUInt16 bmp_height;
lInt16 origin_x;
lInt16 origin_y;
lUInt32 advance_26_6;
lUInt16 advance;
lUInt8 bmp[1];

//=======================================================================
Expand Down
Loading

0 comments on commit 9d5f9db

Please sign in to comment.