Cannot load a spriteFont in XNA4 - c#

Im goofing around with XNA and I've come to the part where I need to load a font in. Easy enough right?
Font1 = Content.Load<SpriteFont>("Arial");
Is the code that I'm using to load the font.
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
<Asset Type="Graphics:FontDescription">
<!--
Modify this string to change the font that will be imported.
-->
<FontName>Arial</FontName>
<!--
Size is a float value, measured in points. Modify this value to change
the size of the font.
-->
<Size>14</Size>
<!--
Spacing is a float value, measured in pixels. Modify this value to change
the amount of spacing in between characters.
-->
<Spacing>0</Spacing>
<!--
UseKerning controls the layout of the font. If this value is true, kerning information
will be used when placing characters.
-->
<UseKerning>true</UseKerning>
<!--
Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
and "Bold, Italic", and are case sensitive.
-->
<Style>Regular</Style>
<!--
If you uncomment this line, the default character will be substituted if you draw
or measure text that contains characters which were not included in the font.
-->
<!-- <DefaultCharacter>*</DefaultCharacter> -->
<!--
CharacterRegions control what letters are available in the font. Every
character from Start to End will be built and made available for drawing. The
default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
character set. The characters are ordered according to the Unicode standard.
See the documentation for more information.
-->
<CharacterRegions>
<CharacterRegion>
<Start> </Start>
<End>~</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
is the spritefont file im using. its located at Content/Arial.spritefont.
Regardless of what font I choose, it is unable to load that font.
Error loading "Arial". File not found.

The code you have posted appears fine, providing you are setting the content root directory (this line is part of the default template):
Content.RootDirectory = "Content";
And providing you have correctly added the .spritefont file to your content project and it is building correctly. Check that an Arial.xnb file is being created in the Content directory beside your executable (in bin/Debug or bin/Release depending on your build target).
If you still have problems, try creating a fresh XNA project and seeing if you can get a font to work in that.

why not using SpriteFont Texture, where you have all letters in one single image. by this you can show font that other computers does not have. Just remember to change ContentProcessor to Font Txture after you add texture to project.
SpriteFont Editor
Some Tutorial

Be sure your content project is added as a content reference to your main project. Right click your project and choose "Add Content Reference." Then just choose the content project(s) listed. This will ensure your content is actually copied.

Related

How to apply custom font icon shapes in UWP

I want to show some custom shapes through font icons in my UWP project. I have created my own .ttf file for custom shapes. But I don't know how to apply it programmatically.
TextBlock text = new TextBlock();
string font = "Assets/mycustomfont.webfont.ttf#MyCustomFont";
text.FontFamily= new Windows.UI.Xaml.Media.FontFamily(font);
this.grid1.Children.Add(text);
Anyone please help me on this.
The approach you are using is practically correct, but make sure to check the following:
The .ttf file must be included in the project and have Build Action set to Content in the Properties window
Start the path to the font with / to make sure it begins in root.
Ensure the # suffix actually matches the font metadata. A font viewer app like dp4 Font Viewer can help you with that. Use the Font Family name as the suffix.
I have written an article on my blog about using custom fonts in UWP so check it out to see if you haven't missed some of the steps there.

Svg Fit Canvas to Drawing via C#

