From 672d67859f5720e69bca3a97ec96a02362091c1a Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Mon, 30 Sep 2019 21:33:15 +0200 Subject: [PATCH] mfoutline: Modernize code - Use C++ enums - Use strongly typed C++11 enum for DIRECTION and optimize struct MFEDGEPT - Use float constant for MF_SCALE_FACTOR - Replace macros by inline functions - Fix documentation comment This fixes several warnings from clang. Signed-off-by: Stefan Weil --- src/classify/mfoutline.cpp | 7 +++-- src/classify/mfoutline.h | 62 +++++++++++++++++++++++--------------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/classify/mfoutline.cpp b/src/classify/mfoutline.cpp index 9cdef184..e506b294 100644 --- a/src/classify/mfoutline.cpp +++ b/src/classify/mfoutline.cpp @@ -63,7 +63,7 @@ MFOUTLINE ConvertOutline(TESSLINE *outline) { if (EdgePoint->pos.x != NextPoint->pos.x || EdgePoint->pos.y != NextPoint->pos.y) { NewPoint = NewEdgePoint(); - ClearMark(NewPoint); + NewPoint->ClearMark(); NewPoint->Hidden = EdgePoint->IsHidden(); NewPoint->Point.x = EdgePoint->pos.x; NewPoint->Point.y = EdgePoint->pos.y; @@ -192,7 +192,7 @@ void MarkDirectionChanges(MFOUTLINE Outline) { Last = First; do { Current = NextDirectionChange (Last); - MarkPoint (PointAt (Current)); + PointAt(Current)->MarkPoint(); Last = Current; } while (Last != First); @@ -303,7 +303,8 @@ void Classify::NormalizeOutlines(LIST Outlines, * starts at Start and ends at End. Note that the previous * direction of End must also be changed to reflect the * change in direction of the point before it. - * @param Start, End defines segment of outline to be modified + * @param Start defines start of segment of outline to be modified + * @param End defines end of segment of outline to be modified * @param Direction new direction to assign to segment */ void ChangeDirection(MFOUTLINE Start, MFOUTLINE End, DIRECTION Direction) { diff --git a/src/classify/mfoutline.h b/src/classify/mfoutline.h index 435408aa..2e14071e 100644 --- a/src/classify/mfoutline.h +++ b/src/classify/mfoutline.h @@ -26,12 +26,9 @@ #include "oldlist.h" #include "params.h" -#define NORMAL_X_HEIGHT (0.5) -#define NORMAL_BASELINE (0.0) - using MFOUTLINE = LIST; -typedef enum { +enum DIRECTION : uint8_t { north, south, east, @@ -40,39 +37,56 @@ typedef enum { northwest, southeast, southwest -} DIRECTION; +}; + +struct MFEDGEPT { + // Inline functions for manipulating micro-feature outline edge points. + + void ClearMark() { + ExtremityMark = false; + } + + void MarkPoint() { + ExtremityMark = true; + } -typedef struct { FPOINT Point; float Slope; - unsigned Padding : 20; - bool Hidden : true; - bool ExtremityMark : true; - DIRECTION Direction : 4; - DIRECTION PreviousDirection : 4; -} MFEDGEPT; + bool Hidden; + bool ExtremityMark; + DIRECTION Direction; + DIRECTION PreviousDirection; +}; -typedef enum { outer, hole } OUTLINETYPE; +enum OUTLINETYPE { outer, hole }; -typedef enum { baseline, character } NORM_METHOD; +enum NORM_METHOD { baseline, character }; /**---------------------------------------------------------------------------- Macros ----------------------------------------------------------------------------**/ #define AverageOf(A, B) (((A) + (B)) / 2) -/* macro for computing the scale factor to use to normalize characters */ -#define MF_SCALE_FACTOR (NORMAL_X_HEIGHT / kBlnXHeight) +// Constant for computing the scale factor to use to normalize characters. +const float MF_SCALE_FACTOR = 0.5f / kBlnXHeight; -/* macros for manipulating micro-feature outlines */ -#define DegenerateOutline(O) (((O) == NIL_LIST) || ((O) == list_rest(O))) -#define PointAt(O) ((MFEDGEPT*)first_node(O)) -#define NextPointAfter(E) (list_rest(E)) -#define MakeOutlineCircular(O) (set_rest(last(O), (O))) +// Inline functions for manipulating micro-feature outlines. -/* macros for manipulating micro-feature outline edge points */ -#define ClearMark(P) ((P)->ExtremityMark = false) -#define MarkPoint(P) ((P)->ExtremityMark = true) +static inline bool DegenerateOutline(MFOUTLINE Outline) { + return (Outline == NIL_LIST) || (Outline == list_rest(Outline)); +} + +static inline MFEDGEPT* PointAt(MFOUTLINE Outline) { + return reinterpret_castfirst_node(Outline); +} + +static inline MFOUTLINE NextPointAfter(MFOUTLINE Outline) { + return list_rest(Outline); +} + +static inline void MakeOutlineCircular(MFOUTLINE Outline) { + set_rest(last(Outline), Outline); +} /**---------------------------------------------------------------------------- Public Function Prototypes