Support for fake bold and italics

This patch adds support for computing when fake bold and fake italics
are needed (because the styles are requested but not provided by the
matching FontFamily), and providing them as part of the layout result.

Part of the fix for bug 15436379 Fake bold doesn't fully work (Minikin)

Change-Id: I180c034b559837943673b5c272c8e890178dff0d
This commit is contained in:
Raph Levien
2014-06-06 17:56:41 -07:00
parent 1e35d09df1
commit 1f8de3019d
7 changed files with 70 additions and 39 deletions

View File

@@ -32,24 +32,8 @@ public:
~FontCollection(); ~FontCollection();
class Run { struct Run {
public: FakedFont fakedFont;
// Do copy constructor, assignment, destructor so it can be used in vectors
Run() : font(NULL) { }
Run(const Run& other): font(other.font), start(other.start), end(other.end) {
if (font) font->RefLocked();
}
Run& operator=(const Run& other) {
if (other.font) other.font->RefLocked();
if (font) font->UnrefLocked();
font = other.font;
start = other.start;
end = other.end;
return *this;
}
~Run() { if (font) font->UnrefLocked(); }
MinikinFont* font;
int start; int start;
int end; int end;
}; };
@@ -60,6 +44,9 @@ public:
// Get the base font for the given style, useful for font-wide metrics. // Get the base font for the given style, useful for font-wide metrics.
MinikinFont* baseFont(FontStyle style); MinikinFont* baseFont(FontStyle style);
// Get base font with fakery information (fake bold could affect metrics)
FakedFont baseFontFaked(FontStyle style);
uint32_t getId() const; uint32_t getId() const;
private: private:
static const int kLogCharsPerPage = 8; static const int kLogCharsPerPage = 8;

View File

