Firemonkey’s abstract TCanvas class has been providing the dimensions of the bounding rectangle of some text on itself. On recent updates it has deprecated providing text rectangle directly in the canvas, in stead encouraged users to use TTextLayout abstract class which handles several text-based operations on the native context of the platforms. So it is possible to get the bounding rectangle of a string on any canvas. Is this enough? Though I am sure this class will improve to involve many new textual features, at current time it lacks some major needs of a developer who designs text drawing applications.

When words in different sizes aligned from top you can’t get a proper sentence view

For the first time, I have faced a feature lack when I saw even TMS’s HTML Text was unable to align words in an HTML notation that have got different sizes. Because having text rectangle is not enough to locate words in different sizes because of the missing baseline information. If you align the words from the top the smaller word’s baseline will be upper (as in the above image), if you align the bottoms the baseline will be lower, so to make a correct match you should know where are the baselines of each word exactly located. So we need to know the Ascent value of the text. If have got the ascent value, we can easily align the words on the baseline as seen on following image. For a complete information on typography see this article on Wikipedia.

For a proper drawing of words in different sizes you should align them on their baselines.

As seen on the above image, having the information of ascent values for each word we can align them properly on the baseline of the sentence. However FMX TTextLayout doesn’t provide this information. So I decided to add a new function to my PlatformExtensions unit to provide metrics information that are gathered from the drawing of that specific font in a specific size. The new function is GetTextMetrics and hast this following structure.

1
2
3
Class Procedure GetTextMetrics(Text:String; Font:TFont; var TextRect:TRectF;
                               var Ascent,Descent:Single;
                               var CapHeight,XHeight:Single);virtual;abstract;

Getting Text Metrics in Windows

Firemonkey uses two different context types on Windows platform that are Windows GDIP and Direct2D. The default one is Windows GDI plus context which is a set of device independent drawing API based on the old GDI. Because of its device independency GDIP lacks of some important drawing features such as XOR brush, but it is always possible to handle these extra operations by locking the context to device dependent HDC and doing things on the device bitmap. What I mean is; getting font metrics is not as easy as it was old GDI based on HDC, so I used the limited information of the GDIP and estimated some others like (x height  of text) by using statistical multipliers. However someone other can collapse the context to HDC level and calculate more detailed font metrics on that level. I didn’t prefer it to be consistent with the design logic of GDIP, and used TGPFontFamily class of GDIP to get the information about the font. Note that the metrics values gathered from this class is in logical units so should be converted to pixels using the proportions.

Here are my calculations:

1
2
3
4
5
6
7
8
9
10
11
fSize := FGPFont.GetHeight(FGraphics);
cAscent := FGpFamily.GetCellAscent(FontStyle);
cDescent := FGpFamily.GetCellDescent(FontStyle);
emHeight := FGpFamily.GetEmHeight(FontStyle);
lSpacing := FGPFamily.GetLineSpacing(FontStyle);
iLeading := cAscent+cDescent - emHeight;
eLeading := lSpacing - cAscent-cDescent;
Ascent := fSize * (cAscent)/lSpacing;
Descent := fSize * (descent)/lSpacing;
CapHeight := fSize * (cAscent-iLeading)/lSpacing;
XHeight :=  fsize * (cAscent-iLeading) *(7/10) /lSpacing;

Note that the calculations use TGPFont, TGPFontFamily and TGPGraphics classes to deal with font metrics. The FGPFont and FGPGraphics objects are not accessible through standard class hierarchy, however we can use RTTI functions to access them.

1
2
3
4
5
6
7
8
9
10
11
12
RttiField := RttiContext.GetType(Layout.ClassType).GetField('FGPFont');
if assigned(RttiField) then
begin
  FGPFont := TGpFont(RttiField.GetValue(Layout).AsObject);
  RttiField := RttiContext.GetType(Layout.ClassType).GetField('FGraphics');
  FGraphics := TGpGraphics(RttiField.GetValue(Layout).AsObject);
 
  FGPFamily := TGPFontFamily.Create;
  FGPFont.GetFamily(FGPFamily);
  // Get Metrics
  // ...
end;

Another point to be mentioned here is that, above calculations work only if the DefaultCanvas is a GDIP canvas. If the developer selects to use the D2D canvas, because of the absence of the FGPFont object in RTTI the text metrics will not be updated. Since I don’t have any experience on Direct2D I haven’t worked on it, but I will be happy if someone does it and send me so that I can put it in the class code by giving credits to his/her name.

Getting Text Metrics in Mac OS X

Things are always easier for programmers in Mac side. You can see below how it is easy getting font metrics using CoreText API of Cocoa.

1
2
3
4
5
6
LFontRef := CTFontCreateWithName(CFSTR(Layout.Font.Family), Layout.Font.Size, nil);
Ascent := CTFontGetAscent(LFontRef);
Descent := CTFontGetDescent(LFontRef);
CapHeight := CTFontGetCapHeight(LFontRef);
XHeight := CTFontGetXHeight(LFontRef);
CFRelease(LFontRef);

