mapscript : adding Label to Layer with persian text - c#

I am having some issues using Persian text as label. I am using C# mapscript. I am setting the following at runtime:(like button click)
private void addLayer(String layerName)
{
if (layer == null)
{
//create a layer
layer = new layerObj(mapInstance);
layer.type = MS_LAYER_TYPE.MS_LAYER_POINT;
layer.status = mapscript.MS_ON;
layer.connectiontype = MS_CONNECTION_TYPE.MS_INLINE;
}
// Create a classObj
OSGeo.MapServer.classObj classobj = new OSGeo.MapServer.classObj(layer);
//create Label
labelObj label = new labelObj();
label.font = "sc";
label.type = MS_FONT_TYPE.MS_TRUETYPE;
label.encoding = "utf-8";
classobj.addLabel(label);
//add user data
//create feature
shapeObj feature = new shapeObj(mapscript.MS_SHAPEFILE_POINT);
lineObj line = new lineObj();
pointObj point = new pointObj(50,50, 0, 0);
line.add(point);
feature.add(line);
feature.text = "این متن فارسی است :گژپچ";
layer.addFeature(feature);
}
But labels are displayed as '?????'.
how can i fix this? Is there any conversion I need to do before setting the text value?
Appreciate any help

Try using different font file: label.font = "sc";.
The ‘□’ comes when it can't find the letter in that .ttf file.(May be it is not mapped to ttf or mapped to a different Unicode character). You can either go into .ttf file and change the font geometry or you can install different font files to get the correct one.

Related

Windows Presentation Foundation Print Label (Zebra Printer)

I am trying to print text to display vertical in Windows Forms Host. The label is printing with report viewer in WPF. Here is my code:
// boolean is based on true or false, when printing labels
private bool _isReportViewerLoaded;
// method to display data in .rdlc
private void ReportViewer_Load(object sender, EventArgs e)
{
// if equal false run this code isReportViewerLoaded
if (!_isReportViewerLoaded)
{
// get the lot based on the parameter Id
Lot lot = BottleLotRespository.GetLotById(this.Parameter);
// settings the page settings
PageSettings pg = new PageSettings();
pg.PrinterSettings
.DefaultPageSettings
.Margins = new Margins(0, 0, 0, 0);
pg.Landscape = false;
PaperSize size = new PaperSize("110.0 x 74.0", 433, 100);
BottleLotDataSet bottleLotDataSet = new BottleLotDataSet();
DataTable reportDataTable = bottleLotDataSet.LotDataTable;
DataRow lotRow = reportDataTable.NewRow();
lotRow["Id"] = lot.Id;
lotRow["Number"] = lot.Number.ToString();
reportDataTable.Rows
.Add(lotRow);
bottleLotDataSet.BeginInit();
this._reportViewer.SetPageSettings(pg);
this.reportDataSource.Name = "DataSet1";
this.reportDataSource.Value = reportDataTable;
this._reportViewer
.LocalReport
.DataSources
.Add(this.reportDataSource);
this._reportViewer
.LocalReport
.ReportEmbeddedResource = "BottleLotWPF.View.Report1.rdlc";
bottleLotDataSet.EndInit();
_reportViewer.RefreshReport();
_isReportViewerLoaded = true;
}
}
My problem is that the Report1.rdlc is not allowing me to rotate the text and there is no settings for it. Is there away of adding a setting to it to rotate the text?
In RDLC we have the option to print vertically it seems.
please take look at the existing thread here and see if it helps.
display-text-vertically-start-

c# setting the name of a dynamically created button so it can be called later

