<commit_before>﻿using OpenKh.Bbs;
using OpenKh.Tools.Common;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Xe.Drawing;
using Xe.Tools;
using Xe.Tools.Wpf.Commands;

namespace OpenKh.Tools.CtdEditor.ViewModels
{
    public class FontEditorViewModel : BaseNotifyPropertyChanged
    {
        private readonly FontsArc _fonts;
        private CharacterViewModel[] _characters;
        private FontEntryViewModel _selectedFont;
        private CharacterViewModel _selectedCharacter;
        private ISurface _surface1;
        private ISurface _surface2;
        private bool orderCharacters;

        public FontEditorViewModel(FontsArc fonts)
        {
            _fonts = fonts;
            Fonts = new[]
            {
                new FontEntryViewModel(fonts.FontCmd),
                new FontEntryViewModel(fonts.FontHelp),
                new FontEntryViewModel(fonts.FontMenu),
                new FontEntryViewModel(fonts.FontMes),
                new FontEntryViewModel(fonts.FontNumeral)
            };

            DrawingContext = new DrawingDirect3D();
            DrawBegin = new RelayCommand(_ =>
            {
                if (_selectedFont?.Font1 == null ||
                    SelectedCharacter == null)
                    return;

                if (_surface1 == null)
                    CreateFontSurfaces();

                if (_surface1 == null)
                    return;

                DrawingContext.Clear(Color.Black);
                PrintCharacter(SelectedCharacter);
                DrawingContext.Flush();
            });
        }

        public FontEntryViewModel[] Fonts { get; }
        public FontEntryViewModel SelectedFont
        {
            get => _selectedFont;
            set
            {
                _selectedFont = value;
                _characters = _selectedFont.CharactersInfo.Select(x => new CharacterViewModel(x)).ToArray();

                DestroyFontSurfaces();
                OnPropertyChanged(nameof(IsFontSeleted));
                OnPropertyChanged(nameof(Characters));
            }
        }
        public bool IsFontSeleted => SelectedFont != null;

        public IEnumerable<CharacterViewModel> Characters =>
            OrderCharacters ? _characters.OrderBy(x => x.Id) : _characters as IEnumerable<CharacterViewModel>;
        public CharacterViewModel SelectedCharacter
        {
            get => _selectedCharacter;
            set
            {<commit_msg>Fix a crash when re-ordering font characters without any font loaded<commit_after>﻿using OpenKh.Bbs;
using OpenKh.Tools.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using Xe.Drawing;
using Xe.Tools;
using Xe.Tools.Wpf.Commands;

namespace OpenKh.Tools.CtdEditor.ViewModels
{
    public class FontEditorViewModel : BaseNotifyPropertyChanged
    {
        private readonly FontsArc _fonts;
        private CharacterViewModel[] _characters;
        private FontEntryViewModel _selectedFont;
        private CharacterViewModel _selectedCharacter;
        private ISurface _surface1;
        private ISurface _surface2;
        private bool orderCharacters;

        public FontEditorViewModel(FontsArc fonts)
        {
            _fonts = fonts;
            Fonts = new[]
            {
                new FontEntryViewModel(fonts.FontCmd),
                new FontEntryViewModel(fonts.FontHelp),
                new FontEntryViewModel(fonts.FontMenu),
                new FontEntryViewModel(fonts.FontMes),
                new FontEntryViewModel(fonts.FontNumeral)
            };

            DrawingContext = new DrawingDirect3D();
            DrawBegin = new RelayCommand(_ =>
            {
                if (_selectedFont?.Font1 == null ||
                    SelectedCharacter == null)
                    return;

                if (_surface1 == null)
                    CreateFontSurfaces();

                if (_surface1 == null)
                    return;

                DrawingContext.Clear(Color.Black);
                PrintCharacter(SelectedCharacter);
                DrawingContext.Flush();
            });
        }

        public FontEntryViewModel[] Fonts { get; }
        public FontEntryViewModel SelectedFont
        {
            get => _selectedFont;
            set
            {
                _selectedFont = value;
                _characters = _selectedFont.CharactersInfo.Select(x => new CharacterViewModel(x)).ToArray();

                DestroyFontSurfaces();
                OnPropertyChanged(nameof(IsFontSeleted));
                OnPropertyChanged(nameof(Characters));
            }
        }
        public bool IsFontSeleted => SelectedFont != null;

        public IEnumerable<CharacterViewModel> Characters =>
            (OrderCharacters ? _characters?.OrderBy(x => x.Id) : _characters as IEnumerable<CharacterViewModel>)
            ?? Array.Empty<CharacterViewModel>();

        public CharacterViewModel SelectedCharacter
        {
            get => _selectedCharacter;
            set
            {
{'fraction_non_alphanumeric': 0.0739866194411649, 'fraction_numerical': 0.0023612750885478157, 'mean_word_length': 1.8460246360582306, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '824089', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1165, 'n_words': 259}