| 1 | /* |
| 2 | * Copyright (C) 2006 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include "BitmapImage.h" |
| 29 | #include "GUniquePtrGtk.h" |
| 30 | #include "GdkCairoUtilities.h" |
| 31 | #include "SharedBuffer.h" |
| 32 | #include <cairo.h> |
| 33 | #include <gtk/gtk.h> |
| 34 | #include <wtf/glib/GRefPtr.h> |
| 35 | #include <wtf/glib/GUniquePtr.h> |
| 36 | |
| 37 | namespace WebCore { |
| 38 | |
| 39 | static Ref<Image> loadImageFromGResource(const char* iconName) |
| 40 | { |
| 41 | auto icon = BitmapImage::create(); |
| 42 | GUniquePtr<char> path(g_strdup_printf("/org/webkitgtk/resources/images/%s" , iconName)); |
| 43 | GRefPtr<GBytes> data = adoptGRef(g_resources_lookup_data(path.get(), G_RESOURCE_LOOKUP_FLAGS_NONE, nullptr)); |
| 44 | ASSERT(data); |
| 45 | icon->setData(SharedBuffer::create(static_cast<const unsigned char*>(g_bytes_get_data(data.get(), nullptr)), g_bytes_get_size(data.get())), true); |
| 46 | return icon; |
| 47 | } |
| 48 | |
| 49 | static Ref<SharedBuffer> loadResourceSharedBuffer(const char* filename) |
| 50 | { |
| 51 | GUniqueOutPtr<gchar> content; |
| 52 | gsize length; |
| 53 | if (!g_file_get_contents(filename, &content.outPtr(), &length, nullptr)) |
| 54 | return SharedBuffer::create(); |
| 55 | |
| 56 | return SharedBuffer::create(content.get(), length); |
| 57 | } |
| 58 | |
| 59 | void BitmapImage::invalidatePlatformData() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | static Ref<Image> loadMissingImageIconFromTheme(const char* name) |
| 64 | { |
| 65 | int iconSize = g_str_has_suffix(name, "@2x" ) ? 32 : 16; |
| 66 | auto icon = BitmapImage::create(); |
| 67 | GUniquePtr<GtkIconInfo> iconInfo(gtk_icon_theme_lookup_icon(gtk_icon_theme_get_default(), GTK_STOCK_MISSING_IMAGE, iconSize, GTK_ICON_LOOKUP_NO_SVG)); |
| 68 | if (iconInfo) { |
| 69 | auto buffer = loadResourceSharedBuffer(gtk_icon_info_get_filename(iconInfo.get())); |
| 70 | icon->setData(WTFMove(buffer), true); |
| 71 | return icon; |
| 72 | } |
| 73 | |
| 74 | return loadImageFromGResource(name); |
| 75 | } |
| 76 | |
| 77 | Ref<Image> Image::loadPlatformResource(const char* name) |
| 78 | { |
| 79 | return g_str_has_prefix(name, "missingImage" ) ? loadMissingImageIconFromTheme(name) : loadImageFromGResource(name); |
| 80 | } |
| 81 | |
| 82 | GdkPixbuf* BitmapImage::getGdkPixbuf() |
| 83 | { |
| 84 | RefPtr<cairo_surface_t> surface = nativeImageForCurrentFrame(); |
| 85 | return surface ? cairoSurfaceToGdkPixbuf(surface.get()) : 0; |
| 86 | } |
| 87 | |
| 88 | } |
| 89 | |