recognize the color of text(numbers) and save them to excel/text file by DM scripting - dm-script

In a DM format image, there is some text on it.
I would like to reach the function that could recognize the color of text, and save/export them to excel/txt file by scripting. I mean the exported text will be sorted by their color in the Excel file.
How to reach it by DM scripting?

I assume you mean the text-color on image annotations?
Text-annotations are just one kind of component, so the commands you are looking for are:
void ComponentGetBackgroundColor( Component comp, NumberVariable red_o, NumberVariable green_o, NumberVariable blue_o )
and
void ComponentGetForegroundColor( Component comp, NumberVariable red_o, NumberVariable green_o, NumberVariable blue_o )
Both will return the red, green and blue value of the component comp into the respective variables.

Related

Apply Custom Layout to Image in DigitalMicrograph GMS3

I have an image in DigitalMicrograph GMS3 (v 3.21.1374.0) which i have applied a custom databar to (trying to learn how to do this via script here: add / apply custom databar to image in DigitalMicrograph GMS3)
I have a custom layout that I can add manually by doing the following:
Right click on the image
Hover on layout (in context menu)
Left click "Apply Layout..."
Select the custom layout in dialog that pops up (the one I want is called "CheckLayout")
Click OK
How do I do this with a script? I know how to get the image and the imagedisplay objects but that is as far as I get.
//main - get front image and apply custom layout
image Img := GetFrontImage()
imageDisplay imgDisplay = Img.ImageGetImageDisplay(0)
//apply custom layout to image here
Any ideas?
The layout is a property of an ImageDocument not an image. The correct way of doing this (provided there exists a layout of name 'MyLayout') is:
ImageDocument doc = GetFrontImageDocument()
doc.ImageDocumentApplyLayout("MyLayout")
You might additionally be interested in the commands:
void ImageDocumentApplyLayout( ImageDocument, String )
void ImageDocumentRemoveDatabar( ImageDocument )
Number ImageDocumentGetLayoutCount( ImageDocument )
String ImageDocumentGetLayoutName( ImageDocument, Number )
as used in
ImageDocument doc = GetFrontImageDocument()
number nLO = doc.ImageDocumentGetLayoutCount()
Result( "\n Layouts in document:" + nLO )
For( number i=0; i<nLO; i++)
{
string layoutName = doc.ImageDocumentGetLayoutName(i)
Result( "\n\t"+i+":"+layoutName)
}

Get font height/weight from TextRenderInfo how?

