1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 19:37:34 +00:00

Libraries: Move to Userland/Libraries/

This commit is contained in:
Andreas Kling 2021-01-12 12:17:30 +01:00
parent dc28c07fa5
commit 13d7c09125
1857 changed files with 266 additions and 274 deletions

View file

@ -0,0 +1,175 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/LogStream.h>
#include <AK/Optional.h>
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Rect.h>
namespace Gfx {
bool AffineTransform::is_identity() const
{
return m_values[0] == 1 && m_values[1] == 0 && m_values[2] == 0 && m_values[3] == 1 && m_values[4] == 0 && m_values[5] == 0;
}
static float hypotenuse(float x, float y)
{
// FIXME: This won't handle overflow :(
return sqrt(x * x + y * y);
}
float AffineTransform::x_scale() const
{
return hypotenuse(m_values[0], m_values[1]);
}
float AffineTransform::y_scale() const
{
return hypotenuse(m_values[2], m_values[3]);
}
AffineTransform& AffineTransform::scale(float sx, float sy)
{
m_values[0] *= sx;
m_values[1] *= sx;
m_values[2] *= sy;
m_values[3] *= sy;
return *this;
}
AffineTransform& AffineTransform::translate(float tx, float ty)
{
m_values[4] += tx * m_values[0] + ty * m_values[2];
m_values[5] += tx * m_values[1] + ty * m_values[3];
return *this;
}
AffineTransform& AffineTransform::multiply(const AffineTransform& other)
{
AffineTransform result;
result.m_values[0] = other.a() * a() + other.b() * c();
result.m_values[1] = other.a() * b() + other.b() * d();
result.m_values[2] = other.c() * a() + other.d() * c();
result.m_values[3] = other.c() * b() + other.d() * d();
result.m_values[4] = other.e() * a() + other.f() * c() + e();
result.m_values[5] = other.e() * b() + other.f() * d() + f();
*this = result;
return *this;
}
AffineTransform& AffineTransform::rotate_radians(float radians)
{
float sin_angle = sinf(radians);
float cos_angle = cosf(radians);
AffineTransform rotation(cos_angle, sin_angle, -sin_angle, cos_angle, 0, 0);
multiply(rotation);
return *this;
}
void AffineTransform::map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const
{
mapped_x = (m_values[0] * unmapped_x + m_values[2] * unmapped_y + m_values[4]);
mapped_y = (m_values[1] * unmapped_x + m_values[3] * unmapped_y + m_values[5]);
}
template<>
IntPoint AffineTransform::map(const IntPoint& point) const
{
float mapped_x;
float mapped_y;
map(point.x(), point.y(), mapped_x, mapped_y);
return { roundf(mapped_x), roundf(mapped_y) };
}
template<>
FloatPoint AffineTransform::map(const FloatPoint& point) const
{
float mapped_x;
float mapped_y;
map(point.x(), point.y(), mapped_x, mapped_y);
return { mapped_x, mapped_y };
}
template<>
IntSize AffineTransform::map(const IntSize& size) const
{
return { roundf(size.width() * x_scale()), roundf(size.height() * y_scale()) };
}
template<>
FloatSize AffineTransform::map(const FloatSize& size) const
{
return { size.width() * x_scale(), size.height() * y_scale() };
}
template<typename T>
static T smallest_of(T p1, T p2, T p3, T p4)
{
return min(min(p1, p2), min(p3, p4));
}
template<typename T>
static T largest_of(T p1, T p2, T p3, T p4)
{
return max(max(p1, p2), max(p3, p4));
}
template<>
FloatRect AffineTransform::map(const FloatRect& rect) const
{
FloatPoint p1 = map(rect.top_left());
FloatPoint p2 = map(rect.top_right().translated(1, 0));
FloatPoint p3 = map(rect.bottom_right().translated(1, 1));
FloatPoint p4 = map(rect.bottom_left().translated(0, 1));
float left = smallest_of(p1.x(), p2.x(), p3.x(), p4.x());
float top = smallest_of(p1.y(), p2.y(), p3.y(), p4.y());
float right = largest_of(p1.x(), p2.x(), p3.x(), p4.x());
float bottom = largest_of(p1.y(), p2.y(), p3.y(), p4.y());
return { left, top, right - left, bottom - top };
}
template<>
IntRect AffineTransform::map(const IntRect& rect) const
{
return enclosing_int_rect(map(FloatRect(rect)));
}
const LogStream& operator<<(const LogStream& stream, const AffineTransform& value)
{
if (value.is_identity())
return stream << "{ Identity }";
return stream << "{ "
<< value.a() << ", "
<< value.b() << ", "
<< value.c() << ", "
<< value.d() << ", "
<< value.e() << ", "
<< value.f() << " }";
}
}

View file

