Class ShapingEngine

java.lang.Object
com.mta.tehreer.sfnt.ShapingEngine
All Implemented Interfaces:
Disposable

public class ShapingEngine extends Object implements Disposable
The ShapingEngine class represents text shaping engine.
  • Constructor Details

    • ShapingEngine

      public ShapingEngine()
      Constructs a shaping engine object.
  • Method Details

    • finalizable

      @NonNull public static ShapingEngine finalizable(@NonNull ShapingEngine shapingEngine)
      Wraps a shaping engine object into a finalizable instance which is guaranteed to be disposed automatically by the GC when no longer in use. After calling this method, dispose() should not be called on either original object or returned object. Calling dispose() on returned object will throw an UnsupportedOperationException.

      Note: The behavior is undefined if the passed-in object is already disposed or wrapped into another finalizable instance.

      Parameters:
      shapingEngine - The shaping engine object to wrap into a finalizable instance.
      Returns:
      The finalizable instance of the passed-in shaping engine object.
    • isFinalizable

      public static boolean isFinalizable(@NonNull ShapingEngine shapingEngine)
      Checks whether a shaping engine object is finalizable or not.
      Parameters:
      shapingEngine - The shaping engine object to check.
      Returns:
      true if the passed-in shaping engine object is finalizable, false otherwise.
    • getScriptDirection

      public static WritingDirection getScriptDirection(int scriptTag)
      Returns the default writing direction of a script.
      Parameters:
      scriptTag - The tag of the script whose default direction is returned.
      Returns:
      The default writing direction of the script identified by scriptTag.
    • getTypeface

      public Typeface getTypeface()
      Returns the typeface which this shaping engine will use for shaping text.
      Returns:
      This current typeface.
    • setTypeface

      public void setTypeface(Typeface typeface)
      Sets the typeface which this shaping engine will use for shaping text.
      Parameters:
      typeface - The new typeface.
    • getTypeSize

      public float getTypeSize()
      Returns the type size which this shaping engine will use for shaping text.
      Returns:
      This current type size.
    • setTypeSize

      public void setTypeSize(float typeSize)
      Sets the type size which this shaping engine will use for shaping text.
      Parameters:
      typeSize - The new type size.
    • getScriptTag

      public int getScriptTag()
      Returns the script tag which this shaping engine will use for shaping text. The default value is 'DFLT'.
      Returns:
      The current script tag.
    • setScriptTag

      public void setScriptTag(int scriptTag)
      Sets the script tag which this shaping engine will use for shaping text. The default value is 'DFLT'.

      A tag can be created from string by using SfntTag.make(String) method.

      Parameters:
      scriptTag - The new script tag.
    • getLanguageTag

      public int getLanguageTag()
      Returns the language tag which this shaping engine will use for shaping text. The default value is 'dflt'.
      Returns:
      The current language tag.
    • setLanguageTag

      public void setLanguageTag(int languageTag)
      Sets the language tag which this shaping engine will use for shaping text. The default value is 'dflt'.

      A tag can be created from string by using SfntTag.make(String) method.

      Parameters:
      languageTag - The new language tag.
    • getOpenTypeFeatures

      @NonNull public Set<OpenTypeFeature> getOpenTypeFeatures()
      Returns the user-specified open type feature settings.
      Returns:
      A set of open type features.
    • setOpenTypeFeatures

      public void setOpenTypeFeatures(@NonNull Set<OpenTypeFeature> features)
      Sets the user-specified open type feature settings. If the value of a feature is set to zero, it would be disabled provided that it is not a required feature of the chosen script. If the value of a feature is greater than zero, it would be enabled. In case of an alternate feature, this value would be used to pick the alternate glyph at this position.
      Parameters:
      features - A set of open type features.
    • getWritingDirection

      @NonNull public WritingDirection getWritingDirection()
      Returns the direction in which this shaping engine will place the resultant glyphs. The default value is WritingDirection.LEFT_TO_RIGHT.
      Returns:
      The current writing direction.
    • setWritingDirection

      public void setWritingDirection(@NonNull WritingDirection writingDirection)
      Sets the direction in which this shaping engine will place the resultant glyphs. The default value is WritingDirection.LEFT_TO_RIGHT.

      The value of writingDirection must reflect the rendering direction of source script so that cursive and mark glyphs are placed at appropriate locations. It should not be confused with the direction of a bidirectional run as that may not reflect the script direction if overridden explicitly.

      Parameters:
      writingDirection - The new writing direction.
    • getShapingOrder

      @NonNull public ShapingOrder getShapingOrder()
      Returns the order in which this shaping engine will process the text. The default value is ShapingOrder.FORWARD.
      Returns:
      The current shaping order.
    • setShapingOrder

      public void setShapingOrder(@NonNull ShapingOrder shapingOrder)
      Sets the order in which this shaping engine will process the text. The default value is ShapingOrder.FORWARD.

      This method provides a convenient way to shape a bidirectional run whose direction is opposite to that of script. For example, if the direction of a run, 'car' is explicitly set as right-to-left, backward order will automatically read it as 'rac' without reordering the original text.

      Parameters:
      shapingOrder - The new shaping order.
    • shapeText

      @NonNull public ShapingResult shapeText(@NonNull String text, int fromIndex, int toIndex)
      Shapes the specified range of text into glyphs.

      The output glyphs in the ShapingResult object flow visually in writing direction. For left-to-right direction, the position of pen is incremented with glyph's advance after rendering it. Similarly, for right-to-left direction, the position of pen is decremented with glyph's advance after rendering it.

      Parameters:
      text - The text to shape into glyphs.
      fromIndex - The index of the first character (inclusive) to be shaped.
      toIndex - The index of the last character (exclusive) to be shaped.
      Returns:
      A non-finalizable instance of a ShapingResult object.
      Throws:
      IllegalStateException - if current typeface is null.
      NullPointerException - if text is null.
      IllegalArgumentException - if fromIndex is negative, or toIndex is greater than text.length(), or fromIndex is greater than toIndex
    • dispose

      public void dispose()
      Description copied from interface: Disposable
      Releases the native memory of this object. Failing to call this method will cause memory leaks.

      Note: The behavior is undefined if this method is called more than once on the same object.

      Specified by:
      dispose in interface Disposable
    • toString

      @NonNull public String toString()
      Overrides:
      toString in class Object