dwrite: Implement paragraph alignment.

This commit is contained in:
Nikolay Sivov 2015-07-06 09:06:08 +03:00 committed by Alexandre Julliard
parent 44c135dd17
commit 862ac80a0d
2 changed files with 168 additions and 10 deletions

View File

@ -342,6 +342,16 @@ static inline HRESULT format_set_textalignment(struct dwrite_textformat_data *fo
return S_OK; return S_OK;
} }
static inline HRESULT format_set_paralignment(struct dwrite_textformat_data *format,
DWRITE_PARAGRAPH_ALIGNMENT alignment, BOOL *changed)
{
if ((UINT32)alignment > DWRITE_PARAGRAPH_ALIGNMENT_CENTER)
return E_INVALIDARG;
if (changed) *changed = format->paralign != alignment;
format->paralign = alignment;
return S_OK;
}
static HRESULT get_fontfallback_from_format(const struct dwrite_textformat_data *format, IDWriteFontFallback **fallback) static HRESULT get_fontfallback_from_format(const struct dwrite_textformat_data *format, IDWriteFontFallback **fallback)
{ {
*fallback = format->fallback; *fallback = format->fallback;
@ -1182,6 +1192,50 @@ static void layout_apply_text_alignment(struct dwrite_textlayout *layout)
} }
} }
static void layout_apply_par_alignment(struct dwrite_textlayout *layout)
{
struct layout_effective_inline *inrun;
struct layout_effective_run *erun;
FLOAT origin_y = 0.0;
UINT32 line;
/* alignment mode defines origin, after that all run origins are updated
the same way */
switch (layout->format.paralign)
{
case DWRITE_PARAGRAPH_ALIGNMENT_NEAR:
origin_y = 0.0;
break;
case DWRITE_PARAGRAPH_ALIGNMENT_FAR:
origin_y = layout->metrics.layoutHeight - layout->metrics.height;
break;
case DWRITE_PARAGRAPH_ALIGNMENT_CENTER:
origin_y = (layout->metrics.layoutHeight - layout->metrics.height) / 2.0;
break;
default:
;
}
layout->metrics.top = origin_y;
erun = layout_get_next_erun(layout, NULL);
inrun = layout_get_next_inline_run(layout, NULL);
for (line = 0; line < layout->metrics.lineCount; line++) {
origin_y += layout->lines[line].baseline;
while (erun && erun->line == line) {
erun->origin_y = origin_y;
erun = layout_get_next_erun(layout, erun);
}
while (inrun && inrun->line == line) {
inrun->origin_y = origin_y;
inrun = layout_get_next_inline_run(layout, inrun);
}
}
}
static HRESULT layout_compute_effective_runs(struct dwrite_textlayout *layout) static HRESULT layout_compute_effective_runs(struct dwrite_textlayout *layout)
{ {
struct layout_effective_inline *inrun; struct layout_effective_inline *inrun;
@ -1354,6 +1408,10 @@ static HRESULT layout_compute_effective_runs(struct dwrite_textlayout *layout)
if (layout->format.textalignment != DWRITE_TEXT_ALIGNMENT_LEADING) if (layout->format.textalignment != DWRITE_TEXT_ALIGNMENT_LEADING)
layout_apply_text_alignment(layout); layout_apply_text_alignment(layout);
/* initial paragraph alignment is always near */
if (layout->format.paralign != DWRITE_PARAGRAPH_ALIGNMENT_NEAR)
layout_apply_par_alignment(layout);
layout->metrics.heightIncludingTrailingWhitespace = layout->metrics.height; /* FIXME: not true for vertical text */ layout->metrics.heightIncludingTrailingWhitespace = layout->metrics.height; /* FIXME: not true for vertical text */
layout->recompute &= ~RECOMPUTE_EFFECTIVE_RUNS; layout->recompute &= ~RECOMPUTE_EFFECTIVE_RUNS;
@ -2072,7 +2130,6 @@ static HRESULT WINAPI dwritetextlayout_SetTextAlignment(IDWriteTextLayout2 *ifac
static HRESULT WINAPI dwritetextlayout_SetParagraphAlignment(IDWriteTextLayout2 *iface, DWRITE_PARAGRAPH_ALIGNMENT alignment) static HRESULT WINAPI dwritetextlayout_SetParagraphAlignment(IDWriteTextLayout2 *iface, DWRITE_PARAGRAPH_ALIGNMENT alignment)
{ {
struct dwrite_textlayout *This = impl_from_IDWriteTextLayout2(iface); struct dwrite_textlayout *This = impl_from_IDWriteTextLayout2(iface);
TRACE("(%p)->(%d)\n", This, alignment);
return IDWriteTextFormat1_SetParagraphAlignment(&This->IDWriteTextFormat1_iface, alignment); return IDWriteTextFormat1_SetParagraphAlignment(&This->IDWriteTextFormat1_iface, alignment);
} }
@ -3100,8 +3157,20 @@ static HRESULT WINAPI dwritetextformat1_layout_SetTextAlignment(IDWriteTextForma
static HRESULT WINAPI dwritetextformat1_layout_SetParagraphAlignment(IDWriteTextFormat1 *iface, DWRITE_PARAGRAPH_ALIGNMENT alignment) static HRESULT WINAPI dwritetextformat1_layout_SetParagraphAlignment(IDWriteTextFormat1 *iface, DWRITE_PARAGRAPH_ALIGNMENT alignment)
{ {
struct dwrite_textlayout *This = impl_layout_form_IDWriteTextFormat1(iface); struct dwrite_textlayout *This = impl_layout_form_IDWriteTextFormat1(iface);
FIXME("(%p)->(%d): stub\n", This, alignment); BOOL changed;
return E_NOTIMPL; HRESULT hr;
TRACE("(%p)->(%d)\n", This, alignment);
hr = format_set_paralignment(&This->format, alignment, &changed);
if (FAILED(hr))
return hr;
/* if layout is not ready there's nothing to align */
if (changed && !(This->recompute & RECOMPUTE_EFFECTIVE_RUNS))
layout_apply_par_alignment(This);
return S_OK;
} }
static HRESULT WINAPI dwritetextformat1_layout_SetWordWrapping(IDWriteTextFormat1 *iface, DWRITE_WORD_WRAPPING wrapping) static HRESULT WINAPI dwritetextformat1_layout_SetWordWrapping(IDWriteTextFormat1 *iface, DWRITE_WORD_WRAPPING wrapping)
@ -3974,14 +4043,8 @@ static HRESULT WINAPI dwritetextformat_SetTextAlignment(IDWriteTextFormat1 *ifac
static HRESULT WINAPI dwritetextformat_SetParagraphAlignment(IDWriteTextFormat1 *iface, DWRITE_PARAGRAPH_ALIGNMENT alignment) static HRESULT WINAPI dwritetextformat_SetParagraphAlignment(IDWriteTextFormat1 *iface, DWRITE_PARAGRAPH_ALIGNMENT alignment)
{ {
struct dwrite_textformat *This = impl_from_IDWriteTextFormat1(iface); struct dwrite_textformat *This = impl_from_IDWriteTextFormat1(iface);
TRACE("(%p)->(%d)\n", This, alignment); TRACE("(%p)->(%d)\n", This, alignment);
return format_set_paralignment(&This->format, alignment, NULL);
if ((UINT32)alignment > DWRITE_PARAGRAPH_ALIGNMENT_CENTER)
return E_INVALIDARG;
This->format.paralign = alignment;
return S_OK;
} }
static HRESULT WINAPI dwritetextformat_SetWordWrapping(IDWriteTextFormat1 *iface, DWRITE_WORD_WRAPPING wrapping) static HRESULT WINAPI dwritetextformat_SetWordWrapping(IDWriteTextFormat1 *iface, DWRITE_WORD_WRAPPING wrapping)

View File

@ -2972,6 +2972,100 @@ static void test_SetTextAlignment(void)
IDWriteFactory_Release(factory); IDWriteFactory_Release(factory);
} }
static void test_SetParagraphAlignment(void)
{
static const WCHAR strW[] = {'a',0};
DWRITE_TEXT_METRICS metrics;
IDWriteTextFormat *format;
IDWriteTextLayout *layout;
IDWriteFactory *factory;
DWRITE_PARAGRAPH_ALIGNMENT v;
DWRITE_LINE_METRICS lines[1];
UINT32 count;
HRESULT hr;
factory = create_factory();
hr = IDWriteFactory_CreateTextFormat(factory, tahomaW, NULL, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL, 12.0, enusW, &format);
ok(hr == S_OK, "got 0x%08x\n", hr);
v = IDWriteTextFormat_GetParagraphAlignment(format);
ok(v == DWRITE_PARAGRAPH_ALIGNMENT_NEAR, "got %d\n", v);
hr = IDWriteFactory_CreateTextLayout(factory, strW, 1, format, 500.0, 100.0, &layout);
ok(hr == S_OK, "got 0x%08x\n", hr);
v = IDWriteTextLayout_GetParagraphAlignment(layout);
ok(v == DWRITE_PARAGRAPH_ALIGNMENT_NEAR, "got %d\n", v);
hr = IDWriteTextLayout_SetParagraphAlignment(layout, DWRITE_PARAGRAPH_ALIGNMENT_FAR);
ok(hr == S_OK, "got 0x%08x\n", hr);
hr = IDWriteTextLayout_SetParagraphAlignment(layout, DWRITE_PARAGRAPH_ALIGNMENT_FAR);
ok(hr == S_OK, "got 0x%08x\n", hr);
v = IDWriteTextFormat_GetParagraphAlignment(format);
ok(v == DWRITE_PARAGRAPH_ALIGNMENT_NEAR, "got %d\n", v);
v = IDWriteTextLayout_GetParagraphAlignment(layout);
ok(v == DWRITE_PARAGRAPH_ALIGNMENT_FAR, "got %d\n", v);
hr = IDWriteTextLayout_SetParagraphAlignment(layout, DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
ok(hr == S_OK, "got 0x%08x\n", hr);
v = IDWriteTextLayout_GetParagraphAlignment(layout);
ok(v == DWRITE_PARAGRAPH_ALIGNMENT_CENTER, "got %d\n", v);
count = 0;
hr = IDWriteTextLayout_GetLineMetrics(layout, lines, 1, &count);
ok(hr == S_OK, "got 0x%08x\n", hr);
ok(count == 1, "got %u\n", count);
/* maxheight is 100, near alignment */
hr = IDWriteTextLayout_SetParagraphAlignment(layout, DWRITE_PARAGRAPH_ALIGNMENT_NEAR);
ok(hr == S_OK, "got 0x%08x\n", hr);
hr = IDWriteTextLayout_GetMetrics(layout, &metrics);
ok(hr == S_OK, "got 0x%08x\n", hr);
ok(metrics.top == 0.0, "got %.2f\n", metrics.top);
ok(metrics.height == lines[0].height, "got %.2f\n", metrics.height);
ok(metrics.layoutHeight == 100.0, "got %.2f\n", metrics.layoutHeight);
ok(metrics.lineCount == 1, "got %d\n", metrics.lineCount);
/* maxwidth is 100, far alignment */
hr = IDWriteTextLayout_SetParagraphAlignment(layout, DWRITE_PARAGRAPH_ALIGNMENT_FAR);
ok(hr == S_OK, "got 0x%08x\n", hr);
hr = IDWriteTextLayout_GetMetrics(layout, &metrics);
ok(hr == S_OK, "got 0x%08x\n", hr);
ok(metrics.top == metrics.layoutHeight - metrics.height, "got %.2f\n", metrics.top);
ok(metrics.height == lines[0].height, "got %.2f\n", metrics.height);
ok(metrics.layoutHeight == 100.0, "got %.2f\n", metrics.layoutHeight);
ok(metrics.lineCount == 1, "got %d\n", metrics.lineCount);
IDWriteTextLayout_Release(layout);
/* initially created with centered alignment */
hr = IDWriteTextFormat_SetParagraphAlignment(format, DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
ok(hr == S_OK, "got 0x%08x\n", hr);
hr = IDWriteFactory_CreateTextLayout(factory, strW, 1, format, 500.0, 100.0, &layout);
ok(hr == S_OK, "got 0x%08x\n", hr);
hr = IDWriteTextLayout_GetMetrics(layout, &metrics);
ok(hr == S_OK, "got 0x%08x\n", hr);
ok(metrics.top == (metrics.layoutHeight - lines[0].height) / 2, "got %.2f\n", metrics.top);
ok(metrics.height == lines[0].height, "got %.2f\n", metrics.height);
ok(metrics.lineCount == 1, "got %d\n", metrics.lineCount);
IDWriteTextLayout_Release(layout);
IDWriteTextFormat_Release(format);
IDWriteFactory_Release(factory);
}
START_TEST(layout) START_TEST(layout)
{ {
static const WCHAR ctrlstrW[] = {0x202a,0}; static const WCHAR ctrlstrW[] = {0x202a,0};
@ -3013,6 +3107,7 @@ START_TEST(layout)
test_SetDrawingEffect(); test_SetDrawingEffect();
test_GetLineMetrics(); test_GetLineMetrics();
test_SetTextAlignment(); test_SetTextAlignment();
test_SetParagraphAlignment();
IDWriteFactory_Release(factory); IDWriteFactory_Release(factory);
} }