diff --git a/src/hb-aat-var-hvgl-table.cc b/src/hb-aat-var-hvgl-table.cc index ad786d1e2..846c582f0 100644 --- a/src/hb-aat-var-hvgl-table.cc +++ b/src/hb-aat-var-hvgl-table.cc @@ -179,6 +179,127 @@ PartShape::get_path_at (const struct hvgl &hvgl, } } +void +PartComposite::apply_transforms (hb_array_t transforms, + hb_array_t coords) const +{ + const auto &allTranslations = StructAtOffset (this, allTranslationsOff4 * 4); + const auto &allRotations = StructAtOffset (this, allRotationsOff4 * 4); + + const auto &masterTranslationDelta = allTranslations.masterTranslationDelta; + const auto &extremumTranslationDelta = StructAfter (masterTranslationDelta, sparseMasterTranslationCount); + const auto &extremumTranslationIndex = StructAfter (extremumTranslationDelta, sparseExtremumTranslationCount); + const auto &masterTranslationIndex = StructAfter (extremumTranslationIndex, sparseExtremumTranslationCount); + + const auto &masterRotationDelta = allRotations.masterRotationDelta; + const auto &extremumRotationDelta = StructAfter (masterRotationDelta, sparseMasterRotationCount); + const auto &extremumRotationIndex = StructAfter (extremumRotationDelta, sparseExtremumRotationCount); + const auto &masterRotationIndex = StructAfter (extremumRotationIndex, sparseExtremumRotationCount); + + auto master_translation_deltas = masterTranslationDelta.as_array (sparseMasterTranslationCount); + auto master_translation_indices = masterTranslationIndex.as_array (sparseMasterTranslationCount); + auto extremum_translation_deltas = extremumTranslationDelta.as_array (sparseExtremumTranslationCount); + auto extremum_translation_indices = extremumTranslationIndex.as_array (sparseExtremumTranslationCount); + + auto master_rotation_deltas = masterRotationDelta.as_array (sparseMasterRotationCount); + auto master_rotation_indices = masterRotationIndex.as_array (sparseMasterRotationCount); + auto extremum_rotation_deltas = extremumRotationDelta.as_array (sparseExtremumRotationCount); + auto extremum_rotation_indices = extremumRotationIndex.as_array (sparseExtremumRotationCount); + + for (unsigned row = 0; row < transforms.length; row++) + { + hb_transform_t transform; + + auto master_rotation_delta = Null(HBFLOAT32LE); + if (row == master_rotation_indices[0]) + { + master_rotation_delta = master_rotation_deltas[0]; + master_rotation_deltas++; + master_rotation_indices++; + } + auto master_translation_delta = Null(TranslationDelta); + if (row == master_translation_indices[0]) + { + master_translation_delta = master_translation_deltas[0]; + master_translation_deltas++; + master_translation_indices++; + } + transform.rotate (master_rotation_delta); + transform.translate (master_translation_delta.x, master_translation_delta.y); + + unsigned translation_index_end = 0; + while (translation_index_end < extremum_translation_indices.length && + row == extremum_translation_indices[translation_index_end].row) + translation_index_end++; + + unsigned rotation_index_end = 0; + while (rotation_index_end < extremum_rotation_indices.length && + row == extremum_rotation_indices[rotation_index_end].row) + rotation_index_end++; + + unsigned translation_index = 0; + unsigned rotation_index = 0; + while (translation_index < translation_index_end || + rotation_index < rotation_index_end) + { + auto extremum_translation_delta = Null(TranslationDelta); + auto extremum_rotation_delta = Null(HBFLOAT32LE); + + unsigned column; + if (translation_index < translation_index_end && + (rotation_index == rotation_index_end || + extremum_translation_indices[translation_index].column < extremum_rotation_indices[rotation_index].column)) + column = extremum_translation_indices[translation_index].column; + else + column = extremum_rotation_indices[rotation_index].column; + + if (translation_index < translation_index_end && + extremum_translation_indices[translation_index].column == column) + { + extremum_translation_delta = extremum_translation_deltas[translation_index]; + translation_index++; + } + if (rotation_index < rotation_index_end && + extremum_rotation_indices[rotation_index].column == column) + { + extremum_rotation_delta = extremum_rotation_deltas[rotation_index]; + rotation_index++; + } + + bool pos = column & 1; + unsigned axis_idx = column / 2; + float coord = coords[axis_idx]; + if (!coord || (pos != (coord > 0))) + continue; + float scalar = fabsf (coord); + + hb_transform_t extremum_transform; + + float eigen_x, eigen_y; + if (transform.get_eigen_vector (eigen_x, eigen_y)) + { + // Scale rotation around eigen vector + extremum_transform.translate (-eigen_x, -eigen_y); + extremum_transform.rotate (extremum_rotation_delta * scalar); + extremum_transform.translate (eigen_x, eigen_y); + } + else + { + // No rotation, just scale the translate + extremum_transform.translate (extremum_translation_delta.x * scalar, + extremum_translation_delta.y * scalar); + } + + transform.transform (extremum_transform); + } + extremum_translation_deltas += translation_index_end;; + extremum_translation_indices += translation_index_end; + extremum_rotation_deltas += rotation_index_end; + extremum_rotation_indices += rotation_index_end; + + transforms[row].transform (transform); + } +} void PartComposite::get_path_at (const struct hvgl &hvgl, @@ -210,14 +331,13 @@ PartComposite::get_path_at (const struct hvgl &hvgl, auto transforms_head = transforms[0]; auto transforms_tail = transforms.sub_array (1); -#if 0 - const auto &allTranslations = StructAtOffset (this, allTranslationsOff4 * 4); - const auto &allRotations = StructAtOffset (this, allRotationsOff4 * 4); -#endif + apply_transforms (transforms_tail, coords); for (const auto &subPart : subParts.as_array (subPartCount)) { - transforms_tail[subPart.treeTransformIndex].transform (transforms_head); + hb_transform_t total_transform (transforms_head); + total_transform.transform (transforms_tail[subPart.treeTransformIndex]); + transforms_tail[subPart.treeTransformIndex] = total_transform; hvgl.get_part_path_at (subPart.partIndex, draw_session, coords_tail.sub_array (subPart.treeAxisIndex), transforms_tail.sub_array (subPart.treeTransformIndex), diff --git a/src/hb-aat-var-hvgl-table.hh b/src/hb-aat-var-hvgl-table.hh index 8c24a46cc..4f332549c 100644 --- a/src/hb-aat-var-hvgl-table.hh +++ b/src/hb-aat-var-hvgl-table.hh @@ -280,33 +280,35 @@ struct MatrixIndex struct AllTranslations { + friend struct PartComposite; + public: bool sanitize (hb_sanitize_context_t *c, - unsigned sparseMasterTranslationCount, - unsigned sparseExtremumTranslationCount) const + unsigned sparse_master_translation_count, + unsigned sparse_extremum_translation_count) const { TRACE_SANITIZE (this); - if (unlikely (!masterTranslationDelta.sanitize (c, sparseMasterTranslationCount))) return_trace (false); + if (unlikely (!masterTranslationDelta.sanitize (c, sparse_master_translation_count))) return_trace (false); - const auto &extremumTranslationDelta = StructAfter (masterTranslationDelta, sparseMasterTranslationCount); - if (unlikely (!extremumTranslationDelta.sanitize (c, sparseExtremumTranslationCount))) return_trace (false); + const auto &extremumTranslationDelta = StructAfter (masterTranslationDelta, sparse_master_translation_count); + if (unlikely (!extremumTranslationDelta.sanitize (c, sparse_extremum_translation_count))) return_trace (false); - const auto &extremumTranslationIndex = StructAfter (extremumTranslationDelta, sparseExtremumTranslationCount); - if (unlikely (!extremumTranslationIndex.sanitize (c, sparseExtremumTranslationCount))) return_trace (false); + const auto &extremumTranslationIndex = StructAfter (extremumTranslationDelta, sparse_extremum_translation_count); + if (unlikely (!extremumTranslationIndex.sanitize (c, sparse_extremum_translation_count))) return_trace (false); - const auto &masterTranslationIndex = StructAfter (extremumTranslationIndex, sparseExtremumTranslationCount); - if (unlikely (!masterTranslationIndex.sanitize (c, sparseMasterTranslationCount))) return_trace (false); + const auto &masterTranslationIndex = StructAfter (extremumTranslationIndex, sparse_extremum_translation_count); + if (unlikely (!masterTranslationIndex.sanitize (c, sparse_master_translation_count))) return_trace (false); return_trace (true); } protected: - UnsizedArrayOf masterTranslationDelta; // length: sparseMasterTranslationCount; Master translation deltas - UnsizedArrayOf extremumTranslationDeltaX; // length: sparseExtremumTranslationCount; Extremum translation deltas - UnsizedArrayOf extremumTranslationIndexX; // length: sparseExtremumTranslationCount; Extremum translation indices - UnsizedArrayOf masterTranslationIndexX; // length: sparseMasterTranslationCount; Master translation indices + UnsizedArrayOf masterTranslationDelta; // length: sparse_master_translation_count; Master translation deltas + UnsizedArrayOf extremumTranslationDeltaX; // length: sparse_extremum_translation_count; Extremum translation deltas + UnsizedArrayOf extremumTranslationIndexX; // length: sparse_extremum_translation_count; Extremum translation indices + UnsizedArrayOf masterTranslationIndexX; // length: sparse_master_translation_count; Master translation indices Align<4> padding; // Pad to float32le alignment public: @@ -315,33 +317,35 @@ struct AllTranslations struct AllRotations { + friend struct PartComposite; + public: bool sanitize (hb_sanitize_context_t *c, - unsigned sparseMasterRotationCount, - unsigned sparseExtremumRotationCount) const + unsigned sparse_master_rotation_count, + unsigned sparse_extremum_rotation_count) const { TRACE_SANITIZE (this); - if (unlikely (!masterRotationDelta.sanitize (c, sparseMasterRotationCount))) return_trace (false); + if (unlikely (!masterRotationDelta.sanitize (c, sparse_master_rotation_count))) return_trace (false); - const auto &extremumRotationDelta = StructAfter (masterRotationDelta, sparseMasterRotationCount); - if (unlikely (!extremumRotationDelta.sanitize (c, sparseExtremumRotationCount))) return_trace (false); + const auto &extremumRotationDelta = StructAfter (masterRotationDelta, sparse_master_rotation_count); + if (unlikely (!extremumRotationDelta.sanitize (c, sparse_extremum_rotation_count))) return_trace (false); - const auto &extremumRotationIndex = StructAfter (extremumRotationDelta, sparseExtremumRotationCount); - if (unlikely (!extremumRotationIndex.sanitize (c, sparseExtremumRotationCount))) return_trace (false); + const auto &extremumRotationIndex = StructAfter (extremumRotationDelta, sparse_extremum_rotation_count); + if (unlikely (!extremumRotationIndex.sanitize (c, sparse_extremum_rotation_count))) return_trace (false); - const auto &masterRotationIndex = StructAfter (extremumRotationIndex, sparseExtremumRotationCount); - if (unlikely (!masterRotationIndex.sanitize (c, sparseMasterRotationCount))) return_trace (false); + const auto &masterRotationIndex = StructAfter (extremumRotationIndex, sparse_extremum_rotation_count); + if (unlikely (!masterRotationIndex.sanitize (c, sparse_master_rotation_count))) return_trace (false); return_trace (true); } protected: - UnsizedArrayOf masterRotationDelta; // length: sparseMasterRotationCount; Master rotation deltas - UnsizedArrayOf extremumRotationDeltaX; // length: sparseExtremumRotationCount; Extremum rotation deltas - UnsizedArrayOf extremumRotationIndexX; // length: sparseExtremumRotationCount; Extremum rotation indices - UnsizedArrayOf masterRotationIndexX; // length: sparseMasterRotationCount; Master rotation indices + UnsizedArrayOf masterRotationDelta; // length: sparse_master_rotation_count; Master rotation deltas + UnsizedArrayOf extremumRotationDeltaX; // length: sparse_extremum_rotation_count; Extremum rotation deltas + UnsizedArrayOf extremumRotationIndexX; // length: sparse_extremum_rotation_count; Extremum rotation indices + UnsizedArrayOf masterRotationIndexX; // length: sparse_master_rotation_count; Master rotation indices Align<4> padding; // Pad to float32le alignment public: @@ -354,6 +358,9 @@ struct PartComposite { public: + HB_INTERNAL void apply_transforms (hb_array_t transforms, + hb_array_t coords) const; + unsigned get_total_num_axes () const { return totalNumAxes; } diff --git a/src/hb-geometry.hh b/src/hb-geometry.hh index 11bce0cf7..1a185e38c 100644 --- a/src/hb-geometry.hh +++ b/src/hb-geometry.hh @@ -225,6 +225,19 @@ struct hb_transform_t transform (other); } + bool get_eigen_vector (float &eigen_x, float &eigen_y) const + { + // Find the eigenvector of the transform, or determine that there is none. + float a = xx - yy; + float b = 2 * xy; + float d = sqrtf (a * a + b * b); + if (d == 0.f) + return false; + eigen_x = b / d; + eigen_y = -a / d; + return true; + } + float xx = 1.f; float yx = 0.f; float xy = 0.f;