Code Monkey home page Code Monkey logo

Comments (6)

behdad avatar behdad commented on July 17, 2024

The correct code should do something like this:


const short lowestCode = 253;

short value;

 

if(value > (3 * lowestCode) || value <= -lowestCode)

{

      os.write(253);

      os.write((byte)(absValue >> 8));

      os.write((byte)(absValue & 0xff));

}

else

{

 

  if (value < 0) {

      os.write(250); /* flip the sign */

  value = (short)-value;

  }

  else if (value >= lowestCode) {

    value = (short)(value - lowestCode);

    if (value >= lowestCode) {

      value = (short)(value - lowestCode);

      os.write(254);

    }

    else {

      os.write(255);

    }

  }

  os.write((byte)value);

}

from sfntly.

nyshadhr9 avatar nyshadhr9 commented on July 17, 2024

There are some values which can be encoded in multiple ways. For Ex:
500 can be encoded as both:
FE (oneMoreByteCode2) 00 (decimal 0)
or
FF(oneMoreByteCode1) FA (decimal 250)

Is there a preference for one or the other? Or are we just concerned about minimizing the number of bytes taken to encode, so either one works.

from sfntly.

behdad avatar behdad commented on July 17, 2024

Yeah I see the overlaps. No idea. @taylorb-monotype can you advise, which way does Monotype encode / recommend encoding these? Thanks.

from sfntly.

taylorb-monotype avatar taylorb-monotype commented on July 17, 2024

Our code to write 255Shorts is the same as what Behdad has indicated above.
With this code I don't think there is any ambiguity. The choice is made by the code above.
For example, you have the following encodings:

value encoding
0 00
1 01
...
249 f9
250 ff 00
251 ff 01
...
499 ff f9
500 fe 00
501 fe 01
...
749 fe f9
750 fd 02 ee
751 fd 02 ef
...
767 fd 02 ff
768 fd 03 00
769 fd 03 01
...
etc.

from sfntly.

behdad avatar behdad commented on July 17, 2024

Our code to write 255Shorts is the same as what Behdad has indicated above.

Thanks for confirming.

With this code I don't think there is any ambiguity.

There is ambiguity in the spec, but yeah sticking to the above scheme works.

The choice is made by the code above.
For example, you have the following encodings:

value encoding
0 00
1 01
...
249 f9
250 ff 00
251 ff 01
...
499 ff f9
500 fe 00
501 fe 01
...
749 fe f9
750 fd 02 ee
751 fd 02 ef
...
767 fd 02 ff
768 fd 03 00
769 fd 03 01
...
etc.

from sfntly.

nyshadhr9 avatar nyshadhr9 commented on July 17, 2024

Thank you for clarifying :)

My concern with the above approach was that, 750-755 take up 3 bytes whereas they could be encoded in 2 bytes as FE FA - FE FF. But I guess that can be sacrificed for a simpler and consistent solution.

from sfntly.

Related Issues (20)

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.