1cb93a386Sopenharmony_ciText API Overview 2cb93a386Sopenharmony_ci============= 3cb93a386Sopenharmony_ci*2D Graphics* is a broad area. It consists of a host of related, but different ideas and primitives 4cb93a386Sopenharmony_ci- rectangles, lines, paths 5cb93a386Sopenharmony_ci- images (often scaled, transformed, filtered) 6cb93a386Sopenharmony_ci- text (also scalable and transformable) 7cb93a386Sopenharmony_ci- ... 8cb93a386Sopenharmony_ci 9cb93a386Sopenharmony_ciNative (desktop, mobile) frameworks combine these primitives with common rendering constructs 10cb93a386Sopenharmony_ci- transforms and clipping 11cb93a386Sopenharmony_ci- colors, gradients, patterns 12cb93a386Sopenharmony_ci- blending and colorspaces 13cb93a386Sopenharmony_ci 14cb93a386Sopenharmony_ciOne primitive in particular stands out: **Text**. It can be drawn with the same common constructs as rects and paths, but it has another component unrelated to actual drawing, a 'processing' step that is surprisingly complex and expensive: **Shaping**. The native frameworks recognized this, and offer a separate set of APIs just around this: 15cb93a386Sopenharmony_ci- CoreGraphics --> CoreText 16cb93a386Sopenharmony_ci- Direct2D/X --> DirectWrite 17cb93a386Sopenharmony_ci- Skia --> SkShaper 18cb93a386Sopenharmony_ci 19cb93a386Sopenharmony_ciThis proposal embraces this pattern, and aims to expose this functionality to the Web. 20cb93a386Sopenharmony_ci 21cb93a386Sopenharmony_ci 22cb93a386Sopenharmony_ci## Background 23cb93a386Sopenharmony_ci 24cb93a386Sopenharmony_ciPart of this complexity lies in language and alphabets themselves, and part is due to how our system of fonts and font formats have evolved. This document does not claim to detail either of these, but will briefly touch on each to motivate the proposed API suite. 25cb93a386Sopenharmony_ci 26cb93a386Sopenharmony_ci## Language and Alphabets 27cb93a386Sopenharmony_ci 28cb93a386Sopenharmony_ci### "The office 建造 is in München." 29cb93a386Sopenharmony_ci 30cb93a386Sopenharmony_ciThis simple sentence is illustrative of some of the richness (i.e. complexity) of displaying text. Some examples 31cb93a386Sopenharmony_ci- The 'ffi" in office (or possibly just the "fi") **could** be drawn with a specialized form called a [ligature](https://en.wikipedia.org/wiki/Ligature_(writing)). 32cb93a386Sopenharmony_ci- The "ü" could have been drawn as a single shape, or it could have been drawn by separately drawing "u" followed by drawing the umlaut (¨) on top. 33cb93a386Sopenharmony_ci- The third word may not be reprsentable in the same font as the other words, so a different font may have been needed. 34cb93a386Sopenharmony_ci 35cb93a386Sopenharmony_ciThis example in no way attempts to be complete or exhaustive, but it suggests the processing needed to go from just the 'letters' in a sentence, to the actual 'shapes' that must be found or composed from various fonts to finally be able to draw it. 36cb93a386Sopenharmony_ci 37cb93a386Sopenharmony_ci## Fonts and Styles 38cb93a386Sopenharmony_ci 39cb93a386Sopenharmony_ciThis [site](https://www.w3schools.com/css/css_font.asp) has an excellent overview of some of the richess of specifying a "font" in CSS. While different native platforms have some variance, most support the following *mapping* from a description of a font, to the actual resource / file / blob containing the drawn shapes. 40cb93a386Sopenharmony_ci 41cb93a386Sopenharmony_ci- family-name : "Helvetica", "Times New Roman", "Lucida Typewriter Sans", ... 42cb93a386Sopenharmony_ci- font-style : bold, italic 43cb93a386Sopenharmony_ci- font-weight : 100 .. 900 44cb93a386Sopenharmony_ci- font-stretch : ultra-condensed .. ultra-expanded 45cb93a386Sopenharmony_ci- font-variation-setting : 'wght' 850 or 'wdth' 25 or ... 46cb93a386Sopenharmony_ci 47cb93a386Sopenharmony_ciAll of these attributes (and more) go into the browser's search for the best matching font resource/file/blob. Let's call the resulting 'resource' a **Typeface**. Note: If the font supports variation-settings, our definition will also include the specific settings (e.g. a font resource + variation settings). 48cb93a386Sopenharmony_ci 49cb93a386Sopenharmony_ci## Typefaces and Glyphs 50cb93a386Sopenharmony_ci 51cb93a386Sopenharmony_ciWe define a Typeface to be a coherent collection of shapes in a given typograph style (including any variation settings). Typically a typeface is stored in a single file or blob (e.g. an OpenType file). 52cb93a386Sopenharmony_ci 53cb93a386Sopenharmony_ciAlong with a single Typeface we define the individual *shapes* or images in the typeface as **Glyphs**. Glyphs represent the smallest independent drawing element within a typeface. By convention, glyphs are identified by an *index*, ranging from 0 to however many are in a particular typeface. 54cb93a386Sopenharmony_ci 55cb93a386Sopenharmony_ciDetermining what glyphs, in what order and positions, are needed to represent a set of letters, is the heart of **Shaping**, and it is this process, and resulting typefaces + positioned glyphs, that we propose exposing to Web Apps. 56cb93a386Sopenharmony_ci 57cb93a386Sopenharmony_ci## Summary 58cb93a386Sopenharmony_ci 59cb93a386Sopenharmony_ciWe posit that drawing *internationally correct* Text is critical to most Web Apps, and that it is both complex to get correct, and can be computationally expensive. We propose exposing this processing to apps, providing them with results that can be efficiently drawn / animated. 60cb93a386Sopenharmony_ci 61cb93a386Sopenharmony_ciThe core [Shaping APIs](text_shaper.md) are detailed here. 62cb93a386Sopenharmony_ci 63cb93a386Sopenharmony_ciAssocated [Canvas2D extensions](text_c2d.md) are detailed here. 64cb93a386Sopenharmony_ci 65cb93a386Sopenharmony_ciNote: it is an explicit goal to **not** tie Shaping or its results to Canvas2D. We envision multiple scenarios where a framework or app will want to shape text, but not need a Canvas2D context . 66cb93a386Sopenharmony_ci- drawn using WebGL or WebGPU 67cb93a386Sopenharmony_ci- drawn using DOM (but utilizing line-breaking and metrics results) 68cb93a386Sopenharmony_ci- drawn using a bespoke (i.e. wasm) renderer 69cb93a386Sopenharmony_ci 70cb93a386Sopenharmony_ciWe are also proposing a lower level interface, one that just addresses exposing [unicode properties](uni_characterize.md). 71cb93a386Sopenharmony_ci 72cb93a386Sopenharmony_ci 73cb93a386Sopenharmony_ci## Contributors: 74cb93a386Sopenharmony_ci [mikerreed](https://github.com/mikerreed), 75