How to display text on GTK label using two different fonts? - c

I have a GTK label and I am displaying text on it in Arial Rounded Mt Bold by using following code:
PangoFontDescription *df;
df = pango_font_description_new ();
pango_font_description_set_family(df,"Arial Rounded Mt Bold");
pango_font_description_set_size(df,fontsize*PANGO_SCALE);
gtk_widget_modify_font(Message_Label, df);
gtk_label_set_text(GTK_LABEL(Label), "Hello World");
pango_font_description_free (df);
Now this Hello World is displayed in Arial Rounded Mt Bold. But what if I want to display Hello in Arial Rounded Mt Bold and World in some other font for example Arial. Is this possible in GTK label. I am doing it in C. Any advice or any useful links. Thanks.

gtk_widget_modify_font() is deprecated and won't let you do what you want.
You can use a PangoAttrList, which combines attributes (including the individual components of a PangoFontDescriptor) over ranges of text. For instance:
PangoAttrList *attrlist;
PangoAttribute *attr;
PangoFontDescription *df;
attrlist = pango_attr_list_new();
// First, let's set up the base attributes.
// This part is copied from your code (and slightly bugfixed and reformatted):
df = pango_font_description_new();
pango_font_description_set_family(df, "Arial Rounded MT");
pango_font_description_set_size(df, fontsize * PANGO_SCALE);
pango_font_description_set_weight(df, PANGO_WEIGHT_BOLD);
// You can also use pango_font_description_new_from_string() and pass in a string like "Arial Rounded MT Bold (whatever fontsize is)".
// But here's where things change:
attr = pango_attr_font_desc_new(df);
// This is not documented, but pango_attr_font_desc_new() makes a copy of df, so let's release ours:
pango_font_description_free(df);
// Pango and GTK+ use UTF-8, so our string is indexed between 0 and 11.
// Note that the end_index is exclusive!
attr->start_index = 0;
attr->end_index = 11;
pango_attr_list_insert(attrlist, attr);
// And pango_attr_list_insert() takes ownership of attr, so we don't free it ourselves.
// As an alternative to all that, you can have each component of the PangoFontDescriptor be its own attribute; see the PangoAttribute documentation page.
// And now the attribute for the word "World".
attr = pango_attr_family_new("Arial");
// "World" starts at 6 and ends at 11.
attr->start_index = 6;
attr->end_index = 11;
pango_attr_list_insert(attrlist, attr);
// And finally, give the GtkLabel our attribute list.
gtk_label_set_attributes(GTK_LABEL(Label), attrlist);
// And (IIRC this is not documented either) gtk_label_set_attributes() takes a reference on the attribute list, so we can remove ours.
pango_attr_list_unref(attrlist);
You can also use gtk_label_set_markup() to use an HTML-like markup language to set both text and styles in one go:
gtk_label_set_markup(GTK_LABEL(Label),
"<span face=\"Arial Rounded MT\" size=\"(whatever fontsize * PANGO_SCALE is)\" weight=\"bold\">Hello <span face=\"Arial\">World</span></span>");
// Or even...
gtk_label_set_markup(GTK_LABEL(Label),
"<span font=\"Arial Rounded MT Bold (whatever fontsize is)\">Hello <span face=\"Arial\">World</span></span>");

Related

getattribute in OSL is not giving expected results

I'm working on a custom OSL shader for Maya. I'm adding attributes to the shape node of objects and using getattribute() in my OSL code to manipulate those attributes. I have attributes for color correct, base color, rim width and rim color. (the last 2 are for facing ratio). My OSL always compiles and I'm always able to connect it to the material I'm using. But every time I check if it's working in the Arnold render view, my sliders on my attributes don't do anything. The goal is to have an image file connected to the base color of the material and have the color correct node affect base color (which would affect the texture file)
At first I thought my problem was I was trying to do too much in a single OSL node (I was using getattribute for all of the attributes I listed above). So I tried isolating it to just the color correct attribute and still nothing. Is anyone familiar with OSL and can help me?
This is what my OSL code is for color correct:
shader
colorCorrect
(
float modifier = 1[[float min = 0, float max = 72]],
color bob = 0,
output color result = 0
)
{
color temp = transformc("rgb", "hsv", bob);
temp[0] = temp[0] * modifier;
result = transformc("hsv", "rgb", temp);
}
This is what my OSL code is for facing ratio:
shader
Velvet(
float rim_width = 0.2
[[
string label = "Rim Width",
]],
color basecolor = color(1,1,1)
[[
string label = "Base Color",
string widget = "checkBox",
]],
color rimcolor = color(1,0,0)
[[
string label = "Rim Color",
]],
output color resultRGB = 0,
output float resultF = 0)
{
vector i = normalize(I);
vector n = normalize(N);
float d = fabs(dot(-i, n));
d = smoothstep(rim_width, 1.0, d);
getattribute("base_color", resultRGB = mix(rimcolor, basecolor, d));
getattribute("rim_width", resultF = d - 0.5);
}