how do i set a buttons name when i created it in c# so i can call it later?
i have a a List of strings (Commands).
i loop over it an create a button for each item in the List.
commands.ForEach(delegate (String i)
{
Button button = new Button()
{
Name = i,
Tag = i,
MaxWidth = 50,
MaxHeight = 50,
BorderBrush = null
};
button.Click += new RoutedEventHandler(button_Click);
this.grid.Children.Add(button);
Uri resourceUri = new Uri("led_Off.png", UriKind.Relative);
StreamResourceInfo streamInfo = Application.GetResourceStream(resourceUri);
BitmapFrame temp = BitmapFrame.Create(streamInfo.Stream);
var brush = new ImageBrush();
brush.ImageSource = temp;
button.Background = brush;
});
This loop works fine until i add the line
Name = i
i am in a spot where these buttons were created and i now need to change some of there back ground images.
is there a better way to call them then by there name?
Name should be a valid string: try Name = "button" + i.ToString() (FYI, Button Name cannot be just "1", it's an invalid Name). Also, Tag =i.ToString(). Hope this may help
Don't do it, use data binding and data templating with commands instead.
There is no reason to ever create any UI elements in a loop in WPF.
Edit: Just this.

Set Adaptive Tile Background from Image

I am creating a TileNotification to display the last edited project on my app's tile. When doing this I want to set the tile's background to be the project cover image. I'm creating the TileContent like this:
TileContent content = new TileContent()
{
Visual = new TileVisual()
{
TileMedium = new TileBinding()
{
Content = new TileBindingContentAdaptive()
{
BackgroundImage = new TileBackgroundImage()
{
Source = new TileImageSource("ms-appx:///Assets/Images/MainPageBackground.jpg"),
Overlay = 10
},
Children =
{
new TileText()
{
Text = DataModel.Instance.CurrentProject.Title,
Align = TileTextAlign.Left
}
}
}
}
}
};
The problem is that the only way to set the Source property seems to be with a TileImageSource, which only accepts a string. And since the project's cover image is stored in ApplicationData.Current.LocalFolder... I can't just give it a string. Is there any way to set the image source from an actual image rather than a string?
After some more searching I found this method of doing it.
You can use the "ms-appdata:///Local" prefix to get the file. In my case:
$"ms-appdata:///local/Projects/{ProjectName}/Slides/0.gif".
That can then be handed in as the source.

Converting System.Windows.Forms.Font to System.Windows.Media.FontFamily not working for irregular styles

So, I know there are multiple thread on how to do the conversion between the aforementioned systems. And I know they're not 1-to-1. However, I'm hoping there's a way to be able to get things to work.
The fonts specifically in question are just examples, as I'm sure others have the same issue, Segoe UI just happens to be my default font. What's not working though, is when I select Segoe UI Semibold Italic or some other in-between font.
Here's my conversion code:
// Font family
FontFamilyConverter ffc = new FontFamilyConverter();
TextContent.FontFamily = (System.Windows.Media.FontFamily)
ffc.ConvertFromString(fontDialog.Font.Name);
// Font size
TextContent.FontSize = fontDialog.Font.Size;
// Bold?
TextContent.FontWeight = (fontDialog.Font.Bold ? FontWeights.Bold : FontWeights.Normal);
// Italic?
TextContent.FontStyle = (fontDialog.Font.Italic ? FontStyles.Italic : FontStyles.Normal);
// Underline and strikethrough?
TextContent.TextDecorations = new TextDecorationCollection();
if (fontDialog.Font.Strikeout) {
TextContent.TextDecorations.Add(TextDecorations.Strikethrough);
}
if (fontDialog.Font.Underline) {
TextContent.TextDecorations.Add(TextDecorations.Underline);
}
// Color
TextContent.Foreground = new SolidColorBrush(
System.Windows.Media.Color.FromArgb(fontDialog.Color.A,
fontDialog.Color.R,
fontDialog.Color.G,
fontDialog.Color.B)
);
From using the debugger, I know that the Italic property is being properly set, but the font isn't coming through as Semibold Italic it's just coming through as Semibold. If (when in the debugger) I change the FontFamily to "Segoe UI Semibold Italic" then it works.
Is there something I'm missing to be able to get all the styles to come across correctly?
Thanks.
Note: I know size isn't working correctly. Just haven't fixed it yet
Here is what I ended up with:
After the dialog returns OK:
FontFamilyConverter ffc = new FontFamilyConverter();
TextContent.FontFamily = (System.Windows.Media.FontFamily) ffc.ConvertFromString(getFontName(fontDialog.Font));
A helper method:
private List<string> limitFontList(List<string> fontList, string word) {
// Create a new list
var newFontList = new List<string>();
// Go through each element in the list
foreach (var fontFamily in fontList) {
// If the elment contains the word
if (fontFamily.ToLower().Contains(word.ToLower())) {
// Add it to the new list
newFontList.Add(fontFamily);
}
}
// Return the new list if anything was put in it, otherwise the original list.
return newFontList.Count > 0 ? newFontList : fontList;
}
getFontName:
private string getFontName(Font font) {
// Holds the font we want to return. This will be the original name if
// a better one cannot be found
string fontWanted = font.FontFamily.Name;
// Create a new Media.FontFamily
var family = new System.Windows.Media.FontFamily(fontWanted);
/// Get the base font name
string baseFont = ""; // Holds the name
/* FamilyNames.Values will holds the base name, but it's in a collection
** and the easiest way to get it is to use a foreach. To the best of my
** knowledge, there is only ever one value in Values.
** E.g. If the font set is Segoe UI SemiBold Italc, gets Segoe UI.
*/
foreach(var baseF in family.FamilyNames.Values){
baseFont = baseF;
}
// If the baseFont is what we were passed in, then just return
if(baseFont == fontWanted) {
return fontWanted;
}
// Get the typeface by extracting the basefont from the name.
// Trim removes any preceeeding spaces.
string fontTypeface = fontWanted.Substring(baseFont.Length).Trim();
// Will hold all of the font names to be checked.
var fontNames = new List<string>();
// Go through all of the available typefaces, and add them to the list
foreach (var typeface in family.FamilyTypefaces) {
foreach(var fn in typeface.AdjustedFaceNames) {
fontNames.Add(baseFont + " " + fn.Value);
}
}
// Limit the list to elements which contain the specified typeface
fontNames = limitFontList(fontNames, fontTypeface);
// If the font is bold, and the original name doesn't have bold in it (semibold?)
if(!baseFont.ToLower().Contains("bold") && font.Bold) {
fontNames = limitFontList(fontNames, "bold");
}
// In a similar manner for italics
if (!baseFont.ToLower().Contains("italic") && font.Italic) {
fontNames = limitFontList(fontNames, "italic");
}
// If we have only one result left
if(fontNames.Count == 1) {
return fontNames[0];
}
// Otherwise, we can't accurately determine what the long name is,
// So hope whatever the short name is will work.
return fontWanted;
}

Render CheckBox to Image for Tile

I want to display one or more CheckBoxes on a tile in my Windows Phone app. This works already for TextBlocks, but with a CheckBox it shows only the Text of the CheckBox and not the Checkmark itself.
This is a sample of my code:
public void CreateTile()
{
StackPanel panel = new StackPanel();
panel.VerticalAlignment = VerticalAlignment.Top;
panel.Margin = new Thickness(7.0, 7.0, 7.0, 0);
panel.Width = 336;
panel.Height = 336;
panel.Orientation = Orientation.Vertical;
// Create and add a CheckBox for each task
foreach (var task in _tasks)
{
TextBlock textBlock = new TextBlock();
textBlock.TextWrapping = TextWrapping.Wrap;
textBlock.Style = App.Current.Resources["PhoneTextLargeStyle"] as Style;
textBlock.Foreground = new SolidColorBrush(Colors.White);
textBlock.Text = task.Text;
CheckBox checkBox = new CheckBox();
checkBox.IsChecked = task.IsDone;
checkBox.Content = textBlock;
panel.Children.Add(checkBox);
}
Grid layoutRoot = new Grid();
layoutRoot.Background = new SolidColorBrush(Colors.Blue);
layoutRoot.Width = 336;
layoutRoot.Height = 336;
layoutRoot.Children.Add(panel);
layoutRoot.Measure(new Size(336, 336));
layoutRoot.Arrange(new Rect(0, 0, 336, 336));
layoutRoot.UpdateLayout();
// Render grid into bitmap
WriteableBitmap bitmap = new WriteableBitmap(336, 336);
bitmap.Render(layoutRoot, null);
bitmap.Invalidate();
// Save background image for tile to isolated storage
Uri backgroundImage = TileHelper.SaveTileImage(bitmap);
}
If I create a tile with a background image created by the method above, the tile will look like this:
As you can see the text is displayed but there is no checkmark/square before the text.
I personally like to use Segoe UI Symbol as the Font Family in such situations. This gives me the flexibility to use Text and Symbols together while not messing around too much with code / images. SUS has great modern icons (or characters if you may call them) that are very much Metroish, I'd say.
Just open up Charmap (Win + R and type in charmap) and in the Font Select -> Segoe UI Symbol. Now you can select any character you like and paste into Visual Studio Editor itself. Yes, it works!
The symbol may not display properly in the Editor itself but it will at Runtime
Here are some suggestions:
Here are the corresponding characters:
☑

✅

Don't worry about them not looking right HERE. They should when you follow the above steps.
You can always "hack" it by using images of checkbox controls. Did you try to show created control in UI? i.e. adding it to page? Just to see if your code is executed correctly.
Or another solution would be to use check mark character - http://www.fileformat.info/info/unicode/char/2713/index.htm
I'll try to replicate this problem in my test app since it is strange that it does not work.

Categories

Resources