@@ -94,6 +94,25 @@ inline hash_t hash_type(const FontStyle &style) {
return style.hash(); return style.hash();
} }
// attributes representing transforms (fake bold, fake italic) to match styles
class FontFakery {
public:
FontFakery() : mFakeBold(false), mFakeItalic(false) { }
FontFakery(bool fakeBold, bool fakeItalic) : mFakeBold(fakeBold), mFakeItalic(fakeItalic) { }
// TODO: want to support graded fake bolding
bool isFakeBold() { return mFakeBold; }
bool isFakeItalic() { return mFakeItalic; }
private:
bool mFakeBold;
bool mFakeItalic;
};
struct FakedFont {
// ownership is the enclosing FontCollection
MinikinFont* font;
FontFakery fakery;
};
class FontFamily : public MinikinRefCounted { class FontFamily : public MinikinRefCounted {
public: public:
FontFamily() { } FontFamily() { }
@@ -107,7 +126,7 @@ public:
bool addFont(MinikinFont* typeface); bool addFont(MinikinFont* typeface);
void addFont(MinikinFont* typeface, FontStyle style); void addFont(MinikinFont* typeface, FontStyle style);
MinikinFont* getClosestMatch(FontStyle style) const; FakedFont getClosestMatch(FontStyle style) const;
FontLanguage lang() const { return mLang; } FontLanguage lang() const { return mLang; }
int variant() const { return mVariant; } int variant() const { return mVariant; }

View File

@@ -87,6 +87,7 @@ public:
size_t nGlyphs() const; size_t nGlyphs() const;
// Does not bump reference; ownership is still layout // Does not bump reference; ownership is still layout
MinikinFont *getFont(int i) const; MinikinFont *getFont(int i) const;
FontFakery getFakery(int i) const;
unsigned int getGlyphId(int i) const; unsigned int getGlyphId(int i) const;
float getX(int i) const; float getX(int i) const;
float getY(int i) const; float getY(int i) const;
@@ -101,7 +102,7 @@ public:
private: private:
// Find a face in the mFaces vector, or create a new entry // Find a face in the mFaces vector, or create a new entry
int findFace(MinikinFont* face, LayoutContext* ctx); int findFace(FakedFont face, LayoutContext* ctx);
// Lay out a single bidi run // Lay out a single bidi run
void doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize, void doLayoutRunCached(const uint16_t* buf, size_t start, size_t count, size_t bufSize,
@@ -125,7 +126,7 @@ private:
std::vector<float> mAdvances; std::vector<float> mAdvances;
const FontCollection* mCollection; const FontCollection* mCollection;
std::vector<MinikinFont *> mFaces; std::vector<FakedFont> mFaces;
float mAdvance; float mAdvance;
MinikinRect mBounds; MinikinRect mBounds;
}; };

View File

@@ -18,6 +18,7 @@
#define MINIKIN_FONT_H #define MINIKIN_FONT_H
#include <minikin/MinikinRefCounted.h> #include <minikin/MinikinRefCounted.h>
#include <minikin/FontFamily.h>
// An abstraction for platform fonts, allowing Minikin to be used with // An abstraction for platform fonts, allowing Minikin to be used with
// multiple actual implementations of fonts. // multiple actual implementations of fonts.
@@ -34,6 +35,7 @@ struct MinikinPaint {
float scaleX; float scaleX;
float skewX; float skewX;
uint32_t paintFlags; uint32_t paintFlags;
FontFakery fakery;
}; };
struct MinikinRect { struct MinikinRect {

View File

@@ -52,7 +52,7 @@ FontCollection::FontCollection(const vector<FontFamily*>& typefaces) :
FontInstance* instance = &mInstances.back(); FontInstance* instance = &mInstances.back();
instance->mFamily = family; instance->mFamily = family;
instance->mCoverage = new SparseBitSet; instance->mCoverage = new SparseBitSet;
MinikinFont* typeface = family->getClosestMatch(defaultStyle); MinikinFont* typeface = family->getClosestMatch(defaultStyle).font;
if (typeface == NULL) { if (typeface == NULL) {
ALOGE("FontCollection: closest match was null"); ALOGE("FontCollection: closest match was null");
// TODO: we shouldn't hit this, as there should be more robust // TODO: we shouldn't hit this, as there should be more robust
@@ -171,11 +171,9 @@ void FontCollection::itemize(const uint16_t *string, size_t string_size, FontSty
result->push_back(dummy); result->push_back(dummy);
run = &result->back(); run = &result->back();
if (instance == NULL) { if (instance == NULL) {
run->font = NULL; // maybe we should do something different here run->fakedFont.font = NULL;
} else { } else {
run->font = instance->mFamily->getClosestMatch(style); run->fakedFont = instance->mFamily->getClosestMatch(style);
// TODO: simplify refcounting (FontCollection lifetime dominates)
run->font->RefLocked();
} }
lastInstance = instance; lastInstance = instance;
run->start = i; run->start = i;
@@ -186,13 +184,16 @@ void FontCollection::itemize(const uint16_t *string, size_t string_size, FontSty
} }
MinikinFont* FontCollection::baseFont(FontStyle style) { MinikinFont* FontCollection::baseFont(FontStyle style) {
return baseFontFaked(style).font;
}
FakedFont FontCollection::baseFontFaked(FontStyle style) {
if (mInstances.empty()) { if (mInstances.empty()) {
return NULL; return FakedFont();
} }
return mInstances[0].mFamily->getClosestMatch(style); return mInstances[0].mFamily->getClosestMatch(style);
} }
uint32_t FontCollection::getId() const { uint32_t FontCollection::getId() const {
return mId; return mId;
} }

View File

@@ -109,7 +109,7 @@ void FontFamily::addFontLocked(MinikinFont* typeface, FontStyle style) { type
} }
// Compute a matching metric between two styles - 0 is an exact match // Compute a matching metric between two styles - 0 is an exact match
int computeMatch(FontStyle style1, FontStyle style2) { static int computeMatch(FontStyle style1, FontStyle style2) {
if (style1 == style2) return 0; if (style1 == style2) return 0;
int score = abs(style1.getWeight() - style2.getWeight()); int score = abs(style1.getWeight() - style2.getWeight());
if (style1.getItalic() != style2.getItalic()) { if (style1.getItalic() != style2.getItalic()) {
@@ -118,7 +118,15 @@ int computeMatch(FontStyle style1, FontStyle style2) {
return score; return score;
} }
MinikinFont* FontFamily::getClosestMatch(FontStyle style) const { static FontFakery computeFakery(FontStyle wanted, FontStyle actual) {
// If desired weight is 2 or more grades higher than actual
// (for example, medium 500 -> bold 700), then select fake bold.
bool isFakeBold = (wanted.getWeight() - actual.getWeight()) >= 2;
bool isFakeItalic = wanted.getItalic() && !actual.getItalic();
return FontFakery(isFakeBold, isFakeItalic);
}
FakedFont FontFamily::getClosestMatch(FontStyle style) const {
const Font* bestFont = NULL; const Font* bestFont = NULL;
int bestMatch = 0; int bestMatch = 0;
for (size_t i = 0; i < mFonts.size(); i++) { for (size_t i = 0; i < mFonts.size(); i++) {
@@ -129,7 +137,14 @@ MinikinFont* FontFamily::getClosestMatch(FontStyle style) const {
bestMatch = match; bestMatch = match;
} }
} }
return bestFont == NULL ? NULL : bestFont->typeface; FakedFont result;
if (bestFont == NULL) {
result.font = NULL;
} else {
result.font = bestFont->typeface;
result.fakery = computeFakery(style, bestFont->style);
}
return result;
} }
size_t FontFamily::getNumFonts() const { size_t FontFamily::getNumFonts() const {

View File

@@ -328,10 +328,10 @@ void Layout::dump() const {
} }
} }
int Layout::findFace(MinikinFont* face, LayoutContext* ctx) { int Layout::findFace(FakedFont face, LayoutContext* ctx) {
unsigned int ix; unsigned int ix;
for (ix = 0; ix < mFaces.size(); ix++) { for (ix = 0; ix < mFaces.size(); ix++) {
if (mFaces[ix] == face) { if (mFaces[ix].font == face.font) {
return ix; return ix;
} }
} }
@@ -339,7 +339,7 @@ int Layout::findFace(MinikinFont* face, LayoutContext* ctx) {
// Note: ctx == NULL means we're copying from the cache, no need to create // Note: ctx == NULL means we're copying from the cache, no need to create
// corresponding hb_font object. // corresponding hb_font object.
if (ctx != NULL) { if (ctx != NULL) {
hb_font_t* font = create_hb_font(face, &ctx->paint); hb_font_t* font = create_hb_font(face.font, &ctx->paint);
ctx->hbFonts.push_back(font); ctx->hbFonts.push_back(font);
} }
return ix; return ix;
@@ -631,12 +631,13 @@ void Layout::doLayoutRun(const uint16_t* buf, size_t start, size_t count, size_t
float y = 0; float y = 0;
for (size_t run_ix = 0; run_ix < items.size(); run_ix++) { for (size_t run_ix = 0; run_ix < items.size(); run_ix++) {
FontCollection::Run &run = items[run_ix]; FontCollection::Run &run = items[run_ix];
if (run.font == NULL) { if (run.fakedFont.font == NULL) {
ALOGE("no font for run starting u+%04x length %d", buf[run.start], run.end - run.start); ALOGE("no font for run starting u+%04x length %d", buf[run.start], run.end - run.start);
continue; continue;
} }
int font_ix = findFace(run.font, ctx); int font_ix = findFace(run.fakedFont, ctx);
ctx->paint.font = mFaces[font_ix]; ctx->paint.font = mFaces[font_ix].font;
ctx->paint.fakery = mFaces[font_ix].fakery;
hb_font_t* hbFont = ctx->hbFonts[font_ix]; hb_font_t* hbFont = ctx->hbFonts[font_ix];
#ifdef VERBOSE #ifdef VERBOSE
std::cout << "Run " << run_ix << ", font " << font_ix << std::cout << "Run " << run_ix << ", font " << font_ix <<
@@ -729,7 +730,7 @@ void Layout::draw(Bitmap* surface, int x0, int y0, float size) const {
*/ */
for (size_t i = 0; i < mGlyphs.size(); i++) { for (size_t i = 0; i < mGlyphs.size(); i++) {
const LayoutGlyph& glyph = mGlyphs[i]; const LayoutGlyph& glyph = mGlyphs[i];
MinikinFont* mf = mFaces[glyph.font_ix]; MinikinFont* mf = mFaces[glyph.font_ix].font;
MinikinFontFreeType* face = static_cast<MinikinFontFreeType*>(mf); MinikinFontFreeType* face = static_cast<MinikinFontFreeType*>(mf);
GlyphBitmap glyphBitmap; GlyphBitmap glyphBitmap;
MinikinPaint paint; MinikinPaint paint;
@@ -754,7 +755,12 @@ size_t Layout::nGlyphs() const {
MinikinFont* Layout::getFont(int i) const { MinikinFont* Layout::getFont(int i) const {
const LayoutGlyph& glyph = mGlyphs[i]; const LayoutGlyph& glyph = mGlyphs[i];
return mFaces[glyph.font_ix]; return mFaces[glyph.font_ix].font;
}
FontFakery Layout::getFakery(int i) const {
const LayoutGlyph& glyph = mGlyphs[i];
return mFaces[glyph.font_ix].fakery;
} }
unsigned int Layout::getGlyphId(int i) const { unsigned int Layout::getGlyphId(int i) const {