Skip to content

Commit

Permalink
Fixed|libgui|Font: Text transformations for fonts
Browse files Browse the repository at this point in the history
After the improved font caching, transformations must be tracked
like any other font property.
  • Loading branch information
skyjake committed Feb 5, 2017
1 parent 125486a commit a80e99d
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
19 changes: 12 additions & 7 deletions doomsday/sdk/libgui/include/de/text/nativefont.h
Expand Up @@ -40,11 +40,7 @@ namespace de {
class LIBGUI_PUBLIC NativeFont : public Asset
{
public:
enum Style
{
Regular,
Italic
};
enum Style { Regular, Italic };

enum Weight
{
Expand All @@ -55,19 +51,26 @@ class LIBGUI_PUBLIC NativeFont : public Asset
Black = 100
};

enum Transform { NoTransform, Uppercase, Lowercase };

struct Spec
{
Style style;
dint weight;
Transform transform;

Spec(Style s = Regular, dint w = Normal) : style(s), weight(w) {}
Spec(Style s = Regular, dint w = Normal, Transform xform = NoTransform)
: style(s), weight(w), transform(xform) {}

bool operator == (Spec const &other) const {
return style == other.style && weight == other.weight;
return style == other.style && weight == other.weight && transform == other.transform;
}
bool operator < (Spec const &other) const { // QMap key order
if (weight < other.weight) return true;
if (weight > other.weight) return false;
if (style == other.style) {
return transform < other.transform;
}
return (style < other.style);
}
};
Expand All @@ -91,11 +94,13 @@ class LIBGUI_PUBLIC NativeFont : public Asset
void setSize(dfloat size);
void setStyle(Style style);
void setWeight(dint weight);
void setTransform(Transform transform);

String family() const;
dfloat size() const;
Style style() const;
dint weight() const;
Transform transform() const;

/**
* Determines the native font name based on style mappings.
Expand Down
20 changes: 6 additions & 14 deletions doomsday/sdk/libgui/src/text/coretextnativefont_macx.cpp
Expand Up @@ -161,8 +161,6 @@ static CoreTextFontCache fontCache;

DENG2_PIMPL(CoreTextNativeFont)
{
enum Transformation { NoTransform, Uppercase, Lowercase };

CTFontRef font;
float ascent;
float descent;
Expand Down Expand Up @@ -212,16 +210,13 @@ DENG2_PIMPL(CoreTextNativeFont)
};
Cache cache;

Transformation xform;

Impl(Public *i)
: Base(i)
, font(0)
, ascent(0)
, descent(0)
, height(0)
, lineSpacing(0)
, xform(NoTransform)
{}

Impl(Public *i, Impl const &other)
Expand All @@ -231,7 +226,6 @@ DENG2_PIMPL(CoreTextNativeFont)
, descent(other.descent)
, height(other.height)
, lineSpacing(other.lineSpacing)
, xform(other.xform)
{}

~Impl()
Expand All @@ -241,7 +235,7 @@ DENG2_PIMPL(CoreTextNativeFont)

String applyTransformation(String const &str) const
{
switch (xform)
switch (self().transform())
{
case Uppercase:
return str.toUpper();
Expand Down Expand Up @@ -317,13 +311,11 @@ CoreTextNativeFont::CoreTextNativeFont(String const &family)
CoreTextNativeFont::CoreTextNativeFont(QFont const &font)
: NativeFont(font.family()), d(new Impl(this))
{
setSize (font.pointSizeF());
setWeight(font.weight());
setStyle (font.italic()? Italic : Regular);

d->xform = (font.capitalization() == QFont::AllUppercase? Impl::Uppercase :
font.capitalization() == QFont::AllLowercase? Impl::Lowercase :
Impl::NoTransform);
setSize (font.pointSizeF());
setWeight (font.weight());
setStyle (font.italic()? Italic : Regular);
setTransform(font.capitalization() == QFont::AllUppercase? Uppercase :
font.capitalization() == QFont::AllLowercase? Lowercase : NoTransform);
}

CoreTextNativeFont::CoreTextNativeFont(CoreTextNativeFont const &other)
Expand Down
13 changes: 8 additions & 5 deletions doomsday/sdk/libgui/src/text/font.cpp
Expand Up @@ -46,10 +46,11 @@ namespace internal

FontParams(PlatformFont const &font)
{
family = font.family();
size = font.size();
spec.weight = font.weight();
spec.style = font.style();
family = font.family();
size = font.size();
spec.weight = font.weight();
spec.style = font.style();
spec.transform = font.transform();
}

bool operator == (FontParams const &other) const
Expand All @@ -65,7 +66,8 @@ namespace internal
return ::qHash(params.family)
^ ::qHash(int(100 * params.size))
^ ::qHash(params.spec.weight)
^ ::qHash(int(params.spec.style));
^ ::qHash(int(params.spec.style))
^ uint(params.spec.transform);
}
}

Expand Down Expand Up @@ -151,6 +153,7 @@ DENG2_PIMPL(Font), public Lockable
mod->setSize(params.size);
mod->setStyle(params.spec.style);
mod->setWeight(params.spec.weight);
mod->setTransform(params.spec.transform);
fontMods.insert(params, mod);
return *mod;
}
Expand Down
22 changes: 18 additions & 4 deletions doomsday/sdk/libgui/src/text/nativefont.cpp
Expand Up @@ -32,6 +32,7 @@ DENG2_PIMPL(NativeFont)
dfloat size;
Style style;
int weight;
Transform transform;

Lockable mutex;
QHash<QString, Rectanglei> measureCache;
Expand All @@ -41,6 +42,7 @@ DENG2_PIMPL(NativeFont)
, size(12.f)
, style(Regular)
, weight(Normal)
, transform(NoTransform)
{}

void prepare()
Expand Down Expand Up @@ -77,10 +79,11 @@ NativeFont::NativeFont(NativeFont const &other) : Asset(other), d(new Impl(this)

NativeFont &NativeFont::operator = (NativeFont const &other)
{
d->family = other.d->family;
d->style = other.d->style;
d->size = other.d->size;
d->weight = other.d->weight;
d->family = other.d->family;
d->style = other.d->style;
d->size = other.d->size;
d->weight = other.d->weight;
d->transform = other.d->transform;
d->markNotReady();
return *this;
}
Expand Down Expand Up @@ -109,6 +112,12 @@ void NativeFont::setWeight(dint weight)
d->markNotReady();
}

void NativeFont::setTransform(de::NativeFont::Transform transform)
{
d->transform = transform;
d->markNotReady();
}

String NativeFont::family() const
{
return d->family;
Expand All @@ -129,6 +138,11 @@ dint NativeFont::weight() const
return d->weight;
}

NativeFont::Transform NativeFont::transform() const
{
return d->transform;
}

String NativeFont::nativeFontName() const
{
// Check the defined mappings.
Expand Down
6 changes: 5 additions & 1 deletion doomsday/sdk/libgui/src/text/qtnativefont.cpp
Expand Up @@ -13,7 +13,7 @@
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details. You should have received a copy of
* the GNU Lesser General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
* http://www.gnu.org/licenses</small>
*/

#include "../src/text/qtnativefont.h"
Expand Down Expand Up @@ -44,6 +44,8 @@ QtNativeFont::QtNativeFont(QFont const &font)
setSize(font.pointSizeF());
setWeight(font.weight());
setStyle(font.italic()? Italic : Regular);
setTransform(font.capitalization() == QFont::AllUppercase? Uppercase :
font.capitalization() == QFont::AllLowercase? Lowercase : NoTransform);
}

QtNativeFont::QtNativeFont(QtNativeFont const &other)
Expand All @@ -65,6 +67,8 @@ void QtNativeFont::commit() const
d->font.setPointSizeF(size());
d->font.setStyle(style() == Italic? QFont::StyleItalic : QFont::StyleNormal);
d->font.setWeight(weight());
d->font.setCapitalization(transform() == Uppercase? QFont::AllUppercase :
transform() == Lowercase? QFont::AllLowercase : QFont::MixedCase);

d->metrics.reset(new QFontMetrics(d->font));
}
Expand Down

0 comments on commit a80e99d

Please sign in to comment.