Eliminate the distinction between layout and display pixels per unit

This commit is contained in:
Patrick Walton 2017-11-30 09:51:07 -08:00
parent 86660572bd
commit 95a1dd0195
5 changed files with 28 additions and 49 deletions

View File

@ -922,7 +922,7 @@ class ThreeDAtlasRenderer extends TextRenderer {
protected compositeIfNecessary(): void {}
private calculateGlyphTexCoords(): void {
const displayPixelsPerUnit = this.displayPixelsPerUnit;
const pixelsPerUnit = this.pixelsPerUnit;
const glyphCount = this.renderContext.atlasGlyphs.length;
const font = this.renderContext.font;
const hint = this.createHint();
@ -932,7 +932,7 @@ class ThreeDAtlasRenderer extends TextRenderer {
for (let glyphIndex = 0; glyphIndex < glyphCount; glyphIndex++) {
const glyph = this.renderContext.atlasGlyphs[glyphIndex];
const glyphPixelOrigin = glyph.calculateSubpixelOrigin(displayPixelsPerUnit);
const glyphPixelOrigin = glyph.calculateSubpixelOrigin(pixelsPerUnit);
const glyphMetrics = font.metricsForGlyph(glyph.glyphKey.id);
if (glyphMetrics == null)
continue;
@ -940,7 +940,7 @@ class ThreeDAtlasRenderer extends TextRenderer {
const glyphUnitMetrics = new UnitMetrics(glyphMetrics, glmatrix.vec2.create());
const atlasGlyphRect = calculatePixelRectForGlyph(glyphUnitMetrics,
glyphPixelOrigin,
displayPixelsPerUnit,
pixelsPerUnit,
hint);
this.glyphSizes.push(glmatrix.vec2.clone([

View File

@ -578,7 +578,6 @@ class ReferenceTestRenderer extends Renderer {
const textRun = unwrapNull(appController.textRun);
const glyphID = textRun.glyphIDs[glyphIndex];
return textRun.pixelRectForGlyphAt(glyphIndex,
this.pixelsPerUnit,
this.pixelsPerUnit,
hint,
this.stemDarkeningAmount,
@ -611,7 +610,6 @@ class ReferenceTestRenderer extends Renderer {
const textRun = unwrapNull(appController.textRun);
const glyphID = textRun.glyphIDs[0];
const pixelRect = textRun.pixelRectForGlyphAt(0,
this.pixelsPerUnit,
this.pixelsPerUnit,
hint,
glmatrix.vec2.create(),

View File

@ -246,7 +246,7 @@ class TextDemoController extends DemoAppController<TextDemoView> {
this.view.then(view => view.renderer.relayoutText());
}
get layoutPixelsPerUnit(): number {
get pixelsPerUnit(): number {
return this._fontSize / this.font.opentypeFont.unitsPerEm;
}
@ -304,8 +304,8 @@ class TextDemoView extends DemoView implements TextRenderContext {
return this.appController.pathCount;
}
get layoutPixelsPerUnit(): number {
return this.appController.layoutPixelsPerUnit;
get pixelsPerUnit(): number {
return this.appController.pixelsPerUnit;
}
get useHinting(): boolean {
@ -505,8 +505,7 @@ class TextDemoRenderer extends TextRenderer {
const glyphIndices = new Uint32Array(totalGlyphCount * 6);
const hint = this.createHint();
const displayPixelsPerUnit = this.displayPixelsPerUnit;
const layoutPixelsPerUnit = this.layoutPixelsPerUnit;
const pixelsPerUnit = this.pixelsPerUnit;
let globalGlyphIndex = 0;
for (const run of this.layout.textFrame.runs) {
@ -514,8 +513,7 @@ class TextDemoRenderer extends TextRenderer {
glyphIndex < run.glyphIDs.length;
glyphIndex++, globalGlyphIndex++) {
const rect = run.pixelRectForGlyphAt(glyphIndex,
layoutPixelsPerUnit,
displayPixelsPerUnit,
pixelsPerUnit,
hint,
this.stemDarkeningAmount,
SUBPIXEL_GRANULARITY);
@ -546,8 +544,7 @@ class TextDemoRenderer extends TextRenderer {
private buildGlyphs(): void {
const font = this.renderContext.font;
const glyphStore = this.renderContext.glyphStore;
const layoutPixelsPerUnit = this.layoutPixelsPerUnit;
const displayPixelsPerUnit = this.displayPixelsPerUnit;
const pixelsPerUnit = this.pixelsPerUnit;
const textFrame = this.layout.textFrame;
const hint = this.createHint();
@ -565,8 +562,7 @@ class TextDemoRenderer extends TextRenderer {
for (const run of textFrame.runs) {
for (let glyphIndex = 0; glyphIndex < run.glyphIDs.length; glyphIndex++) {
const pixelRect = run.pixelRectForGlyphAt(glyphIndex,
layoutPixelsPerUnit,
displayPixelsPerUnit,
pixelsPerUnit,
hint,
this.stemDarkeningAmount,
SUBPIXEL_GRANULARITY);
@ -579,7 +575,7 @@ class TextDemoRenderer extends TextRenderer {
continue;
const subpixel = run.subpixelForGlyphAt(glyphIndex,
layoutPixelsPerUnit,
pixelsPerUnit,
hint,
SUBPIXEL_GRANULARITY);
const glyphKey = new GlyphKey(glyphID, subpixel);
@ -600,8 +596,7 @@ class TextDemoRenderer extends TextRenderer {
const atlasGlyphs = this.renderContext.atlasGlyphs;
const hint = this.createHint();
const layoutPixelsPerUnit = this.layoutPixelsPerUnit;
const displayPixelsPerUnit = this.displayPixelsPerUnit;
const pixelsPerUnit = this.pixelsPerUnit;
const atlasGlyphKeys = atlasGlyphs.map(atlasGlyph => atlasGlyph.glyphKey.sortKey);
@ -615,7 +610,7 @@ class TextDemoRenderer extends TextRenderer {
const textGlyphID = run.glyphIDs[glyphIndex];
const subpixel = run.subpixelForGlyphAt(glyphIndex,
layoutPixelsPerUnit,
pixelsPerUnit,
hint,
SUBPIXEL_GRANULARITY);
@ -635,10 +630,10 @@ class TextDemoRenderer extends TextRenderer {
this.stemDarkeningAmount);
const atlasGlyphPixelOrigin =
atlasGlyph.calculateSubpixelOrigin(displayPixelsPerUnit);
atlasGlyph.calculateSubpixelOrigin(pixelsPerUnit);
const atlasGlyphRect = calculatePixelRectForGlyph(atlasGlyphUnitMetrics,
atlasGlyphPixelOrigin,
displayPixelsPerUnit,
pixelsPerUnit,
hint);
const atlasGlyphBL = atlasGlyphRect.slice(0, 2) as glmatrix.vec2;
const atlasGlyphTR = atlasGlyphRect.slice(2, 4) as glmatrix.vec2;

View File

@ -92,14 +92,10 @@ export abstract class TextRenderer extends Renderer {
return 0.0;
}
protected get layoutPixelsPerUnit(): number {
protected get pixelsPerUnit(): number {
return this.renderContext.fontSize / this.renderContext.font.opentypeFont.unitsPerEm;
}
protected get displayPixelsPerUnit(): number {
return this.layoutPixelsPerUnit;
}
protected get worldTransform(): glmatrix.mat4 {
const transform = glmatrix.mat4.create();
glmatrix.mat4.translate(transform, transform, [-1.0, -1.0, 0.0]);
@ -108,10 +104,8 @@ export abstract class TextRenderer extends Renderer {
}
protected get stemDarkeningAmount(): glmatrix.vec2 {
if (this.stemDarkening === 'dark') {
return computeStemDarkeningAmount(this.renderContext.fontSize,
this.layoutPixelsPerUnit);
}
if (this.stemDarkening === 'dark')
return computeStemDarkeningAmount(this.renderContext.fontSize, this.pixelsPerUnit);
return glmatrix.vec2.create();
}
@ -153,7 +147,7 @@ export abstract class TextRenderer extends Renderer {
pathBoundingRects(objectIndex: number): Float32Array {
const pathCount = this.pathCount;
const atlasGlyphs = this.renderContext.atlasGlyphs;
const pixelsPerUnit = this.displayPixelsPerUnit;
const pixelsPerUnit = this.pixelsPerUnit;
const font = this.renderContext.font;
const hint = this.createHint();
@ -201,7 +195,7 @@ export abstract class TextRenderer extends Renderer {
protected buildAtlasGlyphs(atlasGlyphs: AtlasGlyph[]): void {
const font = this.renderContext.font;
const displayPixelsPerUnit = this.displayPixelsPerUnit;
const pixelsPerUnit = this.pixelsPerUnit;
const hint = this.createHint();
atlasGlyphs.sort((a, b) => a.glyphKey.sortKey - b.glyphKey.sortKey);
@ -212,7 +206,7 @@ export abstract class TextRenderer extends Renderer {
this.renderContext.atlasGlyphs = atlasGlyphs;
this.renderContext.atlas.layoutGlyphs(atlasGlyphs,
font,
displayPixelsPerUnit,
pixelsPerUnit,
hint,
this.stemDarkeningAmount);
@ -237,7 +231,7 @@ export abstract class TextRenderer extends Renderer {
protected pathTransformsForObject(objectIndex: number): PathTransformBuffers<Float32Array> {
const pathCount = this.pathCount;
const atlasGlyphs = this.renderContext.atlasGlyphs;
const pixelsPerUnit = this.displayPixelsPerUnit;
const pixelsPerUnit = this.pixelsPerUnit;
const rotationAngle = this.rotationAngle;
// FIXME(pcwalton): This is a hack that tries to preserve the vertical extents of the glyph
@ -288,7 +282,7 @@ export abstract class TextRenderer extends Renderer {
protected createHint(): Hint {
return new Hint(this.renderContext.font,
this.displayPixelsPerUnit,
this.pixelsPerUnit,
this.renderContext.useHinting);
}

View File

@ -118,37 +118,29 @@ export class TextRun {
}
}
calculatePixelOriginForGlyphAt(index: number,
layoutPixelsPerUnit: number,
hint: Hint):
calculatePixelOriginForGlyphAt(index: number, pixelsPerUnit: number, hint: Hint):
glmatrix.vec2 {
const textGlyphOrigin = glmatrix.vec2.clone(this.origin);
textGlyphOrigin[0] += this.advances[index];
glmatrix.vec2.scale(textGlyphOrigin, textGlyphOrigin, layoutPixelsPerUnit);
glmatrix.vec2.scale(textGlyphOrigin, textGlyphOrigin, pixelsPerUnit);
return textGlyphOrigin;
}
pixelRectForGlyphAt(index: number,
layoutPixelsPerUnit: number,
displayPixelsPerUnit: number,
pixelsPerUnit: number,
hint: Hint,
stemDarkeningAmount: glmatrix.vec2,
subpixelGranularity: number):
glmatrix.vec4 {
const metrics = unwrapNull(this.font.metricsForGlyph(this.glyphIDs[index]));
const unitMetrics = new UnitMetrics(metrics, stemDarkeningAmount);
const textGlyphOrigin = this.calculatePixelOriginForGlyphAt(index,
layoutPixelsPerUnit,
hint);
const textGlyphOrigin = this.calculatePixelOriginForGlyphAt(index, pixelsPerUnit, hint);
textGlyphOrigin[0] *= subpixelGranularity;
glmatrix.vec2.round(textGlyphOrigin, textGlyphOrigin);
textGlyphOrigin[0] /= subpixelGranularity;
return calculatePixelRectForGlyph(unitMetrics,
textGlyphOrigin,
displayPixelsPerUnit,
hint);
return calculatePixelRectForGlyph(unitMetrics, textGlyphOrigin, pixelsPerUnit, hint);
}
subpixelForGlyphAt(index: number,