/// <summary> /// Measures the size of the specified text by a faster method (using GlyphTypefaces). /// </summary> /// <param name="text">The text.</param> /// <param name="fontFamily">The font family.</param> /// <param name="fontSize">The font size.</param> /// <param name="fontWeight">The font weight.</param> /// <returns>The size of the text.</returns> protected OxySize MeasureTextByGlyphTypeface(string text, string fontFamily, double fontSize, double fontWeight) { if (string.IsNullOrEmpty(text)) { return(OxySize.Empty); } // var typeface = new Typeface( // new FontFamily(fontFamily), FontStyles.Normal, GetFontWeight(fontWeight), FontStretches.Normal); // // GlyphTypeface glyphTypeface; // if (!typeface.TryGetGlyphTypeface(out glyphTypeface)) // { // throw new InvalidOperationException("No glyph typeface found"); // } FontFamily ff = new FontFamily("Assets/Samples/Fonts/Laffayette Comic Pro"); Font font = NoesisGUISystem.LoadFont(ff, FontStyle.Normal, FontWeight.Normal, (float)fontSize, 0.0f); float textWidth = 0.0f; foreach (char ch in text) { textWidth += font.GetGlyphAdvance((uint)ch); } //return new OxySize(textWidth, fontSize); return(MeasureTextSize(font, fontSize, text)); }
//////////////////////////////////////////////////////////////////////////////////////////////// private void LoadXaml() { // Create NoesisGUI System NoesisGUISystem.Create(); // Create UI Renderer if (NoesisGUISystem.IsInitialized && _xamlFile.Length > 0 && _uiRenderer == null) { FrameworkElement root = NoesisGUISystem.LoadXaml(_xamlFile) as FrameworkElement; if (root != null) { if (_styleFile != "") { ResourceDictionary resources = NoesisGUISystem.LoadXaml(_styleFile) as ResourceDictionary; if (resources != null) { root.Resources.MergedDictionaries.Add(resources); } else { throw new System.Exception("Unable to load style xaml: " + _styleFile); } } _uiRenderer = new Noesis.UIRenderer(root, _offscreenSize, gameObject); NoesisGUISystem.AddPanel(this); } else { throw new System.Exception("Unable to load xaml: " + _xamlFile); } } }
//////////////////////////////////////////////////////////////////////////////////////////////// internal void DestroyRenderer() { // Destroy native UI renderer if (_uiRenderer != null) { NoesisGUISystem.RemovePanel(this); _uiRenderer.Destroy(); _uiRenderer = null; } }
//////////////////////////////////////////////////////////////////////////////////////////////// internal static void Create() { if (!_isCreated) { GameObject go = new GameObject("NoesisGUISystem"); _isCreated = true; go.AddComponent<NoesisGUISystem>(); _instance = go.GetComponent<NoesisGUISystem>(); } }