Exemple #1
0
 public InstalledTypefaceCollection()
 {
     //-----------------------------------------------------
     //init wellknown subfam
     _regular = CreateCreateNewGroup(TypefaceStyle.Regular, "regular", "normal");
     _italic  = CreateCreateNewGroup(TypefaceStyle.Italic, "Italic", "italique");
     //
     _bold = CreateCreateNewGroup(TypefaceStyle.Bold, "bold");
     //
     _bold_italic = CreateCreateNewGroup(TypefaceStyle.Bold | TypefaceStyle.Italic, "bold italic");
     //
 }
Exemple #2
0
        InstalledTypefaceGroup CreateCreateNewGroup(TypefaceStyle installedFontStyle, params string[] names)
        {
            //create font group
            var fontGroup = new InstalledTypefaceGroup();

            //single dic may be called by many names
            foreach (string name in names)
            {
                string upperCaseName = name.ToUpper();
                //register name
                //should not duplicate
                _subFamToFontGroup.Add(upperCaseName, fontGroup);
            }
            _allGroups.Add(fontGroup);
            return(fontGroup);
        }
Exemple #3
0
        bool Register(InstalledTypeface newTypeface)
        {
            InstalledTypefaceGroup selectedFontGroup = null;

            string fontSubFamUpperCaseName = newTypeface.TypographicFontSubFamily;
            bool   use_typographicSubFam   = true;

            if (fontSubFamUpperCaseName == null)
            {
                //switch to FontSubFamily, this should not be null!
                fontSubFamUpperCaseName = newTypeface.FontSubFamily;
                use_typographicSubFam   = false;
            }
            fontSubFamUpperCaseName = fontSubFamUpperCaseName.ToUpper();
            //--------------

            switch (newTypeface.TypefaceStyle)
            {
            default:
            {
                if (!_subFamToFontGroup.TryGetValue(fontSubFamUpperCaseName, out selectedFontGroup))
                {
                    //create new group, we don't known this font group before
                    //so we add to 'other group' list
                    selectedFontGroup = new InstalledTypefaceGroup();
#if DEBUG
                    selectedFontGroup.dbugGroupName = fontSubFamUpperCaseName;
#endif
                    _subFamToFontGroup.Add(fontSubFamUpperCaseName, selectedFontGroup);
                    _allGroups.Add(selectedFontGroup);
                }
            }
            break;

            case TypefaceStyle.Bold:
                selectedFontGroup = _bold;
                break;

            case TypefaceStyle.Italic:
                selectedFontGroup = _italic;
                break;

            case TypefaceStyle.Regular:
            {
                selectedFontGroup = _regular;

                if (fontSubFamUpperCaseName != "REGULAR" &&
                    !_subFamToFontGroup.TryGetValue(fontSubFamUpperCaseName, out selectedFontGroup))
                {
                    //create new group, we don't known this font group before
                    //so we add to 'other group' list
                    selectedFontGroup = new InstalledTypefaceGroup();
#if DEBUG
                    selectedFontGroup.dbugGroupName = fontSubFamUpperCaseName;
#endif
                    _subFamToFontGroup.Add(fontSubFamUpperCaseName, selectedFontGroup);
                    _allGroups.Add(selectedFontGroup);
                }
            }
            break;

            case (TypefaceStyle.Bold | TypefaceStyle.Italic):
                selectedFontGroup = _bold_italic;
                break;
            }

            //------------------
            //for font management
            //we use 'typographic family name' if avaliable,
            string register_name          = newTypeface.TypographicFamilyName;
            bool   use_typographicFontFam = true;
            if (register_name == null)
            {
                //switch to font name, this should not be null!
                register_name          = newTypeface.FontName;
                use_typographicFontFam = false;
            }

            register_name = register_name.ToUpper(); //***
            bool register_result = false;

            if (selectedFontGroup.TryGetValue(register_name, out InstalledTypeface found))
            {
                //TODO:
                //we already have this font name
                //(but may be different file
                //we let user to handle it
                if (_fontNameDuplicatedHandler != null)
                {
                    switch (_fontNameDuplicatedHandler(found, newTypeface))
                    {
                    default:
                        throw new NotSupportedException();

                    case FontNameDuplicatedDecision.Skip:
                        break;

                    case FontNameDuplicatedDecision.Replace:
                        selectedFontGroup.Replace(register_name, newTypeface);
                        register_result = true;
                        break;
                    }
                }
            }
            else
            {
                selectedFontGroup.AddFont(register_name, newTypeface);
                register_result = true;
            }

            if (use_typographicFontFam &&
                newTypeface.FontName != newTypeface.TypographicFamilyName &&
                newTypeface.TypefaceStyle == TypefaceStyle.Regular)
            {
                //in this case, the code above register the typeface with TypographicFamilyName
                //so we register this typeface with original name too
                _otherFontNames.Add(newTypeface.FontName.ToUpper(), newTypeface);
            }

            return(register_result);
        }
        bool Register(InstalledTypeface newTypeface)
        {
            InstalledTypefaceGroup selectedFontGroup = null;

            switch (newTypeface.TypefaceStyle)
            {
            default:
            {
                string fontSubFamUpperCaseName = newTypeface.FontSubFamily.ToUpper();
                if (!_subFamToFontGroup.TryGetValue(fontSubFamUpperCaseName, out selectedFontGroup))
                {
                    //create new group, we don't known this font group before
                    //so we add to 'other group' list
                    selectedFontGroup = new InstalledTypefaceGroup();
                    _subFamToFontGroup.Add(fontSubFamUpperCaseName, selectedFontGroup);
                    _allGroups.Add(selectedFontGroup);
                }
            }
            break;

            case TypefaceStyle.Bold:
                selectedFontGroup = _bold;
                break;

            case TypefaceStyle.Italic:
                selectedFontGroup = _italic;
                break;

            case TypefaceStyle.Regular:
                selectedFontGroup = _regular;
                break;

            case (TypefaceStyle.Bold | TypefaceStyle.Italic):
                selectedFontGroup = _bold_italic;
                break;
            }

            //
            string fontNameUpper = newTypeface.FontName.ToUpper();

            InstalledTypeface found;

            if (selectedFontGroup.TryGetValue(fontNameUpper, out found))
            {
                //TODO:
                //we already have this font name
                //(but may be different file
                //we let user to handle it
                if (_fontNameDuplicatedHandler != null)
                {
                    switch (_fontNameDuplicatedHandler(found, newTypeface))
                    {
                    default:
                        throw new NotSupportedException();

                    case FontNameDuplicatedDecision.Skip:
                        return(false);

                    case FontNameDuplicatedDecision.Replace:
                        selectedFontGroup.Replace(newTypeface);
                        return(true);
                    }
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                selectedFontGroup.AddFont(newTypeface);
                return(true);
            }
        }
        bool Register(InstalledTypeface instTypeface)
        {
            //[A] ---------------------------------------
            string register_name = instTypeface.TypographicFamilyName;

            //use typographic name first
            if (register_name == null)
            {
                //switch to font name, this should not be null!
                register_name = instTypeface.FontName;
            }

            string reg_name_only = register_name.ToUpper();

            register_name = reg_name_only + "," + instTypeface.TypefaceStyle + "," + instTypeface.WeightClass; //***
            bool register_result = false;

            if (_all3.TryGetValue(register_name, out InstalledTypeface found))
            {
                //TODO:
                //we already have this font name
                //(but may be different file
                //we let user to handle it
                if (_fontNameDuplicatedHandler != null)
                {
                    switch (_fontNameDuplicatedHandler(found, instTypeface))
                    {
                    default:
                        throw new NotSupportedException();

                    case FontNameDuplicatedDecision.Skip:
                        break;

                    case FontNameDuplicatedDecision.Replace:
                        //selectedFontGroup.Replace(register_name, instTypeface);
                        _all3[register_name] = instTypeface;
                        register_result      = true;
                        break;
                    }
                }
            }
            else
            {
                _all3.Add(register_name, instTypeface);
                register_result = true;
            }


            if (!register_result)
            {
                return(false);
            }                                      //early exit


            //[B]---------------------------------------
            //register other names...



            if (!_regNames.TryGetValue(reg_name_only, out InstalledTypefaceGroup regNameGroup))
            {
                regNameGroup = new InstalledTypefaceGroup(reg_name_only, instTypeface);
                _regNames.Add(reg_name_only, regNameGroup);
            }
            else
            {
                regNameGroup.AddInstalledTypeface(instTypeface);
            }


            string fontName = instTypeface.FontName.ToUpper();

            if (fontName != null && fontName != reg_name_only)
            {
                if (!_otherNames.TryGetValue(fontName, out InstalledTypefaceGroup found2))
                {
                    found2 = new InstalledTypefaceGroup(fontName, instTypeface);
                    _otherNames.Add(fontName, found2);
                }
                else
                {
                    found2.AddInstalledTypeface(instTypeface);
                }
            }

            //-----
            string typographicName = instTypeface.TypographicFamilyName?.ToUpper();

            if (typographicName != null && typographicName != reg_name_only && typographicName != fontName)
            {
                if (!_otherNames.TryGetValue(typographicName, out InstalledTypefaceGroup found2))
                {
                    found2 = new InstalledTypefaceGroup(typographicName, instTypeface);
                    _otherNames.Add(typographicName, found2);
                }
                else
                {
                    found2.AddInstalledTypeface(instTypeface);
                }
            }
            //-----
            string postScriptName = instTypeface.PostScriptName?.ToUpper();

            if (postScriptName != null && postScriptName != fontName && postScriptName != typographicName)
            {
                if (!_otherNames.TryGetValue(postScriptName, out InstalledTypefaceGroup found2))
                {
                    found2 = new InstalledTypefaceGroup(postScriptName, instTypeface);
                    _otherNames.Add(postScriptName, found2);
                }
                else
                {
                    found2.AddInstalledTypeface(instTypeface);
                }
            }


            //classified by its weight
            GetInstalledTypefaceByWeightClass(instTypeface.WeightClass).Add(instTypeface);


            //register by its path (if available)
            if (instTypeface.FontPath != null &&
                !_installedTypefacesByFilenames.ContainsKey(instTypeface.FontPath)) //beware case-sensitive!
            {
                _installedTypefacesByFilenames.Add(instTypeface.FontPath, instTypeface);
            }

            return(true);
        }