I am using Skia for one of my sample program. I have a canvas and inside this I am writing text with font_size 30, this is the code snippet.
string = "Test String";
SkString text(string);
SkPaint paint;
SkScalar textWidth;
paint.setTextSize(SkIntToScalar(font_size));
paint.getFontMetrics(&metrics);
textWidth = paint.measureText(text.c_str(), text.size());
textWidth will give the exact width of the text inside the canvas. My question is how can I get the height of the text ? Please help.
I once had to look into this myself in the past, this link here should help you, even though it is java, fonts all work on the same idea as far as I know.
I assume you will want from the ascender to the baseline, Which is just the ascent. Or you may want the whole thing from top to bottom which is the ascent and descent combined,
If you were writing on lined paper, the baseline is the same as the line you write on, anything above that is the ascent, anything below is the descent.
I dont know anything about skia, But A quick look into skia, at this link here, that there is a public member called fAscent in FontMetrics, and fDescent, Maybe you can use those.
the font size your have specified as "font_size" is the height of single line text
Related
I want the outline of text with the standard Windows font "Bahnschrift". I tried to convert it with "Path->Object to Path" and then setting fill to X and stroke to black, but looks like the font is constructed with separate polygons for each letter. See this example:
Top is Bahnschrift, and bottom is Arial which works fine.
Is it possible to calculate somehow automatically the outline of the top text, without intersections, and without doing it all manually for all the letters? And including the right outline for letters with holes, like the "e"?
Some letters are even kind of twisted internally, looks pretty bad:
So I guess an algorithm to detect if it is a real hole, or if it is a hole from such twists and to ignore it, could be difficult.
Select the complete character in question. Then select from the drop down menu:
Path intersection followed by path union. This fixes the character font outline problem where the outline crosses into the body of the character.
I've been trying to find a way to change a font file's line spacing default value, using QFont, QFontMetrics or something like that. I'm using QPainter::drawText to draw some text in a bounding rectangle.
It's strange that QFont allows for font kerning to be changed and even has some stretch operation and letter spacing but nothing to change the default space between lines. I've searched and found some partial solutions using QTextLayout but none seemed to work properly.
I need to use QPainter because I generate a texture with the text to be rendered with OpenGL.
Looking for more ideas for me to try out!
UPDATE
I've found that I can use QPainter to draw a QStaticText which allows for HTML text formatting, similar to QTextDocument. However, CSS styling doesn't work like in QTextDocument (there's a bug report)... Therefore still no leading but I hope this puts me on the right track.
SOLVED
I got what I wanted using QTextDocument, like Mykhaylo suggested. Link to solution
QFontMetrics was not designed particularly for multi-line text.
Use QTextDocument. You can print multi-line and rich text with it, even using QPainter. See the solution how to use QPainter with QTextDocument
It seems there is not much that can be done here.
QFontMetrics::lineSpacing returns what you need but it is read-only.
It's the sum of font height and leading. You can adjust height - set it in QFont constructor. But you can't set leading.
Some people add \n to the end of string to increase space between lines but of course this is not always a good solution.
I am drawing a chart in a text file and I want to set the rows color grey and white one by one but I couldn't find the function that can help me.
my compiler is code blocks on windows.
can anyone please tell me the number of these colors and related function?
thanks
It is impossible in c++ to change color of texts in a file
A text file is just text. It has just nothing but the letters and punctuation. It has no notation of color or whatever. So you can't do it in C++ or anything else.
For formatting and colors there are special files - HTML, RTF and others. If you want to write such thing you must ask the question that way.
Does anyone have an idea of how to get a glow to show up on text? I thought it would be pretty easy to do so but nothing is rendering on the fiddle.
Fiddle
Thanks
It looks to me as though Raphael simply does not support text glow. Check out the first lines of the glow function:
if (this.type == "text") {
return null;
}
It might be worth your while to investigate the use of print with a Cufonized font -- it'll return a path representing the text you give it instead of a tspan, and glow can be applied to paths.
Or you could settle for creating a simple drop shadow.
I've staged both of these alternatives by way of demonstration here.
You could create a rectangle behind the text and make that glow. I think that should look just fine. Use getBBox() to find out the size of the rectangle you need.
Something like this: http://jsfiddle.net/7ZPtq/51/
Or maybe use some other primitive, e.g. line.
I'm new to GUI programming in C and Linux, and I'm having a hard time with it. It seems like a fairly simple/straightforward thing, but I can't find any answers googling. I want to add a background color to a widget. XmNbackground seems to be what I want to use, but I don't understand what I set it to, like a simple color blue, how do I get "blue" to set XmNbackground color to that?
See here for an answer in the function Pixel convert_color_name_to_pixel, and also here.
Hope this helps.
You can use a resource file which includes "*background: blue" or use XSetArgVal to do the same thing in the code.
If you have the widget in a function, you can set the widget background color resource like this, assuming the widget name is w:
XtVaSetValues(w, XmNbackground, XmRstring, "#c4c4c4", 8, NULL);
Where #c4c4c3 is the hex color code for red (first two characters), green (middle two characters) and blue (last two characters).
You can also use any of the textual color names from file rgb.txt - where that file is located seems to be something of a moving target, but on the computer I'm using at the moment it's in /usr/share/X11