What's the proper way of getting text bounding box in FreeType 2? - c

I wonder what's the best way of getting a text bounding box with FreeType 2?
To get the linespace bounding box width, I iterate over all the characters of the text and get it's advance and kearning:
FT_Face face = ...;
text_bbox_width = 0;
while (*p_text)
{
...
FT_Get_Kerning(...);
text_bbox_width += (face->glyph->advance.x + kerning.x) >> 6;
}
How to get linespace bounding box height? Is it necessary to iterate or can it be obtained using font face data? I.e:
text_bbox_height = (face->ascender - face->descender) >> 6

Good news: you do not need to iterate over the characters in each of your strings. You can use face->size->metrics->height, as described in 3. Global glyph metrics of http://www.freetype.org/freetype2/docs/tutorial/step2.html. Note the warnings on using ascender and descender.
Do not mistake this height for the actual pixel bounding box. Individual glyphs may stick out of this box. You can use this line height to get an even spacing over multiple lines in the same text block. To get 'larger' or 'smaller' spacing, you can multiply this value with a constant, such as 1.5 or 2.0 for "double line spacing".
I'm guessing that the value of height that Freetype calculates is the "normal" or "optimal" line spacing for a certain font.

Related

How to remove xTextLabel in a barChart?

The code to draw a bar chart as follows:
XYMultipleSeriesRenderer renderer = buildBarRenderer(colors);
renderer.setXLabels(4);
renderer.setYLabels(4);
ChartLabel chartLabel = getChartLabelfromString(dateL);
for (int i=0; i<chartLabel.labelIdxs.size(); i++){
renderer.addXTextLabel(chartLabel.labelIdxs.get(i),chartLabel.labelStrs.get(i));
}
BarChart chart = new BarChart(buildBarDataset(titles, valueDDL), renderer, BarChart.Type.DEFAULT);
chartLabel.labelIdxs.size() = 4
There are two strange values in the xlabels marked as red color. How do I remove the values 5,10 in the Xaxis?
Barchart Sample Code Output with labels A B C D
There are a number of ways to remove a symbol from your Watchlist.
Using the Links Column
Click on the Links icon.
Select "Delete from Watchlist". The symbol is immediately removed.
It seems you're setting the font size to be relatively large with long labels so they override each other. You can set the angle of the text to fit longer text or you can shrink the font size but there's a limit to what can physically fit under a chart. I suggest just using the month denominator in the labels.
There might be other options since the renderer class includes a lot of them.

Draw text and make it fit inside a rectangle in GDI?

Is there's a function that can draw text and fit it inside a rectangle (the function will make the size of the text smaller as appropriate to make it fit or something).
I checked the parameters for DrawText() but I don't think it supports such a feature.
Maybe you can use GetTextMetrics to check if the text fits inside the rectangle, and if it doesn't, reduce the current font size and repeat the measurement.
GetTextMetrics:
http://msdn.microsoft.com/en-us/library/windows/desktop/dd144941%28v=vs.85%29.aspx
There is no function doing it right away, but your can write your own using DrawText with DT_CALCRECT parameter. This parameter will only calculate size of your text and return it to you. It allows also multi line text and also checks provided maximal width you can allow text to be. So if the output rectangle - as calculated by DrawText is too large, then you must calculate it again but with smaller font, you can speed up calculations by using binary method to find the most appropriate font size.

Keep absolute divs always on image parent container