@ -0,0 +1,81 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <AK/LogStream.h>
#include <LibGfx/Forward.h>
namespace Gfx {
class AffineTransform {
public:
AffineTransform()
: m_values { 1, 0, 0, 1, 0, 0 }
{
}
AffineTransform(float a, float b, float c, float d, float e, float f)
: m_values { a, b, c, d, e, f }
{
}
bool is_identity() const;
void map(float unmapped_x, float unmapped_y, float& mapped_x, float& mapped_y) const;
template<typename T>
Point<T> map(const Point<T>&) const;
template<typename T>
Size<T> map(const Size<T>&) const;
template<typename T>
Rect<T> map(const Rect<T>&) const;
float a() const { return m_values[0]; }
float b() const { return m_values[1]; }
float c() const { return m_values[2]; }
float d() const { return m_values[3]; }
float e() const { return m_values[4]; }
float f() const { return m_values[5]; }
float x_scale() const;
float y_scale() const;
AffineTransform& scale(float sx, float sy);
AffineTransform& translate(float tx, float ty);
AffineTransform& rotate_radians(float);
AffineTransform& multiply(const AffineTransform&);
private:
float m_values[6] { 0 };
};
const LogStream& operator<<(const LogStream&, const AffineTransform&);
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2020, Matthew Olsson <matthewcolsson@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
namespace Gfx {
RefPtr<Gfx::Bitmap> load_bmp(const StringView& path);
RefPtr<Gfx::Bitmap> load_bmp_from_memory(const u8*, size_t);
struct BMPLoadingContext;
class BMPImageDecoderPlugin final : public ImageDecoderPlugin {
public:
virtual ~BMPImageDecoderPlugin() override;
BMPImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile() override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ImageFrameDescriptor frame(size_t i) override;
private:
OwnPtr<BMPLoadingContext> m_context;
};
}

View file

@ -0,0 +1,135 @@
/*
* Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Vector.h>
#include <LibGfx/BMPWriter.h>
#include <LibGfx/Bitmap.h>
#include <cstring>
namespace Gfx {
constexpr int bytes_per_pixel = 3;
#define FILE_HEADER_SIZE 14
#define IMAGE_INFORMATION_SIZE 40
#define PIXEL_DATA_OFFSET FILE_HEADER_SIZE + IMAGE_INFORMATION_SIZE
class Streamer {
public:
Streamer(u8* data)
: m_data(data)
{
}
void write_u8(u8 i)
{
*(m_data++) = i;
}
void write_u16(u16 i)
{
*(m_data++) = i & 0xFF;
*(m_data++) = (i >> 8) & 0xFF;
}
void write_u32(u32 i)
{
write_u16(i & 0xFFFF);
write_u16((i >> 16) & 0xFFFF);
}
void write_i32(i32 i)
{
write_u32(static_cast<u32>(i));
}
private:
u8* m_data;
};
static ByteBuffer write_pixel_data(const RefPtr<Bitmap> bitmap, int pixel_row_data_size)
{
int image_size = pixel_row_data_size * bitmap->height();
auto buffer = ByteBuffer::create_uninitialized(image_size);
int current_row = 0;
for (int y = bitmap->height() - 1; y >= 0; --y) {
auto* row = buffer.data() + (pixel_row_data_size * current_row++);
for (int x = 0; x < bitmap->width(); x++) {
auto pixel = bitmap->get_pixel(x, y);
row[x * bytes_per_pixel + 0] = pixel.blue();
row[x * bytes_per_pixel + 1] = pixel.green();
row[x * bytes_per_pixel + 2] = pixel.red();
}
}
return buffer;
}
static ByteBuffer compress_pixel_data(const ByteBuffer& pixel_data, BMPWriter::Compression compression)
{
switch (compression) {
case BMPWriter::Compression::RGB:
return pixel_data;
}
ASSERT_NOT_REACHED();
}
ByteBuffer BMPWriter::dump(const RefPtr<Bitmap> bitmap)
{
int pixel_row_data_size = (bytes_per_pixel * 8 * bitmap->width() + 31) / 32 * 4;
int image_size = pixel_row_data_size * bitmap->height();
auto buffer = ByteBuffer::create_uninitialized(PIXEL_DATA_OFFSET);
auto pixel_data = write_pixel_data(bitmap, pixel_row_data_size);
pixel_data = compress_pixel_data(pixel_data, m_compression);
int file_size = PIXEL_DATA_OFFSET + pixel_data.size();
Streamer streamer(buffer.data());
streamer.write_u8('B');
streamer.write_u8('M');
streamer.write_u32(file_size);
streamer.write_u32(0);
streamer.write_u32(PIXEL_DATA_OFFSET);
streamer.write_u32(IMAGE_INFORMATION_SIZE); // Header size
streamer.write_i32(bitmap->width()); // ImageWidth
streamer.write_i32(bitmap->height()); // ImageHeight
streamer.write_u16(1); // Planes
streamer.write_u16(bytes_per_pixel * 8); // BitsPerPixel
streamer.write_u32((u32)m_compression); // Compression
streamer.write_u32(image_size); // ImageSize
streamer.write_i32(0); // XpixelsPerMeter
streamer.write_i32(0); // YpixelsPerMeter
streamer.write_u32(0); // TotalColors
streamer.write_u32(0); // ImportantColors
buffer.append(pixel_data.data(), pixel_data.size());
return buffer;
}
}

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2020, Ben Jilks <benjyjilks@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/ByteBuffer.h>
namespace Gfx {
class Bitmap;
class BMPWriter {
public:
BMPWriter() = default;
ByteBuffer dump(const RefPtr<Bitmap>);
enum class Compression : u32 {
RGB = 0,
};
inline void set_compression(Compression compression) { m_compression = compression; }
private:
Compression m_compression { Compression::RGB };
};
}

View file

@ -0,0 +1,495 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Checked.h>
#include <AK/Memory.h>
#include <AK/MemoryStream.h>
#include <AK/Optional.h>
#include <AK/SharedBuffer.h>
#include <AK/String.h>
#include <LibGfx/BMPLoader.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/GIFLoader.h>
#include <LibGfx/ICOLoader.h>
#include <LibGfx/JPGLoader.h>
#include <LibGfx/PBMLoader.h>
#include <LibGfx/PGMLoader.h>
#include <LibGfx/PNGLoader.h>
#include <LibGfx/PPMLoader.h>
#include <LibGfx/ShareableBitmap.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>
namespace Gfx {
struct BackingStore {
void* data { nullptr };
size_t pitch { 0 };
size_t size_in_bytes { 0 };
};
size_t Bitmap::minimum_pitch(size_t width, BitmapFormat format)
{
size_t element_size;
switch (determine_storage_format(format)) {
case StorageFormat::Indexed8:
element_size = 1;
break;
case StorageFormat::RGB32:
case StorageFormat::RGBA32:
element_size = 4;
break;
default:
ASSERT_NOT_REACHED();
}
return width * element_size;
}
static bool size_would_overflow(BitmapFormat format, const IntSize& size)
{
if (size.width() < 0 || size.height() < 0)
return true;
// This check is a bit arbitrary, but should protect us from most shenanigans:
if (size.width() >= 32768 || size.height() >= 32768)
return true;
// In contrast, this check is absolutely necessary:
size_t pitch = Bitmap::minimum_pitch(size.width(), format);
return Checked<size_t>::multiplication_would_overflow(pitch, size.height());
}
RefPtr<Bitmap> Bitmap::create(BitmapFormat format, const IntSize& size)
{
auto backing_store = Bitmap::allocate_backing_store(format, size, Purgeable::No);
if (!backing_store.has_value())
return nullptr;
return adopt(*new Bitmap(format, size, Purgeable::No, backing_store.value()));
}
RefPtr<Bitmap> Bitmap::create_purgeable(BitmapFormat format, const IntSize& size)
{
auto backing_store = Bitmap::allocate_backing_store(format, size, Purgeable::Yes);
if (!backing_store.has_value())
return nullptr;
return adopt(*new Bitmap(format, size, Purgeable::Yes, backing_store.value()));
}
RefPtr<Bitmap> Bitmap::create_shareable(BitmapFormat format, const IntSize& size)
{
if (size_would_overflow(format, size))
return nullptr;
const auto pitch = minimum_pitch(size.width(), format);
const auto data_size = size_in_bytes(pitch, size.height());
auto shared_buffer = SharedBuffer::create_with_size(data_size);
if (!shared_buffer)
return nullptr;
return adopt(*new Bitmap(format, shared_buffer.release_nonnull(), size, Vector<RGBA32>()));
}
Bitmap::Bitmap(BitmapFormat format, const IntSize& size, Purgeable purgeable, const BackingStore& backing_store)
: m_size(size)
, m_data(backing_store.data)
, m_pitch(backing_store.pitch)
, m_format(format)
, m_purgeable(purgeable == Purgeable::Yes)
{
ASSERT(!m_size.is_empty());
ASSERT(!size_would_overflow(format, size));
ASSERT(m_data);
ASSERT(backing_store.size_in_bytes == size_in_bytes());
allocate_palette_from_format(format, {});
m_needs_munmap = true;
}
RefPtr<Bitmap> Bitmap::create_wrapper(BitmapFormat format, const IntSize& size, size_t pitch, void* data)
{
if (size_would_overflow(format, size))
return nullptr;
return adopt(*new Bitmap(format, size, pitch, data));
}
RefPtr<Bitmap> Bitmap::load_from_file(const StringView& path)
{
#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) \
return load_##Name(path);
ENUMERATE_IMAGE_FORMATS
#undef __ENUMERATE_IMAGE_FORMAT
return nullptr;
}
Bitmap::Bitmap(BitmapFormat format, const IntSize& size, size_t pitch, void* data)
: m_size(size)
, m_data(data)
, m_pitch(pitch)
, m_format(format)
{
ASSERT(pitch >= minimum_pitch(size.width(), format));
ASSERT(!size_would_overflow(format, size));
// FIXME: assert that `data` is actually long enough!
allocate_palette_from_format(format, {});
}
RefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size)
{
return create_with_shared_buffer(format, move(shared_buffer), size, {});
}
static bool check_size(const IntSize& size, BitmapFormat format, unsigned actual_size)
{
// FIXME: Code duplication of size_in_bytes() and m_pitch
unsigned expected_size_min = Bitmap::minimum_pitch(size.width(), format) * size.height();
unsigned expected_size_max = round_up_to_power_of_two(expected_size_min, PAGE_SIZE);
if (expected_size_min > actual_size || actual_size > expected_size_max) {
// Getting here is most likely an error.
dbgln("Constructing a shared bitmap for format {} and size {}, which demands {} bytes, which rounds up to at most {}.",
static_cast<int>(format),
size,
expected_size_min,
expected_size_max);
dbgln("However, we were given {} bytes, which is outside this range?! Refusing cowardly.", actual_size);
return false;
}
return true;
}
RefPtr<Bitmap> Bitmap::create_with_shared_buffer(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size, const Vector<RGBA32>& palette)
{
if (size_would_overflow(format, size))
return nullptr;
if (!check_size(size, format, shared_buffer->size()))
return {};
return adopt(*new Bitmap(format, move(shared_buffer), size, palette));
}
/// Read a bitmap as described by:
/// - actual size
/// - width
/// - height
/// - format
/// - palette count
/// - palette data (= palette count * RGBA32)
/// - image data (= actual size * u8)
RefPtr<Bitmap> Bitmap::create_from_serialized_byte_buffer(ByteBuffer&& buffer)
{
InputMemoryStream stream { buffer };
unsigned actual_size;
unsigned width;
unsigned height;
BitmapFormat format;
unsigned palette_size;
Vector<RGBA32> palette;
auto read = [&]<typename T>(T& value) {
if (stream.read({ &value, sizeof(T) }) != sizeof(T))
return false;
return true;
};
if (!read(actual_size) || !read(width) || !read(height) || !read(format) || !read(palette_size))
return nullptr;
if (format > BitmapFormat::RGBA32 || format < BitmapFormat::Indexed1)
return nullptr;
if (!check_size({ width, height }, format, actual_size))
return {};
palette.ensure_capacity(palette_size);
for (size_t i = 0; i < palette_size; ++i) {
if (!read(palette[i]))
return {};
}
if (stream.remaining() < actual_size)
return {};
auto data = stream.bytes().slice(stream.offset(), actual_size);
auto bitmap = Bitmap::create(format, { width, height });
if (!bitmap)
return {};
bitmap->m_palette = new RGBA32[palette_size];
memcpy(bitmap->m_palette, palette.data(), palette_size * sizeof(RGBA32));
data.copy_to({ bitmap->scanline(0), bitmap->size_in_bytes() });
return bitmap;
}
ByteBuffer Bitmap::serialize_to_byte_buffer() const
{
auto buffer = ByteBuffer::create_uninitialized(4 * sizeof(unsigned) + sizeof(BitmapFormat) + sizeof(RGBA32) * palette_size(m_format) + size_in_bytes());
OutputMemoryStream stream { buffer };
auto write = [&]<typename T>(T value) {
if (stream.write({ &value, sizeof(T) }) != sizeof(T))
return false;
return true;
};
auto palette = palette_to_vector();
if (!write(size_in_bytes()) || !write((unsigned)size().width()) || !write((unsigned)size().height()) || !write(m_format) || !write((unsigned)palette.size()))
return {};
for (auto& p : palette) {
if (!write(p))
return {};
}
auto size = size_in_bytes();
ASSERT(stream.remaining() == size);
if (stream.write({ scanline(0), size }) != size)
return {};
return buffer;
}
Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer, const IntSize& size, const Vector<RGBA32>& palette)
: m_size(size)
, m_data(shared_buffer->data<void>())
, m_pitch(minimum_pitch(size.width(), format))
, m_format(format)
, m_shared_buffer(move(shared_buffer))
{
ASSERT(!is_indexed() || !palette.is_empty());
ASSERT(!size_would_overflow(format, size));
ASSERT(size_in_bytes() <= static_cast<size_t>(m_shared_buffer->size()));
if (is_indexed(m_format))
allocate_palette_from_format(m_format, palette);
}
RefPtr<Gfx::Bitmap> Bitmap::clone() const
{
RefPtr<Gfx::Bitmap> new_bitmap {};
if (m_purgeable) {
new_bitmap = Bitmap::create_purgeable(format(), size());
} else {
new_bitmap = Bitmap::create(format(), size());
}
if (!new_bitmap) {
return nullptr;
}
ASSERT(size_in_bytes() == new_bitmap->size_in_bytes());
memcpy(new_bitmap->scanline(0), scanline(0), size_in_bytes());
return new_bitmap;
}
RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
{
auto w = this->width();
auto h = this->height();
auto new_bitmap = Gfx::Bitmap::create(this->format(), { h, w });
if (!new_bitmap)
return nullptr;
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Color color;
if (rotation_direction == Gfx::RotationDirection::Left)
color = this->get_pixel(w - i - 1, j);
else
color = this->get_pixel(i, h - j - 1);
new_bitmap->set_pixel(j, i, color);
}
}
return new_bitmap;
}
RefPtr<Gfx::Bitmap> Bitmap::flipped(Gfx::Orientation orientation) const
{
auto w = this->width();
auto h = this->height();
auto new_bitmap = Gfx::Bitmap::create(this->format(), { w, h });
if (!new_bitmap)
return nullptr;
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Color color = this->get_pixel(i, j);
if (orientation == Orientation::Vertical)
new_bitmap->set_pixel(i, h - j - 1, color);
else
new_bitmap->set_pixel(w - i - 1, j, color);
}
}
return new_bitmap;
}
RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
{
if (m_shared_buffer)
return *this;
auto buffer = SharedBuffer::create_with_size(size_in_bytes());
if (!buffer)
return nullptr;
auto bitmap = Bitmap::create_with_shared_buffer(m_format, *buffer, m_size, palette_to_vector());
if (!bitmap)
return nullptr;
memcpy(buffer->data<void>(), scanline(0), size_in_bytes());
return bitmap;
}
Bitmap::~Bitmap()
{
if (m_needs_munmap) {
int rc = munmap(m_data, size_in_bytes());
ASSERT(rc == 0);
}
m_data = nullptr;
delete[] m_palette;
}
void Bitmap::set_mmap_name([[maybe_unused]] const StringView& name)
{
ASSERT(m_needs_munmap);
#ifdef __serenity__
::set_mmap_name(m_data, size_in_bytes(), name.to_string().characters());
#endif
}
void Bitmap::fill(Color color)
{
ASSERT(!is_indexed(m_format));
for (int y = 0; y < height(); ++y) {
auto* scanline = this->scanline(y);
fast_u32_fill(scanline, color.value(), width());
}
}
void Bitmap::set_volatile()
{
ASSERT(m_purgeable);
if (m_volatile)
return;
#ifdef __serenity__
int rc = madvise(m_data, size_in_bytes(), MADV_SET_VOLATILE);
if (rc < 0) {
perror("madvise(MADV_SET_VOLATILE)");
ASSERT_NOT_REACHED();
}
#endif
m_volatile = true;
}
[[nodiscard]] bool Bitmap::set_nonvolatile()
{
ASSERT(m_purgeable);
if (!m_volatile)
return true;
#ifdef __serenity__
int rc = madvise(m_data, size_in_bytes(), MADV_SET_NONVOLATILE);
if (rc < 0) {
perror("madvise(MADV_SET_NONVOLATILE)");
ASSERT_NOT_REACHED();
}
#else
int rc = 0;
#endif
m_volatile = false;
return rc == 0;
}
int Bitmap::shbuf_id() const
{
return m_shared_buffer ? m_shared_buffer->shbuf_id() : -1;
}
ShareableBitmap Bitmap::to_shareable_bitmap(pid_t peer_pid) const
{
auto bitmap = to_bitmap_backed_by_shared_buffer();
if (!bitmap)
return {};
if (peer_pid > 0)
bitmap->shared_buffer()->share_with(peer_pid);
return ShareableBitmap(*bitmap);
}
Optional<BackingStore> Bitmap::allocate_backing_store(BitmapFormat format, const IntSize& size, [[maybe_unused]] Purgeable purgeable)
{
if (size_would_overflow(format, size))
return {};
const auto pitch = minimum_pitch(size.width(), format);
const auto data_size_in_bytes = size_in_bytes(pitch, size.height());
int map_flags = MAP_ANONYMOUS | MAP_PRIVATE;
if (purgeable == Purgeable::Yes)
map_flags |= MAP_NORESERVE;
#ifdef __serenity__
void* data = mmap_with_name(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0, String::format("GraphicsBitmap [%dx%d]", size.width(), size.height()).characters());
#else
void* data = mmap(nullptr, data_size_in_bytes, PROT_READ | PROT_WRITE, map_flags, 0, 0);
#endif
if (data == MAP_FAILED) {
perror("mmap");
return {};
}
return { { data, pitch, data_size_in_bytes } };
}
void Bitmap::allocate_palette_from_format(BitmapFormat format, const Vector<RGBA32>& source_palette)
{
size_t size = palette_size(format);
if (size == 0)
return;
m_palette = new RGBA32[size];
if (!source_palette.is_empty()) {
ASSERT(source_palette.size() == size);
memcpy(m_palette, source_palette.data(), size * sizeof(RGBA32));
}
}
Vector<RGBA32> Bitmap::palette_to_vector() const
{
Vector<RGBA32> vector;
auto size = palette_size(m_format);
vector.ensure_capacity(size);
for (size_t i = 0; i < size; ++i)
vector.unchecked_append(palette_color(i).value());
return vector;
}
}

View file

@ -0,0 +1,336 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <LibGfx/Color.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Rect.h>
#define ENUMERATE_IMAGE_FORMATS \
__ENUMERATE_IMAGE_FORMAT(pbm, ".pbm") \
__ENUMERATE_IMAGE_FORMAT(pgm, ".pgm") \
__ENUMERATE_IMAGE_FORMAT(png, ".png") \
__ENUMERATE_IMAGE_FORMAT(ppm, ".ppm") \
__ENUMERATE_IMAGE_FORMAT(gif, ".gif") \
__ENUMERATE_IMAGE_FORMAT(bmp, ".bmp") \
__ENUMERATE_IMAGE_FORMAT(ico, ".ico") \
__ENUMERATE_IMAGE_FORMAT(jpg, ".jpg") \
__ENUMERATE_IMAGE_FORMAT(jpg, ".jpeg")
namespace Gfx {
enum class BitmapFormat {
Invalid,
Indexed1,
Indexed2,
Indexed4,
Indexed8,
RGB32,
RGBA32,
};
enum class StorageFormat {
Indexed8,
RGB32,
RGBA32,
};
static StorageFormat determine_storage_format(BitmapFormat format)
{
switch (format) {
case BitmapFormat::RGB32:
return StorageFormat::RGB32;
case BitmapFormat::RGBA32:
return StorageFormat::RGBA32;
case BitmapFormat::Indexed1:
case BitmapFormat::Indexed2:
case BitmapFormat::Indexed4:
case BitmapFormat::Indexed8:
return StorageFormat::Indexed8;
default:
ASSERT_NOT_REACHED();
}
}
struct BackingStore;
enum RotationDirection {
Left,
Right
};
class Bitmap : public RefCounted<Bitmap> {
public:
static RefPtr<Bitmap> create(BitmapFormat, const IntSize&);
static RefPtr<Bitmap> create_shareable(BitmapFormat, const IntSize&);
static RefPtr<Bitmap> create_purgeable(BitmapFormat, const IntSize&);
static RefPtr<Bitmap> create_wrapper(BitmapFormat, const IntSize&, size_t pitch, void*);
static RefPtr<Bitmap> load_from_file(const StringView& path);
static RefPtr<Bitmap> create_with_shared_buffer(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const IntSize&);
static RefPtr<Bitmap> create_with_shared_buffer(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const IntSize&, const Vector<RGBA32>& palette);
static RefPtr<Bitmap> create_from_serialized_byte_buffer(ByteBuffer&& buffer);
static bool is_path_a_supported_image_format(const StringView& path)
{
#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \
if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) \
return true;
ENUMERATE_IMAGE_FORMATS
#undef __ENUMERATE_IMAGE_FORMAT
return false;
}
RefPtr<Gfx::Bitmap> clone() const;
RefPtr<Gfx::Bitmap> rotated(Gfx::RotationDirection) const;
RefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const;
RefPtr<Bitmap> to_bitmap_backed_by_shared_buffer() const;
ByteBuffer serialize_to_byte_buffer() const;
ShareableBitmap to_shareable_bitmap(pid_t peer_pid = -1) const;
~Bitmap();
u8* scanline_u8(int y);
const u8* scanline_u8(int y) const;
RGBA32* scanline(int y);
const RGBA32* scanline(int y) const;
IntRect rect() const { return { {}, m_size }; }
IntSize size() const { return m_size; }
int width() const { return m_size.width(); }
int height() const { return m_size.height(); }
size_t pitch() const { return m_pitch; }
int shbuf_id() const;
SharedBuffer* shared_buffer() { return m_shared_buffer.ptr(); }
const SharedBuffer* shared_buffer() const { return m_shared_buffer.ptr(); }
ALWAYS_INLINE bool is_indexed() const
{
return is_indexed(m_format);
}
ALWAYS_INLINE static bool is_indexed(BitmapFormat format)
{
return format == BitmapFormat::Indexed8 || format == BitmapFormat::Indexed4
|| format == BitmapFormat::Indexed2 || format == BitmapFormat::Indexed1;
}
static size_t palette_size(BitmapFormat format)
{
switch (format) {
case BitmapFormat::Indexed1:
return 2;
case BitmapFormat::Indexed2:
return 4;
case BitmapFormat::Indexed4:
return 16;
case BitmapFormat::Indexed8:
return 256;
default:
return 0;
}
}
Vector<RGBA32> palette_to_vector() const;
static unsigned bpp_for_format(BitmapFormat format)
{
switch (format) {
case BitmapFormat::Indexed1:
return 1;
case BitmapFormat::Indexed2:
return 2;
case BitmapFormat::Indexed4:
return 4;
case BitmapFormat::Indexed8:
return 8;
case BitmapFormat::RGB32:
case BitmapFormat::RGBA32:
return 32;
default:
ASSERT_NOT_REACHED();
case BitmapFormat::Invalid:
return 0;
}
}
static size_t minimum_pitch(size_t width, BitmapFormat);
unsigned bpp() const
{
return bpp_for_format(m_format);
}
void fill(Color);
bool has_alpha_channel() const { return m_format == BitmapFormat::RGBA32; }
BitmapFormat format() const { return m_format; }
void set_mmap_name(const StringView&);
static constexpr size_t size_in_bytes(size_t pitch, int height) { return pitch * height; }
size_t size_in_bytes() const { return size_in_bytes(m_pitch, height()); }
Color palette_color(u8 index) const { return Color::from_rgba(m_palette[index]); }
void set_palette_color(u8 index, Color color) { m_palette[index] = color.value(); }
template<StorageFormat>
Color get_pixel(int x, int y) const;
Color get_pixel(int x, int y) const;
Color get_pixel(const IntPoint& position) const
{
return get_pixel(position.x(), position.y());
}
template<StorageFormat>
void set_pixel(int x, int y, Color);
void set_pixel(int x, int y, Color);
void set_pixel(const IntPoint& position, Color color)
{
set_pixel(position.x(), position.y(), color);
}
bool is_purgeable() const { return m_purgeable; }
bool is_volatile() const { return m_volatile; }
void set_volatile();
[[nodiscard]] bool set_nonvolatile();
private:
enum class Purgeable {
No,
Yes
};
Bitmap(BitmapFormat, const IntSize&, Purgeable, const BackingStore&);
Bitmap(BitmapFormat, const IntSize&, size_t pitch, void*);
Bitmap(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const IntSize&, const Vector<RGBA32>& palette);
static Optional<BackingStore> allocate_backing_store(BitmapFormat, const IntSize&, Purgeable);
void allocate_palette_from_format(BitmapFormat, const Vector<RGBA32>& source_palette);
IntSize m_size;
void* m_data { nullptr };
RGBA32* m_palette { nullptr };
size_t m_pitch { 0 };
BitmapFormat m_format { BitmapFormat::Invalid };
bool m_needs_munmap { false };
bool m_purgeable { false };
bool m_volatile { false };
RefPtr<SharedBuffer> m_shared_buffer;
};
inline u8* Bitmap::scanline_u8(int y)
{
ASSERT(y >= 0 && y < height());
return reinterpret_cast<u8*>(m_data) + (y * m_pitch);
}
inline const u8* Bitmap::scanline_u8(int y) const
{
ASSERT(y >= 0 && y < height());
return reinterpret_cast<const u8*>(m_data) + (y * m_pitch);
}
inline RGBA32* Bitmap::scanline(int y)
{
return reinterpret_cast<RGBA32*>(scanline_u8(y));
}
inline const RGBA32* Bitmap::scanline(int y) const
{
return reinterpret_cast<const RGBA32*>(scanline_u8(y));
}
template<>
inline Color Bitmap::get_pixel<StorageFormat::RGB32>(int x, int y) const
{
ASSERT(x >= 0 && x < width());
return Color::from_rgb(scanline(y)[x]);
}
template<>
inline Color Bitmap::get_pixel<StorageFormat::RGBA32>(int x, int y) const
{
ASSERT(x >= 0 && x < width());
return Color::from_rgba(scanline(y)[x]);
}
template<>
inline Color Bitmap::get_pixel<StorageFormat::Indexed8>(int x, int y) const
{
ASSERT(x >= 0 && x < width());
return Color::from_rgb(m_palette[scanline_u8(y)[x]]);
}
inline Color Bitmap::get_pixel(int x, int y) const
{
switch (determine_storage_format(m_format)) {
case StorageFormat::RGB32:
return get_pixel<StorageFormat::RGB32>(x, y);
case StorageFormat::RGBA32:
return get_pixel<StorageFormat::RGBA32>(x, y);
case StorageFormat::Indexed8:
return get_pixel<StorageFormat::Indexed8>(x, y);
default:
ASSERT_NOT_REACHED();
}
}
template<>
inline void Bitmap::set_pixel<StorageFormat::RGB32>(int x, int y, Color color)
{
ASSERT(x >= 0 && x < width());
scanline(y)[x] = color.value();
}
template<>
inline void Bitmap::set_pixel<StorageFormat::RGBA32>(int x, int y, Color color)
{
ASSERT(x >= 0 && x < width());
scanline(y)[x] = color.value(); // drop alpha
}
inline void Bitmap::set_pixel(int x, int y, Color color)
{
switch (determine_storage_format(m_format)) {
case StorageFormat::RGB32:
set_pixel<StorageFormat::RGB32>(x, y, color);
break;
case StorageFormat::RGBA32:
set_pixel<StorageFormat::RGBA32>(x, y, color);
break;
case StorageFormat::Indexed8:
ASSERT_NOT_REACHED();
default:
ASSERT_NOT_REACHED();
}
}
}

View file

@ -0,0 +1,324 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "BitmapFont.h"
#include "Bitmap.h"
#include "Emoji.h"
#include <AK/StdLibExtras.h>
#include <AK/StringBuilder.h>
#include <AK/Utf32View.h>
#include <AK/Utf8View.h>
#include <AK/Vector.h>
#include <AK/kmalloc.h>
#include <LibCore/FileStream.h>
#include <LibGfx/FontDatabase.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <unistd.h>
namespace Gfx {
struct [[gnu::packed]] FontFileHeader {
char magic[4];
u8 glyph_width;
u8 glyph_height;
u8 type;
u8 is_variable_width;
u8 glyph_spacing;
u8 baseline;
u8 mean_line;
u8 presentation_size;
u16 weight;
char name[32];
char family[32];
};
NonnullRefPtr<Font> BitmapFont::clone() const
{
size_t bytes_per_glyph = sizeof(u32) * glyph_height();
auto* new_rows = static_cast<unsigned*>(malloc(bytes_per_glyph * m_glyph_count));
memcpy(new_rows, m_rows, bytes_per_glyph * m_glyph_count);
auto* new_widths = static_cast<u8*>(malloc(m_glyph_count));
if (m_glyph_widths)
memcpy(new_widths, m_glyph_widths, m_glyph_count);
else
memset(new_widths, m_glyph_width, m_glyph_count);
return adopt(*new BitmapFont(m_name, m_family, new_rows, new_widths, m_fixed_width, m_glyph_width, m_glyph_height, m_glyph_spacing, m_type, m_baseline, m_mean_line, m_presentation_size, m_weight, true));
}
NonnullRefPtr<BitmapFont> BitmapFont::create(u8 glyph_height, u8 glyph_width, bool fixed, FontTypes type)
{
size_t bytes_per_glyph = sizeof(u32) * glyph_height;
size_t count = glyph_count_by_type(type);
auto* new_rows = static_cast<unsigned*>(malloc(bytes_per_glyph * count));
memset(new_rows, 0, bytes_per_glyph * count);
auto* new_widths = static_cast<u8*>(malloc(count));
memset(new_widths, glyph_width, count);
return adopt(*new BitmapFont("Untitled", "Untitled", new_rows, new_widths, fixed, glyph_width, glyph_height, 1, type, 0, 0, 0, 400, true));
}
BitmapFont::BitmapFont(String name, String family, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, bool owns_arrays)
: m_name(name)
, m_family(family)
, m_type(type)
, m_rows(rows)
, m_glyph_widths(widths)
, m_glyph_width(glyph_width)
, m_glyph_height(glyph_height)
, m_min_glyph_width(glyph_width)
, m_max_glyph_width(glyph_width)
, m_glyph_spacing(glyph_spacing)
, m_baseline(baseline)
, m_mean_line(mean_line)
, m_presentation_size(presentation_size)
, m_weight(weight)
, m_fixed_width(is_fixed_width)
, m_owns_arrays(owns_arrays)
{
update_x_height();
m_glyph_count = glyph_count_by_type(m_type);
if (!m_fixed_width) {
u8 maximum = 0;
u8 minimum = 255;
for (size_t i = 0; i < m_glyph_count; ++i) {
minimum = min(minimum, m_glyph_widths[i]);
maximum = max(maximum, m_glyph_widths[i]);
}
m_min_glyph_width = minimum;
m_max_glyph_width = maximum;
}
}
BitmapFont::~BitmapFont()
{
if (m_owns_arrays) {
free(m_glyph_widths);
free(m_rows);
}
}
RefPtr<BitmapFont> BitmapFont::load_from_memory(const u8* data)
{
auto& header = *reinterpret_cast<const FontFileHeader*>(data);
if (memcmp(header.magic, "!Fnt", 4)) {
dbgln("header.magic != '!Fnt', instead it's '{:c}{:c}{:c}{:c}'", header.magic[0], header.magic[1], header.magic[2], header.magic[3]);
return nullptr;
}
if (header.name[sizeof(header.name) - 1] != '\0') {
dbgln("Font name not fully null-terminated");
return nullptr;
}
if (header.family[sizeof(header.family) - 1] != '\0') {
dbgln("Font family not fully null-terminated");
return nullptr;
}
FontTypes type;
if (header.type == 0)
type = FontTypes::Default;
else if (header.type == 1)
type = FontTypes::LatinExtendedA;
else
ASSERT_NOT_REACHED();
size_t count = glyph_count_by_type(type);
size_t bytes_per_glyph = sizeof(unsigned) * header.glyph_height;
auto* rows = const_cast<unsigned*>((const unsigned*)(data + sizeof(FontFileHeader)));
u8* widths = nullptr;
if (header.is_variable_width)
widths = (u8*)(rows) + count * bytes_per_glyph;
return adopt(*new BitmapFont(String(header.name), String(header.family), rows, widths, !header.is_variable_width, header.glyph_width, header.glyph_height, header.glyph_spacing, type, header.baseline, header.mean_line, header.presentation_size, header.weight));
}
size_t BitmapFont::glyph_count_by_type(FontTypes type)
{
if (type == FontTypes::Default)
return 256;
if (type == FontTypes::LatinExtendedA)
return 384;
dbgln("Unknown font type: {}", (int)type);
ASSERT_NOT_REACHED();
}
RefPtr<BitmapFont> BitmapFont::load_from_file(const StringView& path)
{
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
auto font = load_from_memory((const u8*)file_or_error.value()->data());
if (!font)
return nullptr;
font->m_mapped_file = file_or_error.release_value();
return font;
}
bool BitmapFont::write_to_file(const StringView& path)
{
FontFileHeader header;
memset(&header, 0, sizeof(FontFileHeader));
memcpy(header.magic, "!Fnt", 4);
header.glyph_width = m_glyph_width;
header.glyph_height = m_glyph_height;
header.type = m_type;
header.baseline = m_baseline;
header.mean_line = m_mean_line;
header.is_variable_width = !m_fixed_width;
header.glyph_spacing = m_glyph_spacing;
header.presentation_size = m_presentation_size;
header.weight = m_weight;
memcpy(header.name, m_name.characters(), min(m_name.length(), sizeof(header.name) - 1));
memcpy(header.family, m_family.characters(), min(m_family.length(), sizeof(header.family) - 1));
size_t bytes_per_glyph = sizeof(unsigned) * m_glyph_height;
size_t count = glyph_count_by_type(m_type);
auto stream_result = Core::OutputFileStream::open_buffered(path);
if (stream_result.is_error())
return false;
auto& stream = stream_result.value();
stream << ReadonlyBytes { &header, sizeof(header) };
stream << ReadonlyBytes { m_rows, count * bytes_per_glyph };
stream << ReadonlyBytes { m_glyph_widths, count };
stream.flush();
if (stream.handle_any_error())
return false;
return true;
}
GlyphBitmap BitmapFont::glyph_bitmap(u32 code_point) const
{
return GlyphBitmap(&m_rows[code_point * m_glyph_height], { glyph_width(code_point), m_glyph_height });
}
int BitmapFont::glyph_or_emoji_width(u32 code_point) const
{
if (code_point < m_glyph_count)
return glyph_width(code_point);
if (m_fixed_width)
return m_glyph_width;
auto* emoji = Emoji::emoji_for_code_point(code_point);
if (emoji == nullptr)
return glyph_width('?');
return emoji->size().width();
}
int BitmapFont::width(const StringView& string) const
{
Utf8View utf8 { string };
return width(utf8);
}
int BitmapFont::width(const Utf8View& utf8) const
{
bool first = true;
int width = 0;
for (u32 code_point : utf8) {
if (!first)
width += glyph_spacing();
first = false;
width += glyph_or_emoji_width(code_point);
}
return width;
}
int BitmapFont::width(const Utf32View& view) const
{
if (view.length() == 0)
return 0;
int width = (view.length() - 1) * glyph_spacing();
for (size_t i = 0; i < view.length(); ++i)
width += glyph_or_emoji_width(view.code_points()[i]);
return width;
}
void BitmapFont::set_type(FontTypes type)
{
if (type == m_type)
return;
if (type == FontTypes::Default)
return;
size_t new_glyph_count = glyph_count_by_type(type);
if (new_glyph_count <= m_glyph_count) {
m_glyph_count = new_glyph_count;
return;
}
int item_count_to_copy = min(m_glyph_count, new_glyph_count);
size_t bytes_per_glyph = sizeof(u32) * glyph_height();
auto* new_rows = static_cast<unsigned*>(kmalloc(bytes_per_glyph * new_glyph_count));
memset(new_rows, (unsigned)0, bytes_per_glyph * new_glyph_count);
memcpy(new_rows, m_rows, bytes_per_glyph * item_count_to_copy);
auto* new_widths = static_cast<u8*>(kmalloc(new_glyph_count));
memset(new_widths, (u8)0, new_glyph_count);
memcpy(new_widths, m_glyph_widths, item_count_to_copy);
kfree(m_rows);
kfree(m_glyph_widths);
m_type = type;
m_glyph_count = new_glyph_count;
m_rows = new_rows;
m_glyph_widths = new_widths;
}
String BitmapFont::qualified_name() const
{
return String::formatted("{} {} {}", family(), presentation_size(), weight());
}
const Font& BitmapFont::bold_variant() const
{
if (m_bold_variant)
return *m_bold_variant;
m_bold_variant = Gfx::FontDatabase::the().get(m_family, m_presentation_size, 700);
if (!m_bold_variant)
m_bold_variant = this;
return *m_bold_variant;
}
}

View file

@ -0,0 +1,150 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/MappedFile.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <LibGfx/Font.h>
#include <LibGfx/Size.h>
namespace Gfx {
enum FontTypes {
Default = 0,
LatinExtendedA = 1
};
class BitmapFont : public Font {
public:
NonnullRefPtr<Font> clone() const;
static NonnullRefPtr<BitmapFont> create(u8 glyph_height, u8 glyph_width, bool fixed, FontTypes type);
static RefPtr<BitmapFont> load_from_file(const StringView& path);
bool write_to_file(const StringView& path);
~BitmapFont();
u8 presentation_size() const { return m_presentation_size; }
void set_presentation_size(u8 size) { m_presentation_size = size; }
u16 weight() const { return m_weight; }
void set_weight(u16 weight) { m_weight = weight; }
GlyphBitmap glyph_bitmap(u32 code_point) const;
u8 glyph_width(size_t ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[ch]; }
int glyph_or_emoji_width(u32 code_point) const;
u8 glyph_height() const { return m_glyph_height; }
int x_height() const { return m_x_height; }
u8 min_glyph_width() const { return m_min_glyph_width; }
u8 max_glyph_width() const { return m_max_glyph_width; }
u8 glyph_fixed_width() const { return m_glyph_width; }
u8 baseline() const { return m_baseline; }
void set_baseline(u8 baseline)
{
m_baseline = baseline;
update_x_height();
}
u8 mean_line() const { return m_mean_line; }
void set_mean_line(u8 mean_line)
{
m_mean_line = mean_line;
update_x_height();
}
int width(const StringView&) const;
int width(const Utf8View&) const;
int width(const Utf32View&) const;
const String& name() const { return m_name; }
void set_name(String name) { m_name = move(name); }
bool is_fixed_width() const { return m_fixed_width; }
void set_fixed_width(bool b) { m_fixed_width = b; }
u8 glyph_spacing() const { return m_glyph_spacing; }
void set_glyph_spacing(u8 spacing) { m_glyph_spacing = spacing; }
void set_glyph_width(size_t ch, u8 width)
{
ASSERT(m_glyph_widths);
m_glyph_widths[ch] = width;
}
int glyph_count() const { return m_glyph_count; }
FontTypes type() { return m_type; }
void set_type(FontTypes type);
const String& family() const { return m_family; }
void set_family(String family) { m_family = move(family); }
String qualified_name() const;
const Font& bold_variant() const;
private:
BitmapFont(String name, String family, unsigned* rows, u8* widths, bool is_fixed_width, u8 glyph_width, u8 glyph_height, u8 glyph_spacing, FontTypes type, u8 baseline, u8 mean_line, u8 presentation_size, u16 weight, bool owns_arrays = false);
static RefPtr<BitmapFont> load_from_memory(const u8*);
static size_t glyph_count_by_type(FontTypes type);
void update_x_height() { m_x_height = m_baseline - m_mean_line; };
String m_name;
String m_family;
FontTypes m_type;
size_t m_glyph_count { 256 };
unsigned* m_rows { nullptr };
u8* m_glyph_widths { nullptr };
RefPtr<MappedFile> m_mapped_file;
u8 m_glyph_width { 0 };
u8 m_glyph_height { 0 };
u8 m_x_height { 0 };
u8 m_min_glyph_width { 0 };
u8 m_max_glyph_width { 0 };
u8 m_glyph_spacing { 0 };
u8 m_baseline { 0 };
u8 m_mean_line { 0 };
u8 m_presentation_size { 0 };
u16 m_weight { 0 };
bool m_fixed_width { false };
bool m_owns_arrays { false };
mutable RefPtr<Gfx::Font> m_bold_variant;
};
}

View file

@ -0,0 +1,37 @@
set(SOURCES
AffineTransform.cpp
Bitmap.cpp
BitmapFont.cpp
BMPLoader.cpp
BMPWriter.cpp
CharacterBitmap.cpp
ClassicStylePainter.cpp
ClassicWindowTheme.cpp
Color.cpp
DisjointRectSet.cpp
Emoji.cpp
Font.cpp
FontDatabase.cpp
GIFLoader.cpp
ICOLoader.cpp
ImageDecoder.cpp
JPGLoader.cpp
Painter.cpp
Palette.cpp
Path.cpp
PBMLoader.cpp
PGMLoader.cpp
PNGLoader.cpp
PPMLoader.cpp
Point.cpp
Rect.cpp
ShareableBitmap.cpp
Size.cpp
StylePainter.cpp
SystemTheme.cpp
Triangle.cpp
WindowTheme.cpp
)
serenity_lib(LibGfx gfx)
target_link_libraries(LibGfx LibM LibCore)

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "CharacterBitmap.h"
namespace Gfx {
CharacterBitmap::CharacterBitmap(const char* ascii_data, unsigned width, unsigned height)
: m_bits(ascii_data)
, m_size(width, height)
{
}
CharacterBitmap::~CharacterBitmap()
{
}
NonnullRefPtr<CharacterBitmap> CharacterBitmap::create_from_ascii(const char* asciiData, unsigned width, unsigned height)
{
return adopt(*new CharacterBitmap(asciiData, width, height));
}
}

View file

@ -0,0 +1,54 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "Size.h"
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
namespace Gfx {
class CharacterBitmap : public RefCounted<CharacterBitmap> {
public:
static NonnullRefPtr<CharacterBitmap> create_from_ascii(const char* asciiData, unsigned width, unsigned height);
~CharacterBitmap();
bool bit_at(unsigned x, unsigned y) const { return m_bits[y * width() + x] == '#'; }
const char* bits() const { return m_bits; }
IntSize size() const { return m_size; }
unsigned width() const { return m_size.width(); }
unsigned height() const { return m_size.height(); }
private:
CharacterBitmap(const char* b, unsigned w, unsigned h);
const char* m_bits { nullptr };
IntSize m_size;
};
}

View file

@ -0,0 +1,405 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020 Sarah Taube <metalflakecobaltpaint@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/StringView.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/CharacterBitmap.h>
#include <LibGfx/ClassicStylePainter.h>
#include <LibGfx/Painter.h>
#include <LibGfx/Palette.h>
namespace Gfx {
void ClassicStylePainter::paint_tab_button(Painter& painter, const IntRect& rect, const Palette& palette, bool active, bool hovered, bool enabled, bool top)
{
Color base_color = palette.button();
Color highlight_color2 = palette.threed_highlight();
Color shadow_color1 = palette.threed_shadow1();
Color shadow_color2 = palette.threed_shadow2();
if (hovered && enabled && !active)
base_color = palette.hover_highlight();
PainterStateSaver saver(painter);
painter.translate(rect.location());
if (top) {
// Base
painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 1 }, base_color);
// Top line
painter.draw_line({ 2, 0 }, { rect.width() - 3, 0 }, highlight_color2);
// Left side
painter.draw_line({ 0, 2 }, { 0, rect.height() - 1 }, highlight_color2);
painter.set_pixel({ 1, 1 }, highlight_color2);
// Right side
IntPoint top_right_outer { rect.width() - 1, 2 };
IntPoint bottom_right_outer { rect.width() - 1, rect.height() - 1 };
painter.draw_line(top_right_outer, bottom_right_outer, shadow_color2);
IntPoint top_right_inner { rect.width() - 2, 2 };
IntPoint bottom_right_inner { rect.width() - 2, rect.height() - 1 };
painter.draw_line(top_right_inner, bottom_right_inner, shadow_color1);
painter.set_pixel(rect.width() - 2, 1, shadow_color2);
} else {
// Base
painter.fill_rect({ 0, 0, rect.width() - 1, rect.height() }, base_color);
// Bottom line
painter.draw_line({ 2, rect.height() - 1 }, { rect.width() - 3, rect.height() - 1 }, shadow_color2);
// Left side
painter.draw_line({ 0, 0 }, { 0, rect.height() - 3 }, highlight_color2);
painter.set_pixel({ 1, rect.height() - 2 }, highlight_color2);
// Right side
IntPoint top_right_outer { rect.width() - 1, 0 };
IntPoint bottom_right_outer { rect.width() - 1, rect.height() - 3 };
painter.draw_line(top_right_outer, bottom_right_outer, shadow_color2);
IntPoint top_right_inner { rect.width() - 2, 0 };
IntPoint bottom_right_inner { rect.width() - 2, rect.height() - 3 };
painter.draw_line(top_right_inner, bottom_right_inner, shadow_color1);
painter.set_pixel(rect.width() - 2, rect.height() - 2, shadow_color2);
}
}
static void paint_button_new(Painter& painter, const IntRect& a_rect, const Palette& palette, bool pressed, bool checked, bool hovered, bool enabled, bool focused)
{
Color button_color = palette.button();
Color highlight_color = palette.threed_highlight();
Color shadow_color1 = palette.threed_shadow1();
Color shadow_color2 = palette.threed_shadow2();
if (checked && enabled) {
if (hovered)
button_color = palette.hover_highlight();
else
button_color = palette.button();
} else if (hovered && enabled)
button_color = palette.hover_highlight();
PainterStateSaver saver(painter);
auto rect = a_rect;
if (focused) {
painter.draw_rect(a_rect, palette.threed_shadow2());
rect.shrink(2, 2);
}
painter.translate(rect.location());
if (pressed || checked) {
// Base
Gfx::IntRect base_rect { 1, 1, rect.width() - 2, rect.height() - 2 };
if (checked && !pressed)
painter.fill_rect_with_dither_pattern(base_rect, palette.button().lightened(1.3f), palette.button());
else
painter.fill_rect(base_rect, button_color);
// Top shadow
painter.draw_line({ 0, 0 }, { rect.width() - 2, 0 }, shadow_color2);
painter.draw_line({ 0, 0 }, { 0, rect.height() - 2 }, shadow_color2);
// Sunken shadow
painter.draw_line({ 1, 1 }, { rect.width() - 3, 1 }, shadow_color1);
painter.draw_line({ 1, 2 }, { 1, rect.height() - 3 }, shadow_color1);
// Outer highlight
painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, highlight_color);
painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, highlight_color);
// Inner highlight
painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, palette.button());
painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, palette.button());
} else {
// Base
painter.fill_rect({ 0, 0, rect.width(), rect.height() }, button_color);
// Top highlight
painter.draw_line({ 1, 1 }, { rect.width() - 3, 1 }, highlight_color);
painter.draw_line({ 1, 1 }, { 1, rect.height() - 3 }, highlight_color);
// Outer shadow
painter.draw_line({ 0, rect.height() - 1 }, { rect.width() - 1, rect.height() - 1 }, shadow_color2);
painter.draw_line({ rect.width() - 1, 0 }, { rect.width() - 1, rect.height() - 2 }, shadow_color2);
// Inner shadow
painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color1);
painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color1);
}
}
void ClassicStylePainter::paint_button(Painter& painter, const IntRect& rect, const Palette& palette, ButtonStyle button_style, bool pressed, bool hovered, bool checked, bool enabled, bool focused)
{
if (button_style == ButtonStyle::Normal)
return paint_button_new(painter, rect, palette, pressed, checked, hovered, enabled, focused);
if (button_style == ButtonStyle::CoolBar && !enabled)
return;
Color button_color = palette.button();
Color highlight_color = palette.threed_highlight();
Color shadow_color = palette.threed_shadow1();
PainterStateSaver saver(painter);
painter.translate(rect.location());
if (pressed || checked) {
// Base
IntRect base_rect { 1, 1, rect.width() - 2, rect.height() - 2 };
if (checked && !pressed) {
painter.fill_rect_with_dither_pattern(base_rect, palette.button().lightened(1.3f), palette.button());
} else {
painter.fill_rect(base_rect, button_color);
}
// Sunken shadow
painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, shadow_color);
painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, shadow_color);
// Bottom highlight
painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, highlight_color);
painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, highlight_color);
} else if (button_style == ButtonStyle::CoolBar && hovered) {
// Base
painter.fill_rect({ 1, 1, rect.width() - 2, rect.height() - 2 }, button_color);
// Top highlight
painter.draw_line({ 1, 1 }, { rect.width() - 2, 1 }, highlight_color);
painter.draw_line({ 1, 2 }, { 1, rect.height() - 2 }, highlight_color);
// Bottom shadow
painter.draw_line({ rect.width() - 2, 1 }, { rect.width() - 2, rect.height() - 3 }, shadow_color);
painter.draw_line({ 1, rect.height() - 2 }, { rect.width() - 2, rect.height() - 2 }, shadow_color);
}
}
void ClassicStylePainter::paint_surface(Painter& painter, const IntRect& rect, const Palette& palette, bool paint_vertical_lines, bool paint_top_line)
{
painter.fill_rect({ rect.x(), rect.y() + 1, rect.width(), rect.height() - 2 }, palette.button());
painter.draw_line(rect.top_left(), rect.top_right(), paint_top_line ? palette.threed_highlight() : palette.button());
painter.draw_line(rect.bottom_left(), rect.bottom_right(), palette.threed_shadow1());
if (paint_vertical_lines) {
painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), palette.threed_highlight());
painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), palette.threed_shadow1());
}
}
void ClassicStylePainter::paint_frame(Painter& painter, const IntRect& rect, const Palette& palette, FrameShape shape, FrameShadow shadow, int thickness, bool skip_vertical_lines)
{
Color top_left_color;
Color bottom_right_color;
Color dark_shade = palette.threed_shadow1();
Color light_shade = palette.threed_highlight();
if (shape == FrameShape::Container && thickness >= 2) {
if (shadow == FrameShadow::Raised) {
dark_shade = palette.threed_shadow2();
}
}
if (shadow == FrameShadow::Raised) {
top_left_color = light_shade;
bottom_right_color = dark_shade;
} else if (shadow == FrameShadow::Sunken) {
top_left_color = dark_shade;
bottom_right_color = light_shade;
} else if (shadow == FrameShadow::Plain) {
top_left_color = dark_shade;
bottom_right_color = dark_shade;
}
if (thickness >= 1) {
painter.draw_line(rect.top_left(), rect.top_right(), top_left_color);
painter.draw_line(rect.bottom_left(), rect.bottom_right(), bottom_right_color);
if (shape != FrameShape::Panel || !skip_vertical_lines) {
painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left().translated(0, -1), top_left_color);
painter.draw_line(rect.top_right(), rect.bottom_right().translated(0, -1), bottom_right_color);
}
}
if (shape == FrameShape::Container && thickness >= 2) {
Color top_left_color;
Color bottom_right_color;
Color dark_shade = palette.threed_shadow2();
Color light_shade = palette.button();
if (shadow == FrameShadow::Raised) {
dark_shade = palette.threed_shadow1();
top_left_color = light_shade;
bottom_right_color = dark_shade;
} else if (shadow == FrameShadow::Sunken) {
top_left_color = dark_shade;
bottom_right_color = light_shade;
} else if (shadow == FrameShadow::Plain) {
top_left_color = dark_shade;
bottom_right_color = dark_shade;
}
IntRect inner_container_frame_rect = rect.shrunken(2, 2);
painter.draw_line(inner_container_frame_rect.top_left(), inner_container_frame_rect.top_right(), top_left_color);
painter.draw_line(inner_container_frame_rect.bottom_left(), inner_container_frame_rect.bottom_right(), bottom_right_color);
painter.draw_line(inner_container_frame_rect.top_left().translated(0, 1), inner_container_frame_rect.bottom_left().translated(0, -1), top_left_color);
painter.draw_line(inner_container_frame_rect.top_right(), inner_container_frame_rect.bottom_right().translated(0, -1), bottom_right_color);
}
if (shape == FrameShape::Box && thickness >= 2) {
swap(top_left_color, bottom_right_color);
IntRect inner_rect = rect.shrunken(2, 2);
painter.draw_line(inner_rect.top_left(), inner_rect.top_right(), top_left_color);
painter.draw_line(inner_rect.bottom_left(), inner_rect.bottom_right(), bottom_right_color);
painter.draw_line(inner_rect.top_left().translated(0, 1), inner_rect.bottom_left().translated(0, -1), top_left_color);
painter.draw_line(inner_rect.top_right(), inner_rect.bottom_right().translated(0, -1), bottom_right_color);
}
}
void ClassicStylePainter::paint_window_frame(Painter& painter, const IntRect& rect, const Palette& palette)
{
Color base_color = palette.button();
Color dark_shade = palette.threed_shadow2();
Color mid_shade = palette.threed_shadow1();
Color light_shade = palette.threed_highlight();
painter.draw_line(rect.top_left(), rect.top_right(), base_color);
painter.draw_line(rect.top_left().translated(0, 1), rect.bottom_left(), base_color);
painter.draw_line(rect.top_left().translated(1, 1), rect.top_right().translated(-1, 1), light_shade);
painter.draw_line(rect.top_left().translated(1, 1), rect.bottom_left().translated(1, -1), light_shade);
painter.draw_line(rect.top_left().translated(2, 2), rect.top_right().translated(-2, 2), base_color);
painter.draw_line(rect.top_left().translated(2, 2), rect.bottom_left().translated(2, -2), base_color);
painter.draw_line(rect.top_left().translated(3, 3), rect.top_right().translated(-3, 3), base_color);
painter.draw_line(rect.top_left().translated(3, 3), rect.bottom_left().translated(3, -3), base_color);
painter.draw_line(rect.top_right(), rect.bottom_right(), dark_shade);
painter.draw_line(rect.top_right().translated(-1, 1), rect.bottom_right().translated(-1, -1), mid_shade);
painter.draw_line(rect.top_right().translated(-2, 2), rect.bottom_right().translated(-2, -2), base_color);
painter.draw_line(rect.top_right().translated(-3, 3), rect.bottom_right().translated(-3, -3), base_color);
painter.draw_line(rect.bottom_left(), rect.bottom_right(), dark_shade);
painter.draw_line(rect.bottom_left().translated(1, -1), rect.bottom_right().translated(-1, -1), mid_shade);
painter.draw_line(rect.bottom_left().translated(2, -2), rect.bottom_right().translated(-2, -2), base_color);
painter.draw_line(rect.bottom_left().translated(3, -3), rect.bottom_right().translated(-3, -3), base_color);
}
void ClassicStylePainter::paint_progress_bar(Painter& painter, const IntRect& rect, const Palette& palette, int min, int max, int value, const StringView& text)
{
// First we fill the entire widget with the gradient. This incurs a bit of
// overdraw but ensures a consistent look throughout the progression.
Color start_color = palette.active_window_border1();
Color end_color = palette.active_window_border2();
painter.fill_rect_with_gradient(rect, start_color, end_color);
if (!text.is_null()) {
painter.draw_text(rect.translated(1, 1), text, TextAlignment::Center, palette.base_text());
painter.draw_text(rect, text, TextAlignment::Center, palette.base_text().inverted());
}
float range_size = max - min;
float progress = (value - min) / range_size;
// Then we carve out a hole in the remaining part of the widget.
// We draw the text a third time, clipped and inverse, for sharp contrast.
float progress_width = progress * rect.width();
IntRect hole_rect { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() };
hole_rect.move_by(rect.location());
hole_rect.set_right_without_resize(rect.right());
PainterStateSaver saver(painter);
painter.fill_rect(hole_rect, palette.base());
painter.add_clip_rect(hole_rect);
if (!text.is_null())
painter.draw_text(rect.translated(0, 0), text, TextAlignment::Center, palette.base_text());
}
static RefPtr<Gfx::Bitmap> s_unfilled_circle_bitmap;
static RefPtr<Gfx::Bitmap> s_filled_circle_bitmap;
static RefPtr<Gfx::Bitmap> s_changing_filled_circle_bitmap;
static RefPtr<Gfx::Bitmap> s_changing_unfilled_circle_bitmap;
static const Gfx::Bitmap& circle_bitmap(bool checked, bool changing)
{
if (changing)
return checked ? *s_changing_filled_circle_bitmap : *s_changing_unfilled_circle_bitmap;
return checked ? *s_filled_circle_bitmap : *s_unfilled_circle_bitmap;
}
void ClassicStylePainter::paint_radio_button(Painter& painter, const IntRect& rect, const Palette&, bool is_checked, bool is_being_pressed)
{
if (!s_unfilled_circle_bitmap) {
s_unfilled_circle_bitmap = Bitmap::load_from_file("/res/icons/serenity/unfilled-radio-circle.png");
s_filled_circle_bitmap = Bitmap::load_from_file("/res/icons/serenity/filled-radio-circle.png");
s_changing_filled_circle_bitmap = Bitmap::load_from_file("/res/icons/serenity/changing-filled-radio-circle.png");
s_changing_unfilled_circle_bitmap = Bitmap::load_from_file("/res/icons/serenity/changing-unfilled-radio-circle.png");
}
auto& bitmap = circle_bitmap(is_checked, is_being_pressed);
painter.blit(rect.location(), bitmap, bitmap.rect());
}
static const char* s_checked_bitmap_data = {
" "
" # "
" ## "
" ### "
" ## ### "
" ##### "
" ### "
" # "
" "
};
static Gfx::CharacterBitmap* s_checked_bitmap;
static const int s_checked_bitmap_width = 9;
static const int s_checked_bitmap_height = 9;
void ClassicStylePainter::paint_check_box(Painter& painter, const IntRect& rect, const Palette& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
{
painter.fill_rect(rect, is_enabled ? palette.base() : palette.window());
paint_frame(painter, rect, palette, Gfx::FrameShape::Container, Gfx::FrameShadow::Sunken, 2);
if (is_being_pressed) {
// FIXME: This color should not be hard-coded.
painter.draw_rect(rect.shrunken(4, 4), Color::MidGray);
}
if (is_checked) {
if (!s_checked_bitmap)
s_checked_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
painter.draw_bitmap(rect.shrunken(4, 4).location(), *s_checked_bitmap, is_enabled ? palette.base_text() : palette.threed_shadow1());
}
}
void ClassicStylePainter::paint_transparency_grid(Painter& painter, const IntRect& rect, const Palette& palette)
{
painter.fill_rect_with_checkerboard(rect, { 8, 8 }, palette.base().darkened(0.9), palette.base());
}
}

View file

@ -0,0 +1,49 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020 Sarah Taube <metalflakecobaltpaint@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <LibGfx/Forward.h>
#include <LibGfx/StylePainter.h>
namespace Gfx {
class ClassicStylePainter : public BaseStylePainter {
public:
void paint_button(Painter&, const IntRect&, const Palette&, ButtonStyle, bool pressed, bool hovered = false, bool checked = false, bool enabled = true, bool focused = false) override;
void paint_tab_button(Painter&, const IntRect&, const Palette&, bool active, bool hovered, bool enabled, bool top) override;
void paint_surface(Painter&, const IntRect&, const Palette&, bool paint_vertical_lines = true, bool paint_top_line = true) override;
void paint_frame(Painter&, const IntRect&, const Palette&, FrameShape, FrameShadow, int thickness, bool skip_vertical_lines = false) override;
void paint_window_frame(Painter&, const IntRect&, const Palette&) override;
void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text) override;
void paint_radio_button(Painter&, const IntRect&, const Palette&, bool is_checked, bool is_being_pressed) override;
void paint_check_box(Painter&, const IntRect&, const Palette&, bool is_enabled, bool is_checked, bool is_being_pressed) override;
void paint_transparency_grid(Painter&, const IntRect&, const Palette&) override;
};
}

View file

@ -0,0 +1,215 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGfx/Bitmap.h>
#include <LibGfx/ClassicWindowTheme.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
#include <LibGfx/Painter.h>
#include <LibGfx/Palette.h>
#include <LibGfx/StylePainter.h>
namespace Gfx {
ClassicWindowTheme::ClassicWindowTheme()
{
}
ClassicWindowTheme::~ClassicWindowTheme()
{
}
Gfx::IntRect ClassicWindowTheme::title_bar_icon_rect(WindowType window_type, const IntRect& window_rect, const Palette& palette) const
{
auto titlebar_rect = title_bar_rect(window_type, window_rect, palette);
Gfx::IntRect icon_rect {
titlebar_rect.x() + 2,
titlebar_rect.y(),
16,
16,
};
icon_rect.center_vertically_within(titlebar_rect);
icon_rect.move_by(0, 1);
return icon_rect;
}
Gfx::IntRect ClassicWindowTheme::title_bar_text_rect(WindowType window_type, const IntRect& window_rect, const Palette& palette) const
{
auto titlebar_rect = title_bar_rect(window_type, window_rect, palette);
auto titlebar_icon_rect = title_bar_icon_rect(window_type, window_rect, palette);
return {
titlebar_rect.x() + 3 + titlebar_icon_rect.width() + 2,
titlebar_rect.y(),
titlebar_rect.width() - 5 - titlebar_icon_rect.width() - 2,
titlebar_rect.height()
};
}
void ClassicWindowTheme::paint_normal_frame(Painter& painter, WindowState window_state, const IntRect& window_rect, const StringView& title_text, const Bitmap& icon, const Palette& palette, const IntRect& leftmost_button_rect) const
{
auto frame_rect = frame_rect_for_window(WindowType::Normal, window_rect, palette);
frame_rect.set_location({ 0, 0 });
Gfx::StylePainter::paint_window_frame(painter, frame_rect, palette);
auto& title_font = FontDatabase::default_bold_font();
auto titlebar_rect = title_bar_rect(WindowType::Normal, window_rect, palette);
auto titlebar_icon_rect = title_bar_icon_rect(WindowType::Normal, window_rect, palette);
auto titlebar_inner_rect = title_bar_text_rect(WindowType::Normal, window_rect, palette);
auto titlebar_title_rect = titlebar_inner_rect;
titlebar_title_rect.set_width(FontDatabase::default_bold_font().width(title_text));
auto [title_color, border_color, border_color2, stripes_color, shadow_color] = compute_frame_colors(window_state, palette);
painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), palette.button());
painter.draw_line(titlebar_rect.bottom_left().translated(0, 2), titlebar_rect.bottom_right().translated(0, 2), palette.button());
painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2);
int stripe_left = titlebar_title_rect.right() + 5;
int stripe_right = leftmost_button_rect.left() - 3;
if (stripe_left && stripe_right && stripe_left < stripe_right) {
for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) {
painter.draw_line({ stripe_left, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, stripes_color);
}
}
auto clipped_title_rect = titlebar_title_rect;
clipped_title_rect.set_width(stripe_right - clipped_title_rect.x());
if (!clipped_title_rect.is_empty()) {
painter.draw_text(clipped_title_rect.translated(1, 2), title_text, title_font, Gfx::TextAlignment::CenterLeft, shadow_color, Gfx::TextElision::Right);
// FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline.
painter.draw_text(clipped_title_rect.translated(0, 1), title_text, title_font, Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right);
}
painter.blit(titlebar_icon_rect.location(), icon, icon.rect());
}
IntRect ClassicWindowTheme::title_bar_rect(WindowType window_type, const IntRect& window_rect, const Palette& palette) const
{
auto& title_font = FontDatabase::default_bold_font();
auto window_titlebar_height = title_bar_height(palette);
// FIXME: The top of the titlebar doesn't get redrawn properly if this padding is different
int total_vertical_padding = title_font.glyph_height() - 1;
if (window_type == WindowType::Notification)
return { window_rect.width() + 3, total_vertical_padding / 2 - 1, window_titlebar_height, window_rect.height() };
return { 4, 4, window_rect.width(), window_titlebar_height };
}
ClassicWindowTheme::FrameColors ClassicWindowTheme::compute_frame_colors(WindowState state, const Palette& palette) const
{
switch (state) {
case WindowState::Highlighted:
return { palette.highlight_window_title(), palette.highlight_window_border1(), palette.highlight_window_border2(), palette.highlight_window_title_stripes(), palette.highlight_window_title_shadow() };
case WindowState::Moving:
return { palette.moving_window_title(), palette.moving_window_border1(), palette.moving_window_border2(), palette.moving_window_title_stripes(), palette.moving_window_title_shadow() };
case WindowState::Active:
return { palette.active_window_title(), palette.active_window_border1(), palette.active_window_border2(), palette.active_window_title_stripes(), palette.active_window_title_shadow() };
case WindowState::Inactive:
return { palette.inactive_window_title(), palette.inactive_window_border1(), palette.inactive_window_border2(), palette.inactive_window_title_stripes(), palette.inactive_window_title_shadow() };
default:
ASSERT_NOT_REACHED();
}
}
void ClassicWindowTheme::paint_notification_frame(Painter& painter, const IntRect& window_rect, const Palette& palette, const IntRect& close_button_rect) const
{
auto frame_rect = frame_rect_for_window(WindowType::Notification, window_rect, palette);
frame_rect.set_location({ 0, 0 });
Gfx::StylePainter::paint_window_frame(painter, frame_rect, palette);
auto titlebar_rect = title_bar_rect(WindowType::Notification, window_rect, palette);
painter.fill_rect_with_gradient(Gfx::Orientation::Vertical, titlebar_rect, palette.active_window_border1(), palette.active_window_border2());
int stripe_top = close_button_rect.bottom() + 4;
int stripe_bottom = window_rect.height() - 3;
if (stripe_top && stripe_bottom && stripe_top < stripe_bottom) {
for (int i = 2; i <= palette.window_title_height() - 2; i += 2) {
painter.draw_line({ titlebar_rect.x() + i, stripe_top }, { titlebar_rect.x() + i, stripe_bottom }, palette.active_window_title_stripes());
}
}
}
IntRect ClassicWindowTheme::frame_rect_for_window(WindowType window_type, const IntRect& window_rect, const Gfx::Palette& palette) const
{
auto window_titlebar_height = title_bar_height(palette);
switch (window_type) {
case WindowType::Normal:
return {
window_rect.x() - 4,
window_rect.y() - window_titlebar_height - 6,
window_rect.width() + 8,
window_rect.height() + 10 + window_titlebar_height
};
case WindowType::Notification:
return {
window_rect.x() - 3,
window_rect.y() - 3,
window_rect.width() + 6 + window_titlebar_height,
window_rect.height() + 6
};
default:
return window_rect;
}
}
Vector<IntRect> ClassicWindowTheme::layout_buttons(WindowType window_type, const IntRect& window_rect, const Palette& palette, size_t buttons) const
{
int window_button_width = palette.window_title_button_width();
int window_button_height = palette.window_title_button_height();
int pos;
Vector<IntRect> button_rects;
if (window_type == WindowType::Notification)
pos = title_bar_rect(window_type, window_rect, palette).top() + 2;
else
pos = title_bar_text_rect(window_type, window_rect, palette).right() + 1;
for (size_t i = 0; i < buttons; i++) {
if (window_type == WindowType::Notification) {
// The button height & width have to be equal or it leaks out of its area
Gfx::IntRect rect { 0, pos, window_button_height, window_button_height };
rect.center_horizontally_within(title_bar_rect(window_type, window_rect, palette));
button_rects.append(rect);
pos += window_button_height;
} else {
pos -= window_button_width;
Gfx::IntRect rect { pos, 0, window_button_width, window_button_height };
rect.center_vertically_within(title_bar_text_rect(window_type, window_rect, palette));
button_rects.append(rect);
}
}
return button_rects;
}
int ClassicWindowTheme::title_bar_height(const Palette& palette) const
{
auto& title_font = FontDatabase::default_bold_font();
return max(palette.window_title_height(), title_font.glyph_height() + 8);
}
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Color.h>
#include <LibGfx/WindowTheme.h>
namespace Gfx {
class ClassicWindowTheme final : public WindowTheme {
public:
ClassicWindowTheme();
virtual ~ClassicWindowTheme() override;
virtual void paint_normal_frame(Painter&, WindowState, const IntRect& window_rect, const StringView& title, const Bitmap& icon, const Palette&, const IntRect& leftmost_button_rect) const override;
virtual void paint_notification_frame(Painter&, const IntRect& window_rect, const Palette&, const IntRect& close_button_rect) const override;
virtual int title_bar_height(const Palette&) const override;
virtual IntRect title_bar_rect(WindowType, const IntRect& window_rect, const Palette&) const override;
virtual IntRect title_bar_icon_rect(WindowType, const IntRect& window_rect, const Palette&) const override;
virtual IntRect title_bar_text_rect(WindowType, const IntRect& window_rect, const Palette&) const override;
virtual IntRect frame_rect_for_window(WindowType, const IntRect& window_rect, const Palette&) const override;
virtual Vector<IntRect> layout_buttons(WindowType, const IntRect& window_rect, const Palette&, size_t buttons) const override;
private:
struct FrameColors {
Color title_color;
Color border_color;
Color border_color2;
Color title_stripes_color;
Color title_shadow_color;
};
FrameColors compute_frame_colors(WindowState, const Palette&) const;
};
}

View file

@ -0,0 +1,440 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Assertions.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibGfx/Color.h>
#include <LibGfx/SystemTheme.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
namespace Gfx {
Color::Color(NamedColor named)
{
if (named == Transparent) {
m_value = 0;
return;
}
struct {
u8 r;
u8 g;
u8 b;
} rgb;
switch (named) {
case Black:
rgb = { 0, 0, 0 };
break;
case White:
rgb = { 255, 255, 255 };
break;
case Red:
rgb = { 255, 0, 0 };
break;
case Green:
rgb = { 0, 255, 0 };
break;
case Cyan:
rgb = { 0, 255, 255 };
break;
case DarkCyan:
rgb = { 0, 127, 127 };
break;
case MidCyan:
rgb = { 0, 192, 192 };
break;
case Blue:
rgb = { 0, 0, 255 };
break;
case Yellow:
rgb = { 255, 255, 0 };
break;
case Magenta:
rgb = { 255, 0, 255 };
break;
case DarkGray:
rgb = { 64, 64, 64 };
break;
case MidGray:
rgb = { 127, 127, 127 };
break;
case LightGray:
rgb = { 192, 192, 192 };
break;
case MidGreen:
rgb = { 0, 192, 0 };
break;
case MidBlue:
rgb = { 0, 0, 192 };
break;
case MidRed:
rgb = { 192, 0, 0 };
break;
case MidMagenta:
rgb = { 192, 0, 192 };
break;
case DarkGreen:
rgb = { 0, 128, 0 };
break;
case DarkBlue:
rgb = { 0, 0, 128 };
break;
case DarkRed:
rgb = { 128, 0, 0 };
break;
case WarmGray:
rgb = { 212, 208, 200 };
break;
default:
ASSERT_NOT_REACHED();
break;
}
m_value = 0xff000000 | (rgb.r << 16) | (rgb.g << 8) | rgb.b;
}
String Color::to_string() const
{
return String::format("#%02x%02x%02x%02x", red(), green(), blue(), alpha());
}
String Color::to_string_without_alpha() const
{
return String::format("#%02x%02x%02x", red(), green(), blue());
}
static Optional<Color> parse_rgb_color(const StringView& string)
{
ASSERT(string.starts_with("rgb("));
ASSERT(string.ends_with(")"));
auto substring = string.substring_view(4, string.length() - 5);
auto parts = substring.split_view(',');
if (parts.size() != 3)
return {};
auto r = parts[0].to_uint().value_or(256);
auto g = parts[1].to_uint().value_or(256);
auto b = parts[2].to_uint().value_or(256);
if (r > 255 || g > 255 || b > 255)
return {};
return Color(r, g, b);
}
static Optional<Color> parse_rgba_color(const StringView& string)
{
ASSERT(string.starts_with("rgba("));
ASSERT(string.ends_with(")"));
auto substring = string.substring_view(5, string.length() - 6);
auto parts = substring.split_view(',');
if (parts.size() != 4)
return {};
auto r = parts[0].to_int().value_or(256);
auto g = parts[1].to_int().value_or(256);
auto b = parts[2].to_int().value_or(256);
double alpha = strtod(parts[3].to_string().characters(), nullptr);
unsigned a = alpha * 255;
if (r > 255 || g > 255 || b > 255 || a > 255)
return {};
return Color(r, g, b, a);
}
Optional<Color> Color::from_string(const StringView& string)
{
if (string.is_empty())
return {};
struct ColorAndWebName {
constexpr ColorAndWebName(RGBA32 c, const char* n)
: color(c)
, name(n)
{
}
RGBA32 color;
StringView name;
};
constexpr ColorAndWebName web_colors[] = {
// CSS Level 1
{ 0x000000, "black" },
{ 0xc0c0c0, "silver" },
{ 0x808080, "gray" },
{ 0xffffff, "white" },
{ 0x800000, "maroon" },
{ 0xff0000, "red" },
{ 0x800080, "purple" },
{ 0xff00ff, "fuchsia" },
{ 0x008000, "green" },
{ 0x00ff00, "lime" },
{ 0x808000, "olive" },
{ 0xffff00, "yellow" },
{ 0x000080, "navy" },
{ 0x0000ff, "blue" },
{ 0x008080, "teal" },
{ 0x00ffff, "aqua" },
// CSS Level 2 (Revision 1)
{ 0xffa500, "orange" },
// CSS Color Module Level 3
{ 0xf0f8ff, "aliceblue" },
{ 0xfaebd7, "antiquewhite" },
{ 0x7fffd4, "aquamarine" },
{ 0xf0ffff, "azure" },
{ 0xf5f5dc, "beige" },
{ 0xffe4c4, "bisque" },
{ 0xffebcd, "blanchedalmond" },
{ 0x8a2be2, "blueviolet" },
{ 0xa52a2a, "brown" },
{ 0xdeb887, "burlywood" },
{ 0x5f9ea0, "cadetblue" },
{ 0x7fff00, "chartreuse" },
{ 0xd2691e, "chocolate" },
{ 0xff7f50, "coral" },
{ 0x6495ed, "cornflowerblue" },
{ 0xfff8dc, "cornsilk" },
{ 0xdc143c, "crimson" },
{ 0x00ffff, "cyan" },
{ 0x00008b, "darkblue" },
{ 0x008b8b, "darkcyan" },
{ 0xb8860b, "darkgoldenrod" },
{ 0xa9a9a9, "darkgray" },
{ 0x006400, "darkgreen" },
{ 0xa9a9a9, "darkgrey" },
{ 0xbdb76b, "darkkhaki" },
{ 0x8b008b, "darkmagenta" },
{ 0x556b2f, "darkolivegreen" },
{ 0xff8c00, "darkorange" },
{ 0x9932cc, "darkorchid" },
{ 0x8b0000, "darkred" },
{ 0xe9967a, "darksalmon" },
{ 0x8fbc8f, "darkseagreen" },
{ 0x483d8b, "darkslateblue" },
{ 0x2f4f4f, "darkslategray" },
{ 0x2f4f4f, "darkslategrey" },
{ 0x00ced1, "darkturquoise" },
{ 0x9400d3, "darkviolet" },
{ 0xff1493, "deeppink" },
{ 0x00bfff, "deepskyblue" },
{ 0x696969, "dimgray" },
{ 0x696969, "dimgrey" },
{ 0x1e90ff, "dodgerblue" },
{ 0xb22222, "firebrick" },
{ 0xfffaf0, "floralwhite" },
{ 0x228b22, "forestgreen" },
{ 0xdcdcdc, "gainsboro" },
{ 0xf8f8ff, "ghostwhite" },
{ 0xffd700, "gold" },
{ 0xdaa520, "goldenrod" },
{ 0xadff2f, "greenyellow" },
{ 0x808080, "grey" },
{ 0xf0fff0, "honeydew" },
{ 0xff69b4, "hotpink" },
{ 0xcd5c5c, "indianred" },
{ 0x4b0082, "indigo" },
{ 0xfffff0, "ivory" },
{ 0xf0e68c, "khaki" },
{ 0xe6e6fa, "lavender" },
{ 0xfff0f5, "lavenderblush" },
{ 0x7cfc00, "lawngreen" },
{ 0xfffacd, "lemonchiffon" },
{ 0xadd8e6, "lightblue" },
{ 0xf08080, "lightcoral" },
{ 0xe0ffff, "lightcyan" },
{ 0xfafad2, "lightgoldenrodyellow" },
{ 0xd3d3d3, "lightgray" },
{ 0x90ee90, "lightgreen" },
{ 0xd3d3d3, "lightgrey" },
{ 0xffb6c1, "lightpink" },
{ 0xffa07a, "lightsalmon" },
{ 0x20b2aa, "lightseagreen" },
{ 0x87cefa, "lightskyblue" },
{ 0x778899, "lightslategray" },
{ 0x778899, "lightslategrey" },
{ 0xb0c4de, "lightsteelblue" },
{ 0xffffe0, "lightyellow" },
{ 0x32cd32, "limegreen" },
{ 0xfaf0e6, "linen" },
{ 0xff00ff, "magenta" },
{ 0x66cdaa, "mediumaquamarine" },
{ 0x0000cd, "mediumblue" },
{ 0xba55d3, "mediumorchid" },
{ 0x9370db, "mediumpurple" },
{ 0x3cb371, "mediumseagreen" },
{ 0x7b68ee, "mediumslateblue" },
{ 0x00fa9a, "mediumspringgreen" },
{ 0x48d1cc, "mediumturquoise" },
{ 0xc71585, "mediumvioletred" },
{ 0x191970, "midnightblue" },
{ 0xf5fffa, "mintcream" },
{ 0xffe4e1, "mistyrose" },
{ 0xffe4b5, "moccasin" },
{ 0xffdead, "navajowhite" },
{ 0xfdf5e6, "oldlace" },
{ 0x6b8e23, "olivedrab" },
{ 0xff4500, "orangered" },
{ 0xda70d6, "orchid" },
{ 0xeee8aa, "palegoldenrod" },
{ 0x98fb98, "palegreen" },
{ 0xafeeee, "paleturquoise" },
{ 0xdb7093, "palevioletred" },
{ 0xffefd5, "papayawhip" },
{ 0xffdab9, "peachpuff" },
{ 0xcd853f, "peru" },
{ 0xffc0cb, "pink" },
{ 0xdda0dd, "plum" },
{ 0xb0e0e6, "powderblue" },
{ 0xbc8f8f, "rosybrown" },
{ 0x4169e1, "royalblue" },
{ 0x8b4513, "saddlebrown" },
{ 0xfa8072, "salmon" },
{ 0xf4a460, "sandybrown" },
{ 0x2e8b57, "seagreen" },
{ 0xfff5ee, "seashell" },
{ 0xa0522d, "sienna" },
{ 0x87ceeb, "skyblue" },
{ 0x6a5acd, "slateblue" },
{ 0x708090, "slategray" },
{ 0x708090, "slategrey" },
{ 0xfffafa, "snow" },
{ 0x00ff7f, "springgreen" },
{ 0x4682b4, "steelblue" },
{ 0xd2b48c, "tan" },
{ 0xd8bfd8, "thistle" },
{ 0xff6347, "tomato" },
{ 0x40e0d0, "turquoise" },
{ 0xee82ee, "violet" },
{ 0xf5deb3, "wheat" },
{ 0xf5f5f5, "whitesmoke" },
{ 0x9acd32, "yellowgreen" },
// CSS Color Module Level 4
{ 0x663399, "rebeccapurple" },
// (Fallback)
{ 0x000000, nullptr }
};
for (size_t i = 0; !web_colors[i].name.is_null(); ++i) {
if (string == web_colors[i].name)
return Color::from_rgb(web_colors[i].color);
}
if (string.starts_with("rgb(") && string.ends_with(")"))
return parse_rgb_color(string);
if (string.starts_with("rgba(") && string.ends_with(")"))
return parse_rgba_color(string);
if (string[0] != '#')
return {};
auto hex_nibble_to_u8 = [](char nibble) -> Optional<u8> {
if (!isxdigit(nibble))
return {};
if (nibble >= '0' && nibble <= '9')
return nibble - '0';
return 10 + (tolower(nibble) - 'a');
};
if (string.length() == 4) {
Optional<u8> r = hex_nibble_to_u8(string[1]);
Optional<u8> g = hex_nibble_to_u8(string[2]);
Optional<u8> b = hex_nibble_to_u8(string[3]);
if (!r.has_value() || !g.has_value() || !b.has_value())
return {};
return Color(r.value() * 17, g.value() * 17, b.value() * 17);
}
if (string.length() == 5) {
Optional<u8> r = hex_nibble_to_u8(string[1]);
Optional<u8> g = hex_nibble_to_u8(string[2]);
Optional<u8> b = hex_nibble_to_u8(string[3]);
Optional<u8> a = hex_nibble_to_u8(string[4]);
if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value())
return {};
return Color(r.value() * 17, g.value() * 17, b.value() * 17, a.value() * 17);
}
if (string.length() != 7 && string.length() != 9)
return {};
auto to_hex = [&](char c1, char c2) -> Optional<u8> {
auto nib1 = hex_nibble_to_u8(c1);
auto nib2 = hex_nibble_to_u8(c2);
if (!nib1.has_value() || !nib2.has_value())
return {};
return nib1.value() << 4 | nib2.value();
};
Optional<u8> r = to_hex(string[1], string[2]);
Optional<u8> g = to_hex(string[3], string[4]);
Optional<u8> b = to_hex(string[5], string[6]);
Optional<u8> a = string.length() == 9 ? to_hex(string[7], string[8]) : Optional<u8>(255);
if (!r.has_value() || !g.has_value() || !b.has_value() || !a.has_value())
return {};
return Color(r.value(), g.value(), b.value(), a.value());
}
const LogStream& operator<<(const LogStream& stream, Color value)
{
return stream << value.to_string();
}
}
bool IPC::encode(IPC::Encoder& encoder, const Color& color)
{
encoder << color.value();
return true;
}
bool IPC::decode(IPC::Decoder& decoder, Color& color)
{
u32 rgba = 0;
if (!decoder.decode(rgba))
return false;
color = Color::from_rgba(rgba);
return true;
}
void AK::Formatter<Gfx::Color>::format(FormatBuilder& builder, const Gfx::Color& value)
{
Formatter<StringView>::format(builder, value.to_string());
}

View file

@ -0,0 +1,333 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Assertions.h>
#include <AK/Format.h>
#include <AK/Forward.h>
#include <AK/SIMD.h>
#include <AK/StdLibExtras.h>
#include <LibIPC/Forward.h>
namespace Gfx {
enum class ColorRole;
typedef u32 RGBA32;
constexpr u32 make_rgb(u8 r, u8 g, u8 b)
{
return ((r << 16) | (g << 8) | b);
}
struct HSV {
double hue { 0 };
double saturation { 0 };
double value { 0 };
};
class Color {
public:
enum NamedColor {
Transparent,
Black,
White,
Red,
Green,
Cyan,
Blue,
Yellow,
Magenta,
DarkGray,
MidGray,
LightGray,
WarmGray,
DarkCyan,
DarkGreen,
DarkBlue,
DarkRed,
MidCyan,
MidGreen,
MidRed,
MidBlue,
MidMagenta,
};
constexpr Color() { }
Color(NamedColor);
constexpr Color(u8 r, u8 g, u8 b)
: m_value(0xff000000 | (r << 16) | (g << 8) | b)
{
}
constexpr Color(u8 r, u8 g, u8 b, u8 a)
: m_value((a << 24) | (r << 16) | (g << 8) | b)
{
}
static constexpr Color from_rgb(unsigned rgb) { return Color(rgb | 0xff000000); }
static constexpr Color from_rgba(unsigned rgba) { return Color(rgba); }
constexpr u8 red() const { return (m_value >> 16) & 0xff; }
constexpr u8 green() const { return (m_value >> 8) & 0xff; }
constexpr u8 blue() const { return m_value & 0xff; }
constexpr u8 alpha() const { return (m_value >> 24) & 0xff; }
void set_alpha(u8 value)
{
m_value &= 0x00ffffff;
m_value |= value << 24;
}
constexpr void set_red(u8 value)
{
m_value &= 0xff00ffff;
m_value |= value << 16;
}
constexpr void set_green(u8 value)
{
m_value &= 0xffff00ff;
m_value |= value << 8;
}
constexpr void set_blue(u8 value)
{
m_value &= 0xffffff00;
m_value |= value;
}
Color with_alpha(u8 alpha) const
{
return Color((m_value & 0x00ffffff) | alpha << 24);
}
Color blend(Color source) const
{
if (!alpha() || source.alpha() == 255)
return source;
if (!source.alpha())
return *this;
#ifdef __SSE__
using AK::SIMD::i32x4;
const i32x4 color = {
red(),
green(),
blue()
};
const i32x4 source_color = {
source.red(),
source.green(),
source.blue()
};
const int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
const i32x4 out = (color * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source_color) / d;
return Color(out[0], out[1], out[2], d / 255);
#else
int d = 255 * (alpha() + source.alpha()) - alpha() * source.alpha();
u8 r = (red() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.red()) / d;
u8 g = (green() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.green()) / d;
u8 b = (blue() * alpha() * (255 - source.alpha()) + 255 * source.alpha() * source.blue()) / d;
u8 a = d / 255;
return Color(r, g, b, a);
#endif
}
Color to_grayscale() const
{
int gray = (red() + green() + blue()) / 3;
return Color(gray, gray, gray, alpha());
}
Color darkened(float amount = 0.5f) const
{
return Color(red() * amount, green() * amount, blue() * amount, alpha());
}
Color lightened(float amount = 1.2f) const
{
return Color(min(255, (int)((float)red() * amount)), min(255, (int)((float)green() * amount)), min(255, (int)((float)blue() * amount)), alpha());
}
Color inverted() const
{
return Color(~red(), ~green(), ~blue(), alpha());
}
Color xored(const Color& other) const
{
return Color(((other.m_value ^ m_value) & 0x00ffffff) | (m_value & 0xff000000));
}
RGBA32 value() const { return m_value; }
bool operator==(const Color& other) const
{
return m_value == other.m_value;
}
bool operator!=(const Color& other) const
{
return m_value != other.m_value;
}
String to_string() const;
String to_string_without_alpha() const;
static Optional<Color> from_string(const StringView&);
HSV to_hsv() const
{
HSV hsv;
double r = static_cast<double>(red()) / 255.0;
double g = static_cast<double>(green()) / 255.0;
double b = static_cast<double>(blue()) / 255.0;
double max = AK::max(AK::max(r, g), b);
double min = AK::min(AK::min(r, g), b);
double chroma = max - min;
if (!chroma)
hsv.hue = 0.0;
else if (max == r)
hsv.hue = (60.0 * ((g - b) / chroma)) + 360.0;
else if (max == g)
hsv.hue = (60.0 * ((b - r) / chroma)) + 120.0;
else
hsv.hue = (60.0 * ((r - g) / chroma)) + 240.0;
if (hsv.hue >= 360.0)
hsv.hue -= 360.0;
if (!max)
hsv.saturation = 0;
else
hsv.saturation = chroma / max;
hsv.value = max;
ASSERT(hsv.hue >= 0.0 && hsv.hue < 360.0);
ASSERT(hsv.saturation >= 0.0 && hsv.saturation <= 1.0);
ASSERT(hsv.value >= 0.0 && hsv.value <= 1.0);
return hsv;
}
static Color from_hsv(double hue, double saturation, double value)
{
return from_hsv({ hue, saturation, value });
}
static Color from_hsv(const HSV& hsv)
{
ASSERT(hsv.hue >= 0.0 && hsv.hue < 360.0);
ASSERT(hsv.saturation >= 0.0 && hsv.saturation <= 1.0);
ASSERT(hsv.value >= 0.0 && hsv.value <= 1.0);
double hue = hsv.hue;
double saturation = hsv.saturation;
double value = hsv.value;
int high = static_cast<int>(hue / 60.0) % 6;
double f = (hue / 60.0) - high;
double c1 = value * (1.0 - saturation);
double c2 = value * (1.0 - saturation * f);
double c3 = value * (1.0 - saturation * (1.0 - f));
double r = 0;
double g = 0;
double b = 0;
switch (high) {
case 0:
r = value;
g = c3;
b = c1;
break;
case 1:
r = c2;
g = value;
b = c1;
break;
case 2:
r = c1;
g = value;
b = c3;
break;
case 3:
r = c1;
g = c2;
b = value;
break;
case 4:
r = c3;
g = c1;
b = value;
break;
case 5:
r = value;
g = c1;
b = c2;
break;
}
u8 out_r = (u8)(r * 255);
u8 out_g = (u8)(g * 255);
u8 out_b = (u8)(b * 255);
return Color(out_r, out_g, out_b);
}
private:
constexpr explicit Color(RGBA32 rgba)
: m_value(rgba)
{
}
RGBA32 m_value { 0 };
};
const LogStream& operator<<(const LogStream&, Color);
}
using Gfx::Color;
namespace AK {
template<>
struct Formatter<Gfx::Color> : public Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Color& value);
};
}
namespace IPC {
bool encode(Encoder&, const Gfx::Color&);
bool decode(Decoder&, Gfx::Color&);
}

View file

@ -0,0 +1,188 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGfx/DisjointRectSet.h>
namespace Gfx {
bool DisjointRectSet::add_no_shatter(const IntRect& new_rect)
{
if (new_rect.is_empty())
return false;
for (auto& rect : m_rects) {
if (rect.contains(new_rect))
return false;
}
m_rects.append(new_rect);
return true;
}
void DisjointRectSet::shatter()
{
Vector<IntRect, 32> output;
output.ensure_capacity(m_rects.size());
bool pass_had_intersections = false;
do {
pass_had_intersections = false;
output.clear_with_capacity();
for (size_t i = 0; i < m_rects.size(); ++i) {
auto& r1 = m_rects[i];
for (size_t j = 0; j < m_rects.size(); ++j) {
if (i == j)
continue;
auto& r2 = m_rects[j];
if (!r1.intersects(r2))
continue;
pass_had_intersections = true;
auto pieces = r1.shatter(r2);
for (auto& piece : pieces)
output.append(piece);
m_rects.remove(i);
for (; i < m_rects.size(); ++i)
output.append(m_rects[i]);
goto next_pass;
}
output.append(r1);
}
next_pass:
swap(output, m_rects);
} while (pass_had_intersections);
}
void DisjointRectSet::move_by(int dx, int dy)
{
for (auto& r : m_rects)
r.move_by(dx, dy);
}
bool DisjointRectSet::contains(const IntRect& rect) const
{
if (is_empty() || rect.is_empty())
return false;
// TODO: This could use some optimization
DisjointRectSet remainder(rect);
for (auto& r : m_rects) {
auto shards = remainder.shatter(r);
if (shards.is_empty())
return true;
remainder = move(shards);
}
return false;
}
bool DisjointRectSet::intersects(const IntRect& rect) const
{
for (auto& r : m_rects) {
if (r.intersects(rect))
return true;
}
return false;
}
bool DisjointRectSet::intersects(const DisjointRectSet& rects) const
{
if (this == &rects)
return true;
for (auto& r : m_rects) {
for (auto& r2 : rects.m_rects) {
if (r.intersects(r2))
return true;
}
}
return false;
}
DisjointRectSet DisjointRectSet::intersected(const IntRect& rect) const
{
DisjointRectSet intersected_rects;
intersected_rects.m_rects.ensure_capacity(m_rects.capacity());
for (auto& r : m_rects) {
auto intersected_rect = r.intersected(rect);
if (!intersected_rect.is_empty())
intersected_rects.m_rects.append(intersected_rect);
}
// Since there should be no overlaps, we don't need to call shatter()
return intersected_rects;
}
DisjointRectSet DisjointRectSet::intersected(const DisjointRectSet& rects) const
{
if (&rects == this)
return clone();
if (is_empty() || rects.is_empty())
return {};
DisjointRectSet intersected_rects;
intersected_rects.m_rects.ensure_capacity(m_rects.capacity());
for (auto& r : m_rects) {
for (auto& r2 : rects.m_rects) {
auto intersected_rect = r.intersected(r2);
if (!intersected_rect.is_empty())
intersected_rects.m_rects.append(intersected_rect);
}
}
// Since there should be no overlaps, we don't need to call shatter()
return intersected_rects;
}
DisjointRectSet DisjointRectSet::shatter(const IntRect& hammer) const
{
if (hammer.is_empty())
return clone();
DisjointRectSet shards;
for (auto& rect : m_rects) {
for (auto& shard : rect.shatter(hammer))
shards.add_no_shatter(shard);
}
// Since there should be no overlaps, we don't need to call shatter()
return shards;
}
DisjointRectSet DisjointRectSet::shatter(const DisjointRectSet& hammer) const
{
if (this == &hammer)
return {};
if (hammer.is_empty() || !intersects(hammer))
return clone();
// TODO: This could use some optimization
DisjointRectSet shards = shatter(hammer.m_rects[0]);
auto rects_count = hammer.m_rects.size();
for (size_t i = 1; i < rects_count && !shards.is_empty(); i++) {
if (hammer.m_rects[i].intersects(shards.m_rects)) {
auto shattered = shards.shatter(hammer.m_rects[i]);
shards = move(shattered);
}
}
// Since there should be no overlaps, we don't need to call shatter()
return shards;
}
}

View file

@ -0,0 +1,159 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Vector.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
namespace Gfx {
class DisjointRectSet {
public:
DisjointRectSet(const DisjointRectSet&) = delete;
DisjointRectSet& operator=(const DisjointRectSet&) = delete;
DisjointRectSet() { }
~DisjointRectSet() { }
DisjointRectSet(const IntRect& rect)
{
m_rects.append(rect);
}
DisjointRectSet(DisjointRectSet&&) = default;
DisjointRectSet& operator=(DisjointRectSet&&) = default;
DisjointRectSet clone() const
{
DisjointRectSet rects;
rects.m_rects = m_rects;
return rects;
}
void move_by(int dx, int dy);
void move_by(const IntPoint& delta)
{
move_by(delta.x(), delta.y());
}
void add(const IntRect& rect)
{
if (add_no_shatter(rect) && m_rects.size() > 1)
shatter();
}
template<typename Container>
void add_many(const Container& rects)
{
bool added = false;
for (const auto& rect : rects) {
if (add_no_shatter(rect))
added = true;
}
if (added && m_rects.size() > 1)
shatter();
}
void add(const DisjointRectSet& rect_set)
{
if (this == &rect_set)
return;
if (m_rects.is_empty()) {
m_rects = rect_set.m_rects;
} else {
add_many(rect_set.rects());
}
}
DisjointRectSet shatter(const IntRect&) const;
DisjointRectSet shatter(const DisjointRectSet& hammer) const;
bool contains(const IntRect&) const;
bool intersects(const IntRect&) const;
bool intersects(const DisjointRectSet&) const;
DisjointRectSet intersected(const IntRect&) const;
DisjointRectSet intersected(const DisjointRectSet&) const;
template<typename Function>
IterationDecision for_each_intersected(const IntRect& rect, Function f) const
{
if (is_empty() || rect.is_empty())
return IterationDecision::Continue;
for (auto& r : m_rects) {
auto intersected_rect = r.intersected(rect);
if (intersected_rect.is_empty())
continue;
IterationDecision decision = f(intersected_rect);
if (decision != IterationDecision::Continue)
return decision;
}
return IterationDecision::Continue;
}
template<typename Function>
IterationDecision for_each_intersected(const DisjointRectSet& rects, Function f) const
{
if (is_empty() || rects.is_empty())
return IterationDecision::Continue;
if (this == &rects) {
for (auto& r : m_rects) {
IterationDecision decision = f(r);
if (decision != IterationDecision::Continue)
return decision;
}
} else {
for (auto& r : m_rects) {
for (auto& r2 : rects.m_rects) {
auto intersected_rect = r.intersected(r2);
if (intersected_rect.is_empty())
continue;
IterationDecision decision = f(intersected_rect);
if (decision != IterationDecision::Continue)
return decision;
}
}
}
return IterationDecision::Continue;
}
bool is_empty() const { return m_rects.is_empty(); }
size_t size() const { return m_rects.size(); }
void clear() { m_rects.clear(); }
void clear_with_capacity() { m_rects.clear_with_capacity(); }
const Vector<IntRect, 32>& rects() const { return m_rects; }
Vector<IntRect, 32> take_rects() { return move(m_rects); }
private:
bool add_no_shatter(const IntRect&);
void shatter();
Vector<IntRect, 32> m_rects;
};
}

View file

@ -0,0 +1,54 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/HashMap.h>
#include <AK/String.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Emoji.h>
namespace Gfx {
static HashMap<u32, RefPtr<Gfx::Bitmap>> s_emojis;
const Bitmap* Emoji::emoji_for_code_point(u32 code_point)
{
auto it = s_emojis.find(code_point);
if (it != s_emojis.end())
return (*it).value.ptr();
String path = String::format("/res/emoji/U+%X.png", code_point);
auto bitmap = Bitmap::load_from_file(path);
if (!bitmap) {
s_emojis.set(code_point, nullptr);
return nullptr;
}
s_emojis.set(code_point, bitmap);
return bitmap.ptr();
}
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Types.h>
namespace Gfx {
class Bitmap;
class Emoji {
public:
static const Gfx::Bitmap* emoji_for_code_point(u32 code_point);
};
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "GenericConvolutionFilter.h"
namespace Gfx {
template<size_t N>
class BoxBlurFilter : public GenericConvolutionFilter<N> {
public:
BoxBlurFilter() { }
virtual ~BoxBlurFilter() { }
virtual const char* class_name() const override { return "BoxBlurFilter"; }
};
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Bitmap.h>
#include <LibGfx/Rect.h>
namespace Gfx {
class Filter {
public:
class Parameters {
public:
virtual bool is_generic_convolution_filter() const { return false; }
virtual ~Parameters() { }
};
virtual ~Filter() { }
virtual const char* class_name() const = 0;
virtual void apply(Bitmap&, const IntRect&, const Bitmap&, const IntRect&, const Parameters&) = 0;
protected:
Filter() { }
};
}

View file

@ -0,0 +1,173 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "Filter.h"
#include <LibGfx/Matrix.h>
#include <LibGfx/Matrix4x4.h>
namespace Gfx {
template<size_t N, typename T>
inline static constexpr void normalize(Matrix<N, T>& matrix)
{
auto sum = 0.0f;
for (size_t i = 0; i < matrix.Size; ++i) {
for (size_t j = 0; j < matrix.Size; ++j) {
sum += matrix.elements()[i][j];
}
}
for (size_t i = 0; i < matrix.Size; ++i) {
for (size_t j = 0; j < matrix.Size; ++j) {
matrix.elements()[i][j] /= sum;
}
}
}
template<size_t N>
class GenericConvolutionFilter : public Filter {
public:
class Parameters : public Filter::Parameters {
public:
Parameters(const Gfx::Matrix<N, float>& kernel, bool should_wrap = false)
: m_kernel(kernel)
, m_should_wrap(should_wrap)
{
}
const Gfx::Matrix<N, float>& kernel() const { return m_kernel; }
Gfx::Matrix<N, float>& kernel() { return m_kernel; }
bool should_wrap() const { return m_should_wrap; }
private:
virtual bool is_generic_convolution_filter() const override { return true; }
Gfx::Matrix<N, float> m_kernel;
bool m_should_wrap { false };
};
class ApplyCache {
template<size_t>
friend class GenericConvolutionFilter;
private:
RefPtr<Gfx::Bitmap> m_target;
};
GenericConvolutionFilter() { }
virtual ~GenericConvolutionFilter() { }
virtual const char* class_name() const override { return "GenericConvolutionFilter"; }
virtual void apply(Bitmap& target_bitmap, const IntRect& target_rect, const Bitmap& source_bitmap, const IntRect& source_rect, const Filter::Parameters& parameters) override
{
ASSERT(parameters.is_generic_convolution_filter());
auto& gcf_params = static_cast<const GenericConvolutionFilter::Parameters&>(parameters);
ApplyCache apply_cache;
apply(target_bitmap, target_rect, source_bitmap, source_rect, gcf_params, apply_cache);
}
void apply(Bitmap& target, IntRect target_rect, const Bitmap& source, const IntRect& source_rect, const GenericConvolutionFilter::Parameters& parameters, ApplyCache& apply_cache)
{
// The target area (where the filter is applied) must be entirely
// contained by the source area. source_rect should be describing
// the pixels that can be accessed to apply this filter, while
// target_rect should describe the area where to apply the filter on.
ASSERT(source_rect.contains(target_rect));
ASSERT(source.size().contains(target.size()));
ASSERT(target.rect().contains(target_rect));
ASSERT(source.rect().contains(source_rect));
// If source is different from target, it should still be describing
// essentially the same bitmap. But it allows us to modify target
// without a temporary bitmap. This is important if this filter
// is applied on multiple areas of the same bitmap, at which point
// we would need to be able to access unmodified pixels if the
// areas are (almost) adjacent.
int source_delta_x = target_rect.x() - source_rect.x();
int source_delta_y = target_rect.y() - source_rect.y();
if (&target == &source && (!apply_cache.m_target || !apply_cache.m_target->size().contains(source_rect.size()))) {
// TODO: We probably don't need the entire source_rect, we could inflate
// the target_rect appropriately
apply_cache.m_target = Gfx::Bitmap::create(source.format(), source_rect.size());
target_rect.move_by(-target_rect.location());
}
Bitmap* render_target_bitmap = (&target != &source) ? &target : apply_cache.m_target.ptr();
// FIXME: Help! I am naive!
constexpr static ssize_t offset = N / 2;
for (auto i_ = 0; i_ < target_rect.width(); ++i_) {
ssize_t i = i_ + target_rect.x();
for (auto j_ = 0; j_ < target_rect.height(); ++j_) {
ssize_t j = j_ + target_rect.y();
FloatVector3 value(0, 0, 0);
for (auto k = 0l; k < (ssize_t)N; ++k) {
auto ki = i + k - offset;
if (ki < source_rect.x() || ki > source_rect.right()) {
if (parameters.should_wrap())
ki = (ki + source.size().width()) % source.size().width(); // TODO: fix up using source_rect
else
continue;
}
for (auto l = 0l; l < (ssize_t)N; ++l) {
auto lj = j + l - offset;
if (lj < source_rect.y() || lj > source_rect.bottom()) {
if (parameters.should_wrap())
lj = (lj + source.size().height()) % source.size().height(); // TODO: fix up using source_rect
else
continue;
}
auto pixel = source.get_pixel(ki, lj);
FloatVector3 pixel_value(pixel.red(), pixel.green(), pixel.blue());
value = value + pixel_value * parameters.kernel().elements()[k][l];
}
}
value.clamp(0, 255);
render_target_bitmap->set_pixel(i, j, Color(value.x(), value.y(), value.z(), source.get_pixel(i + source_delta_x, j + source_delta_y).alpha()));
}
}
if (render_target_bitmap != &target) {
// FIXME: Substitute for some sort of faster "blit" method.
for (auto i_ = 0; i_ < target_rect.width(); ++i_) {
auto i = i_ + target_rect.x();
for (auto j_ = 0; j_ < target_rect.height(); ++j_) {
auto j = j_ + target_rect.y();
target.set_pixel(i, j, render_target_bitmap->get_pixel(i_, j_));
}
}
}
}
};
}

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "GenericConvolutionFilter.h"
namespace Gfx {
class LaplacianFilter : public GenericConvolutionFilter<3> {
public:
LaplacianFilter() { }
virtual ~LaplacianFilter() { }
virtual const char* class_name() const override { return "LaplacianFilter"; }
};
}

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "GenericConvolutionFilter.h"
namespace Gfx {
class SharpenFilter : public GenericConvolutionFilter<3> {
public:
SharpenFilter() { }
virtual ~SharpenFilter() { }
virtual const char* class_name() const override { return "SharpenFilter"; }
};
}

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "GenericConvolutionFilter.h"
#include <AK/StdLibExtras.h>
namespace Gfx {
template<size_t N, typename = typename AK::EnableIf<N % 2 == 1>::Type>
class SpatialGaussianBlurFilter : public GenericConvolutionFilter<N> {
public:
SpatialGaussianBlurFilter() { }
virtual ~SpatialGaussianBlurFilter() { }
virtual const char* class_name() const override { return "SpatialGaussianBlurFilter"; }
};
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGfx/BitmapFont.h>
#include <LibGfx/Font.h>
namespace Gfx {
RefPtr<Font> Font::load_from_file(const StringView& path)
{
if (path.ends_with(".font")) {
return BitmapFont::load_from_file(path);
}
return {};
}
}

View file

@ -0,0 +1,114 @@
/*
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/MappedFile.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <LibGfx/Size.h>
namespace Gfx {
// FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
class GlyphBitmap {
friend class BitmapFont;
public:
const unsigned* rows() const { return m_rows; }
unsigned row(unsigned index) const { return m_rows[index]; }
bool bit_at(int x, int y) const { return row(y) & (1 << x); }
void set_bit_at(int x, int y, bool b)
{
auto& mutable_row = const_cast<unsigned*>(m_rows)[y];
if (b)
mutable_row |= 1 << x;
else
mutable_row &= ~(1 << x);
}
IntSize size() const { return m_size; }
int width() const { return m_size.width(); }
int height() const { return m_size.height(); }
private:
GlyphBitmap(const unsigned* rows, IntSize size)
: m_rows(rows)
, m_size(size)
{
}
const unsigned* m_rows { nullptr };
IntSize m_size;
};
class Font : public RefCounted<Font> {
public:
static RefPtr<Font> load_from_file(const StringView& path);
virtual NonnullRefPtr<Font> clone() const = 0;
virtual ~Font() {};
virtual u8 presentation_size() const = 0;
virtual u16 weight() const = 0;
virtual GlyphBitmap glyph_bitmap(u32 code_point) const = 0;
virtual u8 glyph_width(size_t ch) const = 0;
virtual int glyph_or_emoji_width(u32 code_point) const = 0;
virtual u8 glyph_height() const = 0;
virtual int x_height() const = 0;
virtual u8 min_glyph_width() const = 0;
virtual u8 max_glyph_width() const = 0;
virtual u8 glyph_fixed_width() const = 0;
virtual u8 baseline() const = 0;
virtual u8 mean_line() const = 0;
virtual int width(const StringView&) const = 0;
virtual int width(const Utf8View&) const = 0;
virtual int width(const Utf32View&) const = 0;
virtual const String& name() const = 0;
virtual bool is_fixed_width() const = 0;
virtual u8 glyph_spacing() const = 0;
virtual int glyph_count() const = 0;
virtual const String& family() const = 0;
virtual String qualified_name() const = 0;
virtual const Font& bold_variant() const = 0;
};
}

View file

@ -0,0 +1,159 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/NonnullRefPtrVector.h>
#include <AK/QuickSort.h>
#include <LibCore/DirIterator.h>
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
namespace Gfx {
static FontDatabase* s_the;
FontDatabase& FontDatabase::the()
{
if (!s_the)
s_the = new FontDatabase;
return *s_the;
}
Font& FontDatabase::default_font()
{
static Font* font;
if (!font) {
font = FontDatabase::the().get_by_name("Katica 10 400");
ASSERT(font);
}
return *font;
}
Font& FontDatabase::default_fixed_width_font()
{
static Font* font;
if (!font) {
font = FontDatabase::the().get_by_name("Csilla 10 400");
ASSERT(font);
}
return *font;
}
Font& FontDatabase::default_bold_fixed_width_font()
{
static Font* font;
if (!font) {
font = FontDatabase::the().get_by_name("Csilla 10 700");
ASSERT(font);
}
return *font;
}
Font& FontDatabase::default_bold_font()
{
static Font* font;
if (!font) {
font = FontDatabase::the().get_by_name("Katica 10 700");
ASSERT(font);
}
return *font;
}
struct FontDatabase::Private {
HashMap<String, RefPtr<Gfx::Font>> full_name_to_font_map;
};
FontDatabase::FontDatabase()
: m_private(make<Private>())
{
Core::DirIterator di("/res/fonts", Core::DirIterator::SkipDots);
if (di.has_error()) {
fprintf(stderr, "DirIterator: %s\n", di.error_string());
exit(1);
}
while (di.has_next()) {
String name = di.next_path();
if (!name.ends_with(".font"))
continue;
auto path = String::format("/res/fonts/%s", name.characters());
if (auto font = Gfx::Font::load_from_file(path)) {
m_private->full_name_to_font_map.set(font->qualified_name(), font);
}
}
}
FontDatabase::~FontDatabase()
{
}
void FontDatabase::for_each_font(Function<void(const Gfx::Font&)> callback)
{
Vector<RefPtr<Gfx::Font>> fonts;
fonts.ensure_capacity(m_private->full_name_to_font_map.size());
for (auto& it : m_private->full_name_to_font_map)
fonts.append(it.value);
quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
for (auto& font : fonts)
callback(*font);
}
void FontDatabase::for_each_fixed_width_font(Function<void(const Gfx::Font&)> callback)
{
Vector<RefPtr<Gfx::Font>> fonts;
fonts.ensure_capacity(m_private->full_name_to_font_map.size());
for (auto& it : m_private->full_name_to_font_map) {
if (it.value->is_fixed_width())
fonts.append(it.value);
}
quick_sort(fonts, [](auto& a, auto& b) { return a->qualified_name() < b->qualified_name(); });
for (auto& font : fonts)
callback(*font);
}
RefPtr<Gfx::Font> FontDatabase::get_by_name(const StringView& name)
{
auto it = m_private->full_name_to_font_map.find(name);
if (it == m_private->full_name_to_font_map.end()) {
dbgln("Font lookup failed: '{}'", name);
return nullptr;
}
return it->value;
}
RefPtr<Gfx::Font> FontDatabase::get(const String& family, unsigned size, unsigned weight)
{
for (auto& it : m_private->full_name_to_font_map) {
auto& font = *it.value;
if (font.family() == family && font.presentation_size() == size && font.weight() == weight)
return font;
}
return nullptr;
}
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/String.h>
#include <LibGfx/Forward.h>
namespace Gfx {
class FontDatabase {
public:
static FontDatabase& the();
static Font& default_font();
static Font& default_bold_font();
static Font& default_fixed_width_font();
static Font& default_bold_fixed_width_font();
RefPtr<Gfx::Font> get(const String& family, unsigned size, unsigned weight);
RefPtr<Gfx::Font> get_by_name(const StringView&);
void for_each_font(Function<void(const Gfx::Font&)>);
void for_each_fixed_width_font(Function<void(const Gfx::Font&)>);
private:
FontDatabase();
~FontDatabase();
struct Private;
OwnPtr<Private> m_private;
};
}

View file

@ -0,0 +1,72 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
namespace Gfx {
class Bitmap;
class CharacterBitmap;
class Color;
class DisjointRectSet;
class Emoji;
class Font;
class GlyphBitmap;
class ImageDecoder;
class Painter;
class Palette;
class PaletteImpl;
class Path;
class ShareableBitmap;
class StylePainter;
struct SystemTheme;
class Triangle;
template<typename T>
class Point;
template<typename T>
class Size;
template<typename T>
class Rect;
using IntRect = Rect<int>;
using FloatRect = Rect<float>;
using IntPoint = Point<int>;
using FloatPoint = Point<float>;
using IntSize = Size<int>;
using FloatSize = Size<float>;
enum class BitmapFormat;
enum class ColorRole;
enum class TextAlignment;
}
using Gfx::Color;

View file

@ -0,0 +1,765 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Array.h>
#include <AK/ByteBuffer.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/MemoryStream.h>
#include <AK/NonnullOwnPtrVector.h>
#include <LibGfx/GIFLoader.h>
#include <LibGfx/Painter.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
//#define GIF_DEBUG
namespace Gfx {
// Row strides and offsets for each interlace pass.
static const int INTERLACE_ROW_STRIDES[] = { 8, 8, 4, 2 };
static const int INTERLACE_ROW_OFFSETS[] = { 0, 4, 2, 1 };
struct ImageDescriptor {
u16 x { 0 };
u16 y { 0 };
u16 width { 0 };
u16 height { 0 };
bool use_global_color_map { true };
bool interlaced { false };
Color color_map[256];
u8 lzw_min_code_size { 0 };
Vector<u8> lzw_encoded_bytes;
// Fields from optional graphic control extension block
enum DisposalMethod : u8 {
None = 0,
InPlace = 1,
RestoreBackground = 2,
RestorePrevious = 3,
};
DisposalMethod disposal_method { None };
u8 transparency_index { 0 };
u16 duration { 0 };
bool transparent { false };
bool user_input { false };
const IntRect rect() const
{
return { this->x, this->y, this->width, this->height };
}
};
struct LogicalScreen {
u16 width;
u16 height;
Color color_map[256];
};
struct GIFLoadingContext {
enum State {
NotDecoded = 0,
FrameDescriptorsLoaded,
FrameComplete,
};
State state { NotDecoded };
enum ErrorState {
NoError = 0,
FailedToDecodeAllFrames,
FailedToDecodeAnyFrame,
FailedToLoadFrameDescriptors,
};
ErrorState error_state { NoError };
const u8* data { nullptr };
size_t data_size { 0 };
LogicalScreen logical_screen {};
u8 background_color_index { 0 };
NonnullOwnPtrVector<ImageDescriptor> images {};
size_t loops { 1 };
RefPtr<Gfx::Bitmap> frame_buffer;
size_t current_frame { 0 };
RefPtr<Gfx::Bitmap> prev_frame_buffer;
};
RefPtr<Gfx::Bitmap> load_gif(const StringView& path)
{
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
GIFImageDecoderPlugin gif_decoder((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
auto bitmap = gif_decoder.bitmap();
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded GIF: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;
}
RefPtr<Gfx::Bitmap> load_gif_from_memory(const u8* data, size_t length)
{
GIFImageDecoderPlugin gif_decoder(data, length);
auto bitmap = gif_decoder.bitmap();
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded GIF: <memory>", bitmap->size()));
return bitmap;
}
enum class GIFFormat {
GIF87a,
GIF89a,
};
static Optional<GIFFormat> decode_gif_header(InputMemoryStream& stream)
{
static const char valid_header_87[] = "GIF87a";
static const char valid_header_89[] = "GIF89a";
Array<u8, 6> header;
stream >> header;
if (stream.handle_any_error())
return {};
if (header.span() == ReadonlyBytes { valid_header_87, 6 })
return GIFFormat::GIF87a;
if (header.span() == ReadonlyBytes { valid_header_89, 6 })
return GIFFormat::GIF89a;
return {};
}
class LZWDecoder {
private:
static constexpr int max_code_size = 12;
public:
explicit LZWDecoder(const Vector<u8>& lzw_bytes, u8 min_code_size)
: m_lzw_bytes(lzw_bytes)
, m_code_size(min_code_size)
, m_original_code_size(min_code_size)
, m_table_capacity(pow(2, min_code_size))
{
init_code_table();
}
u16 add_control_code()
{
const u16 control_code = m_code_table.size();
m_code_table.append(Vector<u8> {});
m_original_code_table.append(Vector<u8> {});
if (m_code_table.size() >= m_table_capacity && m_code_size < max_code_size) {
++m_code_size;
++m_original_code_size;
m_table_capacity *= 2;
}
return control_code;
}
void reset()
{
m_code_table.clear();
m_code_table.append(m_original_code_table);
m_code_size = m_original_code_size;
m_table_capacity = pow(2, m_code_size);
m_output.clear();
}
Optional<u16> next_code()
{
size_t current_byte_index = m_current_bit_index / 8;
if (current_byte_index >= m_lzw_bytes.size()) {
return {};
}
// Extract the code bits using a 32-bit mask to cover the possibility that if
// the current code size > 9 bits then the code can span 3 bytes.
u8 current_bit_offset = m_current_bit_index % 8;
u32 mask = (u32)(m_table_capacity - 1) << current_bit_offset;
// Make a padded copy of the final bytes in the data to ensure we don't read past the end.
if (current_byte_index + sizeof(mask) > m_lzw_bytes.size()) {
u8 padded_last_bytes[sizeof(mask)] = { 0 };
for (int i = 0; current_byte_index + i < m_lzw_bytes.size(); ++i) {
padded_last_bytes[i] = m_lzw_bytes[current_byte_index + i];
}
const u32* addr = (const u32*)&padded_last_bytes;
m_current_code = (*addr & mask) >> current_bit_offset;
} else {
const u32* addr = (const u32*)&m_lzw_bytes.at(current_byte_index);
m_current_code = (*addr & mask) >> current_bit_offset;
}
if (m_current_code > m_code_table.size()) {
#ifdef GIF_DEBUG
dbg() << "Corrupted LZW stream, invalid code: " << m_current_code << " at bit index: "
<< m_current_bit_index << ", code table size: " << m_code_table.size();
#endif
return {};
} else if (m_current_code == m_code_table.size() && m_output.is_empty()) {
#ifdef GIF_DEBUG
dbg() << "Corrupted LZW stream, valid new code but output buffer is empty: " << m_current_code
<< " at bit index: " << m_current_bit_index << ", code table size: " << m_code_table.size();
#endif
return {};
}
m_current_bit_index += m_code_size;
return m_current_code;
}
Vector<u8>& get_output()
{
ASSERT(m_current_code <= m_code_table.size());
if (m_current_code < m_code_table.size()) {
Vector<u8> new_entry = m_output;
m_output = m_code_table.at(m_current_code);
new_entry.append(m_output[0]);
extend_code_table(new_entry);
} else if (m_current_code == m_code_table.size()) {
ASSERT(!m_output.is_empty());
m_output.append(m_output[0]);
extend_code_table(m_output);
}
return m_output;
}
private:
void init_code_table()
{
m_code_table.clear();
for (u16 i = 0; i < m_table_capacity; ++i) {
m_code_table.append({ (u8)i });
}
m_original_code_table = m_code_table;
}
void extend_code_table(const Vector<u8>& entry)
{
if (entry.size() > 1 && m_code_table.size() < 4096) {
m_code_table.append(entry);
if (m_code_table.size() >= m_table_capacity && m_code_size < max_code_size) {
++m_code_size;
m_table_capacity *= 2;
}
}
}
const Vector<u8>& m_lzw_bytes;
int m_current_bit_index { 0 };
Vector<Vector<u8>> m_code_table {};
Vector<Vector<u8>> m_original_code_table {};
u8 m_code_size { 0 };
u8 m_original_code_size { 0 };
u32 m_table_capacity { 0 };
u16 m_current_code { 0 };
Vector<u8> m_output {};
};
static void copy_frame_buffer(Bitmap& dest, const Bitmap& src)
{
ASSERT(dest.size_in_bytes() == src.size_in_bytes());
memcpy(dest.scanline(0), src.scanline(0), dest.size_in_bytes());
}
static bool decode_frame(GIFLoadingContext& context, size_t frame_index)
{
if (frame_index >= context.images.size()) {
return false;
}
if (context.state >= GIFLoadingContext::State::FrameComplete && frame_index == context.current_frame) {
return true;
}
size_t start_frame = context.current_frame + 1;
if (context.state < GIFLoadingContext::State::FrameComplete) {
start_frame = 0;
context.frame_buffer = Bitmap::create_purgeable(BitmapFormat::RGBA32, { context.logical_screen.width, context.logical_screen.height });
if (!context.frame_buffer)
return false;
context.prev_frame_buffer = Bitmap::create_purgeable(BitmapFormat::RGBA32, { context.logical_screen.width, context.logical_screen.height });
if (!context.prev_frame_buffer)
return false;
} else if (frame_index < context.current_frame) {
start_frame = 0;
}
for (size_t i = start_frame; i <= frame_index; ++i) {
auto& image = context.images.at(i);
const auto previous_image_disposal_method = i > 0 ? context.images.at(i - 1).disposal_method : ImageDescriptor::DisposalMethod::None;
if (i == 0) {
context.frame_buffer->fill(Color::Transparent);
} else if (i > 0 && image.disposal_method == ImageDescriptor::DisposalMethod::RestorePrevious
&& previous_image_disposal_method != ImageDescriptor::DisposalMethod::RestorePrevious) {
// This marks the start of a run of frames that once disposed should be restored to the
// previous underlying image contents. Therefore we make a copy of the current frame
// buffer so that it can be restored later.
copy_frame_buffer(*context.prev_frame_buffer, *context.frame_buffer);
}
if (previous_image_disposal_method == ImageDescriptor::DisposalMethod::RestoreBackground) {
// Note: RestoreBackground could be interpreted either as restoring the underlying
// background of the entire image (e.g. container element's background-color), or the
// background color of the GIF itself. It appears that all major browsers and most other
// GIF decoders adhere to the former interpretation, therefore we will do the same by
// clearing the entire frame buffer to transparent.
Painter painter(*context.frame_buffer);
painter.clear_rect(context.images.at(i - 1).rect(), Color::Transparent);
} else if (i > 0 && previous_image_disposal_method == ImageDescriptor::DisposalMethod::RestorePrevious) {
// Previous frame indicated that once disposed, it should be restored to *its* previous
// underlying image contents, therefore we restore the saved previous frame buffer.
copy_frame_buffer(*context.frame_buffer, *context.prev_frame_buffer);
}
LZWDecoder decoder(image.lzw_encoded_bytes, image.lzw_min_code_size);
// Add GIF-specific control codes
const int clear_code = decoder.add_control_code();
const int end_of_information_code = decoder.add_control_code();
const auto& color_map = image.use_global_color_map ? context.logical_screen.color_map : image.color_map;
int pixel_index = 0;
int row = 0;
int interlace_pass = 0;
while (true) {
Optional<u16> code = decoder.next_code();
if (!code.has_value()) {
#ifdef GIF_DEBUG
dbgln("Unexpectedly reached end of gif frame data");
#endif
return false;
}
if (code.value() == clear_code) {
decoder.reset();
continue;
}
if (code.value() == end_of_information_code)
break;
if (!image.width)
continue;
auto colors = decoder.get_output();
for (const auto& color : colors) {
auto c = color_map[color];
int x = pixel_index % image.width + image.x;
int y = row + image.y;
if (context.frame_buffer->rect().contains(x, y) && (!image.transparent || color != image.transparency_index)) {
context.frame_buffer->set_pixel(x, y, c);
}
++pixel_index;
if (pixel_index % image.width == 0) {
if (image.interlaced) {
if (row + INTERLACE_ROW_STRIDES[interlace_pass] >= image.height) {
++interlace_pass;
// FIXME: We could probably figure this out earlier and fail before doing a bunch of work.
if (interlace_pass >= 4)
return false;
row = INTERLACE_ROW_OFFSETS[interlace_pass];
} else {
row += INTERLACE_ROW_STRIDES[interlace_pass];
}
} else {
++row;
}
}
}
}
context.current_frame = i;
context.state = GIFLoadingContext::State::FrameComplete;
}
return true;
}
static bool load_gif_frame_descriptors(GIFLoadingContext& context)
{
if (context.data_size < 32)
return false;
InputMemoryStream stream { { context.data, context.data_size } };
Optional<GIFFormat> format = decode_gif_header(stream);
if (!format.has_value()) {
return false;
}
LittleEndian<u16> value;
stream >> value;
context.logical_screen.width = value;
stream >> value;
context.logical_screen.height = value;
if (stream.handle_any_error())
return false;
if (context.logical_screen.width > maximum_width_for_decoded_images || context.logical_screen.height > maximum_height_for_decoded_images) {
dbgln("This GIF is too large for comfort: {}x{}", context.logical_screen.width, context.logical_screen.height);
return false;
}
u8 gcm_info = 0;
stream >> gcm_info;
if (stream.handle_any_error())
return false;
stream >> context.background_color_index;
if (stream.handle_any_error())
return false;
u8 pixel_aspect_ratio = 0;
stream >> pixel_aspect_ratio;
if (stream.handle_any_error())
return false;
u8 bits_per_pixel = (gcm_info & 7) + 1;
int color_map_entry_count = 1;
for (int i = 0; i < bits_per_pixel; ++i)
color_map_entry_count *= 2;
for (int i = 0; i < color_map_entry_count; ++i) {
u8 r = 0;
u8 g = 0;
u8 b = 0;
stream >> r >> g >> b;
context.logical_screen.color_map[i] = { r, g, b };
}
if (stream.handle_any_error())
return false;
NonnullOwnPtr<ImageDescriptor> current_image = make<ImageDescriptor>();
for (;;) {
u8 sentinel = 0;
stream >> sentinel;
if (stream.handle_any_error())
return false;
if (sentinel == '!') {
u8 extension_type = 0;
stream >> extension_type;
if (stream.handle_any_error())
return false;
u8 sub_block_length = 0;
Vector<u8> sub_block {};
for (;;) {
stream >> sub_block_length;
if (stream.handle_any_error())
return false;
if (sub_block_length == 0)
break;
u8 dummy = 0;
for (u16 i = 0; i < sub_block_length; ++i) {
stream >> dummy;
sub_block.append(dummy);
}
if (stream.handle_any_error())
return false;
}
if (extension_type == 0xF9) {
if (sub_block.size() != 4) {
#ifdef GIF_DEBUG
dbgln("Unexpected graphic control size");
#endif
continue;
}
u8 disposal_method = (sub_block[0] & 0x1C) >> 2;
current_image->disposal_method = (ImageDescriptor::DisposalMethod)disposal_method;
u8 user_input = (sub_block[0] & 0x2) >> 1;
current_image->user_input = user_input == 1;
u8 transparent = sub_block[0] & 1;
current_image->transparent = transparent == 1;
u16 duration = sub_block[1] + ((u16)sub_block[2] >> 8);
current_image->duration = duration;
current_image->transparency_index = sub_block[3];
}
if (extension_type == 0xFF) {
if (sub_block.size() != 14) {
#ifdef GIF_DEBUG
dbg() << "Unexpected application extension size: " << sub_block.size();
#endif
continue;
}
if (sub_block[11] != 1) {
#ifdef GIF_DEBUG
dbgln("Unexpected application extension format");
#endif
continue;
}
u16 loops = sub_block[12] + (sub_block[13] << 8);
context.loops = loops;
}
continue;
}
if (sentinel == ',') {
context.images.append(move(current_image));
auto& image = context.images.last();
LittleEndian<u16> tmp;
u8 packed_fields { 0 };
stream >> tmp;
image.x = tmp;
stream >> tmp;
image.y = tmp;
stream >> tmp;
image.width = tmp;
stream >> tmp;
image.height = tmp;
stream >> packed_fields;
if (stream.handle_any_error())
return false;
image.use_global_color_map = !(packed_fields & 0x80);
image.interlaced = (packed_fields & 0x40) != 0;
if (!image.use_global_color_map) {
size_t local_color_table_size = pow(2, (packed_fields & 7) + 1);
for (size_t i = 0; i < local_color_table_size; ++i) {
u8 r = 0;
u8 g = 0;
u8 b = 0;
stream >> r >> g >> b;
image.color_map[i] = { r, g, b };
}
}
stream >> image.lzw_min_code_size;
if (stream.handle_any_error())
return false;
u8 lzw_encoded_bytes_expected = 0;
for (;;) {
stream >> lzw_encoded_bytes_expected;
if (stream.handle_any_error())
return false;
if (lzw_encoded_bytes_expected == 0)
break;
Array<u8, 256> buffer;
stream >> buffer.span().trim(lzw_encoded_bytes_expected);
if (stream.handle_any_error())
return false;
for (int i = 0; i < lzw_encoded_bytes_expected; ++i) {
image.lzw_encoded_bytes.append(buffer[i]);
}
}
current_image = make<ImageDescriptor>();
continue;
}
if (sentinel == ';') {
break;
}
return false;
}
context.state = GIFLoadingContext::State::FrameDescriptorsLoaded;
return true;
}
GIFImageDecoderPlugin::GIFImageDecoderPlugin(const u8* data, size_t size)
{
m_context = make<GIFLoadingContext>();
m_context->data = data;
m_context->data_size = size;
}
GIFImageDecoderPlugin::~GIFImageDecoderPlugin() { }
IntSize GIFImageDecoderPlugin::size()
{
if (m_context->error_state == GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors) {
return {};
}
if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
if (!load_gif_frame_descriptors(*m_context)) {
m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors;
return {};
}
}
return { m_context->logical_screen.width, m_context->logical_screen.height };
}
RefPtr<Gfx::Bitmap> GIFImageDecoderPlugin::bitmap()
{
if (m_context->state < GIFLoadingContext::State::FrameComplete) {
return frame(0).image;
}
return m_context->frame_buffer;
}
void GIFImageDecoderPlugin::set_volatile()
{
if (m_context->frame_buffer) {
m_context->frame_buffer->set_volatile();
}
}
bool GIFImageDecoderPlugin::set_nonvolatile()
{
if (!m_context->frame_buffer) {
return true;
}
return m_context->frame_buffer->set_nonvolatile();
}
bool GIFImageDecoderPlugin::sniff()
{
InputMemoryStream stream { { m_context->data, m_context->data_size } };
return decode_gif_header(stream).has_value();
}
bool GIFImageDecoderPlugin::is_animated()
{
if (m_context->error_state != GIFLoadingContext::ErrorState::NoError) {
return false;
}
if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
if (!load_gif_frame_descriptors(*m_context)) {
m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors;
return false;
}
}
return m_context->images.size() > 1;
}
size_t GIFImageDecoderPlugin::loop_count()
{
if (m_context->error_state != GIFLoadingContext::ErrorState::NoError) {
return 0;
}
if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
if (!load_gif_frame_descriptors(*m_context)) {
m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors;
return 0;
}
}
return m_context->loops;
}
size_t GIFImageDecoderPlugin::frame_count()
{
if (m_context->error_state != GIFLoadingContext::ErrorState::NoError) {
return 1;
}
if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
if (!load_gif_frame_descriptors(*m_context)) {
m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors;
return 1;
}
}
return m_context->images.size();
}
ImageFrameDescriptor GIFImageDecoderPlugin::frame(size_t i)
{
if (m_context->error_state >= GIFLoadingContext::ErrorState::FailedToDecodeAnyFrame) {
return {};
}
if (m_context->state < GIFLoadingContext::State::FrameDescriptorsLoaded) {
if (!load_gif_frame_descriptors(*m_context)) {
m_context->error_state = GIFLoadingContext::ErrorState::FailedToLoadFrameDescriptors;
return {};
}
}
if (m_context->error_state == GIFLoadingContext::ErrorState::NoError && !decode_frame(*m_context, i)) {
if (m_context->state < GIFLoadingContext::State::FrameComplete || !decode_frame(*m_context, 0)) {
m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAnyFrame;
return {};
}
m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAllFrames;
}
ImageFrameDescriptor frame {};
frame.image = m_context->frame_buffer->clone();
frame.duration = m_context->images.at(i).duration * 10;
if (frame.duration <= 10) {
frame.duration = 100;
}
return frame;
}
}

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
namespace Gfx {
RefPtr<Gfx::Bitmap> load_gif(const StringView& path);
RefPtr<Gfx::Bitmap> load_gif_from_memory(const u8*, size_t);
struct GIFLoadingContext;
class GIFImageDecoderPlugin final : public ImageDecoderPlugin {
public:
virtual ~GIFImageDecoderPlugin() override;
GIFImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile() override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ImageFrameDescriptor frame(size_t i) override;
private:
OwnPtr<GIFLoadingContext> m_context;
};
}

View file

@ -0,0 +1,147 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include "Color.h"
#include <math.h>
#include <xmmintrin.h>
#include <AK/SIMD.h>
#define GAMMA 2.2
// Most computer graphics are stored in the sRGB color space, which stores something close to
// the square root of the display intensity of each color channel. This is problematic for most
// operations that we want to perform on colors, since they typically assume that color scales
// linearly (e.g. rgb(127, 0, 0) is half as bright as rgb(255, 0, 0)). This causes incorrect
// results that look more gray than they should, to fix this we have to convert colors to the linear
// color space before performing these operations, then convert back before displaying.
//
// Conversion between linear and sRGB spaces are somewhat expensive to do on the CPU, so we instead
// interpret sRGB colors as gamma2.2 colors, which are close enough in most cases to be indistinguishable.
// Gamma 2.2 colors follow the simple rule of `display_intensity = pow(stored_intensity, 2.2)`.
// This module implements some fast color space transforms between the gamma2.2 and linear color spaces, plus
// some common primitive operations like blending.
//
// For a more in-depth overview of how gamma-adjustment works, check out:
// https://blog.johnnovak.net/2016/09/21/what-every-coder-should-know-about-gamma/
namespace Gfx {
using AK::SIMD::f32x4;
#ifndef NO_FPU
# ifdef __SSE__
// Transform f32x4 from gamma2.2 space to linear space
// Assumes x is in range [0, 1]
// FIXME: Remove this hack once clang-11 is available as the default in Github Actions.
// This is apparently sometime mid-December. https://github.com/actions/virtual-environments/issues/2130
# if !defined(__clang__) || __clang_major__ >= 11
constexpr f32x4 gamma_to_linear4(f32x4 x)
# else
inline f32x4 gamma_to_linear4(f32x4 x)
# endif
{
return (0.8f + 0.2f * x) * x * x;
}
// Transform f32x4 from linear space to gamma2.2 space
// Assumes x is in range [0, 1]
inline f32x4 linear_to_gamma4(f32x4 x)
{
// Source for approximation: https://mimosa-pudica.net/fast-gamma/
constexpr float a = 0.00279491f;
constexpr float b = 1.15907984f;
float c = (b / sqrt(1 + a)) - 1;
return ((b * __builtin_ia32_rsqrtps(x + a)) - c) * x;
}
// Linearize v1 and v2, lerp them by mix factor, then convert back.
// The output is entirely v1 when mix = 0 and entirely v2 when mix = 1
inline f32x4 gamma_accurate_lerp4(f32x4 v1, f32x4 v2, float mix)
{
return linear_to_gamma4(gamma_to_linear4(v1) * (1 - mix) + gamma_to_linear4(v2) * mix);
}
# endif
// Transform scalar from gamma2.2 space to linear space
// Assumes x is in range [0, 1]
constexpr float gamma_to_linear(float x)
{
return (0.8 + 0.2 * x) * x * x;
}
// Transform scalar from linear space to gamma2.2 space
// Assumes x is in range [0, 1]
inline float linear_to_gamma(float x)
{
// Source for approximation: https://mimosa-pudica.net/fast-gamma/
constexpr float a = 0.00279491;
constexpr float b = 1.15907984;
float c = (b / sqrt(1 + a)) - 1;
return ((b / __builtin_sqrt(x + a)) - c) * x;
}
// Linearize v1 and v2, lerp them by mix factor, then convert back.
// The output is entirely v1 when mix = 0 and entirely v2 when mix = 1
inline float gamma_accurate_lerp(float v1, float v2, float mix)
{
return linear_to_gamma(gamma_to_linear(v1) * (1 - mix) + gamma_to_linear(v2) * mix);
}
// Convert a and b to linear space, blend them by mix factor, then convert back.
// The output is entirely a when mix = 0 and entirely b when mix = 1
inline Color gamma_accurate_blend(Color a, Color b, float mix)
{
# ifdef __SSE__
f32x4 ac = {
(float)a.red(),
(float)a.green(),
(float)a.blue(),
};
f32x4 bc = {
(float)b.red(),
(float)b.green(),
(float)b.blue(),
};
f32x4 out = 255.f * gamma_accurate_lerp4(ac * (1.f / 255.f), bc * (1.f / 255.f), mix);
return Color(out[0], out[1], out[2]);
# else
return {
static_cast<u8>(255. * gamma_accurate_lerp(a.red() / 255., b.red() / 255., mix)),
static_cast<u8>(255. * gamma_accurate_lerp(a.green() / 255., b.green() / 255., mix)),
static_cast<u8>(255. * gamma_accurate_lerp(a.blue() / 255., b.blue() / 255., mix)),
};
# endif
}
#endif
}

View file

@ -0,0 +1,438 @@
/*
* Copyright (c) 2020, Paul Roukema <roukemap@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/ByteBuffer.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/MemoryStream.h>
#include <AK/NonnullOwnPtrVector.h>
#include <AK/Types.h>
#include <LibGfx/ICOLoader.h>
#include <LibGfx/PNGLoader.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
//#define ICO_DEBUG
namespace Gfx {
// FIXME: This is in little-endian order. Maybe need a NetworkOrdered<T> equivalent eventually.
struct ICONDIR {
u16 must_be_0 = 0;
u16 must_be_1 = 0;
u16 image_count = 0;
};
static_assert(sizeof(ICONDIR) == 6);
struct ICONDIRENTRY {
u8 width;
u8 height;
u8 color_count;
u8 reserved_0;
u16 planes;
u16 bits_per_pixel;
u32 size;
u32 offset;
};
static_assert(sizeof(ICONDIRENTRY) == 16);
struct [[gnu::packed]] BMPFILEHEADER {
u8 signature[2];
u32 size;
u16 reserved1;
u16 reserved2;
u32 offset;
};
static_assert(sizeof(BMPFILEHEADER) == 14);
struct BITMAPINFOHEADER {
u32 size;
i32 width;
i32 height;
u16 planes;
u16 bpp;
u32 compression;
u32 size_image;
u32 vres;
u32 hres;
u32 palette_size;
u32 important_colors;
};
static_assert(sizeof(BITMAPINFOHEADER) == 40);
struct [[gnu::packed]] BMP_ARGB {
u8 b;
u8 g;
u8 r;
u8 a;
};
static_assert(sizeof(BMP_ARGB) == 4);
struct ImageDescriptor {
u16 width;
u16 height;
size_t offset;
size_t size;
RefPtr<Gfx::Bitmap> bitmap;
};
struct ICOLoadingContext {
enum State {
NotDecoded = 0,
Error,
DirectoryDecoded,
BitmapDecoded
};
State state { NotDecoded };
const u8* data { nullptr };
size_t data_size { 0 };
Vector<ImageDescriptor> images;
size_t largest_index;
};
RefPtr<Gfx::Bitmap> load_ico(const StringView& path)
{
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
ICOImageDecoderPlugin decoder((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
auto bitmap = decoder.bitmap();
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded ICO: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;
}
RefPtr<Gfx::Bitmap> load_ico_from_memory(const u8* data, size_t length)
{
ICOImageDecoderPlugin decoder(data, length);
auto bitmap = decoder.bitmap();
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded ICO: <memory>", bitmap->size()));
return bitmap;
}
static Optional<size_t> decode_ico_header(InputMemoryStream& stream)
{
ICONDIR header;
stream >> Bytes { &header, sizeof(header) };
if (stream.handle_any_error())
return {};
if (header.must_be_0 != 0 || header.must_be_1 != 1)
return {};
return { header.image_count };
}
static Optional<ImageDescriptor> decode_ico_direntry(InputMemoryStream& stream)
{
ICONDIRENTRY entry;
stream >> Bytes { &entry, sizeof(entry) };
if (stream.handle_any_error())
return {};
ImageDescriptor desc = { entry.width, entry.height, entry.offset, entry.size, nullptr };
if (desc.width == 0)
desc.width = 256;
if (desc.height == 0)
desc.height = 256;
return { desc };
}
static size_t find_largest_image(const ICOLoadingContext& context)
{
size_t max_area = 0;
size_t index = 0;
size_t largest_index = 0;
for (const auto& desc : context.images) {
if (desc.width * desc.height > max_area) {
max_area = desc.width * desc.height;
largest_index = index;
}
++index;
}
return largest_index;
}
static bool load_ico_directory(ICOLoadingContext& context)
{
InputMemoryStream stream { { context.data, context.data_size } };
auto image_count = decode_ico_header(stream);
if (!image_count.has_value() || image_count.value() == 0) {
return false;
}
for (size_t i = 0; i < image_count.value(); ++i) {
auto maybe_desc = decode_ico_direntry(stream);
if (!maybe_desc.has_value()) {
#ifdef ICO_DEBUG
printf("load_ico_directory: error loading entry: %lu\n", i);
#endif
return false;
}
auto& desc = maybe_desc.value();
if (desc.offset + desc.size < desc.offset // detect integer overflow
|| (desc.offset + desc.size) > context.data_size) {
#ifdef ICO_DEBUG
printf("load_ico_directory: offset: %lu size: %lu doesn't fit in ICO size: %lu\n",
desc.offset, desc.size, context.data_size);
#endif
return false;
}
#ifdef ICO_DEBUG
printf("load_ico_directory: index %zu width: %u height: %u offset: %lu size: %lu\n",
i, desc.width, desc.height, desc.offset, desc.size);
#endif
context.images.append(desc);
}
context.largest_index = find_largest_image(context);
context.state = ICOLoadingContext::State::DirectoryDecoded;
return true;
}
static bool load_ico_bmp(ICOLoadingContext& context, ImageDescriptor& desc)
{
BITMAPINFOHEADER info;
if (desc.size < sizeof(info))
return false;
memcpy(&info, context.data + desc.offset, sizeof(info));
if (info.size != sizeof(info)) {
#ifdef ICO_DEBUG
printf("load_ico_bmp: info size: %u, expected: %lu\n", info.size, sizeof(info));
#endif
return false;
}
if (info.width < 0) {
#ifdef ICO_DEBUG
printf("load_ico_bmp: width %d < 0\n", info.width);
#endif
return false;
}
bool topdown = false;
if (info.height < 0) {
topdown = true;
info.height = -info.height;
}
if (info.planes != 1) {
#ifdef ICO_DEBUG
printf("load_ico_bmp: planes: %d != 1", info.planes);
#endif
return false;
}
if (info.bpp != 32) {
#ifdef ICO_DEBUG
printf("load_ico_bmp: unsupported bpp: %u\n", info.bpp);
#endif
return false;
}
#ifdef ICO_DEBUG
printf("load_ico_bmp: width: %d height: %d direction: %s bpp: %d size_image: %u\n",
info.width, info.height, topdown ? "TopDown" : "BottomUp", info.bpp, info.size_image);
#endif
if (info.compression != 0 || info.palette_size != 0 || info.important_colors != 0) {
#ifdef ICO_DEBUG
printf("load_ico_bmp: following fields must be 0: compression: %u palette_size: %u important_colors: %u\n",
info.compression, info.palette_size, info.important_colors);
#endif
return false;
}
if (info.width != desc.width || info.height != 2 * desc.height) {
#ifdef ICO_DEBUG
printf("load_ico_bmp: size mismatch: ico %dx%d, bmp %dx%d\n",
desc.width, desc.height, info.width, info.height);
#endif
return false;
}
// Mask is 1bpp, and each row must be 4-byte aligned
size_t mask_row_len = align_up_to(align_up_to(desc.width, 8) / 8, 4);
size_t required_len = desc.height * (desc.width * sizeof(BMP_ARGB) + mask_row_len);
size_t available_len = desc.size - sizeof(info);
if (required_len > available_len) {
#ifdef ICO_DEBUG
printf("load_ico_bmp: required_len: %lu > available_len: %lu\n",
required_len, available_len);
#endif
return false;
}
desc.bitmap = Bitmap::create_purgeable(BitmapFormat::RGBA32, { desc.width, desc.height });
if (!desc.bitmap)
return false;
Bitmap& bitmap = *desc.bitmap;
const u8* image_base = context.data + desc.offset + sizeof(info);
const BMP_ARGB* data_base = (const BMP_ARGB*)image_base;
const u8* mask_base = image_base + desc.width * desc.height * sizeof(BMP_ARGB);
for (int y = 0; y < desc.height; y++) {
const u8* row_mask = mask_base + mask_row_len * y;
const BMP_ARGB* row_data = data_base + desc.width * y;
for (int x = 0; x < desc.width; x++) {
u8 mask = !!(row_mask[x / 8] & (0x80 >> (x % 8)));
BMP_ARGB data = row_data[x];
bitmap.set_pixel(x, topdown ? y : desc.height - y - 1,
Color(data.r, data.g, data.b, mask ? 0 : data.a));
}
}
return true;
}
static bool load_ico_bitmap(ICOLoadingContext& context, Optional<size_t> index)
{
if (context.state < ICOLoadingContext::State::DirectoryDecoded) {
if (!load_ico_directory(context)) {
context.state = ICOLoadingContext::State::Error;
return false;
}
context.state = ICOLoadingContext::State::DirectoryDecoded;
}
size_t real_index = context.largest_index;
if (index.has_value())
real_index = index.value();
if (real_index >= context.images.size()) {
return false;
}
ImageDescriptor& desc = context.images[real_index];
PNGImageDecoderPlugin png_decoder(context.data + desc.offset, desc.size);
if (png_decoder.sniff()) {
desc.bitmap = png_decoder.bitmap();
if (!desc.bitmap) {
#ifdef ICO_DEBUG
printf("load_ico_bitmap: failed to load PNG encoded image index: %lu\n", real_index);
#endif
return false;
}
return true;
} else {
if (!load_ico_bmp(context, desc)) {
#ifdef ICO_DEBUG
printf("load_ico_bitmap: failed to load BMP encoded image index: %lu\n", real_index);
#endif
return false;
}
return true;
}
}
ICOImageDecoderPlugin::ICOImageDecoderPlugin(const u8* data, size_t size)
{
m_context = make<ICOLoadingContext>();
m_context->data = data;
m_context->data_size = size;
}
ICOImageDecoderPlugin::~ICOImageDecoderPlugin() { }
IntSize ICOImageDecoderPlugin::size()
{
if (m_context->state == ICOLoadingContext::State::Error) {
return {};
}
if (m_context->state < ICOLoadingContext::State::DirectoryDecoded) {
if (!load_ico_directory(*m_context)) {
m_context->state = ICOLoadingContext::State::Error;
return {};
}
m_context->state = ICOLoadingContext::State::DirectoryDecoded;
}
return { m_context->images[m_context->largest_index].width, m_context->images[m_context->largest_index].height };
}
RefPtr<Gfx::Bitmap> ICOImageDecoderPlugin::bitmap()
{
if (m_context->state == ICOLoadingContext::State::Error)
return nullptr;
if (m_context->state < ICOLoadingContext::State::BitmapDecoded) {
// NOTE: This forces the chunk decoding to happen.
bool success = load_ico_bitmap(*m_context, {});
if (!success) {
m_context->state = ICOLoadingContext::State::Error;
return nullptr;
}
m_context->state = ICOLoadingContext::State::BitmapDecoded;
}
ASSERT(m_context->images[m_context->largest_index].bitmap);
return m_context->images[m_context->largest_index].bitmap;
}
void ICOImageDecoderPlugin::set_volatile()
{
if (m_context->images[0].bitmap)
m_context->images[0].bitmap->set_volatile();
}
bool ICOImageDecoderPlugin::set_nonvolatile()
{
if (!m_context->images[0].bitmap)
return false;
return m_context->images[0].bitmap->set_nonvolatile();
}
bool ICOImageDecoderPlugin::sniff()
{
InputMemoryStream stream { { m_context->data, m_context->data_size } };
return decode_ico_header(stream).has_value();
}
bool ICOImageDecoderPlugin::is_animated()
{
return false;
}
size_t ICOImageDecoderPlugin::loop_count()
{
return 0;
}
size_t ICOImageDecoderPlugin::frame_count()
{
return 1;
}
ImageFrameDescriptor ICOImageDecoderPlugin::frame(size_t i)
{
if (i > 0) {
return { bitmap(), 0 };
}
return {};
}
}

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2020, Paul Roukema <roukemap@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
namespace Gfx {
RefPtr<Gfx::Bitmap> load_ico(const StringView& path);
RefPtr<Gfx::Bitmap> load_ico_from_memory(const u8*, size_t);
struct ICOLoadingContext;
class ICOImageDecoderPlugin final : public ImageDecoderPlugin {
public:
virtual ~ICOImageDecoderPlugin() override;
ICOImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile() override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ImageFrameDescriptor frame(size_t i) override;
private:
OwnPtr<ICOLoadingContext> m_context;
};
}

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGfx/BMPLoader.h>
#include <LibGfx/GIFLoader.h>
#include <LibGfx/ICOLoader.h>
#include <LibGfx/ImageDecoder.h>
#include <LibGfx/JPGLoader.h>
#include <LibGfx/PBMLoader.h>
#include <LibGfx/PGMLoader.h>
#include <LibGfx/PNGLoader.h>
#include <LibGfx/PPMLoader.h>
namespace Gfx {
ImageDecoder::ImageDecoder(const u8* data, size_t size)
{
m_plugin = make<PNGImageDecoderPlugin>(data, size);
if (m_plugin->sniff())
return;
m_plugin = make<GIFImageDecoderPlugin>(data, size);
if (m_plugin->sniff())
return;
m_plugin = make<BMPImageDecoderPlugin>(data, size);
if (m_plugin->sniff())
return;
m_plugin = make<PBMImageDecoderPlugin>(data, size);
if (m_plugin->sniff())
return;
m_plugin = make<PGMImageDecoderPlugin>(data, size);
if (m_plugin->sniff())
return;
m_plugin = make<PPMImageDecoderPlugin>(data, size);
if (m_plugin->sniff())
return;
m_plugin = make<ICOImageDecoderPlugin>(data, size);
if (m_plugin->sniff())
return;
m_plugin = make<JPGImageDecoderPlugin>(data, size);
if (m_plugin->sniff())
return;
m_plugin = nullptr;
}
ImageDecoder::~ImageDecoder()
{
}
RefPtr<Gfx::Bitmap> ImageDecoder::bitmap() const
{
if (!m_plugin)
return nullptr;
return m_plugin->bitmap();
}
}

View file

@ -0,0 +1,98 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/OwnPtr.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <LibGfx/Size.h>
namespace Gfx {
class Bitmap;
static constexpr size_t maximum_width_for_decoded_images = 16384;
static constexpr size_t maximum_height_for_decoded_images = 16384;
struct ImageFrameDescriptor {
RefPtr<Bitmap> image;
int duration { 0 };
};
class ImageDecoderPlugin {
public:
virtual ~ImageDecoderPlugin() { }
virtual IntSize size() = 0;
virtual RefPtr<Gfx::Bitmap> bitmap() = 0;
virtual void set_volatile() = 0;
[[nodiscard]] virtual bool set_nonvolatile() = 0;
virtual bool sniff() = 0;
virtual bool is_animated() = 0;
virtual size_t loop_count() = 0;
virtual size_t frame_count() = 0;
virtual ImageFrameDescriptor frame(size_t i) = 0;
protected:
ImageDecoderPlugin() { }
};
class ImageDecoder : public RefCounted<ImageDecoder> {
public:
static NonnullRefPtr<ImageDecoder> create(const u8* data, size_t size) { return adopt(*new ImageDecoder(data, size)); }
static NonnullRefPtr<ImageDecoder> create(const ByteBuffer& data) { return adopt(*new ImageDecoder(data.data(), data.size())); }
~ImageDecoder();
bool is_valid() const { return m_plugin; }
IntSize size() const { return m_plugin ? m_plugin->size() : IntSize(); }
int width() const { return size().width(); }
int height() const { return size().height(); }
RefPtr<Gfx::Bitmap> bitmap() const;
void set_volatile()
{
if (m_plugin)
m_plugin->set_volatile();
}
[[nodiscard]] bool set_nonvolatile() { return m_plugin ? m_plugin->set_nonvolatile() : false; }
bool sniff() const { return m_plugin ? m_plugin->sniff() : false; }
bool is_animated() const { return m_plugin ? m_plugin->is_animated() : false; }
size_t loop_count() const { return m_plugin ? m_plugin->loop_count() : 0; }
size_t frame_count() const { return m_plugin ? m_plugin->frame_count() : 0; }
ImageFrameDescriptor frame(size_t i) const { return m_plugin ? m_plugin->frame(i) : ImageFrameDescriptor(); }
private:
ImageDecoder(const u8*, size_t);
mutable OwnPtr<ImageDecoderPlugin> m_plugin;
};
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2020, The SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Vector.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
#include <LibGfx/Size.h>
namespace Gfx {
RefPtr<Gfx::Bitmap> load_jpg(const StringView& path);
RefPtr<Gfx::Bitmap> load_jpg_from_memory(const u8* data, size_t length);
struct JPGLoadingContext;
class JPGImageDecoderPlugin : public ImageDecoderPlugin {
public:
virtual ~JPGImageDecoderPlugin() override;
JPGImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile() override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ImageFrameDescriptor frame(size_t i) override;
private:
OwnPtr<JPGLoadingContext> m_context;
};
}

View file

@ -0,0 +1,104 @@
/*
* Copyright (c) 2020, the SerenityOS developers.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Types.h>
#include <initializer_list>
namespace Gfx {
template<size_t N, typename T>
class Matrix {
public:
static constexpr size_t Size = N;
Matrix() = default;
Matrix(std::initializer_list<T> elements)
{
ASSERT(elements.size() == N * N);
size_t i = 0;
for (auto& element : elements) {
m_elements[i / N][i % N] = element;
++i;
}
}
template<typename... Args>
Matrix(Args... args)
: Matrix({ (T)args... })
{
}
Matrix(const Matrix& other)
{
__builtin_memcpy(m_elements, other.elements(), sizeof(T) * N * N);
}
auto elements() const { return m_elements; }
auto elements() { return m_elements; }
Matrix operator*(const Matrix& other) const
{
Matrix product;
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
auto& element = product.m_elements[i][j];
if constexpr (N == 4) {
element = m_elements[0][j] * other.m_elements[i][0]
+ m_elements[1][j] * other.m_elements[i][1]
+ m_elements[2][j] * other.m_elements[i][2]
+ m_elements[3][j] * other.m_elements[i][3];
} else if constexpr (N == 3) {
element = m_elements[0][j] * other.m_elements[i][0]
+ m_elements[1][j] * other.m_elements[i][1]
+ m_elements[2][j] * other.m_elements[i][2];
} else if constexpr (N == 2) {
element = m_elements[0][j] * other.m_elements[i][0]
+ m_elements[1][j] * other.m_elements[i][1];
} else if constexpr (N == 1) {
element = m_elements[0][j] * other.m_elements[i][0];
} else {
T value {};
for (size_t k = 0; k < N; ++k)
value += m_elements[k][j] * other.m_elements[i][k];
element = value;
}
}
}
return product;
}
private:
T m_elements[N][N];
};
}
using Gfx::Matrix;

View file

@ -0,0 +1,122 @@
/*
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Matrix.h>
#include <LibGfx/Vector3.h>
#include <math.h>
namespace Gfx {
template<typename T>
class Matrix4x4 final : public Matrix<4, T> {
public:
Matrix4x4() = default;
Matrix4x4(T _11, T _12, T _13, T _14,
T _21, T _22, T _23, T _24,
T _31, T _32, T _33, T _34,
T _41, T _42, T _43, T _44)
: m_elements {
_11, _12, _13, _14,
_21, _22, _23, _24,
_31, _32, _33, _34,
_41, _42, _43, _44
}
{
}
auto elements() const { return m_elements; }
auto elements() { return m_elements; }
Matrix4x4 operator*(const Matrix4x4& other) const
{
Matrix4x4 product;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
product.m_elements[i][j] = m_elements[0][j] * other.m_elements[i][0]
+ m_elements[1][j] * other.m_elements[i][1]
+ m_elements[2][j] * other.m_elements[i][2]
+ m_elements[3][j] * other.m_elements[i][3];
}
}
return product;
}
Vector3<T> transform_point(const Vector3<T>& p) const
{
return Vector3<T>(
p.x() * m_elements[0][0] + p.y() * m_elements[1][0] + p.z() * m_elements[2][0] + m_elements[3][0],
p.x() * m_elements[0][1] + p.y() * m_elements[1][1] + p.z() * m_elements[2][1] + m_elements[3][1],
p.x() * m_elements[0][2] + p.y() * m_elements[1][2] + p.z() * m_elements[2][2] + m_elements[3][2]);
}
static Matrix4x4 translate(const Vector3<T>& p)
{
return Matrix4x4(
1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
p.x(), p.y(), p.z(), 1);
}
static Matrix4x4 scale(const Vector3<T>& s)
{
return Matrix4x4(
s.x(), 0, 0, 0,
0, s.y(), 0, 0,
0, 0, s.z(), 0,
0, 0, 0, 1);
}
static Matrix4x4 rotate(const Vector3<T>& axis, T angle)
{
T c = cos(angle);
T s = sin(angle);
T t = 1 - c;
T x = axis.x();
T y = axis.y();
T z = axis.z();
return Matrix4x4(
t * x * x + c, t * x * y - z * s, t * x * z + y * s, 0,
t * x * y + z * s, t * y * y + c, t * y * z - x * s, 0,
t * x * z - y * s, t * y * z + x * s, t * z * z + c, 0,
0, 0, 0, 1);
}
private:
T m_elements[4][4];
};
typedef Matrix4x4<float> FloatMatrix4x4;
typedef Matrix4x4<double> DoubleMatrix4x4;
}
using Gfx::DoubleMatrix4x4;
using Gfx::FloatMatrix4x4;
using Gfx::Matrix4x4;

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
namespace Gfx {
enum class Orientation {
Horizontal,
Vertical
};
}
using Gfx::Orientation;

View file

@ -0,0 +1,225 @@
/*
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "PBMLoader.h"
#include "PortableImageLoaderCommon.h"
#include "Streamer.h"
#include <AK/Endian.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/StringBuilder.h>
#include <string.h>
namespace Gfx {
struct PBMLoadingContext {
enum Type {
Unknown,
ASCII,
RAWBITS
};
enum State {
NotDecoded = 0,
Error,
MagicNumber,
Width,
Height,
Bitmap,
Decoded
};
static constexpr auto ascii_magic_number = '1';
static constexpr auto binary_magic_number = '4';
static constexpr auto image_type = "PBM";
Type type { Type::Unknown };
State state { State::NotDecoded };
const u8* data { nullptr };
size_t data_size { 0 };
size_t width { 0 };
size_t height { 0 };
RefPtr<Gfx::Bitmap> bitmap;
};
static bool read_image_data(PBMLoadingContext& context, Streamer& streamer)
{
u8 byte;
Vector<Gfx::Color> color_data;
if (context.type == PBMLoadingContext::ASCII) {
while (streamer.read(byte)) {
if (byte == '0') {
color_data.append(Color::White);
} else if (byte == '1') {
color_data.append(Color::Black);
}
}
} else if (context.type == PBMLoadingContext::RAWBITS) {
size_t color_index = 0;
while (streamer.read(byte)) {
for (int i = 0; i < 8; i++) {
int val = byte & 0x80;
if (val == 0) {
color_data.append(Color::White);
} else {
color_data.append(Color::Black);
}
byte = byte << 1;
color_index++;
if (color_index % context.width == 0) {
break;
}
}
}
}
size_t context_size = (u32)context.width * (u32)context.height;
if (context_size != color_data.size()) {
dbgln("Not enough color data in image.");
return false;
}
if (!create_bitmap(context)) {
return false;
}
set_pixels(context, color_data);
context.state = PBMLoadingContext::State::Bitmap;
return true;
}
RefPtr<Gfx::Bitmap> load_pbm(const StringView& path)
{
return load<PBMLoadingContext>(path);
}
RefPtr<Gfx::Bitmap> load_pbm_from_memory(const u8* data, size_t length)
{
auto bitmap = load_impl<PBMLoadingContext>(data, length);
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PBM: <memory>", bitmap->size()));
return bitmap;
}
PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size)
{
m_context = make<PBMLoadingContext>();
m_context->data = data;
m_context->data_size = size;
}
PBMImageDecoderPlugin::~PBMImageDecoderPlugin()
{
}
IntSize PBMImageDecoderPlugin::size()
{
if (m_context->state == PBMLoadingContext::State::Error)
return {};
if (m_context->state < PBMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return {};
}
return { m_context->width, m_context->height };
}
RefPtr<Gfx::Bitmap> PBMImageDecoderPlugin::bitmap()
{
if (m_context->state == PBMLoadingContext::State::Error)
return nullptr;
if (m_context->state < PBMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return nullptr;
}
ASSERT(m_context->bitmap);
return m_context->bitmap;
}
void PBMImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
m_context->bitmap->set_volatile();
}
bool PBMImageDecoderPlugin::set_nonvolatile()
{
if (!m_context->bitmap)
return false;
return m_context->bitmap->set_nonvolatile();
}
bool PBMImageDecoderPlugin::sniff()
{
if (m_context->data_size < 2)
return false;
if (m_context->data[0] == 'P' && m_context->data[1] == '1')
return true;
if (m_context->data[0] == 'P' && m_context->data[1] == '4')
return true;
return false;
}
bool PBMImageDecoderPlugin::is_animated()
{
return false;
}
size_t PBMImageDecoderPlugin::loop_count()
{
return 0;
}
size_t PBMImageDecoderPlugin::frame_count()
{
return 1;
}
ImageFrameDescriptor PBMImageDecoderPlugin::frame(size_t i)
{
if (i > 0) {
return { bitmap(), 0 };
}
return {};
}
}

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
namespace Gfx {
RefPtr<Gfx::Bitmap> load_pbm(const StringView& path);
RefPtr<Gfx::Bitmap> load_pbm_from_memory(const u8*, size_t);
struct PBMLoadingContext;
class PBMImageDecoderPlugin final : public ImageDecoderPlugin {
public:
PBMImageDecoderPlugin(const u8*, size_t);
virtual ~PBMImageDecoderPlugin() override;
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile() override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ImageFrameDescriptor frame(size_t i) override;
private:
OwnPtr<PBMLoadingContext> m_context;
};
}

View file

@ -0,0 +1,228 @@
/*
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "PGMLoader.h"
#include "PortableImageLoaderCommon.h"
#include "Streamer.h"
#include <AK/Endian.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/StringBuilder.h>
#include <string.h>
namespace Gfx {
struct PGMLoadingContext {
enum Type {
Unknown,
ASCII,
RAWBITS
};
enum State {
NotDecoded = 0,
Error,
MagicNumber,
Width,
Height,
Maxval,
Bitmap,
Decoded
};
static constexpr auto ascii_magic_number = '2';
static constexpr auto binary_magic_number = '5';
static constexpr auto image_type = "PGM";
Type type { Type::Unknown };
State state { State::NotDecoded };
const u8* data { nullptr };
size_t data_size { 0 };
u16 width { 0 };
u16 height { 0 };
u16 max_val { 0 };
RefPtr<Gfx::Bitmap> bitmap;
};
static void set_adjusted_pixels(PGMLoadingContext& context, const AK::Vector<Gfx::Color>& color_data)
{
size_t index = 0;
for (size_t y = 0; y < context.height; ++y) {
for (size_t x = 0; x < context.width; ++x) {
Color color = color_data.at(index);
if (context.max_val < 255) {
color = adjust_color(context.max_val, color);
}
context.bitmap->set_pixel(x, y, color);
++index;
}
}
}
static bool read_image_data(PGMLoadingContext& context, Streamer& streamer)
{
Vector<Gfx::Color> color_data;
if (context.type == PGMLoadingContext::ASCII) {
u16 value;
while (true) {
if (!read_number(streamer, &value))
break;
if (!read_white_space(context, streamer))
break;
color_data.append({ (u8)value, (u8)value, (u8)value });
}
} else if (context.type == PGMLoadingContext::RAWBITS) {
u8 pixel;
while (streamer.read(pixel)) {
color_data.append({ pixel, pixel, pixel });
}
}
size_t context_size = (u32)context.width * (u32)context.height;
if (context_size != color_data.size()) {
dbgln("Not enough color data in image.");
return false;
}
if (!create_bitmap(context))
return false;
set_adjusted_pixels(context, color_data);
context.state = PGMLoadingContext::State::Bitmap;
return true;
}
RefPtr<Gfx::Bitmap> load_pgm(const StringView& path)
{
return load<PGMLoadingContext>(path);
}
RefPtr<Gfx::Bitmap> load_pgm_from_memory(const u8* data, size_t length)
{
auto bitmap = load_impl<PGMLoadingContext>(data, length);
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PGM: <memory>", bitmap->size()));
return bitmap;
}
PGMImageDecoderPlugin::PGMImageDecoderPlugin(const u8* data, size_t size)
{
m_context = make<PGMLoadingContext>();
m_context->data = data;
m_context->data_size = size;
}
PGMImageDecoderPlugin::~PGMImageDecoderPlugin()
{
}
IntSize PGMImageDecoderPlugin::size()
{
if (m_context->state == PGMLoadingContext::State::Error)
return {};
if (m_context->state < PGMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return {};
}
return { m_context->width, m_context->height };
}
RefPtr<Gfx::Bitmap> PGMImageDecoderPlugin::bitmap()
{
if (m_context->state == PGMLoadingContext::State::Error)
return nullptr;
if (m_context->state < PGMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return nullptr;
}
ASSERT(m_context->bitmap);
return m_context->bitmap;
}
void PGMImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
m_context->bitmap->set_volatile();
}
bool PGMImageDecoderPlugin::set_nonvolatile()
{
if (!m_context->bitmap)
return false;
return m_context->bitmap->set_nonvolatile();
}
bool PGMImageDecoderPlugin::sniff()
{
if (m_context->data_size < 2)
return false;
if (m_context->data[0] == 'P' && m_context->data[1] == '2')
return true;
if (m_context->data[0] == 'P' && m_context->data[1] == '5')
return true;
return false;
}
bool PGMImageDecoderPlugin::is_animated()
{
return false;
}
size_t PGMImageDecoderPlugin::loop_count()
{
return 0;
}
size_t PGMImageDecoderPlugin::frame_count()
{
return 1;
}
ImageFrameDescriptor PGMImageDecoderPlugin::frame(size_t i)
{
if (i > 0) {
return { bitmap(), 0 };
}
return {};
}
}

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
namespace Gfx {
RefPtr<Gfx::Bitmap> load_pgm(const StringView& path);
RefPtr<Gfx::Bitmap> load_pgm_from_memory(const u8*, size_t);
struct PGMLoadingContext;
class PGMImageDecoderPlugin final : public ImageDecoderPlugin {
public:
PGMImageDecoderPlugin(const u8*, size_t);
virtual ~PGMImageDecoderPlugin() override;
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile() override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ImageFrameDescriptor frame(size_t i) override;
private:
OwnPtr<PGMLoadingContext> m_context;
};
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
namespace Gfx {
RefPtr<Gfx::Bitmap> load_png(const StringView& path);
RefPtr<Gfx::Bitmap> load_png_from_memory(const u8*, size_t);
struct PNGLoadingContext;
class PNGImageDecoderPlugin final : public ImageDecoderPlugin {
public:
virtual ~PNGImageDecoderPlugin() override;
PNGImageDecoderPlugin(const u8*, size_t);
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile() override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ImageFrameDescriptor frame(size_t i) override;
private:
OwnPtr<PNGLoadingContext> m_context;
};
}

View file

@ -0,0 +1,230 @@
/*
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "PPMLoader.h"
#include "PortableImageLoaderCommon.h"
#include "Streamer.h"
#include <AK/Endian.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/ScopeGuard.h>
#include <AK/StringBuilder.h>
#include <string.h>
namespace Gfx {
struct PPMLoadingContext {
enum Type {
Unknown,
ASCII,
RAWBITS
};
enum State {
NotDecoded = 0,
Error,
MagicNumber,
Width,
Height,
Maxval,
Bitmap,
Decoded
};
static constexpr auto ascii_magic_number = '3';
static constexpr auto binary_magic_number = '6';
static constexpr auto image_type = "PPM";
Type type { Type::Unknown };
State state { State::NotDecoded };
const u8* data { nullptr };
size_t data_size { 0 };
u16 width { 0 };
u16 height { 0 };
u16 max_val { 0 };
RefPtr<Gfx::Bitmap> bitmap;
};
static bool read_image_data(PPMLoadingContext& context, Streamer& streamer)
{
Vector<Gfx::Color> color_data;
color_data.ensure_capacity(context.width * context.height);
if (context.type == PPMLoadingContext::ASCII) {
u16 red;
u16 green;
u16 blue;
while (true) {
if (!read_number(streamer, &red))
break;
if (!read_white_space(context, streamer))
break;
if (!read_number(streamer, &green))
break;
if (!read_white_space(context, streamer))
break;
if (!read_number(streamer, &blue))
break;
if (!read_white_space(context, streamer))
break;
Color color { (u8)red, (u8)green, (u8)blue };
if (context.max_val < 255)
color = adjust_color(context.max_val, color);
color_data.append(color);
}
} else if (context.type == PPMLoadingContext::RAWBITS) {
u8 pixel[3];
while (streamer.read_bytes(pixel, 3)) {
color_data.append({ pixel[0], pixel[1], pixel[2] });
}
}
if (context.width * context.height != color_data.size())
return false;
if (!create_bitmap(context)) {
return false;
}
set_pixels(context, color_data);
context.state = PPMLoadingContext::State::Bitmap;
return true;
}
RefPtr<Gfx::Bitmap> load_ppm(const StringView& path)
{
return load<PPMLoadingContext>(path);
}
RefPtr<Gfx::Bitmap> load_ppm_from_memory(const u8* data, size_t length)
{
auto bitmap = load_impl<PPMLoadingContext>(data, length);
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PPM: <memory>", bitmap->size()));
return bitmap;
}
PPMImageDecoderPlugin::PPMImageDecoderPlugin(const u8* data, size_t size)
{
m_context = make<PPMLoadingContext>();
m_context->data = data;
m_context->data_size = size;
}
PPMImageDecoderPlugin::~PPMImageDecoderPlugin()
{
}
IntSize PPMImageDecoderPlugin::size()
{
if (m_context->state == PPMLoadingContext::State::Error)
return {};
if (m_context->state < PPMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return {};
}
return { m_context->width, m_context->height };
}
RefPtr<Gfx::Bitmap> PPMImageDecoderPlugin::bitmap()
{
if (m_context->state == PPMLoadingContext::State::Error)
return nullptr;
if (m_context->state < PPMLoadingContext::State::Decoded) {
bool success = decode(*m_context);
if (!success)
return nullptr;
}
ASSERT(m_context->bitmap);
return m_context->bitmap;
}
void PPMImageDecoderPlugin::set_volatile()
{
if (m_context->bitmap)
m_context->bitmap->set_volatile();
}
bool PPMImageDecoderPlugin::set_nonvolatile()
{
if (!m_context->bitmap)
return false;
return m_context->bitmap->set_nonvolatile();
}
bool PPMImageDecoderPlugin::sniff()
{
if (m_context->data_size < 2)
return false;
if (m_context->data[0] == 'P' && m_context->data[1] == '3')
return true;
if (m_context->data[0] == 'P' && m_context->data[1] == '6')
return true;
return false;
}
bool PPMImageDecoderPlugin::is_animated()
{
return false;
}
size_t PPMImageDecoderPlugin::loop_count()
{
return 0;
}
size_t PPMImageDecoderPlugin::frame_count()
{
return 1;
}
ImageFrameDescriptor PPMImageDecoderPlugin::frame(size_t i)
{
if (i > 0) {
return { bitmap(), 0 };
}
return {};
}
}

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h>
namespace Gfx {
RefPtr<Gfx::Bitmap> load_ppm(const StringView& path);
RefPtr<Gfx::Bitmap> load_ppm_from_memory(const u8*, size_t);
struct PPMLoadingContext;
class PPMImageDecoderPlugin final : public ImageDecoderPlugin {
public:
PPMImageDecoderPlugin(const u8*, size_t);
virtual ~PPMImageDecoderPlugin() override;
virtual IntSize size() override;
virtual RefPtr<Gfx::Bitmap> bitmap() override;
virtual void set_volatile() override;
[[nodiscard]] virtual bool set_nonvolatile() override;
virtual bool sniff() override;
virtual bool is_animated() override;
virtual size_t loop_count() override;
virtual size_t frame_count() override;
virtual ImageFrameDescriptor frame(size_t i) override;
private:
OwnPtr<PPMLoadingContext> m_context;
};
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,167 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Vector.h>
#include <LibGfx/Color.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
#include <LibGfx/TextAlignment.h>
#include <LibGfx/TextElision.h>
namespace Gfx {
class Painter {
public:
explicit Painter(Gfx::Bitmap&);
~Painter();
enum class LineStyle {
Solid,
Dotted,
Dashed,
};
void clear_rect(const IntRect&, Color);
void fill_rect(const IntRect&, Color);
void fill_rect_with_dither_pattern(const IntRect&, Color, Color);
void fill_rect_with_checkerboard(const IntRect&, const IntSize&, Color color_dark, Color color_light);
void fill_rect_with_gradient(Orientation, const IntRect&, Color gradient_start, Color gradient_end);
void fill_rect_with_gradient(const IntRect&, Color gradient_start, Color gradient_end);
void fill_ellipse(const IntRect&, Color);
void draw_rect(const IntRect&, Color, bool rough = false);
void draw_focus_rect(const IntRect&, Color);
void draw_bitmap(const IntPoint&, const CharacterBitmap&, Color = Color());
void draw_bitmap(const IntPoint&, const GlyphBitmap&, Color = Color());
void draw_scaled_bitmap(const IntRect& dst_rect, const Gfx::Bitmap&, const IntRect& src_rect, float opacity = 1.0f);
void draw_triangle(const IntPoint&, const IntPoint&, const IntPoint&, Color);
void draw_ellipse_intersecting(const IntRect&, Color, int thickness = 1);
void set_pixel(const IntPoint&, Color);
void set_pixel(int x, int y, Color color) { set_pixel({ x, y }, color); }
void draw_line(const IntPoint&, const IntPoint&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
void draw_quadratic_bezier_curve(const IntPoint& control_point, const IntPoint&, const IntPoint&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
void draw_elliptical_arc(const IntPoint& p1, const IntPoint& p2, const IntPoint& center, const FloatPoint& radii, float x_axis_rotation, float theta_1, float theta_delta, Color, int thickness = 1, LineStyle style = LineStyle::Solid);
void blit(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, float opacity = 1.0f);
void blit_dimmed(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect);
void blit_brightened(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect);
void blit_filtered(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, Function<Color(Color)>);
void draw_tiled_bitmap(const IntRect& dst_rect, const Gfx::Bitmap&);
void blit_offset(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, const IntPoint&);
void blit_scaled(const IntRect&, const Gfx::Bitmap&, const IntRect&, float, float);
void blit_disabled(const IntPoint&, const Gfx::Bitmap&, const IntRect&, const Palette&);
void draw_text(const IntRect&, const StringView&, const Font&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
void draw_text(const IntRect&, const StringView&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
void draw_text(const IntRect&, const Utf32View&, const Font&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
void draw_text(const IntRect&, const Utf32View&, TextAlignment = TextAlignment::TopLeft, Color = Color::Black, TextElision = TextElision::None);
void draw_text(Function<void(const IntRect&, u32)>, const IntRect&, const StringView&, const Font&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None);
void draw_text(Function<void(const IntRect&, u32)>, const IntRect&, const Utf8View&, const Font&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None);
void draw_text(Function<void(const IntRect&, u32)>, const IntRect&, const Utf32View&, const Font&, TextAlignment = TextAlignment::TopLeft, TextElision = TextElision::None);
void draw_glyph(const IntPoint&, u32, Color);
void draw_glyph(const IntPoint&, u32, const Font&, Color);
void draw_emoji(const IntPoint&, const Gfx::Bitmap&, const Font&);
void draw_glyph_or_emoji(const IntPoint&, u32 code_point, const Font&, Color);
static void for_each_line_segment_on_bezier_curve(const FloatPoint& control_point, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>&);
static void for_each_line_segment_on_bezier_curve(const FloatPoint& control_point, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>&&);
static void for_each_line_segment_on_elliptical_arc(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(const FloatPoint&, const FloatPoint&)>&);
static void for_each_line_segment_on_elliptical_arc(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta, Function<void(const FloatPoint&, const FloatPoint&)>&&);
void stroke_path(const Path&, Color, int thickness);
enum class WindingRule {
Nonzero,
EvenOdd,
};
void fill_path(Path&, Color, WindingRule rule = WindingRule::Nonzero);
const Font& font() const { return *state().font; }
void set_font(const Font& font) { state().font = &font; }
enum class DrawOp {
Copy,
Xor,
Invert
};
void set_draw_op(DrawOp op) { state().draw_op = op; }
DrawOp draw_op() const { return state().draw_op; }
void add_clip_rect(const IntRect& rect);
void clear_clip_rect();
IntRect clip_rect() const { return state().clip_rect; }
void translate(int dx, int dy) { state().translation.move_by(dx, dy); }
void translate(const IntPoint& delta) { state().translation.move_by(delta); }
IntPoint translation() const { return state().translation; }
Gfx::Bitmap* target() { return m_target.ptr(); }
void save() { m_state_stack.append(m_state_stack.last()); }
void restore()
{
ASSERT(m_state_stack.size() > 1);
m_state_stack.take_last();
}
protected:
void set_pixel_with_draw_op(u32& pixel, const Color&);
void fill_scanline_with_draw_op(int y, int x, int width, const Color& color);
void fill_rect_with_draw_op(const IntRect&, Color);
void blit_with_alpha(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect);
void blit_with_opacity(const IntPoint&, const Gfx::Bitmap&, const IntRect& src_rect, float opacity);
void draw_pixel(const IntPoint&, Color, int thickness = 1);
struct State {
const Font* font;
IntPoint translation;
IntRect clip_rect;
DrawOp draw_op;
};
State& state() { return m_state_stack.last(); }
const State& state() const { return m_state_stack.last(); }
IntRect m_clip_origin;
NonnullRefPtr<Gfx::Bitmap> m_target;
Vector<State, 4> m_state_stack;
};
class PainterStateSaver {
public:
explicit PainterStateSaver(Painter&);
~PainterStateSaver();
private:
Painter& m_painter;
};
}

View file

@ -0,0 +1,117 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Badge.h>
#include <AK/SharedBuffer.h>
#include <LibGfx/Palette.h>
#include <string.h>
namespace Gfx {
NonnullRefPtr<PaletteImpl> PaletteImpl::create_with_shared_buffer(SharedBuffer& buffer)
{
return adopt(*new PaletteImpl(buffer));
}
PaletteImpl::PaletteImpl(SharedBuffer& buffer)
: m_theme_buffer(buffer)
{
}
Palette::Palette(const PaletteImpl& impl)
: m_impl(impl)
{
}
Palette::~Palette()
{
}
const SystemTheme& PaletteImpl::theme() const
{
return *m_theme_buffer->data<SystemTheme>();
}
Color PaletteImpl::color(ColorRole role) const
{
ASSERT((int)role < (int)ColorRole::__Count);
return Color::from_rgba(theme().color[(int)role]);
}
int PaletteImpl::metric(MetricRole role) const
{
ASSERT((int)role < (int)MetricRole::__Count);
return theme().metric[(int)role];
}
String PaletteImpl::path(PathRole role) const
{
ASSERT((int)role < (int)PathRole::__Count);
return theme().path[(int)role];
}
NonnullRefPtr<PaletteImpl> PaletteImpl::clone() const
{
auto new_theme_buffer = SharedBuffer::create_with_size(m_theme_buffer->size());
memcpy(new_theme_buffer->data<SystemTheme>(), &theme(), m_theme_buffer->size());
return adopt(*new PaletteImpl(*new_theme_buffer));
}
void Palette::set_color(ColorRole role, Color color)
{
if (m_impl->ref_count() != 1)
m_impl = m_impl->clone();
auto& theme = const_cast<SystemTheme&>(impl().theme());
theme.color[(int)role] = color.value();
}
void Palette::set_metric(MetricRole role, int value)
{
if (m_impl->ref_count() != 1)
m_impl = m_impl->clone();
auto& theme = const_cast<SystemTheme&>(impl().theme());
theme.metric[(int)role] = value;
}
void Palette::set_path(PathRole role, String path)
{
if (m_impl->ref_count() != 1)
m_impl = m_impl->clone();
auto& theme = const_cast<SystemTheme&>(impl().theme());
memcpy(theme.path[(int)role], path.characters(), min(path.length() + 1, sizeof(theme.path[(int)role])));
theme.path[(int)role][sizeof(theme.path[(int)role]) - 1] = '\0';
}
PaletteImpl::~PaletteImpl()
{
}
void PaletteImpl::replace_internal_buffer(Badge<GUI::Application>, SharedBuffer& buffer)
{
m_theme_buffer = buffer;
}
}

View file

@ -0,0 +1,158 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <AK/Noncopyable.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <LibGUI/Forward.h>
#include <LibGfx/SystemTheme.h>
namespace Gfx {
class PaletteImpl : public RefCounted<PaletteImpl> {
AK_MAKE_NONCOPYABLE(PaletteImpl);
AK_MAKE_NONMOVABLE(PaletteImpl);
public:
~PaletteImpl();
static NonnullRefPtr<PaletteImpl> create_with_shared_buffer(SharedBuffer&);
NonnullRefPtr<PaletteImpl> clone() const;
Color color(ColorRole) const;
int metric(MetricRole) const;
String path(PathRole) const;
const SystemTheme& theme() const;
void replace_internal_buffer(Badge<GUI::Application>, SharedBuffer& buffer);
private:
explicit PaletteImpl(SharedBuffer&);
RefPtr<SharedBuffer> m_theme_buffer;
};
class Palette {
public:
explicit Palette(const PaletteImpl&);
~Palette();
Color window() const { return color(ColorRole::Window); }
Color window_text() const { return color(ColorRole::WindowText); }
Color selection() const { return color(ColorRole::Selection); }
Color selection_text() const { return color(ColorRole::SelectionText); }
Color inactive_selection() const { return color(ColorRole::InactiveSelection); }
Color inactive_selection_text() const { return color(ColorRole::InactiveSelectionText); }
Color desktop_background() const { return color(ColorRole::DesktopBackground); }
Color active_window_border1() const { return color(ColorRole::ActiveWindowBorder1); }
Color active_window_border2() const { return color(ColorRole::ActiveWindowBorder2); }
Color active_window_title() const { return color(ColorRole::ActiveWindowTitle); }
Color active_window_title_stripes() const { return color(ColorRole::ActiveWindowTitleStripes); }
Color active_window_title_shadow() const { return color(ColorRole::ActiveWindowTitleShadow); }
Color inactive_window_border1() const { return color(ColorRole::InactiveWindowBorder1); }
Color inactive_window_border2() const { return color(ColorRole::InactiveWindowBorder2); }
Color inactive_window_title() const { return color(ColorRole::InactiveWindowTitle); }
Color inactive_window_title_stripes() const { return color(ColorRole::InactiveWindowTitleStripes); }
Color inactive_window_title_shadow() const { return color(ColorRole::InactiveWindowTitleShadow); }
Color moving_window_border1() const { return color(ColorRole::MovingWindowBorder1); }
Color moving_window_border2() const { return color(ColorRole::MovingWindowBorder2); }
Color moving_window_title() const { return color(ColorRole::MovingWindowTitle); }
Color moving_window_title_stripes() const { return color(ColorRole::MovingWindowTitleStripes); }
Color moving_window_title_shadow() const { return color(ColorRole::MovingWindowTitleShadow); }
Color highlight_window_border1() const { return color(ColorRole::HighlightWindowBorder1); }
Color highlight_window_border2() const { return color(ColorRole::HighlightWindowBorder2); }
Color highlight_window_title() const { return color(ColorRole::HighlightWindowTitle); }
Color highlight_window_title_stripes() const { return color(ColorRole::HighlightWindowTitleStripes); }
Color highlight_window_title_shadow() const { return color(ColorRole::HighlightWindowTitleShadow); }
Color highlight_searching() const { return color(ColorRole::HighlightSearching); }
Color highlight_searching_text() const { return color(ColorRole::HighlightSearchingText); }
Color menu_stripe() const { return color(ColorRole::MenuStripe); }
Color menu_base() const { return color(ColorRole::MenuBase); }
Color menu_base_text() const { return color(ColorRole::MenuBaseText); }
Color menu_selection() const { return color(ColorRole::MenuSelection); }
Color menu_selection_text() const { return color(ColorRole::MenuSelectionText); }
Color base() const { return color(ColorRole::Base); }
Color base_text() const { return color(ColorRole::BaseText); }
Color button() const { return color(ColorRole::Button); }
Color button_text() const { return color(ColorRole::ButtonText); }
Color threed_highlight() const { return color(ColorRole::ThreedHighlight); }
Color threed_shadow1() const { return color(ColorRole::ThreedShadow1); }
Color threed_shadow2() const { return color(ColorRole::ThreedShadow2); }
Color hover_highlight() const { return color(ColorRole::HoverHighlight); }
Color rubber_band_fill() const { return color(ColorRole::RubberBandFill); }
Color rubber_band_border() const { return color(ColorRole::RubberBandBorder); }
Color ruler() const { return color(ColorRole::Ruler); }
Color ruler_border() const { return color(ColorRole::RulerBorder); }
Color ruler_active_text() const { return color(ColorRole::RulerActiveText); }
Color ruler_inactive_text() const { return color(ColorRole::RulerInactiveText); }
Color text_cursor() const { return color(ColorRole::TextCursor); }
Color focus_outline() const { return color(ColorRole::FocusOutline); }
Color link() const { return color(ColorRole::Link); }
Color active_link() const { return color(ColorRole::ActiveLink); }
Color visited_link() const { return color(ColorRole::VisitedLink); }
Color syntax_comment() const { return color(ColorRole::SyntaxComment); }
Color syntax_number() const { return color(ColorRole::SyntaxNumber); }
Color syntax_string() const { return color(ColorRole::SyntaxString); }
Color syntax_identifier() const { return color(ColorRole::SyntaxIdentifier); }
Color syntax_type() const { return color(ColorRole::SyntaxType); }
Color syntax_punctuation() const { return color(ColorRole::SyntaxPunctuation); }
Color syntax_operator() const { return color(ColorRole::SyntaxOperator); }
Color syntax_keyword() const { return color(ColorRole::SyntaxKeyword); }
Color syntax_control_keyword() const { return color(ColorRole::SyntaxControlKeyword); }
Color syntax_preprocessor_statement() const { return color(ColorRole::SyntaxPreprocessorStatement); }
Color syntax_preprocessor_value() const { return color(ColorRole::SyntaxPreprocessorValue); }
int window_title_height() const { return metric(MetricRole::TitleHeight); }
int window_title_button_width() const { return metric(MetricRole::TitleButtonWidth); }
int window_title_button_height() const { return metric(MetricRole::TitleButtonHeight); }
String title_button_icons_path() const { return path(PathRole::TitleButtonIcons); }
Color color(ColorRole role) const { return m_impl->color(role); }
int metric(MetricRole role) const { return m_impl->metric(role); }
String path(PathRole role) const { return m_impl->path(role); }
void set_color(ColorRole, Color);
void set_metric(MetricRole, int);
void set_path(PathRole, String);
const SystemTheme& theme() const { return m_impl->theme(); }
PaletteImpl& impl() { return *m_impl; }
const PaletteImpl& impl() const { return *m_impl; }
private:
NonnullRefPtr<PaletteImpl> m_impl;
};
}
using Gfx::Palette;

View file

@ -0,0 +1,234 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/Function.h>
#include <AK/HashTable.h>
#include <AK/QuickSort.h>
#include <AK/StringBuilder.h>
#include <LibGfx/Painter.h>
#include <LibGfx/Path.h>
namespace Gfx {
void Path::close()
{
if (m_segments.size() <= 1)
return;
invalidate_split_lines();
auto& last_point = m_segments.last().point();
for (ssize_t i = m_segments.size() - 1; i >= 0; --i) {
auto& segment = m_segments[i];
if (segment.type() == Segment::Type::MoveTo) {
if (last_point == segment.point())
return;
append_segment<LineSegment>(segment.point());
return;
}
}
}
void Path::close_all_subpaths()
{
if (m_segments.size() <= 1)
return;
invalidate_split_lines();
Optional<FloatPoint> cursor, start_of_subpath;
bool is_first_point_in_subpath { false };
for (auto& segment : m_segments) {
switch (segment.type()) {
case Segment::Type::MoveTo: {
if (cursor.has_value() && !is_first_point_in_subpath) {
// This is a move from a subpath to another
// connect the two ends of this subpath before
// moving on to the next one
ASSERT(start_of_subpath.has_value());
append_segment<MoveSegment>(cursor.value());
append_segment<LineSegment>(start_of_subpath.value());
}
is_first_point_in_subpath = true;
cursor = segment.point();
break;
}
case Segment::Type::LineTo:
case Segment::Type::QuadraticBezierCurveTo:
case Segment::Type::EllipticalArcTo:
if (is_first_point_in_subpath) {
start_of_subpath = cursor;
is_first_point_in_subpath = false;
}
cursor = segment.point();
break;
case Segment::Type::Invalid:
ASSERT_NOT_REACHED();
break;
}
}
}
String Path::to_string() const
{
StringBuilder builder;
builder.append("Path { ");
for (auto& segment : m_segments) {
switch (segment.type()) {
case Segment::Type::MoveTo:
builder.append("MoveTo");
break;
case Segment::Type::LineTo:
builder.append("LineTo");
break;
case Segment::Type::QuadraticBezierCurveTo:
builder.append("QuadraticBezierCurveTo");
break;
case Segment::Type::EllipticalArcTo:
builder.append("EllipticalArcTo");
break;
case Segment::Type::Invalid:
builder.append("Invalid");
break;
}
builder.appendf("(%s", segment.point().to_string().characters());
switch (segment.type()) {
case Segment::Type::QuadraticBezierCurveTo:
builder.append(", ");
builder.append(static_cast<const QuadraticBezierCurveSegment&>(segment).through().to_string());
break;
case Segment::Type::EllipticalArcTo: {
auto& arc = static_cast<const EllipticalArcSegment&>(segment);
builder.appendf(", %s, %s, %f, %f, %f",
arc.radii().to_string().characters(),
arc.center().to_string().characters(),
arc.x_axis_rotation(),
arc.theta_1(),
arc.theta_delta());
break;
}
default:
break;
}
builder.append(") ");
}
builder.append("}");
return builder.to_string();
}
void Path::segmentize_path()
{
Vector<SplitLineSegment> segments;
float min_x = 0;
float min_y = 0;
float max_x = 0;
float max_y = 0;
auto add_point_to_bbox = [&](const Gfx::FloatPoint& point) {
float x = point.x();
float y = point.y();
min_x = min(min_x, x);
min_y = min(min_y, y);
max_x = max(max_x, x);
max_y = max(max_y, y);
};
auto add_line = [&](const auto& p0, const auto& p1) {
float ymax = p0.y(), ymin = p1.y(), x_of_ymin = p1.x(), x_of_ymax = p0.x();
auto slope = p0.x() == p1.x() ? 0 : ((float)(p0.y() - p1.y())) / ((float)(p0.x() - p1.x()));
if (p0.y() < p1.y()) {
swap(ymin, ymax);
swap(x_of_ymin, x_of_ymax);
}
segments.append({ FloatPoint(p0.x(), p0.y()),
FloatPoint(p1.x(), p1.y()),
slope == 0 ? 0 : 1 / slope,
x_of_ymin,
ymax, ymin, x_of_ymax });
add_point_to_bbox(p1);
};
FloatPoint cursor { 0, 0 };
bool first = true;
for (auto& segment : m_segments) {
switch (segment.type()) {
case Segment::Type::MoveTo:
if (first) {
min_x = segment.point().x();
min_y = segment.point().y();
max_x = segment.point().x();
max_y = segment.point().y();
} else {
add_point_to_bbox(segment.point());
}
cursor = segment.point();
break;
case Segment::Type::LineTo: {
add_line(cursor, segment.point());
cursor = segment.point();
break;
}
case Segment::Type::QuadraticBezierCurveTo: {
auto& control = static_cast<QuadraticBezierCurveSegment&>(segment).through();
Painter::for_each_line_segment_on_bezier_curve(control, cursor, segment.point(), [&](const FloatPoint& p0, const FloatPoint& p1) {
add_line(p0, p1);
});
cursor = segment.point();
break;
}
case Segment::Type::EllipticalArcTo: {
auto& arc = static_cast<EllipticalArcSegment&>(segment);
Painter::for_each_line_segment_on_elliptical_arc(cursor, arc.point(), arc.center(), arc.radii(), arc.x_axis_rotation(), arc.theta_1(), arc.theta_delta(), [&](const FloatPoint& p0, const FloatPoint& p1) {
add_line(p0, p1);
});
cursor = segment.point();
break;
}
case Segment::Type::Invalid:
ASSERT_NOT_REACHED();
}
first = false;
}
// sort segments by ymax
quick_sort(segments, [](const auto& line0, const auto& line1) {
return line1.maximum_y < line0.maximum_y;
});
m_split_lines = move(segments);
m_bounding_box = Gfx::FloatRect { min_x, min_y, max_x - min_x, max_y - min_y };
}
}

View file

@ -0,0 +1,220 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/HashMap.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
namespace Gfx {
class Segment : public RefCounted<Segment> {
public:
enum class Type {
Invalid,
MoveTo,
LineTo,
QuadraticBezierCurveTo,
EllipticalArcTo,
};
Segment(const FloatPoint& point)
: m_point(point)
{
}
virtual ~Segment() = default;
const FloatPoint& point() const { return m_point; }
virtual Type type() const = 0;
protected:
FloatPoint m_point;
};
class MoveSegment final : public Segment {
public:
MoveSegment(const FloatPoint& point)
: Segment(point)
{
}
private:
virtual Type type() const override { return Segment::Type::MoveTo; }
};
class LineSegment final : public Segment {
public:
LineSegment(const FloatPoint& point)
: Segment(point)
{
}
virtual ~LineSegment() override = default;
private:
virtual Type type() const override { return Segment::Type::LineTo; }
};
class QuadraticBezierCurveSegment final : public Segment {
public:
QuadraticBezierCurveSegment(const FloatPoint& point, const FloatPoint& through)
: Segment(point)
, m_through(through)
{
}
virtual ~QuadraticBezierCurveSegment() override = default;
const FloatPoint& through() const { return m_through; }
private:
virtual Type type() const override { return Segment::Type::QuadraticBezierCurveTo; }
FloatPoint m_through;
};
class EllipticalArcSegment final : public Segment {
public:
EllipticalArcSegment(const FloatPoint& point, const FloatPoint& center, const FloatPoint radii, float x_axis_rotation, float theta_1, float theta_delta)
: Segment(point)
, m_center(center)
, m_radii(radii)
, m_x_axis_rotation(x_axis_rotation)
, m_theta_1(theta_1)
, m_theta_delta(theta_delta)
{
}
virtual ~EllipticalArcSegment() override = default;
const FloatPoint& center() const { return m_center; }
const FloatPoint& radii() const { return m_radii; }
float x_axis_rotation() const { return m_x_axis_rotation; }
float theta_1() const { return m_theta_1; }
float theta_delta() const { return m_theta_delta; }
private:
virtual Type type() const override { return Segment::Type::EllipticalArcTo; }
FloatPoint m_center;
FloatPoint m_radii;
float m_x_axis_rotation;
float m_theta_1;
float m_theta_delta;
};
class Path {
public:
Path() { }
void move_to(const FloatPoint& point)
{
append_segment<MoveSegment>(point);
}
void line_to(const FloatPoint& point)
{
append_segment<LineSegment>(point);
invalidate_split_lines();
}
void quadratic_bezier_curve_to(const FloatPoint& through, const FloatPoint& point)
{
append_segment<QuadraticBezierCurveSegment>(point, through);
invalidate_split_lines();
}
void elliptical_arc_to(const FloatPoint& point, const FloatPoint& center, const FloatPoint& radii, float x_axis_rotation, float theta_1, float theta_delta)
{
append_segment<EllipticalArcSegment>(point, center, radii, x_axis_rotation, theta_1, theta_delta);
invalidate_split_lines();
}
void close();
void close_all_subpaths();
struct SplitLineSegment {
FloatPoint from, to;
float inverse_slope;
float x_of_minimum_y;
float maximum_y;
float minimum_y;
float x;
};
const NonnullRefPtrVector<Segment>& segments() const { return m_segments; }
const auto& split_lines()
{
if (!m_split_lines.has_value()) {
segmentize_path();
ASSERT(m_split_lines.has_value());
}
return m_split_lines.value();
}
const Gfx::FloatRect& bounding_box()
{
if (!m_bounding_box.has_value()) {
segmentize_path();
ASSERT(m_bounding_box.has_value());
}
return m_bounding_box.value();
}
String to_string() const;
private:
void invalidate_split_lines()
{
m_split_lines.clear();
}
void segmentize_path();
template<typename T, typename... Args>
void append_segment(Args&&... args)
{
m_segments.append(adopt(*new T(forward<Args>(args)...)));
}
NonnullRefPtrVector<Segment> m_segments {};
Optional<Vector<SplitLineSegment>> m_split_lines {};
Optional<Gfx::FloatRect> m_bounding_box;
};
inline const LogStream& operator<<(const LogStream& stream, const Path& path)
{
return stream << path.to_string();
}
}

View file

@ -0,0 +1,88 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/String.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
namespace Gfx {
template<typename T>
void Point<T>::constrain(const Rect<T>& rect)
{
if (x() < rect.left()) {
set_x(rect.left());
} else if (x() > rect.right()) {
set_x(rect.right());
}
if (y() < rect.top()) {
set_y(rect.top());
} else if (y() > rect.bottom()) {
set_y(rect.bottom());
}
}
template<>
String IntPoint::to_string() const
{
return String::formatted("[{},{}]", x(), y());
}
template<>
String FloatPoint::to_string() const
{
return String::formatted("[{},{}]", x(), y());
}
}
namespace IPC {
bool encode(Encoder& encoder, const Gfx::IntPoint& point)
{
encoder << point.x() << point.y();
return true;
}
bool decode(Decoder& decoder, Gfx::IntPoint& point)
{
int x = 0;
int y = 0;
if (!decoder.decode(x))
return false;
if (!decoder.decode(y))
return false;
point = { x, y };
return true;
}
}
template class Gfx::Point<int>;
template class Gfx::Point<float>;

View file

@ -0,0 +1,257 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Format.h>
#include <AK/StdLibExtras.h>
#include <LibGfx/Forward.h>
#include <LibGfx/Orientation.h>
#include <LibIPC/Forward.h>
#include <math.h>
#include <stdlib.h>
namespace Gfx {
template<typename T>
class Point {
public:
Point() { }
Point(T x, T y)
: m_x(x)
, m_y(y)
{
}
template<typename U>
Point(U x, U y)
: m_x(x)
, m_y(y)
{
}
template<typename U>
explicit Point(const Point<U>& other)
: m_x(other.x())
, m_y(other.y())
{
}
T x() const { return m_x; }
T y() const { return m_y; }
void set_x(T x) { m_x = x; }
void set_y(T y) { m_y = y; }
void move_by(T dx, T dy)
{
m_x += dx;
m_y += dy;
}
void move_by(const Point<T>& delta)
{
move_by(delta.x(), delta.y());
}
Point<T> translated(const Point<T>& delta) const
{
Point<T> point = *this;
point.move_by(delta);
return point;
}
Point<T> translated(T dx, T dy) const
{
Point<T> point = *this;
point.move_by(dx, dy);
return point;
}
Point<T> translated(T dboth) const
{
Point<T> point = *this;
point.move_by(dboth, dboth);
return point;
}
void constrain(const Rect<T>&);
Point<T> constrained(const Rect<T>& rect) const
{
Point<T> point = *this;
point.constrain(rect);
return point;
}
bool operator==(const Point<T>& other) const
{
return m_x == other.m_x && m_y == other.m_y;
}
bool operator!=(const Point<T>& other) const
{
return !(*this == other);
}
Point<T> operator+(const Point<T>& other) const { return { m_x + other.m_x, m_y + other.m_y }; }
Point<T>& operator+=(const Point<T>& other)
{
m_x += other.m_x;
m_y += other.m_y;
return *this;
}
Point<T> operator-() const { return { -m_x, -m_y }; }
Point<T> operator-(const Point<T>& other) const { return { m_x - other.m_x, m_y - other.m_y }; }
Point<T>& operator-=(const Point<T>& other)
{
m_x -= other.m_x;
m_y -= other.m_y;
return *this;
}
Point<T> operator*(T factor) const { return { m_x * factor, m_y * factor }; }
Point<T>& operator*=(T factor)
{
m_x *= factor;
m_y *= factor;
return *this;
}
Point<T> operator/(T factor) const { return { m_x / factor, m_y / factor }; }
Point<T>& operator/=(T factor)
{
m_x /= factor;
m_y /= factor;
return *this;
}
bool is_null() const { return !m_x && !m_y; }
T primary_offset_for_orientation(Orientation orientation) const
{
return orientation == Orientation::Vertical ? y() : x();
}
void set_primary_offset_for_orientation(Orientation orientation, T value)
{
if (orientation == Orientation::Vertical) {
set_y(value);
} else {
set_x(value);
}
}
T secondary_offset_for_orientation(Orientation orientation) const
{
return orientation == Orientation::Vertical ? x() : y();
}
void set_secondary_offset_for_orientation(Orientation orientation, T value)
{
if (orientation == Orientation::Vertical) {
set_x(value);
} else {
set_y(value);
}
}
T dx_relative_to(const Point<T>& other) const
{
return x() - other.x();
}
T dy_relative_to(const Point<T>& other) const
{
return y() - other.y();
}
// Returns pixels moved from other in either direction
T pixels_moved(const Point<T>& other) const
{
return max(abs(dx_relative_to(other)), abs(dy_relative_to(other)));
}
float distance_from(const Point<T>& other) const
{
if (*this == other)
return 0;
return sqrtf(powf(m_x - other.m_x, 2.0f) + powf(m_y - other.m_y, 2.0f));
}
Point absolute_relative_distance_to(const Point& other) const
{
return { abs(dx_relative_to(other)), abs(dy_relative_to(other)) };
}
template<typename U>
Point<U> to_type() const
{
return Point<U>(*this);
}
String to_string() const;
private:
T m_x { 0 };
T m_y { 0 };
};
template<typename T>
const LogStream& operator<<(const LogStream& stream, const Point<T>& point)
{
return stream << point.to_string();
}
using IntPoint = Point<int>;
using FloatPoint = Point<float>;
}
namespace AK {
template<typename T>
struct Formatter<Gfx::Point<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Point<T>& value)
{
Formatter<StringView>::format(builder, value.to_string());
}
};
}
namespace IPC {
bool encode(Encoder&, const Gfx::IntPoint&);
bool decode(Decoder&, Gfx::IntPoint&);
}

View file

@ -0,0 +1,309 @@
/*
* Copyright (c) 2020, Hüseyin Aslıtürk <asliturk@hotmail.com>
* Copyright (c) 2020, the SerenityOS developers
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Array.h>
#include <AK/Endian.h>
#include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/ScopeGuard.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Color.h>
#include <LibGfx/ImageDecoder.h>
#include <LibGfx/Streamer.h>
//#define PORTABLE_IMAGE_LOADER_DEBUG
namespace Gfx {
static constexpr Color adjust_color(u16 max_val, Color color)
{
color.set_red((color.red() * 255) / max_val);
color.set_green((color.green() * 255) / max_val);
color.set_blue((color.blue() * 255) / max_val);
return color;
}
template<typename TValue>
static bool read_number(Streamer& streamer, TValue* value)
{
u8 byte {};
StringBuilder sb {};
while (streamer.read(byte)) {
if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
streamer.step_back();
break;
}
sb.append(byte);
}
const auto opt_value = sb.to_string().to_uint();
if (!opt_value.has_value()) {
*value = 0;
return false;
}
*value = static_cast<u16>(opt_value.value());
return true;
}
template<typename TContext>
static bool read_comment([[maybe_unused]] TContext& context, Streamer& streamer)
{
bool exist = false;
u8 byte {};
while (streamer.read(byte)) {
if (byte == '#') {
exist = true;
} else if (byte == '\t' || byte == '\n') {
return exist;
}
}
return exist;
}
template<typename TContext>
static bool read_magic_number(TContext& context, Streamer& streamer)
{
if (context.state >= TContext::State::MagicNumber) {
return true;
}
if (!context.data || context.data_size < 2) {
context.state = TContext::State::Error;
#ifdef PORTABLE_IMAGE_LOADER_DEBUG
dbg() << "There is no enough data for " << TContext::image_type;
#endif
return false;
}
u8 magic_number[2] {};
if (!streamer.read_bytes(magic_number, 2)) {
context.state = TContext::State::Error;
#ifdef PORTABLE_IMAGE_LOADER_DEBUG
dbg() << "We can't read magic number for " << TContext::image_type;
#endif
return false;
}
if (magic_number[0] == 'P' && magic_number[1] == TContext::ascii_magic_number) {
context.type = TContext::Type::ASCII;
context.state = TContext::State::MagicNumber;
return true;
}
if (magic_number[0] == 'P' && magic_number[1] == TContext::binary_magic_number) {
context.type = TContext::Type::RAWBITS;
context.state = TContext::State::MagicNumber;
return true;
}
context.state = TContext::State::Error;
#ifdef PORTABLE_IMAGE_LOADER_DEBUG
dbg() << "Magic number is not valid for "
<< magic_number[0] << magic_number[1] << TContext::image_type;
#endif
return false;
}
template<typename TContext>
static bool read_white_space(TContext& context, Streamer& streamer)
{
bool exist = false;
u8 byte {};
while (streamer.read(byte)) {
if (byte == ' ' || byte == '\t' || byte == '\n' || byte == '\r') {
exist = true;
} else if (byte == '#') {
streamer.step_back();
read_comment(context, streamer);
} else {
streamer.step_back();
return exist;
}
}
return exist;
}
template<typename TContext>
static bool read_width(TContext& context, Streamer& streamer)
{
if (const bool result = read_number(streamer, &context.width);
!result || context.width == 0) {
return false;
}
context.state = TContext::Width;
return true;
}
template<typename TContext>
static bool read_height(TContext& context, Streamer& streamer)
{
if (const bool result = read_number(streamer, &context.height);
!result || context.height == 0) {
return false;
}
context.state = TContext::Height;
return true;
}
template<typename TContext>
static bool read_max_val(TContext& context, Streamer& streamer)
{
if (const bool result = read_number(streamer, &context.max_val);
!result || context.max_val == 0) {
return false;
}
if (context.max_val > 255) {
#ifdef PORTABLE_IMAGE_LOADER_DEBUG
dbg() << "We can't parse 2 byte color for " << TContext::image_type;
#endif
context.state = TContext::Error;
return false;
}
context.state = TContext::Maxval;
return true;
}
template<typename TContext>
static bool create_bitmap(TContext& context)
{
context.bitmap = Bitmap::create_purgeable(BitmapFormat::RGB32, { context.width, context.height });
if (!context.bitmap) {
context.state = TContext::State::Error;
return false;
}
return true;
}
template<typename TContext>
static void set_pixels(TContext& context, const AK::Vector<Gfx::Color>& color_data)
{
size_t index = 0;
for (size_t y = 0; y < context.height; ++y) {
for (size_t x = 0; x < context.width; ++x) {
context.bitmap->set_pixel(x, y, color_data.at(index));
index++;
}
}
}
template<typename TContext>
static bool decode(TContext& context)
{
if (context.state >= TContext::State::Decoded)
return true;
auto error_guard = ArmedScopeGuard([&] {
context.state = TContext::State::Error;
});
Streamer streamer(context.data, context.data_size);
if (!read_magic_number(context, streamer))
return false;
if (!read_white_space(context, streamer))
return false;
if (!read_width(context, streamer))
return false;
if (!read_white_space(context, streamer))
return false;
if (!read_height(context, streamer))
return false;
if (context.width > maximum_width_for_decoded_images || context.height > maximum_height_for_decoded_images) {
dbgln("This portable network image is too large for comfort: {}x{}", context.width, context.height);
return false;
}
if (!read_white_space(context, streamer))
return false;
if constexpr (requires { context.max_val; }) {
if (!read_max_val(context, streamer))
return false;
if (!read_white_space(context, streamer))
return false;
}
if (!read_image_data(context, streamer))
return false;
error_guard.disarm();
context.state = TContext::State::Decoded;
return true;
}
template<typename TContext>
static RefPtr<Gfx::Bitmap> load_impl(const u8* data, size_t data_size)
{
TContext context {};
context.data = data;
context.data_size = data_size;
if (!decode(context)) {
return nullptr;
}
return context.bitmap;
}
template<typename TContext>
static RefPtr<Gfx::Bitmap> load(const StringView& path)
{
auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error())
return nullptr;
auto bitmap = load_impl<TContext>((const u8*)file_or_error.value()->data(), file_or_error.value()->size());
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded {}: {}",
bitmap->size(),
TContext::image_type,
LexicalPath::canonicalized_path(path)));
return bitmap;
}
}

View file

@ -0,0 +1,181 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/StdLibExtras.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibGfx/Rect.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
namespace Gfx {
template<typename T>
void Rect<T>::intersect(const Rect<T>& other)
{
T l = max(left(), other.left());
T r = min(right(), other.right());
T t = max(top(), other.top());
T b = min(bottom(), other.bottom());
if (l > r || t > b) {
m_location = {};
m_size = {};
return;
}
m_location.set_x(l);
m_location.set_y(t);
m_size.set_width((r - l) + 1);
m_size.set_height((b - t) + 1);
}
template<typename T>
Rect<T> Rect<T>::united(const Rect<T>& other) const
{
if (is_null())
return other;
if (other.is_null())
return *this;
Rect<T> rect;
rect.set_left(min(left(), other.left()));
rect.set_top(min(top(), other.top()));
rect.set_right(max(right(), other.right()));
rect.set_bottom(max(bottom(), other.bottom()));
return rect;
}
template<typename T>
Vector<Rect<T>, 4> Rect<T>::shatter(const Rect<T>& hammer) const
{
Vector<Rect<T>, 4> pieces;
if (!intersects(hammer)) {
pieces.unchecked_append(*this);
return pieces;
}
Rect<T> top_shard {
x(),
y(),
width(),
hammer.y() - y()
};
Rect<T> bottom_shard {
x(),
hammer.y() + hammer.height(),
width(),
(y() + height()) - (hammer.y() + hammer.height())
};
Rect<T> left_shard {
x(),
max(hammer.y(), y()),
hammer.x() - x(),
min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
};
Rect<T> right_shard {
hammer.x() + hammer.width(),
max(hammer.y(), y()),
right() - hammer.right(),
min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
};
if (!top_shard.is_empty())
pieces.unchecked_append(top_shard);
if (!bottom_shard.is_empty())
pieces.unchecked_append(bottom_shard);
if (!left_shard.is_empty())
pieces.unchecked_append(left_shard);
if (!right_shard.is_empty())
pieces.unchecked_append(right_shard);
return pieces;
}
template<typename T>
void Rect<T>::align_within(const Rect<T>& other, TextAlignment alignment)
{
switch (alignment) {
case TextAlignment::Center:
center_within(other);
return;
case TextAlignment::TopLeft:
set_location(other.location());
return;
case TextAlignment::TopRight:
set_x(other.x() + other.width() - width());
set_y(other.y());
return;
case TextAlignment::CenterLeft:
set_x(other.x());
center_vertically_within(other);
return;
case TextAlignment::CenterRight:
set_x(other.x() + other.width() - width());
center_vertically_within(other);
return;
case TextAlignment::BottomRight:
set_x(other.x() + other.width() - width());
set_y(other.y() + other.height() - height());
return;
}
}
template<>
String IntRect::to_string() const
{
return String::format("[%d,%d %dx%d]", x(), y(), width(), height());
}
template<>
String FloatRect::to_string() const
{
return String::format("[%f,%f %fx%f]", x(), y(), width(), height());
}
}
namespace IPC {
bool encode(Encoder& encoder, const Gfx::IntRect& rect)
{
encoder << rect.location() << rect.size();
return true;
}
bool decode(Decoder& decoder, Gfx::IntRect& rect)
{
Gfx::IntPoint point;
Gfx::IntSize size;
if (!decoder.decode(point))
return false;
if (!decoder.decode(size))
return false;
rect = { point, size };
return true;
}
}
template class Gfx::Rect<int>;
template class Gfx::Rect<float>;

View file

@ -0,0 +1,471 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Format.h>
#include <LibGfx/Orientation.h>
#include <LibGfx/Point.h>
#include <LibGfx/Size.h>
#include <LibGfx/TextAlignment.h>
#include <math.h>
namespace Gfx {
template<typename T>
T abst(T value)
{
return value < 0 ? -value : value;
}
template<typename T>
class Rect {
public:
Rect() { }
Rect(T x, T y, T width, T height)
: m_location(x, y)
, m_size(width, height)
{
}
template<typename U>
Rect(U x, U y, U width, U height)
: m_location(x, y)
, m_size(width, height)
{
}
Rect(const Point<T>& location, const Size<T>& size)
: m_location(location)
, m_size(size)
{
}
template<typename U>
Rect(const Point<U>& location, const Size<U>& size)
: m_location(location)
, m_size(size)
{
}
template<typename U>
explicit Rect(const Rect<U>& other)
: m_location(other.location())
, m_size(other.size())
{
}
bool is_null() const
{
return width() == 0 && height() == 0;
}
bool is_empty() const
{
return width() <= 0 || height() <= 0;
}
void move_by(T dx, T dy)
{
m_location.move_by(dx, dy);
}
void move_by(const Point<T>& delta)
{
m_location.move_by(delta);
}
Point<T> center() const
{
return { x() + width() / 2, y() + height() / 2 };
}
void set_location(const Point<T>& location)
{
m_location = location;
}
void set_size(const Size<T>& size)
{
m_size = size;
}
void set_size(T width, T height)
{
m_size.set_width(width);
m_size.set_height(height);
}
void inflate(T w, T h)
{
set_x(x() - w / 2);
set_width(width() + w);
set_y(y() - h / 2);
set_height(height() + h);
}
void inflate(const Size<T>& size)
{
set_x(x() - size.width() / 2);
set_width(width() + size.width());
set_y(y() - size.height() / 2);
set_height(height() + size.height());
}
void shrink(T w, T h)
{
set_x(x() + w / 2);
set_width(width() - w);
set_y(y() + h / 2);
set_height(height() - h);
}
void shrink(const Size<T>& size)
{
set_x(x() + size.width() / 2);
set_width(width() - size.width());
set_y(y() + size.height() / 2);
set_height(height() - size.height());
}
Rect<T> shrunken(T w, T h) const
{
Rect<T> rect = *this;
rect.shrink(w, h);
return rect;
}
Rect<T> shrunken(const Size<T>& size) const
{
Rect<T> rect = *this;
rect.shrink(size);
return rect;
}
Rect<T> inflated(T w, T h) const
{
Rect<T> rect = *this;
rect.inflate(w, h);
return rect;
}
Rect<T> inflated(const Size<T>& size) const
{
Rect<T> rect = *this;
rect.inflate(size);
return rect;
}
Rect<T> translated(T dx, T dy) const
{
Rect<T> rect = *this;
rect.move_by(dx, dy);
return rect;
}
Rect<T> translated(const Point<T>& delta) const
{
Rect<T> rect = *this;
rect.move_by(delta);
return rect;
}
bool contains_vertically(T y) const
{
return y >= top() && y <= bottom();
}
bool contains_horizontally(T x) const
{
return x >= left() && x <= right();
}
bool contains(T x, T y) const
{
return x >= m_location.x() && x <= right() && y >= m_location.y() && y <= bottom();
}
bool contains(const Point<T>& point) const
{
return contains(point.x(), point.y());
}
bool contains(const Rect<T>& other) const
{
return left() <= other.left()
&& right() >= other.right()
&& top() <= other.top()
&& bottom() >= other.bottom();
}
template<typename Container>
bool contains(const Container& others) const
{
bool have_any = false;
for (const auto& other : others) {
if (!contains(other))
return false;
have_any = true;
}
return have_any;
}
int primary_offset_for_orientation(Orientation orientation) const { return m_location.primary_offset_for_orientation(orientation); }
void set_primary_offset_for_orientation(Orientation orientation, int value) { m_location.set_primary_offset_for_orientation(orientation, value); }
int secondary_offset_for_orientation(Orientation orientation) const { return m_location.secondary_offset_for_orientation(orientation); }
void set_secondary_offset_for_orientation(Orientation orientation, int value) { m_location.set_secondary_offset_for_orientation(orientation, value); }
int primary_size_for_orientation(Orientation orientation) const { return m_size.primary_size_for_orientation(orientation); }
int secondary_size_for_orientation(Orientation orientation) const { return m_size.secondary_size_for_orientation(orientation); }
void set_primary_size_for_orientation(Orientation orientation, int value) { m_size.set_primary_size_for_orientation(orientation, value); }
void set_secondary_size_for_orientation(Orientation orientation, int value) { m_size.set_secondary_size_for_orientation(orientation, value); }
T first_edge_for_orientation(Orientation orientation) const
{
if (orientation == Orientation::Vertical)
return top();
return left();
}
T last_edge_for_orientation(Orientation orientation) const
{
if (orientation == Orientation::Vertical)
return bottom();
return right();
}
T left() const { return x(); }
T right() const { return x() + width() - 1; }
T top() const { return y(); }
T bottom() const { return y() + height() - 1; }
void set_left(T left)
{
set_x(left);
}
void set_top(T top)
{
set_y(top);
}
void set_right(T right)
{
set_width(right - x() + 1);
}
void set_bottom(T bottom)
{
set_height(bottom - y() + 1);
}
void set_right_without_resize(T new_right)
{
int delta = new_right - right();
move_by(delta, 0);
}
void set_bottom_without_resize(T new_bottom)
{
int delta = new_bottom - bottom();
move_by(0, delta);
}
bool intersects_vertically(const Rect<T>& other) const
{
return top() <= other.bottom() && other.top() <= bottom();
}
bool intersects_horizontally(const Rect<T>& other) const
{
return left() <= other.right() && other.left() <= right();
}
bool intersects(const Rect<T>& other) const
{
return left() <= other.right()
&& other.left() <= right()
&& top() <= other.bottom()
&& other.top() <= bottom();
}
template<typename Container>
bool intersects(const Container& others) const
{
for (const auto& other : others) {
if (intersects(other))
return true;
}
return false;
}
template<typename Container, typename Function>
IterationDecision for_each_intersected(const Container& others, Function f) const
{
if (is_empty())
return IterationDecision::Continue;
for (const auto& other : others) {
auto intersected_rect = intersected(other);
if (!intersected_rect.is_empty()) {
IterationDecision decision = f(intersected_rect);
if (decision != IterationDecision::Continue)
return decision;
}
}
return IterationDecision::Continue;
}
T x() const { return location().x(); }
T y() const { return location().y(); }
T width() const { return m_size.width(); }
T height() const { return m_size.height(); }
void set_x(T x) { m_location.set_x(x); }
void set_y(T y) { m_location.set_y(y); }
void set_width(T width) { m_size.set_width(width); }
void set_height(T height) { m_size.set_height(height); }
const Point<T>& location() const { return m_location; }
const Size<T>& size() const { return m_size; }
Vector<Rect<T>, 4> shatter(const Rect<T>& hammer) const;
bool operator==(const Rect<T>& other) const
{
return m_location == other.m_location && m_size == other.m_size;
}
bool operator!=(const Rect<T>& other) const
{
return !(*this == other);
}
Rect<T> operator*(T factor) const { return { m_location * factor, m_size * factor }; }
Rect<T>& operator*=(T factor)
{
m_location *= factor;
m_size *= factor;
return *this;
}
void intersect(const Rect<T>&);
static Rect<T> from_two_points(const Point<T>& a, const Point<T>& b)
{
return { min(a.x(), b.x()), min(a.y(), b.y()), abst(a.x() - b.x()), abst(a.y() - b.y()) };
}
static Rect<T> intersection(const Rect<T>& a, const Rect<T>& b)
{
Rect<T> r = a;
r.intersect(b);
return r;
}
Rect<T> intersected(const Rect<T>& other) const
{
return intersection(*this, other);
}
Rect<T> united(const Rect<T>&) const;
Point<T> top_left() const { return { left(), top() }; }
Point<T> top_right() const { return { right(), top() }; }
Point<T> bottom_left() const { return { left(), bottom() }; }
Point<T> bottom_right() const { return { right(), bottom() }; }
void align_within(const Rect<T>&, TextAlignment);
void center_within(const Rect<T>& other)
{
center_horizontally_within(other);
center_vertically_within(other);
}
void center_horizontally_within(const Rect<T>& other)
{
set_x(other.center().x() - width() / 2);
}
void center_vertically_within(const Rect<T>& other)
{
set_y(other.center().y() - height() / 2);
}
template<typename U>
Rect<U> to() const
{
return Rect<U>(*this);
}
String to_string() const;
private:
Point<T> m_location;
Size<T> m_size;
};
template<typename T>
const LogStream& operator<<(const LogStream& stream, const Rect<T>& rect)
{
return stream << rect.to_string();
}
using IntRect = Rect<int>;
using FloatRect = Rect<float>;
ALWAYS_INLINE IntRect enclosing_int_rect(const FloatRect& float_rect)
{
return {
(int)float_rect.x(),
(int)float_rect.y(),
(int)ceilf(float_rect.width()),
(int)ceilf(float_rect.height()),
};
}
}
namespace AK {
template<typename T>
struct Formatter<Gfx::Rect<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Rect<T>& value)
{
Formatter<StringView>::format(builder, value.to_string());
}
};
}
namespace IPC {
bool decode(Decoder&, Gfx::IntRect&);
bool encode(Encoder&, const Gfx::IntRect&);
}

View file

@ -0,0 +1,76 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/SharedBuffer.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/ShareableBitmap.h>
#include <LibGfx/Size.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
namespace Gfx {
ShareableBitmap::ShareableBitmap(const Bitmap& bitmap)
: m_bitmap(bitmap.to_bitmap_backed_by_shared_buffer())
{
}
}
namespace IPC {
bool encode(Encoder& encoder, const Gfx::ShareableBitmap& shareable_bitmap)
{
encoder << shareable_bitmap.shbuf_id();
encoder << shareable_bitmap.width();
encoder << shareable_bitmap.height();
return true;
}
bool decode(Decoder& decoder, Gfx::ShareableBitmap& shareable_bitmap)
{
i32 shbuf_id = 0;
Gfx::IntSize size;
if (!decoder.decode(shbuf_id))
return false;
if (!decoder.decode(size))
return false;
if (shbuf_id == -1)
return true;
dbgln("Decoding a ShareableBitmap with shbuf_id={}, size={}", shbuf_id, size);
auto shared_buffer = SharedBuffer::create_from_shbuf_id(shbuf_id);
if (!shared_buffer)
return false;
auto bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, shared_buffer.release_nonnull(), size);
shareable_bitmap = bitmap->to_shareable_bitmap();
return true;
}
}

View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/RefPtr.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Size.h>
namespace Gfx {
class ShareableBitmap {
public:
ShareableBitmap() { }
explicit ShareableBitmap(const Gfx::Bitmap&);
bool is_valid() const { return m_bitmap; }
i32 shbuf_id() const { return m_bitmap ? m_bitmap->shbuf_id() : -1; }
const Bitmap* bitmap() const { return m_bitmap; }
Bitmap* bitmap() { return m_bitmap; }
IntSize size() const { return m_bitmap ? m_bitmap->size() : IntSize(); }
IntRect rect() const { return m_bitmap ? m_bitmap->rect() : IntRect(); }
int width() const { return size().width(); }
int height() const { return size().height(); }
private:
RefPtr<Bitmap> m_bitmap;
};
}
namespace IPC {
bool encode(Encoder&, const Gfx::ShareableBitmap&);
bool decode(Decoder&, Gfx::ShareableBitmap&);
}

View file

@ -0,0 +1,71 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/String.h>
#include <LibGfx/Size.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
namespace Gfx {
template<>
String IntSize::to_string() const
{
return String::formatted("[{}x{}]", m_width, m_height);
}
template<>
String FloatSize::to_string() const
{
return String::formatted("[{}x{}]", m_width, m_height);
}
}
namespace IPC {
bool encode(Encoder& encoder, const Gfx::IntSize& size)
{
encoder << size.width() << size.height();
return true;
}
bool decode(Decoder& decoder, Gfx::IntSize& size)
{
int width = 0;
int height = 0;
if (!decoder.decode(width))
return false;
if (!decoder.decode(height))
return false;
size = { width, height };
return true;
}
}
template class Gfx::Size<int>;
template class Gfx::Size<float>;

View file

@ -0,0 +1,179 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Format.h>
#include <LibGfx/Orientation.h>
#include <LibIPC/Forward.h>
namespace Gfx {
template<typename T>
class Size {
public:
Size() { }
Size(T w, T h)
: m_width(w)
, m_height(h)
{
}
template<typename U>
Size(U width, U height)
: m_width(width)
, m_height(height)
{
}
template<typename U>
explicit Size(const Size<U>& other)
: m_width(other.width())
, m_height(other.height())
{
}
bool is_null() const { return !m_width && !m_height; }
bool is_empty() const { return m_width <= 0 || m_height <= 0; }
T width() const { return m_width; }
T height() const { return m_height; }
T area() const { return width() * height(); }
void set_width(T w) { m_width = w; }
void set_height(T h) { m_height = h; }
template<typename U>
bool contains(const Size<U>& other) const
{
return other.m_width <= m_width && other.m_height <= m_height;
}
bool operator==(const Size<T>& other) const
{
return m_width == other.m_width && m_height == other.m_height;
}
bool operator!=(const Size<T>& other) const
{
return !(*this == other);
}
Size<T>& operator-=(const Size<T>& other)
{
m_width -= other.m_width;
m_height -= other.m_height;
return *this;
}
Size<T>& operator+=(const Size<T>& other)
{
m_width += other.m_width;
m_height += other.m_height;
return *this;
}
Size<T> operator*(T factor) const { return { m_width * factor, m_height * factor }; }
Size<T>& operator*=(T factor)
{
m_width *= factor;
m_height *= factor;
return *this;
}
T primary_size_for_orientation(Orientation orientation) const
{
return orientation == Orientation::Vertical ? height() : width();
}
void set_primary_size_for_orientation(Orientation orientation, T value)
{
if (orientation == Orientation::Vertical) {
set_height(value);
} else {
set_width(value);
}
}
T secondary_size_for_orientation(Orientation orientation) const
{
return orientation == Orientation::Vertical ? width() : height();
}
void set_secondary_size_for_orientation(Orientation orientation, T value)
{
if (orientation == Orientation::Vertical) {
set_width(value);
} else {
set_height(value);
}
}
template<typename U>
Size<U> to_type() const
{
return Size<U>(*this);
}
String to_string() const;
private:
T m_width { 0 };
T m_height { 0 };
};
template<typename T>
const LogStream& operator<<(const LogStream& stream, const Gfx::Size<T>& size)
{
return stream << size.to_string();
}
using IntSize = Size<int>;
using FloatSize = Size<float>;
}
namespace AK {
template<typename T>
struct Formatter<Gfx::Size<T>> : Formatter<StringView> {
void format(FormatBuilder& builder, const Gfx::Size<T>& value)
{
Formatter<StringView>::format(builder, value.to_string());
}
};
}
namespace IPC {
bool encode(Encoder&, const Gfx::IntSize&);
bool decode(Decoder&, Gfx::IntSize&);
}

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
namespace Gfx {
enum class StandardCursor {
None = 0,
Hidden,
Arrow,
Crosshair,
IBeam,
ResizeHorizontal,
ResizeVertical,
ResizeDiagonalTLBR,
ResizeDiagonalBLTR,
ResizeColumn,
ResizeRow,
Hand,
Help,
Drag,
Move,
Wait,
__Count,
};
}

View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2020, Hüseyin ASLITÜRK <asliturk@hotmail.com>, the SerenityOS developers
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Array.h>
#include <AK/Endian.h>
#include <AK/Types.h>
#include <string.h>
namespace Gfx {
class Streamer {
public:
constexpr Streamer(const u8* data, size_t size)
: m_data_ptr(data)
, m_size_remaining(size)
{
}
template<typename T>
constexpr bool read(T& value)
{
AK::Array<u8, sizeof(T)> network_buffer {};
auto network_value = new (network_buffer.data()) AK::NetworkOrdered<T> {};
auto res = read_bytes(network_buffer.data(), sizeof(T));
value = T(*network_value);
return res;
}
constexpr bool read_bytes(u8* buffer, size_t count)
{
if (m_size_remaining < count) {
return false;
}
memcpy(buffer, m_data_ptr, count);
m_data_ptr += count;
m_size_remaining -= count;
return true;
}
constexpr bool at_end() const { return !m_size_remaining; }
constexpr void step_back()
{
m_data_ptr -= 1;
m_size_remaining += 1;
}
private:
const u8* m_data_ptr { nullptr };
size_t m_size_remaining { 0 };
};
}

View file

@ -0,0 +1,86 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGfx/Bitmap.h>
#include <LibGfx/ClassicStylePainter.h>
#include <LibGfx/Painter.h>
#include <LibGfx/Palette.h>
#include <LibGfx/StylePainter.h>
namespace Gfx {
BaseStylePainter& StylePainter::current()
{
static ClassicStylePainter style;
return style;
}
void StylePainter::paint_tab_button(Painter& painter, const IntRect& rect, const Palette& palette, bool active, bool hovered, bool enabled, bool top)
{
current().paint_tab_button(painter, rect, palette, active, hovered, enabled, top);
}
void StylePainter::paint_button(Painter& painter, const IntRect& rect, const Palette& palette, ButtonStyle button_style, bool pressed, bool hovered, bool checked, bool enabled, bool focused)
{
current().paint_button(painter, rect, palette, button_style, pressed, hovered, checked, enabled, focused);
}
void StylePainter::paint_surface(Painter& painter, const IntRect& rect, const Palette& palette, bool paint_vertical_lines, bool paint_top_line)
{
current().paint_surface(painter, rect, palette, paint_vertical_lines, paint_top_line);
}
void StylePainter::paint_frame(Painter& painter, const IntRect& rect, const Palette& palette, FrameShape shape, FrameShadow shadow, int thickness, bool skip_vertical_lines)
{
current().paint_frame(painter, rect, palette, shape, shadow, thickness, skip_vertical_lines);
}
void StylePainter::paint_window_frame(Painter& painter, const IntRect& rect, const Palette& palette)
{
current().paint_window_frame(painter, rect, palette);
}
void StylePainter::paint_progress_bar(Painter& painter, const IntRect& rect, const Palette& palette, int min, int max, int value, const StringView& text)
{
current().paint_progress_bar(painter, rect, palette, min, max, value, text);
}
void StylePainter::paint_radio_button(Painter& painter, const IntRect& rect, const Palette& palette, bool is_checked, bool is_being_pressed)
{
current().paint_radio_button(painter, rect, palette, is_checked, is_being_pressed);
}
void StylePainter::paint_check_box(Painter& painter, const IntRect& rect, const Palette& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
{
current().paint_check_box(painter, rect, palette, is_enabled, is_checked, is_being_pressed);
}
void StylePainter::paint_transparency_grid(Painter& painter, const IntRect& rect, const Palette& palette)
{
current().paint_transparency_grid(painter, rect, palette);
}
}

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <LibGfx/Forward.h>
namespace Gfx {
enum class ButtonStyle {
Normal,
CoolBar
};
enum class FrameShadow {
Plain,
Raised,
Sunken
};
enum class FrameShape {
NoFrame,
Box,
Container,
Panel,
VerticalLine,
HorizontalLine
};
// FIXME: should this be in its own header?
class BaseStylePainter {
public:
virtual ~BaseStylePainter() { }
virtual void paint_button(Painter&, const IntRect&, const Palette&, ButtonStyle, bool pressed, bool hovered = false, bool checked = false, bool enabled = true, bool focused = false) = 0;
virtual void paint_tab_button(Painter&, const IntRect&, const Palette&, bool active, bool hovered, bool enabled, bool top) = 0;
virtual void paint_surface(Painter&, const IntRect&, const Palette&, bool paint_vertical_lines = true, bool paint_top_line = true) = 0;
virtual void paint_frame(Painter&, const IntRect&, const Palette&, FrameShape, FrameShadow, int thickness, bool skip_vertical_lines = false) = 0;
virtual void paint_window_frame(Painter&, const IntRect&, const Palette&) = 0;
virtual void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text) = 0;
virtual void paint_radio_button(Painter&, const IntRect&, const Palette&, bool is_checked, bool is_being_pressed) = 0;
virtual void paint_check_box(Painter&, const IntRect&, const Palette&, bool is_enabled, bool is_checked, bool is_being_pressed) = 0;
virtual void paint_transparency_grid(Painter&, const IntRect&, const Palette&) = 0;
protected:
BaseStylePainter() { }
};
class StylePainter {
public:
static BaseStylePainter& current();
// FIXME: These are here for API compatibility, we should probably remove them and move BaseStylePainter into here
static void paint_button(Painter&, const IntRect&, const Palette&, ButtonStyle, bool pressed, bool hovered = false, bool checked = false, bool enabled = true, bool focused = false);
static void paint_tab_button(Painter&, const IntRect&, const Palette&, bool active, bool hovered, bool enabled, bool top);
static void paint_surface(Painter&, const IntRect&, const Palette&, bool paint_vertical_lines = true, bool paint_top_line = true);
static void paint_frame(Painter&, const IntRect&, const Palette&, FrameShape, FrameShadow, int thickness, bool skip_vertical_lines = false);
static void paint_window_frame(Painter&, const IntRect&, const Palette&);
static void paint_progress_bar(Painter&, const IntRect&, const Palette&, int min, int max, int value, const StringView& text);
static void paint_radio_button(Painter&, const IntRect&, const Palette&, bool is_checked, bool is_being_pressed);
static void paint_check_box(Painter&, const IntRect&, const Palette&, bool is_enabled, bool is_checked, bool is_being_pressed);
static void paint_transparency_grid(Painter&, const IntRect&, const Palette&);
};
}

View file

@ -0,0 +1,130 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/SharedBuffer.h>
#include <LibCore/ConfigFile.h>
#include <LibGfx/SystemTheme.h>
#include <string.h>
namespace Gfx {
static SystemTheme dummy_theme;
static const SystemTheme* theme_page = &dummy_theme;
static RefPtr<SharedBuffer> theme_buffer;
const SystemTheme& current_system_theme()
{
ASSERT(theme_page);
return *theme_page;
}
int current_system_theme_buffer_id()
{
ASSERT(theme_buffer);
return theme_buffer->shbuf_id();
}
void set_system_theme(SharedBuffer& buffer)
{
theme_buffer = buffer;
theme_page = theme_buffer->data<SystemTheme>();
}
RefPtr<SharedBuffer> load_system_theme(const String& path)
{
auto file = Core::ConfigFile::open(path);
auto buffer = SharedBuffer::create_with_size(sizeof(SystemTheme));
auto* data = buffer->data<SystemTheme>();
auto get_color = [&](auto& name) {
auto color_string = file->read_entry("Colors", name);
auto color = Color::from_string(color_string);
if (!color.has_value())
return Color(Color::Black);
return color.value();
};
auto get_metric = [&](auto& name, auto role) {
int metric = file->read_num_entry("Metrics", name, -1);
if (metric == -1) {
switch (role) {
case (int)MetricRole::TitleHeight:
return 19;
case (int)MetricRole::TitleButtonHeight:
return 15;
case (int)MetricRole::TitleButtonWidth:
return 15;
default:
dbgln("Metric {} has no fallback value!", name);
return 16;
}
}
return metric;
};
auto get_path = [&](auto& name, auto role) {
auto path = file->read_entry("Paths", name);
if (path.is_empty()) {
switch (role) {
case (int)PathRole::TitleButtonIcons:
return "/res/icons/16x16/";
default:
return "/res/";
}
}
return &path[0];
};
#undef __ENUMERATE_COLOR_ROLE
#define __ENUMERATE_COLOR_ROLE(role) \
data->color[(int)ColorRole::role] = get_color(#role).value();
ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
#undef __ENUMERATE_COLOR_ROLE
#define DO_METRIC(x) \
data->metric[(int)MetricRole::x] = get_metric(#x, (int)MetricRole::x)
DO_METRIC(TitleHeight);
DO_METRIC(TitleButtonWidth);
DO_METRIC(TitleButtonHeight);
#define DO_PATH(x) \
do { \
auto path = get_path(#x, (int)PathRole::x); \
memcpy(data->path[(int)PathRole::x], path, min(strlen(path) + 1, sizeof(data->path[(int)PathRole::x]))); \
data->path[(int)PathRole::x][sizeof(data->path[(int)PathRole::x]) - 1] = '\0'; \
} while (0)
DO_PATH(TitleButtonIcons);
buffer->seal();
buffer->share_globally();
return buffer;
}
}

View file

@ -0,0 +1,162 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <AK/String.h>
#include <AK/Types.h>
#include <LibGfx/Color.h>
namespace Gfx {
#define ENUMERATE_COLOR_ROLES(C) \
C(ActiveLink) \
C(ActiveWindowBorder1) \
C(ActiveWindowBorder2) \
C(ActiveWindowTitle) \
C(ActiveWindowTitleShadow) \
C(ActiveWindowTitleStripes) \
C(Base) \
C(BaseText) \
C(Button) \
C(ButtonText) \
C(DesktopBackground) \
C(FocusOutline) \
C(HighlightWindowBorder1) \
C(HighlightWindowBorder2) \
C(HighlightWindowTitle) \
C(HighlightWindowTitleShadow) \
C(HighlightWindowTitleStripes) \
C(HighlightSearching) \
C(HighlightSearchingText) \
C(HoverHighlight) \
C(InactiveSelection) \
C(InactiveSelectionText) \
C(InactiveWindowBorder1) \
C(InactiveWindowBorder2) \
C(InactiveWindowTitle) \
C(InactiveWindowTitleShadow) \
C(InactiveWindowTitleStripes) \
C(Link) \
C(MenuBase) \
C(MenuBaseText) \
C(MenuSelection) \
C(MenuSelectionText) \
C(MenuStripe) \
C(MovingWindowBorder1) \
C(MovingWindowBorder2) \
C(MovingWindowTitle) \
C(MovingWindowTitleShadow) \
C(MovingWindowTitleStripes) \
C(PlaceholderText) \
C(RubberBandBorder) \
C(RubberBandFill) \
C(Ruler) \
C(RulerActiveText) \
C(RulerBorder) \
C(RulerInactiveText) \
C(Selection) \
C(SelectionText) \
C(SyntaxComment) \
C(SyntaxControlKeyword) \
C(SyntaxIdentifier) \
C(SyntaxKeyword) \
C(SyntaxNumber) \
C(SyntaxOperator) \
C(SyntaxPreprocessorStatement) \
C(SyntaxPreprocessorValue) \
C(SyntaxPunctuation) \
C(SyntaxString) \
C(SyntaxType) \
C(TextCursor) \
C(ThreedHighlight) \
C(ThreedShadow1) \
C(ThreedShadow2) \
C(Tooltip) \
C(TooltipText) \
C(VisitedLink) \
C(Window) \
C(WindowText)
enum class ColorRole {
NoRole,
#undef __ENUMERATE_COLOR_ROLE
#define __ENUMERATE_COLOR_ROLE(role) role,
ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
#undef __ENUMERATE_COLOR_ROLE
__Count,
Background = Window,
DisabledText = ThreedShadow1,
};
inline const char* to_string(ColorRole role)
{
switch (role) {
case ColorRole::NoRole:
return "NoRole";
#undef __ENUMERATE_COLOR_ROLE
#define __ENUMERATE_COLOR_ROLE(role) \
case ColorRole::role: \
return #role;
ENUMERATE_COLOR_ROLES(__ENUMERATE_COLOR_ROLE)
#undef __ENUMERATE_COLOR_ROLE
default:
ASSERT_NOT_REACHED();
}
}
enum class MetricRole {
NoRole,
TitleHeight,
TitleButtonWidth,
TitleButtonHeight,
__Count,
};
enum class PathRole {
NoRole,
TitleButtonIcons,
__Count,
};
struct SystemTheme {
RGBA32 color[(int)ColorRole::__Count];
int metric[(int)MetricRole::__Count];
char path[(int)PathRole::__Count][256]; // TODO: PATH_MAX?
};
const SystemTheme& current_system_theme();
int current_system_theme_buffer_id();
void set_system_theme(SharedBuffer&);
RefPtr<SharedBuffer> load_system_theme(const String& path);
}
using Gfx::ColorRole;

View file

@ -0,0 +1,92 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Optional.h>
#include <AK/StringView.h>
namespace Gfx {
#define GFX_ENUMERATE_TEXT_ALIGNMENTS(M) \
M(TopLeft) \
M(CenterLeft) \
M(Center) \
M(CenterRight) \
M(TopRight) \
M(BottomRight)
enum class TextAlignment {
#define __ENUMERATE(x) x,
GFX_ENUMERATE_TEXT_ALIGNMENTS(__ENUMERATE)
#undef __ENUMERATE
};
inline bool is_right_text_alignment(TextAlignment alignment)
{
switch (alignment) {
case TextAlignment::CenterRight:
case TextAlignment::TopRight:
case TextAlignment::BottomRight:
return true;
default:
return false;
}
}
inline bool is_vertically_centered_text_alignment(TextAlignment alignment)
{
switch (alignment) {
case TextAlignment::CenterLeft:
case TextAlignment::CenterRight:
case TextAlignment::Center:
return true;
default:
return false;
}
}
inline Optional<TextAlignment> text_alignment_from_string(const StringView& string)
{
#define __ENUMERATE(x) \
if (string == #x) \
return TextAlignment::x;
GFX_ENUMERATE_TEXT_ALIGNMENTS(__ENUMERATE)
#undef __ENUMERATE
return {};
}
inline const char* to_string(TextAlignment text_alignment)
{
#define __ENUMERATE(x) \
if (text_alignment == TextAlignment::x) \
return #x;
GFX_ENUMERATE_TEXT_ALIGNMENTS(__ENUMERATE)
#undef __ENUMERATE
return {};
}
}

View file

@ -0,0 +1,41 @@
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Optional.h>
#include <LibGfx/Color.h>
namespace Gfx {
struct TextAttributes {
Color color;
Optional<Color> background_color;
bool underline { false };
bool bold { false };
};
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
namespace Gfx {
enum class TextElision {
None,
Right,
};
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/String.h>
#include <LibGfx/Triangle.h>
namespace Gfx {
String Triangle::to_string() const
{
return String::formatted("({},{},{})", m_a, m_b, m_c);
}
const LogStream& operator<<(const LogStream& stream, const Triangle& value)
{
return stream << value.to_string();
}
}

View file

@ -0,0 +1,81 @@
/*
* Copyright (c) 2020, Shannon Booth <shannon.ml.booth@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <LibGfx/Point.h>
namespace Gfx {
class Triangle {
public:
Triangle(IntPoint a, IntPoint b, IntPoint c)
: m_a(a)
, m_b(b)
, m_c(c)
{
m_det = (m_b.x() - m_a.x()) * (m_c.y() - m_a.y()) - (m_b.y() - m_a.y()) * (m_c.x() - m_a.x());
}
IntPoint a() const { return m_a; }
IntPoint b() const { return m_b; }
IntPoint c() const { return m_c; }
bool contains(IntPoint p) const
{
int x = p.x();
int y = p.y();
int ax = m_a.x();
int bx = m_b.x();
int cx = m_c.x();
int ay = m_a.y();
int by = m_b.y();
int cy = m_c.y();
if (m_det * ((bx - ax) * (y - ay) - (by - ay) * (x - ax)) <= 0)
return false;
if (m_det * ((cx - bx) * (y - by) - (cy - by) * (x - bx)) <= 0)
return false;
if (m_det * ((ax - cx) * (y - cy) - (ay - cy) * (x - cx)) <= 0)
return false;
return true;
}
String to_string() const;
private:
int m_det;
IntPoint m_a;
IntPoint m_b;
IntPoint m_c;
};
const LogStream& operator<<(const LogStream&, const Triangle&);
}

View file

@ -0,0 +1,133 @@
/*
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@gmx.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <math.h>
namespace Gfx {
template<typename T>
class Vector3 {
public:
Vector3() = default;
Vector3(T x, T y, T z)
: m_x(x)
, m_y(y)
, m_z(z)
{
}
T x() const { return m_x; }
T y() const { return m_y; }
T z() const { return m_z; }
void set_x(T value) { m_x = value; }
void set_y(T value) { m_y = value; }
void set_z(T value) { m_z = value; }
Vector3 operator+(const Vector3& other) const
{
return Vector3(m_x + other.m_x, m_y + other.m_y, m_z + other.m_z);
}
Vector3 operator-(const Vector3& other) const
{
return Vector3(m_x - other.m_x, m_y - other.m_y, m_z - other.m_z);
}
Vector3 operator*(T f) const
{
return Vector3(m_x * f, m_y * f, m_z * f);
}
Vector3 operator/(T f) const
{
return Vector3(m_x / f, m_y / f, m_z / f);
}
T dot(const Vector3& other) const
{
return m_x * other.m_x + m_y * other.m_y + m_z * other.m_z;
}
Vector3 cross(const Vector3& other) const
{
return Vector3(
m_y * other.m_z - m_z * other.m_y,
m_z * other.m_x - m_x * other.m_z,
m_x * other.m_y - m_y * other.m_x);
}
Vector3 normalized() const
{
T inv_length = 1 / length();
return *this * inv_length;
}
Vector3 clamped(T m, T x) const
{
Vector3 copy { *this };
copy.clamp(m, x);
return copy;
}
void clamp(T min_value, T max_value)
{
m_x = max(min_value, m_x);
m_y = max(min_value, m_y);
m_z = max(min_value, m_z);
m_x = min(max_value, m_x);
m_y = min(max_value, m_y);
m_z = min(max_value, m_z);
}
void normalize()
{
T inv_length = 1 / length();
m_x *= inv_length;
m_y *= inv_length;
m_z *= inv_length;
}
T length() const
{
return sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
}
private:
T m_x;
T m_y;
T m_z;
};
typedef Vector3<float> FloatVector3;
typedef Vector3<double> DoubleVector3;
}
using Gfx::DoubleVector3;
using Gfx::FloatVector3;
using Gfx::Vector3;

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGfx/ClassicWindowTheme.h>
#include <LibGfx/WindowTheme.h>
namespace Gfx {
WindowTheme& WindowTheme::current()
{
static ClassicWindowTheme theme;
return theme;
}
WindowTheme::~WindowTheme()
{
}
}

View file

@ -0,0 +1,69 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/Forward.h>
#include <LibGfx/Forward.h>
namespace Gfx {
class WindowTheme {
public:
enum class WindowType {
Normal,
Notification,
Other,
};
enum class WindowState {
Active,
Inactive,
Highlighted,
Moving,
};
virtual ~WindowTheme();
static WindowTheme& current();
virtual void paint_normal_frame(Painter&, WindowState, const IntRect& window_rect, const StringView& title, const Bitmap& icon, const Palette&, const IntRect& leftmost_button_rect) const = 0;
virtual void paint_notification_frame(Painter&, const IntRect& window_rect, const Palette&, const IntRect& close_button_rect) const = 0;
virtual int title_bar_height(const Palette&) const = 0;
virtual IntRect title_bar_rect(WindowType, const IntRect& window_rect, const Palette&) const = 0;
virtual IntRect title_bar_icon_rect(WindowType, const IntRect& window_rect, const Palette&) const = 0;
virtual IntRect title_bar_text_rect(WindowType, const IntRect& window_rect, const Palette&) const = 0;
virtual IntRect frame_rect_for_window(WindowType, const IntRect& window_rect, const Palette&) const = 0;
virtual Vector<IntRect> layout_buttons(WindowType, const IntRect& window_rect, const Palette&, size_t buttons) const = 0;
protected:
WindowTheme() { }
};
}