How to Create Emboss and engrave text on OpenCASCADE

I am searching an API for creating emboss text on my AIS_Shape , If there is no API any better way to do that ?,I am thinking of creating text extrude and doing cut operation with AIS_Shape , can we extrude Text ?
OCCT doesn't provide direct tool defining an emboss text - so you are right, you have to do that using a general Boolean operation.
A general idea can be found within standard samples coming with Draw Harness - take a look onto "Modeling" -> "OCCT Tutorial bottle sample" sample (source $env(CSF_OCCTSamplesPath)/tcl/bottle.tcl):
Tools to use:
Font_BRepFont to load a font and convert a glyph into a planar TopoDS_Shape.
Font_BRepTextBuilder to format a text using Font_BRepFont.
BRepPrimAPI_MakePrism to define a solid from text.
BRepAlgoAPI_Cut to perform a Boolean operation between text solid and another shape.
Here is a pseudo-code:
// text2brep
const double aFontHeight = 20.0;
Font_BRepFont aFont (Font_NOF_SANS_SERIF, Font_FontAspect_Bold, aFontHeight);
Font_BRepTextBuilder aBuilder;
TopoDS_Shape aTextShape2d = aBuilder.Perform (aFont, "OpenCASCADE");
// prism
const double anExtrusion = 5.0;
BRepPrimAPI_MakePrism aPrismTool (aTextShape2d, gp_Vec (0,0,1) * anExtrusion);
TopoDS_Shape aTextShape3d = aPrismTool->Shape();
//aTextShape3d.SetLocation(); // move where needed
// bopcut
TopoDS_Shape theMainShape; // defined elsewhere
BRepAlgoAPI_Cut aCutTool (theMainShape, aTextShape3d);
if (!aCutTool.IsDone()) { error }
TopoDS_Shape aResult = aCutTool.Shape();

How to read Word Paragraph Line by Line in C#?