Im working on a interactive show room. In this showroom visitors can click on products to get a lichtbox with more info about that product.
See: http://codepen.io/xiniyo/pen/wLoxE
Is it possible to keep the dots always on the same position as the image below when you rescale the browser. Normal width = 1140px minimum width = 960px. Now the imag scales but the dots always stay on their absolute position.
I`ve tried it with % calculations but that didn't work either. The image scales faster than the dots do.
With difficult % calculations? or some Javascript.
Or is it possible to calculate the Absolute position from the canter of the div?
position: fixed;
This should work

How to keep font quality on scale transform

I want to stretch the text in a label to fill the width of the label using VB.NET code. This is in a WPF application.
I have tried numerous methods including changing only the font size like this:
scale = 0.25
lbl.FontSize = l.Width * scale
The problem with this method is I need the height to remain the same for all labels and the 0.25 was a guess that gives approximately the correct fill for a string of 4 characters
I tried a recursive method where I change the font size in small increments and compare the string width to the label width until it fits.
Transformations was also considered and it would looks like a good bet:
w = lbl.Width
scale = lbl.content.length/100
lbl.LayoutTransform = New System.Windows.Media.ScaleTransform(w * scale, 1)
This solves my problem of hight but I still need to guess the factor of transformation until it sort of works. Also the stretched text lose it quality and becomes distorted as expected.
Is there any way to reduce the distortion effect on text when its transformed. Perhaps some fonts that display better than other on after transformations?

ComponentOne for WPF: Text seems to render in the incorrect position?

I’m running into problems when rendering text on my document. Specifically, the text renders too low. I tried filling a rectangle behind the text to see what happens, and I discovered that they appear to render slightly offset:
Here’s the code I used to render the box and text:
_doc.FillRectangle(Colors.LightGray, 36, 72, 37.344, 9);
_doc.DrawString("Lorem", new Font("Arial", 12), Colors.Black,
new Rect(36, 72, 37.344, 9));
I know that the height of the rectangle (9) doesn’t appear to match the height of the font (12), which I thought might have been the problem at first. However, I then did a MeasureString on the font itself and discovered that its height was actually 9 rather than 12 (I used the immediate window for this, which is why it's a pic and not a text block):
Any ideas as to what could be causing it and how to avoid it?
Thanks!
-Ari
There are couple of posts that discuss the WPF text rendering inconsistencies.
One of the other posts: WPF Text rendering problem, stated that SnapToDevicePixels could ruin text rendering if text has been resized to display across pixels. The suggested answer was to keep,
SnapToDevicePixels = True on borders/backgrounds but turn it off for text elements.
As for the current method your are using. Please take a look at one of my earliers posts: Increase bar chart values with button clicks : I have used DrawString() to add a letter within a rectangle. All drawing is done in a Panel.
code:
...
panel1.Paint += new PaintEventHandler(panel1_Paint);
using (Graphics g = this.panel1.CreateGraphics())
{
Brush brush = new SolidBrush(Color.Green);
g.FillRectangle(brush, px, py, 20, 20);
Pen pen = new Pen(new SolidBrush(Color.White));
g.DrawRectangle(pen, px, py, 20, 20);
//add each total5Click into chart block
g.DrawString((total5Times).ToString(), new Font("Arial", 7),
new SolidBrush(Color.AntiqueWhite),
px + 1, py+8, StringFormat.GenericDefault);
pen.Dispose();}
...
I would suggest using the method DrawString Method (String, Font, Brush, RectangleF, StringFormat) and supplying the String Format. After reviewing ComponentOne it appears they are putting together several methods so I may be an issue with the StringFormat default set for the method. I am kind of assuming they are calling the main DrawString method and passing in default params if one was not supplied.
Also be sure to check the section for
Use LineAlignment to specify the vertical alignment of the string.
in the link below
Link to Method
Well, after further research and experimentation there's definitely a bug in the ComponentOne library. Specifically, the overload I happened to have used here returned the wrong hight. If you specific an available width explicitly, you get the correct height. Specifically, this code generates the correct data:
var resultHeight = _doc.MeasureString(text, pdfFont, double.MaxValue).Height;
var resultWidth = _doc.MeasureString(text, pdfFont).Width;
return new Tuple<double,double>(resultHeight, resultWidth);
Note the addition of the third parameter for the height only -- double.MaxValue. The width is correctly calculated in both cases, but the height is only correctly calculated if you provide that double parameter. I chose double.MaxValue in this case simply because I don't know how wide the string is going to turn out to be so I don't want to risk being given a multi-line height.

Resources