diff options
author | Jiang Jiang <jiang.jiang@nokia.com> | 2011-02-22 14:15:43 +0100 |
---|---|---|
committer | Jiang Jiang <jiang.jiang@nokia.com> | 2011-03-22 12:03:51 +0100 |
commit | 037e632d4b3884d06bf9e92de77d726c75fe7898 (patch) | |
tree | 6834662159359112e2b2b8acf43f574990dda0e3 /src/gui/text/qfontengine_ft_p.h | |
parent | 23098630c94e6655a33042bee0caa6c933c0c7d9 (diff) | |
download | qt4-tools-037e632d4b3884d06bf9e92de77d726c75fe7898.tar.gz |
Implement subpixel positioning with FreeType
QFontEngineFT are used in raster/OpenGL paint engine and QGLWidget,
also in Symbian, etc. We want to make sure it works well in raster
and QGLWidget first. Regardless subpixel antialiasing (LCD filtering)
is enabled or not (though it does look better when subpixel
antialiasing is on). We also need to support transformations.
The tricky part here is that, under X11, we have a different code
path for QFontEngineFT and other font engines in raster engine,
which uses QFontEngineFT's own glyph cache system. While in other
platforms (Windows and Mac) and QGLWidget, we will use the generic
QTextureGlyphCache.
The generic QTextureGlyphCache already has support for subpixel
positions, this solution is ported to QFontEngineFT for its
QGlyphSet: the key for QGlyphSet hash table has been extended from
glyph_t to <glyph_t, QFixed subPixelPosition> pair.
The real work to enable subpixel positioning with FreeType is in
fact really simple, we just set the horizontal translation to the
subpixel position * 64 (FreeType uses a coordinate system of 1/64
of a pixel resolution internally) immediately before loading the
glyph. A slight tweek to bitmap width is applied when we are
getting the bitmap ourselves instead of using FT_Render_Glyph to
accommodate the subpixel translation, which will only be used in
non-LCD filtering cases (grayscale antialiasing).
From what we have observed, FreeType can generate different bitmaps
for at least 12 different subpixel positions (each with a slight
difference). To limit the memory consumption, we restrict the
number of subpixel positions to be 4 (hardcoded), which should be
good enough for most low resolution displays.
Subpixel positioning (and fractional glyph advances) will only be
enabled when the hintingPreference for the font being used is
PreferNoHinting or PreferVerticalHinting. We will use fontconfig
hintstyle setting by default when no hintingPreference is set for
the font.
Task-number: QTBUG-12279
Reviewed-by: Eskil
Diffstat (limited to 'src/gui/text/qfontengine_ft_p.h')
-rw-r--r-- | src/gui/text/qfontengine_ft_p.h | 52 |
1 files changed, 41 insertions, 11 deletions
diff --git a/src/gui/text/qfontengine_ft_p.h b/src/gui/text/qfontengine_ft_p.h index abdbd2099b..63fd9a7634 100644 --- a/src/gui/text/qfontengine_ft_p.h +++ b/src/gui/text/qfontengine_ft_p.h @@ -166,6 +166,19 @@ public: }; #endif + struct GlyphAndSubPixelPosition + { + GlyphAndSubPixelPosition(glyph_t g, QFixed spp) : glyph(g), subPixelPosition(spp) {} + + bool operator==(const GlyphAndSubPixelPosition &other) const + { + return glyph == other.glyph && subPixelPosition == other.subPixelPosition; + } + + glyph_t glyph; + QFixed subPixelPosition; + }; + struct QGlyphSet { QGlyphSet(); @@ -174,18 +187,21 @@ public: unsigned long id; // server sided id, GlyphSet for X11 bool outline_drawing; - void removeGlyphFromCache(int index); + void removeGlyphFromCache(glyph_t index, QFixed subPixelPosition); void clear(); - inline Glyph *getGlyph(int index) const + inline bool useFastGlyphData(glyph_t index, QFixed subPixelPosition) const { + return (index < 256 && subPixelPosition == 0); + } + inline Glyph *getGlyph(glyph_t index, QFixed subPixelPosition = 0) const { - if (index < 256) + if (useFastGlyphData(index, subPixelPosition)) return fast_glyph_data[index]; - return glyph_data.value(index); + return glyph_data.value(GlyphAndSubPixelPosition(index, subPixelPosition)); } - void setGlyph(int index, Glyph *glyph); + void setGlyph(glyph_t index, QFixed spp, Glyph *glyph); private: - mutable QHash<int, Glyph *> glyph_data; // maps from glyph index to glyph data + mutable QHash<GlyphAndSubPixelPosition, Glyph *> glyph_data; // maps from glyph index to glyph data mutable Glyph *fast_glyph_data[256]; // for fast lookup of glyphs < 256 mutable int fast_glyph_count; }; @@ -193,6 +209,11 @@ private: virtual QFontEngine::FaceId faceId() const; virtual QFontEngine::Properties properties() const; virtual QFixed emSquareSize() const; + virtual bool supportsSubPixelPositions() const + { + return default_hint_style == HintLight || + default_hint_style == HintNone; + } virtual bool getSfntTableData(uint tag, uchar *buffer, uint *length) const; virtual int synthesized() const; @@ -254,17 +275,20 @@ private: inline bool invalid() const { return xsize == 0 && ysize == 0; } inline bool isBitmapFont() const { return defaultFormat == Format_Mono; } - inline Glyph *loadGlyph(uint glyph, GlyphFormat format = Format_None, bool fetchMetricsOnly = false) const - { return loadGlyph(&defaultGlyphSet, glyph, format, fetchMetricsOnly); } - Glyph *loadGlyph(QGlyphSet *set, uint glyph, GlyphFormat = Format_None, bool fetchMetricsOnly = false) const; + inline Glyph *loadGlyph(uint glyph, QFixed subPixelPosition, GlyphFormat format = Format_None, bool fetchMetricsOnly = false) const + { return loadGlyph(&defaultGlyphSet, glyph, subPixelPosition, format, fetchMetricsOnly); } + Glyph *loadGlyph(QGlyphSet *set, uint glyph, QFixed subPixelPosition, GlyphFormat = Format_None, bool fetchMetricsOnly = false) const; QGlyphSet *defaultGlyphs() { return &defaultGlyphSet; } GlyphFormat defaultGlyphFormat() const { return defaultFormat; } - inline Glyph *cachedGlyph(glyph_t g) const { return defaultGlyphSet.getGlyph(g); } + inline Glyph *cachedGlyph(glyph_t g) const { return defaultGlyphSet.getGlyph(g, 0); } QGlyphSet *loadTransformedGlyphSet(const QTransform &matrix); - bool loadGlyphs(QGlyphSet *gs, glyph_t *glyphs, int num_glyphs, GlyphFormat format = Format_Render); + QFixed subPixelPositionForX(QFixed x); + bool loadGlyphs(QGlyphSet *gs, glyph_t *glyphs, int num_glyphs, + QVarLengthArray<QFixedPoint> &positions, + GlyphFormat format = Format_Render); #if defined(Q_WS_QWS) || defined(Q_OS_SYMBIAN) virtual void draw(QPaintEngine * /*p*/, qreal /*x*/, qreal /*y*/, const QTextItemInt & /*si*/) {} @@ -309,6 +333,7 @@ protected: private: QFontEngineFT::Glyph *loadGlyphMetrics(QGlyphSet *set, uint glyph, GlyphFormat format) const; + int loadFlags(QGlyphSet *set, GlyphFormat format, int flags, bool &hsubpixel, int &vfactor) const; GlyphFormat defaultFormat; FT_Matrix matrix; @@ -330,6 +355,11 @@ private: mutable bool kerning_pairs_loaded; }; +inline uint qHash(const QFontEngineFT::GlyphAndSubPixelPosition &g) +{ + return (g.glyph << 8) | (g.subPixelPosition * 10).round().toInt(); +} + QT_END_NAMESPACE #endif // QT_NO_FREETYPE |