I have a program that needs to load fonts into a display according to language ID.
I would like to have a structure containing the attributes for writing the font data:
typedef struct Font_Data_Metrics_s
{
uint8_t const * font_data_start;
size_t font_data_length;
unsigned int language_id;
} Font_Data_Metrics_t;
The font data are in separate files, one font per file.
Example:
Russian_Font_Data.c:
uint8_t const Russian_Font_Data[] =
{
// the data
};
const size_t Size_Russian_Font_Data =
sizeof(Russian_Font_Data) / sizeof(Russian_Font_Data[0]);
Ideally, I'd like to use (outside of Russian_Font_Data.c):
const static Font_Data_Metrics_t russian_metrics =
{
Russian_Font_Data, // This field is the annoying issue.
Size_Fussion_Font_Data,
6, // Russian language ID
};
Here's what I've tried:
(1) extern uint8_t const Russian_Font_Data[];
Error: When an array is declared with external linkage, its size shall be stated explicitly or defined implicitly by initialisation (MISRA C 2004 rule 8.12)
(2) extern uint8_t const * Russian_Font_Data;
Error: Declaration is incompatible with "uint8_t const * Russian_Font_Data"
(3) uint8_t const font_data_start[]; (inside the struct).
Illformed when inside the structure.
How can place the address of an array, with external linkage, into a struct?
(Preferably without annoying MISRA violations)
Edit 1: Background / Usage
I want a table of metrics for over 12 fonts. The table will be searched by language ID. The font data start and font data length will be passed to a function that writes the data to a hardware display device. Other code in the project will be setting the language ID based on the Operator's selection.
Presently, I have a table that is built during run-time by calling "data" functions. This is messy and I'll need "getter" functions for each font. My objective is to have a table with constant data, that can be placed and directly accessed from the data segment (in other words, ready for access before the program starts).
CodePudding user response:
Perhaps you can define the metrics and the font data in the same .c file:
In Russian_Font_Data.c:
#include "Font_Data_Metrics.h"
const static uint8_t Russian_Font_Data[] =
{
// the data
};
const Font_Data_Metrics_t russian_metrics =
{
.font_data_start = Russian_Font_Data,
.font_data_length = sizeof Russian_Font_Data / sizeof Russian_Font_Data[0],
.language_id = 6, // Russian language ID
};
In some header file:
#include "Font_Data_Metrics.h"
extern const Font_Data_Metrics_t russian_metrics;
