Attempted to clean up the Uniscribe stuff.

This commit is contained in:
Pietro Gagliardi 2015-09-14 23:08:44 -04:00
parent 9dbc8a1ba2
commit e79066fd9d
1 changed files with 107 additions and 90 deletions

View File

@ -38,6 +38,22 @@ void uiDrawFreeFont(uiDrawFont *df)
- http://www.microsoft.com/msj/1198/multilang/multilang.aspx - http://www.microsoft.com/msj/1198/multilang/multilang.aspx
Even with all this we're on our own with actual paragraph layout; see the last link for example code (which we don't use here). */ Even with all this we're on our own with actual paragraph layout; see the last link for example code (which we don't use here). */
struct uniitem {
SCRIPT_ITEM item;
int nGlyphs;
WORD *glyphs;
SCRIPT_VISATTR *visattrs;
int *advances;
GOFFSET *goffsets;
WCHAR *pwcChars;
int nChars;
WORD *clusters;
ABC abc;
};
struct uniscribe { struct uniscribe {
WCHAR *text; WCHAR *text;
size_t len; size_t len;
@ -46,26 +62,22 @@ struct uniscribe {
SCRIPT_CONTROL control; SCRIPT_CONTROL control;
SCRIPT_STATE state; SCRIPT_STATE state;
SCRIPT_ITEM *items; struct uniitem *items;
int nItems; int nItems;
SCRIPT_CACHE cache; SCRIPT_CACHE cache;
WORD **glyphs;
WORD **clusters;
SCRIPT_VISATTR **visattrs;
int *nGlyphs;
int **advances;
GOFFSET **gOffsets;
ABC *abcs;
}; };
// the step numbers in this function are according to the "Lay Out Text Using Uniscribe" section of https://msdn.microsoft.com/en-us/library/windows/desktop/dd317792%28v=vs.85%29.aspx // the step numbers in this function are according to the "Lay Out Text Using Uniscribe" section of https://msdn.microsoft.com/en-us/library/windows/desktop/dd317792%28v=vs.85%29.aspx
void initUniscribe(struct uniscribe *s, WCHAR *text, HDC dc)
// this does step 4
// steps 1 through 3 are irrelevent here
static void initUniscribe(struct uniscribe *s, WCHAR *text, HDC dc)
{ {
HRESULT hr; SCRIPT_ITEM *items;
int nAlloc; int nAlloc;
int i; int i;
HRESULT hr;
ZeroMemory(s, sizeof (struct uniscribe)); ZeroMemory(s, sizeof (struct uniscribe));
@ -73,17 +85,15 @@ void initUniscribe(struct uniscribe *s, WCHAR *text, HDC dc)
s->len = wcslen(s->text); s->len = wcslen(s->text);
s->dc = dc; s->dc = dc;
// steps 1 through 3 are irrelevent here items = NULL;
// step 4
nAlloc = 32; nAlloc = 32;
for (;;) { for (;;) {
// TODO use uiRealloc // TODO use uiRealloc
s->items = realloc(s->items, nAlloc * sizeof (SCRIPT_ITEM)); items = realloc(items, nAlloc * sizeof (SCRIPT_ITEM));
hr = ScriptItemize(s->text, s->len, hr = ScriptItemize(s->text, s->len,
nAlloc, nAlloc,
&(s->control), &(s->state), &(s->control), &(s->state),
s->items, &(s->nItems)); items, &(s->nItems));
if (hr == S_OK) if (hr == S_OK)
break; break;
if (hr == E_OUTOFMEMORY) { if (hr == E_OUTOFMEMORY) {
@ -93,96 +103,103 @@ void initUniscribe(struct uniscribe *s, WCHAR *text, HDC dc)
logHRESULT("error itemizing string in initUniscribe()", hr); logHRESULT("error itemizing string in initUniscribe()", hr);
} }
// step 5 // this does part of step 6
// we're not using multiple fonts; we have no lists to merge s->items = malloc(s->nItems * sizeof (struct uniitem));
ZeroMemory(s->items, s->nItems * sizeof (struct uniitem));//TODO remove when switching to uiAlloc
// step 6
s->glyphs = malloc(s->nItems * sizeof (WORD *));
s->clusters = malloc(s->nItems * sizeof (WORD *));
s->visattrs = malloc(s->nItems * sizeof (SCRIPT_VISATTR *));
s->nGlyphs = malloc(s->nItems * sizeof (int));
for (i = 0; i < s->nItems; i++) { for (i = 0; i < s->nItems; i++) {
WCHAR *pwcChars; s->items[i].pwcChars = s->text + items[i].iCharPos;
int nChars; s->items[i].nChars = items[i + 1].iCharPos - items[i].iCharPos;
s->items[i].item = items[i];
pwcChars = s->text + s->items[i].iCharPos;
nChars = s->items[i + 1].iCharPos - s->items[i].iCharPos;
nAlloc = 1.5 * nChars + 16;
s->glyphs[i] = NULL;//TODO remove when switched to uiAlloc
s->visattrs[i] = NULL;
s->clusters[i] = malloc(nChars * sizeof (WORD));
for (;;) {
s->glyphs[i] = realloc(s->glyphs[i], nAlloc * sizeof (WORD));
s->visattrs[i] = realloc(s->visattrs[i], nAlloc * sizeof (SCRIPT_VISATTR));
hr = ScriptShape(s->dc, &(s->cache),
pwcChars, nChars,
nAlloc,
&(s->items[i].a),
s->glyphs[i], s->clusters[i],
s->visattrs[i], &(s->nGlyphs[i]));
if (hr == S_OK)
break;
if (hr == E_OUTOFMEMORY) {
nAlloc *= 2;
continue;
}
logHRESULT("error shaping glyphs in initUniscribe()", hr);
}
} }
free(items);
}
// step 7 static void resetItem(struct uniitem *item)
// TODO {
if (item->nGlyphs == 0)
return;
item->nGlyphs = 0;
// TODO switch to uiFree
free(item->glyphs);
item->glyphs = NULL;
free(item->visattrs);
item->visattrs = NULL;
free(item->advances);
item->advances = NULL;
free(item->goffsets);
item->goffsets = NULL;
free(item->clusters);
item->clusters = NULL;
}
// step 8 // step 5
s->advances = malloc(s->nItems * sizeof (int *)); // we're not using multiple fonts; we have no lists to merge
s->goffsets = malloc(s->nItems * sizeof (GOFFSET *));
s->abcs = malloc(s->nItems * sizeof (ABC)); // step 6
for (i = 0; i < s->nItems; i++) { static void shapeItem(struct uniscribe *s, int n)
s->advances[i] = malloc(s->nGlyphs[i] * sizeof (int)); {
s->goffsets[i] = malloc(s->nGlyphs[i] * sizeof (GOFFSET)); struct uniitem *item;
hr = ScriptPlace(s->dc, &(s->cache), int nAlloc;
s->glyphs[i], s->nGlyphs[i], s->visattrs[i], HRESULT hr;
&(s->items[i].a),
s->advances[i], s->goffsets[i], &(s->abcs[i])); item = &(s->items[n]);
if (hr != S_OK) resetItem(item);
logHRESULT("error placing glyphs in initUniscribe()", hr); item->clusters = malloc(nChars * sizeof (WORD));
// TODO move to uiAlloc
nAlloc = 1.5 * item->nChars + 16;
for (;;) {
item->glyphs = realloc(item->glyphs, nAlloc * sizeof (WORD));
item->visattrs = realloc(item->visattrs, nAlloc * sizeof (SCRIPT_VISATTR));
hr = ScriptShape(s->dc, &(s->cache),
item->pwcChars, item->nChars,
nAlloc,
&(item->item.a),
item->glyphs, item->clusters,
item->visattrs, &(item->nGlyphs));
if (hr == S_OK)
break;
if (hr == E_OUTOFMEMORY) {
nAlloc *= 2;
continue;
}
logHRESULT("error shaping glyphs in shapeItem()", hr);
} }
} }
void uninitUniscribe(struct uniscribe *s) // step 7
// TODO
// step 8
static void placeItem(struct uniscribe *s, int n)
{
struct uniitem *item;
HRESULT hr;
item = &(s->items[n]);
item->advances = malloc(item->nGlyphs * sizeof (int));
item->goffsets = malloc(item->nGlyphs * sizeof (GOFFSET));
hr = ScriptPlace(s->dc, &(s->cache),
item->glyphs, item->nGlyphs, item->visattrs,
&(item->item.a),
item->advances, item->goffsets, &(item->abc));
if (hr != S_OK)
logHRESULT("error placing glyphs in placeItem()", hr);
}
static void uninitUniscribe(struct uniscribe *s)
{ {
HRESULT hr; HRESULT hr;
int i; int i;
// TODO use uiFree // TODO use uiFree
free(s->abcs);
for (i = 0; i < s->goffsets; i++)
free(s->goffsets[i]);
free(s->goffsets);
for (i = 0; i < s->nItems; i++) for (i = 0; i < s->nItems; i++)
free(s->advances[i]); resetItem(&(s->items[i]));
free(s->advances); free(s->items);
free(s->nGlyphs);
for (i = 0; i < s->nItems; i++)
free(s->visattrs[i]);
free(s->visattrs);
for (i = 0; i < s->nItems; i++)
free(s->clusters[i]);
free(s->clusters);
for (i = 0; i < s->nItems; i++)
free(s->glyphs[i]);
free(s->glyphs);
hr = ScriptCacheFree(&(s->cache)); hr = ScriptCacheFree(&(s->cache));
if (hr != S_OK) if (hr != S_OK)
logHRESULT("error freeing Uniscribe cache in uninitUniscribe()"); logHRESULT("error freeing Uniscribe cache in uninitUniscribe()");
free(s->items);
} }