I am trying to read word content line by line. But I am facing an issue. When trying to read paragraph. If paragraph content is multi line. I am getting single line internally. Can any one please help me on this.
Expected Output:
Line 1 - > TERM BHFKGBHFGFKJHGKJSHFKG ABC1 IOUTOYTIUYRUYTIREYTU B08
Line 2 - > NBHFBHDFGJDSBHKHDGFJGJGDJK 3993 JBHKJSFGSDKFJDGFJKDSBF3993
Line 3 - > JHBJKFHKJGDGFSFGB08 HGHGGFGFDGJFFFDSGFABC1 JJBVHGHDFTERM
Line 4 - > TERMBHFKGBHFGFKJHGKJSHFKG ABC1IOUTOYTIUYRUYTIREYTU B08NBHFBHDFGJDSBHKHDGFJGJGDJK
Line 5 - > 39931234567890987654321
Actual Output:
Single Line -> TERM BHFKGBHFGFKJHGKJSHFKG ABC1 IOUTOYTIUYRUYTIREYTU B08 NBHFBHDFGJDSBHKHDGFJGJGDJK 3993 JBHKJSFGSDKFJDGFJKDSBF3993 JHBJKFHKJGDGFSFGB08 HGHGGFGFDGJFFFDSGFABC1 JJBVHGHDFTERM
TERMBHFKGBHFGFKJHGKJSHFKG ABC1IOUTOYTIUYRUYTIREYTU B08NBHFBHDFGJDSBHKHDGFJGJGDJK
39931234567890987654321
Below is my code sample:
OpenXml:
using (WordprocessingDocument doc = WordprocessingDocument.Open(fs, false))
{
var bodyText = doc.MainDocumentPart.Document.Body;
if (bodyText.ChildElements.Count > 0)
{
foreach (var items in bodyText)
{
if (items is Paragraph)
{
var par = items.InnerText;
}
}
}
}
Office.Interop
object nullobj = System.Reflection.Missing.Value;
Word.Application app = new Word.Application();
Word.Document doc = app.Documents.Open(FilePath, ref nullobj, FileAccess.Read,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj,
ref nullobj, ref nullobj, ref nullobj);
foreach (Word.Paragraph paragraph in doc.Paragraphs)
{
var line = paragraph.Range.Text;
}
It is not possible to determine individual lines in the closed file. Lines are dynamically generated when a document is opened in Word and where a line "breaks" depends on many factors - it's not necessarily the same from system profile to system profile. So it's necessary to use the interop, not Open XML to pick up where lines break on the screen.
What's more, the Word object model does not provide "Line" objects for this very reason - there is no "line", only a visual representation of how the page will print, given the current printer driver and version of Windows.
The only part of the Word object model that recognizes "lines" is Selection, as this works solely with what's displayed on the screen.
The following code demonstrates how this can be done.
First, since Selection is being worked with and this is visible on-screen, ScreenUpdating is disabled in order to reduce screen flicker and speed up processing. (Note that working with selections is generally much slower than other object model processing.)
Using ComputeStatistics the number of lines in a paragraph is determined. An array (you can also use a list or anything else) to contain the lines is instantiated. The paragraph range is "collapsed" to its starting point and visually selected.
Now the lines in the paragraph are looped, based on the number of lines. The selection is extended (MoveEnd method) by one line (again, moving by lines is only available to a selection) and the selected text written to the array (or whatever).
Finally, screen updating is turned back on.
wdApp.ScreenUpdating = false;
foreach (Word.Paragraph para in doc.Paragraphs)
{
Word.Range rng = para.Range;
int lNumLines = rng.ComputeStatistics(Word.WdStatistic.wdStatisticLines);
string[] aLines = new String[lNumLines];
rng.Collapse(Word.WdCollapseDirection.wdCollapseStart);
rng.Select();
for (int i = 0; i < lNumLines; i++)
{
wdApp.Selection.MoveEnd(Unit: Word.WdUnits.wdLine, Count: 1);
aLines[i] = wdApp.Selection.Text;
wdApp.Selection.Collapse(Word.WdCollapseDirection.wdCollapseEnd);
}
for (int i = 0; i < aLines.Length; i++)
{
Debug.Print(aLines[i]);
}
}
wdApp.ScreenUpdating = true;
In Word, a paragraph is a sinle line of text. Change the size of the print area (e.g. change the margins and/or page size) or the font/point size and the text reflows accordingly. Moerover, since Word uses the active printer driver to optimise the page layout, what exists on a given line in one computer may not exist on the same line on another computer.
Depending on your requirements, though, you could employ Word's predefined '\Line' bookmark to navigate between lines or the Rectangle.Lines property.

Selenium java code: How to read text from hidden element that contain many tags

1.<svg blablabla style:overflow: hidden > <aaa id=aaa>
2.+
3.<g>
4.<g>
5.<g>
6.<g>
The tags are inside the svg.
1 is the hidden tag, i want to get the 5th line text(), this is what i do.
WebElement hiddenDiv = driver.findElement(By.id("aaa"));
String n = hiddenDiv.getText();
String script = "return arguments[0].innerHTML";
n = (String) ((JavascriptExecutor) driver).executeScript(script, hiddenDiv);
System.out.println(n);
how can i get g[2], i have tried by direct xpath and it got an error because the svg is hidden.
You just need to grab an array of the g tags and pick the one you want. If you want the 5th, you would use the code below.
List<WebElement> gs = driver.findElements(By.cssSelector("#aaa g"));
System.out.println(gs.get(4).getAttribute("innerHTML"));

How to set the width of Underline in attributed text

I am using UITextView and adding attributed text to it. I want to set underline to the text according to the font size, but i was only able to set the size 1. If any one have any idea regarding this please tell.
The code i am using is:-
NSDictionary* dict = nil;
dict = #{NSForegroundColorAttributeName:[UIColor blackColor],
NSBackgroundColorAttributeName: [UIColor clearColor],
NSUnderlineStyleAttributeName: #1
};
TextView.attributedText = [[NSAttributedString alloc]initWithString:#"String to be underline" attributes:dict];

Resources