Skip to content

Commit

Permalink
add more per-platform font options to FontInstancePlatformOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
lsalzman committed Oct 6, 2017
1 parent 8f0e0c6 commit e11297a
Show file tree
Hide file tree
Showing 19 changed files with 596 additions and 289 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion webrender/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "webrender"
version = "0.52.0"
version = "0.52.1"
authors = ["Glenn Watson <[email protected]>"]
license = "MPL-2.0"
repository = "https://github.com/servo/webrender"
Expand Down
2 changes: 1 addition & 1 deletion webrender/res/cs_text_run.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ void main(void) {
// Glyphs size is already in device-pixels.
// The render task origin is in device-pixels. Offset that by
// the glyph offset, relative to its primitive bounding rect.
vec2 size = res.uv_rect.zw - res.uv_rect.xy;
vec2 size = (res.uv_rect.zw - res.uv_rect.xy) * res.scale;
vec2 local_pos = glyph.offset + vec2(res.offset.x, -res.offset.y) / uDevicePixelRatio;
vec2 origin = prim.task.render_target_origin +
uDevicePixelRatio * (local_pos + shadow.offset - shadow_geom.local_rect.p0);
Expand Down
3 changes: 2 additions & 1 deletion webrender/res/prim_shared.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -627,11 +627,12 @@ struct GlyphResource {
vec4 uv_rect;
float layer;
vec2 offset;
float scale;
};

GlyphResource fetch_glyph_resource(int address) {
vec4 data[2] = fetch_from_resource_cache_2(address);
return GlyphResource(data[0], data[1].x, data[1].yz);
return GlyphResource(data[0], data[1].x, data[1].yz, data[1].w);
}

struct ImageResource {
Expand Down
11 changes: 6 additions & 5 deletions webrender/res/ps_text_run.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void main(void) {
vec2(res.offset.x, -res.offset.y) / uDevicePixelRatio;

RectWithSize local_rect = RectWithSize(local_pos,
(res.uv_rect.zw - res.uv_rect.xy) / uDevicePixelRatio);
(res.uv_rect.zw - res.uv_rect.xy) * res.scale / uDevicePixelRatio);

#ifdef WR_FEATURE_TRANSFORM
TransformVertexInfo vi = write_transform_vertex(local_rect,
Expand All @@ -57,7 +57,7 @@ void main(void) {
vec2 st0 = res.uv_rect.xy / texture_size;
vec2 st1 = res.uv_rect.zw / texture_size;

vColor = text.color;
vColor = vec4(text.color.rgb * text.color.a, text.color.a);
vUv = vec3(mix(st0, st1, f), res.layer);
vUvBorder = (res.uv_rect + vec4(0.5, 0.5, -0.5, -0.5)) / texture_size.xyxy;
}
Expand All @@ -71,13 +71,14 @@ void main(void) {
oFragColor = texture(sColor0, tc);
#else
vec4 color = texture(sColor0, tc) * vColor;
float alpha = 1.0;
#ifdef WR_FEATURE_TRANSFORM
float a = 0.0;
init_transform_fs(vLocalPos, a);
color.a *= a;
alpha *= a;
#endif
color.a = min(color.a, do_clip());
oFragColor = color;
alpha = min(alpha, do_clip());
oFragColor = color * alpha;
#endif
}
#endif
45 changes: 10 additions & 35 deletions webrender/src/frame_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use api::{GlyphInstance, GlyphOptions, GradientStop, HitTestFlags, HitTestItem,
use api::{ImageKey, ImageRendering, ItemRange, ItemTag, LayerPoint, LayerPrimitiveInfo, LayerRect};
use api::{LayerSize, LayerToScrollTransform, LayerVector2D, LayoutVector2D, LineOrientation};
use api::{LineStyle, LocalClip, POINT_RELATIVE_TO_PIPELINE_VIEWPORT, PipelineId, RepeatMode};
use api::{ScrollSensitivity, SubpixelDirection, Shadow, TileOffset, TransformStyle};
use api::{ScrollSensitivity, Shadow, TileOffset, TransformStyle};
use api::{WorldPixel, WorldPoint, YuvColorSpace, YuvData, device_length};
use app_units::Au;
use border::ImageBorderSegment;
Expand Down Expand Up @@ -1132,19 +1132,18 @@ impl FrameBuilder {
// TODO(gw): Use a proper algorithm to select
// whether this item should be rendered with
// subpixel AA!
let mut default_render_mode = self.config
let mut render_mode = self.config
.default_font_render_mode
.limit_by(font.render_mode);
if let Some(options) = glyph_options {
default_render_mode = default_render_mode.limit_by(options.render_mode);
render_mode = render_mode.limit_by(options.render_mode);
}

// There are some conditions under which we can't use
// subpixel text rendering, even if enabled.
let mut normal_render_mode = default_render_mode;
if normal_render_mode == FontRenderMode::Subpixel {
if render_mode == FontRenderMode::Subpixel {
if color.a != 1.0 {
normal_render_mode = FontRenderMode::Alpha;
render_mode = FontRenderMode::Alpha;
}

// text on a stacking context that has filters
Expand All @@ -1155,36 +1154,17 @@ impl FrameBuilder {
if let Some(sc_index) = self.stacking_context_stack.last() {
let stacking_context = &self.stacking_context_store[sc_index.0];
if stacking_context.composite_ops.count() > 0 {
normal_render_mode = FontRenderMode::Alpha;
render_mode = FontRenderMode::Alpha;
}
}
}

let color = match font.render_mode {
FontRenderMode::Bitmap => ColorF::new(1.0, 1.0, 1.0, 1.0),
FontRenderMode::Subpixel |
FontRenderMode::Alpha |
FontRenderMode::Mono => *color,
};

// Shadows never use subpixel AA, but need to respect the alpha/mono flag
// for reftests.
let (shadow_render_mode, subpx_dir) = match default_render_mode {
FontRenderMode::Subpixel | FontRenderMode::Alpha => {
// TODO(gw): Expose subpixel direction in API once WR supports
// vertical text runs.
(FontRenderMode::Alpha, font.subpx_dir)
}
FontRenderMode::Mono => (FontRenderMode::Mono, SubpixelDirection::None),
FontRenderMode::Bitmap => (FontRenderMode::Bitmap, font.subpx_dir),
};

let prim_font = FontInstance::new(
font.font_key,
font.size,
color,
normal_render_mode,
subpx_dir,
*color,
render_mode,
font.subpx_dir,
font.platform_options,
font.variations.clone(),
font.synthetic_italics,
Expand All @@ -1195,9 +1175,7 @@ impl FrameBuilder {
glyph_count,
glyph_gpu_blocks: Vec::new(),
glyph_keys: Vec::new(),
shadow_render_mode,
offset: run_offset,
color: color,
};

// Text shadows that have a blur radius of 0 need to be rendered as normal
Expand All @@ -1215,16 +1193,13 @@ impl FrameBuilder {
let shadow = picture_prim.as_shadow();
if shadow.blur_radius == 0.0 {
let mut text_prim = prim.clone();
if font.render_mode != FontRenderMode::Bitmap {
text_prim.font.color = shadow.color.into();
}
text_prim.font.color = shadow.color.into();
// If we have translucent text, we need to ensure it won't go
// through the subpixel blend mode, which doesn't work with
// traditional alpha blending.
if shadow.color.a != 1.0 {
text_prim.font.render_mode = text_prim.font.render_mode.limit_by(FontRenderMode::Alpha);
}
text_prim.color = shadow.color;
text_prim.offset += shadow.offset;
fast_shadow_prims.push(text_prim);
}
Expand Down
1 change: 1 addition & 0 deletions webrender/src/glyph_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct CachedGlyphInfo {
pub glyph_bytes: Arc<Vec<u8>>,
pub size: DeviceUintSize,
pub offset: DevicePoint,
pub scale: f32,
}

pub type GlyphKeyCache = ResourceClassCache<GlyphKey, Option<CachedGlyphInfo>>;
Expand Down
38 changes: 31 additions & 7 deletions webrender/src/glyph_rasterizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#[cfg(test)]
use api::{ColorF, FontRenderMode, IdNamespace, LayoutPoint, SubpixelDirection};
use api::{ColorF, IdNamespace, LayoutPoint};
use api::{DevicePoint, DeviceUintSize, FontInstance};
use api::{FontKey, FontTemplate};
use api::{GlyphDimensions, GlyphKey};
use api::{FontKey, FontTemplate, FontRenderMode, ColorU};
use api::{GlyphDimensions, GlyphKey, SubpixelDirection};
use api::{ImageData, ImageDescriptor, ImageFormat};
#[cfg(test)]
use app_units::Au;
Expand Down Expand Up @@ -144,6 +144,29 @@ impl GlyphRasterizer {
self.fonts_to_remove.push(font_key);
}

pub fn prepare_font(&self, font: &mut FontInstance) {
// In alpha/mono mode, the color of the font is irrelevant.
// Forcing it to black in those cases saves rasterizing glyphs
// of different colors when not needed.
match font.render_mode {
FontRenderMode::Mono | FontRenderMode::Bitmap => {
font.color = ColorU::new(255, 255, 255, 255);
// Subpixel positioning is disabled in mono and bitmap modes.
font.subpx_dir = SubpixelDirection::None;
}
FontRenderMode::Alpha => {
font.color = ColorU::new(255, 255, 255, 255);
}
FontRenderMode::Subpixel => {
// In subpixel mode, we only actually need the color if preblending
// is used in the font backend.
if !FontContext::has_gamma_correct_subpixel_aa() {
font.color = ColorU::new(255, 255, 255, 255);
}
}
}
}

pub fn request_glyphs(
&mut self,
glyph_cache: &mut GlyphCache,
Expand Down Expand Up @@ -183,7 +206,7 @@ impl GlyphRasterizer {
},
TextureFilter::Linear,
ImageData::Raw(glyph_info.glyph_bytes.clone()),
[glyph_info.offset.x, glyph_info.offset.y],
[glyph_info.offset.x, glyph_info.offset.y, glyph_info.scale],
None,
gpu_cache,
);
Expand Down Expand Up @@ -246,10 +269,10 @@ impl GlyphRasterizer {
.get_glyph_dimensions(font, glyph_key)
}

pub fn is_bitmap_font(&self, font_key: FontKey) -> bool {
pub fn is_bitmap_font(&self, font: &FontInstance) -> bool {
self.font_contexts
.lock_shared_context()
.is_bitmap_font(font_key)
.is_bitmap_font(font)
}

pub fn get_glyph_index(&mut self, font_key: FontKey, ch: char) -> Option<u32> {
Expand Down Expand Up @@ -312,7 +335,7 @@ impl GlyphRasterizer {
},
TextureFilter::Linear,
ImageData::Raw(glyph_bytes.clone()),
[glyph.left, glyph.top],
[glyph.left, glyph.top, glyph.scale],
None,
gpu_cache,
);
Expand All @@ -321,6 +344,7 @@ impl GlyphRasterizer {
glyph_bytes,
size: DeviceUintSize::new(glyph.width, glyph.height),
offset: DevicePoint::new(glyph.left, glyph.top),
scale: glyph.scale,
})
} else {
None
Expand Down
15 changes: 10 additions & 5 deletions webrender/src/platform/macos/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct RasterizedGlyph {
pub left: f32,
pub width: u32,
pub height: u32,
pub scale: f32,
pub bytes: Vec<u8>,
}

Expand All @@ -52,6 +53,7 @@ impl RasterizedGlyph {
left: 0.0,
width: 0,
height: 0,
scale: 1.0,
bytes: vec![],
}
}
Expand Down Expand Up @@ -422,18 +424,20 @@ impl FontContext {
}
}

pub fn is_bitmap_font(&mut self, font_key: FontKey) -> bool {
match self.get_ct_font(font_key, Au(16 * 60), &[]) {
pub fn is_bitmap_font(&mut self, font: &FontInstance) -> bool {
match self.get_ct_font(font.font_key, font.size, &font.variations) {
Some(ref ct_font) => {
let traits = ct_font.symbolic_traits();
(traits & kCTFontColorGlyphsTrait) != 0
}
None => {
false
}
None => false,
}
}

pub fn has_gamma_correct_subpixel_aa() -> bool {
true
}

pub fn rasterize_glyph(
&mut self,
font: &FontInstance,
Expand Down Expand Up @@ -585,6 +589,7 @@ impl FontContext {
top: metrics.rasterized_ascent as f32,
width: metrics.rasterized_width,
height: metrics.rasterized_height,
scale: 1.0,
bytes: rasterized_pixels,
})
}
Expand Down
Loading

0 comments on commit e11297a

Please sign in to comment.