예제 #1
0
        private static bool SaveFont()
        {
            try
            {
                string tempPath    = Path.GetTempPath();
                string tempPngFile = Path.Combine(tempPath, "temptexture.png");
                string tempDdsFile = Path.Combine(tempPath, "temptexture.dds");

                const string filename    = "lyrics.dds";
                string       ddsFilename = filename;
                string       pngFilename = filename;
                bool         keepPngFile = false;

                if (filename.EndsWith(".dds"))
                {
                    pngFilename = Path.ChangeExtension(filename, ".png");
                }
                else
                {
                    ddsFilename = Path.ChangeExtension(filename, ".dds");
                    keepPngFile = true;
                }

                string definitionFilename = Path.ChangeExtension(filename, ".glyphs.xml");

                GlyphDefinitions.Save(definitionFilename, FontGenerator);
                BitmapFunctions.SaveImage(FontCanvas, tempPngFile, TextureWidth, TextureHeight);

                using (Process nvdxtProcess = new Process())
                {
                    nvdxtProcess.StartInfo.UseShellExecute = false;
                    nvdxtProcess.StartInfo.CreateNoWindow  = true;
                    nvdxtProcess.StartInfo.FileName        = "nvdxt.exe";
                    nvdxtProcess.StartInfo.Arguments       = $"-file \"{tempPngFile}\" -output \"{tempDdsFile}\" -quality_highest -dxt5 -nomipmap -overwrite -forcewrite";

                    nvdxtProcess.Start();
                    nvdxtProcess.WaitForExit();
                }

                File.Delete(ddsFilename);
                File.Move(tempDdsFile, ddsFilename);

                if (keepPngFile)
                {
                    File.Delete(pngFilename);
                    File.Move(tempPngFile, pngFilename);
                }
                else
                {
                    File.Delete(tempPngFile);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error saving texture file.\n" + ex.Message);
                return(false);
            }

            return(true);
        }
        /// <summary>
        /// Tests whether the glyph height of the capital letter X using the selected font is larger than the default glyph height.
        /// </summary>
        /// <returns>Returns false if the user cancels the operation.</returns>
        private bool TestGlyphSize()
        {
            Grid testGrid = new Grid();
            int  fontSize = Math.Max(FontSettings.FontSize, FontSettings.KanjiFontSize);
            Run  testRun  = new Run
            {
                Text       = "X",
                FontFamily = FontSettings.FontFamily,
                FontSize   = fontSize,
                FontWeight = FontSettings.FontWeight,
                Foreground = Brushes.Red
            };

            TextBlock testText = new TextBlock(testRun)
            {
                Margin = new Thickness(GlyphHorizontalMargin, 0, GlyphHorizontalMargin, 0),
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment   = VerticalAlignment.Center
            };

            testGrid.Children.Add(testText);
            testGrid.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
            testGrid.Arrange(new Rect(testGrid.DesiredSize));

            BitmapSource glyphBitmap = BitmapFunctions.CaptureScreen(testGrid, 96.0, 96.0);
            Rect         innerRect   = DetectInnerRectFromPixels(glyphBitmap, new Rect(0.0, 0.0, testGrid.ActualWidth, testGrid.ActualHeight));

            //if (testGrid.ActualHeight > GlyphHeight)
            if (innerRect.Height > GlyphHeight)
            {
                int newGlyphHeight = (int)Math.Ceiling(innerRect.Height);

                /*string message = Properties.Resources.Error_Generation_GlyphSizeTooLargeMessage +
                 *  Environment.NewLine + Environment.NewLine +
                 *  string.Format(Properties.Resources.Error_Generation_GlyphSizeConfirmation, newGlyphHeight);
                 *
                 * var answer = MessageBox.Show(
                 *  message,
                 *  Properties.Resources.Error_Generation_GlyphSizeTooLargeTitle,
                 *  MessageBoxButton.YesNo,
                 *  MessageBoxImage.Information);
                 *
                 * if (answer == MessageBoxResult.No)
                 *  return false;
                 * else*/
                GlyphHeight = newGlyphHeight;
            }

            return(true);
        }
        private void AddInnerRect(string glyph, Grid glyphGrid, TextBlock innerText, double x, double y)
        {
            Rect         innerRect;
            BitmapSource glyphBitmap = BitmapFunctions.CaptureScreen(glyphGrid, 96.0, 96.0);

            if (string.IsNullOrWhiteSpace(glyph) || glyphBitmap == null || !UseAccurateInnerRects)
            {
                double innerWidth = innerText.ActualWidth + SpacingAdjustment;
                if (innerWidth < 0)
                {
                    innerWidth = 0;
                }

                innerRect = CreateInnerRect(
                    x: x,
                    y: y,
                    outerHeight: OuterRects[glyph].Height,
                    innerWidth: innerWidth,
                    innerHeight: innerText.ActualHeight);
            }
            else
            {
                innerRect = DetectInnerRectFromPixels(glyphBitmap, OuterRects[glyph]);

                // Create new outer rect to keep horizontal margin constant
                OuterRects[glyph] = new Rect(
                    innerRect.X - GlyphHorizontalMargin,
                    OuterRects[glyph].Y,
                    innerRect.Width + (GlyphHorizontalMargin * 2),
                    OuterRects[glyph].Height);

                RectCanvas.Children.RemoveAt(RectCanvas.Children.Count - 1);
                CreateOuterRectangleForCanvas(glyph, OuterRects[glyph]);
            }

            /*if(inner.Height > DefaultGlyphHeight)
             * {
             *  var answer = MessageBox.Show(
             *      "The glyph height for the selected font is larger than the default (52 pixels).\n\n",
             *      "Glyph Size Too Large");
             * }*/

            Brush rectBrush = Brushes.DarkViolet;
            Rect  outer     = OuterRects[glyph];

            // Check if the inner rect is larger than the outer
            if (innerRect.Top < outer.Top ||
                innerRect.Left < outer.Left ||
                innerRect.Bottom > outer.Bottom ||
                innerRect.Right > outer.Right)
            {
                if (!RectErrorMessageShown)
                {
                    //MessageBox.Show(Properties.Resources.Error_Generation_OverlappingBoundingRects, Properties.Resources.Error_Warning);
                    RectErrorMessageShown = true;
                }

                // Resize the width of the inner rect to fit inside the outer
                if (innerRect.Right > outer.Right)
                {
                    innerRect = new Rect(
                        innerRect.X,
                        innerRect.Y,
                        innerRect.Width - (outer.Right - innerRect.Right),
                        innerRect.Height);
                }

                rectBrush = Brushes.Red;
            }

            InnerRects.Add(glyph, innerRect);

            CreateInnerRectangleForCanvas(innerRect, rectBrush);
        }