[Kde-games-devel] Rough edges in localized KFourInLine

Albert Astals Cid aacid at kde.org
Wed Jul 3 22:57:35 UTC 2013


El Dimecres, 3 de juliol de 2013, a les 16:47:10, Eneko Nieto va escriure:
> I'm getting mad with adaptFontSize, I hope you can show me the right
> direction.
> 
> First, I still don't know how to get a QPainter from a
> QGraphicsTextItem (it extends QGraphicsObject that extends
> QGraphicsItem and QObject), so I am creating a dummy QPixmap and a
> QPainter to use adaptFontSize on. Sure there is a more ellegant way to
> achieve this.
> 
> Second, the result I am getting with adaptFontSize are smaller than
> they should be. The code for the title ("Quick start") is as follows:
> 
> //mTextQuicklaunch->setPlainText("this is a very long line so we need
> a small font size");
>   // Create and set current font
>   QFont font;
>   QPixmap p = QPixmap(1, 1);
>   QPainter paint(&p);
>   paint.setFont(font);
> kDebug() << "max fontHeight=" << fontHeight;
>   fontHeight = KFontUtils::adaptFontSize(paint,
> mTextQuicklaunch->toPlainText(), textWidth, textHeight, fontHeight,
> 8.0);
> kDebug() << "new fontHeight=" << fontHeight;
>   font.setPointSizeF(fontHeight);
> 
>   // Set font and color for all text items
>   mTextQuicklaunch->setFont(font);
>   mTextQuicklaunch->setDefaultTextColor(fontColor);
> //  mTextQuicklaunch->setTextWidth(textWidth);
> 
>   // Set position of sub sprites
>   QRectF bounding = mTextQuicklaunch->boundingRect();
> kDebug() << "textWidth=" << textWidth;
> kDebug() << "bounding.width()=" << bounding.width();
> kDebug() << "textHeight=" << textHeight;
> kDebug() << "bounding.height()=" << bounding.height();
>   mTextQuicklaunch->setPos(posQuickstart.x() * width + (textWidth -
> bounding.width()) / 2,
>                            posQuickstart.y() * height + (textHeight -
> bounding.height()) / 2);
> 
> And the output:
> 
> kfourinline(4223) DisplayIntro::changeTheme: max fontHeight= 38.64
> kfourinline(4223) DisplayIntro::changeTheme: new fontHeight= 16.7947
> kfourinline(4223) DisplayIntro::changeTheme: textWidth= 287.1
> kfourinline(4223) DisplayIntro::changeTheme: bounding.width()= 152.719
> kfourinline(4223) DisplayIntro::changeTheme: textHeight= 55.2
> kfourinline(4223) DisplayIntro::changeTheme: bounding.height()= 36
> 
> Why adaptFontSize is not returning a bigger font size? Is there more
> documentation apart of the API reference
> (http://api.kde.org/4.10-api/kdelibs-apidocs/kdeui/html/namespaceKFontUtils.
> html)?
> 
> Besides, I'm not having luck trying to centre the texts. If I use
> setTextWidth() then boundingRect() returns near all the width, so I
> cannot centre single line texts.

Bah, it's probably because KFontUtils sucks, i wrote like 10 variations of 
that code and it seems the not so good one ended up in kdelibs :-/

Feel free to have a look at the code and copy and improve it directly in 
kfourinline

http://quickgit.kde.org/?p=kdelibs.git&a=blob&h=3a80039dae41c08b629681cdc689caf8c7632147&hb=736d5237f822fc72736f75f379c4f86d6bf48098&f=kdeui%2Ffonts%2Fkfontutils.cpp

Basically what you have to do is replace
size = qMin(width * size / boundingRect.width(), height * size / 
boundingRect.height());
with something that decrements size but not so drastically, font sizes are not 
directly proportional so doing what the code does is not always a good idea.

I'll see if i can get a fix in kdelibs too, but for now i guess you can at 
least "cheat" and copy the code until we fix kdelibs.

Cheers,
  Albert

> 
> Thanks in advance!
> 
> Eneko
> 
> 2013/6/20 Eneko Nieto <enekonieto at gmail.com>:
> > Yes sure, I'll work on this the weekend. Thanks for helping me with
> > previous patches :)
> > 
> > Eneko
> > 
> > 2013/6/17 Albert Astals Cid <aacid at kde.org>:
> >> El Dilluns, 17 de juny de 2013, a les 08:24:01, Yuri Chornoivan va 
escriure:
> >>> Hi,
> >>> 
> >>> I have compiled and installed KFourInLine from git/master.
> >>> 
> >>> The game looks beautiful until gets translated.
> >>> 
> >>> English:
> >>> 
> >>> https://dl.dropboxusercontent.com/u/55247264/kfourinline_en.png
> >>> 
> >>> Ukrainian:
> >>> 
> >>> https://dl.dropboxusercontent.com/u/55247264/kfourinline_uk.png
> >>> 
> >>> I have cleaned all config files before starting the game.
> >>> 
> >>> Is there any way to fix this?
> >> 
> >> I guess it'd need to use KFontUtils or something similar to make sure the
> >> text fits on the available box space.
> >> 
> >> Eneko do you think you can give it a try?
> >> 
> >> Cheers,
> >> 
> >>   Albert
> >>> 
> >>> Thanks in advance for your answers.
> >>> 
> >>> Best regards,
> >>> Yuri
> >>> _______________________________________________
> >>> kde-games-devel mailing list
> >>> kde-games-devel at kde.org
> >>> https://mail.kde.org/mailman/listinfo/kde-games-devel



More information about the kde-games-devel mailing list