GithubHelp home page GithubHelp logo

font-metrics's People

Contributors

mkudamatsu avatar

Watchers

 avatar  avatar

font-metrics's Issues

Validate user inputs

cf. Use the short-circuit evaluation so that the error message won't be shown before the user enters any value. See: https://www.evernote.com/l/AFPu_A_V5OFIBJDAJNg4ZKLxbshq0mOLfWM
https://www.evernote.com/l/AFMxkVlPI2VPTbv-PTG42R5uCOXIVspxiYA (the example from TestingJavaScript.com)

Based on Sonny Recio's article entitled "How to use HTML5 form validations with React", we need the following to validate each input field.

Step 1
Enclose all the input elements with the form element along with its noValidate attribute (to disable HTML5's own form validation for which we cannot style the error message)

<form className="form-inside-input"
          onSubmit={this.onSubmit}
          noValidate >
      {/* form inputs here */}
    </form>

And add the min attribute to the input elements

<input type="text"
           name=“xHeightBox"
           min="1"
           required />

Step 2
In the .changeHandler() function, access to event.target.validity.valid

  • This property is true if the user input satisfies all the constraints.
    or to `event.target.validity.rangeUnderflow
  • This property is true if the user input value goes below the minimum value
  • See MDN on ValidityState.rangeUnderFlow for detail
    And store this boolean value as xHeightBox.valid (true if it's valid).

Step 3
Create a component of the error message

const ErrorValidationLabel = ({ errMsg }) => (
  <label htmlFor="" style={{ color: "red" }}>
    {errMsg}
  </label>
);

Step 4
If the constraint is not satisfied, show the error message

const renderValidationError = xHeightBox.valid ? (
    ""
  ) : (
    <ErrorValidationLabel errMsg={'X-height needs to be larger than or equal to 1.'} />
  );

For the time being, we only deal with critical ones

  • Font size being zero
  • X-height scale being zero
    because these two values are used as a denominator in the calculation of line-height.

Feature 2-2: Setting x-height renders sample paragraphs accordingly

The font-size property value in React has to be either a number or a string that ends with px.

We have stored the font-size value as a string value without px at the end so that it will be shown in the font-size box, for issue #14.

Consequently, we use the Number() method to convert the string value into a number.

Set the default font metrics to render sample paragraphs

Why needed?

Without the default font-size, uploading a font file won't show any sample paragraphs because the font-size` is effectively zero.

  • This is not an optimal user experience due to the obvious visual feedback to the user's action.

Without the default font-family, the user without a font file cannot tell what this webapp is about. Once they find it useful, they may visit us again with their own font file.

Feature 1-2: Uploading a font file renders sample paragraphs in the uploaded font

Initially, we used the HTML canvas to render sample paragraphs. This has the following issues:

  • We do not know how to write the test code for checking the content of the canvas element
  • It seems difficult to change the rendering when the user chooses a x-height value.

Consequently, we instead use CSS Font Load API, to set the font property directly to a paragraph element.

Feature 2-1: Setting x-height returns the font-size property value

[x-height in px] = (sxHeight/ unitsPerEm) * [font-size in px]

Consequently, we have
[font-size in px] = (unitsPerEm / sxHeight) * [x-height in px]

We keep the fractional values up to 4 decimal places.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.