I'm trying to split and SVG into many different SVG files each one containing one element original file.
My main problem is not the slipt in itself since for each element I have to split, I get the svg element, remove everything else from the original svg, add back the element into the main layer and then save the file with the name I want. This works fine.
The real problem is that each file I create has the view centered and dimensioned as in the original svg file. So usually it's misplaced and wide (since in the original file it would contain all elements that now are split into different files).
So I need to resize the canvas to the element that remains in the file.
This very function is done by inkscape with the command
inkscape --verb=FitCanvasToDrawing --verb=FileSave --verb=FileClose
But unfortunally this verb doesn't work in --without-gui mode, so if I call it in the code I see thousands of inkscape instances opening, fitting the canvas and then save and close the windows. It works but it's not good for a batch application (they have this "bug" since quite some years).
So I resorted to use SVG engine (https://github.com/vvvv/SVG) but it has a bug that prevents the right calculation of the element bounds (https://github.com/vvvv/SVG/issues/331), so I cannot change the viewbox or the svg element to the right values.
Any suggestion on how to calculate the right position and size? Or any other library (any language that can run in a batch) that works for that?

Iconic Tile BackgroundColor not working when set in WMAppManifest

When settings the background color in the WMAppManifest like following example the phone themes color is still used.
The microsoft documentation points out it will only work if the color starts with #FF... http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207009(v=vs.105).aspx
Important Note:
If the BackgroundColor element's color value does not begin with #FF, such as #FF524742, your custom background color will not display and the default theme color will display instead.
<Tokens>
<PrimaryToken TokenID="WindowsPhoneApp" TaskName="_default">
<TemplateIconic>
<SmallImageURI IsRelative="true" IsResource="false">Assets\Tiles\IconicTileSmall.png</SmallImageURI>
<Count>0</Count>
<IconImageURI IsRelative="true" IsResource="false">Assets\Tiles\IconicTileMediumLarge.png</IconImageURI>
<Title>WindowsPhoneApp</Title>
<Message>
</Message>
<BackgroundColor>#FF016FAC</BackgroundColor>
<HasLarge>True</HasLarge>
<LargeContent1>
</LargeContent1>
<LargeContent2>
</LargeContent2>
<LargeContent3>
</LargeContent3>
<DeviceLockImageURI IsRelative="true" IsResource="false">
</DeviceLockImageURI>
</TemplateIconic>
</PrimaryToken>
</Tokens>
How to get this working? Is this a known bug?
Found the solution. The Microsoft documentation is lacking some information when setting this from the WMAppManifest.
As the documentation reads when setting the backgroundcolor in xml you should always start your color with #FF.
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207009(v=vs.105).aspx
However you should do this when you use an xml file as your tile configuration you should NOT do this in the AppManifest. In the AppManifest just specify the color without the Alpha channel, so just #RRGGBB and make sure there are no spaces arround or new lines.
Following should be on one line exactly like this.
<BackgroundColor>#016FAC</BackgroundColor>
When you try to do it like this it will not work:
<BackgroundColor>
#016FAC
</BackgroundColor>
Microsoft if you read this please update the docs. It will save a lot of people a lot of researching.
Background property does not work when it declared in the manifest, but will work if you'll set it from the code.

Create SVG using C# by overlaying one SVG icon onto another

How (if possible to do so) can I create an SVG image, using C#? I wish to take one SVG, overlay another SVG on it, and save it as a third SVG image.
In my specific case, I allow a module of my software to provide an icon for a folder. I want to overlay a warning or error icon on top of the folder icon when there is an error in the contained data.
I recommend using SVG Rendering Engine to do this. Create a third document, and put the two given svgs one after the other into it. (Nesting svg documents in each other is completely valid):
var icon = SvgDocument.Open(...);
var overlayIcon = SvgDocument.Open(...);
var overlayed = new SvgDocument();
overlayed.Children.Add(icon);
overlayed.Children.Add(overlayIcon);
overlayed.Write(...); // saving
Note: check the overlaying icon's size, viewbox, etc., you may have to change these through SvgDocument.Width, SvgDocument.Height, SvgDocument.ViewBox to get proper result.
SVG is a very simple text format, even simpler than HTML. Overlaying SVGs is a work for XML library. You can just open it and append every node from one file to another file.
Just try taking these two text files:
http://upload.wikimedia.org/wikipedia/commons/0/02/SVG_logo.svg
http://upload.wikimedia.org/wikipedia/commons/6/6b/Bitmap_VS_SVG.svg
Open the first one, copy contents of the the <svg>...</svg>, paste at the end of the other file (just before the ending </svg> tag). You can check in some editor or validator that the result is just a perfectly fine SVG file.
Use System.Xml or System.Xml.Linq for handling it from your C# code. Additionally you can add new nodes, edit attributes like width, height, color, etc... if you want:
<svg width="637" height="637">
<path style="fill:white" d="... "/>
...
</svg>
Have fun.

WPF app custom ttf font not being used?

I downloaded a custom font which displays EAN 128 barcode.
I also downloaded some source code and sample program which converts a string to an encoded string to use with the font (includes check sum etc).
I installed the font, opened my WPF app and set a textblock font to this new font and added some text. The text comes out as if it was Arial or something.
I tried adding the font as a resource and referencing it that way but still not joy.
So I then ran the program that came with the font. The interesting part I found was that when you entered text, the program encoded it and set a label's text and the barcode showed. However, that app allowed you to print preview and print the bar code but when you did the font of the barcode in the print preview and printout changed to like Arial or what ever it was, same issue as I am seeing in my app.
This is how the xaml looks at the moment but as I say, I have tried just setting the font in expression blend.
#Code 128 is the font name and not the file name.
<TextBlock x:Name="tbkBarCode" FontFamily="/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>
Any ideas?
Try this:
<TextBlock x:Name="tbkBarCode" FontFamily="pack://application:,,,/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>
EDIT: I tried a different font first and this way of doing it worked.
I downloaded and tried it with a Code 128 font and it didn't work right away. To get it to work I had to change the Build Action to "Content" and the Copy to Output Directory to "Copy if newer".
Here is the method I used to do that...
I add the Font on my project
I Set property "Build action" of the Font on "Resource"
Finally, I used the Font like that :
TextBlock Grid.Column="2" HorizontalAlignment="Center" FontSize="32" VerticalAlignment="Stretch" FontFamily="./#Code 128" Grid.Row="5" Margin="1" Text="{Binding Checksum}"/
If you create a test Windows Forms app, add a TextBox and select the "Code 128" font as the font to use, then it correctly shows the Barcode font.
With WPF it looks the rendering system inside that that deals with fonts doesn't like that particular font for some reason...maybe some information it expects in the file isn't there.
Even "Glyphs" has trouble getting anything sensible out of that font file:
<Glyphs UnicodeString="ABCD1234567890" FontUri="c:\windows\fonts\code128.ttf" Height="50" Fill="#FF000000" FontRenderingEmSize="25"/>
it just returns "blocks".....which means no-character.
When you use a TextBox/TextBlock etc....the fallback font is used to display the content (because it is unable to display it with the Code123 font)......which is why you see the text reverting to the Arial font (or whatever your fallback font is defined to be).
From where did you download the custom font and sample program...was it this place?
http://www.barcoderesource.com/wpfbarcode.shtml
Or from here?:
http://grandzebu.net/informatique/codbar-en/code128.htm
Are you sure the font name is #Code 128 ?
If you copy the .ttf font into your \Windows\Fonts directory and then use CharacterMap, what font names can you see?
After installation with the barcoderesource font, I have these fonts listed:
That would mean using:
FontFamily="CCode128_S3_Trial"
to refer to the font IF it was installed in Windows
or
FontFamily="/Fonts/#CCode128_S3_Trial"
to refer to a .ttf font file embedded into your application.
Did you add the .TTF font file to your project in a folder called "Fonts" and set the Build Type="Resource"?
http://msdn.microsoft.com/en-us/library/ms753303.aspx

Categories