When I parse an existing PDF using iText(Sharp), I create an object which implements IRenderListener which I pass into PdfReaderContentParser.ProcessContent() and sure enough, my object's RenderText() gets called repeatedly with all the text in the PDF.
The problem is, the TextRenderInfo tells me about the base font (in my case, Helvetica) but I can't tell the height of the font nor its weight (regular vs. bold). Is this a known deficiency of iText(Sharp) or am I missing something?
the TextRenderInfo tells me about the base font (in my case, Helvetica) but I can't tell the height of the font nor its weight (regular vs. bold)
Height
Unfortunately iTextSharp does not provide a public font size method or member in the TextRenderInfo. Some people worked around this by using the distance between its GetAscentLine() and its GetDescentLine().
If you are ready to use Reflection, though, you can do better by exposing and using the private TextRenderInfo member GraphicsState gs, e.g. like in this render listener:
public class LocationTextSizeExtractionStrategy : LocationTextExtractionStrategy
{
//Hold each coordinate
public List<SizeAndTextAndFont> myChunks = new List<SizeAndTextAndFont>();
//Automatically called for each chunk of text in the PDF
public override void RenderText(TextRenderInfo wholeRenderInfo)
{
base.RenderText(wholeRenderInfo);
GraphicsState gs = (GraphicsState) GsField.GetValue(wholeRenderInfo);
myChunks.Add(new SizeAndTextAndFont(gs.FontSize, wholeRenderInfo.GetText(), wholeRenderInfo.GetFont().PostscriptFontName));
}
FieldInfo GsField = typeof(TextRenderInfo).GetField("gs", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
}
//Helper class that stores our rectangle, text, and font
public class SizeAndTextAndFont
{
public float Size;
public String Text;
public String Font;
public SizeAndTextAndFont(float size, String text, String font)
{
this.Size = size;
this.Text = text;
this.Font = font;
}
}
You can extract information with such a render listener like this:
using (var pdfReader = new PdfReader(testFile))
{
// Loop through each page of the document
for (var page = startPage; page < endPage; page++)
{
Console.WriteLine("\n Page {0}", page);
LocationTextSizeExtractionStrategy strategy = new LocationTextSizeExtractionStrategy();
PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
foreach (SizeAndTextAndFont p in strategy.myChunks)
{
Console.WriteLine(string.Format("<{0}> in {2} at {1}", p.Text, p.Size, p.Font));
}
}
}
This produces an output like this:
Page 1
< The Philippine Stock Exchange, Inc> in Helvetica-Bold at 8
< Daily Quotations Report> in Helvetica-Bold at 8
< March 23 , 2015> in Helvetica-Bold at 8
<Name> in Helvetica at 7
<Symbol> in Helvetica at 7
<Bid> in Helvetica at 7
[...]
Considering transformations
The numbers you see in the output as font sizes are the values of the font size property in the PDF graphics state at the time the respective text is drawn.
Due to the flexibility of PDF this may not be font size you eventually see in the output, though, a custom transformation may stretch the output considerably. Some PDF producers even always use a font size of 1 and transformations to stretch the output accordingly.
To get a good value for font sizes in such documents, you can improve the LocationTextSizeExtractionStrategy method RenderText like this:
public override void RenderText(TextRenderInfo wholeRenderInfo)
{
base.RenderText(wholeRenderInfo);
GraphicsState gs = (GraphicsState) GsField.GetValue(wholeRenderInfo);
Matrix textToUserSpaceTransformMatrix = (Matrix) TextToUserSpaceTransformMatrixField.GetValue(wholeRenderInfo);
float transformedFontSize = new Vector(0, gs.FontSize, 0).Cross(textToUserSpaceTransformMatrix).Length;
myChunks.Add(new SizeAndTextAndFont(transformedFontSize, wholeRenderInfo.GetText(), wholeRenderInfo.GetFont().PostscriptFontName));
}
with this additional reflection FieldInfo member.
FieldInfo TextToUserSpaceTransformMatrixField = typeof(TextRenderInfo).GetField("textToUserSpaceTransformMatrix", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
Weight
As you can see in the output above, the name of the font may contain more than the mere font family name but also a weight indicator
< March 23 , 2015> in Helvetica-Bold at 8
In your example, therefore,
the TextRenderInfo tells me about the base font (in my case, Helvetica)
the Helvetica without any decorations would imply a regular weight.
Helvetica is one of the standard 14 fonts which every PDF viewer must provide out-of-the-box: Times-Roman, Helvetica, Courier, Symbol, Times-Bold, Helvetica-Bold, Courier-Bold, ZapfDingbats, Times-Italic, Helvetica-Oblique, Courier-Oblique, Times-BoldItalic, Helvetica-BoldOblique, Courier-BoldOblique. Thus, these names are pretty dependable.
Unfortunately font names in general may be chosen arbitrarily; a bold font may have "Bold" or "Black" or other indicators of boldness in its name or none at all.
One might also try to use the font's FontDescriptor dictionary for which an entry FontWeight is specified. Unfortunately this entry is optional, you cannot count on it being there at all.
Furthermore, a font in a PDF can be artificially bold'ed, cf. this answer:
All these numbers are drawn using the same font, merely adding a rising outline line width.
Thus, I'm afraid there is no dependable way to find the exact font weight, merely a number of heuristics which may or may not return acceptable approximations.

RichEditBox : set a style only for the characters that are about to be written

I'm trying to create a simple text editor in a Windows 8 application, like a simple OneNote. I can set font size, font color and font style, but the problem is that it's applied to the whole content of my RichEditBox, even to the text that has already been written. But I would like that, when I select "Italic" for exemple, only the future characters should be written in Italic, and the words and characters already written remain in the style they were.
It's quite difficult to describe.
Do I use the good XAML element (RichEditBox) ? Is there a method to select the actual position of the writer in the RichEditBox ?
Thanks for your answers !
Try this button event. rebTextNote is RichEditBox object.
using Windows.UI.Text;
public void btnItalic_Click(object sender, RoutedEventArgs e)
{
ITextCharacterFormat format = rebTextNote.Document.Selection.CharacterFormat;
if (format.Italic != FormatEffect.On)
format.Italic = FormatEffect.On;
else
format.Italic = FormatEffect.Off;
}

How to set an Image to top right corner of a form heading

I’m using eclipse rcp forms ,
I'm trying to set an Image using
form.setImage()
merely it sets image to the left of form text. How can i place an image towards top right corner of the form title text.
As shown in below pic(image is the default overview tab of any RCP application)
From the above pic I understand that the images/widgets are placed beside the Form text(apologies if I’m wrong).
As a workaround I tried placing a composite in the form head, but I believe that form head comes after form title level(if we consider form title as 1st row, then form head appears as 2nd row)
Composite composite = formToolkit.createComposite(form.getHead(), SWT.NONE);
form.setHeadClient(composite);
formToolkit.paintBordersFor(composite);
composite.setLayout(new GridLayout(2, false));
In this fashion i attempted to place components to the composite but anyways I don't get the desired style as shown in the image.
How to place an image to the top right of the form title
You cannot set images to the right corner. What you see in the attached image is a toolbar with some IContributionItem's
To create something you have to override org.eclipse.ui.forms.editor.SharedHeaderFormEditor.createHeaderContents(IManagedForm) with something like the following:
#Override
protected void createHeaderContents(final IManagedForm headerForm) {
headerForm.getForm().setText("EditorTitle");
headerForm.getForm().setImage(myLeftImage);
headerForm.getToolkit().decorateFormHeading(
headerForm2.getForm().getForm());
Action action = new Action("Do something") {
#Override
public ImageDescriptor getImageDescriptor() {
return imageDescriptorOfRightImage;
}
};
headerForm.getForm().getToolBarManager().add(action);
headerForm.getForm().getToolBarManager().update(true);
}

How can I change text color in CEikRichTextEditor Symbian

Is there anyway to change parts of the text I add into CEikRichTextEditor control without selecting the text first - which shows the green selection rectangle over the text - and then apply text style?
Here is the code I use which gives an ugly and sloppy style when user see the running green selection rectangle over the text especially when I insert the text inside a loop
CDesCArray* temp = new(ELeave) CDesCArrayFlat(4);
temp->AppendL(_L("First"));
temp->AppendL(_L("Second"));
temp->AppendL(_L("Third"));
temp->AppendL(_L("Fourth"));
TBuf<100>iNumbers;
iNumbers.Copy(_L("Here is the numbers"));
iRichText1->SetTextL(&iNumbers); // iRichText1 is a pointer to CEikRichTextEditor object
for(TInt i = 0; i < temp->Count(); i++)
{
TInt x = iRichText1->Text()->DocumentLength();
iRichText1->RichText()->InsertL(x, (*temp)[i]);
iRichText1->SetSelectionL(x,iRichText1->Text()->DocumentLength());
iRichText1->BoldItalicUnderlineEventL(CEikGlobalTextEditor::EItalic);
TInt line = iRichText1->Text()->DocumentLength();
iRichText1->RichText()->InsertL(line, _L("\f\f"));
}
Many thanks in advance.
You need to operate on the CRichText object owned by the editor and apply a paragraph or character format over it (using ApplyCharFormatL() / ApplyParaFormatL()). This avoids any need to select text.
Example applying a paragraph format
Example applying a character format