LibTTF: Fix code after rebase

This commit is contained in:
Stephan Unverwerth 2020-10-21 00:03:07 +02:00 committed by Andreas Kling
parent 3b31f069f0
commit a060b6cefd
Notes: sideshowbarker 2024-07-19 00:20:33 +09:00
7 changed files with 120 additions and 74 deletions

View file

@ -38,15 +38,6 @@ public:
: m_values { 1, 0, 0, 1, 0, 0 }
{
}
AffineTransform(float a, float b, float c, float d, float e, float f)
{
m_values[0] = a;
m_values[1] = b;
m_values[2] = c;
m_values[3] = d;
m_values[4] = e;
m_values[5] = f;
}
AffineTransform(float a, float b, float c, float d, float e, float f)
: m_values { a, b, c, d, e, f }
@ -81,8 +72,6 @@ public:
AffineTransform& rotate_radians(float);
AffineTransform& multiply(const AffineTransform&);
AffineTransform operator*(const AffineTransform&) const;
private:
float m_values[6] { 0 };
};

View file

@ -33,4 +33,4 @@ set(SOURCES
)
serenity_lib(LibGfx gfx)
target_link_libraries(LibGfx LibM LibCore LibTTF)
target_link_libraries(LibGfx LibM LibCore)

View file

@ -1282,22 +1282,45 @@ void Painter::draw_line(const IntPoint& p1, const IntPoint& p2, Color color, int
}
}
static void split_quadratic_bezier_curve(const FloatPoint& original_control, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>& callback)
{
auto po1_midpoint = original_control + p1;
po1_midpoint /= 2;
auto po2_midpoint = original_control + p2;
po2_midpoint /= 2;
auto new_segment = po1_midpoint + po2_midpoint;
new_segment /= 2;
Painter::for_each_line_segment_on_bezier_curve(po1_midpoint, p1, new_segment, callback);
Painter::for_each_line_segment_on_bezier_curve(po2_midpoint, new_segment, p2, callback);
}
static bool can_approximate_bezier_curve(const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& control)
{
constexpr static int tolerance = 15;
auto p1x = 3 * control.x() - 2 * p1.x() - p2.x();
auto p1y = 3 * control.y() - 2 * p1.y() - p2.y();
auto p2x = 3 * control.x() - 2 * p2.x() - p1.x();
auto p2y = 3 * control.y() - 2 * p2.y() - p1.y();
p1x = p1x * p1x;
p1y = p1y * p1y;
p2x = p2x * p2x;
p2y = p2y * p2y;
return max(p1x, p2x) + max(p1y, p2y) <= tolerance;
}
void Painter::for_each_line_segment_on_bezier_curve(const FloatPoint& control_point, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>& callback)
{
float arbitrary = 15.0;
auto mid_point = FloatPoint::interpolate(p1, p2, 0.5);
float squared_distance = FloatPoint::squared_distance(control_point, mid_point);
size_t num_sections = 1 + floorf(sqrtf(arbitrary * squared_distance));
float delta = 1.0 / num_sections;
float t = 0.0;
FloatPoint p_cur = p1;
for (size_t i = 0; i < num_sections - 1; i++) {
t += delta;
FloatPoint pn = FloatPoint::interpolate(FloatPoint::interpolate(p1, control_point, t), FloatPoint::interpolate(control_point, p2, t), t);
callback(p_cur, pn);
p_cur = pn;
if (can_approximate_bezier_curve(p1, p2, control_point)) {
callback(p1, p2);
} else {
split_quadratic_bezier_curve(control_point, p1, p2, callback);
}
callback(p_cur, p2);
}
void Painter::for_each_line_segment_on_bezier_curve(const FloatPoint& control_point, const FloatPoint& p1, const FloatPoint& p2, Function<void(const FloatPoint&, const FloatPoint&)>&& callback)

View file

@ -36,33 +36,48 @@ extern i16 be_i16(const u8* ptr);
Cmap::Subtable::Platform Cmap::Subtable::platform_id() const
{
switch (m_raw_platform_id) {
case 0: return Platform::Unicode;
case 1: return Platform::Macintosh;
case 3: return Platform::Windows;
case 4: return Platform::Custom;
default: ASSERT_NOT_REACHED();
case 0:
return Platform::Unicode;
case 1:
return Platform::Macintosh;
case 3:
return Platform::Windows;
case 4:
return Platform::Custom;
default:
ASSERT_NOT_REACHED();
}
}
Cmap::Subtable::Format Cmap::Subtable::format() const
{
switch (be_u16(m_slice.offset_pointer(0))) {
case 0: return Format::ByteEncoding;
case 2: return Format::HighByte;
case 4: return Format::SegmentToDelta;
case 6: return Format::TrimmedTable;
case 8: return Format::Mixed16And32;
case 10: return Format::TrimmedArray;
case 12: return Format::SegmentedCoverage;
case 13: return Format::ManyToOneRange;
case 14: return Format::UnicodeVariationSequences;
default: ASSERT_NOT_REACHED();
case 0:
return Format::ByteEncoding;
case 2:
return Format::HighByte;
case 4:
return Format::SegmentToDelta;
case 6:
return Format::TrimmedTable;
case 8:
return Format::Mixed16And32;
case 10:
return Format::TrimmedArray;
case 12:
return Format::SegmentedCoverage;
case 13:
return Format::ManyToOneRange;
case 14:
return Format::UnicodeVariationSequences;
default:
ASSERT_NOT_REACHED();
}
}
u32 Cmap::num_subtables() const
{
return be_u16(m_slice.offset_pointer((u32) Offsets::NumTables));
return be_u16(m_slice.offset_pointer((u32)Offsets::NumTables));
}
Optional<Cmap::Subtable> Cmap::subtable(u32 index) const
@ -70,12 +85,13 @@ Optional<Cmap::Subtable> Cmap::subtable(u32 index) const
if (index >= num_subtables()) {
return {};
}
u32 record_offset = (u32) Sizes::TableHeader + index * (u32) Sizes::EncodingRecord;
u32 record_offset = (u32)Sizes::TableHeader + index * (u32)Sizes::EncodingRecord;
u16 platform_id = be_u16(m_slice.offset_pointer(record_offset));
u16 encoding_id = be_u16(m_slice.offset_pointer(record_offset + (u32) Offsets::EncodingRecord_EncodingID));
u32 subtable_offset = be_u32(m_slice.offset_pointer(record_offset + (u32) Offsets::EncodingRecord_Offset));
u16 encoding_id = be_u16(m_slice.offset_pointer(record_offset + (u32)Offsets::EncodingRecord_EncodingID));
u32 subtable_offset = be_u32(m_slice.offset_pointer(record_offset + (u32)Offsets::EncodingRecord_Offset));
ASSERT(subtable_offset < m_slice.size());
auto subtable_slice = ByteBuffer::wrap(m_slice.offset_pointer(subtable_offset), m_slice.size() - subtable_offset);
// HACK: added const_cast because of new ByteBuffer::wrap behavior, should probably find another workaround
auto subtable_slice = ByteBuffer::wrap(const_cast<u8*>(m_slice.offset_pointer(subtable_offset)), m_slice.size() - subtable_offset);
return Subtable(subtable_slice, platform_id, encoding_id);
}
@ -94,25 +110,25 @@ u32 Cmap::Subtable::glyph_id_for_codepoint(u32 codepoint) const
u32 Cmap::Subtable::glyph_id_for_codepoint_table_4(u32 codepoint) const
{
u32 segcount_x2 = be_u16(m_slice.offset_pointer((u32) Table4Offsets::SegCountX2));
if (m_slice.size() < segcount_x2 * (u32) Table4Sizes::NonConstMultiplier + (u32) Table4Sizes::Constant) {
u32 segcount_x2 = be_u16(m_slice.offset_pointer((u32)Table4Offsets::SegCountX2));
if (m_slice.size() < segcount_x2 * (u32)Table4Sizes::NonConstMultiplier + (u32)Table4Sizes::Constant) {
return 0;
}
for (u32 offset = 0; offset < segcount_x2; offset += 2) {
u32 end_codepoint = be_u16(m_slice.offset_pointer((u32) Table4Offsets::EndConstBase + offset));
u32 end_codepoint = be_u16(m_slice.offset_pointer((u32)Table4Offsets::EndConstBase + offset));
if (codepoint > end_codepoint) {
continue;
}
u32 start_codepoint = be_u16(m_slice.offset_pointer((u32) Table4Offsets::StartConstBase + segcount_x2 + offset));
u32 start_codepoint = be_u16(m_slice.offset_pointer((u32)Table4Offsets::StartConstBase + segcount_x2 + offset));
if (codepoint < start_codepoint) {
break;
}
u32 delta = be_u16(m_slice.offset_pointer((u32) Table4Offsets::DeltaConstBase + segcount_x2 * 2 + offset));
u32 range = be_u16(m_slice.offset_pointer((u32) Table4Offsets::RangeConstBase + segcount_x2 * 3 + offset));
u32 delta = be_u16(m_slice.offset_pointer((u32)Table4Offsets::DeltaConstBase + segcount_x2 * 2 + offset));
u32 range = be_u16(m_slice.offset_pointer((u32)Table4Offsets::RangeConstBase + segcount_x2 * 3 + offset));
if (range == 0) {
return (codepoint + delta) & 0xffff;
}
u32 glyph_offset = (u32) Table4Offsets::GlyphOffsetConstBase + segcount_x2 * 3 + offset + range + (codepoint - start_codepoint) * 2;
u32 glyph_offset = (u32)Table4Offsets::GlyphOffsetConstBase + segcount_x2 * 3 + offset + range + (codepoint - start_codepoint) * 2;
ASSERT(glyph_offset + 2 <= m_slice.size());
return (be_u16(m_slice.offset_pointer(glyph_offset)) + delta) & 0xffff;
}
@ -121,18 +137,18 @@ u32 Cmap::Subtable::glyph_id_for_codepoint_table_4(u32 codepoint) const
u32 Cmap::Subtable::glyph_id_for_codepoint_table_12(u32 codepoint) const
{
u32 num_groups = be_u32(m_slice.offset_pointer((u32) Table12Offsets::NumGroups));
ASSERT(m_slice.size() >= (u32) Table12Sizes::Header + (u32) Table12Sizes::Record * num_groups);
for (u32 offset = 0; offset < num_groups * (u32) Table12Sizes::Record; offset += (u32) Table12Sizes::Record) {
u32 start_codepoint = be_u32(m_slice.offset_pointer((u32) Table12Offsets::Record_StartCode + offset));
u32 num_groups = be_u32(m_slice.offset_pointer((u32)Table12Offsets::NumGroups));
ASSERT(m_slice.size() >= (u32)Table12Sizes::Header + (u32)Table12Sizes::Record * num_groups);
for (u32 offset = 0; offset < num_groups * (u32)Table12Sizes::Record; offset += (u32)Table12Sizes::Record) {
u32 start_codepoint = be_u32(m_slice.offset_pointer((u32)Table12Offsets::Record_StartCode + offset));
if (codepoint < start_codepoint) {
break;
}
u32 end_codepoint = be_u32(m_slice.offset_pointer((u32) Table12Offsets::Record_EndCode + offset));
u32 end_codepoint = be_u32(m_slice.offset_pointer((u32)Table12Offsets::Record_EndCode + offset));
if (codepoint > end_codepoint) {
continue;
}
u32 glyph_offset = be_u32(m_slice.offset_pointer((u32) Table12Offsets::Record_StartGlyph + offset));
u32 glyph_offset = be_u32(m_slice.offset_pointer((u32)Table12Offsets::Record_StartGlyph + offset));
return codepoint - start_codepoint + glyph_offset;
}
return 0;
@ -150,7 +166,7 @@ u32 Cmap::glyph_id_for_codepoint(u32 codepoint) const
Optional<Cmap> Cmap::from_slice(const ByteBuffer& slice)
{
if (slice.size() < (size_t) Sizes::TableHeader) {
if (slice.size() < (size_t)Sizes::TableHeader) {
return {};
}
return Cmap(slice);

View file

@ -38,6 +38,12 @@
namespace TTF {
u16 be_u16(const u8* ptr);
u32 be_u32(const u8* ptr);
i16 be_i16(const u8* ptr);
float be_fword(const u8* ptr);
u32 tag_from_str(const char* str);
u16 be_u16(const u8* ptr)
{
return (((u16)ptr[0]) << 8) | ((u16)ptr[1]);
@ -105,9 +111,12 @@ IndexToLocFormat Head::index_to_loc_format() const
{
i16 raw = be_i16(m_slice.offset_pointer((u32)Offsets::IndexToLocFormat));
switch (raw) {
case 0: return IndexToLocFormat::Offset16;
case 1: return IndexToLocFormat::Offset32;
default: ASSERT_NOT_REACHED();
case 0:
return IndexToLocFormat::Offset16;
case 1:
return IndexToLocFormat::Offset32;
default:
ASSERT_NOT_REACHED();
}
}
@ -428,7 +437,7 @@ int ScaledFont::width(const Utf32View& utf32) const
{
int width = 0;
for (size_t i = 0; i < utf32.length(); i++) {
u32 glyph_id = glyph_id_for_codepoint(utf32.codepoints()[i]);
u32 glyph_id = glyph_id_for_codepoint(utf32.code_points()[i]);
auto metrics = glyph_metrics(glyph_id);
width += metrics.advance_width;
}

View file

@ -24,8 +24,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <LibGfx/FloatPoint.h>
#include <LibGfx/Path.h>
#include <LibGfx/Point.h>
#include <LibTTF/Glyf.h>
namespace TTF {
@ -211,8 +211,8 @@ Optional<Glyf::Glyph::ComponentIterator::Item> Glyf::Glyph::ComponentIterator::n
Rasterizer::Rasterizer(Gfx::IntSize size)
: m_size(size)
, m_data(m_size.width() * m_size.height())
{
m_data.resize(m_size.width() * m_size.height());
for (int i = 0; i < m_size.width() * m_size.height(); i++) {
m_data[i] = 0.0;
}
@ -250,10 +250,18 @@ RefPtr<Gfx::Bitmap> Rasterizer::accumulate()
void Rasterizer::draw_line(Gfx::FloatPoint p0, Gfx::FloatPoint p1)
{
// FIXME: Shift x and y according to dy/dx
if (p0.x() < 0.0) { p0.set_x(roundf(p0.x())); }
if (p0.y() < 0.0) { p0.set_y(roundf(p0.y())); }
if (p1.x() < 0.0) { p1.set_x(roundf(p1.x())); }
if (p1.y() < 0.0) { p1.set_y(roundf(p1.y())); }
if (p0.x() < 0.0) {
p0.set_x(roundf(p0.x()));
}
if (p0.y() < 0.0) {
p0.set_y(roundf(p0.y()));
}
if (p1.x() < 0.0) {
p1.set_x(roundf(p1.x()));
}
if (p1.y() < 0.0) {
p1.set_y(roundf(p1.y()));
}
ASSERT(p0.x() >= 0.0 && p0.y() >= 0.0 && p0.x() <= m_size.width() && p0.y() <= m_size.height());
ASSERT(p1.x() >= 0.0 && p1.y() >= 0.0 && p1.x() <= m_size.width() && p1.y() <= m_size.height());
@ -436,7 +444,7 @@ void Glyf::Glyph::raster_inner(Rasterizer& rasterizer, Gfx::AffineTransform& aff
if (next_item.on_curve) {
path.quadratic_bezier_curve_to(item.point, next_item.point);
} else {
auto mid_point = Gfx::FloatPoint::interpolate(item.point, next_item.point, 0.5);
auto mid_point = (item.point + next_item.point) * 0.5f;
path.quadratic_bezier_curve_to(item.point, mid_point);
last_offcurve_point = next_item.point;
}
@ -462,7 +470,7 @@ void Glyf::Glyph::raster_inner(Rasterizer& rasterizer, Gfx::AffineTransform& aff
if (item.on_curve) {
path.quadratic_bezier_curve_to(point0, item.point);
} else {
auto mid_point = Gfx::FloatPoint::interpolate(point0, item.point, 0.5);
auto mid_point = (point0 + item.point) * 0.5f;
path.quadratic_bezier_curve_to(point0, mid_point);
last_offcurve_point = item.point;
}
@ -494,7 +502,8 @@ Glyf::Glyph Glyf::glyph(u32 offset) const
i16 ymin = be_i16(m_slice.offset_pointer(offset + (u32)Offsets::YMin));
i16 xmax = be_i16(m_slice.offset_pointer(offset + (u32)Offsets::XMax));
i16 ymax = be_i16(m_slice.offset_pointer(offset + (u32)Offsets::YMax));
auto slice = ByteBuffer::wrap(m_slice.offset_pointer(offset + (u32)Sizes::GlyphHeader), m_slice.size() - offset - (u32)Sizes::GlyphHeader);
// HACK: added const_cast because of new wrap behavior
auto slice = ByteBuffer::wrap(const_cast<u8*>(m_slice.offset_pointer(offset + (u32)Sizes::GlyphHeader)), m_slice.size() - offset - (u32)Sizes::GlyphHeader);
return Glyph(slice, xmin, ymin, xmax, ymax, num_contours);
}

View file

@ -27,7 +27,7 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/FixedArray.h>
#include <AK/Vector.h>
#include <LibGfx/AffineTransform.h>
#include <LibGfx/Bitmap.h>
#include <LibTTF/Tables.h>
@ -45,7 +45,7 @@ private:
void draw_line(Gfx::FloatPoint, Gfx::FloatPoint);
Gfx::IntSize m_size;
FixedArray<float> m_data;
AK::Vector<float> m_data;
};
class Loca {