You can see all the details of using TTextLayout class and how to extend it to get Text/Font Metrics in my PlatformExtensions units. You can get the source code of the PlatformExtensions classes with the demo application from this SVN link. For non-programmers the compiledWin32Win64MacOSX (Thanks to Firemonkey) applications are also available to download.

https://delphiscience.wordpress.com/2013/01/06/getting-text-metrics-in-firemonkey/

Getting Text Metrics in Firemonkey(delphiscience的博客)的更多相关文章

  1. 博客代码美化(SyntaxHighlighter)

    这篇博文主要讲解自己使用SyntaxHighlighter对代码进行美工中遇见的问题以及如何使用SyntaxHighlighter? 首先来看看SyntaxHighlighter对代码美工的效果吧! ...

  2. python实战--csdn博客专栏下载器

    打算利用业余时间好好研究Python的web框架--web.py,深入剖析其实现原理,体会web.py精巧之美.但在研究源码的基础上至少得会用web.py.思前想后,没有好的Idea,于是打算开发一个 ...

  3. 【实战】如何通过html+css+mysql+php来快速的制作动态网页(以制作一个博客网站为列)

    一.开发环境的搭建 (1)apache+php+mysql环境搭建 因为要用apache来做服务器,mysql作为数据库来存储数据,php来写代码以此实现网页与数据库的交互数据,所以需要下载上述软件, ...

  4. Sublime Text博客插件 --- iblog

    iblog是一款 sublime 博客插件,目前只支持cnblog. 项目地址:https://github.com/iskeeter/iblog 功能介绍 新建和更新cnblog的博客 支持mark ...

  5. 一步步开发自己的博客 .NET版(10、前端对话框和消息框的实现)

    关于前端对话框.消息框的优秀插件多不胜数.造轮子是为了更好的使用轮子,并不是说自己造的轮子肯定好.所以,这个博客系统基本上都是自己实现的,包括日志记录.响应式布局.评论功能等等一些本可以使用插件的.好 ...

  6. 一步步开发自己的博客 .NET版(1、基本显示)

    前言 我们每个猿都有一个搭建自己独立博客的梦,我也不例外.以前想 现在想 以后也想.之所以一直迟迟没有着手,是因为难以跨出第一步.每次心里想着,等我以后技术好了再说,然后就没有然后了.以前用过word ...

  7. 一步步开发自己的博客 .NET版(3、注册登录功能)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

  8. 一步步开发自己的博客 .NET版(4、文章发布功能)百度编辑器

    前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做个插件,任何网站上的技术 ...

  9. 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

随机推荐

  1. UIAlertController 的使用——NS_CLASS_AVAILABLE_IOS(8_0)

    UIAlertView 随着苹果上次iOS 5的发布,对话框视图样式出现在了我们面前,直到现在它都没有发生过很大的变化.下面的代码片段展示了如何初始化和显示一个带有“取消”和“好的”按钮的对话框视图. ...

  2. 百度地图点聚合MarkerClusterer,性能优化

    参考文献:http://www.cnblogs.com/lightnull/p/6184867.html 百度的点聚合算法 是基于方格和距离的聚合算法,即开始的时候地图上没有任何已知的聚合点,然后遍历 ...

  3. OpenGL —— 基础笔记

    1.基础博文    链接:http://blog.csdn.net/lotusone?viewmode=contents 2.绘制文字    链接:http://www.cnblogs.com/xia ...

  4. Unity3D基础学习 NGUI之Example 13 - Tabs简要概述

    首先建一个2D相机,在Anchor下新建一个子物体,添加WindowDrag Tilt脚本,用作拖动窗口 然后新建一个Panel,包含两个content,两个Tab,设置两个Content用来显示切换 ...

  5. CSDN挑战编程——《数学问题》

    数学问题 题目详情: 给你两个长度为n的正整数序列分别为{a1,a2,a3...an},{b1,b2,b3...bn},0<ai,bi<=100: 设S=max{x1*a1+x2*a2+x ...

  6. 【C++深入探索】Copy-and-swap idiom详解和实现安全自我赋值

    分类: C/C++2012-08-30 21:40 2017人阅读 评论(2) 收藏 举报 任何管理某资源的类比如智能指针需要遵循一个规则(The Rule of Three): 如果你需要显式地声明 ...

  7. 无限循环的ViewPager

    目前情况 在不修改源码的情况下,当ViewPager滑动到最后一个item的时候,他就无法再往右滑动:当ViewPager滑动到第一个item的时候,他也无法再往前滑动.(以上全是废话) 设想 我们可 ...

  8. Java基础知识强化31:String类之String的面试题

    1.先看一个图: 2.String面试题: (1)题1: package cn.itcast_02; /* * 看程序写结果 */ public class StringDemo3 { public ...

  9. JavaScripts学习日记——XML DTD Schema

    今日关键词: XML DTD Schema 1.XML 1 XML的概述 1.1 什么是XML XML全称为Extensible Markup Language,意思是可扩展的标记语言.XML语法上和 ...

  10. AngularJs学习之ng-repeat

    ng-repeat用来遍历一个集合或为集合中的每个元素生成一个模板实例.集合中的每个元素都会被赋予自己的模板和作用域.同时每个模板实例的作用域中都会暴露一些特殊的属性. $index:遍历的进度(0 ...