public HRESULT GetCurrentFontFile(out IDWriteFontFile fontFile)
            {
                DWriteFontFile file;

                try
                {
                    file = _enumerator.Current;
                }
                catch (Exception e)
                {
                    ComError.SetError(e.GetAllMessages());
                    fontFile = null;
                    return(HRESULTS.DISP_E_EXCEPTION);
                }

                if (file is DWriteFontStreamFile sf && sf.FilePath != null)
                {
                    using (var mem = new ComMemory(Marshal.SizeOf <long>()))
                    {
                        if (sf.LastWriteTime.HasValue)
                        {
                            Marshal.WriteInt64(mem.Pointer, sf.LastWriteTime.Value.ToFileTime());
                        }

                        var ptr = sf.LastWriteTime.HasValue ? mem.Pointer : IntPtr.Zero;
                        return(_factory.CreateFontFileReference(sf.FilePath, ptr, out fontFile));
                    }
                }

                var stream = new FontFileStream(file);

                _loader.AddStream(stream);
                return(_factory.CreateCustomFontFileReference(stream.Key, (uint)stream.KeySize, _loader, out fontFile));
            }
 public HRESULT MoveNext(out bool hasCurrentFile)
 {
     try
     {
         hasCurrentFile = _enumerator.MoveNext();
         return(HRESULTS.S_OK);
     }
     catch (Exception e)
     {
         ComError.SetError(e.GetAllMessages());
         hasCurrentFile = false;
         return(HRESULTS.DISP_E_EXCEPTION);
     }
 }
Esempio n. 3
0
        public Exception GetException()
        {
            if (Value == (int)HRESULTS.DISP_E_EXCEPTION)
            {
                var error = ComError.GetError();
                if (error != null)
                {
                    return(error);
                }
            }

            if (Value < 0)
            {
                return(new Win32Exception(Value));
            }

            return(null);
        }
        HRESULT IDWriteFontCollectionLoader.CreateEnumeratorFromKey(IDWriteFactory factory, IntPtr collectionKey, uint collectionKeySize, out IDWriteFontFileEnumerator fontFileEnumerator)
        {
            var func = EnumerableFunc;

            if (func == null)
            {
                fontFileEnumerator = null;
                ComError.SetError(nameof(EnumerableFunc) + " was not set.");
                return(HRESULTS.DISP_E_EXCEPTION);
            }

            byte[] key;
            if (collectionKey == IntPtr.Zero || collectionKeySize == 0)
            {
                key = null;
            }
            else
            {
                key = new byte[collectionKeySize];
                Marshal.Copy(collectionKey, key, 0, (int)collectionKeySize);
            }

            var enumerable = func(factory, key);

            if (enumerable == null)
            {
                fontFileEnumerator = null;
                ComError.SetError(nameof(EnumerableFunc) + " returned nothing.");
                return(HRESULTS.DISP_E_EXCEPTION);
            }

            var enumerator = enumerable.GetEnumerator();

            if (enumerator == null)
            {
                fontFileEnumerator = null;
                ComError.SetError(nameof(EnumerableFunc) + " returned a null enumerator.");
                return(HRESULTS.DISP_E_EXCEPTION);
            }

            fontFileEnumerator = new FontFileEnumerator(factory, _loader, enumerator);
            return(HRESULTS.S_OK);
        }
            HRESULT IDWriteFontFileStream.ReadFileFragment(out IntPtr fragmentStart, ulong fileOffset, ulong fragmentSize, out IntPtr fragmentContext)
            {
                fragmentStart   = IntPtr.Zero;
                fragmentContext = IntPtr.Zero;
                var bytes = _file.ReadFileFragment((long)fileOffset, (int)fragmentSize, out var read);

                if (bytes.IsEmpty() || read == 0)
                {
                    return(HRESULTS.E_FAIL);
                }

                if (read < 0 || read > bytes.Length)
                {
                    ComError.SetError("Invalid ReadFileFragment implementation.");
                    return(HRESULTS.DISP_E_EXCEPTION);
                }

                fragmentContext = Marshal.AllocHGlobal(read);
                fragmentStart   = fragmentContext;
                Marshal.Copy(bytes, 0, fragmentContext, read);
                return(HRESULTS.S_OK);
            }