src/hbcore/utils/hbfontspec.cpp
branchGCC_SURGE
changeset 15 f378acbc9cfb
parent 7 923ff622b8b9
child 23 e6ad4ef83b23
equal deleted inserted replaced
9:730c025d4b77 15:f378acbc9cfb
    31 
    31 
    32 #include <QVariant>
    32 #include <QVariant>
    33 #include <QFont>
    33 #include <QFont>
    34 
    34 
    35 /*!
    35 /*!
    36 	@stable
    36     @stable
    37     @hbcore
    37     @hbcore
    38     \class HbFontSpec
    38     \class HbFontSpec
    39     \brief HbFontSpec is used to request a system font.
    39     \brief HbFontSpec is used to request a system font.
    40 
    40 
    41     It introduces a list of font roles, which can be used to request system
    41     It introduces a list of font roles, which can be used to request system
   106 
   106 
   107     HbFontSpecPrivate();
   107     HbFontSpecPrivate();
   108     ~HbFontSpecPrivate();
   108     ~HbFontSpecPrivate();
   109 
   109 
   110     qreal textHeight() const;
   110     qreal textHeight() const;
   111     QFont font() const;
   111     QFont font();
   112 
   112 
   113 public: // data
   113 public: // data
   114     HbFontSpec::Role mRole;
   114     HbFontSpec::Role mRole;
   115     mutable qreal mTextHeight;
   115     mutable qreal mTextHeight;
       
   116     QString mFontName;
   116 };
   117 };
   117 
   118 
   118 /*!
   119 /*!
   119 \internal
   120 \internal
   120 */
   121 */
   121 HbFontSpecPrivate::HbFontSpecPrivate() :
   122 HbFontSpecPrivate::HbFontSpecPrivate() :
   122     mRole(HbFontSpec::Undefined),
   123     mRole(HbFontSpec::Undefined),
   123     mTextHeight(-1.0f)
   124     mTextHeight(-1.0f),
       
   125     mFontName("")
   124 {
   126 {
   125 }
   127 }
   126 
   128 
   127 /*!
   129 /*!
   128 \internal
   130 \internal
   140 qreal HbFontSpecPrivate::textHeight() const
   142 qreal HbFontSpecPrivate::textHeight() const
   141 {
   143 {
   142 #ifdef HB_BOOTSTRAPPED
   144 #ifdef HB_BOOTSTRAPPED
   143     return 0;
   145     return 0;
   144 #else
   146 #else
   145     if ( (mRole != HbFontSpec::Undefined) && (mTextHeight < 0) ) {
   147     if ((mRole != HbFontSpec::Undefined) && (mTextHeight < 0)) {
   146         qreal parameterValue;
   148         qreal parameterValue;
   147         QString parameterName;
   149         QString parameterName;
   148         switch (mRole){
   150         switch (mRole) {
   149             case HbFontSpec::Primary:
   151         case HbFontSpec::Primary:
   150                 parameterName = "hb-param-text-height-primary";
   152             parameterName = "hb-param-text-height-primary";
   151                 break;
   153             break;
   152             case HbFontSpec::Secondary:
   154         case HbFontSpec::Secondary:
   153                 parameterName = "hb-param-text-height-secondary";
   155             parameterName = "hb-param-text-height-secondary";
   154                 break;
   156             break;
   155             case HbFontSpec::Title:    
   157         case HbFontSpec::Title:
   156                 parameterName = "hb-param-text-height-title";
   158             parameterName = "hb-param-text-height-title";
   157                 break;
   159             break;
   158             case HbFontSpec::PrimarySmall:
   160         case HbFontSpec::PrimarySmall:
   159                 parameterName = "hb-param-text-height-tiny";
   161             parameterName = "hb-param-text-height-tiny";
   160                 break;
   162             break;
   161             case HbFontSpec::Digital:
   163         case HbFontSpec::Digital:
   162             default: // Usage of Secondary as the default system typeface
   164         default: // Usage of Secondary as the default system typeface
   163                 parameterName = "hb-param-text-height-secondary";
   165             parameterName = "hb-param-text-height-secondary";
   164                 break;
   166             break;
   165         }
   167         }
   166         HbInstance::instance()->style()->parameter( parameterName, parameterValue );
   168         HbInstance::instance()->style()->parameter(parameterName, parameterValue);
   167         mTextHeight = parameterValue;
   169         mTextHeight = parameterValue;
   168     }
   170     }
   169 	return mTextHeight;
   171     return mTextHeight;
   170 #endif
   172 #endif
   171 }
   173 }
   172 
   174 
   173 /*!
   175 /*!
   174 \internal
   176 \internal
   175 */
   177 */
   176 QFont HbFontSpecPrivate::font() const
   178 QFont HbFontSpecPrivate::font()
   177 {
   179 {
   178 #ifdef HB_BOOTSTRAPPED
   180 #ifdef HB_BOOTSTRAPPED
   179     return QFont();
   181     return QFont();
   180 #else
   182 #else
   181     if ( mRole == HbFontSpec::Undefined ) {
   183     if (mRole == HbFontSpec::Undefined && mFontName.isEmpty()) {
   182         return QFont();
   184         return QFont();
   183     }
   185     }
   184     QString typefaceFamily;
   186     QString typefaceFamily(mFontName);
   185     int weight;
   187     int weight(QFont::Normal);
   186 
   188 
   187     HbTypefaceInfo *tInfo = HbInstancePrivate::d_ptr()->typefaceInfo(); // Non-owning pointer
   189     HbTypefaceInfo *tInfo = HbInstancePrivate::d_ptr()->typefaceInfo(); // Non-owning pointer
   188 
   190 
       
   191     if (mRole != HbFontSpec::Undefined) {
   189     tInfo->roleToTypeface(mRole, typefaceFamily, weight);
   192     tInfo->roleToTypeface(mRole, typefaceFamily, weight);
       
   193 		mFontName = typefaceFamily;
       
   194 	} else if (!tInfo->containsFamily(typefaceFamily)) {
       
   195 		QString aliasFamily;
       
   196 		if (tInfo->tryGetFamilyFromAliasName(typefaceFamily, aliasFamily, weight)) {
       
   197 			typefaceFamily = aliasFamily;
       
   198 		}
       
   199 	} else {
       
   200 		weight = tInfo->getWeight(typefaceFamily);
       
   201 	}
   190     QFont font(typefaceFamily);
   202     QFont font(typefaceFamily);
   191 
   203 
   192     font.setWeight(weight);
   204     font.setWeight(weight);
   193 
   205 
   194     // Sets default size if text height is not set explicitly.
   206     // Sets default size if text height is not set explicitly.
   195     qreal height = textHeight();
   207     qreal height = textHeight();
   196 	int downSizedSize = tInfo->textHeightToSizeInPixels(typefaceFamily, weight, height);
   208     int downSizedSize = tInfo->textHeightToSizeInPixels(typefaceFamily, weight, height);
   197     font.setPixelSize(downSizedSize);
   209     font.setPixelSize(downSizedSize);
   198 
   210 
   199     return font;
   211     return font;
   200 #endif
   212 #endif
   201 }
   213 }
   210 }
   222 }
   211 
   223 
   212 /*!
   224 /*!
   213     Constructs a new font spec with the given font \a role.
   225     Constructs a new font spec with the given font \a role.
   214 */
   226 */
   215 HbFontSpec::HbFontSpec(HbFontSpec::Role role) 
   227 HbFontSpec::HbFontSpec(HbFontSpec::Role role)
   216     : d(new HbFontSpecPrivate())
   228     : d(new HbFontSpecPrivate())
   217 {
   229 {
   218     d->mRole = role;
   230     d->mRole = role;
       
   231 }
       
   232 HbFontSpec::HbFontSpec(const QString fontName) 
       
   233     : d(new HbFontSpecPrivate())
       
   234 {
       
   235     d->mFontName = fontName;
   219 }
   236 }
   220 
   237 
   221 /*!
   238 /*!
   222     Copy constructs a new font spec using the \a other font spec.
   239     Copy constructs a new font spec using the \a other font spec.
   223 */
   240 */
   305 
   322 
   306 /*!
   323 /*!
   307     Returns true if this fontSpec is equal to \a other fontSpec; otherwise returns false.
   324     Returns true if this fontSpec is equal to \a other fontSpec; otherwise returns false.
   308 */
   325 */
   309 bool HbFontSpec::operator==(const HbFontSpec &other) const
   326 bool HbFontSpec::operator==(const HbFontSpec &other) const
   310 {    
   327 {
   311     return ((d->mRole == other.d->mRole)
   328     return ((d->mRole == other.d->mRole)
   312         && qFuzzyCompare(d->textHeight(), other.d->textHeight()));
   329             && qFuzzyCompare(d->textHeight(), other.d->textHeight()));
   313 }
   330 }
   314 
   331 
   315 /*!
   332 /*!
   316     Returns true if this fontSpec is not equal to \a other fontSpec; otherwise returns false.
   333     Returns true if this fontSpec is not equal to \a other fontSpec; otherwise returns false.
   317 */
   334 */
   327 
   344 
   328     \sa HbFontSpec::setTextHeight
   345     \sa HbFontSpec::setTextHeight
   329 */
   346 */
   330 qreal HbFontSpec::textHeight() const
   347 qreal HbFontSpec::textHeight() const
   331 {
   348 {
   332 	return d->textHeight();
   349     return d->textHeight();
   333 }
   350 }
   334 
   351 
   335 /*!
   352 /*!
   336     Sets the height of the text into which the requested font is to fit.
   353     Sets the height of the text into which the requested font is to fit.
   337     When the \a font method is called, the returned font will have been
   354     When the \a font method is called, the returned font will have been
   349 void HbFontSpec::setTextHeight(qreal textHeight)
   366 void HbFontSpec::setTextHeight(qreal textHeight)
   350 {
   367 {
   351     d->mTextHeight = textHeight;
   368     d->mTextHeight = textHeight;
   352 }
   369 }
   353 
   370 
       
   371 void HbFontSpec::setTypefaceFamily(QString fontName)
       
   372 {
       
   373     d->mFontName = fontName;
       
   374 }
       
   375 
       
   376 QString HbFontSpec::typefaceFamily() const
       
   377 {
       
   378    return d->mFontName;
       
   379 }
   354 // End of File
   380 // End of File