From 25f6ee6bec9c7886ff04fa6012149d3fde72e407 Mon Sep 17 00:00:00 2001
From: Paul Wisbey
Date: Mon, 24 Aug 2015 19:20:41 +0100
Subject: [PATCH] Scaling feature for fixed-size fonts
Change-Id: I77dd56483de63e92a9c2927428aeadf030909b98
---
.../devel-api/text-abstraction/font-client.cpp | 8 +--
text/dali/devel-api/text-abstraction/font-client.h | 6 ++-
.../dali/devel-api/text-abstraction/glyph-info.cpp | 6 ++-
text/dali/devel-api/text-abstraction/glyph-info.h | 15 +++---
.../internal/text-abstraction/font-client-impl.cpp | 8 +--
.../internal/text-abstraction/font-client-impl.h | 4 +-
.../text-abstraction/font-client-plugin-impl.cpp | 61 +++++++++++++++++-----
.../text-abstraction/font-client-plugin-impl.h | 4 +-
8 files changed, 75 insertions(+), 37 deletions(-)
diff --git a/text/dali/devel-api/text-abstraction/font-client.cpp b/text/dali/devel-api/text-abstraction/font-client.cpp
index 377e33d..27feb02 100644
--- a/text/dali/devel-api/text-abstraction/font-client.cpp
+++ b/text/dali/devel-api/text-abstraction/font-client.cpp
@@ -132,9 +132,9 @@ void FontClient::GetFixedSizes( const FontFamily& fontFamily,
GetImplementation(*this).GetFixedSizes( fontFamily, style, sizes );
}
-void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics )
+void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics, int maxFixedSize )
{
- GetImplementation(*this).GetFontMetrics( fontId, metrics );
+ GetImplementation(*this).GetFontMetrics( fontId, metrics, maxFixedSize );
}
GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode )
@@ -142,9 +142,9 @@ GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode )
return GetImplementation(*this).GetGlyphIndex( fontId, charcode );
}
-bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal )
+bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal, int maxFixedSize )
{
- return GetImplementation(*this).GetGlyphMetrics( array, size, horizontal );
+ return GetImplementation(*this).GetGlyphMetrics( array, size, horizontal, maxFixedSize );
}
BufferImage FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex )
diff --git a/text/dali/devel-api/text-abstraction/font-client.h b/text/dali/devel-api/text-abstraction/font-client.h
index 03d23a0..f069bd3 100644
--- a/text/dali/devel-api/text-abstraction/font-client.h
+++ b/text/dali/devel-api/text-abstraction/font-client.h
@@ -244,8 +244,9 @@ public:
*
* @param[in] fontId The ID of the font for the required glyph.
* @param[out] metrics The font metrics.
+ * @param[in] maxFixedSize The metrics for fixed-size fonts will be down-scaled, when exceeding this maximum value in pixels.
*/
- void GetFontMetrics( FontId fontId, FontMetrics& metrics );
+ void GetFontMetrics( FontId fontId, FontMetrics& metrics, int maxFixedSize = 0 );
/**
* @brief Retrieve the glyph index for a UTF-32 character code.
@@ -264,9 +265,10 @@ public:
* On return, the glyph's size value will be initialized. The bearing value will be updated by adding the font's glyph bearing to the one set by the shaping tool.
* @param[in] size The size of the array.
* @param[in] horizontal True for horizontal layouts (set to false for vertical layouting).
+ * @param[in] maxFixedSize The metrics for fixed-size fonts will be down-scaled, when exceeding this maximum value in pixels.
* @return True if all of the requested metrics were found.
*/
- bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal = true );
+ bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal = true, int maxFixedSize = 0 );
/**
* @brief Render a bitmap representation of a glyph.
diff --git a/text/dali/devel-api/text-abstraction/glyph-info.cpp b/text/dali/devel-api/text-abstraction/glyph-info.cpp
index 78f18e2..2d7c811 100644
--- a/text/dali/devel-api/text-abstraction/glyph-info.cpp
+++ b/text/dali/devel-api/text-abstraction/glyph-info.cpp
@@ -31,7 +31,8 @@ GlyphInfo::GlyphInfo()
height( 0 ),
xBearing( 0 ),
yBearing( 0 ),
- advance( 0 )
+ advance( 0 ),
+ scaleFactor( 0 )
{
}
@@ -42,7 +43,8 @@ GlyphInfo::GlyphInfo( FontId font, GlyphIndex i )
height( 0 ),
xBearing( 0 ),
yBearing( 0 ),
- advance( 0 )
+ advance( 0 ),
+ scaleFactor( 0 )
{
}
diff --git a/text/dali/devel-api/text-abstraction/glyph-info.h b/text/dali/devel-api/text-abstraction/glyph-info.h
index c901c55..952a302 100644
--- a/text/dali/devel-api/text-abstraction/glyph-info.h
+++ b/text/dali/devel-api/text-abstraction/glyph-info.h
@@ -46,13 +46,14 @@ struct DALI_IMPORT_API GlyphInfo
*/
GlyphInfo( FontId font, GlyphIndex i );
- FontId fontId; ///< Identifies the font containing the glyph
- GlyphIndex index; ///< Uniquely identifies a glyph for a given FontId
- float width; ///< The width of the glyph
- float height; ///< The height of the glyph
- float xBearing; ///< The distance from the cursor position to the leftmost border of the glyph
- float yBearing; ///< The distance from the baseline to the topmost border of the glyph
- float advance; ///< The distance to move the cursor for this glyph
+ FontId fontId; ///< Identifies the font containing the glyph
+ GlyphIndex index; ///< Uniquely identifies a glyph for a given FontId
+ float width; ///< The width of the glyph
+ float height; ///< The height of the glyph
+ float xBearing; ///< The distance from the cursor position to the leftmost border of the glyph
+ float yBearing; ///< The distance from the baseline to the topmost border of the glyph
+ float advance; ///< The distance to move the cursor for this glyph
+ float scaleFactor; ///< The scaling applied (fixed-size fonts only)
};
} // Dali
diff --git a/text/dali/internal/text-abstraction/font-client-impl.cpp b/text/dali/internal/text-abstraction/font-client-impl.cpp
index 9b5d5d2..c53b4cf 100644
--- a/text/dali/internal/text-abstraction/font-client-impl.cpp
+++ b/text/dali/internal/text-abstraction/font-client-impl.cpp
@@ -179,11 +179,11 @@ FontId FontClient::GetFontId( const FontFamily& fontFamily,
faceIndex );
}
-void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics )
+void FontClient::GetFontMetrics( FontId fontId, FontMetrics& metrics, int maxFixedSize )
{
CreatePlugin();
- return mPlugin->GetFontMetrics( fontId, metrics );
+ return mPlugin->GetFontMetrics( fontId, metrics, maxFixedSize );
}
GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode )
@@ -193,11 +193,11 @@ GlyphIndex FontClient::GetGlyphIndex( FontId fontId, Character charcode )
return mPlugin->GetGlyphIndex( fontId, charcode );
}
-bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal )
+bool FontClient::GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal, int maxFixedSize )
{
CreatePlugin();
- return mPlugin->GetGlyphMetrics( array, size, horizontal );
+ return mPlugin->GetGlyphMetrics( array, size, horizontal, maxFixedSize );
}
BufferImage FontClient::CreateBitmap( FontId fontId, GlyphIndex glyphIndex )
diff --git a/text/dali/internal/text-abstraction/font-client-impl.h b/text/dali/internal/text-abstraction/font-client-impl.h
index b221cb9..4e3edec 100644
--- a/text/dali/internal/text-abstraction/font-client-impl.h
+++ b/text/dali/internal/text-abstraction/font-client-impl.h
@@ -134,7 +134,7 @@ public:
/**
* @copydoc Dali::FontClient::GetFontMetrics()
*/
- void GetFontMetrics( FontId fontId, FontMetrics& metrics );
+ void GetFontMetrics( FontId fontId, FontMetrics& metrics, int maxFixedSize );
/**
* @copydoc Dali::FontClient::GetGlyphIndex()
@@ -144,7 +144,7 @@ public:
/**
* @copydoc Dali::FontClient::GetGlyphMetrics()
*/
- bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
+ bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal, int maxFixedSize );
/**
* @copydoc Dali::FontClient::CreateBitmap()
diff --git a/text/dali/internal/text-abstraction/font-client-plugin-impl.cpp b/text/dali/internal/text-abstraction/font-client-plugin-impl.cpp
index 18a4feb..23609ce 100644
--- a/text/dali/internal/text-abstraction/font-client-plugin-impl.cpp
+++ b/text/dali/internal/text-abstraction/font-client-plugin-impl.cpp
@@ -448,15 +448,30 @@ void FontClient::Plugin::ValidateFont( const FontFamily& fontFamily,
FcPatternDestroy( fontFamilyPattern );
}
-
-
void FontClient::Plugin::GetFontMetrics( FontId fontId,
- FontMetrics& metrics )
+ FontMetrics& metrics,
+ int maxFixedSize )
{
if( fontId > 0 &&
fontId-1 < mFontCache.size() )
{
- metrics = mFontCache[fontId-1].mMetrics;
+ const CacheItem& font = mFontCache[fontId-1];
+
+ metrics = font.mMetrics;
+
+ // Adjust the metrics if the fixed-size font should be down-scaled
+ if( font.mIsFixedSizeBitmap &&
+ ( maxFixedSize > 0 ) &&
+ ( font.mFixedHeightPixels > maxFixedSize ) )
+ {
+ float scaleFactor = static_cast(maxFixedSize) / static_cast(font.mFixedHeightPixels);
+
+ metrics.ascender *= scaleFactor;
+ metrics.descender *= scaleFactor;
+ metrics.height *= scaleFactor;
+ metrics.underlinePosition *= scaleFactor;
+ metrics.underlineThickness *= scaleFactor;
+ }
}
else
{
@@ -482,7 +497,8 @@ GlyphIndex FontClient::Plugin::GetGlyphIndex( FontId fontId,
bool FontClient::Plugin::GetGlyphMetrics( GlyphInfo* array,
uint32_t size,
- bool horizontal )
+ bool horizontal,
+ int maxFixedSize )
{
bool success( true );
@@ -493,20 +509,37 @@ bool FontClient::Plugin::GetGlyphMetrics( GlyphInfo* array,
if( fontId > 0 &&
fontId-1 < mFontCache.size() )
{
- FT_Face ftFace = mFontCache[fontId-1].mFreeTypeFace;
+ const CacheItem& font = mFontCache[fontId-1];
+
+ FT_Face ftFace = font.mFreeTypeFace;
#ifdef FREETYPE_BITMAP_SUPPORT
// Check to see if we should be loading a Fixed Size bitmap?
- if ( mFontCache[fontId-1].mIsFixedSizeBitmap )
+ if ( font.mIsFixedSizeBitmap )
{
int error = FT_Load_Glyph( ftFace, array[i].index, FT_LOAD_COLOR );
if ( FT_Err_Ok == error )
{
- array[i].width = mFontCache[ fontId -1 ].mFixedWidthPixels;
- array[i].height = mFontCache[ fontId -1 ].mFixedHeightPixels;
- array[i].advance = mFontCache[ fontId -1 ].mFixedWidthPixels;
+ array[i].width = font.mFixedWidthPixels;
+ array[i].height = font.mFixedHeightPixels;
+ array[i].advance = font.mFixedWidthPixels;
array[i].xBearing = 0.0f;
- array[i].yBearing = mFontCache[ fontId -1 ].mFixedHeightPixels;
+ array[i].yBearing = font.mFixedHeightPixels;
+
+ // Adjust the metrics if the fixed-size font should be down-scaled
+ if( ( maxFixedSize > 0 ) &&
+ ( font.mFixedHeightPixels > maxFixedSize ) )
+ {
+ float scaleFactor = static_cast(maxFixedSize) / static_cast(font.mFixedHeightPixels);
+
+ array[i].width *= scaleFactor;
+ array[i].height *= scaleFactor;
+ array[i].advance *= scaleFactor;
+ array[i].xBearing *= scaleFactor;
+ array[i].yBearing *= scaleFactor;
+
+ array[i].scaleFactor = scaleFactor;
+ }
}
else
{
@@ -643,7 +676,7 @@ const GlyphInfo& FontClient::Plugin::GetEllipsisGlyph( PointSize26Dot6 pointSize
item.glyph.index = FT_Get_Char_Index( mFontCache[item.glyph.fontId-1].mFreeTypeFace,
ELLIPSIS_CHARACTER );
- GetGlyphMetrics( &item.glyph, 1u, true );
+ GetGlyphMetrics( &item.glyph, 1u, true, 0 );
return item.glyph;
}
@@ -792,9 +825,9 @@ FontId FontClient::Plugin::CreateFont( const FontPath& path,
float fixedHeight = static_cast< float >( ftFace->available_sizes[ i ].height );
// Indicate that the font is a fixed sized bitmap
- FontMetrics metrics( fixedHeight,
+ FontMetrics metrics( fixedHeight, // The ascender in pixels.
0.0f,
- fixedHeight,
+ fixedHeight, // The height in pixels.
0.0f,
0.0f );
diff --git a/text/dali/internal/text-abstraction/font-client-plugin-impl.h b/text/dali/internal/text-abstraction/font-client-plugin-impl.h
index 5032ef2..b292178 100644
--- a/text/dali/internal/text-abstraction/font-client-plugin-impl.h
+++ b/text/dali/internal/text-abstraction/font-client-plugin-impl.h
@@ -208,7 +208,7 @@ struct FontClient::Plugin
/**
* @copydoc Dali::FontClient::GetFontMetrics()
*/
- void GetFontMetrics( FontId fontId, FontMetrics& metrics );
+ void GetFontMetrics( FontId fontId, FontMetrics& metrics, int maxFixedSize );
/**
* @copydoc Dali::FontClient::GetGlyphIndex()
@@ -218,7 +218,7 @@ struct FontClient::Plugin
/**
* @copydoc Dali::FontClient::GetGlyphMetrics()
*/
- bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal );
+ bool GetGlyphMetrics( GlyphInfo* array, uint32_t size, bool horizontal, int maxFixedSize );
/**
* @copydoc Dali::FontClient::CreateBitmap()
--
2.7.4