Esempio n. 1
0
        private IAssetServer LoadAssetDll(string dllName)
        {
            Assembly     pluginAssembly = Assembly.LoadFrom(dllName);
            IAssetServer server         = null;

            foreach (Type pluginType in pluginAssembly.GetTypes())
            {
                if (pluginType.IsPublic)
                {
                    if (!pluginType.IsAbstract)
                    {
                        Type typeInterface = pluginType.GetInterface("IAssetPlugin", true);

                        if (typeInterface != null)
                        {
                            IAssetPlugin plug = (IAssetPlugin)Activator.CreateInstance(pluginAssembly.GetType(pluginType.ToString()));
                            server = plug.GetAssetServer();
                            break;
                        }

                        typeInterface = null;
                    }
                }
            }
            pluginAssembly = null;
            return(server);
        }
Esempio n. 2
0
        protected override AttributedStringBaseFontWrapper Convert(string value, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter == null)
            {
                return(DefaultWrapper(value));
            }
            try {
                string fontName = parameter.ToString();
                if (string.IsNullOrWhiteSpace(fontName))
                {
                    return(DefaultWrapper(value));
                }

                if (_assetPlugin == null)
                {
                    _assetPlugin = MvvmCross.Mvx.IoCProvider.Resolve <IAssetPlugin>();
                }
                if (_context == null)
                {
                    _context = MvvmCross.Mvx.IoCProvider.Resolve <IMvxAndroidCurrentTopActivity>().Activity.BaseContext;
                }

                _extendedFont = _assetPlugin.GetFontByName(fontName) as Font;

                string cleanText = string.Empty;
                List <FontIndexPair> blockIndexes = AttributedFontHelper.GetFontTextBlocks(value, fontName, _assetPlugin, out cleanText);

                SpannableString converted = new SpannableString(cleanText);

                foreach (FontIndexPair block in blockIndexes)
                {
                    SetAttributed(converted, block, _extendedFont);
                }

                return(new AttributedStringBaseFontWrapper()
                {
                    SpannableString = converted, Font = _extendedFont, ContainsClickable = _containsLink, ClickableFont = _clickableFont
                });
            }
            catch (Exception e) {
                MvxBindingLog.Instance.Error(e.Message);
            }

            return(DefaultWrapper(value));
        }
        private object ConvertValue(object value, object parameter, CultureInfo culture)
        {
            if (value is IAssetPlugin)
            {
                _plugin = value as AssetPlugin;
            }
            else if (_plugin == null)
            {
                //try to resolve it. Not ideal but sometimes necessary within simpel cells
                _plugin = MvvmCross.Mvx.IoCProvider.Resolve <IAssetPlugin>();

                MvxPluginLog.Instance.Trace("AssetProvider not available for Color conversion. Resolved it");
            }

            if (_plugin != null)
            {
                try
                {
                    if (parameter != null)
                    {
                        return(GetColorByName(parameter.ToString()));
                    }

                    if (value != null && value is string)
                    {
                        return(GetColorByName(value.ToString()));
                    }
                }
                catch
                {
                    MvxPluginLog.Instance.Warn($"Failed to convert '{(parameter?.ToString() ?? value?.ToString())}' into a color");
                }
            }

            return(null);
        }
        public static List <FontIndexPair> GetFontTextBlocks(string text, string fontName, IAssetPlugin assetPlugin, out string cleanText)
        {
            cleanText = string.Empty;

            if (string.IsNullOrWhiteSpace(fontName))
            {
                return(null);
            }

            //this is a sample <h1>text and</h1> it ends here <h2>more stuff</h2>
            //0				   1  2        3   4

            var fontTextBlocks = IterateThroughTags(assetPlugin, text, fontName);

            //create a clean text and convert the block fontTag pairs to indexTagPairs do that we know which text we need to decorate without tags present
            List <FontIndexPair> blockIndexes = new List <FontIndexPair>();

            int previousIndex = 0;

            foreach (FontTextPair block in fontTextBlocks)
            {
                cleanText = $"{cleanText}{block.Text}";
                blockIndexes.Add(new FontIndexPair()
                {
                    FontTag = block.FontTag, StartIndex = previousIndex, EndIndex = cleanText.Length, TagProperties = block.TagProperties
                });
                previousIndex = cleanText.Length;
            }

            return(blockIndexes);
        }
        private static List <FontTextPair> IterateThroughTags(IAssetPlugin assetPlugin, string text, string fontName)
        {
            //find the first text
            int findIndex = 0;

            List <FontTextPair> fontTextBlocks = new List <FontTextPair>();

            int beginTagStartIndex = -1;
            int beginTagEndIndex   = -1;

            int endTagStartIndex = -1;
            int endTagEndIndex   = -1;

            //Start searching for tags
            bool foundTag         = true;
            int  previousBeginTag = -1;
            Dictionary <string, string> tagProperties;
            bool skippedUnknownTag = false;

            while (foundTag)
            {
                FontTag fontTag = null;
                tagProperties = new Dictionary <string, string>();

                //find the end of the tag
                beginTagStartIndex = text.IndexOf('<', findIndex);

                string tag    = string.Empty;
                string endTag = string.Empty;

                if (beginTagStartIndex != -1)
                {
                    beginTagEndIndex = text.IndexOf('>', beginTagStartIndex);

                    if (beginTagEndIndex != -1)
                    {
                        //there's a tag, get the description
                        tag = text.Substring(beginTagStartIndex + 1, beginTagEndIndex - beginTagStartIndex - 1);

                        tagProperties = GetTagProperties(ref tag);

                        var tagFont = assetPlugin.GetFontByTagWithTag(fontName, tag, out fontTag);

                        if (tagFont == null)
                        {
                            //if the font is not found, let it remain in the text and embed it in another block
                            //beginTagStartIndex = previousBeginTag;
                            if (findIndex == 0)
                            {
                                previousBeginTag = 0;
                            }
                            findIndex         = beginTagEndIndex + 1;
                            skippedUnknownTag = true;
                            continue;
                        }

                        endTag           = $"</{tag}>";
                        endTagStartIndex = text.IndexOf(endTag, beginTagEndIndex);

                        //end tag not found
                        if (endTagStartIndex == -1)
                        {
                            throw new Exception($"No matching end tag found in {text}");
                        }

                        endTagEndIndex = endTagStartIndex + (endTag.Length - 1);

                        if (beginTagStartIndex != 0)
                        {
                            var startIndex = findIndex;
                            if (skippedUnknownTag)
                            {
                                startIndex = previousBeginTag;
                            }
                            //in case the first block has no tag, add an empty block
                            fontTextBlocks.Add(new FontTextPair()
                            {
                                Text = text.Substring(startIndex, beginTagStartIndex - startIndex), FontTag = null
                            });
                        }

                        fontTextBlocks.Add(new FontTextPair()
                        {
                            Text = text.Substring(beginTagEndIndex + 1, endTagStartIndex - beginTagEndIndex - 1), FontTag = fontTag, TagProperties = tagProperties
                        });
                        findIndex = endTagEndIndex + 1;
                    }
                }
                else
                {
                    foundTag = false;
                }

                skippedUnknownTag = false;
                previousBeginTag  = beginTagStartIndex;
            }

            //check if the end tag is the last character, if not add a final block till the end
            if (endTagEndIndex < text.Length - 1)
            {
                fontTextBlocks.Add(new FontTextPair()
                {
                    Text = text.Substring(endTagEndIndex + 1, text.Length - endTagEndIndex - 1), FontTag = null
                });
            }

            return(fontTextBlocks);
        }